blob: 599b3ba01fbe260814eb2f9b7e2b6d4000d2b51d [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
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.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
Alexey Bataev9959db52014-05-06 10:08:46 +000016
Alexey Bataev7292c292016-04-25 12:22:29 +000017#include "CGValue.h"
Alexey Bataev62b63b12015-03-10 07:28:44 +000018#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000019#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000020#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000021#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000022#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000023#include "llvm/ADT/StringMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000024#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000025#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000026
27namespace llvm {
28class ArrayType;
29class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000030class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000031class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000032class StructType;
33class Type;
34class Value;
35} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000036
Alexey Bataev9959db52014-05-06 10:08:46 +000037namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000038class Expr;
Samuel Antaoee8fb302016-01-06 13:42:12 +000039class GlobalDecl;
Alexey Bataev8b427062016-05-25 12:36:08 +000040class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000041class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000042class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000043class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000044class OMPDeclareReductionDecl;
45class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000046
Alexey Bataev9959db52014-05-06 10:08:46 +000047namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000048class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000049class CodeGenFunction;
50class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000051
Alexey Bataev14fa1c62016-03-29 05:34:15 +000052/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
53/// region.
54class PrePostActionTy {
55public:
56 explicit PrePostActionTy() {}
57 virtual void Enter(CodeGenFunction &CGF) {}
58 virtual void Exit(CodeGenFunction &CGF) {}
59 virtual ~PrePostActionTy() {}
60};
61
62/// Class provides a way to call simple version of codegen for OpenMP region, or
63/// an advanced with possible pre|post-actions in codegen.
64class RegionCodeGenTy final {
65 intptr_t CodeGen;
66 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
67 CodeGenTy Callback;
68 mutable PrePostActionTy *PrePostAction;
69 RegionCodeGenTy() = delete;
70 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
71 template <typename Callable>
72 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
73 PrePostActionTy &Action) {
74 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
75 }
76
77public:
78 template <typename Callable>
79 RegionCodeGenTy(
80 Callable &&CodeGen,
81 typename std::enable_if<
82 !std::is_same<typename std::remove_reference<Callable>::type,
83 RegionCodeGenTy>::value>::type * = nullptr)
84 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
85 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
86 PrePostAction(nullptr) {}
87 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
88 void operator()(CodeGenFunction &CGF) const;
89};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000090
Alexey Bataev24b5bae2016-04-28 09:23:51 +000091struct OMPTaskDataTy final {
92 SmallVector<const Expr *, 4> PrivateVars;
93 SmallVector<const Expr *, 4> PrivateCopies;
94 SmallVector<const Expr *, 4> FirstprivateVars;
95 SmallVector<const Expr *, 4> FirstprivateCopies;
96 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000097 SmallVector<const Expr *, 4> LastprivateVars;
98 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +000099 SmallVector<const Expr *, 4> ReductionVars;
100 SmallVector<const Expr *, 4> ReductionCopies;
101 SmallVector<const Expr *, 4> ReductionOps;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000102 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
103 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
104 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000105 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000106 llvm::Value *Reductions = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000107 unsigned NumberOfParts = 0;
108 bool Tied = true;
109 bool Nogroup = false;
110};
111
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000112/// Class intended to support codegen of all kind of the reduction clauses.
113class ReductionCodeGen {
114private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000115 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000116 struct ReductionData {
117 /// Reference to the original shared item.
118 const Expr *Ref = nullptr;
119 /// Helper expression for generation of private copy.
120 const Expr *Private = nullptr;
121 /// Helper expression for generation reduction operation.
122 const Expr *ReductionOp = nullptr;
123 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
124 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
125 };
126 /// List of reduction-based clauses.
127 SmallVector<ReductionData, 4> ClausesData;
128
129 /// List of addresses of original shared variables/expressions.
130 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
131 /// Sizes of the reduction items in chars.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000132 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000133 /// Base declarations for the reduction items.
134 SmallVector<const VarDecl *, 4> BaseDecls;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000135
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000136 /// Emits lvalue for shared expression.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000137 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
138 /// Emits upper bound for shared expression (if array section).
139 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
140 /// Performs aggregate initialization.
141 /// \param N Number of reduction item in the common list.
142 /// \param PrivateAddr Address of the corresponding private item.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000143 /// \param SharedLVal Address of the original shared variable.
144 /// \param DRD Declare reduction construct used for reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000145 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000146 Address PrivateAddr, LValue SharedLVal,
147 const OMPDeclareReductionDecl *DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000148
149public:
150 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
151 ArrayRef<const Expr *> Privates,
152 ArrayRef<const Expr *> ReductionOps);
153 /// Emits lvalue for a reduction item.
154 /// \param N Number of the reduction item.
155 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
156 /// Emits the code for the variable-modified type, if required.
157 /// \param N Number of the reduction item.
158 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
159 /// Emits the code for the variable-modified type, if required.
160 /// \param N Number of the reduction item.
161 /// \param Size Size of the type in chars.
162 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
163 /// Performs initialization of the private copy for the reduction item.
164 /// \param N Number of the reduction item.
165 /// \param PrivateAddr Address of the corresponding private item.
166 /// \param DefaultInit Default initialization sequence that should be
167 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000168 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000169 void
170 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
171 LValue SharedLVal,
172 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000173 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000174 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000175 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000176 /// \param N Number of the reduction item.
177 /// \param PrivateAddr Address of the corresponding private item.
178 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000179 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000180 /// address in normal operations.
181 /// \param N Number of the reduction item.
182 /// \param PrivateAddr Address of the corresponding private item.
183 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
184 Address PrivateAddr);
185 /// Returns LValue for the reduction item.
186 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000187 /// Returns the size of the reduction item (in chars and total number of
188 /// elements in the item), or nullptr, if the size is a constant.
189 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
190 return Sizes[N];
191 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000192 /// Returns the base declaration of the reduction item.
193 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
Alexey Bataev1c44e152018-03-06 18:59:43 +0000194 /// Returns the base declaration of the reduction item.
195 const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000196 /// Returns true if the initialization of the reduction item uses initializer
197 /// from declare reduction construct.
198 bool usesReductionInitializer(unsigned N) const;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000199};
200
Alexey Bataev9959db52014-05-06 10:08:46 +0000201class CGOpenMPRuntime {
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000202public:
203 /// Allows to disable automatic handling of functions used in target regions
204 /// as those marked as `omp declare target`.
205 class DisableAutoDeclareTargetRAII {
206 CodeGenModule &CGM;
207 bool SavedShouldMarkAsGlobal;
208
209 public:
210 DisableAutoDeclareTargetRAII(CodeGenModule &CGM);
211 ~DisableAutoDeclareTargetRAII();
212 };
213
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000214protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000215 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000216 StringRef FirstSeparator, Separator;
217
218 /// Constructor allowing to redefine the name separator for the variables.
219 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
220 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000221
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000222 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000223 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000224 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000225 uint64_t Size, int32_t Flags,
226 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000227
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000228 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000229 /// \param D Directive to emit.
230 /// \param ParentName Name of the function that encloses the target region.
231 /// \param OutlinedFn Outlined function value to be defined by this call.
232 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
233 /// \param IsOffloadEntry True if the outlined function is an offload entry.
234 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000235 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000236 /// evaluates to false.
237 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
238 StringRef ParentName,
239 llvm::Function *&OutlinedFn,
240 llvm::Constant *&OutlinedFnID,
241 bool IsOffloadEntry,
242 const RegionCodeGenTy &CodeGen);
243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000244 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000245 /// function. Here is the logic:
246 /// if (Cond) {
247 /// ThenGen();
248 /// } else {
249 /// ElseGen();
250 /// }
251 void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
252 const RegionCodeGenTy &ThenGen,
253 const RegionCodeGenTy &ElseGen);
254
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000255 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000256 /// \param Flags Flags for OpenMP location.
257 ///
258 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
259 unsigned Flags = 0);
260
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000261 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000262 llvm::Type *getIdentTyPointerTy();
263
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000264 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000265 ///
266 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
267
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000268 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000269 // The name can be customized depending on the target.
270 //
271 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
272
Alexey Bataev3c595a62017-08-14 15:01:03 +0000273 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000274 void emitCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *Callee,
275 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000276
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000277 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000278 /// stored.
279 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
280
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000281private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000282 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000283 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000284 llvm::Constant *DefaultOpenMPPSource = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000285 /// Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000286 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
287 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000288 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000289
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000290 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000291 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000292 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000293 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
294 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000296 /// Original representation is:
297 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000298 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000299 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000300 struct DebugLocThreadIdTy {
301 llvm::Value *DebugLoc;
302 llvm::Value *ThreadID;
303 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000304 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000305 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
306 OpenMPLocThreadIDMapTy;
307 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000308 /// Map of UDRs and corresponding combiner/initializer.
309 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
310 std::pair<llvm::Function *, llvm::Function *>>
311 UDRMapTy;
312 UDRMapTy UDRMap;
313 /// Map of functions and locally defined UDRs.
314 typedef llvm::DenseMap<llvm::Function *,
315 SmallVector<const OMPDeclareReductionDecl *, 4>>
316 FunctionUDRMapTy;
317 FunctionUDRMapTy FunctionUDRMap;
318 IdentifierInfo *In = nullptr;
319 IdentifierInfo *Out = nullptr;
320 IdentifierInfo *Priv = nullptr;
321 IdentifierInfo *Orig = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000322 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000323 /// kmp_critical_name[8];
324 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000325 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000326 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
327 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
328 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
329 /// variables.
330 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
331 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000332 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000333 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000334 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000335 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000336 /// void * shareds; /**< pointer to block of pointers to
337 /// shared vars */
338 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
339 /// executing task */
340 /// kmp_int32 part_id; /**< part id for the task */
341 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
342 /// deconstructors of firstprivate C++ objects */
343 /// } kmp_task_t;
344 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000345 /// Saved kmp_task_t for task directive.
346 QualType SavedKmpTaskTQTy;
347 /// Saved kmp_task_t for taskloop-based directive.
348 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000349 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000350 /// kmp_intptr_t base_addr;
351 /// size_t len;
352 /// struct {
353 /// bool in:1;
354 /// bool out:1;
355 /// } flags;
356 /// } kmp_depend_info_t;
357 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000358 /// struct kmp_dim { // loop bounds info casted to kmp_int64
359 /// kmp_int64 lo; // lower
360 /// kmp_int64 up; // upper
361 /// kmp_int64 st; // stride
362 /// };
363 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000364 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000365 /// void *addr; // Pointer to the offload entry info.
366 /// // (function or global)
367 /// char *name; // Name of the function or global.
368 /// size_t size; // Size of the entry info (0 if it a function).
369 /// };
370 QualType TgtOffloadEntryQTy;
371 /// struct __tgt_device_image{
372 /// void *ImageStart; // Pointer to the target code start.
373 /// void *ImageEnd; // Pointer to the target code end.
374 /// // We also add the host entries to the device image, as it may be useful
375 /// // for the target runtime to have access to that information.
376 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
377 /// // the entries.
378 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
379 /// // entries (non inclusive).
380 /// };
381 QualType TgtDeviceImageQTy;
382 /// struct __tgt_bin_desc{
383 /// int32_t NumDevices; // Number of devices supported.
384 /// __tgt_device_image *DeviceImages; // Arrays of device images
385 /// // (one per device).
386 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
387 /// // entries.
388 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
389 /// // entries (non inclusive).
390 /// };
391 QualType TgtBinaryDescriptorQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000392 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000393 /// far.
394 class OffloadEntriesInfoManagerTy {
395 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000396
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000397 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000398 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000399
400 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000401 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000402 class OffloadEntryInfo {
403 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000404 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000405 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000406 /// Entry is a target region.
407 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000408 /// Entry is a declare target variable.
409 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000410 /// Invalid entry info.
411 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000412 };
413
Alexey Bataev03f270c2018-03-30 18:31:07 +0000414 protected:
415 OffloadEntryInfo() = delete;
416 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000417 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000418 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000419 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000420 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000421
Alexey Bataev03f270c2018-03-30 18:31:07 +0000422 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000423 bool isValid() const { return Order != ~0u; }
424 unsigned getOrder() const { return Order; }
425 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000426 uint32_t getFlags() const { return Flags; }
427 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
428 llvm::Constant *getAddress() const {
429 return cast_or_null<llvm::Constant>(Addr);
430 }
431 void setAddress(llvm::Constant *V) {
432 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
433 Addr = V;
434 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000435 static bool classof(const OffloadEntryInfo *Info) { return true; }
436
Samuel Antaof83efdb2017-01-05 16:02:49 +0000437 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000438 /// Address of the entity that has to be mapped for offloading.
439 llvm::WeakTrackingVH Addr;
440
Samuel Antaof83efdb2017-01-05 16:02:49 +0000441 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000442 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000443
444 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000445 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000446
Alexey Bataev03f270c2018-03-30 18:31:07 +0000447 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000448 };
449
Alexey Bataev03f270c2018-03-30 18:31:07 +0000450 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000451 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000452 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000453 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000454 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000455
Alexey Bataev03f270c2018-03-30 18:31:07 +0000456 //
457 // Target region entries related.
458 //
459
460 /// Kind of the target registry entry.
461 enum OMPTargetRegionEntryKind : uint32_t {
462 /// Mark the entry as target region.
463 OMPTargetRegionEntryTargetRegion = 0x0,
464 /// Mark the entry as a global constructor.
465 OMPTargetRegionEntryCtor = 0x02,
466 /// Mark the entry as a global destructor.
467 OMPTargetRegionEntryDtor = 0x04,
468 };
469
470 /// Target region entries info.
471 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
472 /// Address that can be used as the ID of the entry.
473 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000474
475 public:
476 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000477 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000478 explicit OffloadEntryInfoTargetRegion(unsigned Order,
479 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000480 llvm::Constant *ID,
481 OMPTargetRegionEntryKind Flags)
482 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000483 ID(ID) {
484 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000485 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000486
487 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000488 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000489 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000490 ID = V;
491 }
492 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000493 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000494 }
495 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000496
497 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000498 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
499 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000500 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000501 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000502 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
503 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000504 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000505 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000506 /// Return true if a target region entry with the provided information
507 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000508 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000509 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000510 /// brief Applies action \a Action on all registered entries.
511 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000512 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000513 OffloadTargetRegionEntryInfoActTy;
514 void actOnTargetRegionEntriesInfo(
515 const OffloadTargetRegionEntryInfoActTy &Action);
516
Alexey Bataev03f270c2018-03-30 18:31:07 +0000517 //
518 // Device global variable entries related.
519 //
520
521 /// Kind of the global variable entry..
522 enum OMPTargetGlobalVarEntryKind : uint32_t {
523 /// Mark the entry as a to declare target.
524 OMPTargetGlobalVarEntryTo = 0x0,
525 };
526
527 /// Device global variable entries info.
528 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
529 /// Type of the global variable.
530 CharUnits VarSize;
531 llvm::GlobalValue::LinkageTypes Linkage;
532
533 public:
534 OffloadEntryInfoDeviceGlobalVar()
535 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
536 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
537 OMPTargetGlobalVarEntryKind Flags)
538 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
539 explicit OffloadEntryInfoDeviceGlobalVar(
540 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
541 OMPTargetGlobalVarEntryKind Flags,
542 llvm::GlobalValue::LinkageTypes Linkage)
543 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
544 VarSize(VarSize), Linkage(Linkage) {
545 setAddress(Addr);
546 }
547
548 CharUnits getVarSize() const { return VarSize; }
549 void setVarSize(CharUnits Size) { VarSize = Size; }
550 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
551 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
552 static bool classof(const OffloadEntryInfo *Info) {
553 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
554 }
555 };
556
557 /// Initialize device global variable entry.
558 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
559 OMPTargetGlobalVarEntryKind Flags,
560 unsigned Order);
561
562 /// Register device global variable entry.
563 void
564 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
565 CharUnits VarSize,
566 OMPTargetGlobalVarEntryKind Flags,
567 llvm::GlobalValue::LinkageTypes Linkage);
568 /// Checks if the variable with the given name has been registered already.
569 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
570 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
571 }
572 /// Applies action \a Action on all registered entries.
573 typedef llvm::function_ref<void(StringRef,
574 const OffloadEntryInfoDeviceGlobalVar &)>
575 OffloadDeviceGlobalVarEntryInfoActTy;
576 void actOnDeviceGlobalVarEntriesInfo(
577 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
578
Samuel Antaoee8fb302016-01-06 13:42:12 +0000579 private:
580 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000581 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000582 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000583 OffloadEntriesTargetRegionPerLine;
584 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
585 OffloadEntriesTargetRegionPerParentName;
586 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
587 OffloadEntriesTargetRegionPerFile;
588 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
589 OffloadEntriesTargetRegionPerDevice;
590 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
591 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000592 /// Storage for device global variable entries kind. The storage is to be
593 /// indexed by mangled name.
594 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
595 OffloadEntriesDeviceGlobalVarTy;
596 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000597 };
598 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
599
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000600 bool ShouldMarkAsGlobal = true;
601 llvm::SmallDenseSet<const FunctionDecl *> AlreadyEmittedTargetFunctions;
602
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000603 /// Creates and registers offloading binary descriptor for the current
Samuel Antaoee8fb302016-01-06 13:42:12 +0000604 /// compilation unit. The function that does the registration is returned.
605 llvm::Function *createOffloadingBinaryDescriptorRegistration();
606
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000607 /// Creates all the offload entries in the current compilation unit
Samuel Antaoee8fb302016-01-06 13:42:12 +0000608 /// along with the associated metadata.
609 void createOffloadEntriesAndInfoMetadata();
610
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000611 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000612 /// metadata.
613 void loadOffloadInfoMetadata();
614
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000615 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000616 QualType getTgtOffloadEntryQTy();
617
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000618 /// Returns __tgt_device_image type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000619 QualType getTgtDeviceImageQTy();
620
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000621 /// Returns __tgt_bin_desc type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000622 QualType getTgtBinaryDescriptorQTy();
623
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000624 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000625 /// found along the way.
626 /// \param S Starting statement.
627 /// \param ParentName Name of the function declaration that is being scanned.
628 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000629
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000630 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000631 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000632
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000633 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000634 llvm::Type *getKmpc_MicroPointerTy();
635
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000636 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000637 /// \param Function OpenMP runtime function.
638 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000639 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000640
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000641 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000642 /// size \a IVSize and sign \a IVSigned.
643 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
644
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000645 /// Returns __kmpc_dispatch_init_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000646 /// size \a IVSize and sign \a IVSigned.
647 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
648
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000649 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000650 /// size \a IVSize and sign \a IVSigned.
651 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
652
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000653 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000654 /// size \a IVSize and sign \a IVSigned.
655 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
656
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000657 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000658 /// return threadprivate cache object. This object is a pointer's worth of
659 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000660 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000661 /// \return Cache variable for the specified threadprivate.
662 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
663
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000664 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000665 /// internal global variable with the specified Name. The created variable has
666 /// linkage CommonLinkage by default and is initialized by null value.
667 /// \param Ty Type of the global variable. If it is exist already the type
668 /// must be the same.
669 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000670 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000671 const llvm::Twine &Name);
672
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000673 /// Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000674 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000675
Alexey Bataev34f8a702018-03-28 14:28:54 +0000676 /// Set of declare target variables with the generated initializer.
677 llvm::SmallPtrSet<const VarDecl *, 4> DeclareTargetWithDefinition;
678
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000679 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000680 /// \param VDAddr Address of the global variable \a VD.
681 /// \param Ctor Pointer to a global init function for \a VD.
682 /// \param CopyCtor Pointer to a global copy function for \a VD.
683 /// \param Dtor Pointer to a global destructor function for \a VD.
684 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000685 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000686 llvm::Value *Ctor, llvm::Value *CopyCtor,
687 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000688
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000689 /// Returns corresponding lock object for the specified critical region
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000690 /// name. If the lock object does not exist it is created, otherwise the
691 /// reference to the existing copy is returned.
692 /// \param CriticalName Name of the critical region.
693 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000694 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000695
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000696 struct TaskResultTy {
697 llvm::Value *NewTask = nullptr;
698 llvm::Value *TaskEntry = nullptr;
699 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000700 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000701 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000702 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000703 };
704 /// Emit task region for the task directive. The task region is emitted in
705 /// several steps:
706 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
707 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
708 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
709 /// function:
710 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
711 /// TaskFunction(gtid, tt->part_id, tt->shareds);
712 /// return 0;
713 /// }
714 /// 2. Copy a list of shared variables to field shareds of the resulting
715 /// structure kmp_task_t returned by the previous call (if any).
716 /// 3. Copy a pointer to destructions function to field destructions of the
717 /// resulting structure kmp_task_t.
718 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000719 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
720 /// /*part_id*/, captured_struct */*__context*/);
721 /// \param SharedsTy A type which contains references the shared variables.
722 /// \param Shareds Context with the list of shared variables from the \p
723 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000724 /// \param Data Additional data for task generation like tiednsee, final
725 /// state, list of privates etc.
726 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
727 const OMPExecutableDirective &D,
728 llvm::Value *TaskFunction, QualType SharedsTy,
729 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000730
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000731public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000732 explicit CGOpenMPRuntime(CodeGenModule &CGM)
733 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000734 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000735 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000736
Alexey Bataev18fa2322018-05-02 14:20:50 +0000737 /// Get the platform-specific name separator.
738 std::string getName(ArrayRef<StringRef> Parts) const;
739
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000740 /// Emit code for the specified user defined reduction construct.
741 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
742 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000743 /// Get combiner/initializer for the specified user-defined reduction, if any.
744 virtual std::pair<llvm::Function *, llvm::Function *>
745 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000746
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000747 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000748 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
749 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000750 /// \param D OpenMP directive.
751 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000752 /// \param InnermostKind Kind of innermost directive (for simple directives it
753 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000754 /// \param CodeGen Code generation sequence for the \a D directive.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000755 virtual llvm::Value *emitParallelOutlinedFunction(
756 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
757 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
758
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000759 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000760 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
761 /// kmp_int32 BoundID, struct context_vars*).
762 /// \param D OpenMP directive.
763 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
764 /// \param InnermostKind Kind of innermost directive (for simple directives it
765 /// is a directive itself, for combined - its innermost directive).
766 /// \param CodeGen Code generation sequence for the \a D directive.
767 virtual llvm::Value *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000768 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
769 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000770
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000771 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000772 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
773 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000774 /// \param D OpenMP directive.
775 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000776 /// \param PartIDVar Variable for partition id in the current OpenMP untied
777 /// task region.
778 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000779 /// \param InnermostKind Kind of innermost directive (for simple directives it
780 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000781 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000782 /// \param Tied true if task is generated for tied task, false otherwise.
783 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
784 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000785 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000786 virtual llvm::Value *emitTaskOutlinedFunction(
787 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000788 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
789 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
790 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000791
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000792 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000793 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000794 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000795
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000796 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000797 /// variables captured in a record which address is stored in \a
798 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000799 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000800 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000801 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000802 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000803 /// \param IfCond Condition in the associated 'if' clause, if it was
804 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000805 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000806 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
807 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000808 ArrayRef<llvm::Value *> CapturedVars,
809 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000810
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000811 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000812 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000813 /// \param CriticalOpGen Generator for the statement associated with the given
814 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000815 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000816 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000817 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000818 SourceLocation Loc,
819 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000820
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000821 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000822 /// \param MasterOpGen Generator for the statement associated with the given
823 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000824 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000825 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000826 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000827
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000828 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000829 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000830
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000831 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000832 /// \param TaskgroupOpGen Generator for the statement associated with the
833 /// given taskgroup region.
834 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
835 const RegionCodeGenTy &TaskgroupOpGen,
836 SourceLocation Loc);
837
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000838 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000839 /// \param SingleOpGen Generator for the statement associated with the given
840 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000841 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000842 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000843 SourceLocation Loc,
844 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000845 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000846 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000847 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000848
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000849 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000850 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000851 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000852 virtual void emitOrderedRegion(CodeGenFunction &CGF,
853 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000854 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000855
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000856 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +0000857 /// \param Kind Directive for which this implicit barrier call must be
858 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000859 /// \param EmitChecks true if need to emit checks for cancellation barriers.
860 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
861 /// runtime class decides which one to emit (simple or with cancellation
862 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000863 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000864 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000865 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000866 bool EmitChecks = true,
867 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000868
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000869 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +0000870 /// This kind of worksharing directive is emitted without outer loop.
871 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
872 /// \param Chunked True if chunk is specified in the clause.
873 ///
874 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
875 bool Chunked) const;
876
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000877 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000878 /// This kind of distribute directive is emitted without outer loop.
879 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
880 /// \param Chunked True if chunk is specified in the clause.
881 ///
882 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
883 bool Chunked) const;
884
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000885 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000886 /// This kind of worksharing directive is emitted without outer loop.
887 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
888 ///
889 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
890
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000891 /// struct with the values to be passed to the dispatch runtime function
892 struct DispatchRTInput {
893 /// Loop lower bound
894 llvm::Value *LB = nullptr;
895 /// Loop upper bound
896 llvm::Value *UB = nullptr;
897 /// Chunk size specified using 'schedule' clause (nullptr if chunk
898 /// was not specified)
899 llvm::Value *Chunk = nullptr;
900 DispatchRTInput() = default;
901 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
902 : LB(LB), UB(UB), Chunk(Chunk) {}
903 };
904
905 /// Call the appropriate runtime routine to initialize it before start
906 /// of loop.
907
908 /// This is used for non static scheduled types and when the ordered
909 /// clause is present on the loop construct.
910 /// Depending on the loop schedule, it is necessary to call some runtime
911 /// routine before start of the OpenMP loop to get the loop upper / lower
912 /// bounds \a LB and \a UB and stride \a ST.
913 ///
914 /// \param CGF Reference to current CodeGenFunction.
915 /// \param Loc Clang source location.
916 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
917 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000918 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000919 /// \param Ordered true if loop is ordered, false otherwise.
920 /// \param DispatchValues struct containing llvm values for lower bound, upper
921 /// bound, and chunk expression.
922 /// For the default (nullptr) value, the chunk 1 will be used.
923 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000924 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000925 const OpenMPScheduleTy &ScheduleKind,
926 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000927 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000928
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000929 /// Struct with the values to be passed to the static runtime function
930 struct StaticRTInput {
931 /// Size of the iteration variable in bits.
932 unsigned IVSize = 0;
933 /// Sign of the iteration variable.
934 bool IVSigned = false;
935 /// true if loop is ordered, false otherwise.
936 bool Ordered = false;
937 /// Address of the output variable in which the flag of the last iteration
938 /// is returned.
939 Address IL = Address::invalid();
940 /// Address of the output variable in which the lower iteration number is
941 /// returned.
942 Address LB = Address::invalid();
943 /// Address of the output variable in which the upper iteration number is
944 /// returned.
945 Address UB = Address::invalid();
946 /// Address of the output variable in which the stride value is returned
947 /// necessary to generated the static_chunked scheduled loop.
948 Address ST = Address::invalid();
949 /// Value of the chunk for the static_chunked scheduled loop. For the
950 /// default (nullptr) value, the chunk 1 will be used.
951 llvm::Value *Chunk = nullptr;
952 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
953 Address LB, Address UB, Address ST,
954 llvm::Value *Chunk = nullptr)
955 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
956 UB(UB), ST(ST), Chunk(Chunk) {}
957 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000958 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +0000959 /// of loop.
960 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000961 /// This is used only in case of static schedule, when the user did not
962 /// specify a ordered clause on the loop construct.
963 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +0000964 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000965 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +0000966 ///
967 /// \param CGF Reference to current CodeGenFunction.
968 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000969 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000970 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000971 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +0000972 ///
John McCall7f416cc2015-09-08 08:05:57 +0000973 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000974 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000975 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000976 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +0000977
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000978 ///
979 /// \param CGF Reference to current CodeGenFunction.
980 /// \param Loc Clang source location.
981 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000982 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000983 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000984 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
985 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000986 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000987 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000988
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000989 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000990 /// iteration of the ordered loop with the dynamic scheduling.
991 ///
992 /// \param CGF Reference to current CodeGenFunction.
993 /// \param Loc Clang source location.
994 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000995 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000996 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000997 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
998 SourceLocation Loc, unsigned IVSize,
999 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001000
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001001 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001002 /// all the work with current loop.
1003 ///
1004 /// \param CGF Reference to current CodeGenFunction.
1005 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001006 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001007 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001008 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1009 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001010
Alexander Musman92bdaab2015-03-12 13:37:50 +00001011 /// Call __kmpc_dispatch_next(
1012 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1013 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1014 /// kmp_int[32|64] *p_stride);
1015 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001016 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001017 /// \param IL Address of the output variable in which the flag of the
1018 /// last iteration is returned.
1019 /// \param LB Address of the output variable in which the lower iteration
1020 /// number is returned.
1021 /// \param UB Address of the output variable in which the upper iteration
1022 /// number is returned.
1023 /// \param ST Address of the output variable in which the stride value is
1024 /// returned.
1025 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1026 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001027 Address IL, Address LB,
1028 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001029
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001030 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001031 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1032 /// clause.
1033 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001034 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1035 llvm::Value *NumThreads,
1036 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001037
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001038 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001039 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1040 virtual void emitProcBindClause(CodeGenFunction &CGF,
1041 OpenMPProcBindClauseKind ProcBind,
1042 SourceLocation Loc);
1043
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001044 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001045 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001046 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001047 /// \param VDAddr Address of the global variable \a VD.
1048 /// \param Loc Location of the reference to threadprivate var.
1049 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001050 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1051 const VarDecl *VD,
1052 Address VDAddr,
1053 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001054
Alexey Bataev92327c52018-03-26 16:40:55 +00001055 /// Returns the address of the variable marked as declare target with link
1056 /// clause.
Alexey Bataev03f270c2018-03-30 18:31:07 +00001057 virtual Address getAddrOfDeclareTargetLink(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001058
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001059 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001060 /// a call to runtime library which adds initial value to the newly created
1061 /// threadprivate variable (if it is not constant) and registers destructor
1062 /// for the variable (if any).
1063 /// \param VD Threadprivate variable.
1064 /// \param VDAddr Address of the global variable \a VD.
1065 /// \param Loc Location of threadprivate declaration.
1066 /// \param PerformInit true if initialization expression is not constant.
1067 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001068 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001069 SourceLocation Loc, bool PerformInit,
1070 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001071
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001072 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001073 /// \param VD Declare target variable.
1074 /// \param Addr Address of the global variable \a VD.
1075 /// \param PerformInit true if initialization expression is not constant.
1076 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1077 llvm::GlobalVariable *Addr,
1078 bool PerformInit);
1079
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001080 /// Creates artificial threadprivate variable with name \p Name and type \p
1081 /// VarType.
1082 /// \param VarType Type of the artificial threadprivate variable.
1083 /// \param Name Name of the artificial threadprivate variable.
1084 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1085 QualType VarType,
1086 StringRef Name);
1087
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001088 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001089 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001090 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1091 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001092
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001093 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001094 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001095 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1096 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1097 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1098 /// function:
1099 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1100 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1101 /// return 0;
1102 /// }
1103 /// 2. Copy a list of shared variables to field shareds of the resulting
1104 /// structure kmp_task_t returned by the previous call (if any).
1105 /// 3. Copy a pointer to destructions function to field destructions of the
1106 /// resulting structure kmp_task_t.
1107 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1108 /// kmp_task_t *new_task), where new_task is a resulting structure from
1109 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001110 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001111 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1112 /// /*part_id*/, captured_struct */*__context*/);
1113 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001114 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001115 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001116 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1117 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001118 /// \param Data Additional data for task generation like tiednsee, final
1119 /// state, list of privates etc.
1120 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1121 const OMPExecutableDirective &D,
1122 llvm::Value *TaskFunction, QualType SharedsTy,
1123 Address Shareds, const Expr *IfCond,
1124 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001125
Alexey Bataev7292c292016-04-25 12:22:29 +00001126 /// Emit task region for the taskloop directive. The taskloop region is
1127 /// emitted in several steps:
1128 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1129 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1130 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1131 /// function:
1132 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1133 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1134 /// return 0;
1135 /// }
1136 /// 2. Copy a list of shared variables to field shareds of the resulting
1137 /// structure kmp_task_t returned by the previous call (if any).
1138 /// 3. Copy a pointer to destructions function to field destructions of the
1139 /// resulting structure kmp_task_t.
1140 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1141 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1142 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1143 /// is a resulting structure from
1144 /// previous items.
1145 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001146 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1147 /// /*part_id*/, captured_struct */*__context*/);
1148 /// \param SharedsTy A type which contains references the shared variables.
1149 /// \param Shareds Context with the list of shared variables from the \p
1150 /// TaskFunction.
1151 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1152 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001153 /// \param Data Additional data for task generation like tiednsee, final
1154 /// state, list of privates etc.
Alexey Bataev7292c292016-04-25 12:22:29 +00001155 virtual void emitTaskLoopCall(
1156 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001157 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
1158 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001159
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001160 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001161 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001162 /// \param InnermostKind Kind of innermost directive (for simple directives it
1163 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001164 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001165 /// \param HasCancel true if region has inner cancel directive, false
1166 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001167 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001168 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001169 const RegionCodeGenTy &CodeGen,
1170 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001171
1172 /// Emits reduction function.
1173 /// \param ArgsType Array type containing pointers to reduction variables.
1174 /// \param Privates List of private copies for original reduction arguments.
1175 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1176 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1177 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1178 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00001179 llvm::Value *emitReductionFunction(CodeGenModule &CGM, SourceLocation Loc,
1180 llvm::Type *ArgsType,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001181 ArrayRef<const Expr *> Privates,
1182 ArrayRef<const Expr *> LHSExprs,
1183 ArrayRef<const Expr *> RHSExprs,
1184 ArrayRef<const Expr *> ReductionOps);
1185
1186 /// Emits single reduction combiner
1187 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1188 const Expr *ReductionOp,
1189 const Expr *PrivateRef,
1190 const DeclRefExpr *LHS,
1191 const DeclRefExpr *RHS);
1192
1193 struct ReductionOptionsTy {
1194 bool WithNowait;
1195 bool SimpleReduction;
1196 OpenMPDirectiveKind ReductionKind;
1197 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001198 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001199 /// reduction:
1200 /// \code
1201 ///
1202 /// static kmp_critical_name lock = { 0 };
1203 ///
1204 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1205 /// ...
1206 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1207 /// ...
1208 /// }
1209 ///
1210 /// ...
1211 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1212 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1213 /// RedList, reduce_func, &<lock>)) {
1214 /// case 1:
1215 /// ...
1216 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1217 /// ...
1218 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1219 /// break;
1220 /// case 2:
1221 /// ...
1222 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1223 /// ...
1224 /// break;
1225 /// default:;
1226 /// }
1227 /// \endcode
1228 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001229 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001230 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1231 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1232 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1233 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001234 /// \param Options List of options for reduction codegen:
1235 /// WithNowait true if parent directive has also nowait clause, false
1236 /// otherwise.
1237 /// SimpleReduction Emit reduction operation only. Used for omp simd
1238 /// directive on the host.
1239 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001240 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001241 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001242 ArrayRef<const Expr *> LHSExprs,
1243 ArrayRef<const Expr *> RHSExprs,
1244 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001245 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001246
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001247 /// Emit a code for initialization of task reduction clause. Next code
1248 /// should be emitted for reduction:
1249 /// \code
1250 ///
1251 /// _task_red_item_t red_data[n];
1252 /// ...
1253 /// red_data[i].shar = &origs[i];
1254 /// red_data[i].size = sizeof(origs[i]);
1255 /// red_data[i].f_init = (void*)RedInit<i>;
1256 /// red_data[i].f_fini = (void*)RedDest<i>;
1257 /// red_data[i].f_comb = (void*)RedOp<i>;
1258 /// red_data[i].flags = <Flag_i>;
1259 /// ...
1260 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1261 /// \endcode
1262 ///
1263 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1264 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1265 /// \param Data Additional data for task generation like tiedness, final
1266 /// state, list of privates, reductions etc.
1267 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1268 SourceLocation Loc,
1269 ArrayRef<const Expr *> LHSExprs,
1270 ArrayRef<const Expr *> RHSExprs,
1271 const OMPTaskDataTy &Data);
1272
1273 /// Required to resolve existing problems in the runtime. Emits threadprivate
1274 /// variables to store the size of the VLAs/array sections for
1275 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1276 /// store the pointer to the original reduction item for the custom
1277 /// initializer defined by declare reduction construct.
1278 /// \param RCG Allows to reuse an existing data for the reductions.
1279 /// \param N Reduction item for which fixups must be emitted.
1280 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1281 ReductionCodeGen &RCG, unsigned N);
1282
1283 /// Get the address of `void *` type of the privatue copy of the reduction
1284 /// item specified by the \p SharedLVal.
1285 /// \param ReductionsPtr Pointer to the reduction data returned by the
1286 /// emitTaskReductionInit function.
1287 /// \param SharedLVal Address of the original reduction item.
1288 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1289 llvm::Value *ReductionsPtr,
1290 LValue SharedLVal);
1291
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001292 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001293 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001294
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001295 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001296 /// \param CancelRegion Region kind for which the cancellation point must be
1297 /// emitted.
1298 ///
1299 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1300 SourceLocation Loc,
1301 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001302
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001303 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001304 /// \param IfCond Condition in the associated 'if' clause, if it was
1305 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001306 /// \param CancelRegion Region kind for which the cancel must be emitted.
1307 ///
1308 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001309 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001310 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001311
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001312 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001313 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001314 /// \param ParentName Name of the function that encloses the target region.
1315 /// \param OutlinedFn Outlined function value to be defined by this call.
1316 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1317 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001318 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001319 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001320 /// evaluates to false.
1321 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1322 StringRef ParentName,
1323 llvm::Function *&OutlinedFn,
1324 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001325 bool IsOffloadEntry,
1326 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001327
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001328 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001329 /// code attempts offloading the execution to the device, an the event of
1330 /// a failure it executes the host version outlined in \a OutlinedFn.
1331 /// \param D Directive to emit.
1332 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001333 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001334 /// \param IfCond Expression evaluated in if clause associated with the target
1335 /// directive, or null if no if clause is used.
1336 /// \param Device Expression evaluated in device clause associated with the
1337 /// target directive, or null if no device clause is used.
Samuel Antaobed3c462015-10-02 16:14:20 +00001338 virtual void emitTargetCall(CodeGenFunction &CGF,
1339 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +00001340 llvm::Value *OutlinedFn,
1341 llvm::Value *OutlinedFnID, const Expr *IfCond,
Alexey Bataev8451efa2018-01-15 19:06:12 +00001342 const Expr *Device);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001343
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001344 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001345 /// the function itself in case it is a valid device function. Returns true if
1346 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001347 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001348 virtual bool emitTargetFunctions(GlobalDecl GD);
1349
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001350 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001351 /// Returns true if \a GD was dealt with successfully.
1352 /// \param GD Variable declaration to emit.
1353 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1354
Alexey Bataev03f270c2018-03-30 18:31:07 +00001355 /// Checks if the provided global decl \a GD is a declare target variable and
1356 /// registers it when emitting code for the host.
1357 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1358 llvm::Constant *Addr);
1359
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001360 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001361 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001362 /// \param GD Global to scan.
1363 virtual bool emitTargetGlobal(GlobalDecl GD);
1364
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001365 /// Creates the offloading descriptor in the event any target region
Samuel Antaoee8fb302016-01-06 13:42:12 +00001366 /// was emitted in the current module and return the function that registers
1367 /// it.
1368 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001369
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001370 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001371 /// variables captured in a record which address is stored in \a
1372 /// CapturedStruct.
1373 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1374 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1375 /// \param CapturedVars A pointer to the record with the references to
1376 /// variables used in \a OutlinedFn function.
1377 ///
1378 virtual void emitTeamsCall(CodeGenFunction &CGF,
1379 const OMPExecutableDirective &D,
1380 SourceLocation Loc, llvm::Value *OutlinedFn,
1381 ArrayRef<llvm::Value *> CapturedVars);
1382
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001383 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001384 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1385 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001386 /// \param NumTeams An integer expression of teams.
1387 /// \param ThreadLimit An integer expression of threads.
1388 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1389 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001390
Samuel Antaocc10b852016-07-28 14:23:26 +00001391 /// Struct that keeps all the relevant information that should be kept
1392 /// throughout a 'target data' region.
1393 class TargetDataInfo {
1394 /// Set to true if device pointer information have to be obtained.
1395 bool RequiresDevicePointerInfo = false;
1396
1397 public:
1398 /// The array of base pointer passed to the runtime library.
1399 llvm::Value *BasePointersArray = nullptr;
1400 /// The array of section pointers passed to the runtime library.
1401 llvm::Value *PointersArray = nullptr;
1402 /// The array of sizes passed to the runtime library.
1403 llvm::Value *SizesArray = nullptr;
1404 /// The array of map types passed to the runtime library.
1405 llvm::Value *MapTypesArray = nullptr;
1406 /// The total number of pointers passed to the runtime library.
1407 unsigned NumberOfPtrs = 0u;
1408 /// Map between the a declaration of a capture and the corresponding base
1409 /// pointer address where the runtime returns the device pointers.
1410 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1411
1412 explicit TargetDataInfo() {}
1413 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1414 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1415 /// Clear information about the data arrays.
1416 void clearArrayInfo() {
1417 BasePointersArray = nullptr;
1418 PointersArray = nullptr;
1419 SizesArray = nullptr;
1420 MapTypesArray = nullptr;
1421 NumberOfPtrs = 0u;
1422 }
1423 /// Return true if the current target data information has valid arrays.
1424 bool isValid() {
1425 return BasePointersArray && PointersArray && SizesArray &&
1426 MapTypesArray && NumberOfPtrs;
1427 }
1428 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1429 };
1430
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001431 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001432 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001433 /// \param IfCond Expression evaluated in if clause associated with the
1434 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001435 /// \param Device Expression evaluated in device clause associated with the
1436 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001437 /// \param Info A record used to store information that needs to be preserved
1438 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001439 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1440 const OMPExecutableDirective &D,
1441 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001442 const RegionCodeGenTy &CodeGen,
1443 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001444
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001445 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001446 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001447 /// \param D Directive to emit.
1448 /// \param IfCond Expression evaluated in if clause associated with the target
1449 /// directive, or null if no if clause is used.
1450 /// \param Device Expression evaluated in device clause associated with the
1451 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001452 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1453 const OMPExecutableDirective &D,
1454 const Expr *IfCond,
1455 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001456
1457 /// Marks function \a Fn with properly mangled versions of vector functions.
1458 /// \param FD Function marked as 'declare simd'.
1459 /// \param Fn LLVM function that must be marked with 'declare simd'
1460 /// attributes.
1461 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1462 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001463
1464 /// Emit initialization for doacross loop nesting support.
1465 /// \param D Loop-based construct used in doacross nesting construct.
1466 virtual void emitDoacrossInit(CodeGenFunction &CGF,
1467 const OMPLoopDirective &D);
1468
1469 /// Emit code for doacross ordered directive with 'depend' clause.
1470 /// \param C 'depend' clause with 'sink|source' dependency kind.
1471 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1472 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001473
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001474 /// Translates the native parameter of outlined function if this is required
1475 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001476 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001477 /// \param NativeParam Parameter itself.
1478 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1479 const VarDecl *NativeParam) const {
1480 return NativeParam;
1481 }
1482
1483 /// Gets the address of the native argument basing on the address of the
1484 /// target-specific parameter.
1485 /// \param NativeParam Parameter itself.
1486 /// \param TargetParam Corresponding target-specific parameter.
1487 virtual Address getParameterAddress(CodeGenFunction &CGF,
1488 const VarDecl *NativeParam,
1489 const VarDecl *TargetParam) const;
1490
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001491 /// Emits call of the outlined function with the provided arguments,
1492 /// translating these arguments to correct target-specific arguments.
1493 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001494 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
1495 llvm::Value *OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001496 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001497
1498 /// Emits OpenMP-specific function prolog.
1499 /// Required for device constructs.
1500 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) {}
1501
1502 /// Gets the OpenMP-specific address of the local variable.
1503 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1504 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001505
1506 /// Marks the declaration as alread emitted for the device code and returns
1507 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001508 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001509
Alexey Bataev9959db52014-05-06 10:08:46 +00001510};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001511
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001512/// Class supports emissionof SIMD-only code.
1513class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1514public:
1515 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1516 ~CGOpenMPSIMDRuntime() override {}
1517
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001518 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001519 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1520 /// kmp_int32 BoundID, struct context_vars*).
1521 /// \param D OpenMP directive.
1522 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1523 /// \param InnermostKind Kind of innermost directive (for simple directives it
1524 /// is a directive itself, for combined - its innermost directive).
1525 /// \param CodeGen Code generation sequence for the \a D directive.
1526 llvm::Value *
1527 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1528 const VarDecl *ThreadIDVar,
1529 OpenMPDirectiveKind InnermostKind,
1530 const RegionCodeGenTy &CodeGen) override;
1531
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001532 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001533 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1534 /// kmp_int32 BoundID, struct context_vars*).
1535 /// \param D OpenMP directive.
1536 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1537 /// \param InnermostKind Kind of innermost directive (for simple directives it
1538 /// is a directive itself, for combined - its innermost directive).
1539 /// \param CodeGen Code generation sequence for the \a D directive.
1540 llvm::Value *
1541 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1542 const VarDecl *ThreadIDVar,
1543 OpenMPDirectiveKind InnermostKind,
1544 const RegionCodeGenTy &CodeGen) override;
1545
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001546 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001547 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1548 /// TaskT).
1549 /// \param D OpenMP directive.
1550 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1551 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1552 /// task region.
1553 /// \param TaskTVar Variable for task_t argument.
1554 /// \param InnermostKind Kind of innermost directive (for simple directives it
1555 /// is a directive itself, for combined - its innermost directive).
1556 /// \param CodeGen Code generation sequence for the \a D directive.
1557 /// \param Tied true if task is generated for tied task, false otherwise.
1558 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1559 /// tasks.
1560 ///
1561 llvm::Value *emitTaskOutlinedFunction(
1562 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1563 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1564 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1565 bool Tied, unsigned &NumberOfParts) override;
1566
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001567 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001568 /// variables captured in a record which address is stored in \a
1569 /// CapturedStruct.
1570 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1571 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1572 /// \param CapturedVars A pointer to the record with the references to
1573 /// variables used in \a OutlinedFn function.
1574 /// \param IfCond Condition in the associated 'if' clause, if it was
1575 /// specified, nullptr otherwise.
1576 ///
1577 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1578 llvm::Value *OutlinedFn,
1579 ArrayRef<llvm::Value *> CapturedVars,
1580 const Expr *IfCond) override;
1581
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001582 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001583 /// \param CriticalName Name of the critical region.
1584 /// \param CriticalOpGen Generator for the statement associated with the given
1585 /// critical region.
1586 /// \param Hint Value of the 'hint' clause (optional).
1587 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1588 const RegionCodeGenTy &CriticalOpGen,
1589 SourceLocation Loc,
1590 const Expr *Hint = nullptr) override;
1591
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001592 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001593 /// \param MasterOpGen Generator for the statement associated with the given
1594 /// master region.
1595 void emitMasterRegion(CodeGenFunction &CGF,
1596 const RegionCodeGenTy &MasterOpGen,
1597 SourceLocation Loc) override;
1598
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001599 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001600 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1601
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001602 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001603 /// \param TaskgroupOpGen Generator for the statement associated with the
1604 /// given taskgroup region.
1605 void emitTaskgroupRegion(CodeGenFunction &CGF,
1606 const RegionCodeGenTy &TaskgroupOpGen,
1607 SourceLocation Loc) override;
1608
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001609 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001610 /// \param SingleOpGen Generator for the statement associated with the given
1611 /// single region.
1612 void emitSingleRegion(CodeGenFunction &CGF,
1613 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1614 ArrayRef<const Expr *> CopyprivateVars,
1615 ArrayRef<const Expr *> DestExprs,
1616 ArrayRef<const Expr *> SrcExprs,
1617 ArrayRef<const Expr *> AssignmentOps) override;
1618
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001619 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001620 /// \param OrderedOpGen Generator for the statement associated with the given
1621 /// ordered region.
1622 void emitOrderedRegion(CodeGenFunction &CGF,
1623 const RegionCodeGenTy &OrderedOpGen,
1624 SourceLocation Loc, bool IsThreads) override;
1625
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001626 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001627 /// \param Kind Directive for which this implicit barrier call must be
1628 /// generated. Must be OMPD_barrier for explicit barrier generation.
1629 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1630 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1631 /// runtime class decides which one to emit (simple or with cancellation
1632 /// checks).
1633 ///
1634 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1635 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1636 bool ForceSimpleCall = false) override;
1637
1638 /// This is used for non static scheduled types and when the ordered
1639 /// clause is present on the loop construct.
1640 /// Depending on the loop schedule, it is necessary to call some runtime
1641 /// routine before start of the OpenMP loop to get the loop upper / lower
1642 /// bounds \a LB and \a UB and stride \a ST.
1643 ///
1644 /// \param CGF Reference to current CodeGenFunction.
1645 /// \param Loc Clang source location.
1646 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1647 /// \param IVSize Size of the iteration variable in bits.
1648 /// \param IVSigned Sign of the iteration variable.
1649 /// \param Ordered true if loop is ordered, false otherwise.
1650 /// \param DispatchValues struct containing llvm values for lower bound, upper
1651 /// bound, and chunk expression.
1652 /// For the default (nullptr) value, the chunk 1 will be used.
1653 ///
1654 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1655 const OpenMPScheduleTy &ScheduleKind,
1656 unsigned IVSize, bool IVSigned, bool Ordered,
1657 const DispatchRTInput &DispatchValues) override;
1658
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001659 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001660 /// of loop.
1661 ///
1662 /// This is used only in case of static schedule, when the user did not
1663 /// specify a ordered clause on the loop construct.
1664 /// Depending on the loop schedule, it is necessary to call some runtime
1665 /// routine before start of the OpenMP loop to get the loop upper / lower
1666 /// bounds LB and UB and stride ST.
1667 ///
1668 /// \param CGF Reference to current CodeGenFunction.
1669 /// \param Loc Clang source location.
1670 /// \param DKind Kind of the directive.
1671 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1672 /// \param Values Input arguments for the construct.
1673 ///
1674 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1675 OpenMPDirectiveKind DKind,
1676 const OpenMPScheduleTy &ScheduleKind,
1677 const StaticRTInput &Values) override;
1678
1679 ///
1680 /// \param CGF Reference to current CodeGenFunction.
1681 /// \param Loc Clang source location.
1682 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1683 /// \param Values Input arguments for the construct.
1684 ///
1685 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1686 OpenMPDistScheduleClauseKind SchedKind,
1687 const StaticRTInput &Values) override;
1688
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001689 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001690 /// iteration of the ordered loop with the dynamic scheduling.
1691 ///
1692 /// \param CGF Reference to current CodeGenFunction.
1693 /// \param Loc Clang source location.
1694 /// \param IVSize Size of the iteration variable in bits.
1695 /// \param IVSigned Sign of the iteration variable.
1696 ///
1697 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1698 unsigned IVSize, bool IVSigned) override;
1699
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001700 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001701 /// all the work with current loop.
1702 ///
1703 /// \param CGF Reference to current CodeGenFunction.
1704 /// \param Loc Clang source location.
1705 /// \param DKind Kind of the directive for which the static finish is emitted.
1706 ///
1707 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1708 OpenMPDirectiveKind DKind) override;
1709
1710 /// Call __kmpc_dispatch_next(
1711 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1712 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1713 /// kmp_int[32|64] *p_stride);
1714 /// \param IVSize Size of the iteration variable in bits.
1715 /// \param IVSigned Sign of the iteration variable.
1716 /// \param IL Address of the output variable in which the flag of the
1717 /// last iteration is returned.
1718 /// \param LB Address of the output variable in which the lower iteration
1719 /// number is returned.
1720 /// \param UB Address of the output variable in which the upper iteration
1721 /// number is returned.
1722 /// \param ST Address of the output variable in which the stride value is
1723 /// returned.
1724 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1725 unsigned IVSize, bool IVSigned, Address IL,
1726 Address LB, Address UB, Address ST) override;
1727
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001728 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001729 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1730 /// clause.
1731 /// \param NumThreads An integer value of threads.
1732 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1733 SourceLocation Loc) override;
1734
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001735 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001736 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1737 void emitProcBindClause(CodeGenFunction &CGF,
1738 OpenMPProcBindClauseKind ProcBind,
1739 SourceLocation Loc) override;
1740
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001741 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001742 /// thread.
1743 /// \param VD Threadprivate variable.
1744 /// \param VDAddr Address of the global variable \a VD.
1745 /// \param Loc Location of the reference to threadprivate var.
1746 /// \return Address of the threadprivate variable for the current thread.
1747 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1748 Address VDAddr, SourceLocation Loc) override;
1749
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001750 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001751 /// a call to runtime library which adds initial value to the newly created
1752 /// threadprivate variable (if it is not constant) and registers destructor
1753 /// for the variable (if any).
1754 /// \param VD Threadprivate variable.
1755 /// \param VDAddr Address of the global variable \a VD.
1756 /// \param Loc Location of threadprivate declaration.
1757 /// \param PerformInit true if initialization expression is not constant.
1758 llvm::Function *
1759 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1760 SourceLocation Loc, bool PerformInit,
1761 CodeGenFunction *CGF = nullptr) override;
1762
1763 /// Creates artificial threadprivate variable with name \p Name and type \p
1764 /// VarType.
1765 /// \param VarType Type of the artificial threadprivate variable.
1766 /// \param Name Name of the artificial threadprivate variable.
1767 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1768 QualType VarType,
1769 StringRef Name) override;
1770
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001771 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001772 /// \param Vars List of variables to flush.
1773 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1774 SourceLocation Loc) override;
1775
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001776 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001777 /// emitted in several steps:
1778 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1779 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1780 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1781 /// function:
1782 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1783 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1784 /// return 0;
1785 /// }
1786 /// 2. Copy a list of shared variables to field shareds of the resulting
1787 /// structure kmp_task_t returned by the previous call (if any).
1788 /// 3. Copy a pointer to destructions function to field destructions of the
1789 /// resulting structure kmp_task_t.
1790 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1791 /// kmp_task_t *new_task), where new_task is a resulting structure from
1792 /// previous items.
1793 /// \param D Current task directive.
1794 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1795 /// /*part_id*/, captured_struct */*__context*/);
1796 /// \param SharedsTy A type which contains references the shared variables.
1797 /// \param Shareds Context with the list of shared variables from the \p
1798 /// TaskFunction.
1799 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1800 /// otherwise.
1801 /// \param Data Additional data for task generation like tiednsee, final
1802 /// state, list of privates etc.
1803 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1804 const OMPExecutableDirective &D, llvm::Value *TaskFunction,
1805 QualType SharedsTy, Address Shareds, const Expr *IfCond,
1806 const OMPTaskDataTy &Data) override;
1807
1808 /// Emit task region for the taskloop directive. The taskloop region is
1809 /// emitted in several steps:
1810 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1811 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1812 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1813 /// function:
1814 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1815 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1816 /// return 0;
1817 /// }
1818 /// 2. Copy a list of shared variables to field shareds of the resulting
1819 /// structure kmp_task_t returned by the previous call (if any).
1820 /// 3. Copy a pointer to destructions function to field destructions of the
1821 /// resulting structure kmp_task_t.
1822 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1823 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1824 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1825 /// is a resulting structure from
1826 /// previous items.
1827 /// \param D Current task directive.
1828 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1829 /// /*part_id*/, captured_struct */*__context*/);
1830 /// \param SharedsTy A type which contains references the shared variables.
1831 /// \param Shareds Context with the list of shared variables from the \p
1832 /// TaskFunction.
1833 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1834 /// otherwise.
1835 /// \param Data Additional data for task generation like tiednsee, final
1836 /// state, list of privates etc.
1837 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1838 const OMPLoopDirective &D, llvm::Value *TaskFunction,
1839 QualType SharedsTy, Address Shareds, const Expr *IfCond,
1840 const OMPTaskDataTy &Data) override;
1841
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001842 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001843 /// reduction:
1844 /// \code
1845 ///
1846 /// static kmp_critical_name lock = { 0 };
1847 ///
1848 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1849 /// ...
1850 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1851 /// ...
1852 /// }
1853 ///
1854 /// ...
1855 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1856 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1857 /// RedList, reduce_func, &<lock>)) {
1858 /// case 1:
1859 /// ...
1860 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1861 /// ...
1862 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1863 /// break;
1864 /// case 2:
1865 /// ...
1866 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1867 /// ...
1868 /// break;
1869 /// default:;
1870 /// }
1871 /// \endcode
1872 ///
1873 /// \param Privates List of private copies for original reduction arguments.
1874 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1875 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1876 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1877 /// or 'operator binop(LHS, RHS)'.
1878 /// \param Options List of options for reduction codegen:
1879 /// WithNowait true if parent directive has also nowait clause, false
1880 /// otherwise.
1881 /// SimpleReduction Emit reduction operation only. Used for omp simd
1882 /// directive on the host.
1883 /// ReductionKind The kind of reduction to perform.
1884 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1885 ArrayRef<const Expr *> Privates,
1886 ArrayRef<const Expr *> LHSExprs,
1887 ArrayRef<const Expr *> RHSExprs,
1888 ArrayRef<const Expr *> ReductionOps,
1889 ReductionOptionsTy Options) override;
1890
1891 /// Emit a code for initialization of task reduction clause. Next code
1892 /// should be emitted for reduction:
1893 /// \code
1894 ///
1895 /// _task_red_item_t red_data[n];
1896 /// ...
1897 /// red_data[i].shar = &origs[i];
1898 /// red_data[i].size = sizeof(origs[i]);
1899 /// red_data[i].f_init = (void*)RedInit<i>;
1900 /// red_data[i].f_fini = (void*)RedDest<i>;
1901 /// red_data[i].f_comb = (void*)RedOp<i>;
1902 /// red_data[i].flags = <Flag_i>;
1903 /// ...
1904 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1905 /// \endcode
1906 ///
1907 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1908 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1909 /// \param Data Additional data for task generation like tiedness, final
1910 /// state, list of privates, reductions etc.
1911 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
1912 ArrayRef<const Expr *> LHSExprs,
1913 ArrayRef<const Expr *> RHSExprs,
1914 const OMPTaskDataTy &Data) override;
1915
1916 /// Required to resolve existing problems in the runtime. Emits threadprivate
1917 /// variables to store the size of the VLAs/array sections for
1918 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1919 /// store the pointer to the original reduction item for the custom
1920 /// initializer defined by declare reduction construct.
1921 /// \param RCG Allows to reuse an existing data for the reductions.
1922 /// \param N Reduction item for which fixups must be emitted.
1923 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1924 ReductionCodeGen &RCG, unsigned N) override;
1925
1926 /// Get the address of `void *` type of the privatue copy of the reduction
1927 /// item specified by the \p SharedLVal.
1928 /// \param ReductionsPtr Pointer to the reduction data returned by the
1929 /// emitTaskReductionInit function.
1930 /// \param SharedLVal Address of the original reduction item.
1931 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1932 llvm::Value *ReductionsPtr,
1933 LValue SharedLVal) override;
1934
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001935 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001936 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1937
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001938 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001939 /// \param CancelRegion Region kind for which the cancellation point must be
1940 /// emitted.
1941 ///
1942 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
1943 OpenMPDirectiveKind CancelRegion) override;
1944
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001945 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001946 /// \param IfCond Condition in the associated 'if' clause, if it was
1947 /// specified, nullptr otherwise.
1948 /// \param CancelRegion Region kind for which the cancel must be emitted.
1949 ///
1950 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1951 const Expr *IfCond,
1952 OpenMPDirectiveKind CancelRegion) override;
1953
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001954 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001955 /// \param D Directive to emit.
1956 /// \param ParentName Name of the function that encloses the target region.
1957 /// \param OutlinedFn Outlined function value to be defined by this call.
1958 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1959 /// \param IsOffloadEntry True if the outlined function is an offload entry.
1960 /// \param CodeGen Code generation sequence for the \a D directive.
1961 /// An outlined function may not be an entry if, e.g. the if clause always
1962 /// evaluates to false.
1963 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1964 StringRef ParentName,
1965 llvm::Function *&OutlinedFn,
1966 llvm::Constant *&OutlinedFnID,
1967 bool IsOffloadEntry,
1968 const RegionCodeGenTy &CodeGen) override;
1969
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001970 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001971 /// code attempts offloading the execution to the device, an the event of
1972 /// a failure it executes the host version outlined in \a OutlinedFn.
1973 /// \param D Directive to emit.
1974 /// \param OutlinedFn Host version of the code to be offloaded.
1975 /// \param OutlinedFnID ID of host version of the code to be offloaded.
1976 /// \param IfCond Expression evaluated in if clause associated with the target
1977 /// directive, or null if no if clause is used.
1978 /// \param Device Expression evaluated in device clause associated with the
1979 /// target directive, or null if no device clause is used.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001980 void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1981 llvm::Value *OutlinedFn, llvm::Value *OutlinedFnID,
Alexey Bataev8451efa2018-01-15 19:06:12 +00001982 const Expr *IfCond, const Expr *Device) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001983
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001984 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001985 /// the function itself in case it is a valid device function. Returns true if
1986 /// \a GD was dealt with successfully.
1987 /// \param GD Function to scan.
1988 bool emitTargetFunctions(GlobalDecl GD) override;
1989
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001990 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001991 /// Returns true if \a GD was dealt with successfully.
1992 /// \param GD Variable declaration to emit.
1993 bool emitTargetGlobalVariable(GlobalDecl GD) override;
1994
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001995 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001996 /// if it was emitted successfully.
1997 /// \param GD Global to scan.
1998 bool emitTargetGlobal(GlobalDecl GD) override;
1999
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002000 /// Creates the offloading descriptor in the event any target region
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002001 /// was emitted in the current module and return the function that registers
2002 /// it.
2003 llvm::Function *emitRegistrationFunction() override;
2004
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002005 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002006 /// variables captured in a record which address is stored in \a
2007 /// CapturedStruct.
2008 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2009 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2010 /// \param CapturedVars A pointer to the record with the references to
2011 /// variables used in \a OutlinedFn function.
2012 ///
2013 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2014 SourceLocation Loc, llvm::Value *OutlinedFn,
2015 ArrayRef<llvm::Value *> CapturedVars) override;
2016
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002017 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002018 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2019 /// for num_teams clause.
2020 /// \param NumTeams An integer expression of teams.
2021 /// \param ThreadLimit An integer expression of threads.
2022 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2023 const Expr *ThreadLimit, SourceLocation Loc) override;
2024
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002025 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002026 /// \param D Directive to emit.
2027 /// \param IfCond Expression evaluated in if clause associated with the
2028 /// target directive, or null if no device clause is used.
2029 /// \param Device Expression evaluated in device clause associated with the
2030 /// target directive, or null if no device clause is used.
2031 /// \param Info A record used to store information that needs to be preserved
2032 /// until the region is closed.
2033 void emitTargetDataCalls(CodeGenFunction &CGF,
2034 const OMPExecutableDirective &D, const Expr *IfCond,
2035 const Expr *Device, const RegionCodeGenTy &CodeGen,
2036 TargetDataInfo &Info) override;
2037
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002038 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002039 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2040 /// \param D Directive to emit.
2041 /// \param IfCond Expression evaluated in if clause associated with the target
2042 /// directive, or null if no if clause is used.
2043 /// \param Device Expression evaluated in device clause associated with the
2044 /// target directive, or null if no device clause is used.
2045 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2046 const OMPExecutableDirective &D,
2047 const Expr *IfCond,
2048 const Expr *Device) override;
2049
2050 /// Emit initialization for doacross loop nesting support.
2051 /// \param D Loop-based construct used in doacross nesting construct.
2052 void emitDoacrossInit(CodeGenFunction &CGF,
2053 const OMPLoopDirective &D) override;
2054
2055 /// Emit code for doacross ordered directive with 'depend' clause.
2056 /// \param C 'depend' clause with 'sink|source' dependency kind.
2057 void emitDoacrossOrdered(CodeGenFunction &CGF,
2058 const OMPDependClause *C) override;
2059
2060 /// Translates the native parameter of outlined function if this is required
2061 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002062 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002063 /// \param NativeParam Parameter itself.
2064 const VarDecl *translateParameter(const FieldDecl *FD,
2065 const VarDecl *NativeParam) const override;
2066
2067 /// Gets the address of the native argument basing on the address of the
2068 /// target-specific parameter.
2069 /// \param NativeParam Parameter itself.
2070 /// \param TargetParam Corresponding target-specific parameter.
2071 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2072 const VarDecl *TargetParam) const override;
2073};
2074
Alexey Bataev23b69422014-06-18 07:08:49 +00002075} // namespace CodeGen
2076} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002077
2078#endif