[Clang][LoongArch] Pass "f" and "d" features to cc1 to enable hard float
authorWeining Lu <luweining@loongson.cn>
Thu, 13 Oct 2022 06:37:53 +0000 (14:37 +0800)
committerWeining Lu <luweining@loongson.cn>
Thu, 13 Oct 2022 12:00:29 +0000 (20:00 +0800)
On LoongArch, currently neither of "f" and "d" feature is passed from
clang driver to cc1 by default. This means the backend generates code
for soft float.

In order to run programs in current LoongArch machines (hard float
environment) this patch temporarily enables "f" and "d" features.

In future, we should conditionally turn on these features depend on
various clang options, e.g. -mdouble-float, -msingle-float,
-msoft-float and -mfpu.

clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
clang/lib/Driver/ToolChains/Arch/LoongArch.h
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/Driver/loongarch-default-features.c [new file with mode: 0644]

index f8cda26..d364a2e 100644 (file)
@@ -26,3 +26,14 @@ StringRef loongarch::getLoongArchABI(const ArgList &Args,
   // TODO: select appropiate ABI.
   return Triple.getArch() == llvm::Triple::loongarch32 ? "ilp32d" : "lp64d";
 }
+
+void loongarch::getLoongArchTargetFeatures(const Driver &D,
+                                           const llvm::Triple &Triple,
+                                           const ArgList &Args,
+                                           std::vector<StringRef> &Features) {
+  // FIXME: hornor various clang options that may affect target features, e.g.
+  // -march/-mtune/-mdouble-float/-msingle-float/-msoft-float/-mfpu. See:
+  // https://loongson.github.io/LoongArch-Documentation/LoongArch-toolchain-conventions-EN.html
+  Features.push_back("+f");
+  Features.push_back("+d");
+}
index a128e61..eb09419 100644 (file)
@@ -19,6 +19,10 @@ namespace tools {
 namespace loongarch {
 StringRef getLoongArchABI(const llvm::opt::ArgList &Args,
                           const llvm::Triple &Triple);
+
+void getLoongArchTargetFeatures(const Driver &D, const llvm::Triple &Triple,
+                                const llvm::opt::ArgList &Args,
+                                std::vector<llvm::StringRef> &Features);
 } // end namespace loongarch
 } // end namespace tools
 } // end namespace driver
index 83023a1..15f4527 100644 (file)
@@ -372,6 +372,10 @@ static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
   case llvm::Triple::csky:
     csky::getCSKYTargetFeatures(D, Triple, Args, CmdArgs, Features);
     break;
+  case llvm::Triple::loongarch32:
+  case llvm::Triple::loongarch64:
+    loongarch::getLoongArchTargetFeatures(D, Triple, Args, Features);
+    break;
   }
 
   for (auto Feature : unifyTargetFeatures(Features)) {
diff --git a/clang/test/Driver/loongarch-default-features.c b/clang/test/Driver/loongarch-default-features.c
new file mode 100644 (file)
index 0000000..833ee4f
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang --target=loongarch32 -S -emit-llvm %s -o - | FileCheck %s --check-prefix=LA32
+// RUN: %clang --target=loongarch64 -S -emit-llvm %s -o - | FileCheck %s --check-prefix=LA64
+
+// LA32: "target-features"="+d,+f"
+// LA64: "target-features"="+d,+f"
+
+/// Dummy function
+int foo(void) {
+  return  3;
+}