Skip to content

Commit 118b057

Browse files
Ruykbader
authored andcommitted
[SYCL] Driver option to select SYCL version
Summary: 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]> Subscribers: ebevhan, Anastasia, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72857
1 parent ec1d1f6 commit 118b057

File tree

9 files changed

+58
-8
lines changed

9 files changed

+58
-8
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ LANGOPT(GPURelocatableDeviceCode, 1, 0, "generate relocatable device code")
230230
LANGOPT(GPUAllowDeviceInit, 1, 0, "allowing device side global init functions for HIP")
231231
LANGOPT(GPUMaxThreadsPerBlock, 32, 256, "default max threads per block for kernel launch bounds for HIP")
232232

233+
LANGOPT(SYCL , 1, 0, "SYCL")
233234
LANGOPT(SYCLIsDevice , 1, 0, "Generate code for SYCL device")
235+
LANGOPT(SYCLVersion , 32, 0, "Version of the SYCL standard used")
234236

235237
LANGOPT(HIPUseNewLaunchAPI, 1, 0, "Use new kernel launching API for HIP")
236238

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,10 +3422,12 @@ defm underscoring : BooleanFFlag<"underscoring">, Group<gfortran_Group>;
34223422
defm whole_file : BooleanFFlag<"whole-file">, Group<gfortran_Group>;
34233423

34243424
// C++ SYCL options
3425-
def fsycl : Flag<["-"], "fsycl">, Group<sycl_Group>,
3425+
def fsycl : Flag<["-"], "fsycl">, Group<sycl_Group>, Flags<[CC1Option, CoreOption]>,
34263426
HelpText<"Enable SYCL kernels compilation for device">;
3427-
def fno_sycl : Flag<["-"], "fno-sycl">, Group<sycl_Group>,
3427+
def fno_sycl : Flag<["-"], "fno-sycl">, Group<sycl_Group>, Flags<[CoreOption]>,
34283428
HelpText<"Disable SYCL kernels compilation for device">;
3429+
def sycl_std_EQ : Joined<["-"], "sycl-std=">, Group<sycl_Group>, Flags<[CC1Option, NoArgumentUnused, CoreOption]>,
3430+
HelpText<"SYCL language standard to compile for.">, Values<"2017, 121, 1.2.1, sycl-1.2.1">;
34293431

34303432
include "CC1Options.td"
34313433

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4048,9 +4048,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
40484048
CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
40494049
}
40504050

4051-
if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false))
4051+
if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) {
4052+
CmdArgs.push_back("-fsycl");
40524053
CmdArgs.push_back("-fsycl-is-device");
40534054

4055+
if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) {
4056+
A->render(Args, CmdArgs);
4057+
} else {
4058+
// Ensure the default version in SYCL mode is 1.2.1 (aka 2017)
4059+
CmdArgs.push_back("-sycl-std=2017");
4060+
}
4061+
}
4062+
40544063
if (IsOpenMPDevice) {
40554064
// We have to pass the triple of the host if compiling for an OpenMP device.
40564065
std::string NormalizedTriple =

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,24 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
25442544
LangStd = OpenCLLangStd;
25452545
}
25462546

2547+
Opts.SYCL = Args.hasArg(options::OPT_fsycl);
2548+
Opts.SYCLIsDevice = Opts.SYCL && Args.hasArg(options::OPT_fsycl_is_device);
2549+
if (Opts.SYCL) {
2550+
// -sycl-std applies to any SYCL source, not only those containing kernels,
2551+
// but also those using the SYCL API
2552+
if (const Arg *A = Args.getLastArg(OPT_sycl_std_EQ)) {
2553+
Opts.SYCLVersion = llvm::StringSwitch<unsigned>(A->getValue())
2554+
.Cases("2017", "1.2.1", "121", "sycl-1.2.1", 2017)
2555+
.Default(0U);
2556+
2557+
if (Opts.SYCLVersion == 0U) {
2558+
// User has passed an invalid value to the flag, this is an error
2559+
Diags.Report(diag::err_drv_invalid_value)
2560+
<< A->getAsString(Args) << A->getValue();
2561+
}
2562+
}
2563+
}
2564+
25472565
Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
25482566
Opts.DeclareOpenCLBuiltins = Args.hasArg(OPT_fdeclare_opencl_builtins);
25492567

@@ -3145,8 +3163,6 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
31453163
<< Opts.OMPHostIRFile;
31463164
}
31473165

