Skip to content

Commit ca4221b

Browse files
committed
[Driver][SYCL] Add support for -fsyclbin
Adds support for the -fsyclbin option. This option allows for creation of a SYCL device specific binary. The binary is created via a device compilation before being packaged and sent to the clang-linker-wrapper. The clang-linker-wrapper is responsible to take the device 'object' and create the corresponding .syclbin file.
1 parent cdcf1a6 commit ca4221b

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7266,6 +7266,8 @@ defm sycl_allow_device_image_dependencies: BoolOptionWithoutMarshalling<"f", "sy
72667266
def fsycl_dump_device_code_EQ : Joined<["-"], "fsycl-dump-device-code=">,
72677267
Flags<[NoXarchOption]>,
72687268
HelpText<"Dump device code into the user provided directory.">;
7269+
def fsyclbin : Flag<["-"], "fsyclbin">,
7270+
HelpText<"Create a SYCLBIN file">;
72697271
} // let Group = sycl_Group
72707272

72717273
// FIXME: -fsycl-explicit-simd is deprecated. remove it when support is dropped.

clang/lib/Driver/Driver.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8414,6 +8414,19 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
84148414
C.MakeAction<LinkJobAction>(OffloadActions, types::TY_HIP_FATBIN);
84158415
DDep.add(*FatbinAction, *C.getSingleOffloadToolChain<Action::OFK_HIP>(),
84168416
nullptr, Action::OFK_HIP);
8417+
} else if (C.isOffloadingHostKind(Action::OFK_SYCL) &&
8418+
Args.hasArg(options::OPT_fsyclbin)) {
8419+
// With '-fsyclbin', package all the offloading actions into a single output
8420+
// that is sent to the clang-linker-wrapper.
8421+
Action *PackagerAction =
8422+
C.MakeAction<OffloadPackagerJobAction>(OffloadActions, types::TY_Image);
8423+
ActionList PackagerActions;
8424+
PackagerActions.push_back(PackagerAction);
8425+
Action *LinkAction = C.MakeAction<LinkerWrapperJobAction>(
8426+
PackagerActions, types::TY_Image);
8427+
DDep.add(*LinkAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
8428+
nullptr, C.getActiveOffloadKinds());
8429+
return C.MakeAction<OffloadAction>(DDep, types::TY_Nothing);
84178430
} else {
84188431
// Package all the offloading actions into a single output that can be
84198432
// embedded in the host and linked.
@@ -10037,6 +10050,24 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
1003710050
else
1003810051
BaseName = llvm::sys::path::filename(BasePath);
1003910052

10053+
// When compiling with -fsyclbin, maintain a simple output file name for the
10054+
// resulting image. A '.syclbin' extension is intended to be added during
10055+
// the linking step with the clang-linker-wrapper.
10056+
if (JA.getOffloadingDeviceKind() == Action::OFK_SYCL &&
10057+
C.getArgs().hasArgNoClaim(options::OPT_fsyclbin) &&
10058+
JA.getType() == types::TY_Image) {
10059+
const char *SYCLBinOutput;
10060+
if (IsCLMode()) {
10061+
// clang-cl uses BaseName for the executable name.
10062+
SYCLBinOutput =
10063+
MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image);
10064+
} else {
10065+
SmallString<128> Output(getDefaultImageName());
10066+
SYCLBinOutput = C.getArgs().MakeArgString(Output.c_str());
10067+
}
10068+
return C.addResultFile(SYCLBinOutput, &JA);
10069+
}
10070+
1004010071
// Determine what the derived output name should be.
1004110072
const char *NamedOutput;
1004210073

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11733,6 +11733,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
1173311733
CmdArgs.push_back(
1173411734
Args.MakeArgString("--sycl-target-link-options=" + LinkOptString));
1173511735
}
11736+
// Add option to enable creating of the .syclbin file
11737+
if (Args.hasArg(options::OPT_fsyclbin))
11738+
CmdArgs.push_back(Args.MakeArgString("--syclbin"));
1173611739
}
1173711740

1173811741
// Construct the link job so we can wrap around it.

clang/test/Driver/fsyclbin.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// Tests behaviors of -fsyclbin
2+
3+
/// -fsyclbin is only used with the new offloading model.
4+
// RUN: %clangxx -fsycl -fsyclbin --no-offload-new-driver %s -### 2>&1 \
5+
// RUN: | FileCheck %s --check-prefix=UNUSED
6+
// UNUSED: warning: argument unused during compilation: '-fsyclbin'
7+
8+
/// Check tool invocation contents.
9+
// RUN: %clangxx -fsycl -fsyclbin --offload-new-driver %s -### 2>&1 \
10+
// RUN: | FileCheck %s --check-prefix=CHECK_TOOLS
11+
// RUN: %clang_cl -fsycl -fsyclbin --offload-new-driver %s -### 2>&1 \
12+
// RUN: | FileCheck %s --check-prefix=CHECK_TOOLS
13+
// CHECK_TOOLS: clang-offload-packager
14+
// CHECK_TOOLS-SAME: --image=file={{.*}}.bc,triple=spir64-unknown-unknown
15+
// CHECK_TOOLS-SAME: kind=sycl
16+
// CHECK_TOOLS: clang-linker-wrapper
17+
// CHECK_TOOLS-SAME: --syclbin
18+
19+
/// Check compilation phases, only device compile should be performed
20+
// RUN: %clangxx --target=x86_64-unknown-linux-gnu -fsycl -fsyclbin \
21+
// RUN: --offload-new-driver %s -ccc-print-phases 2>&1 \
22+
// RUN: | FileCheck %s --check-prefix=CHECK_PHASES
23+
// CHECK_PHASES: 0: input, "{{.*}}", c++, (device-sycl)
24+
// CHECK_PHASES: 1: preprocessor, {0}, c++-cpp-output, (device-sycl)
25+
// CHECK_PHASES: 2: compiler, {1}, ir, (device-sycl)
26+
// CHECK_PHASES: 3: backend, {2}, ir, (device-sycl)
27+
// CHECK_PHASES: 4: offload, "device-sycl (spir64-unknown-unknown)" {3}, ir
28+
// CHECK_PHASES: 5: clang-offload-packager, {4}, image, (device-sycl)
29+
// CHECK_PHASES: 6: clang-linker-wrapper, {5}, image, (device-sycl)
30+
// CHECK_PHASES: 7: offload, "device-sycl (x86_64-unknown-linux-gnu)" {6}, none

0 commit comments

Comments
 (0)