blob: a4fe15eee26acc5138fc28ae3141ee058d9562f4 [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 Bataev45588422020-01-07 14:11:45 -050023#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000024#include "llvm/ADT/StringMap.h"
Alexey Bataev2a6f3f52018-11-07 19:11:14 +000025#include "llvm/ADT/StringSet.h"
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060026#include "llvm/Frontend/OpenMP/OMPConstants.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000027#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000028#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000029
30namespace llvm {
31class ArrayType;
32class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000033class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000034class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000035class StructType;
36class Type;
37class Value;
38} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000039
Alexey Bataev9959db52014-05-06 10:08:46 +000040namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000041class Expr;
Alexey Bataev8b427062016-05-25 12:36:08 +000042class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000043class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000044class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000045class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000046class OMPDeclareReductionDecl;
47class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000048
Alexey Bataev9959db52014-05-06 10:08:46 +000049namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000050class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000051class CodeGenFunction;
52class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000053
Alexey Bataev14fa1c62016-03-29 05:34:15 +000054/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
55/// region.
56class PrePostActionTy {
57public:
58 explicit PrePostActionTy() {}
59 virtual void Enter(CodeGenFunction &CGF) {}
60 virtual void Exit(CodeGenFunction &CGF) {}
61 virtual ~PrePostActionTy() {}
62};
63
64/// Class provides a way to call simple version of codegen for OpenMP region, or
65/// an advanced with possible pre|post-actions in codegen.
66class RegionCodeGenTy final {
67 intptr_t CodeGen;
68 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
69 CodeGenTy Callback;
70 mutable PrePostActionTy *PrePostAction;
71 RegionCodeGenTy() = delete;
72 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
73 template <typename Callable>
74 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
75 PrePostActionTy &Action) {
76 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
77 }
78
79public:
80 template <typename Callable>
81 RegionCodeGenTy(
82 Callable &&CodeGen,
Justin Lebar027eb712020-02-10 23:23:44 -080083 std::enable_if_t<!std::is_same<std::remove_reference_t<Callable>,
84 RegionCodeGenTy>::value> * = nullptr)
Alexey Bataev14fa1c62016-03-29 05:34:15 +000085 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
Justin Lebar027eb712020-02-10 23:23:44 -080086 Callback(CallbackFn<std::remove_reference_t<Callable>>),
Alexey Bataev14fa1c62016-03-29 05:34:15 +000087 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
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500225 /// Maps the expression for the lastprivate variable to the global copy used
226 /// to store new value because original variables are not mapped in inner
227 /// parallel regions. Only private copies are captured but we need also to
228 /// store private copy in shared address.
229 /// Also, stores the expression for the private loop counter and it
230 /// threaprivate name.
231 struct LastprivateConditionalData {
Alexey Bataev46978742020-01-30 10:46:11 -0500232 llvm::MapVector<CanonicalDeclPtr<const Decl>, SmallString<16>>
233 DeclToUniqueName;
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500234 LValue IVLVal;
Alexey Bataev46978742020-01-30 10:46:11 -0500235 llvm::Function *Fn = nullptr;
236 bool Disabled = false;
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500237 };
238 /// Manages list of lastprivate conditional decls for the specified directive.
239 class LastprivateConditionalRAII {
Alexey Bataev46978742020-01-30 10:46:11 -0500240 enum class ActionToDo {
241 DoNotPush,
242 PushAsLastprivateConditional,
243 DisableLastprivateConditional,
244 };
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500245 CodeGenModule &CGM;
Alexey Bataev46978742020-01-30 10:46:11 -0500246 ActionToDo Action = ActionToDo::DoNotPush;
247
248 /// Check and try to disable analysis of inner regions for changes in
249 /// lastprivate conditional.
250 void tryToDisableInnerAnalysis(const OMPExecutableDirective &S,
251 llvm::DenseSet<CanonicalDeclPtr<const Decl>>
252 &NeedToAddForLPCsAsDisabled) const;
253
254 LastprivateConditionalRAII(CodeGenFunction &CGF,
255 const OMPExecutableDirective &S);
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500256
257 public:
Alexey Bataev46978742020-01-30 10:46:11 -0500258 explicit LastprivateConditionalRAII(CodeGenFunction &CGF,
259 const OMPExecutableDirective &S,
260 LValue IVLVal);
261 static LastprivateConditionalRAII disable(CodeGenFunction &CGF,
262 const OMPExecutableDirective &S);
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500263 ~LastprivateConditionalRAII();
264 };
265
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000266protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000267 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000268 StringRef FirstSeparator, Separator;
269
270 /// Constructor allowing to redefine the name separator for the variables.
271 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
272 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000273
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000274 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000275 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000276 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000277 uint64_t Size, int32_t Flags,
278 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000279
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000280 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000281 /// \param D Directive to emit.
282 /// \param ParentName Name of the function that encloses the target region.
283 /// \param OutlinedFn Outlined function value to be defined by this call.
284 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
285 /// \param IsOffloadEntry True if the outlined function is an offload entry.
286 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000287 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000288 /// evaluates to false.
289 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
290 StringRef ParentName,
291 llvm::Function *&OutlinedFn,
292 llvm::Constant *&OutlinedFnID,
293 bool IsOffloadEntry,
294 const RegionCodeGenTy &CodeGen);
295
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000296 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000297 /// \param Flags Flags for OpenMP location.
298 ///
299 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
300 unsigned Flags = 0);
301
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000303 llvm::Type *getIdentTyPointerTy();
304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000305 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000306 ///
307 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
308
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000309 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000310 // The name can be customized depending on the target.
311 //
312 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
313
Alexey Bataev3c595a62017-08-14 15:01:03 +0000314 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
James Y Knight9871db02019-02-05 16:42:33 +0000315 void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
316 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000317 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000318
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000319 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000320 /// stored.
321 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
322
Alexey Bataevfd006c42018-10-05 15:08:53 +0000323 void setLocThreadIdInsertPt(CodeGenFunction &CGF,
324 bool AtCurrentPoint = false);
325 void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
326
Alexey Bataevceeaa482018-11-21 21:04:34 +0000327 /// Check if the default location must be constant.
328 /// Default is false to support OMPT/OMPD.
329 virtual bool isDefaultLocationConstant() const { return false; }
330
331 /// Returns additional flags that can be stored in reserved_2 field of the
332 /// default location.
333 virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
334
Alexey Bataevc2cd2d42019-10-10 17:28:10 +0000335 /// Tries to emit declare variant function for \p OldGD from \p NewGD.
336 /// \param OrigAddr LLVM IR value for \p OldGD.
337 /// \param IsForDefinition true, if requested emission for the definition of
338 /// \p OldGD.
339 /// \returns true, was able to emit a definition function for \p OldGD, which
340 /// points to \p NewGD.
341 virtual bool tryEmitDeclareVariant(const GlobalDecl &NewGD,
342 const GlobalDecl &OldGD,
343 llvm::GlobalValue *OrigAddr,
344 bool IsForDefinition);
345
Alexey Bataevc3028ca2018-12-04 15:03:25 +0000346 /// Returns default flags for the barriers depending on the directive, for
347 /// which this barier is going to be emitted.
348 static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
349
Alexey Bataeva1166022018-11-27 21:24:54 +0000350 /// Get the LLVM type for the critical name.
351 llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
352
353 /// Returns corresponding lock object for the specified critical region
354 /// name. If the lock object does not exist it is created, otherwise the
355 /// reference to the existing copy is returned.
356 /// \param CriticalName Name of the critical region.
357 ///
358 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
359
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000360private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000361 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000362 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000363 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataevceeaa482018-11-21 21:04:34 +0000364 using FlagsTy = std::pair<unsigned, unsigned>;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000365 /// Map of flags and corresponding default locations.
Alexey Bataevceeaa482018-11-21 21:04:34 +0000366 using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
Alexey Bataev15007ba2014-05-07 06:18:01 +0000367 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000368 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000369
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000370 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000371 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000372 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000373 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
374 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000375 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000376 /// Original representation is:
377 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000378 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000379 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000380 struct DebugLocThreadIdTy {
381 llvm::Value *DebugLoc;
382 llvm::Value *ThreadID;
Alexey Bataevfd006c42018-10-05 15:08:53 +0000383 /// Insert point for the service instructions.
384 llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000385 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000386 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000387 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
388 OpenMPLocThreadIDMapTy;
389 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000390 /// Map of UDRs and corresponding combiner/initializer.
391 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
392 std::pair<llvm::Function *, llvm::Function *>>
393 UDRMapTy;
394 UDRMapTy UDRMap;
395 /// Map of functions and locally defined UDRs.
396 typedef llvm::DenseMap<llvm::Function *,
397 SmallVector<const OMPDeclareReductionDecl *, 4>>
398 FunctionUDRMapTy;
399 FunctionUDRMapTy FunctionUDRMap;
Michael Krused47b9432019-08-05 18:43:21 +0000400 /// Map from the user-defined mapper declaration to its corresponding
401 /// functions.
402 llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
403 /// Map of functions and their local user-defined mappers.
404 using FunctionUDMMapTy =
405 llvm::DenseMap<llvm::Function *,
406 SmallVector<const OMPDeclareMapperDecl *, 4>>;
407 FunctionUDMMapTy FunctionUDMMap;
Alexey Bataev46978742020-01-30 10:46:11 -0500408 /// Maps local variables marked as lastprivate conditional to their internal
409 /// types.
410 llvm::DenseMap<llvm::Function *,
411 llvm::DenseMap<CanonicalDeclPtr<const Decl>,
412 std::tuple<QualType, const FieldDecl *,
413 const FieldDecl *, LValue>>>
414 LastprivateConditionalToTypes;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000415 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000416 /// kmp_critical_name[8];
417 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000418 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000419 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
420 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
421 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
422 /// variables.
423 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
424 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000425 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000426 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000427 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000428 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000429 /// void * shareds; /**< pointer to block of pointers to
430 /// shared vars */
431 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
432 /// executing task */
433 /// kmp_int32 part_id; /**< part id for the task */
434 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
435 /// deconstructors of firstprivate C++ objects */
436 /// } kmp_task_t;
437 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000438 /// Saved kmp_task_t for task directive.
439 QualType SavedKmpTaskTQTy;
440 /// Saved kmp_task_t for taskloop-based directive.
441 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000442 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000443 /// kmp_intptr_t base_addr;
444 /// size_t len;
445 /// struct {
446 /// bool in:1;
447 /// bool out:1;
448 /// } flags;
449 /// } kmp_depend_info_t;
450 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000451 /// struct kmp_dim { // loop bounds info casted to kmp_int64
452 /// kmp_int64 lo; // lower
453 /// kmp_int64 up; // upper
454 /// kmp_int64 st; // stride
455 /// };
456 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000457 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000458 /// void *addr; // Pointer to the offload entry info.
459 /// // (function or global)
460 /// char *name; // Name of the function or global.
461 /// size_t size; // Size of the entry info (0 if it a function).
Alexey Bataev4c117032020-01-09 09:28:59 -0500462 /// int32_t flags;
463 /// int32_t reserved;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000464 /// };
465 QualType TgtOffloadEntryQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000466 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000467 /// far.
468 class OffloadEntriesInfoManagerTy {
469 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000470
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000471 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000472 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000473
474 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000475 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000476 class OffloadEntryInfo {
477 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000478 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000479 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000480 /// Entry is a target region.
481 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000482 /// Entry is a declare target variable.
483 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000484 /// Invalid entry info.
485 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000486 };
487
Alexey Bataev03f270c2018-03-30 18:31:07 +0000488 protected:
489 OffloadEntryInfo() = delete;
490 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000491 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000492 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000493 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000494 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000495
Alexey Bataev03f270c2018-03-30 18:31:07 +0000496 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000497 bool isValid() const { return Order != ~0u; }
498 unsigned getOrder() const { return Order; }
499 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000500 uint32_t getFlags() const { return Flags; }
501 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
502 llvm::Constant *getAddress() const {
503 return cast_or_null<llvm::Constant>(Addr);
504 }
505 void setAddress(llvm::Constant *V) {
506 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
507 Addr = V;
508 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000509 static bool classof(const OffloadEntryInfo *Info) { return true; }
510
Samuel Antaof83efdb2017-01-05 16:02:49 +0000511 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000512 /// Address of the entity that has to be mapped for offloading.
513 llvm::WeakTrackingVH Addr;
514
Samuel Antaof83efdb2017-01-05 16:02:49 +0000515 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000516 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000517
518 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000519 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000520
Alexey Bataev03f270c2018-03-30 18:31:07 +0000521 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000522 };
523
Alexey Bataev03f270c2018-03-30 18:31:07 +0000524 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000525 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000526 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000527 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000528 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000529
Alexey Bataev03f270c2018-03-30 18:31:07 +0000530 //
531 // Target region entries related.
532 //
533
534 /// Kind of the target registry entry.
535 enum OMPTargetRegionEntryKind : uint32_t {
536 /// Mark the entry as target region.
537 OMPTargetRegionEntryTargetRegion = 0x0,
538 /// Mark the entry as a global constructor.
539 OMPTargetRegionEntryCtor = 0x02,
540 /// Mark the entry as a global destructor.
541 OMPTargetRegionEntryDtor = 0x04,
542 };
543
544 /// Target region entries info.
545 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
546 /// Address that can be used as the ID of the entry.
547 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000548
549 public:
550 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000551 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000552 explicit OffloadEntryInfoTargetRegion(unsigned Order,
553 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000554 llvm::Constant *ID,
555 OMPTargetRegionEntryKind Flags)
556 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000557 ID(ID) {
558 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000559 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000560
561 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000562 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000563 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000564 ID = V;
565 }
566 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000567 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000568 }
569 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000570
571 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000572 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
573 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000574 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000575 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000576 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
577 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000578 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000579 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000580 /// Return true if a target region entry with the provided information
581 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000582 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000583 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000584 /// brief Applies action \a Action on all registered entries.
585 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000586 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000587 OffloadTargetRegionEntryInfoActTy;
588 void actOnTargetRegionEntriesInfo(
589 const OffloadTargetRegionEntryInfoActTy &Action);
590
Alexey Bataev03f270c2018-03-30 18:31:07 +0000591 //
592 // Device global variable entries related.
593 //
594
595 /// Kind of the global variable entry..
596 enum OMPTargetGlobalVarEntryKind : uint32_t {
597 /// Mark the entry as a to declare target.
598 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000599 /// Mark the entry as a to declare target link.
600 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000601 };
602
603 /// Device global variable entries info.
604 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
605 /// Type of the global variable.
606 CharUnits VarSize;
607 llvm::GlobalValue::LinkageTypes Linkage;
608
609 public:
610 OffloadEntryInfoDeviceGlobalVar()
611 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
612 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
613 OMPTargetGlobalVarEntryKind Flags)
614 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
615 explicit OffloadEntryInfoDeviceGlobalVar(
616 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
617 OMPTargetGlobalVarEntryKind Flags,
618 llvm::GlobalValue::LinkageTypes Linkage)
619 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
620 VarSize(VarSize), Linkage(Linkage) {
621 setAddress(Addr);
622 }
623
624 CharUnits getVarSize() const { return VarSize; }
625 void setVarSize(CharUnits Size) { VarSize = Size; }
626 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
627 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
628 static bool classof(const OffloadEntryInfo *Info) {
629 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
630 }
631 };
632
633 /// Initialize device global variable entry.
634 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
635 OMPTargetGlobalVarEntryKind Flags,
636 unsigned Order);
637
638 /// Register device global variable entry.
639 void
640 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
641 CharUnits VarSize,
642 OMPTargetGlobalVarEntryKind Flags,
643 llvm::GlobalValue::LinkageTypes Linkage);
644 /// Checks if the variable with the given name has been registered already.
645 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
646 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
647 }
648 /// Applies action \a Action on all registered entries.
649 typedef llvm::function_ref<void(StringRef,
650 const OffloadEntryInfoDeviceGlobalVar &)>
651 OffloadDeviceGlobalVarEntryInfoActTy;
652 void actOnDeviceGlobalVarEntriesInfo(
653 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
654
Samuel Antaoee8fb302016-01-06 13:42:12 +0000655 private:
656 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000657 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000658 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000659 OffloadEntriesTargetRegionPerLine;
660 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
661 OffloadEntriesTargetRegionPerParentName;
662 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
663 OffloadEntriesTargetRegionPerFile;
664 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
665 OffloadEntriesTargetRegionPerDevice;
666 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
667 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000668 /// Storage for device global variable entries kind. The storage is to be
669 /// indexed by mangled name.
670 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
671 OffloadEntriesDeviceGlobalVarTy;
672 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000673 };
674 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
675
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000676 bool ShouldMarkAsGlobal = true;
Alexey Bataev45588422020-01-07 14:11:45 -0500677 /// List of the emitted declarations.
678 llvm::DenseSet<CanonicalDeclPtr<const Decl>> AlreadyEmittedTargetDecls;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000679 /// List of the global variables with their addresses that should not be
680 /// emitted for the target.
681 llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000682
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000683 /// List of variables that can become declare target implicitly and, thus,
684 /// must be emitted.
685 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
686
Alexey Bataev2df5f122019-10-01 20:18:32 +0000687 /// Mapping of the original functions to their variants and original global
688 /// decl.
689 llvm::MapVector<CanonicalDeclPtr<const FunctionDecl>,
690 std::pair<GlobalDecl, GlobalDecl>>
691 DeferredVariantFunction;
692
Alexey Bataev0860db92019-12-19 10:01:10 -0500693 using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
694 /// Stack for list of declarations in current context marked as nontemporal.
695 /// The set is the union of all current stack elements.
696 llvm::SmallVector<NontemporalDeclsSet, 4> NontemporalDeclsStack;
697
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500698 /// Stack for list of addresses of declarations in current context marked as
699 /// lastprivate conditional. The set is the union of all current stack
700 /// elements.
701 llvm::SmallVector<LastprivateConditionalData, 4> LastprivateConditionalStack;
702
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000703 /// Flag for keeping track of weather a requires unified_shared_memory
704 /// directive is present.
705 bool HasRequiresUnifiedSharedMemory = false;
706
707 /// Flag for keeping track of weather a target region has been emitted.
708 bool HasEmittedTargetRegion = false;
709
710 /// Flag for keeping track of weather a device routine has been emitted.
711 /// Device routines are specific to the
712 bool HasEmittedDeclareTargetRegion = false;
713
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000714 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000715 /// metadata.
716 void loadOffloadInfoMetadata();
717
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000718 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000719 QualType getTgtOffloadEntryQTy();
720
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000721 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000722 /// found along the way.
723 /// \param S Starting statement.
724 /// \param ParentName Name of the function declaration that is being scanned.
725 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000726
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000727 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000728 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000729
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000730 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000731 llvm::Type *getKmpc_MicroPointerTy();
732
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000733 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000734 /// \param Function OpenMP runtime function.
735 /// \return Specified function.
James Y Knight9871db02019-02-05 16:42:33 +0000736 llvm::FunctionCallee createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000737
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000738 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000739 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000740 llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
741 bool IVSigned);
Alexander Musman21212e42015-03-13 10:38:23 +0000742
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000743 /// Returns __kmpc_dispatch_init_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000744 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000745 llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize,
746 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000747
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000748 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000749 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000750 llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
751 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000752
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000753 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000754 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000755 llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
756 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000757
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000758 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000759 /// return threadprivate cache object. This object is a pointer's worth of
760 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000761 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000762 /// \return Cache variable for the specified threadprivate.
763 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
764
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000765 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000766 /// internal global variable with the specified Name. The created variable has
767 /// linkage CommonLinkage by default and is initialized by null value.
768 /// \param Ty Type of the global variable. If it is exist already the type
769 /// must be the same.
770 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000771 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000772 const llvm::Twine &Name,
773 unsigned AddressSpace = 0);
Alexey Bataev97720002014-11-11 04:05:39 +0000774
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000775 /// Set of threadprivate variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000776 llvm::StringSet<> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000777
Alexey Bataev34f8a702018-03-28 14:28:54 +0000778 /// Set of declare target variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000779 llvm::StringSet<> DeclareTargetWithDefinition;
Alexey Bataev34f8a702018-03-28 14:28:54 +0000780
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000781 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000782 /// \param VDAddr Address of the global variable \a VD.
783 /// \param Ctor Pointer to a global init function for \a VD.
784 /// \param CopyCtor Pointer to a global copy function for \a VD.
785 /// \param Dtor Pointer to a global destructor function for \a VD.
786 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000787 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000788 llvm::Value *Ctor, llvm::Value *CopyCtor,
789 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000790
Michael Krused47b9432019-08-05 18:43:21 +0000791 /// Emit the array initialization or deletion portion for user-defined mapper
792 /// code generation.
793 void emitUDMapperArrayInitOrDel(CodeGenFunction &MapperCGF,
794 llvm::Value *Handle, llvm::Value *BasePtr,
795 llvm::Value *Ptr, llvm::Value *Size,
796 llvm::Value *MapType, CharUnits ElementSize,
797 llvm::BasicBlock *ExitBB, bool IsInit);
798
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000799 struct TaskResultTy {
800 llvm::Value *NewTask = nullptr;
James Y Knight9871db02019-02-05 16:42:33 +0000801 llvm::Function *TaskEntry = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000802 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000803 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000804 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000805 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000806 };
807 /// Emit task region for the task directive. The task region is emitted in
808 /// several steps:
809 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
810 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
811 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
812 /// function:
813 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
814 /// TaskFunction(gtid, tt->part_id, tt->shareds);
815 /// return 0;
816 /// }
817 /// 2. Copy a list of shared variables to field shareds of the resulting
818 /// structure kmp_task_t returned by the previous call (if any).
819 /// 3. Copy a pointer to destructions function to field destructions of the
820 /// resulting structure kmp_task_t.
821 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000822 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
823 /// /*part_id*/, captured_struct */*__context*/);
824 /// \param SharedsTy A type which contains references the shared variables.
825 /// \param Shareds Context with the list of shared variables from the \p
826 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000827 /// \param Data Additional data for task generation like tiednsee, final
828 /// state, list of privates etc.
829 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
830 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +0000831 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000832 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000833
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000834 /// Returns default address space for the constant firstprivates, 0 by
835 /// default.
836 virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; }
837
Alexey Bataevec7946e2019-09-23 14:06:51 +0000838 /// Emit code that pushes the trip count of loops associated with constructs
839 /// 'target teams distribute' and 'teams distribute parallel for'.
840 /// \param SizeEmitter Emits the int64 value for the number of iterations of
841 /// the associated loop.
842 void emitTargetNumIterationsCall(
843 CodeGenFunction &CGF, const OMPExecutableDirective &D,
844 llvm::Value *DeviceID,
845 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
846 const OMPLoopDirective &D)>
847 SizeEmitter);
848
Alexey Bataev46978742020-01-30 10:46:11 -0500849 /// Emit update for lastprivate conditional data.
850 void emitLastprivateConditionalUpdate(CodeGenFunction &CGF, LValue IVLVal,
851 StringRef UniqueDeclName, LValue LVal,
852 SourceLocation Loc);
853
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000854public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000855 explicit CGOpenMPRuntime(CodeGenModule &CGM)
856 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000857 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000858 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000859
Alexey Bataevd08c0562019-11-19 12:07:54 -0500860 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
861 /// function. Here is the logic:
862 /// if (Cond) {
863 /// ThenGen();
864 /// } else {
865 /// ElseGen();
866 /// }
867 void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
868 const RegionCodeGenTy &ThenGen,
869 const RegionCodeGenTy &ElseGen);
870
Alexey Bataev5c427362019-04-10 19:11:33 +0000871 /// Checks if the \p Body is the \a CompoundStmt and returns its child
872 /// statement iff there is only one that is not evaluatable at the compile
873 /// time.
874 static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
875
Alexey Bataev18fa2322018-05-02 14:20:50 +0000876 /// Get the platform-specific name separator.
877 std::string getName(ArrayRef<StringRef> Parts) const;
878
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000879 /// Emit code for the specified user defined reduction construct.
880 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
881 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000882 /// Get combiner/initializer for the specified user-defined reduction, if any.
883 virtual std::pair<llvm::Function *, llvm::Function *>
884 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000885
Michael Krused47b9432019-08-05 18:43:21 +0000886 /// Emit the function for the user defined mapper construct.
887 void emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
888 CodeGenFunction *CGF = nullptr);
889
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000890 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000891 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
892 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000893 /// \param D OpenMP directive.
894 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000895 /// \param InnermostKind Kind of innermost directive (for simple directives it
896 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000897 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000898 virtual llvm::Function *emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000899 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
900 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
901
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000902 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000903 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
904 /// kmp_int32 BoundID, struct context_vars*).
905 /// \param D OpenMP directive.
906 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
907 /// \param InnermostKind Kind of innermost directive (for simple directives it
908 /// is a directive itself, for combined - its innermost directive).
909 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000910 virtual llvm::Function *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000911 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
912 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000913
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000914 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000915 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
916 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000917 /// \param D OpenMP directive.
918 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000919 /// \param PartIDVar Variable for partition id in the current OpenMP untied
920 /// task region.
921 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000922 /// \param InnermostKind Kind of innermost directive (for simple directives it
923 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000924 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000925 /// \param Tied true if task is generated for tied task, false otherwise.
926 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
927 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000928 ///
James Y Knight9871db02019-02-05 16:42:33 +0000929 virtual llvm::Function *emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000930 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000931 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
932 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
933 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000934
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000935 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000936 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000937 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000938
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000939 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000940 /// variables captured in a record which address is stored in \a
941 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000942 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000943 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000944 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000945 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000946 /// \param IfCond Condition in the associated 'if' clause, if it was
947 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000948 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000949 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +0000950 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000951 ArrayRef<llvm::Value *> CapturedVars,
952 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000953
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000954 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000955 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000956 /// \param CriticalOpGen Generator for the statement associated with the given
957 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000958 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000959 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000960 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000961 SourceLocation Loc,
962 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000963
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000964 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000965 /// \param MasterOpGen Generator for the statement associated with the given
966 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000967 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000968 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000969 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000970
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000971 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000972 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000973
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000974 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000975 /// \param TaskgroupOpGen Generator for the statement associated with the
976 /// given taskgroup region.
977 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
978 const RegionCodeGenTy &TaskgroupOpGen,
979 SourceLocation Loc);
980
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000981 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000982 /// \param SingleOpGen Generator for the statement associated with the given
983 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000984 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000985 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000986 SourceLocation Loc,
987 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000988 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000989 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000990 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000991
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000992 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000993 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000994 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000995 virtual void emitOrderedRegion(CodeGenFunction &CGF,
996 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000997 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000998
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000999 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +00001000 /// \param Kind Directive for which this implicit barrier call must be
1001 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001002 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1003 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1004 /// runtime class decides which one to emit (simple or with cancellation
1005 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001006 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001007 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001008 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001009 bool EmitChecks = true,
1010 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +00001011
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001012 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +00001013 /// This kind of worksharing directive is emitted without outer loop.
1014 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1015 /// \param Chunked True if chunk is specified in the clause.
1016 ///
1017 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
1018 bool Chunked) const;
1019
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001020 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001021 /// This kind of distribute directive is emitted without outer loop.
1022 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1023 /// \param Chunked True if chunk is specified in the clause.
1024 ///
1025 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
1026 bool Chunked) const;
1027
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00001028 /// Check if the specified \a ScheduleKind is static chunked.
1029 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1030 /// \param Chunked True if chunk is specified in the clause.
1031 ///
1032 virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
1033 bool Chunked) const;
1034
1035 /// Check if the specified \a ScheduleKind is static non-chunked.
1036 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1037 /// \param Chunked True if chunk is specified in the clause.
1038 ///
1039 virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
1040 bool Chunked) const;
1041
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001042 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001043 /// This kind of worksharing directive is emitted without outer loop.
1044 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
1045 ///
1046 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
1047
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001048 /// struct with the values to be passed to the dispatch runtime function
1049 struct DispatchRTInput {
1050 /// Loop lower bound
1051 llvm::Value *LB = nullptr;
1052 /// Loop upper bound
1053 llvm::Value *UB = nullptr;
1054 /// Chunk size specified using 'schedule' clause (nullptr if chunk
1055 /// was not specified)
1056 llvm::Value *Chunk = nullptr;
1057 DispatchRTInput() = default;
1058 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
1059 : LB(LB), UB(UB), Chunk(Chunk) {}
1060 };
1061
1062 /// Call the appropriate runtime routine to initialize it before start
1063 /// of loop.
1064
1065 /// This is used for non static scheduled types and when the ordered
1066 /// clause is present on the loop construct.
1067 /// Depending on the loop schedule, it is necessary to call some runtime
1068 /// routine before start of the OpenMP loop to get the loop upper / lower
1069 /// bounds \a LB and \a UB and stride \a ST.
1070 ///
1071 /// \param CGF Reference to current CodeGenFunction.
1072 /// \param Loc Clang source location.
1073 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1074 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001075 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001076 /// \param Ordered true if loop is ordered, false otherwise.
1077 /// \param DispatchValues struct containing llvm values for lower bound, upper
1078 /// bound, and chunk expression.
1079 /// For the default (nullptr) value, the chunk 1 will be used.
1080 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001081 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001082 const OpenMPScheduleTy &ScheduleKind,
1083 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001084 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001085
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001086 /// Struct with the values to be passed to the static runtime function
1087 struct StaticRTInput {
1088 /// Size of the iteration variable in bits.
1089 unsigned IVSize = 0;
1090 /// Sign of the iteration variable.
1091 bool IVSigned = false;
1092 /// true if loop is ordered, false otherwise.
1093 bool Ordered = false;
1094 /// Address of the output variable in which the flag of the last iteration
1095 /// is returned.
1096 Address IL = Address::invalid();
1097 /// Address of the output variable in which the lower iteration number is
1098 /// returned.
1099 Address LB = Address::invalid();
1100 /// Address of the output variable in which the upper iteration number is
1101 /// returned.
1102 Address UB = Address::invalid();
1103 /// Address of the output variable in which the stride value is returned
1104 /// necessary to generated the static_chunked scheduled loop.
1105 Address ST = Address::invalid();
1106 /// Value of the chunk for the static_chunked scheduled loop. For the
1107 /// default (nullptr) value, the chunk 1 will be used.
1108 llvm::Value *Chunk = nullptr;
1109 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1110 Address LB, Address UB, Address ST,
1111 llvm::Value *Chunk = nullptr)
1112 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1113 UB(UB), ST(ST), Chunk(Chunk) {}
1114 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001115 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +00001116 /// of loop.
1117 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001118 /// This is used only in case of static schedule, when the user did not
1119 /// specify a ordered clause on the loop construct.
1120 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +00001121 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001122 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +00001123 ///
1124 /// \param CGF Reference to current CodeGenFunction.
1125 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001126 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001127 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001128 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +00001129 ///
John McCall7f416cc2015-09-08 08:05:57 +00001130 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001131 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001132 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001133 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00001134
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001135 ///
1136 /// \param CGF Reference to current CodeGenFunction.
1137 /// \param Loc Clang source location.
1138 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001139 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001140 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001141 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1142 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001143 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001144 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001145
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001146 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001147 /// iteration of the ordered loop with the dynamic scheduling.
1148 ///
1149 /// \param CGF Reference to current CodeGenFunction.
1150 /// \param Loc Clang source location.
1151 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001152 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001153 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001154 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1155 SourceLocation Loc, unsigned IVSize,
1156 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001157
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001158 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001159 /// all the work with current loop.
1160 ///
1161 /// \param CGF Reference to current CodeGenFunction.
1162 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001163 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001164 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001165 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1166 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001167
Alexander Musman92bdaab2015-03-12 13:37:50 +00001168 /// Call __kmpc_dispatch_next(
1169 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1170 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1171 /// kmp_int[32|64] *p_stride);
1172 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001173 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001174 /// \param IL Address of the output variable in which the flag of the
1175 /// last iteration is returned.
1176 /// \param LB Address of the output variable in which the lower iteration
1177 /// number is returned.
1178 /// \param UB Address of the output variable in which the upper iteration
1179 /// number is returned.
1180 /// \param ST Address of the output variable in which the stride value is
1181 /// returned.
1182 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1183 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001184 Address IL, Address LB,
1185 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001186
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001187 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001188 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1189 /// clause.
1190 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001191 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1192 llvm::Value *NumThreads,
1193 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001194
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001195 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001196 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1197 virtual void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001198 llvm::omp::ProcBindKind ProcBind,
Alexey Bataev7f210c62015-06-18 13:40:03 +00001199 SourceLocation Loc);
1200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001201 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001202 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001203 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001204 /// \param VDAddr Address of the global variable \a VD.
1205 /// \param Loc Location of the reference to threadprivate var.
1206 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001207 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1208 const VarDecl *VD,
1209 Address VDAddr,
1210 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001211
Alexey Bataev92327c52018-03-26 16:40:55 +00001212 /// Returns the address of the variable marked as declare target with link
Gheorghe-Teodor Bercea0034e842019-06-20 18:04:47 +00001213 /// clause OR as declare target with to clause and unified memory.
1214 virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001216 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001217 /// a call to runtime library which adds initial value to the newly created
1218 /// threadprivate variable (if it is not constant) and registers destructor
1219 /// for the variable (if any).
1220 /// \param VD Threadprivate variable.
1221 /// \param VDAddr Address of the global variable \a VD.
1222 /// \param Loc Location of threadprivate declaration.
1223 /// \param PerformInit true if initialization expression is not constant.
1224 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001225 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001226 SourceLocation Loc, bool PerformInit,
1227 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001229 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001230 /// \param VD Declare target variable.
1231 /// \param Addr Address of the global variable \a VD.
1232 /// \param PerformInit true if initialization expression is not constant.
1233 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1234 llvm::GlobalVariable *Addr,
1235 bool PerformInit);
1236
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001237 /// Creates artificial threadprivate variable with name \p Name and type \p
1238 /// VarType.
1239 /// \param VarType Type of the artificial threadprivate variable.
1240 /// \param Name Name of the artificial threadprivate variable.
1241 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1242 QualType VarType,
1243 StringRef Name);
1244
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001245 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001246 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001247 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
Alexey Bataeve8e05de2020-02-07 12:22:23 -05001248 SourceLocation Loc, llvm::AtomicOrdering AO);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001250 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001251 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001252 /// 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 kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1265 /// kmp_task_t *new_task), where new_task is a resulting structure from
1266 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001267 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001268 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1269 /// /*part_id*/, captured_struct */*__context*/);
1270 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001271 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001272 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001273 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1274 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001275 /// \param Data Additional data for task generation like tiednsee, final
1276 /// state, list of privates etc.
1277 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1278 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001279 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001280 Address Shareds, const Expr *IfCond,
1281 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001282
Alexey Bataev7292c292016-04-25 12:22:29 +00001283 /// Emit task region for the taskloop directive. The taskloop region is
1284 /// emitted in several steps:
1285 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1286 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1287 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1288 /// function:
1289 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1290 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1291 /// return 0;
1292 /// }
1293 /// 2. Copy a list of shared variables to field shareds of the resulting
1294 /// structure kmp_task_t returned by the previous call (if any).
1295 /// 3. Copy a pointer to destructions function to field destructions of the
1296 /// resulting structure kmp_task_t.
1297 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1298 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1299 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1300 /// is a resulting structure from
1301 /// previous items.
1302 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001303 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1304 /// /*part_id*/, captured_struct */*__context*/);
1305 /// \param SharedsTy A type which contains references the shared variables.
1306 /// \param Shareds Context with the list of shared variables from the \p
1307 /// TaskFunction.
1308 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1309 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001310 /// \param Data Additional data for task generation like tiednsee, final
1311 /// state, list of privates etc.
James Y Knight9871db02019-02-05 16:42:33 +00001312 virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1313 const OMPLoopDirective &D,
1314 llvm::Function *TaskFunction,
1315 QualType SharedsTy, Address Shareds,
1316 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001317
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001318 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001319 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001320 /// \param InnermostKind Kind of innermost directive (for simple directives it
1321 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001322 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001323 /// \param HasCancel true if region has inner cancel directive, false
1324 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001325 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001326 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001327 const RegionCodeGenTy &CodeGen,
1328 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001329
1330 /// Emits reduction function.
1331 /// \param ArgsType Array type containing pointers to reduction variables.
1332 /// \param Privates List of private copies for original reduction arguments.
1333 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1334 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1335 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1336 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev982a35e2019-03-19 17:09:52 +00001337 llvm::Function *emitReductionFunction(SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001338 llvm::Type *ArgsType,
1339 ArrayRef<const Expr *> Privates,
1340 ArrayRef<const Expr *> LHSExprs,
1341 ArrayRef<const Expr *> RHSExprs,
1342 ArrayRef<const Expr *> ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001343
1344 /// Emits single reduction combiner
1345 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1346 const Expr *ReductionOp,
1347 const Expr *PrivateRef,
1348 const DeclRefExpr *LHS,
1349 const DeclRefExpr *RHS);
1350
1351 struct ReductionOptionsTy {
1352 bool WithNowait;
1353 bool SimpleReduction;
1354 OpenMPDirectiveKind ReductionKind;
1355 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001356 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001357 /// reduction:
1358 /// \code
1359 ///
1360 /// static kmp_critical_name lock = { 0 };
1361 ///
1362 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1363 /// ...
1364 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1365 /// ...
1366 /// }
1367 ///
1368 /// ...
1369 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1370 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1371 /// RedList, reduce_func, &<lock>)) {
1372 /// case 1:
1373 /// ...
1374 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1375 /// ...
1376 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1377 /// break;
1378 /// case 2:
1379 /// ...
1380 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1381 /// ...
1382 /// break;
1383 /// default:;
1384 /// }
1385 /// \endcode
1386 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001387 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001388 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1389 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1390 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1391 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001392 /// \param Options List of options for reduction codegen:
1393 /// WithNowait true if parent directive has also nowait clause, false
1394 /// otherwise.
1395 /// SimpleReduction Emit reduction operation only. Used for omp simd
1396 /// directive on the host.
1397 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001398 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001399 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001400 ArrayRef<const Expr *> LHSExprs,
1401 ArrayRef<const Expr *> RHSExprs,
1402 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001403 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001404
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001405 /// Emit a code for initialization of task reduction clause. Next code
1406 /// should be emitted for reduction:
1407 /// \code
1408 ///
1409 /// _task_red_item_t red_data[n];
1410 /// ...
1411 /// red_data[i].shar = &origs[i];
1412 /// red_data[i].size = sizeof(origs[i]);
1413 /// red_data[i].f_init = (void*)RedInit<i>;
1414 /// red_data[i].f_fini = (void*)RedDest<i>;
1415 /// red_data[i].f_comb = (void*)RedOp<i>;
1416 /// red_data[i].flags = <Flag_i>;
1417 /// ...
1418 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1419 /// \endcode
1420 ///
1421 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1422 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1423 /// \param Data Additional data for task generation like tiedness, final
1424 /// state, list of privates, reductions etc.
1425 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1426 SourceLocation Loc,
1427 ArrayRef<const Expr *> LHSExprs,
1428 ArrayRef<const Expr *> RHSExprs,
1429 const OMPTaskDataTy &Data);
1430
1431 /// Required to resolve existing problems in the runtime. Emits threadprivate
1432 /// variables to store the size of the VLAs/array sections for
1433 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1434 /// store the pointer to the original reduction item for the custom
1435 /// initializer defined by declare reduction construct.
1436 /// \param RCG Allows to reuse an existing data for the reductions.
1437 /// \param N Reduction item for which fixups must be emitted.
1438 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1439 ReductionCodeGen &RCG, unsigned N);
1440
1441 /// Get the address of `void *` type of the privatue copy of the reduction
1442 /// item specified by the \p SharedLVal.
1443 /// \param ReductionsPtr Pointer to the reduction data returned by the
1444 /// emitTaskReductionInit function.
1445 /// \param SharedLVal Address of the original reduction item.
1446 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1447 llvm::Value *ReductionsPtr,
1448 LValue SharedLVal);
1449
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001450 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001451 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001452
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001453 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001454 /// \param CancelRegion Region kind for which the cancellation point must be
1455 /// emitted.
1456 ///
1457 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1458 SourceLocation Loc,
1459 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001460
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001461 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001462 /// \param IfCond Condition in the associated 'if' clause, if it was
1463 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001464 /// \param CancelRegion Region kind for which the cancel must be emitted.
1465 ///
1466 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001467 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001468 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001470 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001471 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001472 /// \param ParentName Name of the function that encloses the target region.
1473 /// \param OutlinedFn Outlined function value to be defined by this call.
1474 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1475 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001476 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001477 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001478 /// evaluates to false.
1479 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1480 StringRef ParentName,
1481 llvm::Function *&OutlinedFn,
1482 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001483 bool IsOffloadEntry,
1484 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001485
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001486 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001487 /// code attempts offloading the execution to the device, an the event of
1488 /// a failure it executes the host version outlined in \a OutlinedFn.
1489 /// \param D Directive to emit.
1490 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001491 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001492 /// \param IfCond Expression evaluated in if clause associated with the target
1493 /// directive, or null if no if clause is used.
1494 /// \param Device Expression evaluated in device clause associated with the
1495 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00001496 /// \param SizeEmitter Callback to emit number of iterations for loop-based
1497 /// directives.
1498 virtual void
1499 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1500 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
1501 const Expr *IfCond, const Expr *Device,
1502 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
1503 const OMPLoopDirective &D)>
1504 SizeEmitter);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001505
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001506 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001507 /// the function itself in case it is a valid device function. Returns true if
1508 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001509 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001510 virtual bool emitTargetFunctions(GlobalDecl GD);
1511
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001512 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001513 /// Returns true if \a GD was dealt with successfully.
1514 /// \param GD Variable declaration to emit.
1515 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1516
Alexey Bataev03f270c2018-03-30 18:31:07 +00001517 /// Checks if the provided global decl \a GD is a declare target variable and
1518 /// registers it when emitting code for the host.
1519 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1520 llvm::Constant *Addr);
1521
Alexey Bataev1af5bd52019-03-05 17:47:18 +00001522 /// Registers provided target firstprivate variable as global on the
1523 /// target.
1524 llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1525 const VarDecl *VD);
1526
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001527 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001528 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001529 /// \param GD Global to scan.
1530 virtual bool emitTargetGlobal(GlobalDecl GD);
1531
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001532 /// Creates and returns a registration function for when at least one
1533 /// requires directives was used in the current module.
1534 llvm::Function *emitRequiresDirectiveRegFun();
1535
Sergey Dmitriev5836c352019-10-15 18:42:47 +00001536 /// Creates all the offload entries in the current compilation unit
1537 /// along with the associated metadata.
1538 void createOffloadEntriesAndInfoMetadata();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001539
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001540 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001541 /// variables captured in a record which address is stored in \a
1542 /// CapturedStruct.
1543 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1544 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1545 /// \param CapturedVars A pointer to the record with the references to
1546 /// variables used in \a OutlinedFn function.
1547 ///
1548 virtual void emitTeamsCall(CodeGenFunction &CGF,
1549 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001550 SourceLocation Loc, llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001551 ArrayRef<llvm::Value *> CapturedVars);
1552
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001553 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001554 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1555 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001556 /// \param NumTeams An integer expression of teams.
1557 /// \param ThreadLimit An integer expression of threads.
1558 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1559 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001560
Samuel Antaocc10b852016-07-28 14:23:26 +00001561 /// Struct that keeps all the relevant information that should be kept
1562 /// throughout a 'target data' region.
1563 class TargetDataInfo {
1564 /// Set to true if device pointer information have to be obtained.
1565 bool RequiresDevicePointerInfo = false;
1566
1567 public:
1568 /// The array of base pointer passed to the runtime library.
1569 llvm::Value *BasePointersArray = nullptr;
1570 /// The array of section pointers passed to the runtime library.
1571 llvm::Value *PointersArray = nullptr;
1572 /// The array of sizes passed to the runtime library.
1573 llvm::Value *SizesArray = nullptr;
1574 /// The array of map types passed to the runtime library.
1575 llvm::Value *MapTypesArray = nullptr;
1576 /// The total number of pointers passed to the runtime library.
1577 unsigned NumberOfPtrs = 0u;
1578 /// Map between the a declaration of a capture and the corresponding base
1579 /// pointer address where the runtime returns the device pointers.
1580 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1581
1582 explicit TargetDataInfo() {}
1583 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1584 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1585 /// Clear information about the data arrays.
1586 void clearArrayInfo() {
1587 BasePointersArray = nullptr;
1588 PointersArray = nullptr;
1589 SizesArray = nullptr;
1590 MapTypesArray = nullptr;
1591 NumberOfPtrs = 0u;
1592 }
1593 /// Return true if the current target data information has valid arrays.
1594 bool isValid() {
1595 return BasePointersArray && PointersArray && SizesArray &&
1596 MapTypesArray && NumberOfPtrs;
1597 }
1598 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1599 };
1600
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001601 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001602 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001603 /// \param IfCond Expression evaluated in if clause associated with the
1604 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001605 /// \param Device Expression evaluated in device clause associated with the
1606 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001607 /// \param Info A record used to store information that needs to be preserved
1608 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001609 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1610 const OMPExecutableDirective &D,
1611 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001612 const RegionCodeGenTy &CodeGen,
1613 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001614
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001615 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001616 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001617 /// \param D Directive to emit.
1618 /// \param IfCond Expression evaluated in if clause associated with the target
1619 /// directive, or null if no if clause is used.
1620 /// \param Device Expression evaluated in device clause associated with the
1621 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001622 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1623 const OMPExecutableDirective &D,
1624 const Expr *IfCond,
1625 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001626
1627 /// Marks function \a Fn with properly mangled versions of vector functions.
1628 /// \param FD Function marked as 'declare simd'.
1629 /// \param Fn LLVM function that must be marked with 'declare simd'
1630 /// attributes.
1631 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1632 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001633
1634 /// Emit initialization for doacross loop nesting support.
1635 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001636 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1637 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001638
1639 /// Emit code for doacross ordered directive with 'depend' clause.
1640 /// \param C 'depend' clause with 'sink|source' dependency kind.
1641 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1642 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001643
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001644 /// Translates the native parameter of outlined function if this is required
1645 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001646 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001647 /// \param NativeParam Parameter itself.
1648 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1649 const VarDecl *NativeParam) const {
1650 return NativeParam;
1651 }
1652
1653 /// Gets the address of the native argument basing on the address of the
1654 /// target-specific parameter.
1655 /// \param NativeParam Parameter itself.
1656 /// \param TargetParam Corresponding target-specific parameter.
1657 virtual Address getParameterAddress(CodeGenFunction &CGF,
1658 const VarDecl *NativeParam,
1659 const VarDecl *TargetParam) const;
1660
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001661 /// Choose default schedule type and chunk value for the
1662 /// dist_schedule clause.
1663 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1664 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1665 llvm::Value *&Chunk) const {}
1666
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001667 /// Choose default schedule type and chunk value for the
1668 /// schedule clause.
1669 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1670 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
Alexey Bataevf6a53d62019-03-18 18:40:00 +00001671 const Expr *&ChunkExpr) const;
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001672
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001673 /// Emits call of the outlined function with the provided arguments,
1674 /// translating these arguments to correct target-specific arguments.
1675 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001676 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001677 llvm::FunctionCallee OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001678 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001679
1680 /// Emits OpenMP-specific function prolog.
1681 /// Required for device constructs.
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001682 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001683
1684 /// Gets the OpenMP-specific address of the local variable.
1685 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1686 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001687
Raphael Isemannb23ccec2018-12-10 12:37:46 +00001688 /// Marks the declaration as already emitted for the device code and returns
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001689 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001690 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001691
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001692 /// Emit deferred declare target variables marked for deferred emission.
1693 void emitDeferredTargetDecls() const;
Alexey Bataev60705422018-10-30 15:50:12 +00001694
1695 /// Adjust some parameters for the target-based directives, like addresses of
1696 /// the variables captured by reference in lambdas.
1697 virtual void
1698 adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
1699 const OMPExecutableDirective &D) const;
Patrick Lyster8f7f5862018-11-19 15:09:33 +00001700
1701 /// Perform check on requires decl to ensure that target architecture
1702 /// supports unified addressing
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001703 virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D);
Alexey Bataevc5687252019-03-21 19:35:27 +00001704
1705 /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1706 /// the predefined allocator and translates it into the corresponding address
1707 /// space.
1708 virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00001709
1710 /// Return whether the unified_shared_memory has been specified.
1711 bool hasRequiresUnifiedSharedMemory() const;
Alexey Bataev2df5f122019-10-01 20:18:32 +00001712
1713 /// Emits the definition of the declare variant function.
1714 virtual bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition);
Alexey Bataev0860db92019-12-19 10:01:10 -05001715
1716 /// Checks if the \p VD variable is marked as nontemporal declaration in
1717 /// current context.
1718 bool isNontemporalDecl(const ValueDecl *VD) const;
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001719
Alexey Bataev46978742020-01-30 10:46:11 -05001720 /// Create specialized alloca to handle lastprivate conditionals.
1721 Address emitLastprivateConditionalInit(CodeGenFunction &CGF,
1722 const VarDecl *VD);
1723
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001724 /// Checks if the provided \p LVal is lastprivate conditional and emits the
1725 /// code to update the value of the original variable.
1726 /// \code
1727 /// lastprivate(conditional: a)
1728 /// ...
1729 /// <type> a;
1730 /// lp_a = ...;
1731 /// #pragma omp critical(a)
1732 /// if (last_iv_a <= iv) {
1733 /// last_iv_a = iv;
1734 /// global_a = lp_a;
1735 /// }
1736 /// \endcode
1737 virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF,
1738 const Expr *LHS);
1739
Alexey Bataev46978742020-01-30 10:46:11 -05001740 /// Checks if the lastprivate conditional was updated in inner region and
1741 /// writes the value.
1742 /// \code
1743 /// lastprivate(conditional: a)
1744 /// ...
1745 /// <type> a;bool Fired = false;
1746 /// #pragma omp ... shared(a)
1747 /// {
1748 /// lp_a = ...;
1749 /// Fired = true;
1750 /// }
1751 /// if (Fired) {
1752 /// #pragma omp critical(a)
1753 /// if (last_iv_a <= iv) {
1754 /// last_iv_a = iv;
1755 /// global_a = lp_a;
1756 /// }
1757 /// Fired = false;
1758 /// }
1759 /// \endcode
1760 virtual void checkAndEmitSharedLastprivateConditional(
1761 CodeGenFunction &CGF, const OMPExecutableDirective &D,
1762 const llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> &IgnoredDecls);
1763
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001764 /// Gets the address of the global copy used for lastprivate conditional
1765 /// update, if any.
1766 /// \param PrivLVal LValue for the private copy.
1767 /// \param VD Original lastprivate declaration.
1768 virtual void emitLastprivateConditionalFinalUpdate(CodeGenFunction &CGF,
1769 LValue PrivLVal,
1770 const VarDecl *VD,
1771 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +00001772};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001773
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001774/// Class supports emissionof SIMD-only code.
1775class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1776public:
1777 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1778 ~CGOpenMPSIMDRuntime() override {}
1779
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001780 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001781 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1782 /// kmp_int32 BoundID, struct context_vars*).
1783 /// \param D OpenMP directive.
1784 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1785 /// \param InnermostKind Kind of innermost directive (for simple directives it
1786 /// is a directive itself, for combined - its innermost directive).
1787 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001788 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001789 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1790 const VarDecl *ThreadIDVar,
1791 OpenMPDirectiveKind InnermostKind,
1792 const RegionCodeGenTy &CodeGen) override;
1793
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001794 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001795 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1796 /// kmp_int32 BoundID, struct context_vars*).
1797 /// \param D OpenMP directive.
1798 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1799 /// \param InnermostKind Kind of innermost directive (for simple directives it
1800 /// is a directive itself, for combined - its innermost directive).
1801 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001802 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001803 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1804 const VarDecl *ThreadIDVar,
1805 OpenMPDirectiveKind InnermostKind,
1806 const RegionCodeGenTy &CodeGen) override;
1807
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001808 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001809 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1810 /// TaskT).
1811 /// \param D OpenMP directive.
1812 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1813 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1814 /// task region.
1815 /// \param TaskTVar Variable for task_t argument.
1816 /// \param InnermostKind Kind of innermost directive (for simple directives it
1817 /// is a directive itself, for combined - its innermost directive).
1818 /// \param CodeGen Code generation sequence for the \a D directive.
1819 /// \param Tied true if task is generated for tied task, false otherwise.
1820 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1821 /// tasks.
1822 ///
James Y Knight9871db02019-02-05 16:42:33 +00001823 llvm::Function *emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001824 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1825 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1826 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1827 bool Tied, unsigned &NumberOfParts) override;
1828
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001829 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001830 /// variables captured in a record which address is stored in \a
1831 /// CapturedStruct.
1832 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1833 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1834 /// \param CapturedVars A pointer to the record with the references to
1835 /// variables used in \a OutlinedFn function.
1836 /// \param IfCond Condition in the associated 'if' clause, if it was
1837 /// specified, nullptr otherwise.
1838 ///
1839 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001840 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001841 ArrayRef<llvm::Value *> CapturedVars,
1842 const Expr *IfCond) override;
1843
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001844 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001845 /// \param CriticalName Name of the critical region.
1846 /// \param CriticalOpGen Generator for the statement associated with the given
1847 /// critical region.
1848 /// \param Hint Value of the 'hint' clause (optional).
1849 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1850 const RegionCodeGenTy &CriticalOpGen,
1851 SourceLocation Loc,
1852 const Expr *Hint = nullptr) override;
1853
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001854 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001855 /// \param MasterOpGen Generator for the statement associated with the given
1856 /// master region.
1857 void emitMasterRegion(CodeGenFunction &CGF,
1858 const RegionCodeGenTy &MasterOpGen,
1859 SourceLocation Loc) override;
1860
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001861 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001862 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1863
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001864 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001865 /// \param TaskgroupOpGen Generator for the statement associated with the
1866 /// given taskgroup region.
1867 void emitTaskgroupRegion(CodeGenFunction &CGF,
1868 const RegionCodeGenTy &TaskgroupOpGen,
1869 SourceLocation Loc) override;
1870
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001871 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001872 /// \param SingleOpGen Generator for the statement associated with the given
1873 /// single region.
1874 void emitSingleRegion(CodeGenFunction &CGF,
1875 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1876 ArrayRef<const Expr *> CopyprivateVars,
1877 ArrayRef<const Expr *> DestExprs,
1878 ArrayRef<const Expr *> SrcExprs,
1879 ArrayRef<const Expr *> AssignmentOps) override;
1880
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001881 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001882 /// \param OrderedOpGen Generator for the statement associated with the given
1883 /// ordered region.
1884 void emitOrderedRegion(CodeGenFunction &CGF,
1885 const RegionCodeGenTy &OrderedOpGen,
1886 SourceLocation Loc, bool IsThreads) override;
1887
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001888 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001889 /// \param Kind Directive for which this implicit barrier call must be
1890 /// generated. Must be OMPD_barrier for explicit barrier generation.
1891 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1892 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1893 /// runtime class decides which one to emit (simple or with cancellation
1894 /// checks).
1895 ///
1896 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1897 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1898 bool ForceSimpleCall = false) override;
1899
1900 /// This is used for non static scheduled types and when the ordered
1901 /// clause is present on the loop construct.
1902 /// Depending on the loop schedule, it is necessary to call some runtime
1903 /// routine before start of the OpenMP loop to get the loop upper / lower
1904 /// bounds \a LB and \a UB and stride \a ST.
1905 ///
1906 /// \param CGF Reference to current CodeGenFunction.
1907 /// \param Loc Clang source location.
1908 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1909 /// \param IVSize Size of the iteration variable in bits.
1910 /// \param IVSigned Sign of the iteration variable.
1911 /// \param Ordered true if loop is ordered, false otherwise.
1912 /// \param DispatchValues struct containing llvm values for lower bound, upper
1913 /// bound, and chunk expression.
1914 /// For the default (nullptr) value, the chunk 1 will be used.
1915 ///
1916 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1917 const OpenMPScheduleTy &ScheduleKind,
1918 unsigned IVSize, bool IVSigned, bool Ordered,
1919 const DispatchRTInput &DispatchValues) override;
1920
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001921 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001922 /// of loop.
1923 ///
1924 /// This is used only in case of static schedule, when the user did not
1925 /// specify a ordered clause on the loop construct.
1926 /// Depending on the loop schedule, it is necessary to call some runtime
1927 /// routine before start of the OpenMP loop to get the loop upper / lower
1928 /// bounds LB and UB and stride ST.
1929 ///
1930 /// \param CGF Reference to current CodeGenFunction.
1931 /// \param Loc Clang source location.
1932 /// \param DKind Kind of the directive.
1933 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1934 /// \param Values Input arguments for the construct.
1935 ///
1936 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1937 OpenMPDirectiveKind DKind,
1938 const OpenMPScheduleTy &ScheduleKind,
1939 const StaticRTInput &Values) override;
1940
1941 ///
1942 /// \param CGF Reference to current CodeGenFunction.
1943 /// \param Loc Clang source location.
1944 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1945 /// \param Values Input arguments for the construct.
1946 ///
1947 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1948 OpenMPDistScheduleClauseKind SchedKind,
1949 const StaticRTInput &Values) override;
1950
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001951 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001952 /// iteration of the ordered loop with the dynamic scheduling.
1953 ///
1954 /// \param CGF Reference to current CodeGenFunction.
1955 /// \param Loc Clang source location.
1956 /// \param IVSize Size of the iteration variable in bits.
1957 /// \param IVSigned Sign of the iteration variable.
1958 ///
1959 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1960 unsigned IVSize, bool IVSigned) override;
1961
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001962 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001963 /// all the work with current loop.
1964 ///
1965 /// \param CGF Reference to current CodeGenFunction.
1966 /// \param Loc Clang source location.
1967 /// \param DKind Kind of the directive for which the static finish is emitted.
1968 ///
1969 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1970 OpenMPDirectiveKind DKind) override;
1971
1972 /// Call __kmpc_dispatch_next(
1973 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1974 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1975 /// kmp_int[32|64] *p_stride);
1976 /// \param IVSize Size of the iteration variable in bits.
1977 /// \param IVSigned Sign of the iteration variable.
1978 /// \param IL Address of the output variable in which the flag of the
1979 /// last iteration is returned.
1980 /// \param LB Address of the output variable in which the lower iteration
1981 /// number is returned.
1982 /// \param UB Address of the output variable in which the upper iteration
1983 /// number is returned.
1984 /// \param ST Address of the output variable in which the stride value is
1985 /// returned.
1986 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1987 unsigned IVSize, bool IVSigned, Address IL,
1988 Address LB, Address UB, Address ST) override;
1989
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001990 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001991 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1992 /// clause.
1993 /// \param NumThreads An integer value of threads.
1994 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1995 SourceLocation Loc) override;
1996
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001997 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001998 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1999 void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06002000 llvm::omp::ProcBindKind ProcBind,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002001 SourceLocation Loc) override;
2002
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002003 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002004 /// thread.
2005 /// \param VD Threadprivate variable.
2006 /// \param VDAddr Address of the global variable \a VD.
2007 /// \param Loc Location of the reference to threadprivate var.
2008 /// \return Address of the threadprivate variable for the current thread.
2009 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
2010 Address VDAddr, SourceLocation Loc) override;
2011
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002012 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002013 /// a call to runtime library which adds initial value to the newly created
2014 /// threadprivate variable (if it is not constant) and registers destructor
2015 /// for the variable (if any).
2016 /// \param VD Threadprivate variable.
2017 /// \param VDAddr Address of the global variable \a VD.
2018 /// \param Loc Location of threadprivate declaration.
2019 /// \param PerformInit true if initialization expression is not constant.
2020 llvm::Function *
2021 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
2022 SourceLocation Loc, bool PerformInit,
2023 CodeGenFunction *CGF = nullptr) override;
2024
2025 /// Creates artificial threadprivate variable with name \p Name and type \p
2026 /// VarType.
2027 /// \param VarType Type of the artificial threadprivate variable.
2028 /// \param Name Name of the artificial threadprivate variable.
2029 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
2030 QualType VarType,
2031 StringRef Name) override;
2032
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002033 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002034 /// \param Vars List of variables to flush.
2035 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
Alexey Bataeve8e05de2020-02-07 12:22:23 -05002036 SourceLocation Loc, llvm::AtomicOrdering AO) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002037
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002038 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002039 /// emitted in several steps:
2040 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2041 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2042 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2043 /// function:
2044 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2045 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2046 /// return 0;
2047 /// }
2048 /// 2. Copy a list of shared variables to field shareds of the resulting
2049 /// structure kmp_task_t returned by the previous call (if any).
2050 /// 3. Copy a pointer to destructions function to field destructions of the
2051 /// resulting structure kmp_task_t.
2052 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
2053 /// kmp_task_t *new_task), where new_task is a resulting structure from
2054 /// previous items.
2055 /// \param D Current task directive.
2056 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2057 /// /*part_id*/, captured_struct */*__context*/);
2058 /// \param SharedsTy A type which contains references the shared variables.
2059 /// \param Shareds Context with the list of shared variables from the \p
2060 /// TaskFunction.
2061 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2062 /// otherwise.
2063 /// \param Data Additional data for task generation like tiednsee, final
2064 /// state, list of privates etc.
2065 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002066 const OMPExecutableDirective &D,
2067 llvm::Function *TaskFunction, QualType SharedsTy,
2068 Address Shareds, const Expr *IfCond,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002069 const OMPTaskDataTy &Data) override;
2070
2071 /// Emit task region for the taskloop directive. The taskloop region is
2072 /// emitted in several steps:
2073 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2074 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2075 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2076 /// function:
2077 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2078 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2079 /// return 0;
2080 /// }
2081 /// 2. Copy a list of shared variables to field shareds of the resulting
2082 /// structure kmp_task_t returned by the previous call (if any).
2083 /// 3. Copy a pointer to destructions function to field destructions of the
2084 /// resulting structure kmp_task_t.
2085 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
2086 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
2087 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
2088 /// is a resulting structure from
2089 /// previous items.
2090 /// \param D Current task directive.
2091 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2092 /// /*part_id*/, captured_struct */*__context*/);
2093 /// \param SharedsTy A type which contains references the shared variables.
2094 /// \param Shareds Context with the list of shared variables from the \p
2095 /// TaskFunction.
2096 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2097 /// otherwise.
2098 /// \param Data Additional data for task generation like tiednsee, final
2099 /// state, list of privates etc.
2100 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002101 const OMPLoopDirective &D, llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002102 QualType SharedsTy, Address Shareds, const Expr *IfCond,
2103 const OMPTaskDataTy &Data) override;
2104
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002105 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002106 /// reduction:
2107 /// \code
2108 ///
2109 /// static kmp_critical_name lock = { 0 };
2110 ///
2111 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
2112 /// ...
2113 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
2114 /// ...
2115 /// }
2116 ///
2117 /// ...
2118 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
2119 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
2120 /// RedList, reduce_func, &<lock>)) {
2121 /// case 1:
2122 /// ...
2123 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
2124 /// ...
2125 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
2126 /// break;
2127 /// case 2:
2128 /// ...
2129 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
2130 /// ...
2131 /// break;
2132 /// default:;
2133 /// }
2134 /// \endcode
2135 ///
2136 /// \param Privates List of private copies for original reduction arguments.
2137 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
2138 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
2139 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
2140 /// or 'operator binop(LHS, RHS)'.
2141 /// \param Options List of options for reduction codegen:
2142 /// WithNowait true if parent directive has also nowait clause, false
2143 /// otherwise.
2144 /// SimpleReduction Emit reduction operation only. Used for omp simd
2145 /// directive on the host.
2146 /// ReductionKind The kind of reduction to perform.
2147 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2148 ArrayRef<const Expr *> Privates,
2149 ArrayRef<const Expr *> LHSExprs,
2150 ArrayRef<const Expr *> RHSExprs,
2151 ArrayRef<const Expr *> ReductionOps,
2152 ReductionOptionsTy Options) override;
2153
2154 /// Emit a code for initialization of task reduction clause. Next code
2155 /// should be emitted for reduction:
2156 /// \code
2157 ///
2158 /// _task_red_item_t red_data[n];
2159 /// ...
2160 /// red_data[i].shar = &origs[i];
2161 /// red_data[i].size = sizeof(origs[i]);
2162 /// red_data[i].f_init = (void*)RedInit<i>;
2163 /// red_data[i].f_fini = (void*)RedDest<i>;
2164 /// red_data[i].f_comb = (void*)RedOp<i>;
2165 /// red_data[i].flags = <Flag_i>;
2166 /// ...
2167 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2168 /// \endcode
2169 ///
2170 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2171 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2172 /// \param Data Additional data for task generation like tiedness, final
2173 /// state, list of privates, reductions etc.
2174 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2175 ArrayRef<const Expr *> LHSExprs,
2176 ArrayRef<const Expr *> RHSExprs,
2177 const OMPTaskDataTy &Data) override;
2178
2179 /// Required to resolve existing problems in the runtime. Emits threadprivate
2180 /// variables to store the size of the VLAs/array sections for
2181 /// initializer/combiner/finalizer functions + emits threadprivate variable to
2182 /// store the pointer to the original reduction item for the custom
2183 /// initializer defined by declare reduction construct.
2184 /// \param RCG Allows to reuse an existing data for the reductions.
2185 /// \param N Reduction item for which fixups must be emitted.
2186 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2187 ReductionCodeGen &RCG, unsigned N) override;
2188
2189 /// Get the address of `void *` type of the privatue copy of the reduction
2190 /// item specified by the \p SharedLVal.
2191 /// \param ReductionsPtr Pointer to the reduction data returned by the
2192 /// emitTaskReductionInit function.
2193 /// \param SharedLVal Address of the original reduction item.
2194 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2195 llvm::Value *ReductionsPtr,
2196 LValue SharedLVal) override;
2197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002198 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002199 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002201 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002202 /// \param CancelRegion Region kind for which the cancellation point must be
2203 /// emitted.
2204 ///
2205 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2206 OpenMPDirectiveKind CancelRegion) override;
2207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002208 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002209 /// \param IfCond Condition in the associated 'if' clause, if it was
2210 /// specified, nullptr otherwise.
2211 /// \param CancelRegion Region kind for which the cancel must be emitted.
2212 ///
2213 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2214 const Expr *IfCond,
2215 OpenMPDirectiveKind CancelRegion) override;
2216
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002217 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002218 /// \param D Directive to emit.
2219 /// \param ParentName Name of the function that encloses the target region.
2220 /// \param OutlinedFn Outlined function value to be defined by this call.
2221 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2222 /// \param IsOffloadEntry True if the outlined function is an offload entry.
2223 /// \param CodeGen Code generation sequence for the \a D directive.
2224 /// An outlined function may not be an entry if, e.g. the if clause always
2225 /// evaluates to false.
2226 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
2227 StringRef ParentName,
2228 llvm::Function *&OutlinedFn,
2229 llvm::Constant *&OutlinedFnID,
2230 bool IsOffloadEntry,
2231 const RegionCodeGenTy &CodeGen) override;
2232
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002233 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002234 /// code attempts offloading the execution to the device, an the event of
2235 /// a failure it executes the host version outlined in \a OutlinedFn.
2236 /// \param D Directive to emit.
2237 /// \param OutlinedFn Host version of the code to be offloaded.
2238 /// \param OutlinedFnID ID of host version of the code to be offloaded.
2239 /// \param IfCond Expression evaluated in if clause associated with the target
2240 /// directive, or null if no if clause is used.
2241 /// \param Device Expression evaluated in device clause associated with the
2242 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00002243 void
2244 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2245 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
2246 const Expr *IfCond, const Expr *Device,
2247 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2248 const OMPLoopDirective &D)>
2249 SizeEmitter) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002251 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002252 /// the function itself in case it is a valid device function. Returns true if
2253 /// \a GD was dealt with successfully.
2254 /// \param GD Function to scan.
2255 bool emitTargetFunctions(GlobalDecl GD) override;
2256
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002257 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002258 /// Returns true if \a GD was dealt with successfully.
2259 /// \param GD Variable declaration to emit.
2260 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2261
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002262 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002263 /// if it was emitted successfully.
2264 /// \param GD Global to scan.
2265 bool emitTargetGlobal(GlobalDecl GD) override;
2266
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002267 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002268 /// variables captured in a record which address is stored in \a
2269 /// CapturedStruct.
2270 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2271 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2272 /// \param CapturedVars A pointer to the record with the references to
2273 /// variables used in \a OutlinedFn function.
2274 ///
2275 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002276 SourceLocation Loc, llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002277 ArrayRef<llvm::Value *> CapturedVars) override;
2278
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002279 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002280 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2281 /// for num_teams clause.
2282 /// \param NumTeams An integer expression of teams.
2283 /// \param ThreadLimit An integer expression of threads.
2284 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2285 const Expr *ThreadLimit, SourceLocation Loc) override;
2286
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002287 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002288 /// \param D Directive to emit.
2289 /// \param IfCond Expression evaluated in if clause associated with the
2290 /// target directive, or null if no device clause is used.
2291 /// \param Device Expression evaluated in device clause associated with the
2292 /// target directive, or null if no device clause is used.
2293 /// \param Info A record used to store information that needs to be preserved
2294 /// until the region is closed.
2295 void emitTargetDataCalls(CodeGenFunction &CGF,
2296 const OMPExecutableDirective &D, const Expr *IfCond,
2297 const Expr *Device, const RegionCodeGenTy &CodeGen,
2298 TargetDataInfo &Info) override;
2299
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002300 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002301 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2302 /// \param D Directive to emit.
2303 /// \param IfCond Expression evaluated in if clause associated with the target
2304 /// directive, or null if no if clause is used.
2305 /// \param Device Expression evaluated in device clause associated with the
2306 /// target directive, or null if no device clause is used.
2307 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2308 const OMPExecutableDirective &D,
2309 const Expr *IfCond,
2310 const Expr *Device) override;
2311
2312 /// Emit initialization for doacross loop nesting support.
2313 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002314 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2315 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002316
2317 /// Emit code for doacross ordered directive with 'depend' clause.
2318 /// \param C 'depend' clause with 'sink|source' dependency kind.
2319 void emitDoacrossOrdered(CodeGenFunction &CGF,
2320 const OMPDependClause *C) override;
2321
2322 /// Translates the native parameter of outlined function if this is required
2323 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002324 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002325 /// \param NativeParam Parameter itself.
2326 const VarDecl *translateParameter(const FieldDecl *FD,
2327 const VarDecl *NativeParam) const override;
2328
2329 /// Gets the address of the native argument basing on the address of the
2330 /// target-specific parameter.
2331 /// \param NativeParam Parameter itself.
2332 /// \param TargetParam Corresponding target-specific parameter.
2333 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2334 const VarDecl *TargetParam) const override;
Alexey Bataev4f680db2019-03-19 16:41:16 +00002335
2336 /// Gets the OpenMP-specific address of the local variable.
2337 Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2338 const VarDecl *VD) override {
2339 return Address::invalid();
2340 }
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002341};
2342
Alexey Bataev23b69422014-06-18 07:08:49 +00002343} // namespace CodeGen
2344} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002345
2346#endif