1*e8d8bef9SDimitry Andric //===------ CGOpenMPRuntimeGPU.h - Interface to OpenMP GPU Runtimes ------===// 2*e8d8bef9SDimitry Andric // 3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*e8d8bef9SDimitry Andric // 7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8*e8d8bef9SDimitry Andric // 9*e8d8bef9SDimitry Andric // This provides a generalized class for OpenMP runtime code generation 10*e8d8bef9SDimitry Andric // specialized by GPU targets NVPTX and AMDGCN. 11*e8d8bef9SDimitry Andric // 12*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 13*e8d8bef9SDimitry Andric 14*e8d8bef9SDimitry Andric #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMEGPU_H 15*e8d8bef9SDimitry Andric #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMEGPU_H 16*e8d8bef9SDimitry Andric 17*e8d8bef9SDimitry Andric #include "CGOpenMPRuntime.h" 18*e8d8bef9SDimitry Andric #include "CodeGenFunction.h" 19*e8d8bef9SDimitry Andric #include "clang/AST/StmtOpenMP.h" 20*e8d8bef9SDimitry Andric #include "llvm/Frontend/OpenMP/OMPGridValues.h" 21*e8d8bef9SDimitry Andric 22*e8d8bef9SDimitry Andric namespace clang { 23*e8d8bef9SDimitry Andric namespace CodeGen { 24*e8d8bef9SDimitry Andric 25*e8d8bef9SDimitry Andric class CGOpenMPRuntimeGPU : public CGOpenMPRuntime { 26*e8d8bef9SDimitry Andric public: 27*e8d8bef9SDimitry Andric /// Defines the execution mode. 28*e8d8bef9SDimitry Andric enum ExecutionMode { 29*e8d8bef9SDimitry Andric /// SPMD execution mode (all threads are worker threads). 30*e8d8bef9SDimitry Andric EM_SPMD, 31*e8d8bef9SDimitry Andric /// Non-SPMD execution mode (1 master thread, others are workers). 32*e8d8bef9SDimitry Andric EM_NonSPMD, 33*e8d8bef9SDimitry Andric /// Unknown execution mode (orphaned directive). 34*e8d8bef9SDimitry Andric EM_Unknown, 35*e8d8bef9SDimitry Andric }; 36*e8d8bef9SDimitry Andric private: 37*e8d8bef9SDimitry Andric /// Parallel outlined function work for workers to execute. 38*e8d8bef9SDimitry Andric llvm::SmallVector<llvm::Function *, 16> Work; 39*e8d8bef9SDimitry Andric 40*e8d8bef9SDimitry Andric struct EntryFunctionState { 41*e8d8bef9SDimitry Andric llvm::BasicBlock *ExitBB = nullptr; 42*e8d8bef9SDimitry Andric }; 43*e8d8bef9SDimitry Andric 44*e8d8bef9SDimitry Andric class WorkerFunctionState { 45*e8d8bef9SDimitry Andric public: 46*e8d8bef9SDimitry Andric llvm::Function *WorkerFn; 47*e8d8bef9SDimitry Andric const CGFunctionInfo &CGFI; 48*e8d8bef9SDimitry Andric SourceLocation Loc; 49*e8d8bef9SDimitry Andric 50*e8d8bef9SDimitry Andric WorkerFunctionState(CodeGenModule &CGM, SourceLocation Loc); 51*e8d8bef9SDimitry Andric 52*e8d8bef9SDimitry Andric private: 53*e8d8bef9SDimitry Andric void createWorkerFunction(CodeGenModule &CGM); 54*e8d8bef9SDimitry Andric }; 55*e8d8bef9SDimitry Andric 56*e8d8bef9SDimitry Andric ExecutionMode getExecutionMode() const; 57*e8d8bef9SDimitry Andric 58*e8d8bef9SDimitry Andric bool requiresFullRuntime() const { return RequiresFullRuntime; } 59*e8d8bef9SDimitry Andric 60*e8d8bef9SDimitry Andric /// Get barrier to synchronize all threads in a block. 61*e8d8bef9SDimitry Andric void syncCTAThreads(CodeGenFunction &CGF); 62*e8d8bef9SDimitry Andric 63*e8d8bef9SDimitry Andric /// Emit the worker function for the current target region. 64*e8d8bef9SDimitry Andric void emitWorkerFunction(WorkerFunctionState &WST); 65*e8d8bef9SDimitry Andric 66*e8d8bef9SDimitry Andric /// Helper for worker function. Emit body of worker loop. 67*e8d8bef9SDimitry Andric void emitWorkerLoop(CodeGenFunction &CGF, WorkerFunctionState &WST); 68*e8d8bef9SDimitry Andric 69*e8d8bef9SDimitry Andric /// Helper for non-SPMD target entry function. Guide the master and 70*e8d8bef9SDimitry Andric /// worker threads to their respective locations. 71*e8d8bef9SDimitry Andric void emitNonSPMDEntryHeader(CodeGenFunction &CGF, EntryFunctionState &EST, 72*e8d8bef9SDimitry Andric WorkerFunctionState &WST); 73*e8d8bef9SDimitry Andric 74*e8d8bef9SDimitry Andric /// Signal termination of OMP execution for non-SPMD target entry 75*e8d8bef9SDimitry Andric /// function. 76*e8d8bef9SDimitry Andric void emitNonSPMDEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST); 77*e8d8bef9SDimitry Andric 78*e8d8bef9SDimitry Andric /// Helper for generic variables globalization prolog. 79*e8d8bef9SDimitry Andric void emitGenericVarsProlog(CodeGenFunction &CGF, SourceLocation Loc, 80*e8d8bef9SDimitry Andric bool WithSPMDCheck = false); 81*e8d8bef9SDimitry Andric 82*e8d8bef9SDimitry Andric /// Helper for generic variables globalization epilog. 83*e8d8bef9SDimitry Andric void emitGenericVarsEpilog(CodeGenFunction &CGF, bool WithSPMDCheck = false); 84*e8d8bef9SDimitry Andric 85*e8d8bef9SDimitry Andric /// Helper for SPMD mode target directive's entry function. 86*e8d8bef9SDimitry Andric void emitSPMDEntryHeader(CodeGenFunction &CGF, EntryFunctionState &EST, 87*e8d8bef9SDimitry Andric const OMPExecutableDirective &D); 88*e8d8bef9SDimitry Andric 89*e8d8bef9SDimitry Andric /// Signal termination of SPMD mode execution. 90*e8d8bef9SDimitry Andric void emitSPMDEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST); 91*e8d8bef9SDimitry Andric 92*e8d8bef9SDimitry Andric // 93*e8d8bef9SDimitry Andric // Base class overrides. 94*e8d8bef9SDimitry Andric // 95*e8d8bef9SDimitry Andric 96*e8d8bef9SDimitry Andric /// Creates offloading entry for the provided entry ID \a ID, 97*e8d8bef9SDimitry Andric /// address \a Addr, size \a Size, and flags \a Flags. 98*e8d8bef9SDimitry Andric void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr, 99*e8d8bef9SDimitry Andric uint64_t Size, int32_t Flags, 100*e8d8bef9SDimitry Andric llvm::GlobalValue::LinkageTypes Linkage) override; 101*e8d8bef9SDimitry Andric 102*e8d8bef9SDimitry Andric /// Emit outlined function specialized for the Fork-Join 103*e8d8bef9SDimitry Andric /// programming model for applicable target directives on the NVPTX device. 104*e8d8bef9SDimitry Andric /// \param D Directive to emit. 105*e8d8bef9SDimitry Andric /// \param ParentName Name of the function that encloses the target region. 106*e8d8bef9SDimitry Andric /// \param OutlinedFn Outlined function value to be defined by this call. 107*e8d8bef9SDimitry Andric /// \param OutlinedFnID Outlined function ID value to be defined by this call. 108*e8d8bef9SDimitry Andric /// \param IsOffloadEntry True if the outlined function is an offload entry. 109*e8d8bef9SDimitry Andric /// An outlined function may not be an entry if, e.g. the if clause always 110*e8d8bef9SDimitry Andric /// evaluates to false. 111*e8d8bef9SDimitry Andric void emitNonSPMDKernel(const OMPExecutableDirective &D, StringRef ParentName, 112*e8d8bef9SDimitry Andric llvm::Function *&OutlinedFn, 113*e8d8bef9SDimitry Andric llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, 114*e8d8bef9SDimitry Andric const RegionCodeGenTy &CodeGen); 115*e8d8bef9SDimitry Andric 116*e8d8bef9SDimitry Andric /// Emit outlined function specialized for the Single Program 117*e8d8bef9SDimitry Andric /// Multiple Data programming model for applicable target directives on the 118*e8d8bef9SDimitry Andric /// NVPTX device. 119*e8d8bef9SDimitry Andric /// \param D Directive to emit. 120*e8d8bef9SDimitry Andric /// \param ParentName Name of the function that encloses the target region. 121*e8d8bef9SDimitry Andric /// \param OutlinedFn Outlined function value to be defined by this call. 122*e8d8bef9SDimitry Andric /// \param OutlinedFnID Outlined function ID value to be defined by this call. 123*e8d8bef9SDimitry Andric /// \param IsOffloadEntry True if the outlined function is an offload entry. 124*e8d8bef9SDimitry Andric /// \param CodeGen Object containing the target statements. 125*e8d8bef9SDimitry Andric /// An outlined function may not be an entry if, e.g. the if clause always 126*e8d8bef9SDimitry Andric /// evaluates to false. 127*e8d8bef9SDimitry Andric void emitSPMDKernel(const OMPExecutableDirective &D, StringRef ParentName, 128*e8d8bef9SDimitry Andric llvm::Function *&OutlinedFn, 129*e8d8bef9SDimitry Andric llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, 130*e8d8bef9SDimitry Andric const RegionCodeGenTy &CodeGen); 131*e8d8bef9SDimitry Andric 132*e8d8bef9SDimitry Andric /// Emit outlined function for 'target' directive on the NVPTX 133*e8d8bef9SDimitry Andric /// device. 134*e8d8bef9SDimitry Andric /// \param D Directive to emit. 135*e8d8bef9SDimitry Andric /// \param ParentName Name of the function that encloses the target region. 136*e8d8bef9SDimitry Andric /// \param OutlinedFn Outlined function value to be defined by this call. 137*e8d8bef9SDimitry Andric /// \param OutlinedFnID Outlined function ID value to be defined by this call. 138*e8d8bef9SDimitry Andric /// \param IsOffloadEntry True if the outlined function is an offload entry. 139*e8d8bef9SDimitry Andric /// An outlined function may not be an entry if, e.g. the if clause always 140*e8d8bef9SDimitry Andric /// evaluates to false. 141*e8d8bef9SDimitry Andric void emitTargetOutlinedFunction(const OMPExecutableDirective &D, 142*e8d8bef9SDimitry Andric StringRef ParentName, 143*e8d8bef9SDimitry Andric llvm::Function *&OutlinedFn, 144*e8d8bef9SDimitry Andric llvm::Constant *&OutlinedFnID, 145*e8d8bef9SDimitry Andric bool IsOffloadEntry, 146*e8d8bef9SDimitry Andric const RegionCodeGenTy &CodeGen) override; 147*e8d8bef9SDimitry Andric 148*e8d8bef9SDimitry Andric /// Emits code for parallel or serial call of the \a OutlinedFn with 149*e8d8bef9SDimitry Andric /// variables captured in a record which address is stored in \a 150*e8d8bef9SDimitry Andric /// CapturedStruct. 151*e8d8bef9SDimitry Andric /// This call is for the Non-SPMD Execution Mode. 152*e8d8bef9SDimitry Andric /// \param OutlinedFn Outlined function to be run in parallel threads. Type of 153*e8d8bef9SDimitry Andric /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). 154*e8d8bef9SDimitry Andric /// \param CapturedVars A pointer to the record with the references to 155*e8d8bef9SDimitry Andric /// variables used in \a OutlinedFn function. 156*e8d8bef9SDimitry Andric /// \param IfCond Condition in the associated 'if' clause, if it was 157*e8d8bef9SDimitry Andric /// specified, nullptr otherwise. 158*e8d8bef9SDimitry Andric void emitNonSPMDParallelCall(CodeGenFunction &CGF, SourceLocation Loc, 159*e8d8bef9SDimitry Andric llvm::Value *OutlinedFn, 160*e8d8bef9SDimitry Andric ArrayRef<llvm::Value *> CapturedVars, 161*e8d8bef9SDimitry Andric const Expr *IfCond); 162*e8d8bef9SDimitry Andric 163*e8d8bef9SDimitry Andric /// Emits code for parallel or serial call of the \a OutlinedFn with 164*e8d8bef9SDimitry Andric /// variables captured in a record which address is stored in \a 165*e8d8bef9SDimitry Andric /// CapturedStruct. 166*e8d8bef9SDimitry Andric /// This call is for a parallel directive within an SPMD target directive. 167*e8d8bef9SDimitry Andric /// \param OutlinedFn Outlined function to be run in parallel threads. Type of 168*e8d8bef9SDimitry Andric /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). 169*e8d8bef9SDimitry Andric /// \param CapturedVars A pointer to the record with the references to 170*e8d8bef9SDimitry Andric /// variables used in \a OutlinedFn function. 171*e8d8bef9SDimitry Andric /// \param IfCond Condition in the associated 'if' clause, if it was 172*e8d8bef9SDimitry Andric /// specified, nullptr otherwise. 173*e8d8bef9SDimitry Andric /// 174*e8d8bef9SDimitry Andric void emitSPMDParallelCall(CodeGenFunction &CGF, SourceLocation Loc, 175*e8d8bef9SDimitry Andric llvm::Function *OutlinedFn, 176*e8d8bef9SDimitry Andric ArrayRef<llvm::Value *> CapturedVars, 177*e8d8bef9SDimitry Andric const Expr *IfCond); 178*e8d8bef9SDimitry Andric 179*e8d8bef9SDimitry Andric protected: 180*e8d8bef9SDimitry Andric /// Get the function name of an outlined region. 181*e8d8bef9SDimitry Andric // The name can be customized depending on the target. 182*e8d8bef9SDimitry Andric // 183*e8d8bef9SDimitry Andric StringRef getOutlinedHelperName() const override { 184*e8d8bef9SDimitry Andric return "__omp_outlined__"; 185*e8d8bef9SDimitry Andric } 186*e8d8bef9SDimitry Andric 187*e8d8bef9SDimitry Andric /// Check if the default location must be constant. 188*e8d8bef9SDimitry Andric /// Constant for NVPTX for better optimization. 189*e8d8bef9SDimitry Andric bool isDefaultLocationConstant() const override { return true; } 190*e8d8bef9SDimitry Andric 191*e8d8bef9SDimitry Andric /// Returns additional flags that can be stored in reserved_2 field of the 192*e8d8bef9SDimitry Andric /// default location. 193*e8d8bef9SDimitry Andric /// For NVPTX target contains data about SPMD/Non-SPMD execution mode + 194*e8d8bef9SDimitry Andric /// Full/Lightweight runtime mode. Used for better optimization. 195*e8d8bef9SDimitry Andric unsigned getDefaultLocationReserved2Flags() const override; 196*e8d8bef9SDimitry Andric 197*e8d8bef9SDimitry Andric public: 198*e8d8bef9SDimitry Andric explicit CGOpenMPRuntimeGPU(CodeGenModule &CGM); 199*e8d8bef9SDimitry Andric void clear() override; 200*e8d8bef9SDimitry Andric 201*e8d8bef9SDimitry Andric /// Declare generalized virtual functions which need to be defined 202*e8d8bef9SDimitry Andric /// by all specializations of OpenMPGPURuntime Targets like AMDGCN 203*e8d8bef9SDimitry Andric /// and NVPTX. 204*e8d8bef9SDimitry Andric 205*e8d8bef9SDimitry Andric /// Get the GPU warp size. 206*e8d8bef9SDimitry Andric virtual llvm::Value *getGPUWarpSize(CodeGenFunction &CGF) = 0; 207*e8d8bef9SDimitry Andric 208*e8d8bef9SDimitry Andric /// Get the id of the current thread on the GPU. 209*e8d8bef9SDimitry Andric virtual llvm::Value *getGPUThreadID(CodeGenFunction &CGF) = 0; 210*e8d8bef9SDimitry Andric 211*e8d8bef9SDimitry Andric /// Get the maximum number of threads in a block of the GPU. 212*e8d8bef9SDimitry Andric virtual llvm::Value *getGPUNumThreads(CodeGenFunction &CGF) = 0; 213*e8d8bef9SDimitry Andric 214*e8d8bef9SDimitry Andric /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 215*e8d8bef9SDimitry Andric /// global_tid, int proc_bind) to generate code for 'proc_bind' clause. 216*e8d8bef9SDimitry Andric virtual void emitProcBindClause(CodeGenFunction &CGF, 217*e8d8bef9SDimitry Andric llvm::omp::ProcBindKind ProcBind, 218*e8d8bef9SDimitry Andric SourceLocation Loc) override; 219*e8d8bef9SDimitry Andric 220*e8d8bef9SDimitry Andric /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 221*e8d8bef9SDimitry Andric /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' 222*e8d8bef9SDimitry Andric /// clause. 223*e8d8bef9SDimitry Andric /// \param NumThreads An integer value of threads. 224*e8d8bef9SDimitry Andric virtual void emitNumThreadsClause(CodeGenFunction &CGF, 225*e8d8bef9SDimitry Andric llvm::Value *NumThreads, 226*e8d8bef9SDimitry Andric SourceLocation Loc) override; 227*e8d8bef9SDimitry Andric 228*e8d8bef9SDimitry Andric /// This function ought to emit, in the general case, a call to 229*e8d8bef9SDimitry Andric // the openmp runtime kmpc_push_num_teams. In NVPTX backend it is not needed 230*e8d8bef9SDimitry Andric // as these numbers are obtained through the PTX grid and block configuration. 231*e8d8bef9SDimitry Andric /// \param NumTeams An integer expression of teams. 232*e8d8bef9SDimitry Andric /// \param ThreadLimit An integer expression of threads. 233*e8d8bef9SDimitry Andric void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, 234*e8d8bef9SDimitry Andric const Expr *ThreadLimit, SourceLocation Loc) override; 235*e8d8bef9SDimitry Andric 236*e8d8bef9SDimitry Andric /// Emits inlined function for the specified OpenMP parallel 237*e8d8bef9SDimitry Andric // directive. 238*e8d8bef9SDimitry Andric /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, 239*e8d8bef9SDimitry Andric /// kmp_int32 BoundID, struct context_vars*). 240*e8d8bef9SDimitry Andric /// \param D OpenMP directive. 241*e8d8bef9SDimitry Andric /// \param ThreadIDVar Variable for thread id in the current OpenMP region. 242*e8d8bef9SDimitry Andric /// \param InnermostKind Kind of innermost directive (for simple directives it 243*e8d8bef9SDimitry Andric /// is a directive itself, for combined - its innermost directive). 244*e8d8bef9SDimitry Andric /// \param CodeGen Code generation sequence for the \a D directive. 245*e8d8bef9SDimitry Andric llvm::Function * 246*e8d8bef9SDimitry Andric emitParallelOutlinedFunction(const OMPExecutableDirective &D, 247*e8d8bef9SDimitry Andric const VarDecl *ThreadIDVar, 248*e8d8bef9SDimitry Andric OpenMPDirectiveKind InnermostKind, 249*e8d8bef9SDimitry Andric const RegionCodeGenTy &CodeGen) override; 250*e8d8bef9SDimitry Andric 251*e8d8bef9SDimitry Andric /// Emits inlined function for the specified OpenMP teams 252*e8d8bef9SDimitry Andric // directive. 253*e8d8bef9SDimitry Andric /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, 254*e8d8bef9SDimitry Andric /// kmp_int32 BoundID, struct context_vars*). 255*e8d8bef9SDimitry Andric /// \param D OpenMP directive. 256*e8d8bef9SDimitry Andric /// \param ThreadIDVar Variable for thread id in the current OpenMP region. 257*e8d8bef9SDimitry Andric /// \param InnermostKind Kind of innermost directive (for simple directives it 258*e8d8bef9SDimitry Andric /// is a directive itself, for combined - its innermost directive). 259*e8d8bef9SDimitry Andric /// \param CodeGen Code generation sequence for the \a D directive. 260*e8d8bef9SDimitry Andric llvm::Function * 261*e8d8bef9SDimitry Andric emitTeamsOutlinedFunction(const OMPExecutableDirective &D, 262*e8d8bef9SDimitry Andric const VarDecl *ThreadIDVar, 263*e8d8bef9SDimitry Andric OpenMPDirectiveKind InnermostKind, 264*e8d8bef9SDimitry Andric const RegionCodeGenTy &CodeGen) override; 265*e8d8bef9SDimitry Andric 266*e8d8bef9SDimitry Andric /// Emits code for teams call of the \a OutlinedFn with 267*e8d8bef9SDimitry Andric /// variables captured in a record which address is stored in \a 268*e8d8bef9SDimitry Andric /// CapturedStruct. 269*e8d8bef9SDimitry Andric /// \param OutlinedFn Outlined function to be run by team masters. Type of 270*e8d8bef9SDimitry Andric /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). 271*e8d8bef9SDimitry Andric /// \param CapturedVars A pointer to the record with the references to 272*e8d8bef9SDimitry Andric /// variables used in \a OutlinedFn function. 273*e8d8bef9SDimitry Andric /// 274*e8d8bef9SDimitry Andric void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, 275*e8d8bef9SDimitry Andric SourceLocation Loc, llvm::Function *OutlinedFn, 276*e8d8bef9SDimitry Andric ArrayRef<llvm::Value *> CapturedVars) override; 277*e8d8bef9SDimitry Andric 278*e8d8bef9SDimitry Andric /// Emits code for parallel or serial call of the \a OutlinedFn with 279*e8d8bef9SDimitry Andric /// variables captured in a record which address is stored in \a 280*e8d8bef9SDimitry Andric /// CapturedStruct. 281*e8d8bef9SDimitry Andric /// \param OutlinedFn Outlined function to be run in parallel threads. Type of 282*e8d8bef9SDimitry Andric /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). 283*e8d8bef9SDimitry Andric /// \param CapturedVars A pointer to the record with the references to 284*e8d8bef9SDimitry Andric /// variables used in \a OutlinedFn function. 285*e8d8bef9SDimitry Andric /// \param IfCond Condition in the associated 'if' clause, if it was 286*e8d8bef9SDimitry Andric /// specified, nullptr otherwise. 287*e8d8bef9SDimitry Andric void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, 288*e8d8bef9SDimitry Andric llvm::Function *OutlinedFn, 289*e8d8bef9SDimitry Andric ArrayRef<llvm::Value *> CapturedVars, 290*e8d8bef9SDimitry Andric const Expr *IfCond) override; 291*e8d8bef9SDimitry Andric 292*e8d8bef9SDimitry Andric /// Emit an implicit/explicit barrier for OpenMP threads. 293*e8d8bef9SDimitry Andric /// \param Kind Directive for which this implicit barrier call must be 294*e8d8bef9SDimitry Andric /// generated. Must be OMPD_barrier for explicit barrier generation. 295*e8d8bef9SDimitry Andric /// \param EmitChecks true if need to emit checks for cancellation barriers. 296*e8d8bef9SDimitry Andric /// \param ForceSimpleCall true simple barrier call must be emitted, false if 297*e8d8bef9SDimitry Andric /// runtime class decides which one to emit (simple or with cancellation 298*e8d8bef9SDimitry Andric /// checks). 299*e8d8bef9SDimitry Andric /// 300*e8d8bef9SDimitry Andric void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, 301*e8d8bef9SDimitry Andric OpenMPDirectiveKind Kind, bool EmitChecks = true, 302*e8d8bef9SDimitry Andric bool ForceSimpleCall = false) override; 303*e8d8bef9SDimitry Andric 304*e8d8bef9SDimitry Andric /// Emits a critical region. 305*e8d8bef9SDimitry Andric /// \param CriticalName Name of the critical region. 306*e8d8bef9SDimitry Andric /// \param CriticalOpGen Generator for the statement associated with the given 307*e8d8bef9SDimitry Andric /// critical region. 308*e8d8bef9SDimitry Andric /// \param Hint Value of the 'hint' clause (optional). 309*e8d8bef9SDimitry Andric void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName, 310*e8d8bef9SDimitry Andric const RegionCodeGenTy &CriticalOpGen, 311*e8d8bef9SDimitry Andric SourceLocation Loc, 312*e8d8bef9SDimitry Andric const Expr *Hint = nullptr) override; 313*e8d8bef9SDimitry Andric 314*e8d8bef9SDimitry Andric /// Emit a code for reduction clause. 315*e8d8bef9SDimitry Andric /// 316*e8d8bef9SDimitry Andric /// \param Privates List of private copies for original reduction arguments. 317*e8d8bef9SDimitry Andric /// \param LHSExprs List of LHS in \a ReductionOps reduction operations. 318*e8d8bef9SDimitry Andric /// \param RHSExprs List of RHS in \a ReductionOps reduction operations. 319*e8d8bef9SDimitry Andric /// \param ReductionOps List of reduction operations in form 'LHS binop RHS' 320*e8d8bef9SDimitry Andric /// or 'operator binop(LHS, RHS)'. 321*e8d8bef9SDimitry Andric /// \param Options List of options for reduction codegen: 322*e8d8bef9SDimitry Andric /// WithNowait true if parent directive has also nowait clause, false 323*e8d8bef9SDimitry Andric /// otherwise. 324*e8d8bef9SDimitry Andric /// SimpleReduction Emit reduction operation only. Used for omp simd 325*e8d8bef9SDimitry Andric /// directive on the host. 326*e8d8bef9SDimitry Andric /// ReductionKind The kind of reduction to perform. 327*e8d8bef9SDimitry Andric virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc, 328*e8d8bef9SDimitry Andric ArrayRef<const Expr *> Privates, 329*e8d8bef9SDimitry Andric ArrayRef<const Expr *> LHSExprs, 330*e8d8bef9SDimitry Andric ArrayRef<const Expr *> RHSExprs, 331*e8d8bef9SDimitry Andric ArrayRef<const Expr *> ReductionOps, 332*e8d8bef9SDimitry Andric ReductionOptionsTy Options) override; 333*e8d8bef9SDimitry Andric 334*e8d8bef9SDimitry Andric /// Returns specified OpenMP runtime function for the current OpenMP 335*e8d8bef9SDimitry Andric /// implementation. Specialized for the NVPTX device. 336*e8d8bef9SDimitry Andric /// \param Function OpenMP runtime function. 337*e8d8bef9SDimitry Andric /// \return Specified function. 338*e8d8bef9SDimitry Andric llvm::FunctionCallee createNVPTXRuntimeFunction(unsigned Function); 339*e8d8bef9SDimitry Andric 340*e8d8bef9SDimitry Andric /// Translates the native parameter of outlined function if this is required 341*e8d8bef9SDimitry Andric /// for target. 342*e8d8bef9SDimitry Andric /// \param FD Field decl from captured record for the parameter. 343*e8d8bef9SDimitry Andric /// \param NativeParam Parameter itself. 344*e8d8bef9SDimitry Andric const VarDecl *translateParameter(const FieldDecl *FD, 345*e8d8bef9SDimitry Andric const VarDecl *NativeParam) const override; 346*e8d8bef9SDimitry Andric 347*e8d8bef9SDimitry Andric /// Gets the address of the native argument basing on the address of the 348*e8d8bef9SDimitry Andric /// target-specific parameter. 349*e8d8bef9SDimitry Andric /// \param NativeParam Parameter itself. 350*e8d8bef9SDimitry Andric /// \param TargetParam Corresponding target-specific parameter. 351*e8d8bef9SDimitry Andric Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam, 352*e8d8bef9SDimitry Andric const VarDecl *TargetParam) const override; 353*e8d8bef9SDimitry Andric 354*e8d8bef9SDimitry Andric /// Emits call of the outlined function with the provided arguments, 355*e8d8bef9SDimitry Andric /// translating these arguments to correct target-specific arguments. 356*e8d8bef9SDimitry Andric void emitOutlinedFunctionCall( 357*e8d8bef9SDimitry Andric CodeGenFunction &CGF, SourceLocation Loc, llvm::FunctionCallee OutlinedFn, 358*e8d8bef9SDimitry Andric ArrayRef<llvm::Value *> Args = llvm::None) const override; 359*e8d8bef9SDimitry Andric 360*e8d8bef9SDimitry Andric /// Emits OpenMP-specific function prolog. 361*e8d8bef9SDimitry Andric /// Required for device constructs. 362*e8d8bef9SDimitry Andric void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) override; 363*e8d8bef9SDimitry Andric 364*e8d8bef9SDimitry Andric /// Gets the OpenMP-specific address of the local variable. 365*e8d8bef9SDimitry Andric Address getAddressOfLocalVariable(CodeGenFunction &CGF, 366*e8d8bef9SDimitry Andric const VarDecl *VD) override; 367*e8d8bef9SDimitry Andric 368*e8d8bef9SDimitry Andric /// Target codegen is specialized based on two data-sharing modes: CUDA, in 369*e8d8bef9SDimitry Andric /// which the local variables are actually global threadlocal, and Generic, in 370*e8d8bef9SDimitry Andric /// which the local variables are placed in global memory if they may escape 371*e8d8bef9SDimitry Andric /// their declaration context. 372*e8d8bef9SDimitry Andric enum DataSharingMode { 373*e8d8bef9SDimitry Andric /// CUDA data sharing mode. 374*e8d8bef9SDimitry Andric CUDA, 375*e8d8bef9SDimitry Andric /// Generic data-sharing mode. 376*e8d8bef9SDimitry Andric Generic, 377*e8d8bef9SDimitry Andric }; 378*e8d8bef9SDimitry Andric 379*e8d8bef9SDimitry Andric /// Cleans up references to the objects in finished function. 380*e8d8bef9SDimitry Andric /// 381*e8d8bef9SDimitry Andric void functionFinished(CodeGenFunction &CGF) override; 382*e8d8bef9SDimitry Andric 383*e8d8bef9SDimitry Andric /// Choose a default value for the dist_schedule clause. 384*e8d8bef9SDimitry Andric void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF, 385*e8d8bef9SDimitry Andric const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind, 386*e8d8bef9SDimitry Andric llvm::Value *&Chunk) const override; 387*e8d8bef9SDimitry Andric 388*e8d8bef9SDimitry Andric /// Choose a default value for the schedule clause. 389*e8d8bef9SDimitry Andric void getDefaultScheduleAndChunk(CodeGenFunction &CGF, 390*e8d8bef9SDimitry Andric const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind, 391*e8d8bef9SDimitry Andric const Expr *&ChunkExpr) const override; 392*e8d8bef9SDimitry Andric 393*e8d8bef9SDimitry Andric /// Adjust some parameters for the target-based directives, like addresses of 394*e8d8bef9SDimitry Andric /// the variables captured by reference in lambdas. 395*e8d8bef9SDimitry Andric void adjustTargetSpecificDataForLambdas( 396*e8d8bef9SDimitry Andric CodeGenFunction &CGF, const OMPExecutableDirective &D) const override; 397*e8d8bef9SDimitry Andric 398*e8d8bef9SDimitry Andric /// Perform check on requires decl to ensure that target architecture 399*e8d8bef9SDimitry Andric /// supports unified addressing 400*e8d8bef9SDimitry Andric void processRequiresDirective(const OMPRequiresDecl *D) override; 401*e8d8bef9SDimitry Andric 402*e8d8bef9SDimitry Andric /// Returns default address space for the constant firstprivates, __constant__ 403*e8d8bef9SDimitry Andric /// address space by default. 404*e8d8bef9SDimitry Andric unsigned getDefaultFirstprivateAddressSpace() const override; 405*e8d8bef9SDimitry Andric 406*e8d8bef9SDimitry Andric /// Checks if the variable has associated OMPAllocateDeclAttr attribute with 407*e8d8bef9SDimitry Andric /// the predefined allocator and translates it into the corresponding address 408*e8d8bef9SDimitry Andric /// space. 409*e8d8bef9SDimitry Andric bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS) override; 410*e8d8bef9SDimitry Andric 411*e8d8bef9SDimitry Andric private: 412*e8d8bef9SDimitry Andric /// Track the execution mode when codegening directives within a target 413*e8d8bef9SDimitry Andric /// region. The appropriate mode (SPMD/NON-SPMD) is set on entry to the 414*e8d8bef9SDimitry Andric /// target region and used by containing directives such as 'parallel' 415*e8d8bef9SDimitry Andric /// to emit optimized code. 416*e8d8bef9SDimitry Andric ExecutionMode CurrentExecutionMode = EM_Unknown; 417*e8d8bef9SDimitry Andric 418*e8d8bef9SDimitry Andric /// Check if the full runtime is required (default - yes). 419*e8d8bef9SDimitry Andric bool RequiresFullRuntime = true; 420*e8d8bef9SDimitry Andric 421*e8d8bef9SDimitry Andric /// true if we're emitting the code for the target region and next parallel 422*e8d8bef9SDimitry Andric /// region is L0 for sure. 423*e8d8bef9SDimitry Andric bool IsInTargetMasterThreadRegion = false; 424*e8d8bef9SDimitry Andric /// true if currently emitting code for target/teams/distribute region, false 425*e8d8bef9SDimitry Andric /// - otherwise. 426*e8d8bef9SDimitry Andric bool IsInTTDRegion = false; 427*e8d8bef9SDimitry Andric /// true if we're definitely in the parallel region. 428*e8d8bef9SDimitry Andric bool IsInParallelRegion = false; 429*e8d8bef9SDimitry Andric 430*e8d8bef9SDimitry Andric /// Map between an outlined function and its wrapper. 431*e8d8bef9SDimitry Andric llvm::DenseMap<llvm::Function *, llvm::Function *> WrapperFunctionsMap; 432*e8d8bef9SDimitry Andric 433*e8d8bef9SDimitry Andric /// Emit function which wraps the outline parallel region 434*e8d8bef9SDimitry Andric /// and controls the parameters which are passed to this function. 435*e8d8bef9SDimitry Andric /// The wrapper ensures that the outlined function is called 436*e8d8bef9SDimitry Andric /// with the correct arguments when data is shared. 437*e8d8bef9SDimitry Andric llvm::Function *createParallelDataSharingWrapper( 438*e8d8bef9SDimitry Andric llvm::Function *OutlinedParallelFn, const OMPExecutableDirective &D); 439*e8d8bef9SDimitry Andric 440*e8d8bef9SDimitry Andric /// The data for the single globalized variable. 441*e8d8bef9SDimitry Andric struct MappedVarData { 442*e8d8bef9SDimitry Andric /// Corresponding field in the global record. 443*e8d8bef9SDimitry Andric const FieldDecl *FD = nullptr; 444*e8d8bef9SDimitry Andric /// Corresponding address. 445*e8d8bef9SDimitry Andric Address PrivateAddr = Address::invalid(); 446*e8d8bef9SDimitry Andric /// true, if only one element is required (for latprivates in SPMD mode), 447*e8d8bef9SDimitry Andric /// false, if need to create based on the warp-size. 448*e8d8bef9SDimitry Andric bool IsOnePerTeam = false; 449*e8d8bef9SDimitry Andric MappedVarData() = delete; 450*e8d8bef9SDimitry Andric MappedVarData(const FieldDecl *FD, bool IsOnePerTeam = false) 451*e8d8bef9SDimitry Andric : FD(FD), IsOnePerTeam(IsOnePerTeam) {} 452*e8d8bef9SDimitry Andric }; 453*e8d8bef9SDimitry Andric /// The map of local variables to their addresses in the global memory. 454*e8d8bef9SDimitry Andric using DeclToAddrMapTy = llvm::MapVector<const Decl *, MappedVarData>; 455*e8d8bef9SDimitry Andric /// Set of the parameters passed by value escaping OpenMP context. 456*e8d8bef9SDimitry Andric using EscapedParamsTy = llvm::SmallPtrSet<const Decl *, 4>; 457*e8d8bef9SDimitry Andric struct FunctionData { 458*e8d8bef9SDimitry Andric DeclToAddrMapTy LocalVarData; 459*e8d8bef9SDimitry Andric llvm::Optional<DeclToAddrMapTy> SecondaryLocalVarData = llvm::None; 460*e8d8bef9SDimitry Andric EscapedParamsTy EscapedParameters; 461*e8d8bef9SDimitry Andric llvm::SmallVector<const ValueDecl*, 4> EscapedVariableLengthDecls; 462*e8d8bef9SDimitry Andric llvm::SmallVector<llvm::Value *, 4> EscapedVariableLengthDeclsAddrs; 463*e8d8bef9SDimitry Andric const RecordDecl *GlobalRecord = nullptr; 464*e8d8bef9SDimitry Andric llvm::Optional<const RecordDecl *> SecondaryGlobalRecord = llvm::None; 465*e8d8bef9SDimitry Andric llvm::Value *GlobalRecordAddr = nullptr; 466*e8d8bef9SDimitry Andric llvm::Value *IsInSPMDModeFlag = nullptr; 467*e8d8bef9SDimitry Andric std::unique_ptr<CodeGenFunction::OMPMapVars> MappedParams; 468*e8d8bef9SDimitry Andric }; 469*e8d8bef9SDimitry Andric /// Maps the function to the list of the globalized variables with their 470*e8d8bef9SDimitry Andric /// addresses. 471*e8d8bef9SDimitry Andric llvm::SmallDenseMap<llvm::Function *, FunctionData> FunctionGlobalizedDecls; 472*e8d8bef9SDimitry Andric /// List of records for the globalized variables in target/teams/distribute 473*e8d8bef9SDimitry Andric /// contexts. Inner records are going to be joined into the single record, 474*e8d8bef9SDimitry Andric /// while those resulting records are going to be joined into the single 475*e8d8bef9SDimitry Andric /// union. This resulting union (one per CU) is the entry point for the static 476*e8d8bef9SDimitry Andric /// memory management runtime functions. 477*e8d8bef9SDimitry Andric struct GlobalPtrSizeRecsTy { 478*e8d8bef9SDimitry Andric llvm::GlobalVariable *UseSharedMemory = nullptr; 479*e8d8bef9SDimitry Andric llvm::GlobalVariable *RecSize = nullptr; 480*e8d8bef9SDimitry Andric llvm::GlobalVariable *Buffer = nullptr; 481*e8d8bef9SDimitry Andric SourceLocation Loc; 482*e8d8bef9SDimitry Andric llvm::SmallVector<const RecordDecl *, 2> Records; 483*e8d8bef9SDimitry Andric unsigned RegionCounter = 0; 484*e8d8bef9SDimitry Andric }; 485*e8d8bef9SDimitry Andric llvm::SmallVector<GlobalPtrSizeRecsTy, 8> GlobalizedRecords; 486*e8d8bef9SDimitry Andric llvm::GlobalVariable *KernelTeamsReductionPtr = nullptr; 487*e8d8bef9SDimitry Andric /// List of the records with the list of fields for the reductions across the 488*e8d8bef9SDimitry Andric /// teams. Used to build the intermediate buffer for the fast teams 489*e8d8bef9SDimitry Andric /// reductions. 490*e8d8bef9SDimitry Andric /// All the records are gathered into a union `union.type` is created. 491*e8d8bef9SDimitry Andric llvm::SmallVector<const RecordDecl *, 4> TeamsReductions; 492*e8d8bef9SDimitry Andric /// Shared pointer for the global memory in the global memory buffer used for 493*e8d8bef9SDimitry Andric /// the given kernel. 494*e8d8bef9SDimitry Andric llvm::GlobalVariable *KernelStaticGlobalized = nullptr; 495*e8d8bef9SDimitry Andric /// Pair of the Non-SPMD team and all reductions variables in this team 496*e8d8bef9SDimitry Andric /// region. 497*e8d8bef9SDimitry Andric std::pair<const Decl *, llvm::SmallVector<const ValueDecl *, 4>> 498*e8d8bef9SDimitry Andric TeamAndReductions; 499*e8d8bef9SDimitry Andric }; 500*e8d8bef9SDimitry Andric 501*e8d8bef9SDimitry Andric } // CodeGen namespace. 502*e8d8bef9SDimitry Andric } // clang namespace. 503*e8d8bef9SDimitry Andric 504*e8d8bef9SDimitry Andric #endif // LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMEGPU_H 505