diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -119,6 +119,8 @@ MSVC2017_7 = 1914, }; + enum class SYCLVersionList { SYCL_2015, Undefined }; + /// Clang versions with different platform ABI conformance. enum class ClangABI { /// Attempt to be ABI-compatible with code generated by Clang 3.8.x diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -229,7 +229,9 @@ LANGOPT(GPUAllowDeviceInit, 1, 0, "allowing device side global init functions for HIP") LANGOPT(GPUMaxThreadsPerBlock, 32, 256, "default max threads per block for kernel launch bounds for HIP") +LANGOPT(SYCL , 1, 0, "SYCL") LANGOPT(SYCLIsDevice , 1, 0, "Generate code for SYCL device") +ENUM_LANGOPT(SYCLVersion, SYCLVersionList, 4, SYCLVersionList::Undefined, "Version of the SYCL standard used") LANGOPT(HIPUseNewLaunchAPI, 1, 0, "Use new kernel launching API for HIP") diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3411,10 +3411,12 @@ defm whole_file : BooleanFFlag<"whole-file">, Group; // C++ SYCL options -def fsycl : Flag<["-"], "fsycl">, Group, +def fsycl : Flag<["-"], "fsycl">, Group, Flags<[CC1Option, NoArgumentUnused, CoreOption]>, HelpText<"Enable SYCL kernels compilation for device">; -def fno_sycl : Flag<["-"], "fno-sycl">, Group, +def fno_sycl : Flag<["-"], "fno-sycl">, Group, Flags<[NoArgumentUnused, CoreOption]>, HelpText<"Disable SYCL kernels compilation for device">; +def sycl_std_EQ : Joined<["-"], "sycl-std=">, Group, Flags<[CC1Option, NoArgumentUnused, CoreOption]>, + HelpText<"SYCL language standard to compile for.">, Values<"2015, 121, 1.2.1, sycl-1.2.1">; include "CC1Options.td" diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -4023,9 +4023,18 @@ CmdArgs.push_back(Args.MakeArgString(NormalizedTriple)); } - if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) + if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) { + CmdArgs.push_back("-fsycl"); CmdArgs.push_back("-fsycl-is-device"); + if (Arg *A = Args.getLastArg(options::OPT_sycl_std_EQ)) { + A->render(Args, CmdArgs); + } else { + // Ensure the default version in SYCL mode is 1.2.1 (aka 2015) + CmdArgs.push_back("-sycl-std=2015"); + } + } + if (IsOpenMPDevice) { // We have to pass the triple of the host if compiling for an OpenMP device. std::string NormalizedTriple = diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2544,6 +2544,26 @@ LangStd = OpenCLLangStd; } + Opts.SYCL = Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false); + Opts.SYCLIsDevice = Args.hasArg(options::OPT_fsycl_is_device); + if (Opts.SYCL || Opts.SYCLIsDevice) { + // -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(A->getValue()) + .Cases("2015", "1.2.1", "121", "sycl-1.2.1", + LangOptions::SYCLVersionList::SYCL_2015) + .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(); + } + } + } + Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header); Opts.DeclareOpenCLBuiltins = Args.hasArg(OPT_fdeclare_opencl_builtins); @@ -3144,8 +3164,6 @@ << Opts.OMPHostIRFile; } - Opts.SYCLIsDevice = Args.hasArg(options::OPT_fsycl_is_device); - // Set CUDA mode for OpenMP target NVPTX if specified in options Opts.OpenMPCUDAMode = Opts.OpenMPIsDevice && T.isNVPTX() && Args.hasArg(options::OPT_fopenmp_cuda_mode); diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -450,6 +450,16 @@ if (LangOpts.FastRelaxedMath) Builder.defineMacro("__FAST_RELAXED_MATH__"); } + + // SYCL Version is set to a value when building SYCL applications + switch (LangOpts.getSYCLVersion()) { + case LangOptions::SYCLVersionList::SYCL_2015: + Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121"); + break; + case LangOptions::SYCLVersionList::Undefined: + break; + } + // Not "standard" per se, but available even with the -undef flag. if (LangOpts.AsmPreprocessor) Builder.defineMacro("__ASSEMBLER__"); diff --git a/clang/test/Driver/sycl.c b/clang/test/Driver/sycl.c --- a/clang/test/Driver/sycl.c +++ b/clang/test/Driver/sycl.c @@ -1,10 +1,20 @@ // RUN: %clang -### -fsycl -c %s 2>&1 | FileCheck %s --check-prefix=ENABLED // RUN: %clang -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang -### -fsycl -sycl-std=1.2.1 %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang -### -fsycl -sycl-std=121 %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang -### -fsycl -sycl-std=2015 %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang -### -fsycl -sycl-std=sycl-1.2.1 %s 2>&1 | FileCheck %s --check-prefix=ENABLED // RUN: %clang -### -fno-sycl -fsycl %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang -### -sycl-std=2015 %s 2>&1 | FileCheck %s --check-prefix=DISABLED // RUN: %clangxx -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=ENABLED // RUN: %clangxx -### -fno-sycl %s 2>&1 | FileCheck %s --check-prefix=DISABLED // RUN: %clangxx -### -fsycl -fno-sycl %s 2>&1 | FileCheck %s --check-prefix=DISABLED // RUN: %clangxx -### %s 2>&1 | FileCheck %s --check-prefix=DISABLED +// RUN: %clang_cl -### -fsycl -sycl-std=2015 %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang_cl -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang_cl -### %s 2>&1 | FileCheck %s --check-prefix=DISABLED // ENABLED: "-cc1"{{.*}} "-fsycl-is-device" +// ENABLED-SAME: "-sycl-std={{[-.sycl0-9]+}}" // DISABLED-NOT: "-fsycl-is-device" +// DISABLED-NOT: "-sycl-std=" diff --git a/clang/test/Preprocessor/sycl-macro.cpp b/clang/test/Preprocessor/sycl-macro.cpp --- a/clang/test/Preprocessor/sycl-macro.cpp +++ b/clang/test/Preprocessor/sycl-macro.cpp @@ -1,5 +1,9 @@ // 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 -sycl-std=2015 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s +// RUN: %clang_cc1 %s -fsycl-is-device -sycl-std=1.2.1 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s +// RUN: %clang_cc1 %s -fsycl-is-device -E -dM | FileCheck --check-prefixes=CHECK-SYCL %s // CHECK-NOT:#define __SYCL_DEVICE_ONLY__ 1 +// CHECK-NOT:#define CL_SYCL_LANGUAGE_VERSION 121 +// CHECK-SYCL-STD:#define CL_SYCL_LANGUAGE_VERSION 121 // CHECK-SYCL:#define __SYCL_DEVICE_ONLY__ 1