Skip to content

Commit 60978ef

Browse files
mdtoguchiromanovvlad
authored andcommitted
[SYCL] add support for -fsycl-link
Generate a wrapped device binary that can be used independently with a host link step. Takes the provided fat objects or source, extracts or creates the associated device binaries and wraps them to be used during a regular host link step Signed-off-by: Michael D Toguchi <[email protected]>
1 parent 2c9ab05 commit 60978ef

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def err_drv_invalid_omp_target : Error<"OpenMP target is invalid: '%0'">;
235235
def err_drv_invalid_sycl_target : Error<"SYCL target is invalid: '%0'">;
236236
def err_drv_sycl_target_conflict : Error<"The option -fsycl-targets conflicts with -fsycl-link-targets">;
237237
def err_drv_sycl_add_link_conflict : Error<"The option -fsycl-link-targets conflicts with -fsycl-add-targets">;
238+
def err_drv_sycl_link_link_targets_conflict : Error<"The option -fsycl-link-targets conflicts with -fsycl-link">;
238239
def err_drv_omp_host_ir_file_not_found : Error<
239240
"The provided host compiler IR file '%0' is required to generate code for OpenMP target regions but cannot be found.">;
240241
def err_drv_omp_host_target_not_supported : Error<

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,8 @@ def fsycl_use_bitcode : Flag<["-"], "fsycl-use-bitcode">,
17191719
Flags<[CC1Option]>, HelpText<"Use LLVM bitcode instead of SPIR-V in fat objects">;
17201720
def fno_sycl_use_bitcode : Flag<["-"], "fno-sycl-use-bitcode">,
17211721
Flags<[CC1Option]>, HelpText<"Use SPIR-V instead of LLVM bitcode in fat objects">;
1722+
def fsycl_link : Flag<["-"], "fsycl-link">,
1723+
Flags<[CC1Option]>, HelpText<"Generate partially linked device object to be used with the host link">;
17221724
def fsyntax_only : Flag<["-"], "fsyntax-only">,
17231725
Flags<[DriverOption,CoreOption,CC1Option]>, Group<Action_Group>;
17241726
def ftabstop_EQ : Joined<["-"], "ftabstop=">, Group<f_Group>;

clang/lib/Driver/Driver.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,17 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
719719
C.getInputArgs().getLastArg(options::OPT_fsycl_link_targets_EQ);
720720
Arg *SYCLAddTargets =
721721
C.getInputArgs().getLastArg(options::OPT_fsycl_add_targets_EQ);
722+
Arg *SYCLLink =
723+
C.getInputArgs().getLastArg(options::OPT_fsycl_link);
722724
// -fsycl-targets cannot be used with -fsycl-link-targets
723725
if (SYCLTargets && SYCLLinkTargets)
724726
Diag(clang::diag::err_drv_sycl_target_conflict);
725727
// -fsycl-link-targets and -fsycl-add-targets cannot be used together
726728
if (SYCLLinkTargets && SYCLAddTargets)
727729
Diag(clang::diag::err_drv_sycl_add_link_conflict);
730+
// -fsycl-link-targets is not allowed with -fsycl-link
731+
if (SYCLLinkTargets && SYCLLink)
732+
Diag(clang::diag::err_drv_sycl_link_link_targets_conflict);
728733

