Index: clang/include/clang/Basic/LangOptions.h =================================================================== --- clang/include/clang/Basic/LangOptions.h +++ clang/include/clang/Basic/LangOptions.h @@ -130,6 +130,10 @@ 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. Index: clang/include/clang/Basic/LangOptions.def =================================================================== --- clang/include/clang/Basic/LangOptions.def +++ clang/include/clang/Basic/LangOptions.def @@ -251,7 +251,7 @@ 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") Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -5671,8 +5671,8 @@ 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; Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -4284,8 +4284,8 @@ 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"); } } Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -3635,6 +3635,16 @@ } } + 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(); Index: clang/lib/Frontend/InitPreprocessor.cpp =================================================================== --- clang/lib/Frontend/InitPreprocessor.cpp +++ clang/lib/Frontend/InitPreprocessor.cpp @@ -478,6 +478,8 @@ // 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. Index: clang/test/Driver/sycl.c =================================================================== --- clang/test/Driver/sycl.c +++ 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" Index: clang/test/Preprocessor/sycl-macro.cpp =================================================================== --- clang/test/Preprocessor/sycl-macro.cpp +++ 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 Index: clang/unittests/Frontend/CompilerInvocationTest.cpp =================================================================== --- clang/unittests/Frontend/CompilerInvocationTest.cpp +++ clang/unittests/Frontend/CompilerInvocationTest.cpp @@ -566,18 +566,102 @@ 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) {