3148-
Opts.SYCLIsDevice = Args.hasArg(options::OPT_fsycl_is_device);
3149-
31503166
// Set CUDA mode for OpenMP target NVPTX if specified in options
31513167
Opts.OpenMPCUDAMode = Opts.OpenMPIsDevice && T.isNVPTX() &&
31523168
Args.hasArg(options::OPT_fopenmp_cuda_mode);

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
460460
if (LangOpts.FastRelaxedMath)
461461
Builder.defineMacro("__FAST_RELAXED_MATH__");
462462
}
463+
464+
if (LangOpts.SYCL) {
465+
// SYCL Version is set to a value when building SYCL applications
466+
if (LangOpts.SYCLVersion == 2017)
467+
Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
468+
}
469+
463470
// Not "standard" per se, but available even with the -undef flag.
464471
if (LangOpts.AsmPreprocessor)
465472
Builder.defineMacro("__ASSEMBLER__");

clang/test/Driver/sycl.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
// RUN: %clang -### -fsycl -c %s 2>&1 | FileCheck %s --check-prefix=ENABLED
22
// RUN: %clang -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=ENABLED
3+
// RUN: %clang -### -fsycl -sycl-std=1.2.1 %s 2>&1 | FileCheck %s --check-prefix=ENABLED
4+
// RUN: %clang -### -fsycl -sycl-std=121 %s 2>&1 | FileCheck %s --check-prefix=ENABLED
5+
// RUN: %clang -### -fsycl -sycl-std=2017 %s 2>&1 | FileCheck %s --check-prefix=ENABLED
6+
// RUN: %clang -### -fsycl -sycl-std=sycl-1.2.1 %s 2>&1 | FileCheck %s --check-prefix=ENABLED
37
// RUN: %clang -### -fno-sycl -fsycl %s 2>&1 | FileCheck %s --check-prefix=ENABLED
8+
// RUN: %clang -### -sycl-std=2017 %s 2>&1 | FileCheck %s --check-prefix=DISABLED
49
// RUN: %clangxx -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=ENABLED
510
// RUN: %clangxx -### -fno-sycl %s 2>&1 | FileCheck %s --check-prefix=DISABLED
611
// RUN: %clangxx -### -fsycl -fno-sycl %s 2>&1 | FileCheck %s --check-prefix=DISABLED
712
// RUN: %clangxx -### %s 2>&1 | FileCheck %s --check-prefix=DISABLED
13+
// RUN: %clang_cl -### -fsycl -sycl-std=2017 -- %s 2>&1 | FileCheck %s --check-prefix=ENABLED
14+
// RUN: %clang_cl -### -fsycl -- %s 2>&1 | FileCheck %s --check-prefix=ENABLED
15+
// RUN: %clang_cl -### -- %s 2>&1 | FileCheck %s --check-prefix=DISABLED
816

917
// ENABLED: "-cc1"{{.*}} "-fsycl-is-device"
18+
// ENABLED-SAME: "-sycl-std={{[-.sycl0-9]+}}"
1019
// DISABLED-NOT: "-fsycl-is-device"
20+
// DISABLED-NOT: "-sycl-std="
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_cc1 %s -triple spir -aux-triple x86_64-unknown-linux-gnu -E -dM | FileCheck %s
2-
// RUN: %clang_cc1 %s -fsycl-is-device -triple spir -aux-triple x86_64-unknown-linux-gnu -E -dM | FileCheck --check-prefix=CHECK-SYCL %s
2+
// RUN: %clang_cc1 %s -fsycl -fsycl-is-device -triple spir -aux-triple x86_64-unknown-linux-gnu -E -dM | FileCheck --check-prefix=CHECK-SYCL %s
33

44
// CHECK-NOT:#define __x86_64__ 1
55
// CHECK-SYCL:#define __x86_64__ 1
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// RUN: %clang_cc1 %s -E -dM | FileCheck %s
2-
// RUN: %clang_cc1 %s -fsycl-is-device -E -dM | FileCheck --check-prefix=CHECK-SYCL %s
2+
// RUN: %clang_cc1 %s -fsycl -sycl-std=2017 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s
3+
// RUN: %clang_cc1 %s -fsycl -fsycl-is-device -sycl-std=1.2.1 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s
4+
// RUN: %clang_cc1 %s -fsycl -fsycl-is-device -E -dM | FileCheck --check-prefixes=CHECK-SYCL %s
35

46
// CHECK-NOT:#define __SYCL_DEVICE_ONLY__ 1
7+
// CHECK-NOT:#define CL_SYCL_LANGUAGE_VERSION 121
8+
// CHECK-SYCL-STD:#define CL_SYCL_LANGUAGE_VERSION 121
59
// CHECK-SYCL:#define __SYCL_DEVICE_ONLY__ 1

clang/test/SemaSYCL/kernel-attribute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fsycl-is-device -verify %s
1+
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fsycl -fsycl-is-device -verify %s
22

33
// Only function templates
44
[[clang::sycl_kernel]] int gv2 = 0; // expected-warning {{'sycl_kernel' attribute only applies to function templates}}

0 commit comments

Comments
 (0)