729734
// -fsycl-add-targets is a list of paired items (Triple and file) which are
730735
// gathered and used to be linked into the final device binary. This can
@@ -2997,6 +3002,9 @@ class OffloadingActionBuilder final {
29973002
/// Flag to signal if the user requested device-only compilation.
29983003
bool CompileDeviceOnly = false;
29993004

3005+
/// Flag to signal if the user requested the device object to be wrapped
3006+
bool WrapDeviceOnlyBinary = false;
3007+
30003008
/// The SYCL actions for the current input.
30013009
ActionList SYCLDeviceActions;
30023010

@@ -3061,8 +3069,17 @@ class OffloadingActionBuilder final {
30613069
for (auto SDA : SYCLDeviceActions) {
30623070
SYCLLinkBinaryList.push_back(SDA);
30633071
}
3064-
SYCLLinkBinary = C.MakeAction<LinkJobAction>(SYCLLinkBinaryList,
3065-
types::TY_Image);
3072+
if (WrapDeviceOnlyBinary) {
3073+
auto *DeviceLinkAction =
3074+
C.MakeAction<LinkJobAction>(SYCLLinkBinaryList, types::TY_Image);
3075+
// Wrap the binary when -fsycl-link is given
3076+
SYCLLinkBinary =
3077+
C.MakeAction<OffloadWrappingJobAction>(DeviceLinkAction,
3078+
types::TY_Object);
3079+
}
3080+
else
3081+
SYCLLinkBinary = C.MakeAction<LinkJobAction>(SYCLLinkBinaryList,
3082+
types::TY_Image);
30663083

30673084
// Remove the SYCL actions as they are already connected to an host
30683085
// action or fat binary.
@@ -3217,9 +3234,11 @@ class OffloadingActionBuilder final {
32173234

32183235
Arg *SYCLLinkTargets = Args.getLastArg(
32193236
options::OPT_fsycl_link_targets_EQ);
3220-
CompileDeviceOnly = SYCLLinkTargets &&
3221-
SYCLLinkTargets->getOption().matches(
3222-
options::OPT_fsycl_link_targets_EQ);
3237+
WrapDeviceOnlyBinary = Args.hasArg(options::OPT_fsycl_link);
3238+
CompileDeviceOnly = (SYCLLinkTargets &&
3239+
SYCLLinkTargets->getOption().matches(
3240+
options::OPT_fsycl_link_targets_EQ)) ||
3241+
WrapDeviceOnlyBinary;
32233242
Arg *SYCLAddTargets = Args.getLastArg(
32243243
options::OPT_fsycl_add_targets_EQ);
32253244
if (SYCLAddTargets) {
@@ -4654,9 +4673,10 @@ InputInfo Driver::BuildJobsForActionNoCache(
46544673
Result = InputInfo(A, BaseInput);
46554674
else {
46564675
std::string OffloadingPrefix;
4657-
// When generating binaries with -fsycl-link-target, the output file prefix
4658-
// is the triple arch only
4659-
if (Args.getLastArg(options::OPT_fsycl_link_targets_EQ)) {
4676+
// When generating binaries with -fsycl-link-target or -fsycl-link, the
4677+
// output file prefix is the triple arch only.
4678+
if (Args.getLastArg(options::OPT_fsycl_link_targets_EQ) ||
4679+
Args.hasArg(options::OPT_fsycl_link)) {
46604680
OffloadingPrefix = "-";
46614681
OffloadingPrefix += TC->getTriple().getArchName();
46624682
} else {

clang/test/Driver/sycl-offload.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,32 @@
282282

283283
/// ###########################################################################
284284

285+
/// Check -fsycl-link behaviors unbundle
286+
// RUN: touch %t.o
287+
// RUN: %clang -### -ccc-print-phases -target x86_64-unknown-linux-gnu -fsycl -o %t.out -fsycl-link %t.o 2>&1 \
288+
// RUN: | FileCheck -check-prefix=CHK-LINK-UB %s
289+
// CHK-LINK-UB: 0: input, "[[INPUT:.+\.o]]", object
290+
// CHK-LINK-UB: 1: clang-offload-unbundler, {0}, object
291+
// CHK-LINK-UB: 2: linker, {1}, image, (device-sycl)
292+
// CHK-LINK-UB: 3: clang-offload-wrapper, {2}, object, (device-sycl)
293+
// CHK-LINK-UB: 4: offload, "device-sycl (spir64-unknown-linux-sycldevice)" {3}, object
294+
295+
/// ###########################################################################
296+
297+
/// Check -fsycl-link behaviors from source
298+
// RUN: %clang -### -ccc-print-phases -target x86_64-unknown-linux-gnu -fsycl -o %t.out -fsycl-link %s 2>&1 \
299+
// RUN: | FileCheck -check-prefix=CHK-LINK %s
300+
// CHK-LINK: 0: input, "[[INPUT:.+\.c]]", c, (device-sycl)
301+
// CHK-LINK: 1: preprocessor, {0}, cpp-output, (device-sycl)
302+
// CHK-LINK: 2: compiler, {1}, ir, (device-sycl)
303+
// CHK-LINK: 3: backend, {2}, assembler, (device-sycl)
304+
// CHK-LINK: 4: assembler, {3}, object, (device-sycl)
305+
// CHK-LINK: 5: linker, {4}, image, (device-sycl)
306+
// CHK-LINK: 6: clang-offload-wrapper, {5}, object, (device-sycl)
307+
// CHK-LINK: 7: offload, "device-sycl (spir64-unknown-linux-sycldevice)" {6}, object
308+
309+
/// ###########################################################################
310+
285311
/// Check -fsycl-add-targets=<triple> behaviors unbundle
286312
// RUN: touch %t.o
287313
// RUN: %clang -### -ccc-print-phases -target x86_64-unknown-linux-gnu -fsycl -o %t.out -fsycl-add-targets=spir64-unknown-linux-sycldevice:dummy.spv %t.o 2>&1 \

0 commit comments

Comments
 (0)