Skip to content

Commit 396691f

Browse files
committed
Reapply "[AMDGPU] Use the AMDGPUToolChain when targeting C/C++ directly (#99687)"
Summary: This reverts commit 740e6ae. After discussions it was determined that the behavior for IR inputs needs to be maintained at least for now. In the future we should put a deprecation notice on this behavior. This patch keeps the old beahvior for OpenCL and IR inputs, while others will be standalone. This is good enough for standard compile flows.
1 parent daefb1b commit 396691f

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ getNVIDIAOffloadTargetTriple(const Driver &D, const ArgList &Args,
146146
D.Diag(diag::err_drv_invalid_or_unsupported_offload_target) << TT->str();
147147
return std::nullopt;
148148
}
149+
149150
static std::optional<llvm::Triple>
150151
getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) {
151152
if (!Args.hasArg(options::OPT_offload_EQ)) {
@@ -168,6 +169,18 @@ getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) {
168169
return std::nullopt;
169170
}
170171

172+
template <typename F>
173+
static bool usesInput(const ArgList &Args, F &&Fn) {
174+
return llvm::any_of(Args, [&](Arg *A) {
175+
return (A->getOption().matches(options::OPT_x) &&
176+
Fn(types::lookupTypeForTypeSpecifier(A->getValue()))) ||
177+
(A->getOption().getKind() == Option::InputClass &&
178+
StringRef(A->getValue()).rfind('.') != StringRef::npos &&
179+
Fn(types::lookupTypeForExtension(
180+
&A->getValue()[StringRef(A->getValue()).rfind('.') + 1])));
181+
});
182+
}
183+
171184
// static
172185
std::string Driver::GetResourcesPath(StringRef BinaryPath) {
173186
// Since the resource directory is embedded in the module hash, it's important
@@ -6672,9 +6685,14 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
66726685
case llvm::Triple::CUDA:
66736686
TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
66746687
break;
6675-
case llvm::Triple::AMDHSA:
6676-
TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
6688+
case llvm::Triple::AMDHSA: {
6689+
bool DL =
6690+
usesInput(Args, types::isOpenCL) || usesInput(Args, types::isLLVMIR);
6691+
TC = DL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
6692+
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
6693+
Args);
66776694
break;
6695+
}
66786696
case llvm::Triple::AMDPAL:
66796697
case llvm::Triple::Mesa3D:
66806698
TC = std::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args);

clang/test/Driver/amdgpu-toolchain-opencl.cl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@
3535

3636
// RUN: %clang -### --target=amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm -mcpu=fiji -nogpulib %s 2>&1 | FileCheck -check-prefix=CHECK-WARN-ATOMIC %s
3737
// CHECK-WARN-ATOMIC: "-cc1"{{.*}} "-Werror=atomic-alignment"
38+
39+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -x cl -c -emit-llvm -mcpu=fiji %s 2>&1 \
40+
// RUN: -mcode-object-version=5 --rocm-device-lib-path=%S/Inputs/rocm/amdgcn/bitcode \
41+
// RUN: | FileCheck -check-prefix=DEVICE-LIBS %s
42+
// DEVICE-LIBS: "-mlink-builtin-bitcode" "[[ROCM_PATH:.+]]/opencl.bc" "-mlink-builtin-bitcode" "[[ROCM_PATH]]/ocml.bc" "-mlink-builtin-bitcode" "[[ROCM_PATH]]/ockl.bc"

clang/test/Driver/amdgpu-toolchain.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@
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
42+
43+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=fiji -x ir %s \
44+
// RUN: --rocm-device-lib-path=%S/Inputs/rocm/amdgcn/bitcode 2>&1 \
45+
// RUN: | FileCheck -check-prefix=DEVICE-LIBS %s
46+
// DEVICE-LIBS: "-mlink-builtin-bitcode" "[[ROCM_PATH:.+]]/opencl.bc" "-mlink-builtin-bitcode" "[[ROCM_PATH]]/ocml.bc" "-mlink-builtin-bitcode" "[[ROCM_PATH]]/ockl.bc"

0 commit comments

Comments
 (0)