Skip to content
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

[SYCL] Implement OpenCL kernel function generation #249

Closed
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: 13 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -11632,6 +11632,19 @@ class Sema final {
ConstructorDestructor,
BuiltinFunction
};

private:
/// Contains generated OpenCL kernel functions for SYCL.
SmallVector<Decl *, 4> SYCLKernels;

public:
void addSYCLKernel(Decl *D) { SYCLKernels.push_back(D); }
/// Access to SYCL kernels.
SmallVectorImpl<Decl *> &getSYCLKernels() { return SYCLKernels; }

/// Constructs an OpenCL kernel using the KernelCaller function and adds it to
/// the SYCL device code.
void constructOpenCLKernel(FunctionDecl *KernelCallerFunc, MangleContext &MC);
};

/// RAII object that enters a new expression evaluation context.
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10023,6 +10023,10 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) {
if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
return true;

// If SYCL, only kernels are required.
if (LangOpts.SYCLIsDevice && !(D->hasAttr<OpenCLKernelAttr>()))
return false;

if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
// Forward declarations aren't required.
if (!FD->doesThisDeclarationHaveABody())
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,12 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
}
}

if (LangOpts.SYCLIsDevice && Global->hasAttr<OpenCLKernelAttr>() &&
MustBeEmitted(Global)) {
addDeferredDeclToEmit(GD);
return;
}

// Ignore declarations, they will be emitted on their first use.
if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
// Forward declarations are emitted lazily on first use.
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Parse/ParseAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
for (Decl *D : S.WeakTopLevelDecls())
Consumer->HandleTopLevelDecl(DeclGroupRef(D));

if (S.getLangOpts().SYCLIsDevice)
for (Decl *D : S.getSYCLKernels())
Consumer->HandleTopLevelDecl(DeclGroupRef(D));

Consumer->HandleTranslationUnit(S.getASTContext());

// Finalize the template instantiation observer chain.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ add_clang_library(clangSema
SemaStmt.cpp
SemaStmtAsm.cpp
SemaStmtAttr.cpp
SemaSYCL.cpp
SemaTemplate.cpp
SemaTemplateDeduction.cpp
SemaTemplateInstantiate.cpp
Expand Down
Loading