Skip to content

Commit e721955

Browse files
mdtoguchivladimirlaz
authored andcommitted
[SYCL] Add support for -fsycl-use-bitcode
-fsycl-use-bitcode is used to generate .bc files during device code generation instead of the default SPIR-V binary. The .bc files will also be bundled into the fat object, so -fsycl-use-bitcode is required upon usage of those objects. Used with -fsycl compilations. Signed-off-by: Toguchi, Michael D <[email protected]> Signed-off-by: Vladimir Lazarev <[email protected]>
1 parent 3e64587 commit e721955

File tree

11 files changed

+49
-12
lines changed

11 files changed

+49
-12
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ LANGOPT(CUDADeviceApproxTranscendentals, 1, 0, "using approximate transcendental
216216
LANGOPT(GPURelocatableDeviceCode, 1, 0, "generate relocatable device code")
217217

218218
LANGOPT(SYCL , 1, 0, "Generate code for SYCL device")
219+
LANGOPT(SYCLUseBitcode , 1, 0, "Generate bitcode for SYCL")
219220
LANGOPT(SizedDeallocation , 1, 0, "sized deallocation")
220221
LANGOPT(AlignedAllocation , 1, 0, "aligned allocation")
221222
LANGOPT(AlignedAllocationUnavailable, 1, 0, "aligned allocation functions are unavailable")

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,10 @@ def fsycl_add_targets_EQ : CommaJoined<["-"], "fsycl-add-targets=">, Flags<[Driv
17011701
HelpText<"Specify comma-separated list of triple and device binary image pairs to add to the final SYCL binary">;
17021702
def fsycl_link_targets_EQ : CommaJoined<["-"], "fsycl-link-targets=">, Flags<[DriverOption, CC1Option]>,
17031703
HelpText<"Specify comma-separated list of triples SYCL offloading targets to produce linked device images">;
1704+
def fsycl_use_bitcode : Flag<["-"], "fsycl-use-bitcode">,
1705+
Flags<[CC1Option]>, HelpText<"Use LLVM bitcode instead of SPIR-V in fat objects">;
1706+
def fno_sycl_use_bitcode : Flag<["-"], "fno-sycl-use-bitcode">,
1707+
Flags<[CC1Option]>, HelpText<"Use SPIR-V instead of LLVM bitcode in fat objects">;
17041708
def fsyntax_only : Flag<["-"], "fsyntax-only">,
17051709
Flags<[DriverOption,CoreOption,CC1Option]>, Group<Action_Group>;
17061710
def ftabstop_EQ : Joined<["-"], "ftabstop=">, Group<f_Group>;

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,14 +855,19 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
855855

856856
case Backend_EmitSPIRV:
857857
if (LangOpts.SYCL) {
858+
// TODO: SPIRVNoDerefAttr is not modeled when using the bitcode pass
858859
SPIRV::SPIRVNoDerefAttr = true;
859860
// TODO: this pass added to work around missing linkonce_odr in SPIR-V
860861
PerModulePasses.add(
861862
createAlwaysInlinerLegacyPass(true /*InsertLifetimeIntrinsics*/));
862863
PerModulePasses.add(createASFixerPass());
863864
PerModulePasses.add(createDeadCodeEliminationPass());
864865
}
865-
PerModulePasses.add(createSPIRVWriterPass(*OS));
866+
if (LangOpts.SYCLUseBitcode)
867+
PerModulePasses.add(
868+
createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, false));
869+
else
870+
PerModulePasses.add(createSPIRVWriterPass(*OS));
866871

867872
break;
868873

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,6 +3523,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
35233523
CmdArgs.push_back("-aux-triple");
35243524
CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
35253525
CmdArgs.push_back("-disable-llvm-passes");
3526+
if (Args.hasFlag(options::OPT_fsycl_use_bitcode,
3527+
options::OPT_fno_sycl_use_bitcode, true)) {
3528+
CmdArgs.push_back("-fsycl-use-bitcode");
3529+
}
35263530
}
35273531

