Skip to content

Commit 16746bc

Browse files
RuykDavid Wood
andcommitted
[SYCL] Driver option to select SYCL version
User can select the version of SYCL the compiler will use via the flag -sycl-std, similar to -cl-std. The flag defines the LangOpts.SYCLVersion option to the version of SYCL. The default value is undefined. If driver is building SYCL code, flag is set to the default SYCL version (1.2.1) The preprocessor uses this variable to define CL_SYCL_LANGUAGE_VERSION macro, which should be defined according to SYCL 1.2.1 standard. Only valid value at this point for the flag is 1.2.1. Co-Authored-By: David Wood <[email protected]> Signed-off-by: Ruyman Reyes <[email protected]>
1 parent 772118f commit 16746bc

File tree

8 files changed

+61
-1
lines changed

8 files changed

+61
-1
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ LANGOPT(OpenCL , 1, 0, "OpenCL")
199199
LANGOPT(OpenCLVersion , 32, 0, "OpenCL C version")
200200
LANGOPT(OpenCLCPlusPlus , 1, 0, "OpenCL C++")
201201
LANGOPT(OpenCLCPlusPlusVersion , 32, 0, "OpenCL C++ version")
202+
ENUM_LANGOPT(SYCLVersion, SYCLVersionList, 4, SYCLVersionList::undefined, "Version of the SYCL standard used")
202203
LANGOPT(NativeHalfType , 1, 0, "Native half type support")
203204
LANGOPT(NativeHalfArgsAndReturns, 1, 0, "Native half args and returns")
204205
LANGOPT(HalfArgsAndReturns, 1, 0, "half args and returns")

clang/include/clang/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ class LangOptions : public LangOptionsBase {
113113
MSVC2017_7 = 1914,
114114
};
115115

116+
enum class SYCLVersionList {
117+
sycl_1_2_1,
118+
undefined
119+
};
120+
116121
/// Clang versions with different platform ABI conformance.
117122
enum class ClangABI {
118123
/// Attempt to be ABI-compatible with code generated by Clang 3.8.x

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ def pedantic_Group : OptionGroup<"<pedantic group>">, Group<f_Group>,
124124
def opencl_Group : OptionGroup<"<opencl group>">, Group<f_Group>,
125125
DocName<"OpenCL flags">;
126126

127+
def sycl_Group : OptionGroup<"<sycl group>">, Group<f_Group>,
128+
DocName<"SYCL flags">;
129+
127130
def m_Group : OptionGroup<"<m group>">, Group<CompileOnly_Group>,
128131
DocName<"Target-dependent compilation options">;
129132

@@ -534,6 +537,8 @@ def cl_no_signed_zeros : Flag<["-"], "cl-no-signed-zeros">, Group<opencl_Group>,
534537
HelpText<"OpenCL only. Allow use of less precise no signed zeros computations in the generated binary.">;
535538
def cl_std_EQ : Joined<["-"], "cl-std=">, Group<opencl_Group>, Flags<[CC1Option]>,
536539
HelpText<"OpenCL language standard to compile for.">, Values<"cl,CL,cl1.1,CL1.1,cl1.2,CL1.2,cl2.0,CL2.0,c++">;
540+
def sycl_std_EQ : Joined<["-"], "sycl-std=">, Group<sycl_Group>, Flags<[CC1Option]>,
541+
HelpText<"SYCL language standard to compile for.">, Values<"1.2.1">;
537542
def cl_denorms_are_zero : Flag<["-"], "cl-denorms-are-zero">, Group<opencl_Group>, Flags<[CC1Option]>,
538543
HelpText<"OpenCL only. Allow denormals to be flushed to zero.">;
539544
def cl_fp32_correctly_rounded_divide_sqrt : Flag<["-"], "cl-fp32-correctly-rounded-divide-sqrt">, Group<opencl_Group>, Flags<[CC1Option]>,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,6 +3565,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
35653565
}
35663566
}
35673567

