Skip to content

[SYCL] Driver option to select SYCL version #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ LANGOPT(OpenCL , 1, 0, "OpenCL")
LANGOPT(OpenCLVersion , 32, 0, "OpenCL C version")
LANGOPT(OpenCLCPlusPlus , 1, 0, "OpenCL C++")
LANGOPT(OpenCLCPlusPlusVersion , 32, 0, "OpenCL C++ version")
ENUM_LANGOPT(SYCLVersion, SYCLVersionList, 4, SYCLVersionList::undefined, "Version of the SYCL standard used")
LANGOPT(NativeHalfType , 1, 0, "Native half type support")
LANGOPT(NativeHalfArgsAndReturns, 1, 0, "Native half args and returns")
LANGOPT(HalfArgsAndReturns, 1, 0, "half args and returns")
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class LangOptions : public LangOptionsBase {
MSVC2017_7 = 1914,
};

enum class SYCLVersionList {
sycl_1_2_1,
undefined
};

/// Clang versions with different platform ABI conformance.
enum class ClangABI {
/// Attempt to be ABI-compatible with code generated by Clang 3.8.x
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def pedantic_Group : OptionGroup<"<pedantic group>">, Group<f_Group>,
def opencl_Group : OptionGroup<"<opencl group>">, Group<f_Group>,
DocName<"OpenCL flags">;

def sycl_Group : OptionGroup<"<sycl group>">, Group<f_Group>,
DocName<"SYCL flags">;

def m_Group : OptionGroup<"<m group>">, Group<CompileOnly_Group>,
DocName<"Target-dependent compilation options">;

Expand Down Expand Up @@ -534,6 +537,8 @@ def cl_no_signed_zeros : Flag<["-"], "cl-no-signed-zeros">, Group<opencl_Group>,
HelpText<"OpenCL only. Allow use of less precise no signed zeros computations in the generated binary.">;
def cl_std_EQ : Joined<["-"], "cl-std=">, Group<opencl_Group>, Flags<[CC1Option]>,
HelpText<"OpenCL language standard to compile for.">, Values<"cl,CL,cl1.1,CL1.1,cl1.2,CL1.2,cl2.0,CL2.0,c++">;
def sycl_std_EQ : Joined<["-"], "sycl-std=">, Group<sycl_Group>, Flags<[CC1Option]>,
HelpText<"SYCL language standard to compile for.">, Values<"1.2.1">;
def cl_denorms_are_zero : Flag<["-"], "cl-denorms-are-zero">, Group<opencl_Group>, Flags<[CC1Option]>,
HelpText<"OpenCL only. Allow denormals to be flushed to zero.">;
def cl_fp32_correctly_rounded_divide_sqrt : Flag<["-"], "cl-fp32-correctly-rounded-divide-sqrt">, Group<opencl_Group>, Flags<[CC1Option]>,
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}

if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) {
A->render(Args, CmdArgs);
} else if (IsSYCL) {
// Ensure the default version in SYCL mode is 1.2.1
CmdArgs.push_back("-sycl-std=1.2.1");
}

if (IsOpenMPDevice) {
// We have to pass the triple of the host if compiling for an OpenMP device.
std::string NormalizedTriple =
Expand Down Expand Up @@ -4720,6 +4727,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// Forward -cl options to -cc1
RenderOpenCLOptions(Args, CmdArgs);

// Forward -sycl-std option to -cc1
Args.AddLastArg(CmdArgs, options::OPT_sycl_std_EQ);

if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) {
CmdArgs.push_back(
Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
Expand Down
18 changes: 18 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,24 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
LangStd = OpenCLLangStd;
}

// -sycl-std applies to any SYCL source, not only those containing kernels,
// but also those using the SYCL API
if(const Arg *A = Args.getLastArg(OPT_sycl_std_EQ)) {
Opts.setSYCLVersion(llvm::StringSwitch<LangOptions::SYCLVersionList>(A->getValue())
.Cases("1.2.1", "121", "sycl-1.2.1", LangOptions::SYCLVersionList::sycl_1_2_1)
.Default(LangOptions::SYCLVersionList::undefined));

if (Opts.getSYCLVersion() == LangOptions::SYCLVersionList::undefined) {
// User has passed an invalid value to the flag, this is an error
Diags.Report(diag::err_drv_invalid_value)
<< A->getAsString(Args) << A->getValue();
}
} else if (Args.hasArg(options::OPT_fsycl_is_device)
|| Args.hasArg(options::OPT_fsycl_is_host)
|| Args.hasArg(options::OPT_fsycl)) {
Opts.setSYCLVersion(LangOptions::SYCLVersionList::sycl_1_2_1);
}

Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
Opts.DeclareOpenCLBuiltins = Args.hasArg(OPT_fdeclare_opencl_builtins);

Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,18 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
Builder.defineMacro("__FAST_RELAXED_MATH__");
}
}

// SYCL Version is set to a value when building SYCL applications
switch (LangOpts.getSYCLVersion()) {
case LangOptions::SYCLVersionList::sycl_1_2_1:
Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121");
break;
case LangOptions::SYCLVersionList::undefined:
default:
// This is not a SYCL source, nothing to add
break;
}

// Not "standard" per se, but available even with the -undef flag.
if (LangOpts.AsmPreprocessor)
Builder.defineMacro("__ASSEMBLER__");
Expand Down
7 changes: 5 additions & 2 deletions clang/test/Preprocessor/sycl-macro.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// RUN: %clang_cc1 %s -E -dM | FileCheck %s
// RUN: %clang_cc1 %s -fsycl-is-device -E -dM | FileCheck --check-prefix=CHECK-SYCL %s

// RUN: %clang_cc1 %s -fsycl -E -dM | FileCheck --check-prefix=CHECK-ANY-SYCL %s
// CHECK-NOT:#define __SYCL_DEVICE_ONLY__ 1
// CHECK-SYCL:#define __SYCL_DEVICE_ONLY__ 1
// CHECK-NOT:#define CL_SYCL_LANGUAGE_VERSION 121
// CHECK-ANY-SYCL-NOT:#define __SYCL_DEVICE_ONLY__ 1
// CHECK-ANY-SYCL:#define CL_SYCL_LANGUAGE_VERSION 121
// CHECK-SYCL:#define CL_SYCL_LANGUAGE_VERSION 121
13 changes: 12 additions & 1 deletion sycl/doc/SYCL_compiler_and_runtime_design.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ targets like SPIR-V).

#### Enable SYCL offload

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

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

In the driver, the following bools are defined to determine the compilation
mode in SYCL:

* IsSYCL : True if the user has passed `--sycl` to the compilation
* IsSYCLOffloadDevice: True if calling clang to set up a device compilation
* IsSYCLHost: True if setting up a call to clang to do a host compilation

The option `-sycl-std` allows specifiying which version of
the SYCL standard will be used for the compilation.
The default value for this option is `1.2.1`.

#### Ahead of time (AOT) compilation

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