Skip to content

Commit bdf0248

Browse files
authored
[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](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.
1 parent 2237a18 commit bdf0248

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6463,9 +6463,16 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
64636463
(JA.getOffloadingDeviceKind() == Action::OFK_OpenMP && TC &&
64646464
TC->getTriple().isAMDGPU()));
64656465
};
6466-
if (!AtTopLevel && JA.getType() == types::TY_LLVM_BC &&
6467-
(C.getArgs().hasArg(options::OPT_emit_llvm) ||
6468-
IsAMDRDCInCompilePhase(JA, C.getArgs())))
6466+
6467+
// The linker wrapper may not support the input and output files to be the
6468+
// same one, and without it -save-temps can fail.
6469+
bool IsLinkerWrapper =
6470+
JA.getType() == types::TY_Object && isa<LinkerWrapperJobAction>(JA);
6471+
bool IsEmitBitcode = JA.getType() == types::TY_LLVM_BC &&
6472+
(C.getArgs().hasArg(options::OPT_emit_llvm) ||
6473+
IsAMDRDCInCompilePhase(JA, C.getArgs()));
6474+
6475+
if (!AtTopLevel && (IsLinkerWrapper || IsEmitBitcode))
64696476
Suffixed += ".tmp";
64706477
Suffixed += '.';
64716478
Suffixed += Suffix;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The --offload-new-driver was crashing when using -save-temps due to a failure in clang-linker-wrapper.
2+
// The input and output files cannot be the same.
3+
4+
// RUN: %clang --offload-new-driver -### -save-temps -nogpuinc -nogpulib \
5+
// RUN: --offload-arch=amdgcnspirv -x hip %s 2>&1 \
6+
// RUN: | FileCheck %s
7+
8+
// CHECK-NOT: {{".*clang-linker-wrapper.*"}} {{.*}} "-o" "[[OUTPUT_FILE:.*.o]]" {{.*}}"[[OUTPUT_FILE]]"
9+
// CHECK: {{".*clang-linker-wrapper.*"}} {{.*}} "-o" {{".*.tmp.o"}}

0 commit comments

Comments
 (0)