[clang][Driver] Fix crash in --offload-new-driver and -save-temps. (#165606)

`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](957598f71b/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.
This commit is contained in:
Manuel Carrasco 2025-11-04 14:38:14 +00:00 committed by GitHub
parent 2237a18f25
commit bdf02486a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View file

@ -6463,9 +6463,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;

View file

@ -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"}}