diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 31a42e83fd0e4a..20c03987bd0277 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -252,7 +252,7 @@ LANGOPT(GPUExcludeWrongSideOverloads, 1, 0, "always exclude wrong side overloads LANGOPT(SYCLIsDevice , 1, 0, "Generate code for SYCL device") LANGOPT(SYCLIsHost , 1, 0, "SYCL host compilation") -ENUM_LANGOPT(SYCLVersion , SYCLMajorVersion, 1, SYCL_None, "Version of the SYCL standard used") +ENUM_LANGOPT(SYCLVersion , SYCLMajorVersion, 2, SYCL_None, "Version of the SYCL standard used") LANGOPT(HIPUseNewLaunchAPI, 1, 0, "Use new kernel launching API for HIP") diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index 524b79b1381400..454c101b214254 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -130,6 +130,10 @@ class LangOptions : public LangOptionsBase { enum SYCLMajorVersion { SYCL_None, SYCL_2017, + SYCL_2020, + // The "default" SYCL version to be used when none is specified on the + // frontend command line. + SYCL_Default = SYCL_2020 }; /// Clang versions with different platform ABI conformance. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 67d9a2bbc328cc..09de6b5a50d004 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5691,8 +5691,8 @@ def fsycl_is_host : Flag<["-"], "fsycl-is-host">, def sycl_std_EQ : Joined<["-"], "sycl-std=">, Group, Flags<[CC1Option, NoArgumentUnused, CoreOption]>, HelpText<"SYCL language standard to compile for.">, - Values<"2017,121,1.2.1,sycl-1.2.1">, - NormalizedValues<["SYCL_2017", "SYCL_2017", "SYCL_2017", "SYCL_2017"]>, + Values<"2020,2017,121,1.2.1,sycl-1.2.1">, + NormalizedValues<["SYCL_2020", "SYCL_2017", "SYCL_2017", "SYCL_2017", "SYCL_2017"]>, NormalizedValuesScope<"LangOptions">, MarshallingInfoEnum, "SYCL_None">, ShouldParseIf; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 63ca39ee4e9345..afc119263db965 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -4300,8 +4300,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, 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 2017) - CmdArgs.push_back("-sycl-std=2017"); + // Ensure the default version in SYCL mode is 2020. + CmdArgs.push_back("-sycl-std=2020"); } } diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 9cb859f233238c..809492a36d3fae 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -3648,6 +3648,16 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, } } + if ((Args.hasArg(OPT_fsycl_is_device) || Args.hasArg(OPT_fsycl_is_host)) && + !Args.hasArg(OPT_sycl_std_EQ)) { + // If the user supplied -fsycl-is-device or -fsycl-is-host, but failed to + // provide -sycl-std=, we want to default it to whatever the default SYCL + // version is. I could not find a way to express this with the options + // tablegen because we still want this value to be SYCL_None when the user + // is not in device or host mode. + Opts.setSYCLVersion(LangOptions::SYCL_Default); + } + if (Opts.ObjC) { if (Arg *arg = Args.getLastArg(OPT_fobjc_runtime_EQ)) { StringRef value = arg->getValue(); diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 163c77b04c139c..8e63046f4ab22f 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -483,6 +483,8 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI, // SYCL Version is set to a value when building SYCL applications if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2017) Builder.defineMacro("CL_SYCL_LANGUAGE_VERSION", "121"); + else if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2020) + Builder.defineMacro("SYCL_LANGUAGE_VERSION", "202001"); } // Not "standard" per se, but available even with the -undef flag. diff --git a/clang/test/Driver/sycl.c b/clang/test/Driver/sycl.c index 4a39fef06b6dc7..af3af91cc062a2 100644 --- a/clang/test/Driver/sycl.c +++ b/clang/test/Driver/sycl.c @@ -3,6 +3,7 @@ // 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=2017 %s 2>&1 | FileCheck %s --check-prefix=ENABLED +// RUN: %clang -### -fsycl -sycl-std=2020 %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=2017 %s 2>&1 | FileCheck %s --check-prefix=DISABLED @@ -18,3 +19,9 @@ // ENABLED-SAME: "-sycl-std={{[-.sycl0-9]+}}" // DISABLED-NOT: "-fsycl-is-device" // DISABLED-NOT: "-sycl-std=" + +// RUN: %clang -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=DEFAULT +// RUN: %clangxx -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=DEFAULT +// RUN: %clang_cl -### -fsycl %s 2>&1 | FileCheck %s --check-prefix=DEFAULT + +// DEFAULT: "-sycl-std=2020" diff --git a/clang/test/Preprocessor/sycl-macro.cpp b/clang/test/Preprocessor/sycl-macro.cpp index d1d830210529c6..eecddaa09d1c33 100644 --- a/clang/test/Preprocessor/sycl-macro.cpp +++ b/clang/test/Preprocessor/sycl-macro.cpp @@ -1,10 +1,14 @@ // RUN: %clang_cc1 %s -E -dM | FileCheck %s // RUN: %clang_cc1 %s -fsycl-is-device -sycl-std=2017 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s // RUN: %clang_cc1 %s -fsycl-is-host -sycl-std=2017 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD %s +// RUN: %clang_cc1 %s -fsycl-is-host -sycl-std=2020 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD-2020 %s +// RUN: %clang_cc1 %s -fsycl-is-host -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD-2020 %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 -sycl-std=2020 -E -dM | FileCheck --check-prefix=CHECK-SYCL-STD-2020 %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-STD-2020:#define SYCL_LANGUAGE_VERSION 202001 // CHECK-SYCL:#define __SYCL_DEVICE_ONLY__ 1 diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp index 0baf17f123acc2..9bcbf1fe6001d2 100644 --- a/clang/unittests/Frontend/CompilerInvocationTest.cpp +++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp @@ -566,18 +566,102 @@ TEST_F(CommandLineTest, ConditionalParsingIfFalseFlagPresent) { ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std=")))); } -TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresent) { +TEST_F(CommandLineTest, ConditionalParsingIfNonsenseSyclStdArg) { + const char *Args[] = {"-fsycl-is-device", "-sycl-std=garbage"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + + ASSERT_TRUE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice); + ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost); + ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_None); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device"))); + ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host")))); + ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std=")))); +} + +TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg1) { + const char *Args[] = {"-fsycl-is-device", "-sycl-std=121"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice); + ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost); + ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device"))); + ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host")))); + ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017"))); +} + +TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg2) { + const char *Args[] = {"-fsycl-is-device", "-sycl-std=1.2.1"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice); + ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost); + ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device"))); + ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host")))); + ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017"))); +} + +TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg3) { + const char *Args[] = {"-fsycl-is-device", "-sycl-std=sycl-1.2.1"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice); + ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost); + ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device"))); + ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host")))); + ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017"))); +} + +TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresentHost) { const char *Args[] = {"-fsycl-is-host"}; CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); ASSERT_FALSE(Diags->hasErrorOccurred()); - ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_None); + ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), + LangOptions::SYCL_Default); Invocation.generateCC1CommandLine(GeneratedArgs, *this); ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-host"))); - ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std=")))); + ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std="))); +} + +TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresentDevice) { + const char *Args[] = {"-fsycl-is-device"}; + + CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags); + + ASSERT_FALSE(Diags->hasErrorOccurred()); + ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), + LangOptions::SYCL_Default); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device"))); + ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std="))); } TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagPresent) {