Skip to content

Commit 5990110

Browse files
committed
[AMDGPU] Use the AMDGPUToolChain when targeting C/C++ directly
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 7e37d02 commit 5990110

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6404,9 +6404,22 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
64046404
case llvm::Triple::CUDA:
64056405
TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
64066406
break;
6407-
case llvm::Triple::AMDHSA:
6408-
TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
6407+
case llvm::Triple::AMDHSA: {
6408+
bool IsOpenCL =
6409+
Target.getEnvironment() == llvm::Triple::EnvironmentType::OpenCL ||
6410+
llvm::any_of(Args.filtered(options::OPT_INPUT, options::OPT_x),
6411+
[](auto &Arg) {
6412+
if (Arg->getOption().matches(options::OPT_INPUT))
6413+
return StringRef(Arg->getValue()).ends_with(".cl");
6414+
return StringRef(Arg->getValue()).ends_with("cl");
6415+
});
6416+
TC =
6417+
IsOpenCL
6418+
? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
6419+
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
6420+
Args);
64096421
break;
6422+
}
64106423
case llvm::Triple::AMDPAL:
64116424
case llvm::Triple::Mesa3D:
64126425
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
@@ -28,3 +28,6 @@
2828
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \
2929
// RUN: -fuse-ld=ld %s 2>&1 | FileCheck -check-prefixes=LD %s
3030
// LD: ld.lld
31+
32+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 %s 2>&1 | FileCheck -check-prefix=ROCM %s
33+
// ROCM-NOT: -mlink-builtin-bitcode

0 commit comments

Comments
 (0)