Skip to content

Commit 90c659e

Browse files
authored
[Driver][SYCL][NewOffloadModel] Hook up -fsycl-device-only behaviors (#13672)
When using -fsycl-device-only with the new offloading model, update the behaviors to properly restrict the compilation flow to only produce the device binary. This more tightly integrates with the existing --offload-device-only option that is commonly used.
1 parent 67d8ea1 commit 90c659e

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,12 +1733,13 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
17331733
if (Args.getLastArg(options::OPT_fsycl_dump_device_code_EQ))
17341734
DumpDeviceCode = true;
17351735

1736-
if (const Arg *A = Args.getLastArg(options::OPT_offload_host_only,
1737-
options::OPT_offload_device_only,
1738-
options::OPT_offload_host_device)) {
1736+
if (const Arg *A = Args.getLastArg(
1737+
options::OPT_offload_host_only, options::OPT_offload_device_only,
1738+
options::OPT_offload_host_device, options::OPT_fsycl_device_only)) {
17391739
if (A->getOption().matches(options::OPT_offload_host_only))
17401740
Offload = OffloadHost;
1741-
else if (A->getOption().matches(options::OPT_offload_device_only))
1741+
else if (A->getOption().matches(options::OPT_offload_device_only) ||
1742+
A->getOption().matches(options::OPT_fsycl_device_only))
17421743
Offload = OffloadDevice;
17431744
else
17441745
Offload = OffloadHostDevice;
@@ -4947,7 +4948,7 @@ class OffloadingActionBuilder final {
49474948
getDeviceDependences(OffloadAction::DeviceDependences &DA,
49484949
phases::ID CurPhase, phases::ID FinalPhase,
49494950
PhasesTy &Phases) override {
4950-
bool SYCLDeviceOnly = Args.hasArg(options::OPT_fsycl_device_only);
4951+
bool SYCLDeviceOnly = C.getDriver().offloadDeviceOnly();
49514952
if (CurPhase == phases::Preprocess) {
49524953
// Do not perform the host compilation when doing preprocessing only
49534954
// with -fsycl-device-only.
@@ -7884,9 +7885,9 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
78847885
break;
78857886
}
78867887

7887-
// Backend/Assemble actions are not used for the SYCL device side
7888-
if (Kind == Action::OFK_SYCL &&
7889-
(Phase == phases::Backend || Phase == phases::Assemble))
7888+
// Assemble actions are not used for the SYCL device side. Both compile
7889+
// and backend actions are used to generate IR and textual IR if needed.
7890+
if (Kind == Action::OFK_SYCL && Phase == phases::Assemble)
78907891
continue;
78917892

78927893
auto TCAndArch = TCAndArchs.begin();
@@ -8141,12 +8142,14 @@ Action *Driver::ConstructPhaseAction(
81418142
return C.MakeAction<BackendJobAction>(Input, Output);
81428143
}
81438144
if (Args.hasArg(options::OPT_emit_llvm) ||
8144-
(((Input->getOffloadingToolChain() &&
8145-
Input->getOffloadingToolChain()->getTriple().isAMDGPU()) ||
8146-
TargetDeviceOffloadKind == Action::OFK_HIP) &&
8147-
(Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
8148-
false) ||
8149-
TargetDeviceOffloadKind == Action::OFK_OpenMP))) {
8145+
((TargetDeviceOffloadKind == Action::OFK_SYCL &&
8146+
C.getDriver().getUseNewOffloadingDriver()) ||
8147+
(((Input->getOffloadingToolChain() &&
8148+
Input->getOffloadingToolChain()->getTriple().isAMDGPU()) ||
8149+
TargetDeviceOffloadKind == Action::OFK_HIP) &&
8150+
(Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
8151+
false) ||
8152+
TargetDeviceOffloadKind == Action::OFK_OpenMP)))) {
81508153
types::ID Output =
81518154
Args.hasArg(options::OPT_S) &&
81528155
(TargetDeviceOffloadKind == Action::OFK_None ||
@@ -9403,7 +9406,7 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
94039406
if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
94049407
return C.addResultFile(FinalOutput->getValue(), &JA);
94059408
// Output to destination for -fsycl-device-only and Windows -o
9406-
if (C.getArgs().hasArg(options::OPT_fsycl_device_only))
9409+
if (offloadDeviceOnly() && JA.getOffloadingDeviceKind() == Action::OFK_SYCL)
94079410
if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT__SLASH_o))
94089411
return C.addResultFile(FinalOutput->getValue(), &JA);
94099412
}

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10359,7 +10359,7 @@ static void getOtherSPIRVTransOpts(Compilation &C,
1035910359
C.getDriver().getFinalPhase(C.getArgs()) != phases::Link &&
1036010360
TCArgs.getLastArgValue(options::OPT_fsycl_device_obj_EQ)
1036110361
.equals_insensitive("spirv") &&
10362-
!TCArgs.hasArg(options::OPT_fsycl_device_only);
10362+
!C.getDriver().offloadDeviceOnly();
1036310363
bool ShouldPreserveMetadataInFinalImage =
1036410364
TCArgs.hasArg(options::OPT_fsycl_preserve_device_nonsemantic_metadata);
1036510365
bool ShouldPreserveMetadata =

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ void CudaToolChain::addClangTargetOptions(
980980
}
981981
}
982982

983-
auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv,
984-
options::OPT_fsycl_device_only);
983+
auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv) ||
984+
getDriver().offloadDeviceOnly();
985985
if (DeviceOffloadingKind == Action::OFK_SYCL && !NoLibSpirv) {
986986
std::string LibSpirvFile;
987987

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ void HIPAMDToolChain::addClangTargetOptions(
284284
CC1Args);
285285
}
286286

