[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-03 Thread Kito Cheng via cfe-commits

kito-cheng wrote:

> > The GP relaxation in lld only works for medlow without PIC. Linux defaults 
> > to PIE usually doesn't it?
> 
> Get it. Does the gcc in OS distros also default to PIE?

That depend on distro, but most distro are default to PIE.

Single command to check: `gcc -v 2>&1|grep default-pie -i`

https://github.com/llvm/llvm-project/pull/201265
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-03 Thread Kito Cheng via cfe-commits


@@ -343,6 +343,16 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   CmdArgs.push_back("--no-relax");
   }
 
+  // Default-enable LLD's --relax-gp for RISC-V on Linux when LLD is used and
+  // linker relaxation is not disabled by -mno-relax.
+  if (Triple.isRISCV() && Triple.isOSLinux()) {
+bool IsLLD = false;
+ToolChain.GetLinkerPath(&IsLLD);
+if (IsLLD &&
+Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
+  CmdArgs.push_back("--relax-gp");
+  }

kito-cheng wrote:

Passing the `gp relax` flag and sending it to `-shared` shouldn't cause any 
problems, since the linker mainly checks whether the `__global_pointer$` symbol 
is defined and whether relocation is supported.

So I would incline just pass without checking it's 
-shared/-static/-fPIE/&$%^$^, that may just complicate the condition here.

https://github.com/llvm/llvm-project/pull/201265
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-03 Thread Pengcheng Wang via cfe-commits

wangpc-pp wrote:

> The GP relaxation in lld only works for medlow without PIC. Linux defaults to 
> PIE usually doesn't it?

Get it. Does the gcc in OS distros also default to PIE?

https://github.com/llvm/llvm-project/pull/201265
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-03 Thread Pengcheng Wang via cfe-commits


@@ -343,6 +343,16 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   CmdArgs.push_back("--no-relax");
   }
 
+  // Default-enable LLD's --relax-gp for RISC-V on Linux when LLD is used and
+  // linker relaxation is not disabled by -mno-relax.
+  if (Triple.isRISCV() && Triple.isOSLinux()) {
+bool IsLLD = false;
+ToolChain.GetLinkerPath(&IsLLD);
+if (IsLLD &&
+Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
+  CmdArgs.push_back("--relax-gp");
+  }

wangpc-pp wrote:

I don't know if we can just check if `-static`/`-shared` exists.

https://github.com/llvm/llvm-project/pull/201265
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-02 Thread Craig Topper via cfe-commits

topperc wrote:

The GP relaxation in lld only works for medlow without PIC. Linux defaults to 
PIE usually doesn't it?

https://github.com/llvm/llvm-project/pull/201265
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-02 Thread via cfe-commits

llvmorg-github-actions[bot] wrote:




@llvm/pr-subscribers-backend-risc-v

Author: Pengcheng Wang (wangpc-pp)


Changes

We have supported GP relaxation in https://reviews.llvm.org/D143673
for a long time, but we made it disabled by default.

This PR enables it in Clang driver when targeting Linux.

(Please remind me if I missed the context why we disabled it on Linux).


---
Full diff: https://github.com/llvm/llvm-project/pull/201265.diff


4 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Driver/ToolChains/Gnu.cpp (+10) 
- (modified) clang/test/Driver/riscv32-toolchain.c (+25) 
- (modified) clang/test/Driver/riscv64-toolchain.c (+25) 


``diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 132d9128a795b..d52076c037b76 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -812,6 +812,7 @@ RISC-V Support
 - Tenstorrent Ascalon D8 was renamed to Ascalon X. Use `tt-ascalon-x` with 
`-mcpu` or `-mtune`.
 - Intrinsics were added for the 'Zvabd` (RISC-V Integer Vector Absolute 
Difference) extension.
 - Intrinsics were added for the 'Zvzip` (Reordering Structured Data in Vector 
Registers) extension.
+- GP relaxation was enabled by default on Linux.
 
 CUDA/HIP Language Changes
 ^
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index 131dd725c7289..5be5045ea6252 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -343,6 +343,16 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   CmdArgs.push_back("--no-relax");
   }
 
