Skip to content

Commit 49498a5

Browse files
author
git apple-llvm automerger
committed
Merge commit 'd00f65c6acd9' from llvm.org/main into next
2 parents 700caa8 + d00f65c commit 49498a5

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
@@ -95,6 +95,7 @@ class Action {
9595
OFK_Cuda = 0x02,
9696
OFK_OpenMP = 0x04,
9797
OFK_HIP = 0x08,
98+
OFK_SYCL = 0x10,
9899
};
99100

100101
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">,
@@ -6988,16 +6989,21 @@ defm : FlangIgnoredDiagOpt<"frontend-loop-interchange">;
69886989
defm : FlangIgnoredDiagOpt<"target-lifetime">;
69896990

69906991
// C++ SYCL options
6992+
let Group = sycl_Group in {
69916993
def fsycl : Flag<["-"], "fsycl">,
6992-
Visibility<[ClangOption, CLOption]>,
6993-
Group<sycl_Group>, HelpText<"Enables SYCL kernels compilation for device">;
6994+
HelpText<"Enable SYCL C++ extensions">;
69946995
def fno_sycl : Flag<["-"], "fno-sycl">,
6995-
Visibility<[ClangOption, CLOption]>,
6996-
Group<sycl_Group>, HelpText<"Disables SYCL kernels compilation for device">;
6996+
HelpText<"Disable SYCL C++ extensions">;
6997+
def fsycl_device_only : Flag<["-"], "fsycl-device-only">,
6998+
Alias<offload_device_only>, HelpText<"Compile SYCL code for device only">;
6999+
def fsycl_host_only : Flag<["-"], "fsycl-host-only">,
7000+
Alias<offload_host_only>, HelpText<"Compile SYCL code for host only. Has no "
7001+
"effect on non-SYCL compilations">;
69977002
def sycl_link : Flag<["--"], "sycl-link">, Flags<[HelpHidden]>,
6998-
Visibility<[ClangOption, CLOption]>,
6999-
Group<sycl_Group>, HelpText<"Perform link through clang-sycl-linker via the target "
7003+
HelpText<"Perform link through clang-sycl-linker via the target "
70007004
"offloading toolchain.">;
7005+
} // let Group = sycl_Group
7006+
70017007
// OS-specific options
70027008
let Flags = [TargetSpecific] in {
70037009
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
@@ -112,6 +112,8 @@ std::string Action::getOffloadingKindPrefix() const {
112112
return "device-openmp";
113113
case OFK_HIP:
114114
return "device-hip";
115+
case OFK_SYCL:
116+
return "device-sycl";
115117

116118
// TODO: Add other programming models here.
117119
}
@@ -129,6 +131,8 @@ std::string Action::getOffloadingKindPrefix() const {
129131
Res += "-hip";
130132
if (ActiveOffloadKindMask & OFK_OpenMP)
131133
Res += "-openmp";
134+
if (ActiveOffloadKindMask & OFK_SYCL)
135+
Res += "-sycl";
132136

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

@@ -165,6 +169,8 @@ StringRef Action::GetOffloadKindName(OffloadKind Kind) {
165169
return "openmp";
166170
case OFK_HIP:
167171
return "hip";
172+
case OFK_SYCL:
173+
return "sycl";
168174

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

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

clang/lib/Driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ add_clang_library(clangDriver
8080
ToolChains/Solaris.cpp
8181
ToolChains/SPIRV.cpp
8282
ToolChains/SPIRVOpenMP.cpp
83+
ToolChains/SYCL.cpp
8384
ToolChains/TCE.cpp
8485
ToolChains/UEFI.cpp
8586
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
//
@@ -4236,6 +4297,7 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
42364297

42374298
bool UseNewOffloadingDriver =
42384299
C.isOffloadingHostKind(Action::OFK_OpenMP) ||
4300+
C.isOffloadingHostKind(Action::OFK_SYCL) ||
42394301
Args.hasFlag(options::OPT_foffload_via_llvm,
42404302
options::OPT_fno_offload_via_llvm, false) ||
42414303
Args.hasFlag(options::OPT_offload_new_driver,
@@ -4661,6 +4723,8 @@ Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
46614723
Archs.insert(OffloadArchToString(OffloadArch::HIPDefault));
46624724
else if (Kind == Action::OFK_OpenMP)
46634725
Archs.insert(StringRef());
4726+
else if (Kind == Action::OFK_SYCL)
4727+
Archs.insert(StringRef());
46644728
} else {
46654729
Args.ClaimAllArgs(options::OPT_offload_arch_EQ);
46664730
Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);
@@ -4685,7 +4749,7 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
46854749
OffloadAction::DeviceDependences DDeps;
46864750

46874751
const Action::OffloadKind OffloadKinds[] = {
4688-
Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP};
4752+
Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP, Action::OFK_SYCL};
46894753

46904754
for (Action::OffloadKind Kind : OffloadKinds) {
46914755
SmallVector<const ToolChain *, 2> ToolChains;
@@ -4722,6 +4786,15 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
47224786
if (DeviceActions.empty())
47234787
return HostAction;
47244788

4789+
// FIXME: Do not collapse the host side for Darwin targets with SYCL offload
4790+
// compilations. The toolchain is not properly initialized for the target.
4791+
if (isa<CompileJobAction>(HostAction) && Kind == Action::OFK_SYCL &&
4792+
HostAction->getType() != types::TY_Nothing &&
4793+
C.getSingleOffloadToolChain<Action::OFK_Host>()
4794+
->getTriple()
4795+
.isOSDarwin())
4796+
HostAction->setCannotBeCollapsedWithNextDependentAction();
4797+
47254798
auto PL = types::getCompilationPhases(*this, Args, InputType);
47264799

47274800
for (phases::ID Phase : PL) {
@@ -4730,6 +4803,11 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
47304803
break;
47314804
}
47324805

4806+
// Assemble actions are not used for the SYCL device side. Both compile
4807+
// and backend actions are used to generate IR and textual IR if needed.
4808+
if (Kind == Action::OFK_SYCL && Phase == phases::Assemble)
4809+
continue;
4810+
47334811
auto TCAndArch = TCAndArchs.begin();
47344812
for (Action *&A : DeviceActions) {
47354813
if (A->getType() == types::TY_Nothing)
@@ -4975,6 +5053,7 @@ Action *Driver::ConstructPhaseAction(
49755053
return C.MakeAction<BackendJobAction>(Input, Output);
49765054
}
49775055
if (Args.hasArg(options::OPT_emit_llvm) ||
5056+
TargetDeviceOffloadKind == Action::OFK_SYCL ||
49785057
(((Input->getOffloadingToolChain() &&
49795058
Input->getOffloadingToolChain()->getTriple().isAMDGPU()) ||
49805059
TargetDeviceOffloadKind == Action::OFK_HIP) &&
@@ -6680,11 +6759,16 @@ const ToolChain &Driver::getOffloadingDeviceToolChain(
66806759
HostTC, Args);
66816760
break;
66826761
}
6762+
case Action::OFK_SYCL:
6763+
if (Target.isSPIROrSPIRV())
6764+
TC = std::make_unique<toolchains::SYCLToolChain>(*this, Target, HostTC,
6765+
Args);
6766+
break;
66836767
default:
66846768
break;
66856769
}
66866770
}
6687-
6771+
assert(TC && "Could not create offloading device tool chain.");
66886772
return *TC;
66896773
}
66906774

clang/lib/Driver/ToolChain.cpp

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

1490+
void ToolChain::addSYCLIncludeArgs(const ArgList &DriverArgs,
1491+
ArgStringList &CC1Args) const {}
1492+
14901493
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
14911494
ToolChain::getDeviceLibs(const ArgList &DriverArgs) const {
14921495
return {};

0 commit comments

Comments
 (0)