287-
auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv,
288-
options::OPT_fsycl_device_only);
287+
auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv) ||
288+
getDriver().offloadDeviceOnly();
289289
if (DeviceOffloadingKind == Action::OFK_SYCL && !NoLibSpirv) {
290290
std::string LibSpirvFile;
291291

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
// OFFLOAD-NEW-DRIVER: 4: input, "[[INPUT]]", c++, (device-sycl)
1010
// OFFLOAD-NEW-DRIVER: 5: preprocessor, {4}, c++-cpp-output, (device-sycl)
1111
// OFFLOAD-NEW-DRIVER: 6: compiler, {5}, ir, (device-sycl)
12-
// OFFLOAD-NEW-DRIVER: 7: offload, "device-sycl (nvptx64-nvidia-cuda)" {6}, ir
13-
// OFFLOAD-NEW-DRIVER: 8: input, "[[INPUT]]", c++, (device-sycl)
14-
// OFFLOAD-NEW-DRIVER: 9: preprocessor, {8}, c++-cpp-output, (device-sycl)
15-
// OFFLOAD-NEW-DRIVER: 10: compiler, {9}, ir, (device-sycl)
16-
// OFFLOAD-NEW-DRIVER: 11: offload, "device-sycl (spir64-unknown-unknown)" {10}, ir
17-
// OFFLOAD-NEW-DRIVER: 12: clang-offload-packager, {7, 11}, image, (device-sycl)
18-
// OFFLOAD-NEW-DRIVER: 13: offload, "host-sycl (x86_64-unknown-linux-gnu)" {3}, "device-sycl (x86_64-unknown-linux-gnu)" {12}, ir
19-
// OFFLOAD-NEW-DRIVER: 14: backend, {13}, assembler, (host-sycl)
20-
// OFFLOAD-NEW-DRIVER: 15: assembler, {14}, object, (host-sycl)
21-
// OFFLOAD-NEW-DRIVER: 16: clang-linker-wrapper, {15}, image, (host-sycl)
12+
// OFFLOAD-NEW-DRIVER: 7: backend, {6}, ir, (device-sycl)
13+
// OFFLOAD-NEW-DRIVER: 8: offload, "device-sycl (nvptx64-nvidia-cuda)" {7}, ir
14+
// OFFLOAD-NEW-DRIVER: 9: input, "[[INPUT]]", c++, (device-sycl)
15+
// OFFLOAD-NEW-DRIVER: 10: preprocessor, {9}, c++-cpp-output, (device-sycl)
16+
// OFFLOAD-NEW-DRIVER: 11: compiler, {10}, ir, (device-sycl)
17+
// OFFLOAD-NEW-DRIVER: 12: backend, {11}, ir, (device-sycl)
18+
// OFFLOAD-NEW-DRIVER: 13: offload, "device-sycl (spir64-unknown-unknown)" {12}, ir
19+
// OFFLOAD-NEW-DRIVER: 14: clang-offload-packager, {8, 13}, image, (device-sycl)
20+
// OFFLOAD-NEW-DRIVER: 15: offload, "host-sycl (x86_64-unknown-linux-gnu)" {3}, "device-sycl (x86_64-unknown-linux-gnu)" {14}, ir
21+
// OFFLOAD-NEW-DRIVER: 16: backend, {15}, assembler, (host-sycl)
22+
// OFFLOAD-NEW-DRIVER: 17: assembler, {16}, object, (host-sycl)
23+
// OFFLOAD-NEW-DRIVER: 18: clang-linker-wrapper, {17}, image, (host-sycl)
2224

2325
/// Check the toolflow for SYCL compilation using new offload model
2426
// RUN: %clangxx -### --target=x86_64-unknown-linux-gnu -fsycl -fsycl-targets=spir64 --offload-new-driver %s 2>&1 | FileCheck -check-prefix=CHK-FLOW %s
@@ -47,3 +49,16 @@
4749
// RUN: | FileCheck -check-prefix WRAPPER_OPTIONS_POSTLINK %s
4850
// WRAPPER_OPTIONS_POSTLINK: clang-linker-wrapper{{.*}} "--triple=spir64"
4951
// WRAPPER_OPTIONS_POSTLINK-SAME: "--sycl-post-link-options=-post-link-opt -O2 -spec-const=native -device-globals -split=auto -emit-only-kernels-as-entry-points -emit-param-info -symbols -emit-exported-symbols -split-esimd -lower-esimd"
52+
53+
// -fsycl-device-only behavior
54+
// RUN: %clangxx --target=x86_64-unknown-linux-gnu -fsycl --offload-new-driver \
55+
// RUN: -fsycl-device-only -ccc-print-phases %s 2>&1 \
56+
// RUN | FileCheck -check-prefix DEVICE_ONLY %s
57+
// RUN: %clangxx --target=x86_64-unknown-linux-gnu -fsycl --offload-new-driver \
58+
// RUN: --offload-device-only -ccc-print-phases %s 2>&1 \
59+
// RUN: | FileCheck -check-prefix DEVICE_ONLY %s
60+
// DEVICE_ONLY: 0: input, "{{.*}}", c++, (device-sycl)
61+
// DEVICE_ONLY: 1: preprocessor, {0}, c++-cpp-output, (device-sycl)
62+
// DEVICE_ONLY: 2: compiler, {1}, ir, (device-sycl)
63+
// DEVICE_ONLY: 3: backend, {2}, ir, (device-sycl)
64+
// DEVICE_ONLY: 4: offload, "device-sycl (spir64-unknown-unknown)" {3}, none

0 commit comments

Comments
 (0)