Skip to content

[SYCL][ESIMD] Mark ESIMD kernel callgraph with sycl_explicit_simd. #2096

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
Jul 22, 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
1 change: 1 addition & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -12696,6 +12696,7 @@ class Sema final {
void checkSYCLDeviceVarDecl(VarDecl *Var);
void ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc, MangleContext &MC);
void MarkDevice();
void MarkSyclSimd();

/// Creates a DeviceDiagBuilder that emits the diagnostic if the current
/// context is "used as device code".
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Sema/Sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) {
SyclIntHeader->emit(getLangOpts().SYCLIntHeader);
MarkDevice();
}
if (getLangOpts().SYCLExplicitSIMD)
MarkSyclSimd();

emitDeferredDiags();

Expand Down
26 changes: 24 additions & 2 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,27 @@ void Sema::ConstructOpenCLKernel(FunctionDecl *KernelCallerFunc,
ConstructingOpenCLKernel = false;
}

// This function marks all the callees of explicit SIMD kernel
// with !sycl_explicit_simd. We want to have different semantics
// for functions that are called from SYCL and E-SIMD contexts.
// Later, functions marked with !sycl_explicit_simd will be cloned
// to maintain two different semantics.
void Sema::MarkSyclSimd() {
for (Decl *D : syclDeviceDecls())
if (auto SYCLKernel = dyn_cast<FunctionDecl>(D))
if (SYCLKernel->hasAttr<SYCLSimdAttr>()) {
MarkDeviceFunction Marker(*this);
Marker.SYCLCG.addToCallGraph(getASTContext().getTranslationUnitDecl());
llvm::SmallPtrSet<FunctionDecl *, 10> VisitedSet;
Marker.CollectKernelSet(SYCLKernel, SYCLKernel, VisitedSet);
for (const auto &elt : Marker.KernelSet) {
if (FunctionDecl *Def = elt->getDefinition())
if (!Def->hasAttr<SYCLSimdAttr>())
Def->addAttr(SYCLSimdAttr::CreateImplicit(getASTContext()));
}
}
}

void Sema::MarkDevice(void) {
// Create the call graph so we can detect recursion and check the validity
// of new operator overrides. Add the kernel function itself in case
Expand All @@ -1910,7 +1931,8 @@ void Sema::MarkDevice(void) {
// Iterate through SYCL_EXTERNAL functions and add them to the device decls.
for (const auto &entry : *Marker.SYCLCG.getRoot()) {
if (auto *FD = dyn_cast<FunctionDecl>(entry.Callee->getDecl())) {
if (FD->hasAttr<SYCLDeviceAttr>() && !FD->hasAttr<SYCLKernelAttr>())
if (FD->hasAttr<SYCLDeviceAttr>() && !FD->hasAttr<SYCLKernelAttr>() &&
FD->hasBody())
addSyclDeviceDecl(FD);
}
}
Expand Down Expand Up @@ -1975,7 +1997,7 @@ void Sema::MarkDevice(void) {
case attr::Kind::SYCLIntelMaxWorkGroupSize:
case attr::Kind::SYCLIntelNoGlobalWorkOffset:
case attr::Kind::SYCLSimd: {
if ((A->getKind() == attr::Kind::SYCLSimd) &&
if ((A->getKind() == attr::Kind::SYCLSimd) && KernelBody &&
!KernelBody->getAttr<SYCLSimdAttr>()) {
// Usual kernel can't call ESIMD functions.
Diag(KernelBody->getLocation(),
Expand Down
39 changes: 39 additions & 0 deletions clang/test/CodeGenSYCL/esimd_metadata2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %clang_cc1 -disable-llvm-passes -triple spir64-unknown-unknown-sycldevice -fsycl -fsycl-is-device -fsycl-explicit-simd -S -emit-llvm %s -o - | FileCheck %s --check-prefixes CHECK,CHECK-ESIMD

// In ESIMD mode:
// 1. Attribute !intel_reqd_sub_group_size !1 is added
// for kernels with !sycl_explicit_simd
// 2. Attribute !sycl_explicit_simd is propagated to all the
// callees of ESIMD kernel.

__attribute__((sycl_device)) void shared_func_decl();
__attribute__((sycl_device)) void shared_func() { shared_func_decl(); }

__attribute__((sycl_device)) __attribute__((sycl_explicit_simd)) void esimd_func() { shared_func(); }

// CHECK-ESIMD-DAG: define spir_kernel void @{{.*}}kernel_cm() #{{[0-9]+}} !sycl_explicit_simd !{{[0-9]+}} {{.*}} !intel_reqd_sub_group_size ![[SGSIZE1:[0-9]+]] {{.*}}{
// CHECK-ESIMD-DAG: define spir_func void @{{.*}}esimd_funcv() #{{[0-9]+}} !sycl_explicit_simd !{{[0-9]+}} {
// CHECK-ESIMD-DAG: define spir_func void @{{.*}}shared_funcv() #{{[0-9]+}} !sycl_explicit_simd !{{[0-9]+}} {
// CHECK-ESIMD-DAG: define linkonce_odr spir_func void @_ZN12ESIMDFunctorclEv({{.*}}) #{{[0-9]+}} {{.*}} !sycl_explicit_simd !{{[0-9]+}} {
// CHECK-ESIMD-DAG: declare spir_func void @{{.*}}shared_func_declv() #{{[0-9]+}}

class ESIMDFunctor {
public:
void operator()() __attribute__((sycl_explicit_simd)) {
esimd_func();
}
};

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

void bar() {
ESIMDFunctor cmf;
kernel<class kernel_cm>(cmf);
}

// CHECK: !spirv.Source = !{[[LANG:![0-9]+]]}
// CHECK: [[LANG]] = !{i32 6, i32 100000}
// CHECK-ESIMD: ![[SGSIZE1]] = !{i32 1}
7 changes: 7 additions & 0 deletions clang/test/CodeGenSYCL/esimd_metadata3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang_cc1 -disable-llvm-passes -triple spir64-unknown-unknown-sycldevice -fsycl -fsycl-is-device -fsycl-explicit-simd -S -emit-llvm %s -o - | FileCheck %s --check-prefix CHECK-ESIMD

__attribute__((sycl_device)) void funcWithSpirvIntrin() {}
__attribute__((sycl_device)) __attribute__((sycl_explicit_simd)) void standaloneCmFunc() { funcWithSpirvIntrin(); }

// CHECK-ESIMD-DAG: define spir_func void @{{.*}}funcWithSpirvIntrinv() #{{[0-9]+}} !sycl_explicit_simd !{{[0-9]+}} {
// CHECK-ESIMD-DAG: define spir_func void @{{.*}}standaloneCmFuncv() #{{[0-9]+}} !sycl_explicit_simd !{{[0-9]+}} {