Skip to content

Commit c99522b

Browse files
authored
[Driver][SYCL][NewOffload] Fix arch settings for nvptx and amd (#14340)
When compiling for -fsycl-targets values of nvptx64-nvidia-cuda and amdgcn-amd-gpu, the default arch behaviors were not applied to the compilation. Updates to do the following: - Add default of sm_50 for nvptx64 if not provided - Emit diagnostic if no arch provided for amd - Parse -Xsycl-backend-target for offload-arch values
1 parent 3040061 commit c99522b

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7781,12 +7781,28 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
77817781
for (auto *Arg : Args) {
77827782
// Extract any '--[no-]offload-arch' arguments intended for this toolchain.
77837783
std::unique_ptr<llvm::opt::Arg> ExtractedArg = nullptr;
7784-
if (Arg->getOption().matches(options::OPT_Xopenmp_target_EQ) &&
7785-
ToolChain::getOpenMPTriple(Arg->getValue(0)) == TC->getTriple()) {
7786-
Arg->claim();
7787-
unsigned Index = Args.getBaseArgs().MakeIndex(Arg->getValue(1));
7788-
ExtractedArg = getOpts().ParseOneArg(Args, Index);
7789-
Arg = ExtractedArg.get();
7784+
if (Kind == Action::OFK_SYCL) {
7785+
// For SYCL based offloading, we allow for -Xsycl-target-backend
7786+
// and -Xsycl-target-backend=<target> for specifying options.
7787+
if (Arg->getOption().matches(options::OPT_Xsycl_backend_EQ) &&
7788+
llvm::Triple(Arg->getValue(0)) == TC->getTriple()) {
7789+
Arg->claim();
7790+
unsigned Index = Args.getBaseArgs().MakeIndex(Arg->getValue(1));
7791+
ExtractedArg = getOpts().ParseOneArg(Args, Index);
7792+
Arg = ExtractedArg.get();
7793+
} else if (Arg->getOption().matches(options::OPT_Xsycl_backend)) {
7794+
unsigned Index = Args.getBaseArgs().MakeIndex(Arg->getValue(0));
7795+
ExtractedArg = getOpts().ParseOneArg(Args, Index);
7796+
Arg = ExtractedArg.get();
7797+
}
7798+
} else {
7799+
if (Arg->getOption().matches(options::OPT_Xopenmp_target_EQ) &&
7800+
ToolChain::getOpenMPTriple(Arg->getValue(0)) == TC->getTriple()) {
7801+
Arg->claim();
7802+
unsigned Index = Args.getBaseArgs().MakeIndex(Arg->getValue(1));
7803+
ExtractedArg = getOpts().ParseOneArg(Args, Index);
7804+
Arg = ExtractedArg.get();
7805+
}
77907806
}
77917807

77927808
// Add or remove the seen architectures in order of appearance. If an
@@ -7851,8 +7867,18 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
78517867
Archs.insert(CudaArchToString(CudaArch::HIPDefault));
78527868
else if (Kind == Action::OFK_OpenMP)
78537869
Archs.insert(StringRef());
7854-
else if (Kind == Action::OFK_SYCL)
7855-
Archs.insert(StringRef());
7870+
else if (Kind == Action::OFK_SYCL) {
7871+
// For SYCL offloading, we need to check the triple for NVPTX or AMDGPU.
7872+
// The default arch is set for NVPTX if not provided. For AMDGPU, emit
7873+
// an error as the user is responsible to set the arch.
7874+
if (TC->getTriple().isNVPTX())
7875+
Archs.insert(CudaArchToString(CudaArch::SM_50));
7876+
else if (TC->getTriple().isAMDGPU())
7877+
C.getDriver().Diag(clang::diag::err_drv_sycl_missing_amdgpu_arch)
7878+
<< 1 << TC->getTriple().str();
7879+
else
7880+
Archs.insert(StringRef());
7881+
}
78567882
} else {
78577883
Args.ClaimAllArgs(options::OPT_offload_arch_EQ);
78587884
Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);

clang/test/Driver/sycl-offload-new-driver.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
// OFFLOAD-NEW-DRIVER: 1: append-footer, {0}, c++, (host-sycl)
77
// OFFLOAD-NEW-DRIVER: 2: preprocessor, {1}, c++-cpp-output, (host-sycl)
88
// OFFLOAD-NEW-DRIVER: 3: compiler, {2}, ir, (host-sycl)
9-
// OFFLOAD-NEW-DRIVER: 4: input, "[[INPUT]]", c++, (device-sycl)
10-
// OFFLOAD-NEW-DRIVER: 5: preprocessor, {4}, c++-cpp-output, (device-sycl)
11-
// OFFLOAD-NEW-DRIVER: 6: compiler, {5}, ir, (device-sycl)
12-
// OFFLOAD-NEW-DRIVER: 7: backend, {6}, ir, (device-sycl)
13-
// OFFLOAD-NEW-DRIVER: 8: offload, "device-sycl (nvptx64-nvidia-cuda)" {7}, ir
9+
// OFFLOAD-NEW-DRIVER: 4: input, "[[INPUT]]", c++, (device-sycl, sm_50)
10+
// OFFLOAD-NEW-DRIVER: 5: preprocessor, {4}, c++-cpp-output, (device-sycl, sm_50)
11+
// OFFLOAD-NEW-DRIVER: 6: compiler, {5}, ir, (device-sycl, sm_50)
12+
// OFFLOAD-NEW-DRIVER: 7: backend, {6}, ir, (device-sycl, sm_50)
13+
// OFFLOAD-NEW-DRIVER: 8: offload, "device-sycl (nvptx64-nvidia-cuda:sm_50)" {7}, ir
1414
// OFFLOAD-NEW-DRIVER: 9: input, "[[INPUT]]", c++, (device-sycl)
1515
// OFFLOAD-NEW-DRIVER: 10: preprocessor, {9}, c++-cpp-output, (device-sycl)
1616
// OFFLOAD-NEW-DRIVER: 11: compiler, {10}, ir, (device-sycl)
@@ -173,3 +173,16 @@
173173
// WRAPPER_OPTIONS_BACKEND_AOT: clang-linker-wrapper{{.*}} "--host-triple=x86_64-unknown-linux-gnu"
174174
// WRAPPER_OPTIONS_BACKEND_AOT-SAME: "--gpu-tool-arg={{.*}}-backend-gpu-opt"
175175
// WRAPPER_OPTIONS_BACKEND_AOT-SAME: "--cpu-tool-arg={{.*}}-backend-cpu-opt"
176+
177+
/// Verify arch settings for nvptx and amdgcn targets
178+
// RUN: %clangxx -fsycl -### -fsycl-targets=amdgcn-amd-gpu -fno-sycl-libspirv \
179+
// RUN: -nocudalib --offload-new-driver \
180+
// RUN: -Xsycl-target-backend=amdgcn-amd-gpu --offload-arch=gfx600 \
181+
// RUN: %s 2>&1 \
182+
// RUN: | FileCheck -check-prefix AMD_ARCH %s
183+
// AMD_ARCH: clang-offload-packager{{.*}} "--image=file={{.*}},triple=amdgcn-amd-gpu,arch=gfx600,kind=sycl,compile-opts=--offload-arch=gfx600"
184+
185+
// RUN: %clangxx -fsycl -### -fsycl-targets=nvptx64-nvidia-cuda \
186+
// RUN: -fno-sycl-libspirv -nocudalib --offload-new-driver %s 2>&1 \
187+
// RUN: | FileCheck -check-prefix NVPTX_DEF_ARCH %s
188+
// NVPTX_DEF_ARCH: clang-offload-packager{{.*}} "--image=file={{.*}},triple=nvptx64-nvidia-cuda,arch=sm_50,kind=sycl"

0 commit comments

Comments
 (0)