clang  8.0.0svn
CGOpenMPRuntimeNVPTX.h
Go to the documentation of this file.
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"
19 #include "CodeGenFunction.h"
20 #include "clang/AST/StmtOpenMP.h"
21 #include "llvm/IR/CallSite.h"
22 
23 namespace clang {
24 namespace CodeGen {
25 
27 public:
28  /// Defines the execution mode.
30  /// SPMD execution mode (all threads are worker threads).
32  /// Non-SPMD execution mode (1 master thread, others are workers).
34  /// Unknown execution mode (orphaned directive).
36  };
37 private:
38  /// Parallel outlined function work for workers to execute.
40 
41  struct EntryFunctionState {
42  llvm::BasicBlock *ExitBB = nullptr;
43  };
44 
45  class WorkerFunctionState {
46  public:
47  llvm::Function *WorkerFn;
48  const CGFunctionInfo &CGFI;
49  SourceLocation Loc;
50 
51  WorkerFunctionState(CodeGenModule &CGM, SourceLocation Loc);
52 
53  private:
54  void createWorkerFunction(CodeGenModule &CGM);
55  };
56 
57  ExecutionMode getExecutionMode() const;
58 
59  /// Emit the worker function for the current target region.
60  void emitWorkerFunction(WorkerFunctionState &WST);
61 
62  /// Helper for worker function. Emit body of worker loop.
63  void emitWorkerLoop(CodeGenFunction &CGF, WorkerFunctionState &WST);
64 
65  /// Helper for non-SPMD target entry function. Guide the master and
66  /// worker threads to their respective locations.
67  void emitNonSPMDEntryHeader(CodeGenFunction &CGF, EntryFunctionState &EST,
68  WorkerFunctionState &WST);
69 
70  /// Signal termination of OMP execution for non-SPMD target entry
71  /// function.
72  void emitNonSPMDEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST);
73 
74  /// Helper for generic variables globalization prolog.
75  void emitGenericVarsProlog(CodeGenFunction &CGF, SourceLocation Loc,
76  bool WithSPMDCheck = false);
77 
78  /// Helper for generic variables globalization epilog.
79  void emitGenericVarsEpilog(CodeGenFunction &CGF, bool WithSPMDCheck = false);
80 
81  /// Helper for SPMD mode target directive's entry function.
82  void emitSPMDEntryHeader(CodeGenFunction &CGF, EntryFunctionState &EST,
83  const OMPExecutableDirective &D);
84 
85  /// Signal termination of SPMD mode execution.
86  void emitSPMDEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST);
87 
88  //
89  // Base class overrides.
90  //
91 
92  /// Creates offloading entry for the provided entry ID \a ID,
93  /// address \a Addr, size \a Size, and flags \a Flags.
94  void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
95  uint64_t Size, int32_t Flags,
96  llvm::GlobalValue::LinkageTypes Linkage) override;
97 
98  /// Emit outlined function specialized for the Fork-Join
99  /// programming model for applicable target directives on the NVPTX device.
100  /// \param D Directive to emit.
101  /// \param ParentName Name of the function that encloses the target region.
102  /// \param OutlinedFn Outlined function value to be defined by this call.
103  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
104  /// \param IsOffloadEntry True if the outlined function is an offload entry.
105  /// An outlined function may not be an entry if, e.g. the if clause always
106  /// evaluates to false.
107  void emitNonSPMDKernel(const OMPExecutableDirective &D, StringRef ParentName,
108  llvm::Function *&OutlinedFn,
109  llvm::Constant *&OutlinedFnID, bool IsOffloadEntry,
110  const RegionCodeGenTy &CodeGen);
111 
112  /// Emit outlined function specialized for the Single Program
113  /// Multiple Data programming model for applicable target directives on the
114  /// NVPTX device.
115  /// \param D Directive to emit.
116  /// \param ParentName Name of the function that encloses the target region.
117  /// \param OutlinedFn Outlined function value to be defined by this call.
118  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
119  /// \param IsOffloadEntry True if the outlined function is an offload entry.
120  /// \param CodeGen Object containing the target statements.
121  /// An outlined function may not be an entry if, e.g. the if clause always
122  /// evaluates to false.
123  void emitSPMDKernel(const OMPExecutableDirective &D, StringRef ParentName,
124  llvm::Function *&OutlinedFn,
125  llvm::Constant *&OutlinedFnID, bool IsOffloadEntry,
126  const RegionCodeGenTy &CodeGen);
127 
128  /// Emit outlined function for 'target' directive on the NVPTX
129  /// device.
130  /// \param D Directive to emit.
131  /// \param ParentName Name of the function that encloses the target region.
132  /// \param OutlinedFn Outlined function value to be defined by this call.
133  /// \param OutlinedFnID Outlined function ID value to be defined by this call.
134  /// \param IsOffloadEntry True if the outlined function is an offload entry.
135  /// An outlined function may not be an entry if, e.g. the if clause always
136  /// evaluates to false.
138  StringRef ParentName,
139  llvm::Function *&OutlinedFn,
140  llvm::Constant *&OutlinedFnID,
141  bool IsOffloadEntry,
142  const RegionCodeGenTy &CodeGen) override;
143 
144  /// Emits code for parallel or serial call of the \a OutlinedFn with
145  /// variables captured in a record which address is stored in \a
146  /// CapturedStruct.
147  /// This call is for the Non-SPMD Execution Mode.
148  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
149  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
150  /// \param CapturedVars A pointer to the record with the references to
151  /// variables used in \a OutlinedFn function.
152  /// \param IfCond Condition in the associated 'if' clause, if it was
153  /// specified, nullptr otherwise.
154  void emitNonSPMDParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
155  llvm::Value *OutlinedFn,
156  ArrayRef<llvm::Value *> CapturedVars,
157  const Expr *IfCond);
158 
159  /// Emits code for parallel or serial call of the \a OutlinedFn with
160  /// variables captured in a record which address is stored in \a
161  /// CapturedStruct.
162  /// This call is for a parallel directive within an SPMD target directive.
163  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
164  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
165  /// \param CapturedVars A pointer to the record with the references to
166  /// variables used in \a OutlinedFn function.
167  /// \param IfCond Condition in the associated 'if' clause, if it was
168  /// specified, nullptr otherwise.
169  ///
170  void emitSPMDParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
171  llvm::Value *OutlinedFn,
172  ArrayRef<llvm::Value *> CapturedVars,
173  const Expr *IfCond);
174 
175 protected:
176  /// Get the function name of an outlined region.
177  // The name can be customized depending on the target.
178  //
179  StringRef getOutlinedHelperName() const override {
180  return "__omp_outlined__";
181  }
182 
183 public:
185 
186  /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
187  /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
188  virtual void emitProcBindClause(CodeGenFunction &CGF,
189  OpenMPProcBindClauseKind ProcBind,
190  SourceLocation Loc) override;
191 
192  /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
193  /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
194  /// clause.
195  /// \param NumThreads An integer value of threads.
196  virtual void emitNumThreadsClause(CodeGenFunction &CGF,
197  llvm::Value *NumThreads,
198  SourceLocation Loc) override;
199 
200  /// This function ought to emit, in the general case, a call to
201  // the openmp runtime kmpc_push_num_teams. In NVPTX backend it is not needed
202  // as these numbers are obtained through the PTX grid and block configuration.
203  /// \param NumTeams An integer expression of teams.
204  /// \param ThreadLimit An integer expression of threads.
205  void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
206  const Expr *ThreadLimit, SourceLocation Loc) override;
207 
208  /// Emits inlined function for the specified OpenMP parallel
209  // directive.
210  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
211  /// kmp_int32 BoundID, struct context_vars*).
212  /// \param D OpenMP directive.
213  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
214  /// \param InnermostKind Kind of innermost directive (for simple directives it
215  /// is a directive itself, for combined - its innermost directive).
216  /// \param CodeGen Code generation sequence for the \a D directive.
217  llvm::Value *
219  const VarDecl *ThreadIDVar,
220  OpenMPDirectiveKind InnermostKind,
221  const RegionCodeGenTy &CodeGen) override;
222 
223  /// Emits inlined function for the specified OpenMP teams
224  // directive.
225  /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
226  /// kmp_int32 BoundID, struct context_vars*).
227  /// \param D OpenMP directive.
228  /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
229  /// \param InnermostKind Kind of innermost directive (for simple directives it
230  /// is a directive itself, for combined - its innermost directive).
231  /// \param CodeGen Code generation sequence for the \a D directive.
232  llvm::Value *
234  const VarDecl *ThreadIDVar,
235  OpenMPDirectiveKind InnermostKind,
236  const RegionCodeGenTy &CodeGen) override;
237 
238  /// Emits code for teams call of the \a OutlinedFn with
239  /// variables captured in a record which address is stored in \a
240  /// CapturedStruct.
241  /// \param OutlinedFn Outlined function to be run by team masters. Type of
242  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
243  /// \param CapturedVars A pointer to the record with the references to
244  /// variables used in \a OutlinedFn function.
245  ///
247  SourceLocation Loc, llvm::Value *OutlinedFn,
248  ArrayRef<llvm::Value *> CapturedVars) override;
249 
250  /// Emits code for parallel or serial call of the \a OutlinedFn with
251  /// variables captured in a record which address is stored in \a
252  /// CapturedStruct.
253  /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
254  /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
255  /// \param CapturedVars A pointer to the record with the references to
256  /// variables used in \a OutlinedFn function.
257  /// \param IfCond Condition in the associated 'if' clause, if it was
258  /// specified, nullptr otherwise.
260  llvm::Value *OutlinedFn,
261  ArrayRef<llvm::Value *> CapturedVars,
262  const Expr *IfCond) override;
263 
264  /// Emits a critical region.
265  /// \param CriticalName Name of the critical region.
266  /// \param CriticalOpGen Generator for the statement associated with the given
267  /// critical region.
268  /// \param Hint Value of the 'hint' clause (optional).
269  void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
270  const RegionCodeGenTy &CriticalOpGen,
271  SourceLocation Loc,
272  const Expr *Hint = nullptr) override;
273 
274  /// Emit a code for reduction clause.
275  ///
276  /// \param Privates List of private copies for original reduction arguments.
277  /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
278  /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
279  /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
280  /// or 'operator binop(LHS, RHS)'.
281  /// \param Options List of options for reduction codegen:
282  /// WithNowait true if parent directive has also nowait clause, false
283  /// otherwise.
284  /// SimpleReduction Emit reduction operation only. Used for omp simd
285  /// directive on the host.
286  /// ReductionKind The kind of reduction to perform.
287  virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
289  ArrayRef<const Expr *> LHSExprs,
290  ArrayRef<const Expr *> RHSExprs,
291  ArrayRef<const Expr *> ReductionOps,
292  ReductionOptionsTy Options) override;
293 
294  /// Returns specified OpenMP runtime function for the current OpenMP
295  /// implementation. Specialized for the NVPTX device.
296  /// \param Function OpenMP runtime function.
297  /// \return Specified function.
298  llvm::Constant *createNVPTXRuntimeFunction(unsigned Function);
299 
300  /// Translates the native parameter of outlined function if this is required
301  /// for target.
302  /// \param FD Field decl from captured record for the parameter.
303  /// \param NativeParam Parameter itself.
304  const VarDecl *translateParameter(const FieldDecl *FD,
305  const VarDecl *NativeParam) const override;
306 
307  /// Gets the address of the native argument basing on the address of the
308  /// target-specific parameter.
309  /// \param NativeParam Parameter itself.
310  /// \param TargetParam Corresponding target-specific parameter.
311  Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
312  const VarDecl *TargetParam) const override;
313 
314  /// Emits call of the outlined function with the provided arguments,
315  /// translating these arguments to correct target-specific arguments.
317  CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
318  ArrayRef<llvm::Value *> Args = llvm::None) const override;
319 
320  /// Emits OpenMP-specific function prolog.
321  /// Required for device constructs.
322  void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) override;
323 
324  /// Gets the OpenMP-specific address of the local variable.
326  const VarDecl *VD) override;
327 
328  /// Target codegen is specialized based on two data-sharing modes: CUDA, in
329  /// which the local variables are actually global threadlocal, and Generic, in
330  /// which the local variables are placed in global memory if they may escape
331  /// their declaration context.
333  /// CUDA data sharing mode.
335  /// Generic data-sharing mode.
337  };
338 
339  /// Cleans up references to the objects in finished function.
340  ///
341  void functionFinished(CodeGenFunction &CGF) override;
342 
343 private:
344  /// Track the execution mode when codegening directives within a target
345  /// region. The appropriate mode (SPMD/NON-SPMD) is set on entry to the
346  /// target region and used by containing directives such as 'parallel'
347  /// to emit optimized code.
348  ExecutionMode CurrentExecutionMode = EM_Unknown;
349 
350  /// true if we're emitting the code for the target region and next parallel
351  /// region is L0 for sure.
352  bool IsInTargetMasterThreadRegion = false;
353  /// true if we're definitely in the parallel region.
354  bool IsInParallelRegion = false;
355 
356  /// Map between an outlined function and its wrapper.
357  llvm::DenseMap<llvm::Function *, llvm::Function *> WrapperFunctionsMap;
358 
359  /// Emit function which wraps the outline parallel region
360  /// and controls the parameters which are passed to this function.
361  /// The wrapper ensures that the outlined function is called
362  /// with the correct arguments when data is shared.
363  llvm::Function *createParallelDataSharingWrapper(
364  llvm::Function *OutlinedParallelFn, const OMPExecutableDirective &D);
365 
366  /// The map of local variables to their addresses in the global memory.
367  using DeclToAddrMapTy = llvm::MapVector<const Decl *,
368  std::pair<const FieldDecl *, Address>>;
369  /// Set of the parameters passed by value escaping OpenMP context.
370  using EscapedParamsTy = llvm::SmallPtrSet<const Decl *, 4>;
371  struct FunctionData {
372  DeclToAddrMapTy LocalVarData;
373  EscapedParamsTy EscapedParameters;
374  llvm::SmallVector<const ValueDecl*, 4> EscapedVariableLengthDecls;
375  llvm::SmallVector<llvm::Value *, 4> EscapedVariableLengthDeclsAddrs;
376  const RecordDecl *GlobalRecord = nullptr;
377  llvm::Value *GlobalRecordAddr = nullptr;
378  llvm::Value *IsInSPMDModeFlag = nullptr;
379  std::unique_ptr<CodeGenFunction::OMPMapVars> MappedParams;
380  };
381  /// Maps the function to the list of the globalized variables with their
382  /// addresses.
383  llvm::SmallDenseMap<llvm::Function *, FunctionData> FunctionGlobalizedDecls;
384 };
385 
386 } // CodeGen namespace.
387 } // clang namespace.
388 
389 #endif // LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H
llvm::Value * emitParallelOutlinedFunction(const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) override
Emits inlined function for the specified OpenMP parallel.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam, const VarDecl *TargetParam) const override
Gets the address of the native argument basing on the address of the target-specific parameter...
Represents a variable declaration or definition.
Definition: Decl.h:812
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:24
SPMD execution mode (all threads are worker threads).
Represents a struct/union/class.
Definition: Decl.h:3570
DataSharingMode
Target codegen is specialized based on two data-sharing modes: CUDA, in which the local variables are...
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Represents a member of a struct/union/class.
Definition: Decl.h:2556
void functionFinished(CodeGenFunction &CGF) override
Cleans up references to the objects in finished function.
virtual void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc) override
Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_threads)...
void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, const Expr *ThreadLimit, SourceLocation Loc) override
This function ought to emit, in the general case, a call to.
void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, ArrayRef< llvm::Value *> CapturedVars, const Expr *IfCond) override
Emits code for parallel or serial call of the OutlinedFn with variables captured in a record which ad...
Unknown execution mode (orphaned directive).
This represents one expression.
Definition: Expr.h:105
Address getAddressOfLocalVariable(CodeGenFunction &CGF, const VarDecl *VD) override
Gets the OpenMP-specific address of the local variable.
OpenMPProcBindClauseKind
OpenMP attributes for &#39;proc_bind&#39; clause.
Definition: OpenMPKinds.h:51
Non-SPMD execution mode (1 master thread, others are workers).
llvm::Value * emitTeamsOutlinedFunction(const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) override
Emits inlined function for the specified OpenMP teams.
void emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn, ArrayRef< llvm::Value *> Args=llvm::None) const override
Emits call of the outlined function with the provided arguments, translating these arguments to corre...
void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, SourceLocation Loc, llvm::Value *OutlinedFn, ArrayRef< llvm::Value *> CapturedVars) override
Emits code for teams call of the OutlinedFn with variables captured in a record which address is stor...
Encodes a location in the source.
void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName, const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc, const Expr *Hint=nullptr) override
Emits a critical region.
This is a basic class for representing single OpenMP executable directive.
Definition: StmtOpenMP.h:33
StringRef getOutlinedHelperName() const override
Get the function name of an outlined region.
OpenMPDirectiveKind
OpenMP directives.
Definition: OpenMPKinds.h:23
An aligned address.
Definition: Address.h:25
const VarDecl * translateParameter(const FieldDecl *FD, const VarDecl *NativeParam) const override
Translates the native parameter of outlined function if this is required for target.
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
Class provides a way to call simple version of codegen for OpenMP region, or an advanced with possibl...
virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D, StringRef ParentName, llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID, bool IsOffloadEntry, const RegionCodeGenTy &CodeGen)
Emit outilined function for &#39;target&#39; directive.
llvm::Constant * createNVPTXRuntimeFunction(unsigned Function)
Returns specified OpenMP runtime function for the current OpenMP implementation.
virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc, ArrayRef< const Expr *> Privates, ArrayRef< const Expr *> LHSExprs, ArrayRef< const Expr *> RHSExprs, ArrayRef< const Expr *> ReductionOps, ReductionOptionsTy Options) override
Emit a code for reduction clause.
This file defines OpenMP AST classes for executable directives and clauses.
ExecutionMode
Defines the execution mode.
void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) override
Emits OpenMP-specific function prolog.
Privates[]
Gets the list of initial values for linear variables.
Definition: OpenMPClause.h:151
virtual void emitProcBindClause(CodeGenFunction &CGF, OpenMPProcBindClauseKind ProcBind, SourceLocation Loc) override
Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid, int proc_bind) to generat...