+  // Default-enable LLD's --relax-gp for RISC-V on Linux when LLD is used and
+  // linker relaxation is not disabled by -mno-relax.
+  if (Triple.isRISCV() && Triple.isOSLinux()) {
+bool IsLLD = false;
+ToolChain.GetLinkerPath(&IsLLD);
+if (IsLLD &&
+Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
+  CmdArgs.push_back("--relax-gp");
+  }
+
   const bool IsShared = Args.hasArg(options::OPT_shared);
   if (IsShared)
 CmdArgs.push_back("-shared");
diff --git a/clang/test/Driver/riscv32-toolchain.c 
b/clang/test/Driver/riscv32-toolchain.c
index 04acf1e7edbe0..272e55c19df97 100644
--- a/clang/test/Driver/riscv32-toolchain.c
+++ b/clang/test/Driver/riscv32-toolchain.c
@@ -247,6 +247,31 @@
 // RUN:   | FileCheck -check-prefix=CHECK-RV32-GNU-RELAX %s
 // CHECK-RV32-GNU-RELAX-NOT: "--no-relax"
 
+/// Check that "--relax-gp" is forwarded to LLD for RISC-V on Linux by default.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-LLD-RELAXGP %s
+// CHECK-RV32-LLD-RELAXGP: "--relax-gp"
+
+/// Check that "--relax-gp" is NOT forwarded to GNU ld for RISC-V on Linux.
+// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-GNU-NO-RELAXGP %s
+// CHECK-RV32-GNU-NO-RELAXGP-NOT: "--relax-gp"
+
+/// Check that "--relax-gp" is NOT forwarded to LLD when -mno-relax is set.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie -mno-relax \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-LLD-NORELAX-NOGP %s
+// CHECK-RV32-LLD-NORELAX-NOGP: "--no-relax"
+// CHECK-RV32-LLD-NORELAX-NOGP-NOT: "--relax-gp"
+
 /// Check that "-static -pie" is forwarded to linker when "-static-pie" is used
 // RUN: %clang -static-pie -### %s -fuse-ld= \
 // RUN:   --target=riscv32-unknown-elf -rtlib=platform --unwindlib=platform \
diff --git a/clang/test/Driver/riscv64-toolchain.c 
b/clang/test/Driver/riscv64-toolchain.c
index 378f3d9db7bad..6820b1a34806d 100644
--- a/clang/test/Driver/riscv64-toolchain.c
+++ b/clang/test/Driver/riscv64-toolchain.c
@@ -203,6 +203,31 @@
 // RUN:   | FileCheck -check-prefix=CHECK-RV64-GNU-RELAX %s
 // CHECK-RV64-GNU-RELAX-NOT: "--no-relax"
 
+/// Check that "--relax-gp" is forwarded to LLD for RISC-V on Linux by default.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie \
+// RUN:   --target=riscv64-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=lp64 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | Fi

[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-02 Thread via cfe-commits

llvmorg-github-actions[bot] wrote:




@llvm/pr-subscribers-clang-driver

Author: Pengcheng Wang (wangpc-pp)


Changes

We have supported GP relaxation in https://reviews.llvm.org/D143673
for a long time, but we made it disabled by default.

This PR enables it in Clang driver when targeting Linux.

(Please remind me if I missed the context why we disabled it on Linux).


---
Full diff: https://github.com/llvm/llvm-project/pull/201265.diff


4 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Driver/ToolChains/Gnu.cpp (+10) 
- (modified) clang/test/Driver/riscv32-toolchain.c (+25) 
- (modified) clang/test/Driver/riscv64-toolchain.c (+25) 


``diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 132d9128a795b..d52076c037b76 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -812,6 +812,7 @@ RISC-V Support
 - Tenstorrent Ascalon D8 was renamed to Ascalon X. Use `tt-ascalon-x` with 
`-mcpu` or `-mtune`.
 - Intrinsics were added for the 'Zvabd` (RISC-V Integer Vector Absolute 
Difference) extension.
 - Intrinsics were added for the 'Zvzip` (Reordering Structured Data in Vector 
Registers) extension.
+- GP relaxation was enabled by default on Linux.
 
 CUDA/HIP Language Changes
 ^
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index 131dd725c7289..5be5045ea6252 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -343,6 +343,16 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   CmdArgs.push_back("--no-relax");
   }
 
