[clang] [clang][Driver] Fix crash in --offload-new-driver and -save-temps. (PR #165606)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 29 11:01:03 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-driver
Author: Manuel Carrasco (mgcarrasco)
<details>
<summary>Changes</summary>
`clang -x hip foo.c --offload-arch=amdgcnspirv --offload-new-driver -save-temps` was crashing with the following error:
```
/usr/bin/ld: input file 'foo-x86_64-unknown-linux-gnu.o' is the same as output file
build/bin/clang-linker-wrapper: error: 'ld' failed
```
The `LinkerWrapperJobAction` [is created](https://github.com/llvm/llvm-project/blob/957598f71bd8baa029d886e59ed9aed60e6e9bb9/clang/lib/Driver/Driver.cpp#L4888) with `types::TY_Object` which makes `Driver::GetNamedOutputPath` assign the same name as the assembler's output and thus causing the crash.
---
Full diff: https://github.com/llvm/llvm-project/pull/165606.diff
2 Files Affected:
- (modified) clang/lib/Driver/Driver.cpp (+10-3)
- (added) clang/test/Driver/hip-spirv-translator-new-driver.c (+9)
``````````diff
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 71c52807091ba..114da05886161 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6459,9 +6459,16 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
(JA.getOffloadingDeviceKind() == Action::OFK_OpenMP && TC &&
TC->getTriple().isAMDGPU()));
};
- if (!AtTopLevel && JA.getType() == types::TY_LLVM_BC &&
- (C.getArgs().hasArg(options::OPT_emit_llvm) ||
- IsAMDRDCInCompilePhase(JA, C.getArgs())))
+
+ // The linker wrapper may not support the input and output files to be the
+ // same one, and without it -save-temps can fail.
+ bool IsLinkerWrapper =
+ JA.getType() == types::TY_Object && isa<LinkerWrapperJobAction>(JA);
+ bool IsEmitBitcode = JA.getType() == types::TY_LLVM_BC &&
+ (C.getArgs().hasArg(options::OPT_emit_llvm) ||
+ IsAMDRDCInCompilePhase(JA, C.getArgs()));
+
+ if (!AtTopLevel && (IsLinkerWrapper || IsEmitBitcode))
Suffixed += ".tmp";
Suffixed += '.';
Suffixed += Suffix;
diff --git a/clang/test/Driver/hip-spirv-translator-new-driver.c b/clang/test/Driver/hip-spirv-translator-new-driver.c
new file mode 100644
index 0000000000000..315a74635b9b3
--- /dev/null
+++ b/clang/test/Driver/hip-spirv-translator-new-driver.c
@@ -0,0 +1,9 @@
+// The --offload-new-driver was crashing when using -save-temps due to a failure in clang-linker-wrapper.
+// The input and output files cannot be the same.
+
+// RUN: %clang --offload-new-driver -### -save-temps -nogpuinc -nogpulib \
+// RUN: --offload-arch=amdgcnspirv -x hip %s 2>&1 \
+// RUN: | FileCheck %s
+
+// CHECK-NOT: {{".*clang-linker-wrapper.*"}} {{.*}} "-o" "[[OUTPUT_FILE:.*.o]]" {{.*}}"[[OUTPUT_FILE]]"
+// CHECK: {{".*clang-linker-wrapper.*"}} {{.*}} "-o" {{".*.tmp.o"}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/165606
More information about the cfe-commits
mailing list