Skip to content

Commit 680adc0

Browse files
authored
[SYCL] Add the notion of a "default" -cc1 SYCL mode. (#3702)
This defaults to SYCL 2020 currently, but makes it easier to change to a new default later. This default kicks in only in -cc1 mode when the user specifies -fsycl-is-device or -fsycl-is-host but does not pass -sycl-std= to the invocation. This does not impact the driver behavior.
1 parent a7b8daf commit 680adc0

File tree

7 files changed

+105
-27
lines changed

7 files changed

+105
-27
lines changed

clang/include/clang/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class LangOptions : public LangOptionsBase {
131131
SYCL_None,
132132
SYCL_2017,
133133
SYCL_2020,
134+
// The "default" SYCL version to be used when none is specified on the
135+
// frontend command line.
136+
SYCL_Default = SYCL_2020
134137
};
135138

136139
enum class SYCLVersionList {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,25 +3618,6 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
36183618
LangStd = OpenCLLangStd;
36193619
}
36203620

3621-
if (Args.hasArg(OPT_fsycl_is_device) || Args.hasArg(OPT_fsycl_is_host)) {
3622-
// -sycl-std applies to any SYCL source, not only those containing kernels,
3623-
// but also those using the SYCL API
3624-
if (const Arg *A = Args.getLastArg(OPT_sycl_std_EQ)) {
3625-
Opts.setSYCLVersion(
3626-
llvm::StringSwitch<LangOptions::SYCLMajorVersion>(A->getValue())
3627-
.Cases("2017", "1.2.1", "121", "sycl-1.2.1",
3628-
LangOptions::SYCL_2017)
3629-
.Case("2020", LangOptions::SYCL_2020)
3630-
.Default(LangOptions::SYCL_None));
3631-
3632-
if (Opts.getSYCLVersion() == LangOptions::SYCL_None) {
3633-
// User has passed an invalid value to the flag, this is an error
3634-
Diags.Report(diag::err_drv_invalid_value)
3635-
<< A->getAsString(Args) << A->getValue();
3636-
}
3637-
}
3638-
}
3639-
36403621
// Parse SYCL Default Sub group size.
36413622
if (const Arg *A = Args.getLastArg(OPT_fsycl_default_sub_group_size)) {
36423623
StringRef Value = A->getValue();
@@ -3687,6 +3668,16 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
36873668
}
36883669
}
36893670

