Skip to content

[SYCL] Add the notion of a "default" -cc1 SYCL mode. #3702

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
May 11, 2021
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
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class LangOptions : public LangOptionsBase {
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
};

enum class SYCLVersionList {
Expand Down
29 changes: 10 additions & 19 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3618,25 +3618,6 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
LangStd = OpenCLLangStd;
}

if (Args.hasArg(OPT_fsycl_is_device) || Args.hasArg(OPT_fsycl_is_host)) {
// -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::SYCLMajorVersion>(A->getValue())
.Cases("2017", "1.2.1", "121", "sycl-1.2.1",
LangOptions::SYCL_2017)
.Case("2020", LangOptions::SYCL_2020)
.Default(LangOptions::SYCL_None));

if (Opts.getSYCLVersion() == LangOptions::SYCL_None) {
// 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();
}
}
}

// Parse SYCL Default Sub group size.
if (const Arg *A = Args.getLastArg(OPT_fsycl_default_sub_group_size)) {
StringRef Value = A->getValue();
Expand Down Expand Up @@ -3687,6 +3668,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func_three() {
#endif

template <typename Name, typename Type>
[[clang::sycl_kernel]] void __my_kernel__(Type bar) {
[[clang::sycl_kernel]] void __my_kernel__(const Type &bar) {
bar();
#ifndef TRIGGER_ERROR
func_one();
Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaSYCL/kernel-attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ template <typename T, typename A, int I>
// No diagnostics
template <typename Func>
void __attribute__((sycl_kernel))
KernelImpl4(Func f, int i, double d) {
KernelImpl4(const Func &f, int i, double d) {
f(i, d);
}

template <typename Name, typename Func>
void __attribute__((sycl_kernel))
Kernel(Func f) {
Kernel(const Func &f) {
KernelImpl4(f, 1, 2.0);
}

Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaSYCL/long-double.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RUN: %clang_cc1 -triple spir64 -aux-triple x86_64-linux-gnu -fsycl-is-device -fsyntax-only -mlong-double-64 %s

template <typename Name, typename Func>
__attribute__((sycl_kernel)) void kernel(Func kernelFunc) {
__attribute__((sycl_kernel)) void kernel(const Func &kernelFunc) {
// expected-note@+1 {{called by 'kernel<variables}}
kernelFunc();
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaSYCL/union-kernel-param-neg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ union union_with_sampler {
};

template <typename name, typename Func>
__attribute__((sycl_kernel)) void a_kernel(Func kernelFunc) {
__attribute__((sycl_kernel)) void a_kernel(const Func &kernelFunc) {
kernelFunc();
}

Expand Down
90 changes: 87 additions & 3 deletions clang/unittests/Frontend/CompilerInvocationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down