https://github.com/mgcarrasco updated 
https://github.com/llvm/llvm-project/pull/196074

>From 480ba1e61df5dbe1d719e9b0a76bf0ca228840eb Mon Sep 17 00:00:00 2001
From: Manuel Carrasco <[email protected]>
Date: Wed, 6 May 2026 08:40:01 -0500
Subject: [PATCH 1/4] [Driver][HIP/SPIRV] Fix crash when llvm-link is executed.

There is a design limitation that is forwarding flags to llvm-link
when it shouldn't happen. This commit fixes this issue by sanitizing
the arguments forwarded to llvm-link.

This may happen when clang-linker-wrapper eventually calls clang.
Crash reproducer is here: https://gcc.godbolt.org/z/rxvWcvan3.

The fix is based on MrSidims's old PR (#183492).

Co-authored-by: Dmitry Sidorov <[email protected]>
Co-authored-by: Manuel Carrasco <[email protected]>
---
 clang/lib/Driver/ToolChains/HIPAMD.cpp     |  7 +++++--
 clang/test/Driver/hip-spirv-linker-crash.c | 16 ++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/hip-spirv-linker-crash.c

diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp 
b/clang/lib/Driver/ToolChains/HIPAMD.cpp
index b4ff90c1d61f0..2eca149bbc998 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -39,8 +39,11 @@ void AMDGCN::Linker::constructLLVMLinkCommand(
 
   ArgStringList LinkerInputs;
 
-  for (auto Input : Inputs)
-    LinkerInputs.push_back(Input.getFilename());
+  for (auto Input : Inputs) {
+    if (Input.isFilename()) {
+      LinkerInputs.push_back(Input.getFilename());
+    }
+  }
 
   // Look for archive of bundled bitcode in arguments, and add temporary files
   // for the extracted archive of bitcode to inputs.
diff --git a/clang/test/Driver/hip-spirv-linker-crash.c 
b/clang/test/Driver/hip-spirv-linker-crash.c
new file mode 100644
index 0000000000000..d674d9ddec36c
--- /dev/null
+++ b/clang/test/Driver/hip-spirv-linker-crash.c
@@ -0,0 +1,16 @@
+// Verify that SPIR-V compilation does not crash during the llvm-link step
+// due to extra args that are not meant to be forwarded there.
+//
+// RUN: %clang -### --target=spirv64-amd-amdhsa -use-spirv-backend \
+// RUN:   -Xlinker -opt-bisect-limit=-1 %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-LINKER-OPT
+//
+// RUN: %clang -### --target=spirv64-amd-amdhsa -use-spirv-backend \
+// RUN:   -Xlinker -mllvm -Xlinker -opt-bisect-limit=-1 %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-LINKER-MLLVM
+//
+// CHECK-LINKER-OPT: "{{.*}}llvm-link"
+// CHECK-LINKER-OPT-SAME: "-o" "{{.*}}" "{{.*}}"{{$}}
+//
+// CHECK-LINKER-MLLVM: "{{.*}}llvm-link"
+// CHECK-LINKER-MLLVM-SAME: "-o" "{{.*}}" "{{.*}}"{{$}}

>From db535003fbff0adeca63cfdad7640ca4bf4e0f33 Mon Sep 17 00:00:00 2001
From: Manuel Carrasco <[email protected]>
Date: Wed, 6 May 2026 09:52:16 -0500
Subject: [PATCH 2/4] [review] Fix styling and test's checks.

---
 clang/lib/Driver/ToolChains/HIPAMD.cpp     | 6 ++----
 clang/test/Driver/hip-spirv-linker-crash.c | 4 ++--
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp 
b/clang/lib/Driver/ToolChains/HIPAMD.cpp
index 2eca149bbc998..e086e15a8c380 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -39,11 +39,9 @@ void AMDGCN::Linker::constructLLVMLinkCommand(
 
   ArgStringList LinkerInputs;
 
-  for (auto Input : Inputs) {
-    if (Input.isFilename()) {
+  for (auto Input : Inputs)
+    if (Input.isFilename())
       LinkerInputs.push_back(Input.getFilename());
-    }
-  }
 
   // Look for archive of bundled bitcode in arguments, and add temporary files
   // for the extracted archive of bitcode to inputs.
diff --git a/clang/test/Driver/hip-spirv-linker-crash.c 
b/clang/test/Driver/hip-spirv-linker-crash.c
index d674d9ddec36c..784e6e4d58ec8 100644
--- a/clang/test/Driver/hip-spirv-linker-crash.c
+++ b/clang/test/Driver/hip-spirv-linker-crash.c
@@ -10,7 +10,7 @@
 // RUN:   | FileCheck %s --check-prefix=CHECK-LINKER-MLLVM
 //
 // CHECK-LINKER-OPT: "{{.*}}llvm-link"
-// CHECK-LINKER-OPT-SAME: "-o" "{{.*}}" "{{.*}}"{{$}}
+// CHECK-LINKER-OPT-SAME: "-o" "{{.*}}.bc" "{{.*}}.bc"{{$}}
 //
 // CHECK-LINKER-MLLVM: "{{.*}}llvm-link"
-// CHECK-LINKER-MLLVM-SAME: "-o" "{{.*}}" "{{.*}}"{{$}}
+// CHECK-LINKER-MLLVM-SAME: "-o" "{{.*}}.bc" "{{.*}}.bc"{{$}}

>From a4804395ddc4acae40f04cbfb508f5653095cb96 Mon Sep 17 00:00:00 2001
From: Manuel Carrasco <[email protected]>
Date: Wed, 6 May 2026 09:59:51 -0500
Subject: [PATCH 3/4] [review] improve checks.

---
 clang/test/Driver/hip-spirv-linker-crash.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/test/Driver/hip-spirv-linker-crash.c 
b/clang/test/Driver/hip-spirv-linker-crash.c
index 784e6e4d58ec8..20af2c26f7417 100644
--- a/clang/test/Driver/hip-spirv-linker-crash.c
+++ b/clang/test/Driver/hip-spirv-linker-crash.c
@@ -10,7 +10,11 @@
 // RUN:   | FileCheck %s --check-prefix=CHECK-LINKER-MLLVM
 //
 // CHECK-LINKER-OPT: "{{.*}}llvm-link"
+// CHECK-LINKER-OPT-NOT: opt-bisect-limit
+// CHECK-LINKER-OPT-NOT: -mllvm
 // CHECK-LINKER-OPT-SAME: "-o" "{{.*}}.bc" "{{.*}}.bc"{{$}}
 //
 // CHECK-LINKER-MLLVM: "{{.*}}llvm-link"
+// CHECK-LINKER-MLLVM-NOT: opt-bisect-limit
+// CHECK-LINKER-MLLVM-NOT: -mllvm
 // CHECK-LINKER-MLLVM-SAME: "-o" "{{.*}}.bc" "{{.*}}.bc"{{$}}

>From 29180d11ad2c0e6f7e39ed0b3b046285f288fe91 Mon Sep 17 00:00:00 2001
From: Manuel Carrasco <[email protected]>
Date: Wed, 6 May 2026 10:07:10 -0500
Subject: [PATCH 4/4] [review] Improve test.

---
 clang/test/Driver/hip-spirv-linker-crash.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/clang/test/Driver/hip-spirv-linker-crash.c 
b/clang/test/Driver/hip-spirv-linker-crash.c
index 20af2c26f7417..3f8835f2b5696 100644
--- a/clang/test/Driver/hip-spirv-linker-crash.c
+++ b/clang/test/Driver/hip-spirv-linker-crash.c
@@ -3,18 +3,13 @@
 //
 // RUN: %clang -### --target=spirv64-amd-amdhsa -use-spirv-backend \
 // RUN:   -Xlinker -opt-bisect-limit=-1 %s 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-LINKER-OPT
+// RUN:   | FileCheck %s
 //
 // RUN: %clang -### --target=spirv64-amd-amdhsa -use-spirv-backend \
 // RUN:   -Xlinker -mllvm -Xlinker -opt-bisect-limit=-1 %s 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-LINKER-MLLVM
+// RUN:   | FileCheck %s
 //
-// CHECK-LINKER-OPT: "{{.*}}llvm-link"
-// CHECK-LINKER-OPT-NOT: opt-bisect-limit
-// CHECK-LINKER-OPT-NOT: -mllvm
-// CHECK-LINKER-OPT-SAME: "-o" "{{.*}}.bc" "{{.*}}.bc"{{$}}
-//
-// CHECK-LINKER-MLLVM: "{{.*}}llvm-link"
-// CHECK-LINKER-MLLVM-NOT: opt-bisect-limit
-// CHECK-LINKER-MLLVM-NOT: -mllvm
-// CHECK-LINKER-MLLVM-SAME: "-o" "{{.*}}.bc" "{{.*}}.bc"{{$}}
+// CHECK: "{{.*}}llvm-link"
+// CHECK-NOT: opt-bisect-limit
+// CHECK-NOT: -mllvm
+// CHECK-SAME: "-o" "{{.*}}.bc" "{{.*}}.bc"{{$}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to