+  // Default-enable LLD's --relax-gp for RISC-V on Linux when LLD is used and
+  // linker relaxation is not disabled by -mno-relax.
+  if (Triple.isRISCV() && Triple.isOSLinux()) {
+bool IsLLD = false;
+ToolChain.GetLinkerPath(&IsLLD);
+if (IsLLD &&
+Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
+  CmdArgs.push_back("--relax-gp");
+  }
+
   const bool IsShared = Args.hasArg(options::OPT_shared);
   if (IsShared)
 CmdArgs.push_back("-shared");
diff --git a/clang/test/Driver/riscv32-toolchain.c 
b/clang/test/Driver/riscv32-toolchain.c
index 04acf1e7edbe0..272e55c19df97 100644
--- a/clang/test/Driver/riscv32-toolchain.c
+++ b/clang/test/Driver/riscv32-toolchain.c
@@ -247,6 +247,31 @@
 // RUN:   | FileCheck -check-prefix=CHECK-RV32-GNU-RELAX %s
 // CHECK-RV32-GNU-RELAX-NOT: "--no-relax"
 
+/// Check that "--relax-gp" is forwarded to LLD for RISC-V on Linux by default.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-LLD-RELAXGP %s
+// CHECK-RV32-LLD-RELAXGP: "--relax-gp"
+
+/// Check that "--relax-gp" is NOT forwarded to GNU ld for RISC-V on Linux.
+// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-GNU-NO-RELAXGP %s
+// CHECK-RV32-GNU-NO-RELAXGP-NOT: "--relax-gp"
+
+/// Check that "--relax-gp" is NOT forwarded to LLD when -mno-relax is set.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie -mno-relax \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-LLD-NORELAX-NOGP %s
+// CHECK-RV32-LLD-NORELAX-NOGP: "--no-relax"
+// CHECK-RV32-LLD-NORELAX-NOGP-NOT: "--relax-gp"
+
 /// Check that "-static -pie" is forwarded to linker when "-static-pie" is used
 // RUN: %clang -static-pie -### %s -fuse-ld= \
 // RUN:   --target=riscv32-unknown-elf -rtlib=platform --unwindlib=platform \
diff --git a/clang/test/Driver/riscv64-toolchain.c 
b/clang/test/Driver/riscv64-toolchain.c
index 378f3d9db7bad..6820b1a34806d 100644
--- a/clang/test/Driver/riscv64-toolchain.c
+++ b/clang/test/Driver/riscv64-toolchain.c
@@ -203,6 +203,31 @@
 // RUN:   | FileCheck -check-prefix=CHECK-RV64-GNU-RELAX %s
 // CHECK-RV64-GNU-RELAX-NOT: "--no-relax"
 
+/// Check that "--relax-gp" is forwarded to LLD for RISC-V on Linux by default.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie \
+// RUN:   --target=riscv64-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=lp64 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | File

[clang] [RISCV] Enable GP relaxation for Linux (PR #201265)

2026-06-02 Thread Pengcheng Wang via cfe-commits

https://github.com/wangpc-pp created 
https://github.com/llvm/llvm-project/pull/201265

We have supported GP relaxation in https://reviews.llvm.org/D143673
for a long time, but we made it disabled by default.

This PR enables it in Clang driver when targeting Linux.

(Please remind me if I missed the context why we disabled it on Linux).


>From 1348bdfb945af4f25d31f27de4b17aca8173b864 Mon Sep 17 00:00:00 2001
From: Pengcheng Wang 
Date: Mon, 1 Jun 2026 18:42:37 +0800
Subject: [PATCH] [RISCV] Enable GP relaxation for Linux

We have supported GP relaxation in https://reviews.llvm.org/D143673
for a long time, but we made it disabled by default.

This PR enables it in Clang driver when targeting Linux.

(Please remind me if I missed the context why we disabled it on Linux).
---
 clang/docs/ReleaseNotes.rst   |  1 +
 clang/lib/Driver/ToolChains/Gnu.cpp   | 10 ++
 clang/test/Driver/riscv32-toolchain.c | 25 +
 clang/test/Driver/riscv64-toolchain.c | 25 +
 4 files changed, 61 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 132d9128a795b..d52076c037b76 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -812,6 +812,7 @@ RISC-V Support
 - Tenstorrent Ascalon D8 was renamed to Ascalon X. Use `tt-ascalon-x` with 
`-mcpu` or `-mtune`.
 - Intrinsics were added for the 'Zvabd` (RISC-V Integer Vector Absolute 
Difference) extension.
 - Intrinsics were added for the 'Zvzip` (Reordering Structured Data in Vector 
Registers) extension.
+- GP relaxation was enabled by default on Linux.
 
 CUDA/HIP Language Changes
 ^
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index 131dd725c7289..5be5045ea6252 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -343,6 +343,16 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   CmdArgs.push_back("--no-relax");
   }
 
