Skip to content

Commit dedef40

Browse files
authored
Reapply "[AMDGPU] Use the AMDGPUToolChain when targeting C/C++ directly" (#125744)
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 behavior for OpenCL and IR inputs, while others will be standalone. This is good enough for standard compile flows.
1 parent 0555594 commit dedef40

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

clang/lib/Driver/Driver.cpp

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

172+
template <typename F> static bool usesInput(const ArgList &Args, F &&Fn) {
173+
return llvm::any_of(Args, [&](Arg *A) {
174+
return (A->getOption().matches(options::OPT_x) &&
175+
Fn(types::lookupTypeForTypeSpecifier(A->getValue()))) ||
176+
(A->getOption().getKind() == Option::InputClass &&
177+
StringRef(A->getValue()).rfind('.') != StringRef::npos &&
178+
Fn(types::lookupTypeForExtension(
179+
&A->getValue()[StringRef(A->getValue()).rfind('.') + 1])));
180+
});
181+
}
182+
171183
// static
172184
std::string Driver::GetResourcesPath(StringRef BinaryPath) {
173185
// Since the resource directory is embedded in the module hash, it's important
@@ -6834,9 +6846,14 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
68346846
case llvm::Triple::CUDA:
68356847
TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
68366848
break;
6837-
case llvm::Triple::AMDHSA:
6838-
TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
6849+
case llvm::Triple::AMDHSA: {
6850+
bool DL =
6851+
usesInput(Args, types::isOpenCL) || usesInput(Args, types::isLLVMIR);
6852+
TC = DL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
6853+
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
6854+
Args);
68396855
break;
6856+
}
68406857
case llvm::Triple::AMDPAL:
68416858
case llvm::Triple::Mesa3D:
68426859
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"

clang/test/Driver/amdgpu-toolchain.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@
3838
// RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
3939
// RUN: -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
4040
// STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
41+
42+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 %s 2>&1 | FileCheck -check-prefix=ROCM %s
43+
// ROCM-NOT: -mlink-builtin-bitcode
44+
45+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=fiji -x ir %s \
46+
// RUN: --rocm-device-lib-path=%S/Inputs/rocm/amdgcn/bitcode 2>&1 \
47+
// RUN: | FileCheck -check-prefix=DEVICE-LIBS %s
48+
// DEVICE-LIBS: "-mlink-builtin-bitcode" "[[ROCM_PATH:.+]]ockl.bc"

0 commit comments

Comments
 (0)