Skip to content

Commit 02e8944

Browse files
committed
[SYCL] Implement OpenCL kernel function generation
All SYCL memory objects shared between host and device (buffers/images, these objects map to OpenCL buffers and images) must be accessed through special accessor classes. The "device" side implementation of these classes contain pointers to the device memory. As there is no way in OpenCL to pass structures with pointers inside as kernel arguments, all memory objects shared between host and device must be passed to the kernel as raw pointers. SYCL also has a special mechanism for passing kernel arguments from host to the device. In OpenCL kernel arguments are set by calling `clSetKernelArg` function for each kernel argument, meanwhile in SYCL all the kernel arguments are fields of "SYCL kernel function" which can be defined as a lambda function or a named function object and passed as an argument to SYCL function for invoking kernels (such as `parallel_for` or `single_task`). To facilitate the mapping of SYCL kernel data members to OpenCL kernel arguments and overcome OpenCL limitations we added the generation of an OpenCL kernel function inside the compiler. An OpenCL kernel function contains the body of the SYCL kernel function, receives OpenCL-like parameters and additionally does some manipulation to initialize SYCL kernel data members with these parameters. In some pseudo code the OpenCL kernel function can look like this: ``` // SYCL kernel is defined in SYCL headers: template <typename KernelName, typename KernelType/*, ...*/> __attribute__((sycl_kernel)) void sycl_kernel_function(KernelType KernelFuncObj) { // ... KernelFuncObj(); } // Generated OpenCL kernel function __kernel KernelName(global int* a) { KernelType KernelFuncObj; // Actually kernel function object declaration // doesn't have a name in AST. // Let the kernel function object have one captured field - accessor A. // We need to init it with global pointer from arguments: KernelFuncObj.A.__init(a); // Body of the SYCL kernel from SYCL headers: { KernelFuncObj(); } } ``` OpenCL kernel function is generated by the compiler inside the Sema using AST nodes.
1 parent 28e4942 commit 02e8944

16 files changed

+1035
-3
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11632,6 +11632,19 @@ class Sema final {
1163211632
ConstructorDestructor,
1163311633
BuiltinFunction
1163411634
};
11635+
11636+
private:
11637+
/// Contains generated OpenCL kernel functions for SYCL.
11638+
SmallVector<Decl *, 4> SYCLKernels;
11639+
11640+
public:
11641+
void addSYCLKernel(Decl *D) { SYCLKernels.push_back(D); }
11642+
/// Access to SYCL kernels.
11643+
SmallVectorImpl<Decl *> &getSYCLKernels() { return SYCLKernels; }
11644+
11645+
/// Constructs an OpenCL kernel using the KernelCaller function and adds it to
11646+
/// the SYCL device code.
11647+
void constructOpenCLKernel(FunctionDecl *KernelCallerFunc, MangleContext &MC);
1163511648
};
1163611649

1163711650
/// RAII object that enters a new expression evaluation context.

clang/lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10023,6 +10023,10 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) {
1002310023
if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
1002410024
return true;
1002510025

10026+
// If SYCL, only kernels are required.
10027+
if (LangOpts.SYCLIsDevice && !(D->hasAttr<OpenCLKernelAttr>()))
10028+
return false;
10029+
1002610030
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1002710031
// Forward declarations aren't required.
1002810032
if (!FD->doesThisDeclarationHaveABody())

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,12 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
24742474
}
24752475
}
24762476

2477+
if (LangOpts.SYCLIsDevice && Global->hasAttr<OpenCLKernelAttr>() &&
2478+
MustBeEmitted(Global)) {
2479+
addDeferredDeclToEmit(GD);
2480+
return;
2481+
}
2482+
24772483
// Ignore declarations, they will be emitted on their first use.
24782484
if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
24792485
// Forward declarations are emitted lazily on first use.

clang/lib/Parse/ParseAST.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
168168
for (Decl *D : S.WeakTopLevelDecls())
169169
Consumer->HandleTopLevelDecl(DeclGroupRef(D));
170170

171+
if (S.getLangOpts().SYCLIsDevice)
172+
for (Decl *D : S.getSYCLKernels())
173+
Consumer->HandleTopLevelDecl(DeclGroupRef(D));
174+
171175
Consumer->HandleTranslationUnit(S.getASTContext());
172176

173177
// Finalize the template instantiation observer chain.

clang/lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ add_clang_library(clangSema
5757
SemaStmt.cpp
5858
SemaStmtAsm.cpp
5959
SemaStmtAttr.cpp
60+
SemaSYCL.cpp
6061
SemaTemplate.cpp
6162
SemaTemplateDeduction.cpp
6263
SemaTemplateInstantiate.cpp

0 commit comments

Comments
 (0)