3671+
if ((Args.hasArg(OPT_fsycl_is_device) || Args.hasArg(OPT_fsycl_is_host)) &&
3672+
!Args.hasArg(OPT_sycl_std_EQ)) {
3673+
// If the user supplied -fsycl-is-device or -fsycl-is-host, but failed to
3674+
// provide -sycl-std=, we want to default it to whatever the default SYCL
3675+
// version is. I could not find a way to express this with the options
3676+
// tablegen because we still want this value to be SYCL_None when the user
3677+
// is not in device or host mode.
3678+
Opts.setSYCLVersion(LangOptions::SYCL_Default);
3679+
}
3680+
36903681
if (Opts.ObjC) {
36913682
if (Arg *arg = Args.getLastArg(OPT_fobjc_runtime_EQ)) {
36923683
StringRef value = arg->getValue();

clang/test/SemaSYCL/check-notdirect-attribute-propagation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func_three() {
2626
#endif
2727

2828
template <typename Name, typename Type>
29-
[[clang::sycl_kernel]] void __my_kernel__(Type bar) {
29+
[[clang::sycl_kernel]] void __my_kernel__(const Type &bar) {
3030
bar();
3131
#ifndef TRIGGER_ERROR
3232
func_one();

clang/test/SemaSYCL/kernel-attribute.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ template <typename T, typename A, int I>
5050
// No diagnostics
5151
template <typename Func>
5252
void __attribute__((sycl_kernel))
53-
KernelImpl4(Func f, int i, double d) {
53+
KernelImpl4(const Func &f, int i, double d) {
5454
f(i, d);
5555
}
5656

5757
template <typename Name, typename Func>
5858
void __attribute__((sycl_kernel))
59-
Kernel(Func f) {
59+
Kernel(const Func &f) {
6060
KernelImpl4(f, 1, 2.0);
6161
}
6262

clang/test/SemaSYCL/long-double.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: %clang_cc1 -triple spir64 -aux-triple x86_64-linux-gnu -fsycl-is-device -fsyntax-only -mlong-double-64 %s
44

55
template <typename Name, typename Func>
6-
__attribute__((sycl_kernel)) void kernel(Func kernelFunc) {
6+
__attribute__((sycl_kernel)) void kernel(const Func &kernelFunc) {
77
// expected-note@+1 {{called by 'kernel<variables}}
88
kernelFunc();
99
}

clang/test/SemaSYCL/union-kernel-param-neg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ union union_with_sampler {
1212
};
1313

1414
template <typename name, typename Func>
15-
__attribute__((sycl_kernel)) void a_kernel(Func kernelFunc) {
15+
__attribute__((sycl_kernel)) void a_kernel(const Func &kernelFunc) {
1616
kernelFunc();
1717
}
1818

clang/unittests/Frontend/CompilerInvocationTest.cpp

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,18 +566,102 @@ TEST_F(CommandLineTest, ConditionalParsingIfFalseFlagPresent) {
566566
ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std="))));
567567
}
568568

569-
TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresent) {
569+
TEST_F(CommandLineTest, ConditionalParsingIfNonsenseSyclStdArg) {
570+
const char *Args[] = {"-fsycl-is-device", "-sycl-std=garbage"};
571+
572+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
573+
574+
ASSERT_TRUE(Diags->hasErrorOccurred());
575+
ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
576+
ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
577+
ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_None);
578+
579+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
580+
581+
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
582+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
583+
ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std="))));
584+
}
585+
586+
TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg1) {
587+
const char *Args[] = {"-fsycl-is-device", "-sycl-std=121"};
588+
589+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
590+
591+
ASSERT_FALSE(Diags->hasErrorOccurred());
592+
ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
593+
ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
594+
ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017);
595+
596+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
597+
598+
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
599+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
600+
ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017")));
601+
}
602+
603+
TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg2) {
604+
const char *Args[] = {"-fsycl-is-device", "-sycl-std=1.2.1"};
605+
606+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
607+
608+
ASSERT_FALSE(Diags->hasErrorOccurred());
609+
ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
610+
ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
611+
ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017);
612+
613+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
614+
615+
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
616+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
617+
ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017")));
618+
}
619+
620+
TEST_F(CommandLineTest, ConditionalParsingIfOddSyclStdArg3) {
621+
const char *Args[] = {"-fsycl-is-device", "-sycl-std=sycl-1.2.1"};
622+
623+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
624+
625+
ASSERT_FALSE(Diags->hasErrorOccurred());
626+
ASSERT_TRUE(Invocation.getLangOpts()->SYCLIsDevice);
627+
ASSERT_FALSE(Invocation.getLangOpts()->SYCLIsHost);
628+
ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_2017);
629+
630+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
631+
632+
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
633+
ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fsycl-is-host"))));
634+
ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=2017")));
635+
}
636+
637+
TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresentHost) {
570638
const char *Args[] = {"-fsycl-is-host"};
571639

572640
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
573641

574642
ASSERT_FALSE(Diags->hasErrorOccurred());
575-
ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(), LangOptions::SYCL_None);
643+
ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(),
644+
LangOptions::SYCL_Default);
576645

577646
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
578647

579648
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-host")));
580-
ASSERT_THAT(GeneratedArgs, Not(Contains(HasSubstr("-sycl-std="))));
649+
ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=")));
650+
}
651+
652+
TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagNotPresentDevice) {
653+
const char *Args[] = {"-fsycl-is-device"};
654+
655+
CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
656+
657+
ASSERT_FALSE(Diags->hasErrorOccurred());
658+
ASSERT_EQ(Invocation.getLangOpts()->getSYCLVersion(),
659+
LangOptions::SYCL_Default);
660+
661+
Invocation.generateCC1CommandLine(GeneratedArgs, *this);
662+
663+
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fsycl-is-device")));
664+
ASSERT_THAT(GeneratedArgs, Contains(HasSubstr("-sycl-std=")));
581665
}
582666

583667
TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagPresent) {

0 commit comments

Comments
 (0)