| 1 | //===- StmtSYCL.h - Classes for SYCL kernel calls ---------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// This file defines SYCL AST classes used to represent calls to SYCL kernels. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #ifndef LLVM_CLANG_AST_STMTSYCL_H |
| 13 | #define LLVM_CLANG_AST_STMTSYCL_H |
| 14 | |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/Stmt.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
| 19 | |
| 20 | namespace clang { |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // AST classes for SYCL kernel calls. |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | /// SYCLKernelCallStmt represents the transformation that is applied to the body |
| 27 | /// of a function declared with the sycl_kernel_entry_point attribute. The body |
| 28 | /// of such a function specifies the statements to be executed on a SYCL device |
| 29 | /// to invoke a SYCL kernel with a particular set of kernel arguments. The |
| 30 | /// SYCLKernelCallStmt associates an original statement (the compound statement |
| 31 | /// that is the function body) with a kernel launch statement to execute on a |
| 32 | /// SYCL host and an OutlinedFunctionDecl that holds the kernel parameters and |
| 33 | /// the transformed body to execute on a SYCL device. During code generation, |
| 34 | /// the OutlinedFunctionDecl is used to emit an offload kernel entry point |
| 35 | /// suitable for invocation from a SYCL library implementation. |
| 36 | class SYCLKernelCallStmt : public Stmt { |
| 37 | friend class ASTStmtReader; |
| 38 | friend class ASTStmtWriter; |
| 39 | |
| 40 | private: |
| 41 | Stmt *OriginalStmt = nullptr; |
| 42 | Stmt *KernelLaunchStmt = nullptr; |
| 43 | OutlinedFunctionDecl *OFDecl = nullptr; |
| 44 | |
| 45 | public: |
| 46 | /// Construct a SYCL kernel call statement. |
| 47 | SYCLKernelCallStmt(CompoundStmt *CS, Stmt *S, OutlinedFunctionDecl *OFD) |
| 48 | : Stmt(SYCLKernelCallStmtClass), OriginalStmt(CS), KernelLaunchStmt(S), |
| 49 | OFDecl(OFD) {} |
| 50 | |
| 51 | /// Construct an empty SYCL kernel call statement. |
| 52 | SYCLKernelCallStmt(EmptyShell Empty) : Stmt(SYCLKernelCallStmtClass, Empty) {} |
| 53 | |
| 54 | CompoundStmt *getOriginalStmt() { return cast<CompoundStmt>(Val: OriginalStmt); } |
| 55 | const CompoundStmt *getOriginalStmt() const { |
| 56 | return cast<CompoundStmt>(Val: OriginalStmt); |
| 57 | } |
| 58 | |
| 59 | void setOriginalStmt(CompoundStmt *CS) { OriginalStmt = CS; } |
| 60 | |
| 61 | Stmt *getKernelLaunchStmt() { return KernelLaunchStmt; } |
| 62 | const Stmt *getKernelLaunchStmt() const { return KernelLaunchStmt; } |
| 63 | |
| 64 | void setKernelLaunchStmt(Stmt *S) { KernelLaunchStmt = S; } |
| 65 | |
| 66 | OutlinedFunctionDecl *getOutlinedFunctionDecl() { return OFDecl; } |
| 67 | const OutlinedFunctionDecl *getOutlinedFunctionDecl() const { return OFDecl; } |
| 68 | |
| 69 | void setOutlinedFunctionDecl(OutlinedFunctionDecl *OFD) { OFDecl = OFD; } |
| 70 | |
| 71 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 72 | return getOriginalStmt()->getBeginLoc(); |
| 73 | } |
| 74 | |
| 75 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 76 | return getOriginalStmt()->getEndLoc(); |
| 77 | } |
| 78 | |
| 79 | SourceRange getSourceRange() const LLVM_READONLY { |
| 80 | return getOriginalStmt()->getSourceRange(); |
| 81 | } |
| 82 | |
| 83 | static bool classof(const Stmt *T) { |
| 84 | return T->getStmtClass() == SYCLKernelCallStmtClass; |
| 85 | } |
| 86 | |
| 87 | child_range children() { |
| 88 | return child_range(&OriginalStmt, &OriginalStmt + 1); |
| 89 | } |
| 90 | |
| 91 | const_child_range children() const { |
| 92 | return const_child_range(&OriginalStmt, &OriginalStmt + 1); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | // UnresolvedSYCLKernelCallStmt represents an invocation of a SYCL kernel in |
| 97 | // a dependent context for which lookup of the sycl_kernel_launch identifier |
| 98 | // cannot be performed. These statements are transformed to SYCLKernelCallStmt |
| 99 | // during template instantiation. |
| 100 | class UnresolvedSYCLKernelCallStmt : public Stmt { |
| 101 | friend class ASTStmtReader; |
| 102 | friend class ASTStmtWriter; |
| 103 | |
| 104 | private: |
| 105 | Stmt *OriginalStmt = nullptr; |
| 106 | // KernelLaunchIdExpr stores an UnresolvedLookupExpr or UnresolvedMemberExpr |
| 107 | // corresponding to the SYCL kernel launch function for which a call |
| 108 | // will be synthesized during template instantiation. |
| 109 | Expr *KernelLaunchIdExpr = nullptr; |
| 110 | |
| 111 | UnresolvedSYCLKernelCallStmt(CompoundStmt *CS, Expr *IdExpr) |
| 112 | : Stmt(UnresolvedSYCLKernelCallStmtClass), OriginalStmt(CS), |
| 113 | KernelLaunchIdExpr(IdExpr) {} |
| 114 | |
| 115 | void setOriginalStmt(CompoundStmt *CS) { OriginalStmt = CS; } |
| 116 | |
| 117 | void setKernelLaunchIdExpr(Expr *IdExpr) { KernelLaunchIdExpr = IdExpr; } |
| 118 | |
| 119 | public: |
| 120 | static UnresolvedSYCLKernelCallStmt *Create(const ASTContext &C, |
| 121 | CompoundStmt *CS, Expr *IdExpr) { |
| 122 | return new (C) UnresolvedSYCLKernelCallStmt(CS, IdExpr); |
| 123 | } |
| 124 | |
| 125 | static UnresolvedSYCLKernelCallStmt *CreateEmpty(const ASTContext &C) { |
| 126 | return new (C) UnresolvedSYCLKernelCallStmt(nullptr, nullptr); |
| 127 | } |
| 128 | |
| 129 | CompoundStmt *getOriginalStmt() { return cast<CompoundStmt>(Val: OriginalStmt); } |
| 130 | const CompoundStmt *getOriginalStmt() const { |
| 131 | return cast<CompoundStmt>(Val: OriginalStmt); |
| 132 | } |
| 133 | |
| 134 | Expr *getKernelLaunchIdExpr() { return KernelLaunchIdExpr; } |
| 135 | const Expr *getKernelLaunchIdExpr() const { return KernelLaunchIdExpr; } |
| 136 | |
| 137 | SourceLocation getBeginLoc() const LLVM_READONLY { |
| 138 | return getOriginalStmt()->getBeginLoc(); |
| 139 | } |
| 140 | |
| 141 | SourceLocation getEndLoc() const LLVM_READONLY { |
| 142 | return getOriginalStmt()->getEndLoc(); |
| 143 | } |
| 144 | static bool classof(const Stmt *T) { |
| 145 | return T->getStmtClass() == UnresolvedSYCLKernelCallStmtClass; |
| 146 | } |
| 147 | child_range children() { |
| 148 | return child_range(&OriginalStmt, &OriginalStmt + 1); |
| 149 | } |
| 150 | |
| 151 | const_child_range children() const { |
| 152 | return const_child_range(&OriginalStmt, &OriginalStmt + 1); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | } // end namespace clang |
| 157 | |
| 158 | #endif // LLVM_CLANG_AST_STMTSYCL_H |
| 159 | |