https://github.com/zhaoqi5 created 
https://github.com/llvm/llvm-project/pull/156315

None

>From 3e7ee8d89f636af50c595476dd953a5f624160eb Mon Sep 17 00:00:00 2001
From: Qi Zhao <zhaoq...@loongson.cn>
Date: Mon, 1 Sep 2025 19:11:09 +0800
Subject: [PATCH] [Driver][LoongArch] Enable linker relaxation by default for
 loongarch64

---
 .../lib/Driver/ToolChains/Arch/LoongArch.cpp  | 29 +++++++++----------
 clang/test/Driver/loongarch-features.c        |  2 +-
 clang/test/Driver/loongarch-march.c           | 20 ++++++-------
 clang/test/Driver/loongarch-relax-features.c  |  2 +-
 4 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp 
b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
index ee7b0d10c24ba..156ea03045569 100644
--- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
@@ -134,22 +134,19 @@ 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");
-    }
+  // -mrelax is default, unless -mno-relax is specified.
+  // FIXME: Only for loongarch64, loongarch32 has not been fully verified.
+  if (Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax,
+                   Triple.isLoongArch64() ? true : false)) {
+    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 if (Args.getLastArg(options::OPT_mno_relax)) {
+    Features.push_back("-relax");
   }
 
   std::string ArchName;
diff --git a/clang/test/Driver/loongarch-features.c 
b/clang/test/Driver/loongarch-features.c
index 90634bbcf0035..6f40ed71c2c0e 100644
--- a/clang/test/Driver/loongarch-features.c
+++ b/clang/test/Driver/loongarch-features.c
@@ -2,7 +2,7 @@
 // RUN: %clang --target=loongarch64 -S -emit-llvm %s -o - | FileCheck %s 
--check-prefix=LA64
 
 // LA32: "target-features"="+32bit"
-// LA64: "target-features"="+64bit,+d,+f,+lsx,+ual"
+// LA64: "target-features"="+64bit,+d,+f,+lsx,+relax,+ual"
 
 int foo(void) {
   return 3;
diff --git a/clang/test/Driver/loongarch-march.c 
b/clang/test/Driver/loongarch-march.c
index b52cdb330716f..87cdffd8d3a02 100644
--- a/clang/test/Driver/loongarch-march.c
+++ b/clang/test/Driver/loongarch-march.c
@@ -21,39 +21,39 @@
 
 // CC1-LOONGARCH64: "-target-cpu" "loongarch64"
 // CC1-LOONGARCH64-NOT: "-target-feature"
-// CC1-LOONGARCH64: "-target-feature" "+64bit" "-target-feature" "+f" 
"-target-feature" "+d" "-target-feature" "+ual"
+// CC1-LOONGARCH64: "-target-feature" "+relax" "-target-feature" "+64bit" 
"-target-feature" "+f" "-target-feature" "+d" "-target-feature" "+ual"
 // CC1-LOONGARCH64-NOT: "-target-feature"
 // CC1-LOONGARCH64: "-target-abi" "lp64d"
 
 // CC1-LA464: "-target-cpu" "la464"
 // CC1-LA464-NOT: "-target-feature"
-// CC1-LA464: "-target-feature" "+64bit" "-target-feature" "+f" 
"-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+lasx" 
"-target-feature" "+ual"
+// CC1-LA464: "-target-feature" "+relax" "-target-feature" "+64bit" 
"-target-feature" "+f" "-target-feature" "+d" "-target-feature" "+lsx" 
"-target-feature" "+lasx" "-target-feature" "+ual"
 // CC1-LA464-NOT: "-target-feature"
 // CC1-LA464: "-target-abi" "lp64d"
 
 // CC1-LA64V1P0: "-target-cpu" "loongarch64"
 // CC1-LA64V1P0-NOT: "-target-feature"
-// CC1-LA64V1P0: "-target-feature" "+64bit" "-target-feature" "+d" 
"-target-feature" "+lsx" "-target-feature" "+ual"
+// CC1-LA64V1P0: "-target-feature" "+relax" "-target-feature" "+64bit" 
"-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+ual"
 // CC1-LA64V1P0-NOT: "-target-feature"
 // CC1-LA64V1P0: "-target-abi" "lp64d"
 
 // CC1-LA64V1P1: "-target-cpu" "loongarch64"
 // CC1-LA64V1P1-NOT: "-target-feature"
-// CC1-LA64V1P1: "-target-feature" "+64bit" "-target-feature" "+d" 
"-target-feature" "+lsx" "-target-feature" "+ual" "-target-feature" "+frecipe" 
"-target-feature" "+lam-bh" "-target-feature" "+lamcas" "-target-feature" 
"+ld-seq-sa" "-target-feature" "+div32" "-target-feature" "+scq"
+// CC1-LA64V1P1: "-target-feature" "+relax" "-target-feature" "+64bit" 
"-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+ual" 
"-target-feature" "+frecipe" "-target-feature" "+lam-bh" "-target-feature" 
"+lamcas" "-target-feature" "+ld-seq-sa" "-target-feature" "+div32" 
"-target-feature" "+scq"
 // CC1-LA64V1P1-NOT: "-target-feature"
 // CC1-LA64V1P1: "-target-abi" "lp64d"
 
 // CC1-LA664: "-target-cpu" "la664"
 // CC1-LA664-NOT: "-target-feature"
-// CC1-LA664: "-target-feature" "+64bit" "-target-feature" "+f" 
"-target-feature" "+d" "-target-feature" "+lsx" "-target-feature" "+lasx" 
"-target-feature" "+ual" "-target-feature" "+frecipe" "-target-feature" 
"+lam-bh" "-target-feature" "+lamcas" "-target-feature" "+ld-seq-sa" 
"-target-feature" "+div32" "-target-feature" "+scq"
+// CC1-LA664: "-target-feature" "+relax" "-target-feature" "+64bit" 
"-target-feature" "+f" "-target-feature" "+d" "-target-feature" "+lsx" 
"-target-feature" "+lasx" "-target-feature" "+ual" "-target-feature" "+frecipe" 
"-target-feature" "+lam-bh" "-target-feature" "+lamcas" "-target-feature" 
"+ld-seq-sa" "-target-feature" "+div32" "-target-feature" "+scq"
 // CC1-LA664-NOT: "-target-feature"
 // CC1-LA664: "-target-abi" "lp64d"
 
-// IR-LOONGARCH64: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" 
{{.*}}"target-features"="+64bit,+d,+f,+ual"
-// IR-LA464: attributes #[[#]] ={{.*}}"target-cpu"="la464" 
{{.*}}"target-features"="+64bit,+d,+f,+lasx,+lsx,+ual"
-// IR-LA64V1P0: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" 
{{.*}}"target-features"="+64bit,+d,+lsx,+ual"
-// IR-LA64V1P1: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" 
{{.*}}"target-features"="+64bit,+d,+div32,+frecipe,+lam-bh,+lamcas,+ld-seq-sa,+lsx,+scq,+ual"
-// IR-LA664: attributes #[[#]] ={{.*}}"target-cpu"="la664" 
{{.*}}"target-features"="+64bit,+d,+div32,+f,+frecipe,+lam-bh,+lamcas,+lasx,+ld-seq-sa,+lsx,+scq,+ual"
+// IR-LOONGARCH64: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" 
{{.*}}"target-features"="+64bit,+d,+f,+relax,+ual"
+// IR-LA464: attributes #[[#]] ={{.*}}"target-cpu"="la464" 
{{.*}}"target-features"="+64bit,+d,+f,+lasx,+lsx,+relax,+ual"
+// IR-LA64V1P0: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" 
{{.*}}"target-features"="+64bit,+d,+lsx,+relax,+ual"
+// IR-LA64V1P1: attributes #[[#]] ={{.*}}"target-cpu"="loongarch64" 
{{.*}}"target-features"="+64bit,+d,+div32,+frecipe,+lam-bh,+lamcas,+ld-seq-sa,+lsx,+relax,+scq,+ual"
+// IR-LA664: attributes #[[#]] ={{.*}}"target-cpu"="la664" 
{{.*}}"target-features"="+64bit,+d,+div32,+f,+frecipe,+lam-bh,+lamcas,+lasx,+ld-seq-sa,+lsx,+relax,+scq,+ual"
 
 int foo(void) {
   return 3;
diff --git a/clang/test/Driver/loongarch-relax-features.c 
b/clang/test/Driver/loongarch-relax-features.c
index c6ef15a8fcff0..2f032516462df 100644
--- a/clang/test/Driver/loongarch-relax-features.c
+++ b/clang/test/Driver/loongarch-relax-features.c
@@ -17,7 +17,7 @@
 // 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"
+// LA64: "target-features"="+64bit,+d,+f,+lsx,+relax,+ual"
 
 // LA32-NORELAX: "target-features"="+32bit,-relax"
 // LA64-NORELAX: "target-features"="+64bit,+d,+f,+lsx,+ual,-relax"

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to