Skip to content

[SYCL] Allow gcc asm statements in kernel code. #1341

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 1 commit into from
Mar 19, 2020
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
13 changes: 0 additions & 13 deletions clang/lib/Sema/SemaStmtAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,6 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
// The parser verifies that there is a string literal here.
assert(AsmString->isAscii());

// Skip all the checks if we are compiling SYCL device code, but the function
// is not marked to be used on device, this code won't be codegen'ed anyway.
if (getLangOpts().SYCLIsDevice) {
SYCLDiagIfDeviceCode(AsmLoc, diag::err_sycl_restrict) << KernelUseAssembly;
return new (Context)
GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
Names, Constraints, Exprs.data(), AsmString, NumClobbers,
Clobbers, NumLabels, RParenLoc);
}

FunctionDecl *FD = dyn_cast<FunctionDecl>(getCurLexicalContext());
llvm::StringMap<bool> FeatureMap;
Context.getFunctionFeatureMap(FeatureMap, FD);
Expand Down Expand Up @@ -902,9 +892,6 @@ StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc,
SourceLocation EndLoc) {
bool IsSimple = (NumOutputs != 0 || NumInputs != 0);
setFunctionHasBranchProtectedScope();
if (getLangOpts().SYCLIsDevice)
SYCLDiagIfDeviceCode(AsmLoc, diag::err_sycl_restrict)
<< KernelUseAssembly;
MSAsmStmt *NS =
new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple,
/*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs,
Expand Down
27 changes: 27 additions & 0 deletions clang/test/CodeGenSYCL/inline_asm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %clang_cc1 -fsycl -fsycl-is-device -triple spir64-unknown-unknown-sycldevice -emit-llvm -x c++ %s -o - | FileCheck %s

class kernel;

template <typename name, typename Func>
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
// CHECK: %[[ARRAY_A:[0-9a-z]+]] = alloca [100 x i32], align 4
// CHECK: %[[IDX:.*]] = getelementptr inbounds [100 x i32], [100 x i32]* %[[ARRAY_A]], i64 0, i64 0
int a[100], i = 0;
// CHECK-NEXT: call void asm sideeffect
// CHECK: ".decl V52 v_type=G type=d num_elts=16 align=GRF
// CHECK: svm_gather.4.1 (M1, 16) $0.0 V52.0
// CHECK: add(M1, 16) V52(0, 0)<1> V52(0, 0)<1; 1, 0> 0x1
// CHECK: svm_scatter.4.1 (M1, 16) $0.0 V52.0",
// CHECK: "rw"(i32* nonnull %[[IDX]])
asm volatile(".decl V52 v_type=G type=d num_elts=16 align=GRF\n"
"svm_gather.4.1 (M1, 16) %0.0 V52.0\n"
"add(M1, 16) V52(0, 0)<1> V52(0, 0)<1; 1, 0> 0x1\n"
"svm_scatter.4.1 (M1, 16) %0.0 V52.0"
:
: "rw"(&a[i]));
}

int main() {
kernel_single_task<class kernel>([]() {});
return 0;
}
12 changes: 6 additions & 6 deletions clang/test/SemaSYCL/inline-asm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsyntax-only -verify %s -DLINUX_ASM
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsyntax-only -verify -triple x86_64-windows -fasm-blocks %s

// expected-no-diagnostics

void foo() {
int a;
#ifdef LINUX_ASM
Expand All @@ -13,26 +15,24 @@ void foo() {
void bar() {
int a;
#ifdef LINUX_ASM
__asm__("int3"); // expected-error {{SYCL kernel cannot use inline assembly}}
__asm__("int3");
#else
__asm int 3 // expected-error {{SYCL kernel cannot use inline assembly}}
__asm int 3
#endif // LINUX_ASM
}

template <typename Name, typename Func>
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
// expected-note@+1 {{called by 'kernel_single_task<fake_kernel, (lambda}}
kernelFunc();
#ifdef LINUX_ASM
__asm__("int3"); // expected-error {{SYCL kernel cannot use inline assembly}}
__asm__("int3");
#else
__asm int 3 // expected-error {{SYCL kernel cannot use inline assembly}}
__asm int 3
#endif // LINUX_ASM
}

int main() {
foo();
// expected-note@+1 {{called by 'operator()'}}
kernel_single_task<class fake_kernel>([]() { bar(); });
return 0;
}