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