Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 1 | //===----- CGOpenMPRuntimeNVPTX.h - Interface to OpenMP NVPTX Runtimes ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides a class for OpenMP runtime code generation specialized to NVPTX |
| 11 | // targets. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H |
| 17 | |
| 18 | #include "CGOpenMPRuntime.h" |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
| 20 | #include "clang/AST/StmtOpenMP.h" |
| 21 | #include "llvm/IR/CallSite.h" |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
| 24 | namespace CodeGen { |
| 25 | |
| 26 | class CGOpenMPRuntimeNVPTX : public CGOpenMPRuntime { |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 27 | private: |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 28 | // Parallel outlined function work for workers to execute. |
| 29 | llvm::SmallVector<llvm::Function *, 16> Work; |
| 30 | |
Alexey Bataev | 5e87c34 | 2016-12-22 19:44:05 +0000 | [diff] [blame] | 31 | struct EntryFunctionState { |
| 32 | llvm::BasicBlock *ExitBB = nullptr; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | class WorkerFunctionState { |
| 36 | public: |
| 37 | llvm::Function *WorkerFn; |
| 38 | const CGFunctionInfo *CGFI; |
| 39 | |
| 40 | WorkerFunctionState(CodeGenModule &CGM); |
| 41 | |
| 42 | private: |
| 43 | void createWorkerFunction(CodeGenModule &CGM); |
| 44 | }; |
| 45 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 46 | bool isInSpmdExecutionMode() const; |
| 47 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 48 | /// \brief Emit the worker function for the current target region. |
| 49 | void emitWorkerFunction(WorkerFunctionState &WST); |
| 50 | |
| 51 | /// \brief Helper for worker function. Emit body of worker loop. |
| 52 | void emitWorkerLoop(CodeGenFunction &CGF, WorkerFunctionState &WST); |
| 53 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 54 | /// \brief Helper for generic target entry function. Guide the master and |
| 55 | /// worker threads to their respective locations. |
| 56 | void emitGenericEntryHeader(CodeGenFunction &CGF, EntryFunctionState &EST, |
| 57 | WorkerFunctionState &WST); |
| 58 | |
| 59 | /// \brief Signal termination of OMP execution for generic target entry |
| 60 | /// function. |
| 61 | void emitGenericEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST); |
| 62 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 63 | /// \brief Helper for Spmd mode target directive's entry function. |
| 64 | void emitSpmdEntryHeader(CodeGenFunction &CGF, EntryFunctionState &EST, |
| 65 | const OMPExecutableDirective &D); |
| 66 | |
| 67 | /// \brief Signal termination of Spmd mode execution. |
| 68 | void emitSpmdEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST); |
| 69 | |
Arpith Chacko Jacob | bd6344c | 2017-02-16 14:25:35 +0000 | [diff] [blame] | 70 | /// \brief Returns specified OpenMP runtime function for the current OpenMP |
| 71 | /// implementation. Specialized for the NVPTX device. |
| 72 | /// \param Function OpenMP runtime function. |
| 73 | /// \return Specified function. |
| 74 | llvm::Constant *createNVPTXRuntimeFunction(unsigned Function); |
| 75 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 76 | // |
| 77 | // Base class overrides. |
| 78 | // |
| 79 | |
| 80 | /// \brief Creates offloading entry for the provided entry ID \a ID, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 81 | /// address \a Addr, size \a Size, and flags \a Flags. |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 82 | void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 83 | uint64_t Size, int32_t Flags = 0) override; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 84 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 85 | /// \brief Emit outlined function specialized for the Fork-Join |
| 86 | /// programming model for applicable target directives on the NVPTX device. |
| 87 | /// \param D Directive to emit. |
| 88 | /// \param ParentName Name of the function that encloses the target region. |
| 89 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 90 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 91 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
| 92 | /// An outlined function may not be an entry if, e.g. the if clause always |
| 93 | /// evaluates to false. |
| 94 | void emitGenericKernel(const OMPExecutableDirective &D, StringRef ParentName, |
| 95 | llvm::Function *&OutlinedFn, |
| 96 | llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, |
| 97 | const RegionCodeGenTy &CodeGen); |
| 98 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 99 | /// \brief Emit outlined function specialized for the Single Program |
| 100 | /// Multiple Data programming model for applicable target directives on the |
| 101 | /// NVPTX device. |
| 102 | /// \param D Directive to emit. |
| 103 | /// \param ParentName Name of the function that encloses the target region. |
| 104 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 105 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 106 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
| 107 | /// \param CodeGen Object containing the target statements. |
| 108 | /// An outlined function may not be an entry if, e.g. the if clause always |
| 109 | /// evaluates to false. |
| 110 | void emitSpmdKernel(const OMPExecutableDirective &D, StringRef ParentName, |
| 111 | llvm::Function *&OutlinedFn, |
| 112 | llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, |
| 113 | const RegionCodeGenTy &CodeGen); |
| 114 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 115 | /// \brief Emit outlined function for 'target' directive on the NVPTX |
| 116 | /// device. |
| 117 | /// \param D Directive to emit. |
| 118 | /// \param ParentName Name of the function that encloses the target region. |
| 119 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 120 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 121 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
| 122 | /// An outlined function may not be an entry if, e.g. the if clause always |
| 123 | /// evaluates to false. |
| 124 | void emitTargetOutlinedFunction(const OMPExecutableDirective &D, |
| 125 | StringRef ParentName, |
| 126 | llvm::Function *&OutlinedFn, |
| 127 | llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 128 | bool IsOffloadEntry, |
| 129 | const RegionCodeGenTy &CodeGen) override; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 130 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 131 | /// \brief Emits code for parallel or serial call of the \a OutlinedFn with |
| 132 | /// variables captured in a record which address is stored in \a |
| 133 | /// CapturedStruct. |
| 134 | /// This call is for the Generic Execution Mode. |
| 135 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
| 136 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 137 | /// \param CapturedVars A pointer to the record with the references to |
| 138 | /// variables used in \a OutlinedFn function. |
| 139 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 140 | /// specified, nullptr otherwise. |
| 141 | void emitGenericParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 142 | llvm::Value *OutlinedFn, |
| 143 | ArrayRef<llvm::Value *> CapturedVars, |
| 144 | const Expr *IfCond); |
| 145 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 146 | /// \brief Emits code for parallel or serial call of the \a OutlinedFn with |
| 147 | /// variables captured in a record which address is stored in \a |
| 148 | /// CapturedStruct. |
| 149 | /// This call is for a parallel directive within an SPMD target directive. |
| 150 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
| 151 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 152 | /// \param CapturedVars A pointer to the record with the references to |
| 153 | /// variables used in \a OutlinedFn function. |
| 154 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 155 | /// specified, nullptr otherwise. |
| 156 | /// |
| 157 | void emitSpmdParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 158 | llvm::Value *OutlinedFn, |
| 159 | ArrayRef<llvm::Value *> CapturedVars, |
| 160 | const Expr *IfCond); |
| 161 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 162 | protected: |
| 163 | /// \brief Get the function name of an outlined region. |
| 164 | // The name can be customized depending on the target. |
| 165 | // |
| 166 | StringRef getOutlinedHelperName() const override { |
| 167 | return "__omp_outlined__"; |
| 168 | } |
| 169 | |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 170 | public: |
| 171 | explicit CGOpenMPRuntimeNVPTX(CodeGenModule &CGM); |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 172 | |
Arpith Chacko Jacob | 2cd6eea | 2017-01-25 16:55:10 +0000 | [diff] [blame] | 173 | /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 |
| 174 | /// global_tid, int proc_bind) to generate code for 'proc_bind' clause. |
| 175 | virtual void emitProcBindClause(CodeGenFunction &CGF, |
| 176 | OpenMPProcBindClauseKind ProcBind, |
| 177 | SourceLocation Loc) override; |
| 178 | |
Arpith Chacko Jacob | e04da5d | 2017-01-25 01:18:34 +0000 | [diff] [blame] | 179 | /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 |
| 180 | /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' |
| 181 | /// clause. |
| 182 | /// \param NumThreads An integer value of threads. |
| 183 | virtual void emitNumThreadsClause(CodeGenFunction &CGF, |
| 184 | llvm::Value *NumThreads, |
| 185 | SourceLocation Loc) override; |
| 186 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 187 | /// \brief This function ought to emit, in the general case, a call to |
| 188 | // the openmp runtime kmpc_push_num_teams. In NVPTX backend it is not needed |
| 189 | // as these numbers are obtained through the PTX grid and block configuration. |
| 190 | /// \param NumTeams An integer expression of teams. |
| 191 | /// \param ThreadLimit An integer expression of threads. |
| 192 | void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, |
| 193 | const Expr *ThreadLimit, SourceLocation Loc) override; |
| 194 | |
| 195 | /// \brief Emits inlined function for the specified OpenMP parallel |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 196 | // directive. |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 197 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
| 198 | /// kmp_int32 BoundID, struct context_vars*). |
| 199 | /// \param D OpenMP directive. |
| 200 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
| 201 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 202 | /// is a directive itself, for combined - its innermost directive). |
| 203 | /// \param CodeGen Code generation sequence for the \a D directive. |
| 204 | llvm::Value * |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 205 | emitParallelOutlinedFunction(const OMPExecutableDirective &D, |
| 206 | const VarDecl *ThreadIDVar, |
| 207 | OpenMPDirectiveKind InnermostKind, |
| 208 | const RegionCodeGenTy &CodeGen) override; |
| 209 | |
| 210 | /// \brief Emits inlined function for the specified OpenMP teams |
| 211 | // directive. |
| 212 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
| 213 | /// kmp_int32 BoundID, struct context_vars*). |
| 214 | /// \param D OpenMP directive. |
| 215 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
| 216 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 217 | /// is a directive itself, for combined - its innermost directive). |
| 218 | /// \param CodeGen Code generation sequence for the \a D directive. |
| 219 | llvm::Value * |
| 220 | emitTeamsOutlinedFunction(const OMPExecutableDirective &D, |
| 221 | const VarDecl *ThreadIDVar, |
| 222 | OpenMPDirectiveKind InnermostKind, |
| 223 | const RegionCodeGenTy &CodeGen) override; |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 224 | |
| 225 | /// \brief Emits code for teams call of the \a OutlinedFn with |
| 226 | /// variables captured in a record which address is stored in \a |
| 227 | /// CapturedStruct. |
| 228 | /// \param OutlinedFn Outlined function to be run by team masters. Type of |
| 229 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 230 | /// \param CapturedVars A pointer to the record with the references to |
| 231 | /// variables used in \a OutlinedFn function. |
| 232 | /// |
| 233 | void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 234 | SourceLocation Loc, llvm::Value *OutlinedFn, |
| 235 | ArrayRef<llvm::Value *> CapturedVars) override; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 236 | |
| 237 | /// \brief Emits code for parallel or serial call of the \a OutlinedFn with |
| 238 | /// variables captured in a record which address is stored in \a |
| 239 | /// CapturedStruct. |
| 240 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
| 241 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 242 | /// \param CapturedVars A pointer to the record with the references to |
| 243 | /// variables used in \a OutlinedFn function. |
| 244 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 245 | /// specified, nullptr otherwise. |
| 246 | void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 247 | llvm::Value *OutlinedFn, |
| 248 | ArrayRef<llvm::Value *> CapturedVars, |
| 249 | const Expr *IfCond) override; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 250 | |
Arpith Chacko Jacob | bd6344c | 2017-02-16 14:25:35 +0000 | [diff] [blame] | 251 | public: |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 252 | /// Target codegen is specialized based on two programming models: the |
| 253 | /// 'generic' fork-join model of OpenMP, and a more GPU efficient 'spmd' |
| 254 | /// model for constructs like 'target parallel' that support it. |
| 255 | enum ExecutionMode { |
| 256 | /// Single Program Multiple Data. |
| 257 | Spmd, |
| 258 | /// Generic codegen to support fork-join model. |
| 259 | Generic, |
| 260 | Unknown, |
| 261 | }; |
| 262 | |
| 263 | private: |
| 264 | // Track the execution mode when codegening directives within a target |
| 265 | // region. The appropriate mode (generic/spmd) is set on entry to the |
| 266 | // target region and used by containing directives such as 'parallel' |
| 267 | // to emit optimized code. |
| 268 | ExecutionMode CurrentExecutionMode; |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | } // CodeGen namespace. |
| 272 | } // clang namespace. |
| 273 | |
| 274 | #endif // LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H |