Skip to content

Commit 6ff86f2

Browse files
authored
[AMDGPU] Use the AMDGPUToolChain when targeting C/C++ directly (#99687)
Summary: The `getToolChain` pass uses the triple to determine which toolchain to create. Currently the `amdgcn-amd-amdhsa` triple maps to the `ROCmToolChain` which uses things expected to be provided by `ROCm`. This is neded for OpenCL, but directly targeting C++ does not want this since it's primarily being used for creating GPU runtime code. As far as I know I'm the only user of this, so this shouldn't change anything. Unfortunately, there's no good logic for detercting this, so I simply checked ahead of time if the input is either `foo.cl` or `-x cl foo.c` to choose between the two. This allows us to use the AMDGPU target normally, as otherwise it will error without passing `-nogpulib`.
1 parent 81d18ad commit 6ff86f2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6669,9 +6669,21 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
66696669
case llvm::Triple::CUDA:
66706670
TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
66716671
break;
6672-
case llvm::Triple::AMDHSA:
6673-
TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
6672+
case llvm::Triple::AMDHSA: {
6673+
bool CL = llvm::any_of(Args, [&](Arg *A) {
6674+
return (A->getOption().matches(options::OPT_x) &&
6675+
types::isOpenCL(
6676+
types::lookupTypeForExtension(A->getValue()))) ||
6677+
(A->getOption().getKind() == Option::InputClass &&
6678+
StringRef(A->getValue()).rfind('.') != StringRef::npos &&
6679+
types::isOpenCL(types::lookupTypeForExtension(
6680+
&A->getValue()[StringRef(A->getValue()).rfind('.') + 1])));
6681+
});
6682+
TC = CL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
6683+
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
6684+
Args);
66746685
break;
6686+
}
66756687
case llvm::Triple::AMDPAL:
66766688
case llvm::Triple::Mesa3D:
66776689
TC = std::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args);

clang/test/Driver/amdgpu-toolchain.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@
3636
// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
3737
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
3838
// STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
39+
//
40+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 %s 2>&1 | FileCheck -check-prefix=ROCM %s
41+
// ROCM-NOT: -mlink-builtin-bitcode

0 commit comments

Comments
 (0)