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 | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 70 | // |
| 71 | // Base class overrides. |
| 72 | // |
| 73 | |
| 74 | /// \brief Creates offloading entry for the provided entry ID \a ID, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 75 | /// address \a Addr, size \a Size, and flags \a Flags. |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 76 | void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr, |
Samuel Antao | f83efdb | 2017-01-05 16:02:49 +0000 | [diff] [blame] | 77 | uint64_t Size, int32_t Flags = 0) override; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 78 | |
Arpith Chacko Jacob | 406acdb | 2017-01-05 15:24:05 +0000 | [diff] [blame] | 79 | /// \brief Emit outlined function specialized for the Fork-Join |
| 80 | /// programming model for applicable target directives on the NVPTX device. |
| 81 | /// \param D Directive to emit. |
| 82 | /// \param ParentName Name of the function that encloses the target region. |
| 83 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 84 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 85 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
| 86 | /// An outlined function may not be an entry if, e.g. the if clause always |
| 87 | /// evaluates to false. |
| 88 | void emitGenericKernel(const OMPExecutableDirective &D, StringRef ParentName, |
| 89 | llvm::Function *&OutlinedFn, |
| 90 | llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, |
| 91 | const RegionCodeGenTy &CodeGen); |
| 92 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 93 | /// \brief Emit outlined function specialized for the Single Program |
| 94 | /// Multiple Data programming model for applicable target directives on the |
| 95 | /// NVPTX device. |
| 96 | /// \param D Directive to emit. |
| 97 | /// \param ParentName Name of the function that encloses the target region. |
| 98 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 99 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 100 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
| 101 | /// \param CodeGen Object containing the target statements. |
| 102 | /// An outlined function may not be an entry if, e.g. the if clause always |
| 103 | /// evaluates to false. |
| 104 | void emitSpmdKernel(const OMPExecutableDirective &D, StringRef ParentName, |
| 105 | llvm::Function *&OutlinedFn, |
| 106 | llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, |
| 107 | const RegionCodeGenTy &CodeGen); |
| 108 | |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 109 | /// \brief Emit outlined function for 'target' directive on the NVPTX |
| 110 | /// device. |
| 111 | /// \param D Directive to emit. |
| 112 | /// \param ParentName Name of the function that encloses the target region. |
| 113 | /// \param OutlinedFn Outlined function value to be defined by this call. |
| 114 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
| 115 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
| 116 | /// An outlined function may not be an entry if, e.g. the if clause always |
| 117 | /// evaluates to false. |
| 118 | void emitTargetOutlinedFunction(const OMPExecutableDirective &D, |
| 119 | StringRef ParentName, |
| 120 | llvm::Function *&OutlinedFn, |
| 121 | llvm::Constant *&OutlinedFnID, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 122 | bool IsOffloadEntry, |
| 123 | const RegionCodeGenTy &CodeGen) override; |
Arpith Chacko Jacob | 5c309e4 | 2016-03-22 01:48:56 +0000 | [diff] [blame] | 124 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 125 | /// \brief Emits code for parallel or serial call of the \a OutlinedFn with |
| 126 | /// variables captured in a record which address is stored in \a |
| 127 | /// CapturedStruct. |
| 128 | /// This call is for the Generic Execution Mode. |
| 129 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
| 130 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 131 | /// \param CapturedVars A pointer to the record with the references to |
| 132 | /// variables used in \a OutlinedFn function. |
| 133 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 134 | /// specified, nullptr otherwise. |
| 135 | void emitGenericParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 136 | llvm::Value *OutlinedFn, |
| 137 | ArrayRef<llvm::Value *> CapturedVars, |
| 138 | const Expr *IfCond); |
| 139 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 140 | /// \brief Emits code for parallel or serial call of the \a OutlinedFn with |
| 141 | /// variables captured in a record which address is stored in \a |
| 142 | /// CapturedStruct. |
| 143 | /// This call is for a parallel directive within an SPMD target directive. |
| 144 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
| 145 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 146 | /// \param CapturedVars A pointer to the record with the references to |
| 147 | /// variables used in \a OutlinedFn function. |
| 148 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 149 | /// specified, nullptr otherwise. |
| 150 | /// |
| 151 | void emitSpmdParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 152 | llvm::Value *OutlinedFn, |
| 153 | ArrayRef<llvm::Value *> CapturedVars, |
| 154 | const Expr *IfCond); |
| 155 | |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 156 | protected: |
| 157 | /// \brief Get the function name of an outlined region. |
| 158 | // The name can be customized depending on the target. |
| 159 | // |
| 160 | StringRef getOutlinedHelperName() const override { |
| 161 | return "__omp_outlined__"; |
| 162 | } |
| 163 | |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 164 | public: |
| 165 | explicit CGOpenMPRuntimeNVPTX(CodeGenModule &CGM); |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 166 | |
Arpith Chacko Jacob | 2cd6eea | 2017-01-25 16:55:10 +0000 | [diff] [blame] | 167 | /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 |
| 168 | /// global_tid, int proc_bind) to generate code for 'proc_bind' clause. |
| 169 | virtual void emitProcBindClause(CodeGenFunction &CGF, |
| 170 | OpenMPProcBindClauseKind ProcBind, |
| 171 | SourceLocation Loc) override; |
| 172 | |
Arpith Chacko Jacob | e04da5d | 2017-01-25 01:18:34 +0000 | [diff] [blame] | 173 | /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 |
| 174 | /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' |
| 175 | /// clause. |
| 176 | /// \param NumThreads An integer value of threads. |
| 177 | virtual void emitNumThreadsClause(CodeGenFunction &CGF, |
| 178 | llvm::Value *NumThreads, |
| 179 | SourceLocation Loc) override; |
| 180 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 181 | /// \brief This function ought to emit, in the general case, a call to |
| 182 | // the openmp runtime kmpc_push_num_teams. In NVPTX backend it is not needed |
| 183 | // as these numbers are obtained through the PTX grid and block configuration. |
| 184 | /// \param NumTeams An integer expression of teams. |
| 185 | /// \param ThreadLimit An integer expression of threads. |
| 186 | void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, |
| 187 | const Expr *ThreadLimit, SourceLocation Loc) override; |
| 188 | |
| 189 | /// \brief Emits inlined function for the specified OpenMP parallel |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 190 | // directive. |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 191 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
| 192 | /// kmp_int32 BoundID, struct context_vars*). |
| 193 | /// \param D OpenMP directive. |
| 194 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
| 195 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 196 | /// is a directive itself, for combined - its innermost directive). |
| 197 | /// \param CodeGen Code generation sequence for the \a D directive. |
| 198 | llvm::Value * |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 199 | emitParallelOutlinedFunction(const OMPExecutableDirective &D, |
| 200 | const VarDecl *ThreadIDVar, |
| 201 | OpenMPDirectiveKind InnermostKind, |
| 202 | const RegionCodeGenTy &CodeGen) override; |
| 203 | |
| 204 | /// \brief Emits inlined function for the specified OpenMP teams |
| 205 | // directive. |
| 206 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
| 207 | /// kmp_int32 BoundID, struct context_vars*). |
| 208 | /// \param D OpenMP directive. |
| 209 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
| 210 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
| 211 | /// is a directive itself, for combined - its innermost directive). |
| 212 | /// \param CodeGen Code generation sequence for the \a D directive. |
| 213 | llvm::Value * |
| 214 | emitTeamsOutlinedFunction(const OMPExecutableDirective &D, |
| 215 | const VarDecl *ThreadIDVar, |
| 216 | OpenMPDirectiveKind InnermostKind, |
| 217 | const RegionCodeGenTy &CodeGen) override; |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 218 | |
| 219 | /// \brief Emits code for teams call of the \a OutlinedFn with |
| 220 | /// variables captured in a record which address is stored in \a |
| 221 | /// CapturedStruct. |
| 222 | /// \param OutlinedFn Outlined function to be run by team masters. Type of |
| 223 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 224 | /// \param CapturedVars A pointer to the record with the references to |
| 225 | /// variables used in \a OutlinedFn function. |
| 226 | /// |
| 227 | void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 228 | SourceLocation Loc, llvm::Value *OutlinedFn, |
| 229 | ArrayRef<llvm::Value *> CapturedVars) override; |
Arpith Chacko Jacob | bb36fe8 | 2017-01-10 15:42:51 +0000 | [diff] [blame] | 230 | |
| 231 | /// \brief Emits code for parallel or serial call of the \a OutlinedFn with |
| 232 | /// variables captured in a record which address is stored in \a |
| 233 | /// CapturedStruct. |
| 234 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
| 235 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
| 236 | /// \param CapturedVars A pointer to the record with the references to |
| 237 | /// variables used in \a OutlinedFn function. |
| 238 | /// \param IfCond Condition in the associated 'if' clause, if it was |
| 239 | /// specified, nullptr otherwise. |
| 240 | void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
| 241 | llvm::Value *OutlinedFn, |
| 242 | ArrayRef<llvm::Value *> CapturedVars, |
| 243 | const Expr *IfCond) override; |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 244 | |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 245 | /// Emit a code for reduction clause. |
| 246 | /// |
| 247 | /// \param Privates List of private copies for original reduction arguments. |
| 248 | /// \param LHSExprs List of LHS in \a ReductionOps reduction operations. |
| 249 | /// \param RHSExprs List of RHS in \a ReductionOps reduction operations. |
| 250 | /// \param ReductionOps List of reduction operations in form 'LHS binop RHS' |
| 251 | /// or 'operator binop(LHS, RHS)'. |
| 252 | /// \param Options List of options for reduction codegen: |
| 253 | /// WithNowait true if parent directive has also nowait clause, false |
| 254 | /// otherwise. |
| 255 | /// SimpleReduction Emit reduction operation only. Used for omp simd |
| 256 | /// directive on the host. |
| 257 | /// ReductionKind The kind of reduction to perform. |
| 258 | virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
| 259 | ArrayRef<const Expr *> Privates, |
| 260 | ArrayRef<const Expr *> LHSExprs, |
| 261 | ArrayRef<const Expr *> RHSExprs, |
| 262 | ArrayRef<const Expr *> ReductionOps, |
| 263 | ReductionOptionsTy Options) override; |
| 264 | |
| 265 | /// Returns specified OpenMP runtime function for the current OpenMP |
| 266 | /// implementation. Specialized for the NVPTX device. |
| 267 | /// \param Function OpenMP runtime function. |
| 268 | /// \return Specified function. |
| 269 | llvm::Constant *createNVPTXRuntimeFunction(unsigned Function); |
| 270 | |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 271 | /// Translates the native parameter of outlined function if this is required |
| 272 | /// for target. |
| 273 | /// \param FD Field decl from captured record for the paramater. |
| 274 | /// \param NativeParam Parameter itself. |
| 275 | const VarDecl *translateParameter(const FieldDecl *FD, |
| 276 | const VarDecl *NativeParam) const override; |
| 277 | |
| 278 | /// Gets the address of the native argument basing on the address of the |
| 279 | /// target-specific parameter. |
| 280 | /// \param NativeParam Parameter itself. |
| 281 | /// \param TargetParam Corresponding target-specific parameter. |
| 282 | Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam, |
| 283 | const VarDecl *TargetParam) const override; |
| 284 | |
| 285 | /// Emits call of the outlined function with the provided arguments, |
| 286 | /// translating these arguments to correct target-specific arguments. |
| 287 | void emitOutlinedFunctionCall( |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 288 | CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 289 | ArrayRef<llvm::Value *> Args = llvm::None) const override; |
| 290 | |
Arpith Chacko Jacob | 44a87c9 | 2017-01-18 19:35:00 +0000 | [diff] [blame] | 291 | /// Target codegen is specialized based on two programming models: the |
| 292 | /// 'generic' fork-join model of OpenMP, and a more GPU efficient 'spmd' |
| 293 | /// model for constructs like 'target parallel' that support it. |
| 294 | enum ExecutionMode { |
| 295 | /// Single Program Multiple Data. |
| 296 | Spmd, |
| 297 | /// Generic codegen to support fork-join model. |
| 298 | Generic, |
| 299 | Unknown, |
| 300 | }; |
| 301 | |
| 302 | private: |
| 303 | // Track the execution mode when codegening directives within a target |
| 304 | // region. The appropriate mode (generic/spmd) is set on entry to the |
| 305 | // target region and used by containing directives such as 'parallel' |
| 306 | // to emit optimized code. |
| 307 | ExecutionMode CurrentExecutionMode; |
Gheorghe-Teodor Bercea | eb89b1d | 2017-11-21 15:54:54 +0000 | [diff] [blame] | 308 | |
| 309 | /// Map between an outlined function and its wrapper. |
| 310 | llvm::DenseMap<llvm::Function *, llvm::Function *> WrapperFunctionsMap; |
| 311 | |
| 312 | /// Emit function which wraps the outline parallel region |
| 313 | /// and controls the parameters which are passed to this function. |
| 314 | /// The wrapper ensures that the outlined function is called |
| 315 | /// with the correct arguments when data is shared. |
| 316 | llvm::Function * |
| 317 | createDataSharingWrapper(llvm::Function *OutlinedParallelFn, |
| 318 | const OMPExecutableDirective &D); |
Samuel Antao | 45bfe4c | 2016-02-08 15:59:20 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | } // CodeGen namespace. |
| 322 | } // clang namespace. |
| 323 | |
| 324 | #endif // LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H |