3568+
if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) {
3569+
A->render(Args, CmdArgs);
3570+
} else if (IsSYCL) {
3571+
// Ensure the default version in SYCL mode is 1.2.1
3572+
CmdArgs.push_back("-sycl-std=1.2.1");
3573+
}
3574+
35683575
if (IsOpenMPDevice) {
35693576
// We have to pass the triple of the host if compiling for an OpenMP device.
35703577
std::string NormalizedTriple =
@@ -4720,6 +4727,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
47204727
// Forward -cl options to -cc1
47214728
RenderOpenCLOptions(Args, CmdArgs);
47224729

4730+
// Forward -sycl options to -cc1
4731+
Args.AddLastArg(CmdArgs, options::OPT_sycl_std_EQ);
4732+
47234733
if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) {
47244734
CmdArgs.push_back(
47254735
Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,20 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
24212421
LangStd = OpenCLLangStd;
24222422
}
24232423

2424+
// -sycl-std applies to any SYCL source, not only those containing kernels,
2425+
// but also those using the SYCL API
2426+
if(const Arg *A = Args.getLastArg(OPT_sycl_std_EQ)) {
2427+
Opts.setSYCLVersion(llvm::StringSwitch<LangOptions::SYCLVersionList>(A->getValue())
2428+
.Cases("1.2.1", "121", "sycl-1.2.1", LangOptions::SYCLVersionList::sycl_1_2_1)
2429+
.Default(LangOptions::SYCLVersionList::undefined));
2430+
2431+
if (Opts.getSYCLVersion() == LangOptions::SYCLVersionList::undefined) {
2432+
// User has passed an invalid value to the flag, this is an error
2433+
Diags.Report(diag::err_drv_invalid_value)
2434+
<< A->getAsString(Args) << A->getValue();
2435+
}
2436+
}
2437+
24242438
Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
24252439
Opts.DeclareOpenCLBuiltins = Args.hasArg(OPT_fdeclare_opencl_builtins);
24262440

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,18 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
449449
Builder.defineMacro("__FAST_RELAXED_MATH__");
450450
}
451451
}
452+
453+
// SYCL Version is set to a value when building SYCL applications
454+
switch (LangOpts.getSYCLVersion()) {
455+
case LangOptions::SYCLVersionList::sycl_1_2_1:
456+
Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
457+
break;
458+
case LangOptions::SYCLVersionList::undefined:
459+
default:
460+
// This is not a SYCL source, nothing to add
461+
break;
462+
}
463+
452464
// Not "standard" per se, but available even with the -undef flag.
453465
if (LangOpts.AsmPreprocessor)
454466
Builder.defineMacro("__ASSEMBLER__");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// RUN: %clang %s --sycl -sycl-std=1.2.1 -dM -E -x c++ | FileCheck %s
2+
// CHECK: #define CL_SYCL_LANGUAGE_VERSION 121

sycl/doc/SYCL_compiler_and_runtime_design.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ targets like SPIR-V).
152152

153153
#### Enable SYCL offload
154154

155-
To enable compilation with SYCL specification conformance, a special option
155+
To enable compilation following the SYCL specification, a special option
156156
must be passed to the clang driver:
157157

158158
`-fsycl`
@@ -162,6 +162,17 @@ number of device compilers for targets specified in the `-fsycl-targets`
162162
option. If this option is not specified, then single SPIR-V target is assumed,
163163
and single device compiler for this target is invoked.
164164

165+
In the driver, the following bools are defined to determine the compilation
166+
mode in SYCL:
167+
168+
* IsSYCL : True if the user has passed `-fsycl` or `-sycl` to the compilation
169+
* IsSYCLOffloadDevice: True if calling clang to set up a device compilation
170+
* IsSYCLHost: True if setting up a call to clang to do a host compilation
171+
172+
The additional option `-sycl-std` allows specifiying which version of
173+
the SYCL standard will be used for the compilation.
174+
The default value for this option is `1.2.1`.
175+
165176
#### Ahead of time (AOT) compilation
166177

167178
Ahead-of-time compilation is the process of invoking the back-end at compile

0 commit comments

Comments
 (0)