blob: 4010b46a4cbd664ae1b3243152a9259b325e49f1 [file] [log] [blame]
Samuel Antao45bfe4c2016-02-08 15:59:20 +00001//===----- CGOpenMPRuntimeNVPTX.h - Interface to OpenMP NVPTX Runtimes ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a class for OpenMP runtime code generation specialized to NVPTX
11// targets.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H
16#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H
17
18#include "CGOpenMPRuntime.h"
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000019#include "CodeGenFunction.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "llvm/IR/CallSite.h"
Samuel Antao45bfe4c2016-02-08 15:59:20 +000022
23namespace clang {
24namespace CodeGen {
25
26class CGOpenMPRuntimeNVPTX : public CGOpenMPRuntime {
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +000027private:
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +000028 // Parallel outlined function work for workers to execute.
29 llvm::SmallVector<llvm::Function *, 16> Work;
30
Alexey Bataev5e87c342016-12-22 19:44:05 +000031 struct EntryFunctionState {
32 llvm::BasicBlock *ExitBB = nullptr;
Alexey Bataev14fa1c62016-03-29 05:34:15 +000033 };
34
35 class WorkerFunctionState {
36 public:
37 llvm::Function *WorkerFn;
38 const CGFunctionInfo *CGFI;
39
40 WorkerFunctionState(CodeGenModule &CGM);
41
42 private:
43 void createWorkerFunction(CodeGenModule &CGM);
44 };
45
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000046 /// \brief Emit the worker function for the current target region.
47 void emitWorkerFunction(WorkerFunctionState &WST);
48
49 /// \brief Helper for worker function. Emit body of worker loop.
50 void emitWorkerLoop(CodeGenFunction &CGF, WorkerFunctionState &WST);
51
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +000052 /// \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);
56
57 /// \brief Signal termination of OMP execution for generic target entry
58 /// function.
59 void emitGenericEntryFooter(CodeGenFunction &CGF, EntryFunctionState &EST);
60
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000061 /// \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);
66
67 //
68 // Base class overrides.
69 //
70
71 /// \brief Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +000072 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000073 void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Samuel Antaof83efdb2017-01-05 16:02:49 +000074 uint64_t Size, int32_t Flags = 0) override;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000075
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +000076 /// \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);
89
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000090 /// \brief Emit outlined function for 'target' directive on the NVPTX
91 /// device.
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,
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000103 bool IsOffloadEntry,
104 const RegionCodeGenTy &CodeGen) override;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000105
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000106 /// \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
108 /// CapturedStruct.
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,
119 const Expr *IfCond);
120
121protected:
122 /// \brief Get the function name of an outlined region.
123 // The name can be customized depending on the target.
124 //
125 StringRef getOutlinedHelperName() const override {
126 return "__omp_outlined__";
127 }
128
Samuel Antao45bfe4c2016-02-08 15:59:20 +0000129public:
130 explicit CGOpenMPRuntimeNVPTX(CodeGenModule &CGM);
Carlo Bertollic6872252016-04-04 15:55:02 +0000131
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;
139
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.
149 llvm::Value *
150 emitParallelOrTeamsOutlinedFunction(const OMPExecutableDirective &D,
151 const VarDecl *ThreadIDVar,
152 OpenMPDirectiveKind InnermostKind,
153 const RegionCodeGenTy &CodeGen) override;
154
155 /// \brief Emits code for teams call of the \a OutlinedFn with
156 /// variables captured in a record which address is stored in \a
157 /// CapturedStruct.
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.
162 ///
163 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
164 SourceLocation Loc, llvm::Value *OutlinedFn,
165 ArrayRef<llvm::Value *> CapturedVars) override;
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000166
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
169 /// CapturedStruct.
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;
Samuel Antao45bfe4c2016-02-08 15:59:20 +0000180};
181
182} // CodeGen namespace.
183} // clang namespace.
184
185#endif // LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIMENVPTX_H