1 //===----- CGOpenMPRuntimeNVPTX.h - Interface to OpenMP NVPTX Runtimes ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides a class for OpenMP runtime code generation specialized to NVPTX
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H
18 #include "CGOpenMPRuntime.h"
19 #include "CodeGenFunction.h"
20 #include "clang/AST/StmtOpenMP.h"
21 #include "llvm/IR/CallSite.h"
26 class CGOpenMPRuntimeNVPTX : public CGOpenMPRuntime {
28 // Parallel outlined function work for workers to execute.
29 llvm::SmallVector<llvm::Function *, 16> Work;
31 struct EntryFunctionState {
32 llvm::BasicBlock *ExitBB = nullptr;
35 class WorkerFunctionState {
37 llvm::Function *WorkerFn;
38 const CGFunctionInfo *CGFI;
40 WorkerFunctionState(CodeGenModule &CGM);
43 void createWorkerFunction(CodeGenModule &CGM);
46 /// \brief Emit the worker function for the current target region.
47 void emitWorkerFunction(WorkerFunctionState &WST);
49 /// \brief Helper for worker function. Emit body of worker loop.
50 void emitWorkerLoop(CodeGenFunction &CGF, WorkerFunctionState &WST);
52 /// \brief Helper for generic target entry function. Guide the master and
53 /// worker threads to their respective locations.
54 void emitGenericEntryHeader(CodeGenFunction &CGF, EntryFunctionState &EST,
55 WorkerFunctionState &WST);
57 /// \brief Signal termination of OMP execution for generic target entry
59 void emitGenericEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST);
61 /// \brief Returns specified OpenMP runtime function for the current OpenMP
62 /// implementation. Specialized for the NVPTX device.
63 /// \param Function OpenMP runtime function.
64 /// \return Specified function.
65 llvm::Constant *createNVPTXRuntimeFunction(unsigned Function);
68 // Base class overrides.
71 /// \brief Creates offloading entry for the provided entry ID \a ID,
72 /// address \a Addr, size \a Size, and flags \a Flags.
73 void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
74 uint64_t Size, int32_t Flags = 0) override;
76 /// \brief Emit outlined function specialized for the Fork-Join
77 /// programming model for applicable target directives on the NVPTX device.
78 /// \param D Directive to emit.
79 /// \param ParentName Name of the function that encloses the target region.
80 /// \param OutlinedFn Outlined function value to be defined by this call.
81 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
82 /// \param IsOffloadEntry True if the outlined function is an offload entry.
83 /// An outlined function may not be an entry if, e.g. the if clause always
84 /// evaluates to false.
85 void emitGenericKernel(const OMPExecutableDirective &D, StringRef ParentName,
86 llvm::Function *&OutlinedFn,
87 llvm::Constant *&OutlinedFnID, bool IsOffloadEntry,
88 const RegionCodeGenTy &CodeGen);
90 /// \brief Emit outlined function for 'target' directive on the NVPTX
92 /// \param D Directive to emit.
93 /// \param ParentName Name of the function that encloses the target region.
94 /// \param OutlinedFn Outlined function value to be defined by this call.
95 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
96 /// \param IsOffloadEntry True if the outlined function is an offload entry.
97 /// An outlined function may not be an entry if, e.g. the if clause always
98 /// evaluates to false.
99 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
100 StringRef ParentName,
101 llvm::Function *&OutlinedFn,
102 llvm::Constant *&OutlinedFnID,
104 const RegionCodeGenTy &CodeGen) override;
106 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
107 /// variables captured in a record which address is stored in \a
109 /// This call is for the Generic Execution Mode.
110 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
111 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
112 /// \param CapturedVars A pointer to the record with the references to
113 /// variables used in \a OutlinedFn function.
114 /// \param IfCond Condition in the associated 'if' clause, if it was
115 /// specified, nullptr otherwise.
116 void emitGenericParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
117 llvm::Value *OutlinedFn,
118 ArrayRef<llvm::Value *> CapturedVars,
122 /// \brief Get the function name of an outlined region.
123 // The name can be customized depending on the target.
125 StringRef getOutlinedHelperName() const override {
126 return "__omp_outlined__";
130 explicit CGOpenMPRuntimeNVPTX(CodeGenModule &CGM);
132 /// \brief This function ought to emit, in the general case, a call to
133 // the openmp runtime kmpc_push_num_teams. In NVPTX backend it is not needed
134 // as these numbers are obtained through the PTX grid and block configuration.
135 /// \param NumTeams An integer expression of teams.
136 /// \param ThreadLimit An integer expression of threads.
137 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
138 const Expr *ThreadLimit, SourceLocation Loc) override;
140 /// \brief Emits inlined function for the specified OpenMP parallel
141 // directive but an inlined function for teams.
142 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
143 /// kmp_int32 BoundID, struct context_vars*).
144 /// \param D OpenMP directive.
145 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
146 /// \param InnermostKind Kind of innermost directive (for simple directives it
147 /// is a directive itself, for combined - its innermost directive).
148 /// \param CodeGen Code generation sequence for the \a D directive.
150 emitParallelOrTeamsOutlinedFunction(const OMPExecutableDirective &D,
151 const VarDecl *ThreadIDVar,
152 OpenMPDirectiveKind InnermostKind,
153 const RegionCodeGenTy &CodeGen) override;
155 /// \brief Emits code for teams call of the \a OutlinedFn with
156 /// variables captured in a record which address is stored in \a
158 /// \param OutlinedFn Outlined function to be run by team masters. Type of
159 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
160 /// \param CapturedVars A pointer to the record with the references to
161 /// variables used in \a OutlinedFn function.
163 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
164 SourceLocation Loc, llvm::Value *OutlinedFn,
165 ArrayRef<llvm::Value *> CapturedVars) override;
167 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
168 /// variables captured in a record which address is stored in \a
170 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
171 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
172 /// \param CapturedVars A pointer to the record with the references to
173 /// variables used in \a OutlinedFn function.
174 /// \param IfCond Condition in the associated 'if' clause, if it was
175 /// specified, nullptr otherwise.
176 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
177 llvm::Value *OutlinedFn,
178 ArrayRef<llvm::Value *> CapturedVars,
179 const Expr *IfCond) override;
182 } // CodeGen namespace.
183 } // clang namespace.
185 #endif // LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H