[Clang][LoongArch] Pass the -mabi and -target-abi options to as and cc1as respectively
authorWeining Lu <luweining@loongson.cn>
Tue, 16 May 2023 09:43:59 +0000 (17:43 +0800)
committerWeining Lu <luweining@loongson.cn>
Tue, 16 May 2023 13:10:15 +0000 (21:10 +0800)
This change is necessary to set correct EFlags according to the
options (-m*-float and -mabi=) passed to clang when input is assembly.

Note: `-mabi=` is not documented by `as`.
```
$ as --version
GNU assembler (GNU Binutils) 2.40.50.20230316
...
$ as --target-help
LARCH options:
```

But we can see gcc invokes `as` and passes the `-mabi=` option when compiling C or assembly.
```
$ gcc -c a.c -v 2>&1 -msoft-float | grep "as -v"
 as -v -mabi=lp64s -o a.o /tmp/ccFrxzZi.s
$ gcc -c a.s -v 2>&1 -msoft-float | grep "as -v"
 as -v -mabi=lp64s -o a.o a.s
```

Reviewed By: xen0n

Differential Revision: https://reviews.llvm.org/D150537

clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Driver/ToolChains/Clang.h
clang/lib/Driver/ToolChains/Gnu.cpp
clang/test/Driver/loongarch-as.s [new file with mode: 0644]
clang/test/Driver/loongarch-ias.s [new file with mode: 0644]

index a230bc4..f057e21 100644 (file)
@@ -7945,6 +7945,14 @@ void ClangAs::AddX86TargetArgs(const ArgList &Args,
   }
 }
 
+void ClangAs::AddLoongArchTargetArgs(const ArgList &Args,
+                                     ArgStringList &CmdArgs) const {
+  CmdArgs.push_back("-target-abi");
+  CmdArgs.push_back(loongarch::getLoongArchABI(getToolChain().getDriver(), Args,
+                                               getToolChain().getTriple())
+                        .data());
+}
+
 void ClangAs::AddRISCVTargetArgs(const ArgList &Args,
                                ArgStringList &CmdArgs) const {
   const llvm::Triple &Triple = getToolChain().getTriple();
@@ -8143,6 +8151,11 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
     }
     break;
 
+  case llvm::Triple::loongarch32:
+  case llvm::Triple::loongarch64:
+    AddLoongArchTargetArgs(Args, CmdArgs);
+    break;
+
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
     AddRISCVTargetArgs(Args, CmdArgs);
index 12e13e2..64fc86b 100644 (file)
@@ -125,6 +125,8 @@ class LLVM_LIBRARY_VISIBILITY ClangAs : public Tool {
 public:
   ClangAs(const ToolChain &TC)
       : Tool("clang::as", "clang integrated assembler", TC) {}
+  void AddLoongArchTargetArgs(const llvm::opt::ArgList &Args,
+                              llvm::opt::ArgStringList &CmdArgs) const;
   void AddMIPSTargetArgs(const llvm::opt::ArgList &Args,
                          llvm::opt::ArgStringList &CmdArgs) const;
   void AddX86TargetArgs(const llvm::opt::ArgList &Args,
index b071632..3be65ba 100644 (file)
@@ -9,6 +9,7 @@
 #include "Gnu.h"
 #include "Arch/ARM.h"
 #include "Arch/CSKY.h"
+#include "Arch/LoongArch.h"
 #include "Arch/Mips.h"
 #include "Arch/PPC.h"
 #include "Arch/RISCV.h"
@@ -859,6 +860,13 @@ void tools::gnutools::Assembler::ConstructJob(Compilation &C,
 
     break;
   }
+  // TODO: handle loongarch32.
+  case llvm::Triple::loongarch64: {
+    StringRef ABIName =
+        loongarch::getLoongArchABI(D, Args, getToolChain().getTriple());
+    CmdArgs.push_back(Args.MakeArgString("-mabi=" + ABIName));
+    break;
+  }
   case llvm::Triple::mips:
   case llvm::Triple::mipsel:
   case llvm::Triple::mips64:
diff --git a/clang/test/Driver/loongarch-as.s b/clang/test/Driver/loongarch-as.s
new file mode 100644 (file)
index 0000000..6f6d87f
--- /dev/null
@@ -0,0 +1,15 @@
+/// This file checks options are correctly passed to as for LoongArch targets.
+
+/// Check `-mabi`.
+// RUN: %clang --target=loongarch64 -### -fno-integrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64d --check-prefix=ABI %s
+// RUN: %clang --target=loongarch64 -mabi=lp64d -### -fno-integrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64d --check-prefix=ABI %s
+// RUN: %clang --target=loongarch64 -mabi=lp64f -### -fno-integrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64f --check-prefix=ABI %s
+// RUN: %clang --target=loongarch64 -mabi=lp64s -### -fno-integrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64s --check-prefix=ABI %s
+
+// ALL: as
+
+// ABI: "-mabi=[[ABI]]"
diff --git a/clang/test/Driver/loongarch-ias.s b/clang/test/Driver/loongarch-ias.s
new file mode 100644 (file)
index 0000000..6fec9e6
--- /dev/null
@@ -0,0 +1,23 @@
+/// This file checks options are correctly passed to cc1as for LoongArch targets.
+
+/// Check `-target-abi`.
+// RUN: %clang --target=loongarch32 -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=ilp32d --check-prefix=ABI %s
+// RUN: %clang --target=loongarch32 -mabi=ilp32d -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=ilp32d --check-prefix=ABI %s
+// RUN: %clang --target=loongarch32 -mabi=ilp32f -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=ilp32f --check-prefix=ABI %s
+// RUN: %clang --target=loongarch32 -mabi=ilp32s -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=ilp32s --check-prefix=ABI %s
+// RUN: %clang --target=loongarch64 -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64d --check-prefix=ABI %s
+// RUN: %clang --target=loongarch64 -mabi=lp64d -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64d --check-prefix=ABI %s
+// RUN: %clang --target=loongarch64 -mabi=lp64f -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64f --check-prefix=ABI %s
+// RUN: %clang --target=loongarch64 -mabi=lp64s -### -fintegrated-as -c %s 2>&1 | \
+// RUN:   FileCheck -DABI=lp64s --check-prefix=ABI %s
+
+// ALL: -cc1as
+
+// ABI: "-target-abi" "[[ABI]]"