[clang] [Clang][Driver] Support linker relaxation options for LoongArch (PR #123587)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 21 19:46:35 PST 2025


https://github.com/zhaoqi5 updated https://github.com/llvm/llvm-project/pull/123587

>From 8608d84983580afd13a791bece3ca7b05872b36b Mon Sep 17 00:00:00 2001
From: Qi Zhao <zhaoqi01 at loongson.cn>
Date: Mon, 20 Jan 2025 19:09:22 +0800
Subject: [PATCH 1/3] [Clang][Driver] Pass -X and forward --no-relax to linker
 for LoongArch

---
 clang/lib/Driver/ToolChains/Gnu.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index e5db1b2f1550b8..5557ca4bf4491f 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -422,7 +422,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     return;
   }
 
-  if (Triple.isRISCV()) {
+  if (Triple.isRISCV() || Triple.isLoongArch()) {
     CmdArgs.push_back("-X");
     if (Args.hasArg(options::OPT_mno_relax))
       CmdArgs.push_back("--no-relax");

>From 154904c053c0b7696a492d1832d340e9a980207c Mon Sep 17 00:00:00 2001
From: Qi Zhao <zhaoqi01 at loongson.cn>
Date: Wed, 22 Jan 2025 11:24:33 +0800
Subject: [PATCH 2/3] support for relax option and error for -gsplit-dwarf
 -mrelax

---
 .../clang/Basic/DiagnosticDriverKinds.td      |  3 ++
 .../lib/Driver/ToolChains/Arch/LoongArch.cpp  | 19 +++++++++++
 clang/test/Driver/loongarch-relax-features.c  | 33 +++++++++++++++++++
 3 files changed, 55 insertions(+)
 create mode 100644 clang/test/Driver/loongarch-relax-features.c

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 42c39ac6606c7f..612f7e330ba51e 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -808,6 +808,9 @@ def err_drv_loongarch_invalid_simd_option_combination : Error<
 def err_drv_loongarch_invalid_msimd_EQ : Error<
   "invalid argument '%0' to -msimd=; must be one of: none, lsx, lasx">;
 
+def err_drv_loongarch_unsupported_with_linker_relaxation : Error<
+  "%0 is unsupported with LoongArch linker relaxation (-mrelax)">;
+
 def err_drv_expand_response_file : Error<
   "failed to expand response file: %0">;
 
diff --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
index bbd9397aa2378a..bafe241151763e 100644
--- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "LoongArch.h"
+#include "../Clang.h"
 #include "ToolChains/CommonArgs.h"
 #include "clang/Basic/DiagnosticDriver.h"
 #include "clang/Driver/Driver.h"
@@ -134,6 +135,24 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
       (!Args.hasArgNoClaim(clang::driver::options::OPT_march_EQ)))
     Features.push_back("+lsx");
 
+  // FIXME: Now we must use -mrelax to enable relax, maybe -mrelax will be set
+  // as default in the future.
+  if (const Arg *A =
+          Args.getLastArg(options::OPT_mrelax, options::OPT_mno_relax)) {
+    if (A->getOption().matches(options::OPT_mrelax)) {
+      Features.push_back("+relax");
+      // -gsplit-dwarf -mrelax requires DW_AT_high_pc/DW_AT_ranges/... indexing
+      // into .debug_addr, which is currently not implemented.
+      Arg *A;
+      if (getDebugFissionKind(D, Args, A) != DwarfFissionKind::None)
+        D.Diag(
+            clang::diag::err_drv_loongarch_unsupported_with_linker_relaxation)
+            << A->getAsString(Args);
+    } else {
+      Features.push_back("-relax");
+    }
+  }
+
   std::string ArchName;
   const Arg *MArch = Args.getLastArg(options::OPT_march_EQ);
   if (MArch)
diff --git a/clang/test/Driver/loongarch-relax-features.c b/clang/test/Driver/loongarch-relax-features.c
new file mode 100644
index 00000000000000..c6ef15a8fcff0e
--- /dev/null
+++ b/clang/test/Driver/loongarch-relax-features.c
@@ -0,0 +1,33 @@
+/// Test -m[no-]relax options.
+
+// 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
+// RUN: %clang --target=loongarch32 -mno-relax -S -emit-llvm %s -o - | FileCheck %s --check-prefix=LA32-NORELAX
+// RUN: %clang --target=loongarch64 -mno-relax -S -emit-llvm %s -o - | FileCheck %s --check-prefix=LA64-NORELAX
+// RUN: %clang --target=loongarch32 -mrelax -S -emit-llvm %s -o - | FileCheck %s --check-prefix=LA32-RELAX
+// RUN: %clang --target=loongarch64 -mrelax -S -emit-llvm %s -o - | FileCheck %s --check-prefix=LA64-RELAX
+
+/// Error when using -gsplit-dwarf with linker relaxation (-mrelax).
+
+// RUN: %clang -### -c --target=loongarch32 -mno-relax -g -gsplit-dwarf %s 2>&1 | FileCheck %s --check-prefix=SPLIT-DWARF
+// RUN: not %clang -c --target=loongarch32-linux-gnu -mrelax -gsplit-dwarf %s 2>&1 | FileCheck %s --check-prefix=ERR-SPLIT-DWARF
+// RUN: not %clang -c --target=loongarch32 -mrelax -gsplit-dwarf=single %s 2>&1 | FileCheck %s --check-prefix=ERR-SPLIT-DWARF
+// RUN: %clang -### -c --target=loongarch64 -mno-relax -g -gsplit-dwarf %s 2>&1 | FileCheck %s --check-prefix=SPLIT-DWARF
+// RUN: not %clang -c --target=loongarch64-linux-gnu -mrelax -gsplit-dwarf %s 2>&1 | FileCheck %s --check-prefix=ERR-SPLIT-DWARF
+// RUN: not %clang -c --target=loongarch64 -mrelax -gsplit-dwarf=single %s 2>&1 | FileCheck %s --check-prefix=ERR-SPLIT-DWARF
+
+// LA32: "target-features"="+32bit"
+// LA64: "target-features"="+64bit,+d,+f,+lsx,+ual"
+
+// LA32-NORELAX: "target-features"="+32bit,-relax"
+// LA64-NORELAX: "target-features"="+64bit,+d,+f,+lsx,+ual,-relax"
+
+// LA32-RELAX: "target-features"="+32bit,+relax"
+// LA64-RELAX: "target-features"="+64bit,+d,+f,+lsx,+relax,+ual"
+
+// SPLIT-DWARF:     "-split-dwarf-file"
+// ERR-SPLIT-DWARF: error: -gsplit-dwarf{{.*}} is unsupported with LoongArch linker relaxation (-mrelax)
+
+int foo(void) {
+  return 3;
+}

>From fed6fd76b1c98fa4bc584e6a340b8701441dd3b0 Mon Sep 17 00:00:00 2001
From: Qi Zhao <zhaoqi01 at loongson.cn>
Date: Wed, 22 Jan 2025 11:40:18 +0800
Subject: [PATCH 3/3] update

---
 clang/lib/Driver/ToolChains/Gnu.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 5557ca4bf4491f..1c653e1caac723 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -422,7 +422,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     return;
   }
 
-  if (Triple.isRISCV() || Triple.isLoongArch()) {
+  if (Triple.isLoongArch() || Triple.isRISCV()) {
     CmdArgs.push_back("-X");
     if (Args.hasArg(options::OPT_mno_relax))
       CmdArgs.push_back("--no-relax");



More information about the cfe-commits mailing list