blob: bcaa06aab54a97ad8c5aa1248d4c616e47d872f3 [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"
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060025#include "llvm/Frontend/OpenMP/OMPConstants.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000026#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000027#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000028
29namespace llvm {
30class ArrayType;
31class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000032class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000033class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000034class StructType;
35class Type;
36class Value;
37} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000038
Alexey Bataev9959db52014-05-06 10:08:46 +000039namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000040class Expr;
Alexey Bataev8b427062016-05-25 12:36:08 +000041class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000042class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000043class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000044class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000045class OMPDeclareReductionDecl;
46class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000047
Alexey Bataev9959db52014-05-06 10:08:46 +000048namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000049class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000050class CodeGenFunction;
51class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000052
Alexey Bataev14fa1c62016-03-29 05:34:15 +000053/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
54/// region.
55class PrePostActionTy {
56public:
57 explicit PrePostActionTy() {}
58 virtual void Enter(CodeGenFunction &CGF) {}
59 virtual void Exit(CodeGenFunction &CGF) {}
60 virtual ~PrePostActionTy() {}
61};
62
63/// Class provides a way to call simple version of codegen for OpenMP region, or
64/// an advanced with possible pre|post-actions in codegen.
65class RegionCodeGenTy final {
66 intptr_t CodeGen;
67 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
68 CodeGenTy Callback;
69 mutable PrePostActionTy *PrePostAction;
70 RegionCodeGenTy() = delete;
71 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
72 template <typename Callable>
73 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
74 PrePostActionTy &Action) {
75 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
76 }
77
78public:
79 template <typename Callable>
80 RegionCodeGenTy(
81 Callable &&CodeGen,
82 typename std::enable_if<
83 !std::is_same<typename std::remove_reference<Callable>::type,
84 RegionCodeGenTy>::value>::type * = nullptr)
85 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
86 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
87 PrePostAction(nullptr) {}
88 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
89 void operator()(CodeGenFunction &CGF) const;
90};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000091
Alexey Bataev24b5bae2016-04-28 09:23:51 +000092struct OMPTaskDataTy final {
93 SmallVector<const Expr *, 4> PrivateVars;
94 SmallVector<const Expr *, 4> PrivateCopies;
95 SmallVector<const Expr *, 4> FirstprivateVars;
96 SmallVector<const Expr *, 4> FirstprivateCopies;
97 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000098 SmallVector<const Expr *, 4> LastprivateVars;
99 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000100 SmallVector<const Expr *, 4> ReductionVars;
101 SmallVector<const Expr *, 4> ReductionCopies;
102 SmallVector<const Expr *, 4> ReductionOps;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000103 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
104 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
105 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000106 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000107 llvm::Value *Reductions = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000108 unsigned NumberOfParts = 0;
109 bool Tied = true;
110 bool Nogroup = false;
111};
112
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000113/// Class intended to support codegen of all kind of the reduction clauses.
114class ReductionCodeGen {
115private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000116 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000117 struct ReductionData {
118 /// Reference to the original shared item.
119 const Expr *Ref = nullptr;
120 /// Helper expression for generation of private copy.
121 const Expr *Private = nullptr;
122 /// Helper expression for generation reduction operation.
123 const Expr *ReductionOp = nullptr;
124 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
125 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
126 };
127 /// List of reduction-based clauses.
128 SmallVector<ReductionData, 4> ClausesData;
129
130 /// List of addresses of original shared variables/expressions.
131 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
132 /// Sizes of the reduction items in chars.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000133 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000134 /// Base declarations for the reduction items.
135 SmallVector<const VarDecl *, 4> BaseDecls;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000136
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000137 /// Emits lvalue for shared expression.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000138 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
139 /// Emits upper bound for shared expression (if array section).
140 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
141 /// Performs aggregate initialization.
142 /// \param N Number of reduction item in the common list.
143 /// \param PrivateAddr Address of the corresponding private item.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000144 /// \param SharedLVal Address of the original shared variable.
145 /// \param DRD Declare reduction construct used for reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000146 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000147 Address PrivateAddr, LValue SharedLVal,
148 const OMPDeclareReductionDecl *DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000149
150public:
151 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
152 ArrayRef<const Expr *> Privates,
153 ArrayRef<const Expr *> ReductionOps);
154 /// Emits lvalue for a reduction item.
155 /// \param N Number of the reduction item.
156 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
157 /// Emits the code for the variable-modified type, if required.
158 /// \param N Number of the reduction item.
159 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
160 /// Emits the code for the variable-modified type, if required.
161 /// \param N Number of the reduction item.
162 /// \param Size Size of the type in chars.
163 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
164 /// Performs initialization of the private copy for the reduction item.
165 /// \param N Number of the reduction item.
166 /// \param PrivateAddr Address of the corresponding private item.
167 /// \param DefaultInit Default initialization sequence that should be
168 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000169 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000170 void
171 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
172 LValue SharedLVal,
173 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000174 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000175 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000176 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000177 /// \param N Number of the reduction item.
178 /// \param PrivateAddr Address of the corresponding private item.
179 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000180 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000181 /// address in normal operations.
182 /// \param N Number of the reduction item.
183 /// \param PrivateAddr Address of the corresponding private item.
184 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
185 Address PrivateAddr);
186 /// Returns LValue for the reduction item.
187 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000188 /// Returns the size of the reduction item (in chars and total number of
189 /// elements in the item), or nullptr, if the size is a constant.
190 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
191 return Sizes[N];
192 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000193 /// Returns the base declaration of the reduction item.
194 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
Alexey Bataev1c44e152018-03-06 18:59:43 +0000195 /// Returns the base declaration of the reduction item.
196 const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000197 /// Returns true if the initialization of the reduction item uses initializer
198 /// from declare reduction construct.
199 bool usesReductionInitializer(unsigned N) const;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000200};
201
Alexey Bataev9959db52014-05-06 10:08:46 +0000202class CGOpenMPRuntime {
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000203public:
204 /// Allows to disable automatic handling of functions used in target regions
205 /// as those marked as `omp declare target`.
206 class DisableAutoDeclareTargetRAII {
207 CodeGenModule &CGM;
208 bool SavedShouldMarkAsGlobal;
209
210 public:
211 DisableAutoDeclareTargetRAII(CodeGenModule &CGM);
212 ~DisableAutoDeclareTargetRAII();
213 };
214
Alexey Bataev0860db92019-12-19 10:01:10 -0500215 /// Manages list of nontemporal decls for the specified directive.
216 class NontemporalDeclsRAII {
217 CodeGenModule &CGM;
218 const bool NeedToPush;
219
220 public:
221 NontemporalDeclsRAII(CodeGenModule &CGM, const OMPLoopDirective &S);
222 ~NontemporalDeclsRAII();
223 };
224
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000225protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000226 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000227 StringRef FirstSeparator, Separator;
228
229 /// Constructor allowing to redefine the name separator for the variables.
230 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
231 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000232
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000233 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000234 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000235 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000236 uint64_t Size, int32_t Flags,
237 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000238
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000239 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000240 /// \param D Directive to emit.
241 /// \param ParentName Name of the function that encloses the target region.
242 /// \param OutlinedFn Outlined function value to be defined by this call.
243 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
244 /// \param IsOffloadEntry True if the outlined function is an offload entry.
245 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000246 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000247 /// evaluates to false.
248 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
249 StringRef ParentName,
250 llvm::Function *&OutlinedFn,
251 llvm::Constant *&OutlinedFnID,
252 bool IsOffloadEntry,
253 const RegionCodeGenTy &CodeGen);
254
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000255 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000256 /// \param Flags Flags for OpenMP location.
257 ///
258 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
259 unsigned Flags = 0);
260
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000261 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000262 llvm::Type *getIdentTyPointerTy();
263
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000264 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000265 ///
266 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
267
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000268 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000269 // The name can be customized depending on the target.
270 //
271 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
272
Alexey Bataev3c595a62017-08-14 15:01:03 +0000273 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
James Y Knight9871db02019-02-05 16:42:33 +0000274 void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
275 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000276 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000277
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000278 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000279 /// stored.
280 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
281
Alexey Bataevfd006c42018-10-05 15:08:53 +0000282 void setLocThreadIdInsertPt(CodeGenFunction &CGF,
283 bool AtCurrentPoint = false);
284 void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
285
Alexey Bataevceeaa482018-11-21 21:04:34 +0000286 /// Check if the default location must be constant.
287 /// Default is false to support OMPT/OMPD.
288 virtual bool isDefaultLocationConstant() const { return false; }
289
290 /// Returns additional flags that can be stored in reserved_2 field of the
291 /// default location.
292 virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
293
Alexey Bataevc2cd2d42019-10-10 17:28:10 +0000294 /// Tries to emit declare variant function for \p OldGD from \p NewGD.
295 /// \param OrigAddr LLVM IR value for \p OldGD.
296 /// \param IsForDefinition true, if requested emission for the definition of
297 /// \p OldGD.
298 /// \returns true, was able to emit a definition function for \p OldGD, which
299 /// points to \p NewGD.
300 virtual bool tryEmitDeclareVariant(const GlobalDecl &NewGD,
301 const GlobalDecl &OldGD,
302 llvm::GlobalValue *OrigAddr,
303 bool IsForDefinition);
304
Alexey Bataevc3028ca2018-12-04 15:03:25 +0000305 /// Returns default flags for the barriers depending on the directive, for
306 /// which this barier is going to be emitted.
307 static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
308
Alexey Bataeva1166022018-11-27 21:24:54 +0000309 /// Get the LLVM type for the critical name.
310 llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
311
312 /// Returns corresponding lock object for the specified critical region
313 /// name. If the lock object does not exist it is created, otherwise the
314 /// reference to the existing copy is returned.
315 /// \param CriticalName Name of the critical region.
316 ///
317 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
318
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000319private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000320 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000321 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000322 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataevceeaa482018-11-21 21:04:34 +0000323 using FlagsTy = std::pair<unsigned, unsigned>;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000324 /// Map of flags and corresponding default locations.
Alexey Bataevceeaa482018-11-21 21:04:34 +0000325 using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
Alexey Bataev15007ba2014-05-07 06:18:01 +0000326 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000327 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000328
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000329 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000330 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000331 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000332 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
333 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000334 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000335 /// Original representation is:
336 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000337 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000338 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000339 struct DebugLocThreadIdTy {
340 llvm::Value *DebugLoc;
341 llvm::Value *ThreadID;
Alexey Bataevfd006c42018-10-05 15:08:53 +0000342 /// Insert point for the service instructions.
343 llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000344 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000345 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000346 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
347 OpenMPLocThreadIDMapTy;
348 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000349 /// Map of UDRs and corresponding combiner/initializer.
350 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
351 std::pair<llvm::Function *, llvm::Function *>>
352 UDRMapTy;
353 UDRMapTy UDRMap;
354 /// Map of functions and locally defined UDRs.
355 typedef llvm::DenseMap<llvm::Function *,
356 SmallVector<const OMPDeclareReductionDecl *, 4>>
357 FunctionUDRMapTy;
358 FunctionUDRMapTy FunctionUDRMap;
Michael Krused47b9432019-08-05 18:43:21 +0000359 /// Map from the user-defined mapper declaration to its corresponding
360 /// functions.
361 llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
362 /// Map of functions and their local user-defined mappers.
363 using FunctionUDMMapTy =
364 llvm::DenseMap<llvm::Function *,
365 SmallVector<const OMPDeclareMapperDecl *, 4>>;
366 FunctionUDMMapTy FunctionUDMMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000367 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000368 /// kmp_critical_name[8];
369 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000370 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000371 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
372 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
373 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
374 /// variables.
375 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
376 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000377 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000378 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000379 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000380 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000381 /// void * shareds; /**< pointer to block of pointers to
382 /// shared vars */
383 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
384 /// executing task */
385 /// kmp_int32 part_id; /**< part id for the task */
386 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
387 /// deconstructors of firstprivate C++ objects */
388 /// } kmp_task_t;
389 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000390 /// Saved kmp_task_t for task directive.
391 QualType SavedKmpTaskTQTy;
392 /// Saved kmp_task_t for taskloop-based directive.
393 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000394 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000395 /// kmp_intptr_t base_addr;
396 /// size_t len;
397 /// struct {
398 /// bool in:1;
399 /// bool out:1;
400 /// } flags;
401 /// } kmp_depend_info_t;
402 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000403 /// struct kmp_dim { // loop bounds info casted to kmp_int64
404 /// kmp_int64 lo; // lower
405 /// kmp_int64 up; // upper
406 /// kmp_int64 st; // stride
407 /// };
408 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000410 /// void *addr; // Pointer to the offload entry info.
411 /// // (function or global)
412 /// char *name; // Name of the function or global.
413 /// size_t size; // Size of the entry info (0 if it a function).
414 /// };
415 QualType TgtOffloadEntryQTy;
416 /// struct __tgt_device_image{
417 /// void *ImageStart; // Pointer to the target code start.
418 /// void *ImageEnd; // Pointer to the target code end.
419 /// // We also add the host entries to the device image, as it may be useful
420 /// // for the target runtime to have access to that information.
421 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
422 /// // the entries.
423 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
424 /// // entries (non inclusive).
425 /// };
426 QualType TgtDeviceImageQTy;
427 /// struct __tgt_bin_desc{
428 /// int32_t NumDevices; // Number of devices supported.
429 /// __tgt_device_image *DeviceImages; // Arrays of device images
430 /// // (one per device).
431 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
432 /// // entries.
433 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
434 /// // entries (non inclusive).
435 /// };
436 QualType TgtBinaryDescriptorQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000437 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000438 /// far.
439 class OffloadEntriesInfoManagerTy {
440 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000441
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000442 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000443 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000444
445 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000446 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000447 class OffloadEntryInfo {
448 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000449 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000450 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000451 /// Entry is a target region.
452 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000453 /// Entry is a declare target variable.
454 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000455 /// Invalid entry info.
456 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000457 };
458
Alexey Bataev03f270c2018-03-30 18:31:07 +0000459 protected:
460 OffloadEntryInfo() = delete;
461 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000462 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000463 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000464 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000465 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000466
Alexey Bataev03f270c2018-03-30 18:31:07 +0000467 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000468 bool isValid() const { return Order != ~0u; }
469 unsigned getOrder() const { return Order; }
470 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000471 uint32_t getFlags() const { return Flags; }
472 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
473 llvm::Constant *getAddress() const {
474 return cast_or_null<llvm::Constant>(Addr);
475 }
476 void setAddress(llvm::Constant *V) {
477 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
478 Addr = V;
479 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000480 static bool classof(const OffloadEntryInfo *Info) { return true; }
481
Samuel Antaof83efdb2017-01-05 16:02:49 +0000482 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000483 /// Address of the entity that has to be mapped for offloading.
484 llvm::WeakTrackingVH Addr;
485
Samuel Antaof83efdb2017-01-05 16:02:49 +0000486 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000487 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000488
489 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000490 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000491
Alexey Bataev03f270c2018-03-30 18:31:07 +0000492 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000493 };
494
Alexey Bataev03f270c2018-03-30 18:31:07 +0000495 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000496 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000497 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000498 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000499 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000500
Alexey Bataev03f270c2018-03-30 18:31:07 +0000501 //
502 // Target region entries related.
503 //
504
505 /// Kind of the target registry entry.
506 enum OMPTargetRegionEntryKind : uint32_t {
507 /// Mark the entry as target region.
508 OMPTargetRegionEntryTargetRegion = 0x0,
509 /// Mark the entry as a global constructor.
510 OMPTargetRegionEntryCtor = 0x02,
511 /// Mark the entry as a global destructor.
512 OMPTargetRegionEntryDtor = 0x04,
513 };
514
515 /// Target region entries info.
516 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
517 /// Address that can be used as the ID of the entry.
518 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000519
520 public:
521 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000522 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000523 explicit OffloadEntryInfoTargetRegion(unsigned Order,
524 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000525 llvm::Constant *ID,
526 OMPTargetRegionEntryKind Flags)
527 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000528 ID(ID) {
529 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000530 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000531
532 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000533 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000534 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000535 ID = V;
536 }
537 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000538 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000539 }
540 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000541
542 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000543 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
544 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000545 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000546 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000547 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
548 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000549 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000550 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000551 /// Return true if a target region entry with the provided information
552 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000553 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000554 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000555 /// brief Applies action \a Action on all registered entries.
556 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000557 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000558 OffloadTargetRegionEntryInfoActTy;
559 void actOnTargetRegionEntriesInfo(
560 const OffloadTargetRegionEntryInfoActTy &Action);
561
Alexey Bataev03f270c2018-03-30 18:31:07 +0000562 //
563 // Device global variable entries related.
564 //
565
566 /// Kind of the global variable entry..
567 enum OMPTargetGlobalVarEntryKind : uint32_t {
568 /// Mark the entry as a to declare target.
569 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000570 /// Mark the entry as a to declare target link.
571 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000572 };
573
574 /// Device global variable entries info.
575 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
576 /// Type of the global variable.
577 CharUnits VarSize;
578 llvm::GlobalValue::LinkageTypes Linkage;
579
580 public:
581 OffloadEntryInfoDeviceGlobalVar()
582 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
583 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
584 OMPTargetGlobalVarEntryKind Flags)
585 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
586 explicit OffloadEntryInfoDeviceGlobalVar(
587 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
588 OMPTargetGlobalVarEntryKind Flags,
589 llvm::GlobalValue::LinkageTypes Linkage)
590 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
591 VarSize(VarSize), Linkage(Linkage) {
592 setAddress(Addr);
593 }
594
595 CharUnits getVarSize() const { return VarSize; }
596 void setVarSize(CharUnits Size) { VarSize = Size; }
597 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
598 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
599 static bool classof(const OffloadEntryInfo *Info) {
600 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
601 }
602 };
603
604 /// Initialize device global variable entry.
605 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
606 OMPTargetGlobalVarEntryKind Flags,
607 unsigned Order);
608
609 /// Register device global variable entry.
610 void
611 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
612 CharUnits VarSize,
613 OMPTargetGlobalVarEntryKind Flags,
614 llvm::GlobalValue::LinkageTypes Linkage);
615 /// Checks if the variable with the given name has been registered already.
616 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
617 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
618 }
619 /// Applies action \a Action on all registered entries.
620 typedef llvm::function_ref<void(StringRef,
621 const OffloadEntryInfoDeviceGlobalVar &)>
622 OffloadDeviceGlobalVarEntryInfoActTy;
623 void actOnDeviceGlobalVarEntriesInfo(
624 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
625
Samuel Antaoee8fb302016-01-06 13:42:12 +0000626 private:
627 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000628 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000629 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000630 OffloadEntriesTargetRegionPerLine;
631 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
632 OffloadEntriesTargetRegionPerParentName;
633 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
634 OffloadEntriesTargetRegionPerFile;
635 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
636 OffloadEntriesTargetRegionPerDevice;
637 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
638 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000639 /// Storage for device global variable entries kind. The storage is to be
640 /// indexed by mangled name.
641 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
642 OffloadEntriesDeviceGlobalVarTy;
643 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000644 };
645 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
646
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000647 bool ShouldMarkAsGlobal = true;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000648 /// List of the emitted functions.
649 llvm::StringSet<> AlreadyEmittedTargetFunctions;
650 /// List of the global variables with their addresses that should not be
651 /// emitted for the target.
652 llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000653
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000654 /// List of variables that can become declare target implicitly and, thus,
655 /// must be emitted.
656 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
657
Alexey Bataev2df5f122019-10-01 20:18:32 +0000658 /// Mapping of the original functions to their variants and original global
659 /// decl.
660 llvm::MapVector<CanonicalDeclPtr<const FunctionDecl>,
661 std::pair<GlobalDecl, GlobalDecl>>
662 DeferredVariantFunction;
663
Alexey Bataev0860db92019-12-19 10:01:10 -0500664 using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
665 /// Stack for list of declarations in current context marked as nontemporal.
666 /// The set is the union of all current stack elements.
667 llvm::SmallVector<NontemporalDeclsSet, 4> NontemporalDeclsStack;
668
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000669 /// Flag for keeping track of weather a requires unified_shared_memory
670 /// directive is present.
671 bool HasRequiresUnifiedSharedMemory = false;
672
673 /// Flag for keeping track of weather a target region has been emitted.
674 bool HasEmittedTargetRegion = false;
675
676 /// Flag for keeping track of weather a device routine has been emitted.
677 /// Device routines are specific to the
678 bool HasEmittedDeclareTargetRegion = false;
679
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000680 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000681 /// metadata.
682 void loadOffloadInfoMetadata();
683
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000684 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000685 QualType getTgtOffloadEntryQTy();
686
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000687 /// Returns __tgt_device_image type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000688 QualType getTgtDeviceImageQTy();
689
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000690 /// Returns __tgt_bin_desc type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000691 QualType getTgtBinaryDescriptorQTy();
692
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000693 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000694 /// found along the way.
695 /// \param S Starting statement.
696 /// \param ParentName Name of the function declaration that is being scanned.
697 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000698
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000699 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000700 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000701
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000702 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000703 llvm::Type *getKmpc_MicroPointerTy();
704
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000705 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000706 /// \param Function OpenMP runtime function.
707 /// \return Specified function.
James Y Knight9871db02019-02-05 16:42:33 +0000708 llvm::FunctionCallee createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000709
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000710 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000711 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000712 llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
713 bool IVSigned);
Alexander Musman21212e42015-03-13 10:38:23 +0000714
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000715 /// Returns __kmpc_dispatch_init_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000716 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000717 llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize,
718 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000719
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000720 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000721 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000722 llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
723 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000724
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000725 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000726 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000727 llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
728 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000729
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000730 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000731 /// return threadprivate cache object. This object is a pointer's worth of
732 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000733 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000734 /// \return Cache variable for the specified threadprivate.
735 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
736
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000737 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000738 /// internal global variable with the specified Name. The created variable has
739 /// linkage CommonLinkage by default and is initialized by null value.
740 /// \param Ty Type of the global variable. If it is exist already the type
741 /// must be the same.
742 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000743 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000744 const llvm::Twine &Name,
745 unsigned AddressSpace = 0);
Alexey Bataev97720002014-11-11 04:05:39 +0000746
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000747 /// Set of threadprivate variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000748 llvm::StringSet<> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000749
Alexey Bataev34f8a702018-03-28 14:28:54 +0000750 /// Set of declare target variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000751 llvm::StringSet<> DeclareTargetWithDefinition;
Alexey Bataev34f8a702018-03-28 14:28:54 +0000752
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000753 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000754 /// \param VDAddr Address of the global variable \a VD.
755 /// \param Ctor Pointer to a global init function for \a VD.
756 /// \param CopyCtor Pointer to a global copy function for \a VD.
757 /// \param Dtor Pointer to a global destructor function for \a VD.
758 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000759 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000760 llvm::Value *Ctor, llvm::Value *CopyCtor,
761 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000762
Michael Krused47b9432019-08-05 18:43:21 +0000763 /// Emit the array initialization or deletion portion for user-defined mapper
764 /// code generation.
765 void emitUDMapperArrayInitOrDel(CodeGenFunction &MapperCGF,
766 llvm::Value *Handle, llvm::Value *BasePtr,
767 llvm::Value *Ptr, llvm::Value *Size,
768 llvm::Value *MapType, CharUnits ElementSize,
769 llvm::BasicBlock *ExitBB, bool IsInit);
770
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000771 struct TaskResultTy {
772 llvm::Value *NewTask = nullptr;
James Y Knight9871db02019-02-05 16:42:33 +0000773 llvm::Function *TaskEntry = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000774 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000775 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000776 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000777 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000778 };
779 /// Emit task region for the task directive. The task region is emitted in
780 /// several steps:
781 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
782 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
783 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
784 /// function:
785 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
786 /// TaskFunction(gtid, tt->part_id, tt->shareds);
787 /// return 0;
788 /// }
789 /// 2. Copy a list of shared variables to field shareds of the resulting
790 /// structure kmp_task_t returned by the previous call (if any).
791 /// 3. Copy a pointer to destructions function to field destructions of the
792 /// resulting structure kmp_task_t.
793 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000794 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
795 /// /*part_id*/, captured_struct */*__context*/);
796 /// \param SharedsTy A type which contains references the shared variables.
797 /// \param Shareds Context with the list of shared variables from the \p
798 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000799 /// \param Data Additional data for task generation like tiednsee, final
800 /// state, list of privates etc.
801 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
802 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +0000803 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000804 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000805
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000806 /// Returns default address space for the constant firstprivates, 0 by
807 /// default.
808 virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; }
809
Alexey Bataevec7946e2019-09-23 14:06:51 +0000810 /// Emit code that pushes the trip count of loops associated with constructs
811 /// 'target teams distribute' and 'teams distribute parallel for'.
812 /// \param SizeEmitter Emits the int64 value for the number of iterations of
813 /// the associated loop.
814 void emitTargetNumIterationsCall(
815 CodeGenFunction &CGF, const OMPExecutableDirective &D,
816 llvm::Value *DeviceID,
817 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
818 const OMPLoopDirective &D)>
819 SizeEmitter);
820
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000821public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000822 explicit CGOpenMPRuntime(CodeGenModule &CGM)
823 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000824 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000825 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000826
Alexey Bataevd08c0562019-11-19 12:07:54 -0500827 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
828 /// function. Here is the logic:
829 /// if (Cond) {
830 /// ThenGen();
831 /// } else {
832 /// ElseGen();
833 /// }
834 void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
835 const RegionCodeGenTy &ThenGen,
836 const RegionCodeGenTy &ElseGen);
837
Alexey Bataev5c427362019-04-10 19:11:33 +0000838 /// Checks if the \p Body is the \a CompoundStmt and returns its child
839 /// statement iff there is only one that is not evaluatable at the compile
840 /// time.
841 static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
842
Alexey Bataev18fa2322018-05-02 14:20:50 +0000843 /// Get the platform-specific name separator.
844 std::string getName(ArrayRef<StringRef> Parts) const;
845
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000846 /// Emit code for the specified user defined reduction construct.
847 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
848 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000849 /// Get combiner/initializer for the specified user-defined reduction, if any.
850 virtual std::pair<llvm::Function *, llvm::Function *>
851 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000852
Michael Krused47b9432019-08-05 18:43:21 +0000853 /// Emit the function for the user defined mapper construct.
854 void emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
855 CodeGenFunction *CGF = nullptr);
856
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000857 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000858 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
859 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000860 /// \param D OpenMP directive.
861 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000862 /// \param InnermostKind Kind of innermost directive (for simple directives it
863 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000864 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000865 virtual llvm::Function *emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000866 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
867 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
868
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000869 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000870 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
871 /// kmp_int32 BoundID, struct context_vars*).
872 /// \param D OpenMP directive.
873 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
874 /// \param InnermostKind Kind of innermost directive (for simple directives it
875 /// is a directive itself, for combined - its innermost directive).
876 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000877 virtual llvm::Function *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000878 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
879 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000880
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000881 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000882 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
883 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000884 /// \param D OpenMP directive.
885 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000886 /// \param PartIDVar Variable for partition id in the current OpenMP untied
887 /// task region.
888 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000889 /// \param InnermostKind Kind of innermost directive (for simple directives it
890 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000891 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000892 /// \param Tied true if task is generated for tied task, false otherwise.
893 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
894 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000895 ///
James Y Knight9871db02019-02-05 16:42:33 +0000896 virtual llvm::Function *emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000897 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000898 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
899 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
900 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000901
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000902 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000903 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000904 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000905
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000906 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000907 /// variables captured in a record which address is stored in \a
908 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000909 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000910 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000911 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000912 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000913 /// \param IfCond Condition in the associated 'if' clause, if it was
914 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000915 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000916 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +0000917 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000918 ArrayRef<llvm::Value *> CapturedVars,
919 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000920
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000921 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000922 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000923 /// \param CriticalOpGen Generator for the statement associated with the given
924 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000925 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000926 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000927 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000928 SourceLocation Loc,
929 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000930
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000931 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000932 /// \param MasterOpGen Generator for the statement associated with the given
933 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000934 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000935 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000936 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000937
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000938 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000939 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000940
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000941 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000942 /// \param TaskgroupOpGen Generator for the statement associated with the
943 /// given taskgroup region.
944 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
945 const RegionCodeGenTy &TaskgroupOpGen,
946 SourceLocation Loc);
947
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000948 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000949 /// \param SingleOpGen Generator for the statement associated with the given
950 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000951 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000952 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000953 SourceLocation Loc,
954 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000955 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000956 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000957 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000958
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000959 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000960 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000961 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000962 virtual void emitOrderedRegion(CodeGenFunction &CGF,
963 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000964 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000965
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000966 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +0000967 /// \param Kind Directive for which this implicit barrier call must be
968 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000969 /// \param EmitChecks true if need to emit checks for cancellation barriers.
970 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
971 /// runtime class decides which one to emit (simple or with cancellation
972 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000973 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000974 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000975 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000976 bool EmitChecks = true,
977 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000978
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000979 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +0000980 /// This kind of worksharing directive is emitted without outer loop.
981 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
982 /// \param Chunked True if chunk is specified in the clause.
983 ///
984 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
985 bool Chunked) const;
986
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000987 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000988 /// This kind of distribute directive is emitted without outer loop.
989 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
990 /// \param Chunked True if chunk is specified in the clause.
991 ///
992 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
993 bool Chunked) const;
994
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000995 /// Check if the specified \a ScheduleKind is static chunked.
996 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
997 /// \param Chunked True if chunk is specified in the clause.
998 ///
999 virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
1000 bool Chunked) const;
1001
1002 /// Check if the specified \a ScheduleKind is static non-chunked.
1003 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1004 /// \param Chunked True if chunk is specified in the clause.
1005 ///
1006 virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
1007 bool Chunked) const;
1008
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001009 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001010 /// This kind of worksharing directive is emitted without outer loop.
1011 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
1012 ///
1013 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
1014
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001015 /// struct with the values to be passed to the dispatch runtime function
1016 struct DispatchRTInput {
1017 /// Loop lower bound
1018 llvm::Value *LB = nullptr;
1019 /// Loop upper bound
1020 llvm::Value *UB = nullptr;
1021 /// Chunk size specified using 'schedule' clause (nullptr if chunk
1022 /// was not specified)
1023 llvm::Value *Chunk = nullptr;
1024 DispatchRTInput() = default;
1025 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
1026 : LB(LB), UB(UB), Chunk(Chunk) {}
1027 };
1028
1029 /// Call the appropriate runtime routine to initialize it before start
1030 /// of loop.
1031
1032 /// This is used for non static scheduled types and when the ordered
1033 /// clause is present on the loop construct.
1034 /// Depending on the loop schedule, it is necessary to call some runtime
1035 /// routine before start of the OpenMP loop to get the loop upper / lower
1036 /// bounds \a LB and \a UB and stride \a ST.
1037 ///
1038 /// \param CGF Reference to current CodeGenFunction.
1039 /// \param Loc Clang source location.
1040 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1041 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001042 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001043 /// \param Ordered true if loop is ordered, false otherwise.
1044 /// \param DispatchValues struct containing llvm values for lower bound, upper
1045 /// bound, and chunk expression.
1046 /// For the default (nullptr) value, the chunk 1 will be used.
1047 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001048 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001049 const OpenMPScheduleTy &ScheduleKind,
1050 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001051 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001052
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001053 /// Struct with the values to be passed to the static runtime function
1054 struct StaticRTInput {
1055 /// Size of the iteration variable in bits.
1056 unsigned IVSize = 0;
1057 /// Sign of the iteration variable.
1058 bool IVSigned = false;
1059 /// true if loop is ordered, false otherwise.
1060 bool Ordered = false;
1061 /// Address of the output variable in which the flag of the last iteration
1062 /// is returned.
1063 Address IL = Address::invalid();
1064 /// Address of the output variable in which the lower iteration number is
1065 /// returned.
1066 Address LB = Address::invalid();
1067 /// Address of the output variable in which the upper iteration number is
1068 /// returned.
1069 Address UB = Address::invalid();
1070 /// Address of the output variable in which the stride value is returned
1071 /// necessary to generated the static_chunked scheduled loop.
1072 Address ST = Address::invalid();
1073 /// Value of the chunk for the static_chunked scheduled loop. For the
1074 /// default (nullptr) value, the chunk 1 will be used.
1075 llvm::Value *Chunk = nullptr;
1076 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1077 Address LB, Address UB, Address ST,
1078 llvm::Value *Chunk = nullptr)
1079 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1080 UB(UB), ST(ST), Chunk(Chunk) {}
1081 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001082 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +00001083 /// of loop.
1084 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001085 /// This is used only in case of static schedule, when the user did not
1086 /// specify a ordered clause on the loop construct.
1087 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +00001088 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001089 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +00001090 ///
1091 /// \param CGF Reference to current CodeGenFunction.
1092 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001093 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001094 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001095 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +00001096 ///
John McCall7f416cc2015-09-08 08:05:57 +00001097 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001098 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001099 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001100 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00001101
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001102 ///
1103 /// \param CGF Reference to current CodeGenFunction.
1104 /// \param Loc Clang source location.
1105 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001106 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001107 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001108 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1109 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001110 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001111 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001112
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001113 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001114 /// iteration of the ordered loop with the dynamic scheduling.
1115 ///
1116 /// \param CGF Reference to current CodeGenFunction.
1117 /// \param Loc Clang source location.
1118 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001119 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001120 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001121 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1122 SourceLocation Loc, unsigned IVSize,
1123 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001124
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001125 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001126 /// all the work with current loop.
1127 ///
1128 /// \param CGF Reference to current CodeGenFunction.
1129 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001130 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001131 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001132 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1133 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001134
Alexander Musman92bdaab2015-03-12 13:37:50 +00001135 /// Call __kmpc_dispatch_next(
1136 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1137 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1138 /// kmp_int[32|64] *p_stride);
1139 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001140 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001141 /// \param IL Address of the output variable in which the flag of the
1142 /// last iteration is returned.
1143 /// \param LB Address of the output variable in which the lower iteration
1144 /// number is returned.
1145 /// \param UB Address of the output variable in which the upper iteration
1146 /// number is returned.
1147 /// \param ST Address of the output variable in which the stride value is
1148 /// returned.
1149 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1150 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001151 Address IL, Address LB,
1152 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001153
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001154 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001155 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1156 /// clause.
1157 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001158 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1159 llvm::Value *NumThreads,
1160 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001161
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001162 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001163 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1164 virtual void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001165 llvm::omp::ProcBindKind ProcBind,
Alexey Bataev7f210c62015-06-18 13:40:03 +00001166 SourceLocation Loc);
1167
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001168 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001169 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001170 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001171 /// \param VDAddr Address of the global variable \a VD.
1172 /// \param Loc Location of the reference to threadprivate var.
1173 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001174 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1175 const VarDecl *VD,
1176 Address VDAddr,
1177 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001178
Alexey Bataev92327c52018-03-26 16:40:55 +00001179 /// Returns the address of the variable marked as declare target with link
Gheorghe-Teodor Bercea0034e842019-06-20 18:04:47 +00001180 /// clause OR as declare target with to clause and unified memory.
1181 virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001182
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001183 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001184 /// a call to runtime library which adds initial value to the newly created
1185 /// threadprivate variable (if it is not constant) and registers destructor
1186 /// for the variable (if any).
1187 /// \param VD Threadprivate variable.
1188 /// \param VDAddr Address of the global variable \a VD.
1189 /// \param Loc Location of threadprivate declaration.
1190 /// \param PerformInit true if initialization expression is not constant.
1191 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001192 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001193 SourceLocation Loc, bool PerformInit,
1194 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001195
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001196 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001197 /// \param VD Declare target variable.
1198 /// \param Addr Address of the global variable \a VD.
1199 /// \param PerformInit true if initialization expression is not constant.
1200 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1201 llvm::GlobalVariable *Addr,
1202 bool PerformInit);
1203
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001204 /// Creates artificial threadprivate variable with name \p Name and type \p
1205 /// VarType.
1206 /// \param VarType Type of the artificial threadprivate variable.
1207 /// \param Name Name of the artificial threadprivate variable.
1208 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1209 QualType VarType,
1210 StringRef Name);
1211
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001212 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001213 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001214 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1215 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001216
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001217 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001218 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001219 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1220 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1221 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1222 /// function:
1223 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1224 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1225 /// return 0;
1226 /// }
1227 /// 2. Copy a list of shared variables to field shareds of the resulting
1228 /// structure kmp_task_t returned by the previous call (if any).
1229 /// 3. Copy a pointer to destructions function to field destructions of the
1230 /// resulting structure kmp_task_t.
1231 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1232 /// kmp_task_t *new_task), where new_task is a resulting structure from
1233 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001234 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001235 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1236 /// /*part_id*/, captured_struct */*__context*/);
1237 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001238 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001239 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001240 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1241 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001242 /// \param Data Additional data for task generation like tiednsee, final
1243 /// state, list of privates etc.
1244 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1245 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001246 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001247 Address Shareds, const Expr *IfCond,
1248 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001249
Alexey Bataev7292c292016-04-25 12:22:29 +00001250 /// Emit task region for the taskloop directive. The taskloop region is
1251 /// emitted in several steps:
1252 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1253 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1254 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1255 /// function:
1256 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1257 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1258 /// return 0;
1259 /// }
1260 /// 2. Copy a list of shared variables to field shareds of the resulting
1261 /// structure kmp_task_t returned by the previous call (if any).
1262 /// 3. Copy a pointer to destructions function to field destructions of the
1263 /// resulting structure kmp_task_t.
1264 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1265 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1266 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1267 /// is a resulting structure from
1268 /// previous items.
1269 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001270 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1271 /// /*part_id*/, captured_struct */*__context*/);
1272 /// \param SharedsTy A type which contains references the shared variables.
1273 /// \param Shareds Context with the list of shared variables from the \p
1274 /// TaskFunction.
1275 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1276 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001277 /// \param Data Additional data for task generation like tiednsee, final
1278 /// state, list of privates etc.
James Y Knight9871db02019-02-05 16:42:33 +00001279 virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1280 const OMPLoopDirective &D,
1281 llvm::Function *TaskFunction,
1282 QualType SharedsTy, Address Shareds,
1283 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001284
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001285 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001286 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001287 /// \param InnermostKind Kind of innermost directive (for simple directives it
1288 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001289 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001290 /// \param HasCancel true if region has inner cancel directive, false
1291 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001292 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001293 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001294 const RegionCodeGenTy &CodeGen,
1295 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001296
1297 /// Emits reduction function.
1298 /// \param ArgsType Array type containing pointers to reduction variables.
1299 /// \param Privates List of private copies for original reduction arguments.
1300 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1301 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1302 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1303 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev982a35e2019-03-19 17:09:52 +00001304 llvm::Function *emitReductionFunction(SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001305 llvm::Type *ArgsType,
1306 ArrayRef<const Expr *> Privates,
1307 ArrayRef<const Expr *> LHSExprs,
1308 ArrayRef<const Expr *> RHSExprs,
1309 ArrayRef<const Expr *> ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001310
1311 /// Emits single reduction combiner
1312 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1313 const Expr *ReductionOp,
1314 const Expr *PrivateRef,
1315 const DeclRefExpr *LHS,
1316 const DeclRefExpr *RHS);
1317
1318 struct ReductionOptionsTy {
1319 bool WithNowait;
1320 bool SimpleReduction;
1321 OpenMPDirectiveKind ReductionKind;
1322 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001323 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001324 /// reduction:
1325 /// \code
1326 ///
1327 /// static kmp_critical_name lock = { 0 };
1328 ///
1329 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1330 /// ...
1331 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1332 /// ...
1333 /// }
1334 ///
1335 /// ...
1336 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1337 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1338 /// RedList, reduce_func, &<lock>)) {
1339 /// case 1:
1340 /// ...
1341 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1342 /// ...
1343 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1344 /// break;
1345 /// case 2:
1346 /// ...
1347 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1348 /// ...
1349 /// break;
1350 /// default:;
1351 /// }
1352 /// \endcode
1353 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001354 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001355 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1356 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1357 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1358 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001359 /// \param Options List of options for reduction codegen:
1360 /// WithNowait true if parent directive has also nowait clause, false
1361 /// otherwise.
1362 /// SimpleReduction Emit reduction operation only. Used for omp simd
1363 /// directive on the host.
1364 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001365 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001366 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001367 ArrayRef<const Expr *> LHSExprs,
1368 ArrayRef<const Expr *> RHSExprs,
1369 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001370 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001371
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001372 /// Emit a code for initialization of task reduction clause. Next code
1373 /// should be emitted for reduction:
1374 /// \code
1375 ///
1376 /// _task_red_item_t red_data[n];
1377 /// ...
1378 /// red_data[i].shar = &origs[i];
1379 /// red_data[i].size = sizeof(origs[i]);
1380 /// red_data[i].f_init = (void*)RedInit<i>;
1381 /// red_data[i].f_fini = (void*)RedDest<i>;
1382 /// red_data[i].f_comb = (void*)RedOp<i>;
1383 /// red_data[i].flags = <Flag_i>;
1384 /// ...
1385 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1386 /// \endcode
1387 ///
1388 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1389 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1390 /// \param Data Additional data for task generation like tiedness, final
1391 /// state, list of privates, reductions etc.
1392 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1393 SourceLocation Loc,
1394 ArrayRef<const Expr *> LHSExprs,
1395 ArrayRef<const Expr *> RHSExprs,
1396 const OMPTaskDataTy &Data);
1397
1398 /// Required to resolve existing problems in the runtime. Emits threadprivate
1399 /// variables to store the size of the VLAs/array sections for
1400 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1401 /// store the pointer to the original reduction item for the custom
1402 /// initializer defined by declare reduction construct.
1403 /// \param RCG Allows to reuse an existing data for the reductions.
1404 /// \param N Reduction item for which fixups must be emitted.
1405 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1406 ReductionCodeGen &RCG, unsigned N);
1407
1408 /// Get the address of `void *` type of the privatue copy of the reduction
1409 /// item specified by the \p SharedLVal.
1410 /// \param ReductionsPtr Pointer to the reduction data returned by the
1411 /// emitTaskReductionInit function.
1412 /// \param SharedLVal Address of the original reduction item.
1413 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1414 llvm::Value *ReductionsPtr,
1415 LValue SharedLVal);
1416
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001417 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001418 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001419
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001420 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001421 /// \param CancelRegion Region kind for which the cancellation point must be
1422 /// emitted.
1423 ///
1424 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1425 SourceLocation Loc,
1426 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001427
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001428 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001429 /// \param IfCond Condition in the associated 'if' clause, if it was
1430 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001431 /// \param CancelRegion Region kind for which the cancel must be emitted.
1432 ///
1433 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001434 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001435 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001436
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001437 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001438 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001439 /// \param ParentName Name of the function that encloses the target region.
1440 /// \param OutlinedFn Outlined function value to be defined by this call.
1441 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1442 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001443 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001444 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001445 /// evaluates to false.
1446 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1447 StringRef ParentName,
1448 llvm::Function *&OutlinedFn,
1449 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001450 bool IsOffloadEntry,
1451 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001452
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001453 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001454 /// code attempts offloading the execution to the device, an the event of
1455 /// a failure it executes the host version outlined in \a OutlinedFn.
1456 /// \param D Directive to emit.
1457 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001458 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001459 /// \param IfCond Expression evaluated in if clause associated with the target
1460 /// directive, or null if no if clause is used.
1461 /// \param Device Expression evaluated in device clause associated with the
1462 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00001463 /// \param SizeEmitter Callback to emit number of iterations for loop-based
1464 /// directives.
1465 virtual void
1466 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1467 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
1468 const Expr *IfCond, const Expr *Device,
1469 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
1470 const OMPLoopDirective &D)>
1471 SizeEmitter);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001472
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001473 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001474 /// the function itself in case it is a valid device function. Returns true if
1475 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001476 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001477 virtual bool emitTargetFunctions(GlobalDecl GD);
1478
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001479 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001480 /// Returns true if \a GD was dealt with successfully.
1481 /// \param GD Variable declaration to emit.
1482 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1483
Alexey Bataev03f270c2018-03-30 18:31:07 +00001484 /// Checks if the provided global decl \a GD is a declare target variable and
1485 /// registers it when emitting code for the host.
1486 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1487 llvm::Constant *Addr);
1488
Alexey Bataev1af5bd52019-03-05 17:47:18 +00001489 /// Registers provided target firstprivate variable as global on the
1490 /// target.
1491 llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1492 const VarDecl *VD);
1493
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001494 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001495 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001496 /// \param GD Global to scan.
1497 virtual bool emitTargetGlobal(GlobalDecl GD);
1498
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001499 /// Creates and returns a registration function for when at least one
1500 /// requires directives was used in the current module.
1501 llvm::Function *emitRequiresDirectiveRegFun();
1502
Sergey Dmitriev5836c352019-10-15 18:42:47 +00001503 /// Creates all the offload entries in the current compilation unit
1504 /// along with the associated metadata.
1505 void createOffloadEntriesAndInfoMetadata();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001506
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001507 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001508 /// variables captured in a record which address is stored in \a
1509 /// CapturedStruct.
1510 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1511 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1512 /// \param CapturedVars A pointer to the record with the references to
1513 /// variables used in \a OutlinedFn function.
1514 ///
1515 virtual void emitTeamsCall(CodeGenFunction &CGF,
1516 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001517 SourceLocation Loc, llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001518 ArrayRef<llvm::Value *> CapturedVars);
1519
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001520 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001521 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1522 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001523 /// \param NumTeams An integer expression of teams.
1524 /// \param ThreadLimit An integer expression of threads.
1525 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1526 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001527
Samuel Antaocc10b852016-07-28 14:23:26 +00001528 /// Struct that keeps all the relevant information that should be kept
1529 /// throughout a 'target data' region.
1530 class TargetDataInfo {
1531 /// Set to true if device pointer information have to be obtained.
1532 bool RequiresDevicePointerInfo = false;
1533
1534 public:
1535 /// The array of base pointer passed to the runtime library.
1536 llvm::Value *BasePointersArray = nullptr;
1537 /// The array of section pointers passed to the runtime library.
1538 llvm::Value *PointersArray = nullptr;
1539 /// The array of sizes passed to the runtime library.
1540 llvm::Value *SizesArray = nullptr;
1541 /// The array of map types passed to the runtime library.
1542 llvm::Value *MapTypesArray = nullptr;
1543 /// The total number of pointers passed to the runtime library.
1544 unsigned NumberOfPtrs = 0u;
1545 /// Map between the a declaration of a capture and the corresponding base
1546 /// pointer address where the runtime returns the device pointers.
1547 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1548
1549 explicit TargetDataInfo() {}
1550 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1551 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1552 /// Clear information about the data arrays.
1553 void clearArrayInfo() {
1554 BasePointersArray = nullptr;
1555 PointersArray = nullptr;
1556 SizesArray = nullptr;
1557 MapTypesArray = nullptr;
1558 NumberOfPtrs = 0u;
1559 }
1560 /// Return true if the current target data information has valid arrays.
1561 bool isValid() {
1562 return BasePointersArray && PointersArray && SizesArray &&
1563 MapTypesArray && NumberOfPtrs;
1564 }
1565 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1566 };
1567
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001568 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001569 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001570 /// \param IfCond Expression evaluated in if clause associated with the
1571 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001572 /// \param Device Expression evaluated in device clause associated with the
1573 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001574 /// \param Info A record used to store information that needs to be preserved
1575 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001576 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1577 const OMPExecutableDirective &D,
1578 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001579 const RegionCodeGenTy &CodeGen,
1580 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001581
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001582 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001583 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001584 /// \param D Directive to emit.
1585 /// \param IfCond Expression evaluated in if clause associated with the target
1586 /// directive, or null if no if clause is used.
1587 /// \param Device Expression evaluated in device clause associated with the
1588 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001589 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1590 const OMPExecutableDirective &D,
1591 const Expr *IfCond,
1592 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001593
1594 /// Marks function \a Fn with properly mangled versions of vector functions.
1595 /// \param FD Function marked as 'declare simd'.
1596 /// \param Fn LLVM function that must be marked with 'declare simd'
1597 /// attributes.
1598 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1599 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001600
1601 /// Emit initialization for doacross loop nesting support.
1602 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001603 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1604 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001605
1606 /// Emit code for doacross ordered directive with 'depend' clause.
1607 /// \param C 'depend' clause with 'sink|source' dependency kind.
1608 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1609 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001610
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001611 /// Translates the native parameter of outlined function if this is required
1612 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001613 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001614 /// \param NativeParam Parameter itself.
1615 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1616 const VarDecl *NativeParam) const {
1617 return NativeParam;
1618 }
1619
1620 /// Gets the address of the native argument basing on the address of the
1621 /// target-specific parameter.
1622 /// \param NativeParam Parameter itself.
1623 /// \param TargetParam Corresponding target-specific parameter.
1624 virtual Address getParameterAddress(CodeGenFunction &CGF,
1625 const VarDecl *NativeParam,
1626 const VarDecl *TargetParam) const;
1627
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001628 /// Choose default schedule type and chunk value for the
1629 /// dist_schedule clause.
1630 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1631 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1632 llvm::Value *&Chunk) const {}
1633
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001634 /// Choose default schedule type and chunk value for the
1635 /// schedule clause.
1636 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1637 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
Alexey Bataevf6a53d62019-03-18 18:40:00 +00001638 const Expr *&ChunkExpr) const;
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001639
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001640 /// Emits call of the outlined function with the provided arguments,
1641 /// translating these arguments to correct target-specific arguments.
1642 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001643 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001644 llvm::FunctionCallee OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001645 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001646
1647 /// Emits OpenMP-specific function prolog.
1648 /// Required for device constructs.
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001649 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001650
1651 /// Gets the OpenMP-specific address of the local variable.
1652 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1653 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001654
Raphael Isemannb23ccec2018-12-10 12:37:46 +00001655 /// Marks the declaration as already emitted for the device code and returns
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001656 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001657 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001658
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001659 /// Emit deferred declare target variables marked for deferred emission.
1660 void emitDeferredTargetDecls() const;
Alexey Bataev60705422018-10-30 15:50:12 +00001661
1662 /// Adjust some parameters for the target-based directives, like addresses of
1663 /// the variables captured by reference in lambdas.
1664 virtual void
1665 adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
1666 const OMPExecutableDirective &D) const;
Patrick Lyster8f7f5862018-11-19 15:09:33 +00001667
1668 /// Perform check on requires decl to ensure that target architecture
1669 /// supports unified addressing
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001670 virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D);
Alexey Bataevc5687252019-03-21 19:35:27 +00001671
1672 /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1673 /// the predefined allocator and translates it into the corresponding address
1674 /// space.
1675 virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00001676
1677 /// Return whether the unified_shared_memory has been specified.
1678 bool hasRequiresUnifiedSharedMemory() const;
Alexey Bataev2df5f122019-10-01 20:18:32 +00001679
1680 /// Emits the definition of the declare variant function.
1681 virtual bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition);
Alexey Bataev0860db92019-12-19 10:01:10 -05001682
1683 /// Checks if the \p VD variable is marked as nontemporal declaration in
1684 /// current context.
1685 bool isNontemporalDecl(const ValueDecl *VD) const;
Alexey Bataev9959db52014-05-06 10:08:46 +00001686};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001687
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001688/// Class supports emissionof SIMD-only code.
1689class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1690public:
1691 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1692 ~CGOpenMPSIMDRuntime() override {}
1693
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001694 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001695 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1696 /// kmp_int32 BoundID, struct context_vars*).
1697 /// \param D OpenMP directive.
1698 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1699 /// \param InnermostKind Kind of innermost directive (for simple directives it
1700 /// is a directive itself, for combined - its innermost directive).
1701 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001702 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001703 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1704 const VarDecl *ThreadIDVar,
1705 OpenMPDirectiveKind InnermostKind,
1706 const RegionCodeGenTy &CodeGen) override;
1707
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001708 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001709 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1710 /// kmp_int32 BoundID, struct context_vars*).
1711 /// \param D OpenMP directive.
1712 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1713 /// \param InnermostKind Kind of innermost directive (for simple directives it
1714 /// is a directive itself, for combined - its innermost directive).
1715 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001716 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001717 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1718 const VarDecl *ThreadIDVar,
1719 OpenMPDirectiveKind InnermostKind,
1720 const RegionCodeGenTy &CodeGen) override;
1721
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001722 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001723 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1724 /// TaskT).
1725 /// \param D OpenMP directive.
1726 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1727 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1728 /// task region.
1729 /// \param TaskTVar Variable for task_t argument.
1730 /// \param InnermostKind Kind of innermost directive (for simple directives it
1731 /// is a directive itself, for combined - its innermost directive).
1732 /// \param CodeGen Code generation sequence for the \a D directive.
1733 /// \param Tied true if task is generated for tied task, false otherwise.
1734 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1735 /// tasks.
1736 ///
James Y Knight9871db02019-02-05 16:42:33 +00001737 llvm::Function *emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001738 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1739 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1740 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1741 bool Tied, unsigned &NumberOfParts) override;
1742
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001743 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001744 /// variables captured in a record which address is stored in \a
1745 /// CapturedStruct.
1746 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1747 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1748 /// \param CapturedVars A pointer to the record with the references to
1749 /// variables used in \a OutlinedFn function.
1750 /// \param IfCond Condition in the associated 'if' clause, if it was
1751 /// specified, nullptr otherwise.
1752 ///
1753 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001754 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001755 ArrayRef<llvm::Value *> CapturedVars,
1756 const Expr *IfCond) override;
1757
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001758 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001759 /// \param CriticalName Name of the critical region.
1760 /// \param CriticalOpGen Generator for the statement associated with the given
1761 /// critical region.
1762 /// \param Hint Value of the 'hint' clause (optional).
1763 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1764 const RegionCodeGenTy &CriticalOpGen,
1765 SourceLocation Loc,
1766 const Expr *Hint = nullptr) override;
1767
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001768 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001769 /// \param MasterOpGen Generator for the statement associated with the given
1770 /// master region.
1771 void emitMasterRegion(CodeGenFunction &CGF,
1772 const RegionCodeGenTy &MasterOpGen,
1773 SourceLocation Loc) override;
1774
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001775 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001776 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1777
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001778 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001779 /// \param TaskgroupOpGen Generator for the statement associated with the
1780 /// given taskgroup region.
1781 void emitTaskgroupRegion(CodeGenFunction &CGF,
1782 const RegionCodeGenTy &TaskgroupOpGen,
1783 SourceLocation Loc) override;
1784
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001785 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001786 /// \param SingleOpGen Generator for the statement associated with the given
1787 /// single region.
1788 void emitSingleRegion(CodeGenFunction &CGF,
1789 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1790 ArrayRef<const Expr *> CopyprivateVars,
1791 ArrayRef<const Expr *> DestExprs,
1792 ArrayRef<const Expr *> SrcExprs,
1793 ArrayRef<const Expr *> AssignmentOps) override;
1794
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001795 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001796 /// \param OrderedOpGen Generator for the statement associated with the given
1797 /// ordered region.
1798 void emitOrderedRegion(CodeGenFunction &CGF,
1799 const RegionCodeGenTy &OrderedOpGen,
1800 SourceLocation Loc, bool IsThreads) override;
1801
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001802 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001803 /// \param Kind Directive for which this implicit barrier call must be
1804 /// generated. Must be OMPD_barrier for explicit barrier generation.
1805 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1806 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1807 /// runtime class decides which one to emit (simple or with cancellation
1808 /// checks).
1809 ///
1810 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1811 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1812 bool ForceSimpleCall = false) override;
1813
1814 /// This is used for non static scheduled types and when the ordered
1815 /// clause is present on the loop construct.
1816 /// Depending on the loop schedule, it is necessary to call some runtime
1817 /// routine before start of the OpenMP loop to get the loop upper / lower
1818 /// bounds \a LB and \a UB and stride \a ST.
1819 ///
1820 /// \param CGF Reference to current CodeGenFunction.
1821 /// \param Loc Clang source location.
1822 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1823 /// \param IVSize Size of the iteration variable in bits.
1824 /// \param IVSigned Sign of the iteration variable.
1825 /// \param Ordered true if loop is ordered, false otherwise.
1826 /// \param DispatchValues struct containing llvm values for lower bound, upper
1827 /// bound, and chunk expression.
1828 /// For the default (nullptr) value, the chunk 1 will be used.
1829 ///
1830 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1831 const OpenMPScheduleTy &ScheduleKind,
1832 unsigned IVSize, bool IVSigned, bool Ordered,
1833 const DispatchRTInput &DispatchValues) override;
1834
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001835 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001836 /// of loop.
1837 ///
1838 /// This is used only in case of static schedule, when the user did not
1839 /// specify a ordered clause on the loop construct.
1840 /// Depending on the loop schedule, it is necessary to call some runtime
1841 /// routine before start of the OpenMP loop to get the loop upper / lower
1842 /// bounds LB and UB and stride ST.
1843 ///
1844 /// \param CGF Reference to current CodeGenFunction.
1845 /// \param Loc Clang source location.
1846 /// \param DKind Kind of the directive.
1847 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1848 /// \param Values Input arguments for the construct.
1849 ///
1850 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1851 OpenMPDirectiveKind DKind,
1852 const OpenMPScheduleTy &ScheduleKind,
1853 const StaticRTInput &Values) override;
1854
1855 ///
1856 /// \param CGF Reference to current CodeGenFunction.
1857 /// \param Loc Clang source location.
1858 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1859 /// \param Values Input arguments for the construct.
1860 ///
1861 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1862 OpenMPDistScheduleClauseKind SchedKind,
1863 const StaticRTInput &Values) override;
1864
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001865 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001866 /// iteration of the ordered loop with the dynamic scheduling.
1867 ///
1868 /// \param CGF Reference to current CodeGenFunction.
1869 /// \param Loc Clang source location.
1870 /// \param IVSize Size of the iteration variable in bits.
1871 /// \param IVSigned Sign of the iteration variable.
1872 ///
1873 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1874 unsigned IVSize, bool IVSigned) override;
1875
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001876 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001877 /// all the work with current loop.
1878 ///
1879 /// \param CGF Reference to current CodeGenFunction.
1880 /// \param Loc Clang source location.
1881 /// \param DKind Kind of the directive for which the static finish is emitted.
1882 ///
1883 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1884 OpenMPDirectiveKind DKind) override;
1885
1886 /// Call __kmpc_dispatch_next(
1887 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1888 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1889 /// kmp_int[32|64] *p_stride);
1890 /// \param IVSize Size of the iteration variable in bits.
1891 /// \param IVSigned Sign of the iteration variable.
1892 /// \param IL Address of the output variable in which the flag of the
1893 /// last iteration is returned.
1894 /// \param LB Address of the output variable in which the lower iteration
1895 /// number is returned.
1896 /// \param UB Address of the output variable in which the upper iteration
1897 /// number is returned.
1898 /// \param ST Address of the output variable in which the stride value is
1899 /// returned.
1900 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1901 unsigned IVSize, bool IVSigned, Address IL,
1902 Address LB, Address UB, Address ST) override;
1903
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001904 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001905 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1906 /// clause.
1907 /// \param NumThreads An integer value of threads.
1908 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1909 SourceLocation Loc) override;
1910
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001911 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001912 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1913 void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001914 llvm::omp::ProcBindKind ProcBind,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001915 SourceLocation Loc) override;
1916
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001917 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001918 /// thread.
1919 /// \param VD Threadprivate variable.
1920 /// \param VDAddr Address of the global variable \a VD.
1921 /// \param Loc Location of the reference to threadprivate var.
1922 /// \return Address of the threadprivate variable for the current thread.
1923 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1924 Address VDAddr, SourceLocation Loc) override;
1925
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001926 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001927 /// a call to runtime library which adds initial value to the newly created
1928 /// threadprivate variable (if it is not constant) and registers destructor
1929 /// for the variable (if any).
1930 /// \param VD Threadprivate variable.
1931 /// \param VDAddr Address of the global variable \a VD.
1932 /// \param Loc Location of threadprivate declaration.
1933 /// \param PerformInit true if initialization expression is not constant.
1934 llvm::Function *
1935 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1936 SourceLocation Loc, bool PerformInit,
1937 CodeGenFunction *CGF = nullptr) override;
1938
1939 /// Creates artificial threadprivate variable with name \p Name and type \p
1940 /// VarType.
1941 /// \param VarType Type of the artificial threadprivate variable.
1942 /// \param Name Name of the artificial threadprivate variable.
1943 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1944 QualType VarType,
1945 StringRef Name) override;
1946
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001947 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001948 /// \param Vars List of variables to flush.
1949 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1950 SourceLocation Loc) override;
1951
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001952 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001953 /// emitted in several steps:
1954 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1955 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1956 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1957 /// function:
1958 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1959 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1960 /// return 0;
1961 /// }
1962 /// 2. Copy a list of shared variables to field shareds of the resulting
1963 /// structure kmp_task_t returned by the previous call (if any).
1964 /// 3. Copy a pointer to destructions function to field destructions of the
1965 /// resulting structure kmp_task_t.
1966 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1967 /// kmp_task_t *new_task), where new_task is a resulting structure from
1968 /// previous items.
1969 /// \param D Current task directive.
1970 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1971 /// /*part_id*/, captured_struct */*__context*/);
1972 /// \param SharedsTy A type which contains references the shared variables.
1973 /// \param Shareds Context with the list of shared variables from the \p
1974 /// TaskFunction.
1975 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1976 /// otherwise.
1977 /// \param Data Additional data for task generation like tiednsee, final
1978 /// state, list of privates etc.
1979 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001980 const OMPExecutableDirective &D,
1981 llvm::Function *TaskFunction, QualType SharedsTy,
1982 Address Shareds, const Expr *IfCond,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001983 const OMPTaskDataTy &Data) override;
1984
1985 /// Emit task region for the taskloop directive. The taskloop region is
1986 /// emitted in several steps:
1987 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1988 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1989 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1990 /// function:
1991 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1992 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1993 /// return 0;
1994 /// }
1995 /// 2. Copy a list of shared variables to field shareds of the resulting
1996 /// structure kmp_task_t returned by the previous call (if any).
1997 /// 3. Copy a pointer to destructions function to field destructions of the
1998 /// resulting structure kmp_task_t.
1999 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
2000 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
2001 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
2002 /// is a resulting structure from
2003 /// previous items.
2004 /// \param D Current task directive.
2005 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2006 /// /*part_id*/, captured_struct */*__context*/);
2007 /// \param SharedsTy A type which contains references the shared variables.
2008 /// \param Shareds Context with the list of shared variables from the \p
2009 /// TaskFunction.
2010 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2011 /// otherwise.
2012 /// \param Data Additional data for task generation like tiednsee, final
2013 /// state, list of privates etc.
2014 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002015 const OMPLoopDirective &D, llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002016 QualType SharedsTy, Address Shareds, const Expr *IfCond,
2017 const OMPTaskDataTy &Data) override;
2018
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002019 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002020 /// reduction:
2021 /// \code
2022 ///
2023 /// static kmp_critical_name lock = { 0 };
2024 ///
2025 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
2026 /// ...
2027 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
2028 /// ...
2029 /// }
2030 ///
2031 /// ...
2032 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
2033 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
2034 /// RedList, reduce_func, &<lock>)) {
2035 /// case 1:
2036 /// ...
2037 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
2038 /// ...
2039 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
2040 /// break;
2041 /// case 2:
2042 /// ...
2043 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
2044 /// ...
2045 /// break;
2046 /// default:;
2047 /// }
2048 /// \endcode
2049 ///
2050 /// \param Privates List of private copies for original reduction arguments.
2051 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
2052 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
2053 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
2054 /// or 'operator binop(LHS, RHS)'.
2055 /// \param Options List of options for reduction codegen:
2056 /// WithNowait true if parent directive has also nowait clause, false
2057 /// otherwise.
2058 /// SimpleReduction Emit reduction operation only. Used for omp simd
2059 /// directive on the host.
2060 /// ReductionKind The kind of reduction to perform.
2061 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2062 ArrayRef<const Expr *> Privates,
2063 ArrayRef<const Expr *> LHSExprs,
2064 ArrayRef<const Expr *> RHSExprs,
2065 ArrayRef<const Expr *> ReductionOps,
2066 ReductionOptionsTy Options) override;
2067
2068 /// Emit a code for initialization of task reduction clause. Next code
2069 /// should be emitted for reduction:
2070 /// \code
2071 ///
2072 /// _task_red_item_t red_data[n];
2073 /// ...
2074 /// red_data[i].shar = &origs[i];
2075 /// red_data[i].size = sizeof(origs[i]);
2076 /// red_data[i].f_init = (void*)RedInit<i>;
2077 /// red_data[i].f_fini = (void*)RedDest<i>;
2078 /// red_data[i].f_comb = (void*)RedOp<i>;
2079 /// red_data[i].flags = <Flag_i>;
2080 /// ...
2081 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2082 /// \endcode
2083 ///
2084 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2085 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2086 /// \param Data Additional data for task generation like tiedness, final
2087 /// state, list of privates, reductions etc.
2088 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2089 ArrayRef<const Expr *> LHSExprs,
2090 ArrayRef<const Expr *> RHSExprs,
2091 const OMPTaskDataTy &Data) override;
2092
2093 /// Required to resolve existing problems in the runtime. Emits threadprivate
2094 /// variables to store the size of the VLAs/array sections for
2095 /// initializer/combiner/finalizer functions + emits threadprivate variable to
2096 /// store the pointer to the original reduction item for the custom
2097 /// initializer defined by declare reduction construct.
2098 /// \param RCG Allows to reuse an existing data for the reductions.
2099 /// \param N Reduction item for which fixups must be emitted.
2100 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2101 ReductionCodeGen &RCG, unsigned N) override;
2102
2103 /// Get the address of `void *` type of the privatue copy of the reduction
2104 /// item specified by the \p SharedLVal.
2105 /// \param ReductionsPtr Pointer to the reduction data returned by the
2106 /// emitTaskReductionInit function.
2107 /// \param SharedLVal Address of the original reduction item.
2108 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2109 llvm::Value *ReductionsPtr,
2110 LValue SharedLVal) override;
2111
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002112 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002113 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2114
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002115 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002116 /// \param CancelRegion Region kind for which the cancellation point must be
2117 /// emitted.
2118 ///
2119 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2120 OpenMPDirectiveKind CancelRegion) override;
2121
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002122 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002123 /// \param IfCond Condition in the associated 'if' clause, if it was
2124 /// specified, nullptr otherwise.
2125 /// \param CancelRegion Region kind for which the cancel must be emitted.
2126 ///
2127 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2128 const Expr *IfCond,
2129 OpenMPDirectiveKind CancelRegion) override;
2130
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002131 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002132 /// \param D Directive to emit.
2133 /// \param ParentName Name of the function that encloses the target region.
2134 /// \param OutlinedFn Outlined function value to be defined by this call.
2135 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2136 /// \param IsOffloadEntry True if the outlined function is an offload entry.
2137 /// \param CodeGen Code generation sequence for the \a D directive.
2138 /// An outlined function may not be an entry if, e.g. the if clause always
2139 /// evaluates to false.
2140 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
2141 StringRef ParentName,
2142 llvm::Function *&OutlinedFn,
2143 llvm::Constant *&OutlinedFnID,
2144 bool IsOffloadEntry,
2145 const RegionCodeGenTy &CodeGen) override;
2146
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002147 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002148 /// code attempts offloading the execution to the device, an the event of
2149 /// a failure it executes the host version outlined in \a OutlinedFn.
2150 /// \param D Directive to emit.
2151 /// \param OutlinedFn Host version of the code to be offloaded.
2152 /// \param OutlinedFnID ID of host version of the code to be offloaded.
2153 /// \param IfCond Expression evaluated in if clause associated with the target
2154 /// directive, or null if no if clause is used.
2155 /// \param Device Expression evaluated in device clause associated with the
2156 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00002157 void
2158 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2159 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
2160 const Expr *IfCond, const Expr *Device,
2161 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2162 const OMPLoopDirective &D)>
2163 SizeEmitter) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002164
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002165 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002166 /// the function itself in case it is a valid device function. Returns true if
2167 /// \a GD was dealt with successfully.
2168 /// \param GD Function to scan.
2169 bool emitTargetFunctions(GlobalDecl GD) override;
2170
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002171 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002172 /// Returns true if \a GD was dealt with successfully.
2173 /// \param GD Variable declaration to emit.
2174 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2175
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002176 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002177 /// if it was emitted successfully.
2178 /// \param GD Global to scan.
2179 bool emitTargetGlobal(GlobalDecl GD) override;
2180
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002181 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002182 /// variables captured in a record which address is stored in \a
2183 /// CapturedStruct.
2184 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2185 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2186 /// \param CapturedVars A pointer to the record with the references to
2187 /// variables used in \a OutlinedFn function.
2188 ///
2189 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002190 SourceLocation Loc, llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002191 ArrayRef<llvm::Value *> CapturedVars) override;
2192
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002193 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002194 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2195 /// for num_teams clause.
2196 /// \param NumTeams An integer expression of teams.
2197 /// \param ThreadLimit An integer expression of threads.
2198 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2199 const Expr *ThreadLimit, SourceLocation Loc) override;
2200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002201 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002202 /// \param D Directive to emit.
2203 /// \param IfCond Expression evaluated in if clause associated with the
2204 /// target directive, or null if no device clause is used.
2205 /// \param Device Expression evaluated in device clause associated with the
2206 /// target directive, or null if no device clause is used.
2207 /// \param Info A record used to store information that needs to be preserved
2208 /// until the region is closed.
2209 void emitTargetDataCalls(CodeGenFunction &CGF,
2210 const OMPExecutableDirective &D, const Expr *IfCond,
2211 const Expr *Device, const RegionCodeGenTy &CodeGen,
2212 TargetDataInfo &Info) override;
2213
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002214 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002215 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2216 /// \param D Directive to emit.
2217 /// \param IfCond Expression evaluated in if clause associated with the target
2218 /// directive, or null if no if clause is used.
2219 /// \param Device Expression evaluated in device clause associated with the
2220 /// target directive, or null if no device clause is used.
2221 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2222 const OMPExecutableDirective &D,
2223 const Expr *IfCond,
2224 const Expr *Device) override;
2225
2226 /// Emit initialization for doacross loop nesting support.
2227 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002228 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2229 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002230
2231 /// Emit code for doacross ordered directive with 'depend' clause.
2232 /// \param C 'depend' clause with 'sink|source' dependency kind.
2233 void emitDoacrossOrdered(CodeGenFunction &CGF,
2234 const OMPDependClause *C) override;
2235
2236 /// Translates the native parameter of outlined function if this is required
2237 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002238 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002239 /// \param NativeParam Parameter itself.
2240 const VarDecl *translateParameter(const FieldDecl *FD,
2241 const VarDecl *NativeParam) const override;
2242
2243 /// Gets the address of the native argument basing on the address of the
2244 /// target-specific parameter.
2245 /// \param NativeParam Parameter itself.
2246 /// \param TargetParam Corresponding target-specific parameter.
2247 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2248 const VarDecl *TargetParam) const override;
Alexey Bataev4f680db2019-03-19 16:41:16 +00002249
2250 /// Gets the OpenMP-specific address of the local variable.
2251 Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2252 const VarDecl *VD) override {
2253 return Address::invalid();
2254 }
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002255};
2256
Alexey Bataev23b69422014-06-18 07:08:49 +00002257} // namespace CodeGen
2258} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002259
2260#endif