blob: f1582457ed2daa1fc329375c691b48a6dc8f1b7b [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataev9959db52014-05-06 10:08:46 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This provides a class for OpenMP runtime code generation.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
14#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
Alexey Bataev9959db52014-05-06 10:08:46 +000015
Alexey Bataev7292c292016-04-25 12:22:29 +000016#include "CGValue.h"
Richard Trieuf8b8b392019-01-11 01:32:35 +000017#include "clang/AST/DeclOpenMP.h"
Jordan Rupprecht52690912019-10-01 22:30:10 +000018#include "clang/AST/GlobalDecl.h"
Alexey Bataev62b63b12015-03-10 07:28:44 +000019#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000020#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000021#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "llvm/ADT/DenseMap.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000023#include "llvm/ADT/StringMap.h"
Alexey Bataev2a6f3f52018-11-07 19:11:14 +000024#include "llvm/ADT/StringSet.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000025#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000026#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000027
28namespace llvm {
29class ArrayType;
30class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000031class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000032class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000033class StructType;
34class Type;
35class Value;
36} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000037
Alexey Bataev9959db52014-05-06 10:08:46 +000038namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000039class Expr;
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
Alexey Bataev0860db92019-12-19 10:01:10 -0500214 /// Manages list of nontemporal decls for the specified directive.
215 class NontemporalDeclsRAII {
216 CodeGenModule &CGM;
217 const bool NeedToPush;
218
219 public:
220 NontemporalDeclsRAII(CodeGenModule &CGM, const OMPLoopDirective &S);
221 ~NontemporalDeclsRAII();
222 };
223
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000224protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000225 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000226 StringRef FirstSeparator, Separator;
227
228 /// Constructor allowing to redefine the name separator for the variables.
229 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
230 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000231
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000232 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000233 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000234 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000235 uint64_t Size, int32_t Flags,
236 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000237
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000238 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000239 /// \param D Directive to emit.
240 /// \param ParentName Name of the function that encloses the target region.
241 /// \param OutlinedFn Outlined function value to be defined by this call.
242 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
243 /// \param IsOffloadEntry True if the outlined function is an offload entry.
244 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000245 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000246 /// evaluates to false.
247 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
248 StringRef ParentName,
249 llvm::Function *&OutlinedFn,
250 llvm::Constant *&OutlinedFnID,
251 bool IsOffloadEntry,
252 const RegionCodeGenTy &CodeGen);
253
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000254 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000255 /// \param Flags Flags for OpenMP location.
256 ///
257 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
258 unsigned Flags = 0);
259
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000260 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000261 llvm::Type *getIdentTyPointerTy();
262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000263 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000264 ///
265 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
266
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000267 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000268 // The name can be customized depending on the target.
269 //
270 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
271
Alexey Bataev3c595a62017-08-14 15:01:03 +0000272 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
James Y Knight9871db02019-02-05 16:42:33 +0000273 void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
274 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000275 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
Alexey Bataevfd006c42018-10-05 15:08:53 +0000281 void setLocThreadIdInsertPt(CodeGenFunction &CGF,
282 bool AtCurrentPoint = false);
283 void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
284
Alexey Bataevceeaa482018-11-21 21:04:34 +0000285 /// Check if the default location must be constant.
286 /// Default is false to support OMPT/OMPD.
287 virtual bool isDefaultLocationConstant() const { return false; }
288
289 /// Returns additional flags that can be stored in reserved_2 field of the
290 /// default location.
291 virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
292
Alexey Bataevc2cd2d42019-10-10 17:28:10 +0000293 /// Tries to emit declare variant function for \p OldGD from \p NewGD.
294 /// \param OrigAddr LLVM IR value for \p OldGD.
295 /// \param IsForDefinition true, if requested emission for the definition of
296 /// \p OldGD.
297 /// \returns true, was able to emit a definition function for \p OldGD, which
298 /// points to \p NewGD.
299 virtual bool tryEmitDeclareVariant(const GlobalDecl &NewGD,
300 const GlobalDecl &OldGD,
301 llvm::GlobalValue *OrigAddr,
302 bool IsForDefinition);
303
Alexey Bataevc3028ca2018-12-04 15:03:25 +0000304 /// Returns default flags for the barriers depending on the directive, for
305 /// which this barier is going to be emitted.
306 static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
307
Alexey Bataeva1166022018-11-27 21:24:54 +0000308 /// Get the LLVM type for the critical name.
309 llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
310
311 /// Returns corresponding lock object for the specified critical region
312 /// name. If the lock object does not exist it is created, otherwise the
313 /// reference to the existing copy is returned.
314 /// \param CriticalName Name of the critical region.
315 ///
316 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
317
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000318private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000319 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000320 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000321 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataevceeaa482018-11-21 21:04:34 +0000322 using FlagsTy = std::pair<unsigned, unsigned>;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000323 /// Map of flags and corresponding default locations.
Alexey Bataevceeaa482018-11-21 21:04:34 +0000324 using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
Alexey Bataev15007ba2014-05-07 06:18:01 +0000325 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000326 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000327
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000328 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000329 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000330 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000331 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
332 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000333 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000334 /// Original representation is:
335 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000336 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000337 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000338 struct DebugLocThreadIdTy {
339 llvm::Value *DebugLoc;
340 llvm::Value *ThreadID;
Alexey Bataevfd006c42018-10-05 15:08:53 +0000341 /// Insert point for the service instructions.
342 llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000343 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000344 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000345 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
346 OpenMPLocThreadIDMapTy;
347 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000348 /// Map of UDRs and corresponding combiner/initializer.
349 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
350 std::pair<llvm::Function *, llvm::Function *>>
351 UDRMapTy;
352 UDRMapTy UDRMap;
353 /// Map of functions and locally defined UDRs.
354 typedef llvm::DenseMap<llvm::Function *,
355 SmallVector<const OMPDeclareReductionDecl *, 4>>
356 FunctionUDRMapTy;
357 FunctionUDRMapTy FunctionUDRMap;
Michael Krused47b9432019-08-05 18:43:21 +0000358 /// Map from the user-defined mapper declaration to its corresponding
359 /// functions.
360 llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
361 /// Map of functions and their local user-defined mappers.
362 using FunctionUDMMapTy =
363 llvm::DenseMap<llvm::Function *,
364 SmallVector<const OMPDeclareMapperDecl *, 4>>;
365 FunctionUDMMapTy FunctionUDMMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000366 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000367 /// kmp_critical_name[8];
368 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000369 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000370 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
371 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
372 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
373 /// variables.
374 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
375 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000376 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000377 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000378 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000379 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000380 /// void * shareds; /**< pointer to block of pointers to
381 /// shared vars */
382 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
383 /// executing task */
384 /// kmp_int32 part_id; /**< part id for the task */
385 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
386 /// deconstructors of firstprivate C++ objects */
387 /// } kmp_task_t;
388 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000389 /// Saved kmp_task_t for task directive.
390 QualType SavedKmpTaskTQTy;
391 /// Saved kmp_task_t for taskloop-based directive.
392 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000393 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000394 /// kmp_intptr_t base_addr;
395 /// size_t len;
396 /// struct {
397 /// bool in:1;
398 /// bool out:1;
399 /// } flags;
400 /// } kmp_depend_info_t;
401 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000402 /// struct kmp_dim { // loop bounds info casted to kmp_int64
403 /// kmp_int64 lo; // lower
404 /// kmp_int64 up; // upper
405 /// kmp_int64 st; // stride
406 /// };
407 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000408 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000409 /// void *addr; // Pointer to the offload entry info.
410 /// // (function or global)
411 /// char *name; // Name of the function or global.
412 /// size_t size; // Size of the entry info (0 if it a function).
413 /// };
414 QualType TgtOffloadEntryQTy;
415 /// struct __tgt_device_image{
416 /// void *ImageStart; // Pointer to the target code start.
417 /// void *ImageEnd; // Pointer to the target code end.
418 /// // We also add the host entries to the device image, as it may be useful
419 /// // for the target runtime to have access to that information.
420 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
421 /// // the entries.
422 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
423 /// // entries (non inclusive).
424 /// };
425 QualType TgtDeviceImageQTy;
426 /// struct __tgt_bin_desc{
427 /// int32_t NumDevices; // Number of devices supported.
428 /// __tgt_device_image *DeviceImages; // Arrays of device images
429 /// // (one per device).
430 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
431 /// // entries.
432 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
433 /// // entries (non inclusive).
434 /// };
435 QualType TgtBinaryDescriptorQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000436 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000437 /// far.
438 class OffloadEntriesInfoManagerTy {
439 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000440
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000441 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000442 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000443
444 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000445 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000446 class OffloadEntryInfo {
447 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000448 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000449 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000450 /// Entry is a target region.
451 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000452 /// Entry is a declare target variable.
453 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000454 /// Invalid entry info.
455 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000456 };
457
Alexey Bataev03f270c2018-03-30 18:31:07 +0000458 protected:
459 OffloadEntryInfo() = delete;
460 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000461 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000462 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000463 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000464 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000465
Alexey Bataev03f270c2018-03-30 18:31:07 +0000466 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000467 bool isValid() const { return Order != ~0u; }
468 unsigned getOrder() const { return Order; }
469 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000470 uint32_t getFlags() const { return Flags; }
471 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
472 llvm::Constant *getAddress() const {
473 return cast_or_null<llvm::Constant>(Addr);
474 }
475 void setAddress(llvm::Constant *V) {
476 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
477 Addr = V;
478 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000479 static bool classof(const OffloadEntryInfo *Info) { return true; }
480
Samuel Antaof83efdb2017-01-05 16:02:49 +0000481 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000482 /// Address of the entity that has to be mapped for offloading.
483 llvm::WeakTrackingVH Addr;
484
Samuel Antaof83efdb2017-01-05 16:02:49 +0000485 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000486 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000487
488 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000489 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000490
Alexey Bataev03f270c2018-03-30 18:31:07 +0000491 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000492 };
493
Alexey Bataev03f270c2018-03-30 18:31:07 +0000494 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000495 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000496 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000497 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000498 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000499
Alexey Bataev03f270c2018-03-30 18:31:07 +0000500 //
501 // Target region entries related.
502 //
503
504 /// Kind of the target registry entry.
505 enum OMPTargetRegionEntryKind : uint32_t {
506 /// Mark the entry as target region.
507 OMPTargetRegionEntryTargetRegion = 0x0,
508 /// Mark the entry as a global constructor.
509 OMPTargetRegionEntryCtor = 0x02,
510 /// Mark the entry as a global destructor.
511 OMPTargetRegionEntryDtor = 0x04,
512 };
513
514 /// Target region entries info.
515 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
516 /// Address that can be used as the ID of the entry.
517 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000518
519 public:
520 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000521 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000522 explicit OffloadEntryInfoTargetRegion(unsigned Order,
523 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000524 llvm::Constant *ID,
525 OMPTargetRegionEntryKind Flags)
526 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000527 ID(ID) {
528 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000529 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000530
531 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000532 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000533 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000534 ID = V;
535 }
536 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000537 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000538 }
539 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000540
541 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000542 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
543 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000544 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000545 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000546 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
547 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000548 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000549 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000550 /// Return true if a target region entry with the provided information
551 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000552 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000553 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000554 /// brief Applies action \a Action on all registered entries.
555 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000556 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000557 OffloadTargetRegionEntryInfoActTy;
558 void actOnTargetRegionEntriesInfo(
559 const OffloadTargetRegionEntryInfoActTy &Action);
560
Alexey Bataev03f270c2018-03-30 18:31:07 +0000561 //
562 // Device global variable entries related.
563 //
564
565 /// Kind of the global variable entry..
566 enum OMPTargetGlobalVarEntryKind : uint32_t {
567 /// Mark the entry as a to declare target.
568 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000569 /// Mark the entry as a to declare target link.
570 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000571 };
572
573 /// Device global variable entries info.
574 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
575 /// Type of the global variable.
576 CharUnits VarSize;
577 llvm::GlobalValue::LinkageTypes Linkage;
578
579 public:
580 OffloadEntryInfoDeviceGlobalVar()
581 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
582 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
583 OMPTargetGlobalVarEntryKind Flags)
584 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
585 explicit OffloadEntryInfoDeviceGlobalVar(
586 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
587 OMPTargetGlobalVarEntryKind Flags,
588 llvm::GlobalValue::LinkageTypes Linkage)
589 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
590 VarSize(VarSize), Linkage(Linkage) {
591 setAddress(Addr);
592 }
593
594 CharUnits getVarSize() const { return VarSize; }
595 void setVarSize(CharUnits Size) { VarSize = Size; }
596 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
597 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
598 static bool classof(const OffloadEntryInfo *Info) {
599 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
600 }
601 };
602
603 /// Initialize device global variable entry.
604 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
605 OMPTargetGlobalVarEntryKind Flags,
606 unsigned Order);
607
608 /// Register device global variable entry.
609 void
610 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
611 CharUnits VarSize,
612 OMPTargetGlobalVarEntryKind Flags,
613 llvm::GlobalValue::LinkageTypes Linkage);
614 /// Checks if the variable with the given name has been registered already.
615 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
616 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
617 }
618 /// Applies action \a Action on all registered entries.
619 typedef llvm::function_ref<void(StringRef,
620 const OffloadEntryInfoDeviceGlobalVar &)>
621 OffloadDeviceGlobalVarEntryInfoActTy;
622 void actOnDeviceGlobalVarEntriesInfo(
623 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
624
Samuel Antaoee8fb302016-01-06 13:42:12 +0000625 private:
626 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000627 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000628 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000629 OffloadEntriesTargetRegionPerLine;
630 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
631 OffloadEntriesTargetRegionPerParentName;
632 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
633 OffloadEntriesTargetRegionPerFile;
634 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
635 OffloadEntriesTargetRegionPerDevice;
636 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
637 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000638 /// Storage for device global variable entries kind. The storage is to be
639 /// indexed by mangled name.
640 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
641 OffloadEntriesDeviceGlobalVarTy;
642 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000643 };
644 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
645
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000646 bool ShouldMarkAsGlobal = true;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000647 /// List of the emitted functions.
648 llvm::StringSet<> AlreadyEmittedTargetFunctions;
649 /// List of the global variables with their addresses that should not be
650 /// emitted for the target.
651 llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000652
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000653 /// List of variables that can become declare target implicitly and, thus,
654 /// must be emitted.
655 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
656
Alexey Bataev2df5f122019-10-01 20:18:32 +0000657 /// Mapping of the original functions to their variants and original global
658 /// decl.
659 llvm::MapVector<CanonicalDeclPtr<const FunctionDecl>,
660 std::pair<GlobalDecl, GlobalDecl>>
661 DeferredVariantFunction;
662
Alexey Bataev0860db92019-12-19 10:01:10 -0500663 using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
664 /// Stack for list of declarations in current context marked as nontemporal.
665 /// The set is the union of all current stack elements.
666 llvm::SmallVector<NontemporalDeclsSet, 4> NontemporalDeclsStack;
667
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000668 /// Flag for keeping track of weather a requires unified_shared_memory
669 /// directive is present.
670 bool HasRequiresUnifiedSharedMemory = false;
671
672 /// Flag for keeping track of weather a target region has been emitted.
673 bool HasEmittedTargetRegion = false;
674
675 /// Flag for keeping track of weather a device routine has been emitted.
676 /// Device routines are specific to the
677 bool HasEmittedDeclareTargetRegion = false;
678
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000679 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000680 /// metadata.
681 void loadOffloadInfoMetadata();
682
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000683 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000684 QualType getTgtOffloadEntryQTy();
685
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000686 /// Returns __tgt_device_image type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000687 QualType getTgtDeviceImageQTy();
688
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000689 /// Returns __tgt_bin_desc type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000690 QualType getTgtBinaryDescriptorQTy();
691
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000692 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000693 /// found along the way.
694 /// \param S Starting statement.
695 /// \param ParentName Name of the function declaration that is being scanned.
696 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000697
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000698 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000699 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000700
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000701 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000702 llvm::Type *getKmpc_MicroPointerTy();
703
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000704 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000705 /// \param Function OpenMP runtime function.
706 /// \return Specified function.
James Y Knight9871db02019-02-05 16:42:33 +0000707 llvm::FunctionCallee createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000708
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000709 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000710 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000711 llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
712 bool IVSigned);
Alexander Musman21212e42015-03-13 10:38:23 +0000713
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000714 /// Returns __kmpc_dispatch_init_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000715 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000716 llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize,
717 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000718
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000719 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000720 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000721 llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
722 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000723
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000724 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000725 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000726 llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
727 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000728
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000729 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000730 /// return threadprivate cache object. This object is a pointer's worth of
731 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000732 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000733 /// \return Cache variable for the specified threadprivate.
734 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
735
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000736 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000737 /// internal global variable with the specified Name. The created variable has
738 /// linkage CommonLinkage by default and is initialized by null value.
739 /// \param Ty Type of the global variable. If it is exist already the type
740 /// must be the same.
741 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000742 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000743 const llvm::Twine &Name,
744 unsigned AddressSpace = 0);
Alexey Bataev97720002014-11-11 04:05:39 +0000745
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000746 /// Set of threadprivate variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000747 llvm::StringSet<> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000748
Alexey Bataev34f8a702018-03-28 14:28:54 +0000749 /// Set of declare target variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000750 llvm::StringSet<> DeclareTargetWithDefinition;
Alexey Bataev34f8a702018-03-28 14:28:54 +0000751
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000752 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000753 /// \param VDAddr Address of the global variable \a VD.
754 /// \param Ctor Pointer to a global init function for \a VD.
755 /// \param CopyCtor Pointer to a global copy function for \a VD.
756 /// \param Dtor Pointer to a global destructor function for \a VD.
757 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000758 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000759 llvm::Value *Ctor, llvm::Value *CopyCtor,
760 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000761
Michael Krused47b9432019-08-05 18:43:21 +0000762 /// Emit the array initialization or deletion portion for user-defined mapper
763 /// code generation.
764 void emitUDMapperArrayInitOrDel(CodeGenFunction &MapperCGF,
765 llvm::Value *Handle, llvm::Value *BasePtr,
766 llvm::Value *Ptr, llvm::Value *Size,
767 llvm::Value *MapType, CharUnits ElementSize,
768 llvm::BasicBlock *ExitBB, bool IsInit);
769
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000770 struct TaskResultTy {
771 llvm::Value *NewTask = nullptr;
James Y Knight9871db02019-02-05 16:42:33 +0000772 llvm::Function *TaskEntry = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000773 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000774 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000775 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000776 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000777 };
778 /// Emit task region for the task directive. The task region is emitted in
779 /// several steps:
780 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
781 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
782 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
783 /// function:
784 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
785 /// TaskFunction(gtid, tt->part_id, tt->shareds);
786 /// return 0;
787 /// }
788 /// 2. Copy a list of shared variables to field shareds of the resulting
789 /// structure kmp_task_t returned by the previous call (if any).
790 /// 3. Copy a pointer to destructions function to field destructions of the
791 /// resulting structure kmp_task_t.
792 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000793 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
794 /// /*part_id*/, captured_struct */*__context*/);
795 /// \param SharedsTy A type which contains references the shared variables.
796 /// \param Shareds Context with the list of shared variables from the \p
797 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000798 /// \param Data Additional data for task generation like tiednsee, final
799 /// state, list of privates etc.
800 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
801 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +0000802 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000803 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000804
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000805 /// Returns default address space for the constant firstprivates, 0 by
806 /// default.
807 virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; }
808
Alexey Bataevec7946e2019-09-23 14:06:51 +0000809 /// Emit code that pushes the trip count of loops associated with constructs
810 /// 'target teams distribute' and 'teams distribute parallel for'.
811 /// \param SizeEmitter Emits the int64 value for the number of iterations of
812 /// the associated loop.
813 void emitTargetNumIterationsCall(
814 CodeGenFunction &CGF, const OMPExecutableDirective &D,
815 llvm::Value *DeviceID,
816 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
817 const OMPLoopDirective &D)>
818 SizeEmitter);
819
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000820public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000821 explicit CGOpenMPRuntime(CodeGenModule &CGM)
822 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000823 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000824 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000825
Alexey Bataevd08c0562019-11-19 12:07:54 -0500826 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
827 /// function. Here is the logic:
828 /// if (Cond) {
829 /// ThenGen();
830 /// } else {
831 /// ElseGen();
832 /// }
833 void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
834 const RegionCodeGenTy &ThenGen,
835 const RegionCodeGenTy &ElseGen);
836
Alexey Bataev5c427362019-04-10 19:11:33 +0000837 /// Checks if the \p Body is the \a CompoundStmt and returns its child
838 /// statement iff there is only one that is not evaluatable at the compile
839 /// time.
840 static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
841
Alexey Bataev18fa2322018-05-02 14:20:50 +0000842 /// Get the platform-specific name separator.
843 std::string getName(ArrayRef<StringRef> Parts) const;
844
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000845 /// Emit code for the specified user defined reduction construct.
846 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
847 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000848 /// Get combiner/initializer for the specified user-defined reduction, if any.
849 virtual std::pair<llvm::Function *, llvm::Function *>
850 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000851
Michael Krused47b9432019-08-05 18:43:21 +0000852 /// Emit the function for the user defined mapper construct.
853 void emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
854 CodeGenFunction *CGF = nullptr);
855
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000856 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000857 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
858 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000859 /// \param D OpenMP directive.
860 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000861 /// \param InnermostKind Kind of innermost directive (for simple directives it
862 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000863 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000864 virtual llvm::Function *emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000865 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
866 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
867
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000868 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000869 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
870 /// kmp_int32 BoundID, struct context_vars*).
871 /// \param D OpenMP directive.
872 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
873 /// \param InnermostKind Kind of innermost directive (for simple directives it
874 /// is a directive itself, for combined - its innermost directive).
875 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000876 virtual llvm::Function *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000877 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
878 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000879
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000880 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000881 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
882 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000883 /// \param D OpenMP directive.
884 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000885 /// \param PartIDVar Variable for partition id in the current OpenMP untied
886 /// task region.
887 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000888 /// \param InnermostKind Kind of innermost directive (for simple directives it
889 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000890 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000891 /// \param Tied true if task is generated for tied task, false otherwise.
892 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
893 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000894 ///
James Y Knight9871db02019-02-05 16:42:33 +0000895 virtual llvm::Function *emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000896 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000897 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
898 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
899 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000900
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000901 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000902 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000903 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000904
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000905 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000906 /// variables captured in a record which address is stored in \a
907 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000908 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000909 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000910 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000911 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000912 /// \param IfCond Condition in the associated 'if' clause, if it was
913 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000914 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000915 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +0000916 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000917 ArrayRef<llvm::Value *> CapturedVars,
918 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000919
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000920 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000921 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000922 /// \param CriticalOpGen Generator for the statement associated with the given
923 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000924 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000925 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000926 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000927 SourceLocation Loc,
928 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000929
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000930 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000931 /// \param MasterOpGen Generator for the statement associated with the given
932 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000933 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000934 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000935 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000936
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000937 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000938 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000939
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000940 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000941 /// \param TaskgroupOpGen Generator for the statement associated with the
942 /// given taskgroup region.
943 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
944 const RegionCodeGenTy &TaskgroupOpGen,
945 SourceLocation Loc);
946
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000947 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000948 /// \param SingleOpGen Generator for the statement associated with the given
949 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000950 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000951 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000952 SourceLocation Loc,
953 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000954 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000955 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000956 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000957
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000958 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000959 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000960 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000961 virtual void emitOrderedRegion(CodeGenFunction &CGF,
962 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000963 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000964
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000965 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +0000966 /// \param Kind Directive for which this implicit barrier call must be
967 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000968 /// \param EmitChecks true if need to emit checks for cancellation barriers.
969 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
970 /// runtime class decides which one to emit (simple or with cancellation
971 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000972 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000973 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000974 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000975 bool EmitChecks = true,
976 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000977
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000978 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +0000979 /// This kind of worksharing directive is emitted without outer loop.
980 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
981 /// \param Chunked True if chunk is specified in the clause.
982 ///
983 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
984 bool Chunked) const;
985
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000986 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000987 /// This kind of distribute directive is emitted without outer loop.
988 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
989 /// \param Chunked True if chunk is specified in the clause.
990 ///
991 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
992 bool Chunked) const;
993
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000994 /// Check if the specified \a ScheduleKind is static chunked.
995 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
996 /// \param Chunked True if chunk is specified in the clause.
997 ///
998 virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
999 bool Chunked) const;
1000
1001 /// Check if the specified \a ScheduleKind is static non-chunked.
1002 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1003 /// \param Chunked True if chunk is specified in the clause.
1004 ///
1005 virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
1006 bool Chunked) const;
1007
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001008 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001009 /// This kind of worksharing directive is emitted without outer loop.
1010 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
1011 ///
1012 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
1013
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001014 /// struct with the values to be passed to the dispatch runtime function
1015 struct DispatchRTInput {
1016 /// Loop lower bound
1017 llvm::Value *LB = nullptr;
1018 /// Loop upper bound
1019 llvm::Value *UB = nullptr;
1020 /// Chunk size specified using 'schedule' clause (nullptr if chunk
1021 /// was not specified)
1022 llvm::Value *Chunk = nullptr;
1023 DispatchRTInput() = default;
1024 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
1025 : LB(LB), UB(UB), Chunk(Chunk) {}
1026 };
1027
1028 /// Call the appropriate runtime routine to initialize it before start
1029 /// of loop.
1030
1031 /// This is used for non static scheduled types and when the ordered
1032 /// clause is present on the loop construct.
1033 /// Depending on the loop schedule, it is necessary to call some runtime
1034 /// routine before start of the OpenMP loop to get the loop upper / lower
1035 /// bounds \a LB and \a UB and stride \a ST.
1036 ///
1037 /// \param CGF Reference to current CodeGenFunction.
1038 /// \param Loc Clang source location.
1039 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1040 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001041 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001042 /// \param Ordered true if loop is ordered, false otherwise.
1043 /// \param DispatchValues struct containing llvm values for lower bound, upper
1044 /// bound, and chunk expression.
1045 /// For the default (nullptr) value, the chunk 1 will be used.
1046 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001047 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001048 const OpenMPScheduleTy &ScheduleKind,
1049 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001050 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001051
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001052 /// Struct with the values to be passed to the static runtime function
1053 struct StaticRTInput {
1054 /// Size of the iteration variable in bits.
1055 unsigned IVSize = 0;
1056 /// Sign of the iteration variable.
1057 bool IVSigned = false;
1058 /// true if loop is ordered, false otherwise.
1059 bool Ordered = false;
1060 /// Address of the output variable in which the flag of the last iteration
1061 /// is returned.
1062 Address IL = Address::invalid();
1063 /// Address of the output variable in which the lower iteration number is
1064 /// returned.
1065 Address LB = Address::invalid();
1066 /// Address of the output variable in which the upper iteration number is
1067 /// returned.
1068 Address UB = Address::invalid();
1069 /// Address of the output variable in which the stride value is returned
1070 /// necessary to generated the static_chunked scheduled loop.
1071 Address ST = Address::invalid();
1072 /// Value of the chunk for the static_chunked scheduled loop. For the
1073 /// default (nullptr) value, the chunk 1 will be used.
1074 llvm::Value *Chunk = nullptr;
1075 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1076 Address LB, Address UB, Address ST,
1077 llvm::Value *Chunk = nullptr)
1078 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1079 UB(UB), ST(ST), Chunk(Chunk) {}
1080 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001081 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +00001082 /// of loop.
1083 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001084 /// This is used only in case of static schedule, when the user did not
1085 /// specify a ordered clause on the loop construct.
1086 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +00001087 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001088 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +00001089 ///
1090 /// \param CGF Reference to current CodeGenFunction.
1091 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001092 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001093 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001094 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +00001095 ///
John McCall7f416cc2015-09-08 08:05:57 +00001096 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001097 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001098 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001099 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00001100
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001101 ///
1102 /// \param CGF Reference to current CodeGenFunction.
1103 /// \param Loc Clang source location.
1104 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001105 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001106 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001107 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1108 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001109 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001110 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001111
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001112 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001113 /// iteration of the ordered loop with the dynamic scheduling.
1114 ///
1115 /// \param CGF Reference to current CodeGenFunction.
1116 /// \param Loc Clang source location.
1117 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001118 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001119 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001120 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1121 SourceLocation Loc, unsigned IVSize,
1122 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001123
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001124 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001125 /// all the work with current loop.
1126 ///
1127 /// \param CGF Reference to current CodeGenFunction.
1128 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001129 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001130 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001131 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1132 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001133
Alexander Musman92bdaab2015-03-12 13:37:50 +00001134 /// Call __kmpc_dispatch_next(
1135 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1136 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1137 /// kmp_int[32|64] *p_stride);
1138 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001139 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001140 /// \param IL Address of the output variable in which the flag of the
1141 /// last iteration is returned.
1142 /// \param LB Address of the output variable in which the lower iteration
1143 /// number is returned.
1144 /// \param UB Address of the output variable in which the upper iteration
1145 /// number is returned.
1146 /// \param ST Address of the output variable in which the stride value is
1147 /// returned.
1148 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1149 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001150 Address IL, Address LB,
1151 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001152
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001153 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001154 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1155 /// clause.
1156 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001157 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1158 llvm::Value *NumThreads,
1159 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001160
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001161 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001162 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1163 virtual void emitProcBindClause(CodeGenFunction &CGF,
1164 OpenMPProcBindClauseKind ProcBind,
1165 SourceLocation Loc);
1166
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001167 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001168 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001169 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001170 /// \param VDAddr Address of the global variable \a VD.
1171 /// \param Loc Location of the reference to threadprivate var.
1172 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001173 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1174 const VarDecl *VD,
1175 Address VDAddr,
1176 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001177
Alexey Bataev92327c52018-03-26 16:40:55 +00001178 /// Returns the address of the variable marked as declare target with link
Gheorghe-Teodor Bercea0034e842019-06-20 18:04:47 +00001179 /// clause OR as declare target with to clause and unified memory.
1180 virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001181
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001182 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001183 /// a call to runtime library which adds initial value to the newly created
1184 /// threadprivate variable (if it is not constant) and registers destructor
1185 /// for the variable (if any).
1186 /// \param VD Threadprivate variable.
1187 /// \param VDAddr Address of the global variable \a VD.
1188 /// \param Loc Location of threadprivate declaration.
1189 /// \param PerformInit true if initialization expression is not constant.
1190 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001191 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001192 SourceLocation Loc, bool PerformInit,
1193 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001194
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001195 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001196 /// \param VD Declare target variable.
1197 /// \param Addr Address of the global variable \a VD.
1198 /// \param PerformInit true if initialization expression is not constant.
1199 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1200 llvm::GlobalVariable *Addr,
1201 bool PerformInit);
1202
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001203 /// Creates artificial threadprivate variable with name \p Name and type \p
1204 /// VarType.
1205 /// \param VarType Type of the artificial threadprivate variable.
1206 /// \param Name Name of the artificial threadprivate variable.
1207 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1208 QualType VarType,
1209 StringRef Name);
1210
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001211 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001212 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001213 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1214 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001216 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001217 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001218 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1219 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1220 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1221 /// function:
1222 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1223 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1224 /// return 0;
1225 /// }
1226 /// 2. Copy a list of shared variables to field shareds of the resulting
1227 /// structure kmp_task_t returned by the previous call (if any).
1228 /// 3. Copy a pointer to destructions function to field destructions of the
1229 /// resulting structure kmp_task_t.
1230 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1231 /// kmp_task_t *new_task), where new_task is a resulting structure from
1232 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001233 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001234 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1235 /// /*part_id*/, captured_struct */*__context*/);
1236 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001237 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001238 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001239 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1240 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001241 /// \param Data Additional data for task generation like tiednsee, final
1242 /// state, list of privates etc.
1243 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1244 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001245 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001246 Address Shareds, const Expr *IfCond,
1247 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001248
Alexey Bataev7292c292016-04-25 12:22:29 +00001249 /// Emit task region for the taskloop directive. The taskloop region is
1250 /// emitted in several steps:
1251 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1252 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1253 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1254 /// function:
1255 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1256 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1257 /// return 0;
1258 /// }
1259 /// 2. Copy a list of shared variables to field shareds of the resulting
1260 /// structure kmp_task_t returned by the previous call (if any).
1261 /// 3. Copy a pointer to destructions function to field destructions of the
1262 /// resulting structure kmp_task_t.
1263 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1264 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1265 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1266 /// is a resulting structure from
1267 /// previous items.
1268 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001269 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1270 /// /*part_id*/, captured_struct */*__context*/);
1271 /// \param SharedsTy A type which contains references the shared variables.
1272 /// \param Shareds Context with the list of shared variables from the \p
1273 /// TaskFunction.
1274 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1275 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001276 /// \param Data Additional data for task generation like tiednsee, final
1277 /// state, list of privates etc.
James Y Knight9871db02019-02-05 16:42:33 +00001278 virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1279 const OMPLoopDirective &D,
1280 llvm::Function *TaskFunction,
1281 QualType SharedsTy, Address Shareds,
1282 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001283
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001284 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001285 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001286 /// \param InnermostKind Kind of innermost directive (for simple directives it
1287 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001288 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001289 /// \param HasCancel true if region has inner cancel directive, false
1290 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001291 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001292 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001293 const RegionCodeGenTy &CodeGen,
1294 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001295
1296 /// Emits reduction function.
1297 /// \param ArgsType Array type containing pointers to reduction variables.
1298 /// \param Privates List of private copies for original reduction arguments.
1299 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1300 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1301 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1302 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev982a35e2019-03-19 17:09:52 +00001303 llvm::Function *emitReductionFunction(SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001304 llvm::Type *ArgsType,
1305 ArrayRef<const Expr *> Privates,
1306 ArrayRef<const Expr *> LHSExprs,
1307 ArrayRef<const Expr *> RHSExprs,
1308 ArrayRef<const Expr *> ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001309
1310 /// Emits single reduction combiner
1311 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1312 const Expr *ReductionOp,
1313 const Expr *PrivateRef,
1314 const DeclRefExpr *LHS,
1315 const DeclRefExpr *RHS);
1316
1317 struct ReductionOptionsTy {
1318 bool WithNowait;
1319 bool SimpleReduction;
1320 OpenMPDirectiveKind ReductionKind;
1321 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001322 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001323 /// reduction:
1324 /// \code
1325 ///
1326 /// static kmp_critical_name lock = { 0 };
1327 ///
1328 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1329 /// ...
1330 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1331 /// ...
1332 /// }
1333 ///
1334 /// ...
1335 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1336 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1337 /// RedList, reduce_func, &<lock>)) {
1338 /// case 1:
1339 /// ...
1340 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1341 /// ...
1342 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1343 /// break;
1344 /// case 2:
1345 /// ...
1346 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1347 /// ...
1348 /// break;
1349 /// default:;
1350 /// }
1351 /// \endcode
1352 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001353 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001354 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1355 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1356 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1357 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001358 /// \param Options List of options for reduction codegen:
1359 /// WithNowait true if parent directive has also nowait clause, false
1360 /// otherwise.
1361 /// SimpleReduction Emit reduction operation only. Used for omp simd
1362 /// directive on the host.
1363 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001364 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001365 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001366 ArrayRef<const Expr *> LHSExprs,
1367 ArrayRef<const Expr *> RHSExprs,
1368 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001369 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001370
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001371 /// Emit a code for initialization of task reduction clause. Next code
1372 /// should be emitted for reduction:
1373 /// \code
1374 ///
1375 /// _task_red_item_t red_data[n];
1376 /// ...
1377 /// red_data[i].shar = &origs[i];
1378 /// red_data[i].size = sizeof(origs[i]);
1379 /// red_data[i].f_init = (void*)RedInit<i>;
1380 /// red_data[i].f_fini = (void*)RedDest<i>;
1381 /// red_data[i].f_comb = (void*)RedOp<i>;
1382 /// red_data[i].flags = <Flag_i>;
1383 /// ...
1384 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1385 /// \endcode
1386 ///
1387 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1388 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1389 /// \param Data Additional data for task generation like tiedness, final
1390 /// state, list of privates, reductions etc.
1391 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1392 SourceLocation Loc,
1393 ArrayRef<const Expr *> LHSExprs,
1394 ArrayRef<const Expr *> RHSExprs,
1395 const OMPTaskDataTy &Data);
1396
1397 /// Required to resolve existing problems in the runtime. Emits threadprivate
1398 /// variables to store the size of the VLAs/array sections for
1399 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1400 /// store the pointer to the original reduction item for the custom
1401 /// initializer defined by declare reduction construct.
1402 /// \param RCG Allows to reuse an existing data for the reductions.
1403 /// \param N Reduction item for which fixups must be emitted.
1404 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1405 ReductionCodeGen &RCG, unsigned N);
1406
1407 /// Get the address of `void *` type of the privatue copy of the reduction
1408 /// item specified by the \p SharedLVal.
1409 /// \param ReductionsPtr Pointer to the reduction data returned by the
1410 /// emitTaskReductionInit function.
1411 /// \param SharedLVal Address of the original reduction item.
1412 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1413 llvm::Value *ReductionsPtr,
1414 LValue SharedLVal);
1415
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001416 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001417 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001418
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001419 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001420 /// \param CancelRegion Region kind for which the cancellation point must be
1421 /// emitted.
1422 ///
1423 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1424 SourceLocation Loc,
1425 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001426
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001427 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001428 /// \param IfCond Condition in the associated 'if' clause, if it was
1429 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001430 /// \param CancelRegion Region kind for which the cancel must be emitted.
1431 ///
1432 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001433 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001434 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001435
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001436 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001437 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001438 /// \param ParentName Name of the function that encloses the target region.
1439 /// \param OutlinedFn Outlined function value to be defined by this call.
1440 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1441 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001442 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001443 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001444 /// evaluates to false.
1445 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1446 StringRef ParentName,
1447 llvm::Function *&OutlinedFn,
1448 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001449 bool IsOffloadEntry,
1450 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001451
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001452 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001453 /// code attempts offloading the execution to the device, an the event of
1454 /// a failure it executes the host version outlined in \a OutlinedFn.
1455 /// \param D Directive to emit.
1456 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001457 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001458 /// \param IfCond Expression evaluated in if clause associated with the target
1459 /// directive, or null if no if clause is used.
1460 /// \param Device Expression evaluated in device clause associated with the
1461 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00001462 /// \param SizeEmitter Callback to emit number of iterations for loop-based
1463 /// directives.
1464 virtual void
1465 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1466 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
1467 const Expr *IfCond, const Expr *Device,
1468 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
1469 const OMPLoopDirective &D)>
1470 SizeEmitter);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001471
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001472 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001473 /// the function itself in case it is a valid device function. Returns true if
1474 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001475 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001476 virtual bool emitTargetFunctions(GlobalDecl GD);
1477
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001478 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001479 /// Returns true if \a GD was dealt with successfully.
1480 /// \param GD Variable declaration to emit.
1481 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1482
Alexey Bataev03f270c2018-03-30 18:31:07 +00001483 /// Checks if the provided global decl \a GD is a declare target variable and
1484 /// registers it when emitting code for the host.
1485 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1486 llvm::Constant *Addr);
1487
Alexey Bataev1af5bd52019-03-05 17:47:18 +00001488 /// Registers provided target firstprivate variable as global on the
1489 /// target.
1490 llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1491 const VarDecl *VD);
1492
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001493 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001494 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001495 /// \param GD Global to scan.
1496 virtual bool emitTargetGlobal(GlobalDecl GD);
1497
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001498 /// Creates and returns a registration function for when at least one
1499 /// requires directives was used in the current module.
1500 llvm::Function *emitRequiresDirectiveRegFun();
1501
Sergey Dmitriev5836c352019-10-15 18:42:47 +00001502 /// Creates all the offload entries in the current compilation unit
1503 /// along with the associated metadata.
1504 void createOffloadEntriesAndInfoMetadata();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001505
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001506 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001507 /// variables captured in a record which address is stored in \a
1508 /// CapturedStruct.
1509 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1510 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1511 /// \param CapturedVars A pointer to the record with the references to
1512 /// variables used in \a OutlinedFn function.
1513 ///
1514 virtual void emitTeamsCall(CodeGenFunction &CGF,
1515 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001516 SourceLocation Loc, llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001517 ArrayRef<llvm::Value *> CapturedVars);
1518
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001519 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001520 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1521 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001522 /// \param NumTeams An integer expression of teams.
1523 /// \param ThreadLimit An integer expression of threads.
1524 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1525 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001526
Samuel Antaocc10b852016-07-28 14:23:26 +00001527 /// Struct that keeps all the relevant information that should be kept
1528 /// throughout a 'target data' region.
1529 class TargetDataInfo {
1530 /// Set to true if device pointer information have to be obtained.
1531 bool RequiresDevicePointerInfo = false;
1532
1533 public:
1534 /// The array of base pointer passed to the runtime library.
1535 llvm::Value *BasePointersArray = nullptr;
1536 /// The array of section pointers passed to the runtime library.
1537 llvm::Value *PointersArray = nullptr;
1538 /// The array of sizes passed to the runtime library.
1539 llvm::Value *SizesArray = nullptr;
1540 /// The array of map types passed to the runtime library.
1541 llvm::Value *MapTypesArray = nullptr;
1542 /// The total number of pointers passed to the runtime library.
1543 unsigned NumberOfPtrs = 0u;
1544 /// Map between the a declaration of a capture and the corresponding base
1545 /// pointer address where the runtime returns the device pointers.
1546 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1547
1548 explicit TargetDataInfo() {}
1549 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1550 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1551 /// Clear information about the data arrays.
1552 void clearArrayInfo() {
1553 BasePointersArray = nullptr;
1554 PointersArray = nullptr;
1555 SizesArray = nullptr;
1556 MapTypesArray = nullptr;
1557 NumberOfPtrs = 0u;
1558 }
1559 /// Return true if the current target data information has valid arrays.
1560 bool isValid() {
1561 return BasePointersArray && PointersArray && SizesArray &&
1562 MapTypesArray && NumberOfPtrs;
1563 }
1564 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1565 };
1566
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001567 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001568 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001569 /// \param IfCond Expression evaluated in if clause associated with the
1570 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001571 /// \param Device Expression evaluated in device clause associated with the
1572 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001573 /// \param Info A record used to store information that needs to be preserved
1574 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001575 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1576 const OMPExecutableDirective &D,
1577 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001578 const RegionCodeGenTy &CodeGen,
1579 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001580
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001581 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001582 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001583 /// \param D Directive to emit.
1584 /// \param IfCond Expression evaluated in if clause associated with the target
1585 /// directive, or null if no if clause is used.
1586 /// \param Device Expression evaluated in device clause associated with the
1587 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001588 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1589 const OMPExecutableDirective &D,
1590 const Expr *IfCond,
1591 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001592
1593 /// Marks function \a Fn with properly mangled versions of vector functions.
1594 /// \param FD Function marked as 'declare simd'.
1595 /// \param Fn LLVM function that must be marked with 'declare simd'
1596 /// attributes.
1597 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1598 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001599
1600 /// Emit initialization for doacross loop nesting support.
1601 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001602 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1603 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001604
1605 /// Emit code for doacross ordered directive with 'depend' clause.
1606 /// \param C 'depend' clause with 'sink|source' dependency kind.
1607 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1608 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001609
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001610 /// Translates the native parameter of outlined function if this is required
1611 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001612 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001613 /// \param NativeParam Parameter itself.
1614 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1615 const VarDecl *NativeParam) const {
1616 return NativeParam;
1617 }
1618
1619 /// Gets the address of the native argument basing on the address of the
1620 /// target-specific parameter.
1621 /// \param NativeParam Parameter itself.
1622 /// \param TargetParam Corresponding target-specific parameter.
1623 virtual Address getParameterAddress(CodeGenFunction &CGF,
1624 const VarDecl *NativeParam,
1625 const VarDecl *TargetParam) const;
1626
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001627 /// Choose default schedule type and chunk value for the
1628 /// dist_schedule clause.
1629 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1630 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1631 llvm::Value *&Chunk) const {}
1632
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001633 /// Choose default schedule type and chunk value for the
1634 /// schedule clause.
1635 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1636 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
Alexey Bataevf6a53d62019-03-18 18:40:00 +00001637 const Expr *&ChunkExpr) const;
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001638
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001639 /// Emits call of the outlined function with the provided arguments,
1640 /// translating these arguments to correct target-specific arguments.
1641 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001642 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001643 llvm::FunctionCallee OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001644 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001645
1646 /// Emits OpenMP-specific function prolog.
1647 /// Required for device constructs.
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001648 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001649
1650 /// Gets the OpenMP-specific address of the local variable.
1651 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1652 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001653
Raphael Isemannb23ccec2018-12-10 12:37:46 +00001654 /// Marks the declaration as already emitted for the device code and returns
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001655 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001656 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001657
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001658 /// Emit deferred declare target variables marked for deferred emission.
1659 void emitDeferredTargetDecls() const;
Alexey Bataev60705422018-10-30 15:50:12 +00001660
1661 /// Adjust some parameters for the target-based directives, like addresses of
1662 /// the variables captured by reference in lambdas.
1663 virtual void
1664 adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
1665 const OMPExecutableDirective &D) const;
Patrick Lyster8f7f5862018-11-19 15:09:33 +00001666
1667 /// Perform check on requires decl to ensure that target architecture
1668 /// supports unified addressing
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001669 virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D);
Alexey Bataevc5687252019-03-21 19:35:27 +00001670
1671 /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1672 /// the predefined allocator and translates it into the corresponding address
1673 /// space.
1674 virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00001675
1676 /// Return whether the unified_shared_memory has been specified.
1677 bool hasRequiresUnifiedSharedMemory() const;
Alexey Bataev2df5f122019-10-01 20:18:32 +00001678
1679 /// Emits the definition of the declare variant function.
1680 virtual bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition);
Alexey Bataev0860db92019-12-19 10:01:10 -05001681
1682 /// Checks if the \p VD variable is marked as nontemporal declaration in
1683 /// current context.
1684 bool isNontemporalDecl(const ValueDecl *VD) const;
Alexey Bataev9959db52014-05-06 10:08:46 +00001685};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001686
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001687/// Class supports emissionof SIMD-only code.
1688class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1689public:
1690 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1691 ~CGOpenMPSIMDRuntime() override {}
1692
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001693 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001694 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1695 /// kmp_int32 BoundID, struct context_vars*).
1696 /// \param D OpenMP directive.
1697 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1698 /// \param InnermostKind Kind of innermost directive (for simple directives it
1699 /// is a directive itself, for combined - its innermost directive).
1700 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001701 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001702 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1703 const VarDecl *ThreadIDVar,
1704 OpenMPDirectiveKind InnermostKind,
1705 const RegionCodeGenTy &CodeGen) override;
1706
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001707 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001708 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1709 /// kmp_int32 BoundID, struct context_vars*).
1710 /// \param D OpenMP directive.
1711 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1712 /// \param InnermostKind Kind of innermost directive (for simple directives it
1713 /// is a directive itself, for combined - its innermost directive).
1714 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001715 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001716 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1717 const VarDecl *ThreadIDVar,
1718 OpenMPDirectiveKind InnermostKind,
1719 const RegionCodeGenTy &CodeGen) override;
1720
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001721 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001722 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1723 /// TaskT).
1724 /// \param D OpenMP directive.
1725 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1726 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1727 /// task region.
1728 /// \param TaskTVar Variable for task_t argument.
1729 /// \param InnermostKind Kind of innermost directive (for simple directives it
1730 /// is a directive itself, for combined - its innermost directive).
1731 /// \param CodeGen Code generation sequence for the \a D directive.
1732 /// \param Tied true if task is generated for tied task, false otherwise.
1733 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1734 /// tasks.
1735 ///
James Y Knight9871db02019-02-05 16:42:33 +00001736 llvm::Function *emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001737 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1738 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1739 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1740 bool Tied, unsigned &NumberOfParts) override;
1741
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001742 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001743 /// variables captured in a record which address is stored in \a
1744 /// CapturedStruct.
1745 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1746 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1747 /// \param CapturedVars A pointer to the record with the references to
1748 /// variables used in \a OutlinedFn function.
1749 /// \param IfCond Condition in the associated 'if' clause, if it was
1750 /// specified, nullptr otherwise.
1751 ///
1752 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001753 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001754 ArrayRef<llvm::Value *> CapturedVars,
1755 const Expr *IfCond) override;
1756
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001757 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001758 /// \param CriticalName Name of the critical region.
1759 /// \param CriticalOpGen Generator for the statement associated with the given
1760 /// critical region.
1761 /// \param Hint Value of the 'hint' clause (optional).
1762 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1763 const RegionCodeGenTy &CriticalOpGen,
1764 SourceLocation Loc,
1765 const Expr *Hint = nullptr) override;
1766
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001767 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001768 /// \param MasterOpGen Generator for the statement associated with the given
1769 /// master region.
1770 void emitMasterRegion(CodeGenFunction &CGF,
1771 const RegionCodeGenTy &MasterOpGen,
1772 SourceLocation Loc) override;
1773
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001774 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001775 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1776
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001777 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001778 /// \param TaskgroupOpGen Generator for the statement associated with the
1779 /// given taskgroup region.
1780 void emitTaskgroupRegion(CodeGenFunction &CGF,
1781 const RegionCodeGenTy &TaskgroupOpGen,
1782 SourceLocation Loc) override;
1783
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001784 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001785 /// \param SingleOpGen Generator for the statement associated with the given
1786 /// single region.
1787 void emitSingleRegion(CodeGenFunction &CGF,
1788 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1789 ArrayRef<const Expr *> CopyprivateVars,
1790 ArrayRef<const Expr *> DestExprs,
1791 ArrayRef<const Expr *> SrcExprs,
1792 ArrayRef<const Expr *> AssignmentOps) override;
1793
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001794 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001795 /// \param OrderedOpGen Generator for the statement associated with the given
1796 /// ordered region.
1797 void emitOrderedRegion(CodeGenFunction &CGF,
1798 const RegionCodeGenTy &OrderedOpGen,
1799 SourceLocation Loc, bool IsThreads) override;
1800
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001801 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001802 /// \param Kind Directive for which this implicit barrier call must be
1803 /// generated. Must be OMPD_barrier for explicit barrier generation.
1804 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1805 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1806 /// runtime class decides which one to emit (simple or with cancellation
1807 /// checks).
1808 ///
1809 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1810 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1811 bool ForceSimpleCall = false) override;
1812
1813 /// This is used for non static scheduled types and when the ordered
1814 /// clause is present on the loop construct.
1815 /// Depending on the loop schedule, it is necessary to call some runtime
1816 /// routine before start of the OpenMP loop to get the loop upper / lower
1817 /// bounds \a LB and \a UB and stride \a ST.
1818 ///
1819 /// \param CGF Reference to current CodeGenFunction.
1820 /// \param Loc Clang source location.
1821 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1822 /// \param IVSize Size of the iteration variable in bits.
1823 /// \param IVSigned Sign of the iteration variable.
1824 /// \param Ordered true if loop is ordered, false otherwise.
1825 /// \param DispatchValues struct containing llvm values for lower bound, upper
1826 /// bound, and chunk expression.
1827 /// For the default (nullptr) value, the chunk 1 will be used.
1828 ///
1829 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1830 const OpenMPScheduleTy &ScheduleKind,
1831 unsigned IVSize, bool IVSigned, bool Ordered,
1832 const DispatchRTInput &DispatchValues) override;
1833
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001834 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001835 /// of loop.
1836 ///
1837 /// This is used only in case of static schedule, when the user did not
1838 /// specify a ordered clause on the loop construct.
1839 /// Depending on the loop schedule, it is necessary to call some runtime
1840 /// routine before start of the OpenMP loop to get the loop upper / lower
1841 /// bounds LB and UB and stride ST.
1842 ///
1843 /// \param CGF Reference to current CodeGenFunction.
1844 /// \param Loc Clang source location.
1845 /// \param DKind Kind of the directive.
1846 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1847 /// \param Values Input arguments for the construct.
1848 ///
1849 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1850 OpenMPDirectiveKind DKind,
1851 const OpenMPScheduleTy &ScheduleKind,
1852 const StaticRTInput &Values) override;
1853
1854 ///
1855 /// \param CGF Reference to current CodeGenFunction.
1856 /// \param Loc Clang source location.
1857 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1858 /// \param Values Input arguments for the construct.
1859 ///
1860 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1861 OpenMPDistScheduleClauseKind SchedKind,
1862 const StaticRTInput &Values) override;
1863
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001864 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001865 /// iteration of the ordered loop with the dynamic scheduling.
1866 ///
1867 /// \param CGF Reference to current CodeGenFunction.
1868 /// \param Loc Clang source location.
1869 /// \param IVSize Size of the iteration variable in bits.
1870 /// \param IVSigned Sign of the iteration variable.
1871 ///
1872 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1873 unsigned IVSize, bool IVSigned) override;
1874
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001875 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001876 /// all the work with current loop.
1877 ///
1878 /// \param CGF Reference to current CodeGenFunction.
1879 /// \param Loc Clang source location.
1880 /// \param DKind Kind of the directive for which the static finish is emitted.
1881 ///
1882 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1883 OpenMPDirectiveKind DKind) override;
1884
1885 /// Call __kmpc_dispatch_next(
1886 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1887 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1888 /// kmp_int[32|64] *p_stride);
1889 /// \param IVSize Size of the iteration variable in bits.
1890 /// \param IVSigned Sign of the iteration variable.
1891 /// \param IL Address of the output variable in which the flag of the
1892 /// last iteration is returned.
1893 /// \param LB Address of the output variable in which the lower iteration
1894 /// number is returned.
1895 /// \param UB Address of the output variable in which the upper iteration
1896 /// number is returned.
1897 /// \param ST Address of the output variable in which the stride value is
1898 /// returned.
1899 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1900 unsigned IVSize, bool IVSigned, Address IL,
1901 Address LB, Address UB, Address ST) override;
1902
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001903 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001904 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1905 /// clause.
1906 /// \param NumThreads An integer value of threads.
1907 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1908 SourceLocation Loc) override;
1909
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001910 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001911 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1912 void emitProcBindClause(CodeGenFunction &CGF,
1913 OpenMPProcBindClauseKind ProcBind,
1914 SourceLocation Loc) override;
1915
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001916 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001917 /// thread.
1918 /// \param VD Threadprivate variable.
1919 /// \param VDAddr Address of the global variable \a VD.
1920 /// \param Loc Location of the reference to threadprivate var.
1921 /// \return Address of the threadprivate variable for the current thread.
1922 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1923 Address VDAddr, SourceLocation Loc) override;
1924
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001925 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001926 /// a call to runtime library which adds initial value to the newly created
1927 /// threadprivate variable (if it is not constant) and registers destructor
1928 /// for the variable (if any).
1929 /// \param VD Threadprivate variable.
1930 /// \param VDAddr Address of the global variable \a VD.
1931 /// \param Loc Location of threadprivate declaration.
1932 /// \param PerformInit true if initialization expression is not constant.
1933 llvm::Function *
1934 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1935 SourceLocation Loc, bool PerformInit,
1936 CodeGenFunction *CGF = nullptr) override;
1937
1938 /// Creates artificial threadprivate variable with name \p Name and type \p
1939 /// VarType.
1940 /// \param VarType Type of the artificial threadprivate variable.
1941 /// \param Name Name of the artificial threadprivate variable.
1942 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1943 QualType VarType,
1944 StringRef Name) override;
1945
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001946 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001947 /// \param Vars List of variables to flush.
1948 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1949 SourceLocation Loc) override;
1950
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001951 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001952 /// emitted in several steps:
1953 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1954 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1955 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1956 /// function:
1957 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1958 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1959 /// return 0;
1960 /// }
1961 /// 2. Copy a list of shared variables to field shareds of the resulting
1962 /// structure kmp_task_t returned by the previous call (if any).
1963 /// 3. Copy a pointer to destructions function to field destructions of the
1964 /// resulting structure kmp_task_t.
1965 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1966 /// kmp_task_t *new_task), where new_task is a resulting structure from
1967 /// previous items.
1968 /// \param D Current task directive.
1969 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1970 /// /*part_id*/, captured_struct */*__context*/);
1971 /// \param SharedsTy A type which contains references the shared variables.
1972 /// \param Shareds Context with the list of shared variables from the \p
1973 /// TaskFunction.
1974 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1975 /// otherwise.
1976 /// \param Data Additional data for task generation like tiednsee, final
1977 /// state, list of privates etc.
1978 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001979 const OMPExecutableDirective &D,
1980 llvm::Function *TaskFunction, QualType SharedsTy,
1981 Address Shareds, const Expr *IfCond,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001982 const OMPTaskDataTy &Data) override;
1983
1984 /// Emit task region for the taskloop directive. The taskloop region is
1985 /// emitted in several steps:
1986 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1987 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1988 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1989 /// function:
1990 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1991 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1992 /// return 0;
1993 /// }
1994 /// 2. Copy a list of shared variables to field shareds of the resulting
1995 /// structure kmp_task_t returned by the previous call (if any).
1996 /// 3. Copy a pointer to destructions function to field destructions of the
1997 /// resulting structure kmp_task_t.
1998 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1999 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
2000 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
2001 /// is a resulting structure from
2002 /// previous items.
2003 /// \param D Current task directive.
2004 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2005 /// /*part_id*/, captured_struct */*__context*/);
2006 /// \param SharedsTy A type which contains references the shared variables.
2007 /// \param Shareds Context with the list of shared variables from the \p
2008 /// TaskFunction.
2009 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2010 /// otherwise.
2011 /// \param Data Additional data for task generation like tiednsee, final
2012 /// state, list of privates etc.
2013 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002014 const OMPLoopDirective &D, llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002015 QualType SharedsTy, Address Shareds, const Expr *IfCond,
2016 const OMPTaskDataTy &Data) override;
2017
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002018 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002019 /// reduction:
2020 /// \code
2021 ///
2022 /// static kmp_critical_name lock = { 0 };
2023 ///
2024 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
2025 /// ...
2026 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
2027 /// ...
2028 /// }
2029 ///
2030 /// ...
2031 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
2032 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
2033 /// RedList, reduce_func, &<lock>)) {
2034 /// case 1:
2035 /// ...
2036 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
2037 /// ...
2038 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
2039 /// break;
2040 /// case 2:
2041 /// ...
2042 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
2043 /// ...
2044 /// break;
2045 /// default:;
2046 /// }
2047 /// \endcode
2048 ///
2049 /// \param Privates List of private copies for original reduction arguments.
2050 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
2051 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
2052 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
2053 /// or 'operator binop(LHS, RHS)'.
2054 /// \param Options List of options for reduction codegen:
2055 /// WithNowait true if parent directive has also nowait clause, false
2056 /// otherwise.
2057 /// SimpleReduction Emit reduction operation only. Used for omp simd
2058 /// directive on the host.
2059 /// ReductionKind The kind of reduction to perform.
2060 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2061 ArrayRef<const Expr *> Privates,
2062 ArrayRef<const Expr *> LHSExprs,
2063 ArrayRef<const Expr *> RHSExprs,
2064 ArrayRef<const Expr *> ReductionOps,
2065 ReductionOptionsTy Options) override;
2066
2067 /// Emit a code for initialization of task reduction clause. Next code
2068 /// should be emitted for reduction:
2069 /// \code
2070 ///
2071 /// _task_red_item_t red_data[n];
2072 /// ...
2073 /// red_data[i].shar = &origs[i];
2074 /// red_data[i].size = sizeof(origs[i]);
2075 /// red_data[i].f_init = (void*)RedInit<i>;
2076 /// red_data[i].f_fini = (void*)RedDest<i>;
2077 /// red_data[i].f_comb = (void*)RedOp<i>;
2078 /// red_data[i].flags = <Flag_i>;
2079 /// ...
2080 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2081 /// \endcode
2082 ///
2083 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2084 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2085 /// \param Data Additional data for task generation like tiedness, final
2086 /// state, list of privates, reductions etc.
2087 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2088 ArrayRef<const Expr *> LHSExprs,
2089 ArrayRef<const Expr *> RHSExprs,
2090 const OMPTaskDataTy &Data) override;
2091
2092 /// Required to resolve existing problems in the runtime. Emits threadprivate
2093 /// variables to store the size of the VLAs/array sections for
2094 /// initializer/combiner/finalizer functions + emits threadprivate variable to
2095 /// store the pointer to the original reduction item for the custom
2096 /// initializer defined by declare reduction construct.
2097 /// \param RCG Allows to reuse an existing data for the reductions.
2098 /// \param N Reduction item for which fixups must be emitted.
2099 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2100 ReductionCodeGen &RCG, unsigned N) override;
2101
2102 /// Get the address of `void *` type of the privatue copy of the reduction
2103 /// item specified by the \p SharedLVal.
2104 /// \param ReductionsPtr Pointer to the reduction data returned by the
2105 /// emitTaskReductionInit function.
2106 /// \param SharedLVal Address of the original reduction item.
2107 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2108 llvm::Value *ReductionsPtr,
2109 LValue SharedLVal) override;
2110
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002111 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002112 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2113
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002114 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002115 /// \param CancelRegion Region kind for which the cancellation point must be
2116 /// emitted.
2117 ///
2118 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2119 OpenMPDirectiveKind CancelRegion) override;
2120
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002121 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002122 /// \param IfCond Condition in the associated 'if' clause, if it was
2123 /// specified, nullptr otherwise.
2124 /// \param CancelRegion Region kind for which the cancel must be emitted.
2125 ///
2126 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2127 const Expr *IfCond,
2128 OpenMPDirectiveKind CancelRegion) override;
2129
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002130 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002131 /// \param D Directive to emit.
2132 /// \param ParentName Name of the function that encloses the target region.
2133 /// \param OutlinedFn Outlined function value to be defined by this call.
2134 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2135 /// \param IsOffloadEntry True if the outlined function is an offload entry.
2136 /// \param CodeGen Code generation sequence for the \a D directive.
2137 /// An outlined function may not be an entry if, e.g. the if clause always
2138 /// evaluates to false.
2139 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
2140 StringRef ParentName,
2141 llvm::Function *&OutlinedFn,
2142 llvm::Constant *&OutlinedFnID,
2143 bool IsOffloadEntry,
2144 const RegionCodeGenTy &CodeGen) override;
2145
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002146 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002147 /// code attempts offloading the execution to the device, an the event of
2148 /// a failure it executes the host version outlined in \a OutlinedFn.
2149 /// \param D Directive to emit.
2150 /// \param OutlinedFn Host version of the code to be offloaded.
2151 /// \param OutlinedFnID ID of host version of the code to be offloaded.
2152 /// \param IfCond Expression evaluated in if clause associated with the target
2153 /// directive, or null if no if clause is used.
2154 /// \param Device Expression evaluated in device clause associated with the
2155 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00002156 void
2157 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2158 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
2159 const Expr *IfCond, const Expr *Device,
2160 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2161 const OMPLoopDirective &D)>
2162 SizeEmitter) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002163
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002164 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002165 /// the function itself in case it is a valid device function. Returns true if
2166 /// \a GD was dealt with successfully.
2167 /// \param GD Function to scan.
2168 bool emitTargetFunctions(GlobalDecl GD) override;
2169
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002170 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002171 /// Returns true if \a GD was dealt with successfully.
2172 /// \param GD Variable declaration to emit.
2173 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2174
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002175 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002176 /// if it was emitted successfully.
2177 /// \param GD Global to scan.
2178 bool emitTargetGlobal(GlobalDecl GD) override;
2179
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002180 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002181 /// variables captured in a record which address is stored in \a
2182 /// CapturedStruct.
2183 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2184 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2185 /// \param CapturedVars A pointer to the record with the references to
2186 /// variables used in \a OutlinedFn function.
2187 ///
2188 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002189 SourceLocation Loc, llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002190 ArrayRef<llvm::Value *> CapturedVars) override;
2191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002192 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002193 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2194 /// for num_teams clause.
2195 /// \param NumTeams An integer expression of teams.
2196 /// \param ThreadLimit An integer expression of threads.
2197 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2198 const Expr *ThreadLimit, SourceLocation Loc) override;
2199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002200 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002201 /// \param D Directive to emit.
2202 /// \param IfCond Expression evaluated in if clause associated with the
2203 /// target directive, or null if no device clause is used.
2204 /// \param Device Expression evaluated in device clause associated with the
2205 /// target directive, or null if no device clause is used.
2206 /// \param Info A record used to store information that needs to be preserved
2207 /// until the region is closed.
2208 void emitTargetDataCalls(CodeGenFunction &CGF,
2209 const OMPExecutableDirective &D, const Expr *IfCond,
2210 const Expr *Device, const RegionCodeGenTy &CodeGen,
2211 TargetDataInfo &Info) override;
2212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002213 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002214 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2215 /// \param D Directive to emit.
2216 /// \param IfCond Expression evaluated in if clause associated with the target
2217 /// directive, or null if no if clause is used.
2218 /// \param Device Expression evaluated in device clause associated with the
2219 /// target directive, or null if no device clause is used.
2220 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2221 const OMPExecutableDirective &D,
2222 const Expr *IfCond,
2223 const Expr *Device) override;
2224
2225 /// Emit initialization for doacross loop nesting support.
2226 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002227 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2228 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002229
2230 /// Emit code for doacross ordered directive with 'depend' clause.
2231 /// \param C 'depend' clause with 'sink|source' dependency kind.
2232 void emitDoacrossOrdered(CodeGenFunction &CGF,
2233 const OMPDependClause *C) override;
2234
2235 /// Translates the native parameter of outlined function if this is required
2236 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002237 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002238 /// \param NativeParam Parameter itself.
2239 const VarDecl *translateParameter(const FieldDecl *FD,
2240 const VarDecl *NativeParam) const override;
2241
2242 /// Gets the address of the native argument basing on the address of the
2243 /// target-specific parameter.
2244 /// \param NativeParam Parameter itself.
2245 /// \param TargetParam Corresponding target-specific parameter.
2246 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2247 const VarDecl *TargetParam) const override;
Alexey Bataev4f680db2019-03-19 16:41:16 +00002248
2249 /// Gets the OpenMP-specific address of the local variable.
2250 Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2251 const VarDecl *VD) override {
2252 return Address::invalid();
2253 }
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002254};
2255
Alexey Bataev23b69422014-06-18 07:08:49 +00002256} // namespace CodeGen
2257} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002258
2259#endif