Skip to content

Commit d00f65c

Browse files
authored
[Driver][SYCL] Add initial SYCL offload compilation support (#117268)
Introduces the SYCL based toolchain and initial toolchain construction when using the '-fsycl' option. This option will enable SYCL based offloading, creating a SPIR-V based IR file packaged into the compiled host object. This includes early support for creating the host/device object using the new offloading model. The device object is created using the spir64-unknown-unknown target triple. New/Updated Options: -fsycl Enables SYCL offloading for host and device -fsycl-device-only Enables device only compilation for SYCL -fsycl-host-only Enables host only compilation for SYCL RFC Reference: https://discourse.llvm.org/t/rfc-sycl-driver-enhancements/74092 This is a reland of: #107493
1 parent 98b3191 commit d00f65c

File tree

21 files changed

+495
-29
lines changed

21 files changed

+495
-29
lines changed

clang/include/clang/Driver/Action.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Action {
9494
OFK_Cuda = 0x02,
9595
OFK_OpenMP = 0x04,
9696
OFK_HIP = 0x08,
97+
OFK_SYCL = 0x10,
9798
};
9899

99100
static const char *getClassName(ActionClass AC);

clang/include/clang/Driver/Options.td

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def opencl_Group : OptionGroup<"<opencl group>">, Group<f_Group>,
182182
DocName<"OpenCL options">;
183183

184184
def sycl_Group : OptionGroup<"<SYCL group>">, Group<f_Group>,
185-
DocName<"SYCL options">;
185+
DocName<"SYCL options">,
186+
Visibility<[ClangOption, CLOption]>;
186187

187188
def cuda_Group : OptionGroup<"<CUDA group>">, Group<f_Group>,
188189
DocName<"CUDA options">,
@@ -6839,16 +6840,21 @@ defm : FlangIgnoredDiagOpt<"frontend-loop-interchange">;
68396840
defm : FlangIgnoredDiagOpt<"target-lifetime">;
68406841

68416842
// C++ SYCL options
6843+
let Group = sycl_Group in {
68426844
def fsycl : Flag<["-"], "fsycl">,
6843-
Visibility<[ClangOption, CLOption]>,
6844-
Group<sycl_Group>, HelpText<"Enables SYCL kernels compilation for device">;
6845+
HelpText<"Enable SYCL C++ extensions">;
68456846
def fno_sycl : Flag<["-"], "fno-sycl">,
6846-
Visibility<[ClangOption, CLOption]>,
6847-
Group<sycl_Group>, HelpText<"Disables SYCL kernels compilation for device">;
6847+
HelpText<"Disable SYCL C++ extensions">;
6848+
def fsycl_device_only : Flag<["-"], "fsycl-device-only">,
6849+
Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">;
6850+
def fsycl_host_only : Flag<["-"], "fsycl-host-only">,
6851+
Alias<offload_host_only>, HelpText<"Compile SYCL code for host only. Has no "
6852+
"effect on non-SYCL compilations">;
68486853
def sycl_link : Flag<["--"], "sycl-link">, Flags<[HelpHidden]>,
6849-
Visibility<[ClangOption, CLOption]>,
6850-
Group<sycl_Group>, HelpText<"Perform link through clang-sycl-linker via the target "
6854+
HelpText<"Perform link through clang-sycl-linker via the target "
68516855
"offloading toolchain.">;
6856+
} // let Group = sycl_Group
6857+
68526858
// OS-specific options
68536859
let Flags = [TargetSpecific] in {
68546860
defm android_pad_segment : BooleanFFlag<"android-pad-segment">, Group<f_Group>;

clang/include/clang/Driver/ToolChain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,10 @@ class ToolChain {
762762
virtual void AddHIPIncludeArgs(const llvm::opt::ArgList &DriverArgs,
763763
llvm::opt::ArgStringList &CC1Args) const;
764764

765+
/// Add arguments to use system-specific SYCL includes.
766+
virtual void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs,
767+
llvm::opt::ArgStringList &CC1Args) const;
768+
765769
/// Add arguments to use MCU GCC toolchain includes.
766770
virtual void AddIAMCUIncludeArgs(const llvm::opt::ArgList &DriverArgs,
767771
llvm::opt::ArgStringList &CC1Args) const;

clang/lib/Driver/Action.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ std::string Action::getOffloadingKindPrefix() const {
111111
return "device-openmp";
112112
case OFK_HIP:
113113
return "device-hip";
114+
case OFK_SYCL:
115+
return "device-sycl";
114116

115117
// TODO: Add other programming models here.
116118
}
@@ -128,6 +130,8 @@ std::string Action::getOffloadingKindPrefix() const {
128130
Res += "-hip";
129131
if (ActiveOffloadKindMask & OFK_OpenMP)
130132
Res += "-openmp";
133+
if (ActiveOffloadKindMask & OFK_SYCL)
134+
Res += "-sycl";
131135

132136
// TODO: Add other programming models here.
133137

@@ -164,6 +168,8 @@ StringRef Action::GetOffloadKindName(OffloadKind Kind) {
164168
return "openmp";
165169
case OFK_HIP:
166170
return "hip";
171+
case OFK_SYCL:
172+
return "sycl";
167173

168174
// TODO: Add other programming models here.
169175
}
@@ -320,7 +326,7 @@ void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
320326
DeviceBoundArchs.push_back(BoundArch);
321327

322328
// Add each active offloading kind from a mask.
323-
for (OffloadKind OKind : {OFK_OpenMP, OFK_Cuda, OFK_HIP})
329+
for (OffloadKind OKind : {OFK_OpenMP, OFK_Cuda, OFK_HIP, OFK_SYCL})
324330
if (OKind & OffloadKindMask)
325331
DeviceOffloadKinds.push_back(OKind);
326332
}

clang/lib/Driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ add_clang_library(clangDriver
7878
ToolChains/Solaris.cpp
7979
ToolChains/SPIRV.cpp
8080
ToolChains/SPIRVOpenMP.cpp
81+
ToolChains/SYCL.cpp
8182
ToolChains/TCE.cpp
8283
ToolChains/UEFI.cpp
8384
ToolChains/VEToolchain.cpp

clang/lib/Driver/Compilation.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,11 @@ static bool ActionFailed(const Action *A,
214214
if (FailingCommands.empty())
215215
return false;
216216

217-
// CUDA/HIP can have the same input source code compiled multiple times so do
218-
// not compiled again if there are already failures. It is OK to abort the
219-
// CUDA pipeline on errors.
220-
if (A->isOffloading(Action::OFK_Cuda) || A->isOffloading(Action::OFK_HIP))
217+
// CUDA/HIP/SYCL can have the same input source code compiled multiple times
218+
// so do not compile again if there are already failures. It is OK to abort
219+
// the CUDA/HIP/SYCL pipeline on errors.
220+
if (A->isOffloading(Action::OFK_Cuda) || A->isOffloading(Action::OFK_HIP) ||
221+
A->isOffloading(Action::OFK_SYCL))
221222
return true;
222223

223224
for (const auto &CI : FailingCommands)

clang/lib/Driver/Driver.cpp

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "ToolChains/RISCVToolchain.h"
4545
#include "ToolChains/SPIRV.h"
4646
#include "ToolChains/SPIRVOpenMP.h"
47+
#include "ToolChains/SYCL.h"
4748
#include "ToolChains/Solaris.h"
4849
#include "ToolChains/TCE.h"
4950
#include "ToolChains/UEFI.h"
@@ -781,6 +782,35 @@ Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const {
781782
return RT;
782783
}
783784

785+
static llvm::Triple getSYCLDeviceTriple(StringRef TargetArch) {
786+
SmallVector<StringRef, 5> SYCLAlias = {"spir", "spir64", "spirv", "spirv32",
787+
"spirv64"};
788+
if (llvm::is_contained(SYCLAlias, TargetArch)) {
789+
llvm::Triple TargetTriple;
790+
TargetTriple.setArchName(TargetArch);
791+
TargetTriple.setVendor(llvm::Triple::UnknownVendor);
792+
TargetTriple.setOS(llvm::Triple::UnknownOS);
793+
return TargetTriple;
794+
}
795+
return llvm::Triple(TargetArch);
796+
}
797+
798+
static bool addSYCLDefaultTriple(Compilation &C,
799+
SmallVectorImpl<llvm::Triple> &SYCLTriples) {
800+
// Check current set of triples to see if the default has already been set.
801+
for (const auto &SYCLTriple : SYCLTriples) {
802+
if (SYCLTriple.getSubArch() == llvm::Triple::NoSubArch &&
803+
SYCLTriple.isSPIROrSPIRV())
804+
return false;
805+
}
806+
// Add the default triple as it was not found.
807+
llvm::Triple DefaultTriple = getSYCLDeviceTriple(
808+
C.getDefaultToolChain().getTriple().isArch32Bit() ? "spirv32"
809+
: "spirv64");
810+
SYCLTriples.insert(SYCLTriples.begin(), DefaultTriple);
811+
return true;
812+
}
813+
784814
void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
785815
InputList &Inputs) {
786816

@@ -842,7 +872,6 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
842872
return;
843873
auto *HIPTC = &getOffloadingDeviceToolChain(C.getInputArgs(), *HIPTriple,
844874
*HostTC, OFK);
845-
assert(HIPTC && "Could not create offloading device tool chain.");
846875
C.addOffloadDeviceToolChain(HIPTC, OFK);
847876
}
848877

@@ -997,6 +1026,38 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
9971026
return;
9981027
}
9991028

1029+
// We need to generate a SYCL toolchain if the user specified -fsycl.
1030+
bool IsSYCL = C.getInputArgs().hasFlag(options::OPT_fsycl,
1031+
options::OPT_fno_sycl, false);
1032+
1033+
auto argSYCLIncompatible = [&](OptSpecifier OptId) {
1034+
if (!IsSYCL)
1035+
return;
1036+
if (Arg *IncompatArg = C.getInputArgs().getLastArg(OptId))
1037+
Diag(clang::diag::err_drv_argument_not_allowed_with)
1038+
<< IncompatArg->getSpelling() << "-fsycl";
1039+
};
1040+
// -static-libstdc++ is not compatible with -fsycl.
1041+
argSYCLIncompatible(options::OPT_static_libstdcxx);
1042+
// -ffreestanding cannot be used with -fsycl
1043+
argSYCLIncompatible(options::OPT_ffreestanding);
1044+
1045+
llvm::SmallVector<llvm::Triple, 4> UniqueSYCLTriplesVec;
1046+
1047+
if (IsSYCL) {
1048+
addSYCLDefaultTriple(C, UniqueSYCLTriplesVec);
1049+
1050+
// We'll need to use the SYCL and host triples as the key into
1051+
// getOffloadingDeviceToolChain, because the device toolchains we're
1052+
// going to create will depend on both.
1053+
const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
1054+
for (const auto &TargetTriple : UniqueSYCLTriplesVec) {
1055+
auto SYCLTC = &getOffloadingDeviceToolChain(
1056+
C.getInputArgs(), TargetTriple, *HostTC, Action::OFK_SYCL);
1057+
C.addOffloadDeviceToolChain(SYCLTC, Action::OFK_SYCL);
1058+
}
1059+
}
1060+
10001061
//
10011062
// TODO: Add support for other offloading programming models here.
10021063
//
@@ -4234,6 +4295,7 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
42344295

42354296
bool UseNewOffloadingDriver =
42364297
C.isOffloadingHostKind(Action::OFK_OpenMP) ||
4298+
C.isOffloadingHostKind(Action::OFK_SYCL) ||
42374299
Args.hasFlag(options::OPT_foffload_via_llvm,
42384300
options::OPT_fno_offload_via_llvm, false) ||
42394301
Args.hasFlag(options::OPT_offload_new_driver,
@@ -4651,6 +4713,8 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
46514713
Archs.insert(OffloadArchToString(OffloadArch::HIPDefault));
46524714
else if (Kind == Action::OFK_OpenMP)
46534715
Archs.insert(StringRef());
4716+
else if (Kind == Action::OFK_SYCL)
4717+
Archs.insert(StringRef());
46544718
} else {
46554719
Args.ClaimAllArgs(options::OPT_offload_arch_EQ);
46564720
Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);
@@ -4675,7 +4739,7 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
46754739
OffloadAction::DeviceDependences DDeps;
46764740

46774741
const Action::OffloadKind OffloadKinds[] = {
4678-
Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP};
4742+
Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP, Action::OFK_SYCL};
46794743

46804744
for (Action::OffloadKind Kind : OffloadKinds) {
46814745
SmallVector<const ToolChain *, 2> ToolChains;
@@ -4712,6 +4776,15 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
47124776
if (DeviceActions.empty())
47134777
return HostAction;
47144778

4779+
// FIXME: Do not collapse the host side for Darwin targets with SYCL offload
4780+
// compilations. The toolchain is not properly initialized for the target.
4781+
if (isa<CompileJobAction>(HostAction) && Kind == Action::OFK_SYCL &&
4782+
HostAction->getType() != types::TY_Nothing &&
4783+
C.getSingleOffloadToolChain<Action::OFK_Host>()
4784+
->getTriple()
4785+
.isOSDarwin())
4786+
HostAction->setCannotBeCollapsedWithNextDependentAction();
4787+
47154788
auto PL = types::getCompilationPhases(*this, Args, InputType);
47164789

47174790
for (phases::ID Phase : PL) {
@@ -4720,6 +4793,11 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
47204793
break;
47214794
}
47224795

4796+
// Assemble actions are not used for the SYCL device side. Both compile
4797+
// and backend actions are used to generate IR and textual IR if needed.
4798+
if (Kind == Action::OFK_SYCL && Phase == phases::Assemble)
4799+
continue;
4800+
47234801
auto TCAndArch = TCAndArchs.begin();
47244802
for (Action *&A : DeviceActions) {
47254803
if (A->getType() == types::TY_Nothing)
@@ -4958,6 +5036,7 @@ Action *Driver::ConstructPhaseAction(
49585036
return C.MakeAction<BackendJobAction>(Input, Output);
49595037
}
49605038
if (Args.hasArg(options::OPT_emit_llvm) ||
5039+
TargetDeviceOffloadKind == Action::OFK_SYCL ||
49615040
(((Input->getOffloadingToolChain() &&
49625041
Input->getOffloadingToolChain()->getTriple().isAMDGPU()) ||
49635042
TargetDeviceOffloadKind == Action::OFK_HIP) &&
@@ -6644,11 +6723,16 @@ const ToolChain &Driver::getOffloadingDeviceToolChain(
66446723
HostTC, Args);
66456724
break;
66466725
}
6726+
case Action::OFK_SYCL:
6727+
if (Target.isSPIROrSPIRV())
6728+
TC = std::make_unique<toolchains::SYCLToolChain>(*this, Target, HostTC,
6729+
Args);
6730+
break;
66476731
default:
66486732
break;
66496733
}
66506734
}
6651-
6735+
assert(TC && "Could not create offloading device tool chain.");
66526736
return *TC;
66536737
}
66546738

clang/lib/Driver/ToolChain.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,9 @@ void ToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
14851485
void ToolChain::AddHIPIncludeArgs(const ArgList &DriverArgs,
14861486
ArgStringList &CC1Args) const {}
14871487

1488+
void ToolChain::addSYCLIncludeArgs(const ArgList &DriverArgs,
1489+
ArgStringList &CC1Args) const {}
1490+
14881491
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
14891492
ToolChain::getDeviceLibs(const ArgList &DriverArgs) const {
14901493
return {};

0 commit comments

Comments
 (0)