35283532
if (IsOpenMPDevice) {

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const char *SYCL::Linker::constructLLVMSpirvCommand(Compilation &C,
4040
CmdArgs.push_back("-o");
4141
CmdArgs.push_back(OutputFileName);
4242
} else {
43+
CmdArgs.push_back("-spirv-no-deref-attr");
4344
CmdArgs.push_back("-o");
4445
CmdArgs.push_back(Output.getFilename());
4546
}
@@ -87,7 +88,8 @@ void SYCL::Linker::constructLlcCommand(Compilation &C, const JobAction &JA,
8788
}
8889

8990
// For SYCL the inputs of the linker job are SPIR-V binaries and output is
90-
// a single SPIR-V binary.
91+
// a single SPIR-V binary. Input can also be bitcode when specified by
92+
// the user
9193
void SYCL::Linker::ConstructJob(Compilation &C, const JobAction &JA,
9294
const InputInfo &Output,
9395
const InputInfoList &Inputs,
@@ -110,9 +112,15 @@ void SYCL::Linker::ConstructJob(Compilation &C, const JobAction &JA,
110112
for (const auto &II : Inputs) {
111113
if (!II.isFilename())
112114
continue;
113-
const char *LLVMSpirvOutputFile =
114-
constructLLVMSpirvCommand(C, JA, Output, Prefix, true, II.getFilename());
115-
SpirvInputs.push_back(LLVMSpirvOutputFile);
115+
if (Args.hasFlag(options::OPT_fsycl_use_bitcode,
116+
options::OPT_fno_sycl_use_bitcode, true))
117+
SpirvInputs.push_back(II.getFilename());
118+
else {
119+
const char *LLVMSpirvOutputFile =
120+
constructLLVMSpirvCommand(C, JA, Output, Prefix, true,
121+
II.getFilename());
122+
SpirvInputs.push_back(LLVMSpirvOutputFile);
123+
}
116124
}
117125
const char *LLVMLinkOutputFile =
118126
constructLLVMLinkCommand(C, JA, SubArchName, Prefix, SpirvInputs);

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,8 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
28832883
}
28842884

28852885
Opts.SYCL = Args.hasArg(options::OPT_fsycl_is_device);
2886+
Opts.SYCLUseBitcode = Args.hasFlag(options::OPT_fsycl_use_bitcode,
2887+
options::OPT_fno_sycl_use_bitcode, false);
28862888

28872889
// Set CUDA mode for OpenMP target NVPTX if specified in options
28882890
Opts.OpenMPCUDAMode = Opts.OpenMPIsDevice && T.isNVPTX() &&

clang/test/CodeGenSYCL/spir-no-deref-attr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -S -emit-spirv -x c++ %s -o %t.spv
1+
// RUN: %clang_cc1 -fno-sycl-use-bitcode -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -S -emit-spirv -x c++ %s -o %t.spv
22
// RUN: llvm-spirv %t.spv -to-text -o %t.txt
33
// RUN: FileCheck < %t.txt %s --check-prefix=CHECK
44

clang/test/Driver/sycl-offload.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@
6969
/// We should have an offload action joining the host compile and device
7070
/// preprocessor and another one joining the device linking outputs to the host
7171
/// action. The same graph should be generated when no -fsycl-targets is used
72+
/// The same phase graph will be used with -fsycl-use-bitcode
7273
// RUN: %clang -ccc-print-phases -target x86_64-unknown-linux-gnu -fsycl -fsycl-targets=spir64-unknown-linux-sycldevice %s 2>&1 \
7374
// RUN: | FileCheck -check-prefix=CHK-PHASES %s
74-
// RUN: %clang -ccc-print-phases -target x86_64-unknown-linux-gnu -fsycl %s 2>&1 \
75+
// RUN: %clang -ccc-print-phases -target x86_64-unknown-linux-gnu -fsycl -fno-sycl-use-bitcode %s 2>&1 \
76+
// RUN: | FileCheck -check-prefix=CHK-PHASES %s
77+
// RUN: %clang -ccc-print-phases -target x86_64-unknown-linux-gnu -fsycl -fsycl-use-bitcode %s 2>&1 \
7578
// RUN: | FileCheck -check-prefix=CHK-PHASES %s
7679
// CHK-PHASES: 0: input, "[[INPUT:.+\.c]]", c, (host-sycl)
7780
// CHK-PHASES: 1: preprocessor, {0}, cpp-output, (host-sycl)
@@ -214,10 +217,20 @@
214217
/// ###########################################################################
215218

216219
/// Check -fsycl-is-device is passed when compiling for the device.
220+
/// also check for SPIR-V binary creation
217221
// RUN: %clang -### -no-canonical-prefixes -fsycl -fsycl-targets=spir64-unknown-linux-sycldevice %s 2>&1 \
218222
// RUN: | FileCheck -check-prefix=CHK-FSYCL-IS-DEVICE %s
219223

220-
// CHK-FSYCL-IS-DEVICE: clang{{.*}} "-fsycl-is-device" {{.*}}.c
224+
// CHK-FSYCL-IS-DEVICE: clang{{.*}} "-fsycl-is-device" {{.*}} "-emit-spirv" {{.*}}.c
225+
226+
/// ###########################################################################
227+
228+
/// Check -fsycl-is-device and emitting to .spv when compiling for the device
229+
/// when using -fno-sycl-use-bitcode
230+
// RUN: %clang -### -fno-sycl-use-bitcode -fsycl -fsycl-targets=spir64-unknown-linux-sycldevice %s 2>&1 \
231+
// RUN: | FileCheck -check-prefix=CHK-FSYCL-IS-DEVICE-NO-BITCODE %s
232+
233+
// CHK-FSYCL-IS-DEVICE-NO-BITCODE: clang{{.*}} "-fsycl-is-device" {{.*}} "-emit-spirv" {{.*}}.c
221234

222235
/// ###########################################################################
223236

@@ -257,3 +270,4 @@
257270
// CHK-ADD-TARGETS-UB: 3: input, "dummy.spv", sycl-fatbin, (device-sycl)
258271
// CHK-ADD-TARGETS-UB: 4: clang-offload-wrapper, {3}, object, (device-sycl)
259272
// CHK-ADD-TARGETS-UB: 5: offload, "host-sycl (x86_64-unknown-linux-gnu)" {2}, "device-sycl (spir64-unknown-linux-sycldevice)" {4}, image
273+

sycl/doc/GetStartedWithSYCLCompiler.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ The SYCL Compiler supports two types of compilation:
132132
a. Compile the device code from the C++ file into the SPIR-V file:
133133

134134
```bash
135-
clang++ --sycl -Xclang -fsycl-int-header=simple-sycl-app-int-header.h -c simple-sycl-app.cpp -o kernel.spv
135+
clang++ --sycl -fno-sycl-use-bitcode -Xclang -fsycl-int-header=simple-sycl-app-int-header.h -c simple-sycl-app.cpp -o kernel.spv
136136
# NOTE: The section "-Xclang -fsycl-int-header=simple-sycl-app-int-header.h"
137137
# generates `integration header` file.
138138
# This file must be included for the host side compilation.

sycl/test/aot/with-llvm-bc.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//
66
// Only CPU supports LLVM IR bitcode as a binary
77
// RUN: %CPU_RUN_PLACEHOLDER %t.out
8-
// XFAIL: *
98

109
//==----- with-llvm-bc.cpp - SYCL kernel with LLVM IR bitcode as binary ----==//
1110
//

sycl/test/separate-compile/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// >> ---- compile src1
22
// >> device compilation...
3-
// RUN: %clang -std=c++11 --sycl -Xclang -fsycl-int-header=sycl_ihdr_a.h %s -c -o a_kernel.spv
3+
// RUN: %clang -std=c++11 -fno-sycl-use-bitcode --sycl -Xclang -fsycl-int-header=sycl_ihdr_a.h %s -c -o a_kernel.spv
44
// >> host compilation...
55
// RUN: %clang -std=c++11 -include sycl_ihdr_a.h -g -c %s -o a.o
66
//
77
// >> ---- compile src2
88
// >> device compilation...
9-
// RUN: %clang -DB_CPP=1 -std=c++11 --sycl -Xclang -fsycl-int-header=sycl_ihdr_b.h %s -c -o b_kernel.spv
9+
// RUN: %clang -DB_CPP=1 -std=c++11 -fno-sycl-use-bitcode --sycl -Xclang -fsycl-int-header=sycl_ihdr_b.h %s -c -o b_kernel.spv
1010
// >> host compilation...
1111
// RUN: %clang -DB_CPP=1 -std=c++11 -include sycl_ihdr_b.h -g -c %s -o b.o
1212
//

0 commit comments

Comments
 (0)