+  // Default-enable LLD's --relax-gp for RISC-V on Linux when LLD is used and
+  // linker relaxation is not disabled by -mno-relax.
+  if (Triple.isRISCV() && Triple.isOSLinux()) {
+bool IsLLD = false;
+ToolChain.GetLinkerPath(&IsLLD);
+if (IsLLD &&
+Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, true))
+  CmdArgs.push_back("--relax-gp");
+  }
+
   const bool IsShared = Args.hasArg(options::OPT_shared);
   if (IsShared)
 CmdArgs.push_back("-shared");
diff --git a/clang/test/Driver/riscv32-toolchain.c 
b/clang/test/Driver/riscv32-toolchain.c
index 04acf1e7edbe0..272e55c19df97 100644
--- a/clang/test/Driver/riscv32-toolchain.c
+++ b/clang/test/Driver/riscv32-toolchain.c
@@ -247,6 +247,31 @@
 // RUN:   | FileCheck -check-prefix=CHECK-RV32-GNU-RELAX %s
 // CHECK-RV32-GNU-RELAX-NOT: "--no-relax"
 
+/// Check that "--relax-gp" is forwarded to LLD for RISC-V on Linux by default.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-LLD-RELAXGP %s
+// CHECK-RV32-LLD-RELAXGP: "--relax-gp"
+
+/// Check that "--relax-gp" is NOT forwarded to GNU ld for RISC-V on Linux.
+// RUN: env "PATH=" %clang -### %s -fuse-ld=ld -no-pie \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-GNU-NO-RELAXGP %s
+// CHECK-RV32-GNU-NO-RELAXGP-NOT: "--relax-gp"
+
+/// Check that "--relax-gp" is NOT forwarded to LLD when -mno-relax is set.
+// RUN: %clang -### %s -fuse-ld=lld -B%S/Inputs/lld -no-pie -mno-relax \
+// RUN:   --target=riscv32-unknown-linux-gnu --rtlib=platform 
--unwindlib=platform -mabi=ilp32 \
+// RUN:   --gcc-toolchain=%S/Inputs/multilib_riscv_linux_sdk \
+// RUN:   --sysroot=%S/Inputs/multilib_riscv_linux_sdk/sysroot 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-RV32-LLD-NORELAX-NOGP %s
+// CHECK-RV32-LLD-NORELAX-NOGP: "--no-relax"
+// CHECK-RV32-LLD-NORELAX-NOGP-NOT: "--relax-gp"
+
 /// Check that "-static -pie" is forwarded to linker when "-static-pie" is used
 // RUN: %clang -static-pie -### %s -fuse-ld= \
 // RUN:   --target=riscv32-unknown-elf -rtlib=platform --unwindlib=platform \
diff --git a/clang/test/Driver/riscv64-toolchain.c 
b/clang/test/Driver/riscv64-toolchain.c
index 378f3d9db7bad..6820b1a34806d 100644
--- a/clang/test/Driver/riscv64-toolchain.c
+++ b/clang/test/Driver/riscv64-toolchain.c
@@ -203,6 +203,31 @@
 // RUN:   | FileCheck -check-prefix=CHECK-RV64-GNU-RELAX %s
 // CHECK-RV64-GNU-RELAX-NOT: "--no-relax"
 
+/// Check that "--relax-gp" is