blob: 99b201bcf6feab2bbcd2f1a16450491050e9464b [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 Bataevf3c857f2020-03-18 17:52:41 -040023#include "llvm/ADT/PointerIntPair.h"
Alexey Bataev45588422020-01-07 14:11:45 -050024#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000025#include "llvm/ADT/StringMap.h"
Alexey Bataev2a6f3f52018-11-07 19:11:14 +000026#include "llvm/ADT/StringSet.h"
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060027#include "llvm/Frontend/OpenMP/OMPConstants.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000028#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000029#include "llvm/IR/ValueHandle.h"
Alexey Bataev2d4f80f2020-02-11 15:15:21 -050030#include "llvm/Support/AtomicOrdering.h"
Alexey Bataev18095712014-10-10 12:19:54 +000031
32namespace llvm {
33class ArrayType;
34class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000035class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000036class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000037class StructType;
38class Type;
39class Value;
40} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000041
Alexey Bataev9959db52014-05-06 10:08:46 +000042namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000043class Expr;
Alexey Bataev8b427062016-05-25 12:36:08 +000044class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000045class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000046class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000047class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000048class OMPDeclareReductionDecl;
49class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000050
Alexey Bataev9959db52014-05-06 10:08:46 +000051namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000052class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000053class CodeGenFunction;
54class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000055
Alexey Bataev14fa1c62016-03-29 05:34:15 +000056/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
57/// region.
58class PrePostActionTy {
59public:
60 explicit PrePostActionTy() {}
61 virtual void Enter(CodeGenFunction &CGF) {}
62 virtual void Exit(CodeGenFunction &CGF) {}
63 virtual ~PrePostActionTy() {}
64};
65
66/// Class provides a way to call simple version of codegen for OpenMP region, or
67/// an advanced with possible pre|post-actions in codegen.
68class RegionCodeGenTy final {
69 intptr_t CodeGen;
70 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
71 CodeGenTy Callback;
72 mutable PrePostActionTy *PrePostAction;
73 RegionCodeGenTy() = delete;
74 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
75 template <typename Callable>
76 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
77 PrePostActionTy &Action) {
78 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
79 }
80
81public:
82 template <typename Callable>
83 RegionCodeGenTy(
84 Callable &&CodeGen,
Justin Lebar027eb712020-02-10 23:23:44 -080085 std::enable_if_t<!std::is_same<std::remove_reference_t<Callable>,
86 RegionCodeGenTy>::value> * = nullptr)
Alexey Bataev14fa1c62016-03-29 05:34:15 +000087 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
Justin Lebar027eb712020-02-10 23:23:44 -080088 Callback(CallbackFn<std::remove_reference_t<Callable>>),
Alexey Bataev14fa1c62016-03-29 05:34:15 +000089 PrePostAction(nullptr) {}
90 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
91 void operator()(CodeGenFunction &CGF) const;
92};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000093
Alexey Bataev24b5bae2016-04-28 09:23:51 +000094struct OMPTaskDataTy final {
95 SmallVector<const Expr *, 4> PrivateVars;
96 SmallVector<const Expr *, 4> PrivateCopies;
97 SmallVector<const Expr *, 4> FirstprivateVars;
98 SmallVector<const Expr *, 4> FirstprivateCopies;
99 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000100 SmallVector<const Expr *, 4> LastprivateVars;
101 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000102 SmallVector<const Expr *, 4> ReductionVars;
103 SmallVector<const Expr *, 4> ReductionCopies;
104 SmallVector<const Expr *, 4> ReductionOps;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000105 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
106 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
107 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000108 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000109 llvm::Value *Reductions = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000110 unsigned NumberOfParts = 0;
111 bool Tied = true;
112 bool Nogroup = false;
113};
114
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000115/// Class intended to support codegen of all kind of the reduction clauses.
116class ReductionCodeGen {
117private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000118 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000119 struct ReductionData {
120 /// Reference to the original shared item.
121 const Expr *Ref = nullptr;
122 /// Helper expression for generation of private copy.
123 const Expr *Private = nullptr;
124 /// Helper expression for generation reduction operation.
125 const Expr *ReductionOp = nullptr;
126 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
127 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
128 };
129 /// List of reduction-based clauses.
130 SmallVector<ReductionData, 4> ClausesData;
131
132 /// List of addresses of original shared variables/expressions.
133 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
134 /// Sizes of the reduction items in chars.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000135 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000136 /// Base declarations for the reduction items.
137 SmallVector<const VarDecl *, 4> BaseDecls;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000138
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000139 /// Emits lvalue for shared expression.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000140 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
141 /// Emits upper bound for shared expression (if array section).
142 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
143 /// Performs aggregate initialization.
144 /// \param N Number of reduction item in the common list.
145 /// \param PrivateAddr Address of the corresponding private item.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000146 /// \param SharedLVal Address of the original shared variable.
147 /// \param DRD Declare reduction construct used for reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000148 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000149 Address PrivateAddr, LValue SharedLVal,
150 const OMPDeclareReductionDecl *DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000151
152public:
153 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
154 ArrayRef<const Expr *> Privates,
155 ArrayRef<const Expr *> ReductionOps);
156 /// Emits lvalue for a reduction item.
157 /// \param N Number of the reduction item.
158 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
159 /// Emits the code for the variable-modified type, if required.
160 /// \param N Number of the reduction item.
161 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
162 /// Emits the code for the variable-modified type, if required.
163 /// \param N Number of the reduction item.
164 /// \param Size Size of the type in chars.
165 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
166 /// Performs initialization of the private copy for the reduction item.
167 /// \param N Number of the reduction item.
168 /// \param PrivateAddr Address of the corresponding private item.
169 /// \param DefaultInit Default initialization sequence that should be
170 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000171 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000172 void
173 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
174 LValue SharedLVal,
175 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000176 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000177 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000178 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000179 /// \param N Number of the reduction item.
180 /// \param PrivateAddr Address of the corresponding private item.
181 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000182 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000183 /// address in normal operations.
184 /// \param N Number of the reduction item.
185 /// \param PrivateAddr Address of the corresponding private item.
186 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
187 Address PrivateAddr);
188 /// Returns LValue for the reduction item.
189 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000190 /// Returns the size of the reduction item (in chars and total number of
191 /// elements in the item), or nullptr, if the size is a constant.
192 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
193 return Sizes[N];
194 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000195 /// Returns the base declaration of the reduction item.
196 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
Alexey Bataev1c44e152018-03-06 18:59:43 +0000197 /// Returns the base declaration of the reduction item.
198 const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000199 /// Returns true if the initialization of the reduction item uses initializer
200 /// from declare reduction construct.
201 bool usesReductionInitializer(unsigned N) const;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000202};
203
Alexey Bataev9959db52014-05-06 10:08:46 +0000204class CGOpenMPRuntime {
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000205public:
206 /// Allows to disable automatic handling of functions used in target regions
207 /// as those marked as `omp declare target`.
208 class DisableAutoDeclareTargetRAII {
209 CodeGenModule &CGM;
210 bool SavedShouldMarkAsGlobal;
211
212 public:
213 DisableAutoDeclareTargetRAII(CodeGenModule &CGM);
214 ~DisableAutoDeclareTargetRAII();
215 };
216
Alexey Bataev0860db92019-12-19 10:01:10 -0500217 /// Manages list of nontemporal decls for the specified directive.
218 class NontemporalDeclsRAII {
219 CodeGenModule &CGM;
220 const bool NeedToPush;
221
222 public:
223 NontemporalDeclsRAII(CodeGenModule &CGM, const OMPLoopDirective &S);
224 ~NontemporalDeclsRAII();
225 };
226
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500227 /// Maps the expression for the lastprivate variable to the global copy used
228 /// to store new value because original variables are not mapped in inner
229 /// parallel regions. Only private copies are captured but we need also to
230 /// store private copy in shared address.
231 /// Also, stores the expression for the private loop counter and it
232 /// threaprivate name.
233 struct LastprivateConditionalData {
Alexey Bataev46978742020-01-30 10:46:11 -0500234 llvm::MapVector<CanonicalDeclPtr<const Decl>, SmallString<16>>
235 DeclToUniqueName;
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500236 LValue IVLVal;
Alexey Bataev46978742020-01-30 10:46:11 -0500237 llvm::Function *Fn = nullptr;
238 bool Disabled = false;
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500239 };
240 /// Manages list of lastprivate conditional decls for the specified directive.
241 class LastprivateConditionalRAII {
Alexey Bataev46978742020-01-30 10:46:11 -0500242 enum class ActionToDo {
243 DoNotPush,
244 PushAsLastprivateConditional,
245 DisableLastprivateConditional,
246 };
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500247 CodeGenModule &CGM;
Alexey Bataev46978742020-01-30 10:46:11 -0500248 ActionToDo Action = ActionToDo::DoNotPush;
249
250 /// Check and try to disable analysis of inner regions for changes in
251 /// lastprivate conditional.
252 void tryToDisableInnerAnalysis(const OMPExecutableDirective &S,
253 llvm::DenseSet<CanonicalDeclPtr<const Decl>>
254 &NeedToAddForLPCsAsDisabled) const;
255
256 LastprivateConditionalRAII(CodeGenFunction &CGF,
257 const OMPExecutableDirective &S);
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500258
259 public:
Alexey Bataev46978742020-01-30 10:46:11 -0500260 explicit LastprivateConditionalRAII(CodeGenFunction &CGF,
261 const OMPExecutableDirective &S,
262 LValue IVLVal);
263 static LastprivateConditionalRAII disable(CodeGenFunction &CGF,
264 const OMPExecutableDirective &S);
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500265 ~LastprivateConditionalRAII();
266 };
267
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000268protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000269 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000270 StringRef FirstSeparator, Separator;
271
272 /// Constructor allowing to redefine the name separator for the variables.
273 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
274 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000275
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000276 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000277 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000278 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000279 uint64_t Size, int32_t Flags,
280 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000281
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000282 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000283 /// \param D Directive to emit.
284 /// \param ParentName Name of the function that encloses the target region.
285 /// \param OutlinedFn Outlined function value to be defined by this call.
286 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
287 /// \param IsOffloadEntry True if the outlined function is an offload entry.
288 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000289 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000290 /// evaluates to false.
291 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
292 StringRef ParentName,
293 llvm::Function *&OutlinedFn,
294 llvm::Constant *&OutlinedFnID,
295 bool IsOffloadEntry,
296 const RegionCodeGenTy &CodeGen);
297
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000298 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000299 /// \param Flags Flags for OpenMP location.
300 ///
301 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
302 unsigned Flags = 0);
303
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000304 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000305 llvm::Type *getIdentTyPointerTy();
306
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000307 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000308 ///
309 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
310
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000311 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000312 // The name can be customized depending on the target.
313 //
314 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
315
Alexey Bataev3c595a62017-08-14 15:01:03 +0000316 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
James Y Knight9871db02019-02-05 16:42:33 +0000317 void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
318 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000319 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000320
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000321 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000322 /// stored.
323 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
324
Alexey Bataevfd006c42018-10-05 15:08:53 +0000325 void setLocThreadIdInsertPt(CodeGenFunction &CGF,
326 bool AtCurrentPoint = false);
327 void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
328
Alexey Bataevceeaa482018-11-21 21:04:34 +0000329 /// Check if the default location must be constant.
330 /// Default is false to support OMPT/OMPD.
331 virtual bool isDefaultLocationConstant() const { return false; }
332
333 /// Returns additional flags that can be stored in reserved_2 field of the
334 /// default location.
335 virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
336
Alexey Bataevc2cd2d42019-10-10 17:28:10 +0000337 /// Tries to emit declare variant function for \p OldGD from \p NewGD.
338 /// \param OrigAddr LLVM IR value for \p OldGD.
339 /// \param IsForDefinition true, if requested emission for the definition of
340 /// \p OldGD.
341 /// \returns true, was able to emit a definition function for \p OldGD, which
342 /// points to \p NewGD.
343 virtual bool tryEmitDeclareVariant(const GlobalDecl &NewGD,
344 const GlobalDecl &OldGD,
345 llvm::GlobalValue *OrigAddr,
346 bool IsForDefinition);
347
Alexey Bataevc3028ca2018-12-04 15:03:25 +0000348 /// Returns default flags for the barriers depending on the directive, for
349 /// which this barier is going to be emitted.
350 static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
351
Alexey Bataeva1166022018-11-27 21:24:54 +0000352 /// Get the LLVM type for the critical name.
353 llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
354
355 /// Returns corresponding lock object for the specified critical region
356 /// name. If the lock object does not exist it is created, otherwise the
357 /// reference to the existing copy is returned.
358 /// \param CriticalName Name of the critical region.
359 ///
360 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
361
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000362private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000363 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000364 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000365 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataevceeaa482018-11-21 21:04:34 +0000366 using FlagsTy = std::pair<unsigned, unsigned>;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000367 /// Map of flags and corresponding default locations.
Alexey Bataevceeaa482018-11-21 21:04:34 +0000368 using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
Alexey Bataev15007ba2014-05-07 06:18:01 +0000369 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000370 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000371
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000372 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000373 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000374 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000375 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
376 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000377 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000378 /// Original representation is:
379 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000380 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000381 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000382 struct DebugLocThreadIdTy {
383 llvm::Value *DebugLoc;
384 llvm::Value *ThreadID;
Alexey Bataevfd006c42018-10-05 15:08:53 +0000385 /// Insert point for the service instructions.
386 llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000387 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000388 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000389 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
390 OpenMPLocThreadIDMapTy;
391 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000392 /// Map of UDRs and corresponding combiner/initializer.
393 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
394 std::pair<llvm::Function *, llvm::Function *>>
395 UDRMapTy;
396 UDRMapTy UDRMap;
397 /// Map of functions and locally defined UDRs.
398 typedef llvm::DenseMap<llvm::Function *,
399 SmallVector<const OMPDeclareReductionDecl *, 4>>
400 FunctionUDRMapTy;
401 FunctionUDRMapTy FunctionUDRMap;
Michael Krused47b9432019-08-05 18:43:21 +0000402 /// Map from the user-defined mapper declaration to its corresponding
403 /// functions.
404 llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
405 /// Map of functions and their local user-defined mappers.
406 using FunctionUDMMapTy =
407 llvm::DenseMap<llvm::Function *,
408 SmallVector<const OMPDeclareMapperDecl *, 4>>;
409 FunctionUDMMapTy FunctionUDMMap;
Alexey Bataev46978742020-01-30 10:46:11 -0500410 /// Maps local variables marked as lastprivate conditional to their internal
411 /// types.
412 llvm::DenseMap<llvm::Function *,
413 llvm::DenseMap<CanonicalDeclPtr<const Decl>,
414 std::tuple<QualType, const FieldDecl *,
415 const FieldDecl *, LValue>>>
416 LastprivateConditionalToTypes;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000417 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000418 /// kmp_critical_name[8];
419 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000420 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000421 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
422 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
423 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
424 /// variables.
425 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
426 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000427 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000428 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000429 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000430 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000431 /// void * shareds; /**< pointer to block of pointers to
432 /// shared vars */
433 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
434 /// executing task */
435 /// kmp_int32 part_id; /**< part id for the task */
436 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
437 /// deconstructors of firstprivate C++ objects */
438 /// } kmp_task_t;
439 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000440 /// Saved kmp_task_t for task directive.
441 QualType SavedKmpTaskTQTy;
442 /// Saved kmp_task_t for taskloop-based directive.
443 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000444 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000445 /// kmp_intptr_t base_addr;
446 /// size_t len;
447 /// struct {
448 /// bool in:1;
449 /// bool out:1;
450 /// } flags;
451 /// } kmp_depend_info_t;
452 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000453 /// struct kmp_dim { // loop bounds info casted to kmp_int64
454 /// kmp_int64 lo; // lower
455 /// kmp_int64 up; // upper
456 /// kmp_int64 st; // stride
457 /// };
458 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000459 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000460 /// void *addr; // Pointer to the offload entry info.
461 /// // (function or global)
462 /// char *name; // Name of the function or global.
463 /// size_t size; // Size of the entry info (0 if it a function).
Alexey Bataev4c117032020-01-09 09:28:59 -0500464 /// int32_t flags;
465 /// int32_t reserved;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000466 /// };
467 QualType TgtOffloadEntryQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000468 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000469 /// far.
470 class OffloadEntriesInfoManagerTy {
471 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000472
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000473 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000474 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000475
476 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000477 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000478 class OffloadEntryInfo {
479 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000480 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000481 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000482 /// Entry is a target region.
483 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000484 /// Entry is a declare target variable.
485 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000486 /// Invalid entry info.
487 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000488 };
489
Alexey Bataev03f270c2018-03-30 18:31:07 +0000490 protected:
491 OffloadEntryInfo() = delete;
492 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000493 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000494 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000495 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000496 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000497
Alexey Bataev03f270c2018-03-30 18:31:07 +0000498 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000499 bool isValid() const { return Order != ~0u; }
500 unsigned getOrder() const { return Order; }
501 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000502 uint32_t getFlags() const { return Flags; }
503 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
504 llvm::Constant *getAddress() const {
505 return cast_or_null<llvm::Constant>(Addr);
506 }
507 void setAddress(llvm::Constant *V) {
508 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
509 Addr = V;
510 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000511 static bool classof(const OffloadEntryInfo *Info) { return true; }
512
Samuel Antaof83efdb2017-01-05 16:02:49 +0000513 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000514 /// Address of the entity that has to be mapped for offloading.
515 llvm::WeakTrackingVH Addr;
516
Samuel Antaof83efdb2017-01-05 16:02:49 +0000517 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000518 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000519
520 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000521 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000522
Alexey Bataev03f270c2018-03-30 18:31:07 +0000523 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000524 };
525
Alexey Bataev03f270c2018-03-30 18:31:07 +0000526 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000527 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000528 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000529 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000530 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000531
Alexey Bataev03f270c2018-03-30 18:31:07 +0000532 //
533 // Target region entries related.
534 //
535
536 /// Kind of the target registry entry.
537 enum OMPTargetRegionEntryKind : uint32_t {
538 /// Mark the entry as target region.
539 OMPTargetRegionEntryTargetRegion = 0x0,
540 /// Mark the entry as a global constructor.
541 OMPTargetRegionEntryCtor = 0x02,
542 /// Mark the entry as a global destructor.
543 OMPTargetRegionEntryDtor = 0x04,
544 };
545
546 /// Target region entries info.
547 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
548 /// Address that can be used as the ID of the entry.
549 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000550
551 public:
552 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000553 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000554 explicit OffloadEntryInfoTargetRegion(unsigned Order,
555 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000556 llvm::Constant *ID,
557 OMPTargetRegionEntryKind Flags)
558 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000559 ID(ID) {
560 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000561 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000562
563 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000564 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000565 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000566 ID = V;
567 }
568 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000569 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000570 }
571 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000572
573 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000574 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
575 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000576 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000577 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000578 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
579 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000580 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000581 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000582 /// Return true if a target region entry with the provided information
583 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000584 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000585 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000586 /// brief Applies action \a Action on all registered entries.
587 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000588 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000589 OffloadTargetRegionEntryInfoActTy;
590 void actOnTargetRegionEntriesInfo(
591 const OffloadTargetRegionEntryInfoActTy &Action);
592
Alexey Bataev03f270c2018-03-30 18:31:07 +0000593 //
594 // Device global variable entries related.
595 //
596
597 /// Kind of the global variable entry..
598 enum OMPTargetGlobalVarEntryKind : uint32_t {
599 /// Mark the entry as a to declare target.
600 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000601 /// Mark the entry as a to declare target link.
602 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000603 };
604
605 /// Device global variable entries info.
606 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
607 /// Type of the global variable.
608 CharUnits VarSize;
609 llvm::GlobalValue::LinkageTypes Linkage;
610
611 public:
612 OffloadEntryInfoDeviceGlobalVar()
613 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
614 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
615 OMPTargetGlobalVarEntryKind Flags)
616 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
617 explicit OffloadEntryInfoDeviceGlobalVar(
618 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
619 OMPTargetGlobalVarEntryKind Flags,
620 llvm::GlobalValue::LinkageTypes Linkage)
621 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
622 VarSize(VarSize), Linkage(Linkage) {
623 setAddress(Addr);
624 }
625
626 CharUnits getVarSize() const { return VarSize; }
627 void setVarSize(CharUnits Size) { VarSize = Size; }
628 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
629 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
630 static bool classof(const OffloadEntryInfo *Info) {
631 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
632 }
633 };
634
635 /// Initialize device global variable entry.
636 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
637 OMPTargetGlobalVarEntryKind Flags,
638 unsigned Order);
639
640 /// Register device global variable entry.
641 void
642 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
643 CharUnits VarSize,
644 OMPTargetGlobalVarEntryKind Flags,
645 llvm::GlobalValue::LinkageTypes Linkage);
646 /// Checks if the variable with the given name has been registered already.
647 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
648 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
649 }
650 /// Applies action \a Action on all registered entries.
651 typedef llvm::function_ref<void(StringRef,
652 const OffloadEntryInfoDeviceGlobalVar &)>
653 OffloadDeviceGlobalVarEntryInfoActTy;
654 void actOnDeviceGlobalVarEntriesInfo(
655 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
656
Samuel Antaoee8fb302016-01-06 13:42:12 +0000657 private:
658 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000659 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000660 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000661 OffloadEntriesTargetRegionPerLine;
662 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
663 OffloadEntriesTargetRegionPerParentName;
664 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
665 OffloadEntriesTargetRegionPerFile;
666 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
667 OffloadEntriesTargetRegionPerDevice;
668 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
669 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000670 /// Storage for device global variable entries kind. The storage is to be
671 /// indexed by mangled name.
672 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
673 OffloadEntriesDeviceGlobalVarTy;
674 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000675 };
676 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
677
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000678 bool ShouldMarkAsGlobal = true;
Alexey Bataev45588422020-01-07 14:11:45 -0500679 /// List of the emitted declarations.
680 llvm::DenseSet<CanonicalDeclPtr<const Decl>> AlreadyEmittedTargetDecls;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000681 /// List of the global variables with their addresses that should not be
682 /// emitted for the target.
683 llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000684
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000685 /// List of variables that can become declare target implicitly and, thus,
686 /// must be emitted.
687 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
688
Alexey Bataev2df5f122019-10-01 20:18:32 +0000689 /// Mapping of the original functions to their variants and original global
690 /// decl.
691 llvm::MapVector<CanonicalDeclPtr<const FunctionDecl>,
692 std::pair<GlobalDecl, GlobalDecl>>
693 DeferredVariantFunction;
694
Alexey Bataev0860db92019-12-19 10:01:10 -0500695 using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
696 /// Stack for list of declarations in current context marked as nontemporal.
697 /// The set is the union of all current stack elements.
698 llvm::SmallVector<NontemporalDeclsSet, 4> NontemporalDeclsStack;
699
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500700 /// Stack for list of addresses of declarations in current context marked as
701 /// lastprivate conditional. The set is the union of all current stack
702 /// elements.
703 llvm::SmallVector<LastprivateConditionalData, 4> LastprivateConditionalStack;
704
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000705 /// Flag for keeping track of weather a requires unified_shared_memory
706 /// directive is present.
707 bool HasRequiresUnifiedSharedMemory = false;
708
Alexey Bataev2d4f80f2020-02-11 15:15:21 -0500709 /// Atomic ordering from the omp requires directive.
710 llvm::AtomicOrdering RequiresAtomicOrdering = llvm::AtomicOrdering::Monotonic;
711
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000712 /// Flag for keeping track of weather a target region has been emitted.
713 bool HasEmittedTargetRegion = false;
714
715 /// Flag for keeping track of weather a device routine has been emitted.
716 /// Device routines are specific to the
717 bool HasEmittedDeclareTargetRegion = false;
718
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000719 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000720 /// metadata.
721 void loadOffloadInfoMetadata();
722
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000723 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000724 QualType getTgtOffloadEntryQTy();
725
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000726 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000727 /// found along the way.
728 /// \param S Starting statement.
729 /// \param ParentName Name of the function declaration that is being scanned.
730 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000731
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000732 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000733 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000734
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000735 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000736 llvm::Type *getKmpc_MicroPointerTy();
737
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000738 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000739 /// \param Function OpenMP runtime function.
740 /// \return Specified function.
James Y Knight9871db02019-02-05 16:42:33 +0000741 llvm::FunctionCallee createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000742
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000743 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000744 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000745 llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
746 bool IVSigned);
Alexander Musman21212e42015-03-13 10:38:23 +0000747
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000748 /// Returns __kmpc_dispatch_init_* 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 createDispatchInitFunction(unsigned IVSize,
751 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000752
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000753 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000754 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000755 llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
756 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000757
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000758 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000759 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000760 llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
761 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000762
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000763 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000764 /// return threadprivate cache object. This object is a pointer's worth of
765 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000766 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000767 /// \return Cache variable for the specified threadprivate.
768 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
769
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000770 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000771 /// internal global variable with the specified Name. The created variable has
772 /// linkage CommonLinkage by default and is initialized by null value.
773 /// \param Ty Type of the global variable. If it is exist already the type
774 /// must be the same.
775 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000776 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000777 const llvm::Twine &Name,
778 unsigned AddressSpace = 0);
Alexey Bataev97720002014-11-11 04:05:39 +0000779
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000780 /// Set of threadprivate variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000781 llvm::StringSet<> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000782
Alexey Bataev34f8a702018-03-28 14:28:54 +0000783 /// Set of declare target variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000784 llvm::StringSet<> DeclareTargetWithDefinition;
Alexey Bataev34f8a702018-03-28 14:28:54 +0000785
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000786 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000787 /// \param VDAddr Address of the global variable \a VD.
788 /// \param Ctor Pointer to a global init function for \a VD.
789 /// \param CopyCtor Pointer to a global copy function for \a VD.
790 /// \param Dtor Pointer to a global destructor function for \a VD.
791 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000792 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000793 llvm::Value *Ctor, llvm::Value *CopyCtor,
794 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000795
Michael Krused47b9432019-08-05 18:43:21 +0000796 /// Emit the array initialization or deletion portion for user-defined mapper
797 /// code generation.
798 void emitUDMapperArrayInitOrDel(CodeGenFunction &MapperCGF,
799 llvm::Value *Handle, llvm::Value *BasePtr,
800 llvm::Value *Ptr, llvm::Value *Size,
801 llvm::Value *MapType, CharUnits ElementSize,
802 llvm::BasicBlock *ExitBB, bool IsInit);
803
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000804 struct TaskResultTy {
805 llvm::Value *NewTask = nullptr;
James Y Knight9871db02019-02-05 16:42:33 +0000806 llvm::Function *TaskEntry = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000807 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000808 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000809 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000810 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000811 };
812 /// Emit task region for the task directive. The task region is emitted in
813 /// several steps:
814 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
815 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
816 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
817 /// function:
818 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
819 /// TaskFunction(gtid, tt->part_id, tt->shareds);
820 /// return 0;
821 /// }
822 /// 2. Copy a list of shared variables to field shareds of the resulting
823 /// structure kmp_task_t returned by the previous call (if any).
824 /// 3. Copy a pointer to destructions function to field destructions of the
825 /// resulting structure kmp_task_t.
826 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000827 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
828 /// /*part_id*/, captured_struct */*__context*/);
829 /// \param SharedsTy A type which contains references the shared variables.
830 /// \param Shareds Context with the list of shared variables from the \p
831 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000832 /// \param Data Additional data for task generation like tiednsee, final
833 /// state, list of privates etc.
834 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
835 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +0000836 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000837 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000838
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000839 /// Returns default address space for the constant firstprivates, 0 by
840 /// default.
841 virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; }
842
Alexey Bataevec7946e2019-09-23 14:06:51 +0000843 /// Emit code that pushes the trip count of loops associated with constructs
844 /// 'target teams distribute' and 'teams distribute parallel for'.
845 /// \param SizeEmitter Emits the int64 value for the number of iterations of
846 /// the associated loop.
847 void emitTargetNumIterationsCall(
848 CodeGenFunction &CGF, const OMPExecutableDirective &D,
849 llvm::Value *DeviceID,
850 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
851 const OMPLoopDirective &D)>
852 SizeEmitter);
853
Alexey Bataev46978742020-01-30 10:46:11 -0500854 /// Emit update for lastprivate conditional data.
855 void emitLastprivateConditionalUpdate(CodeGenFunction &CGF, LValue IVLVal,
856 StringRef UniqueDeclName, LValue LVal,
857 SourceLocation Loc);
858
Alexey Bataev63093342020-03-09 17:18:19 -0400859 /// Returns the number of the elements and the address of the depobj
860 /// dependency array.
861 /// \return Number of elements in depobj array and the pointer to the array of
862 /// dependencies.
863 std::pair<llvm::Value *, LValue> getDepobjElements(CodeGenFunction &CGF,
864 LValue DepobjLVal,
865 SourceLocation Loc);
866
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000867public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000868 explicit CGOpenMPRuntime(CodeGenModule &CGM)
869 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000870 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000871 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000872
Alexey Bataevd08c0562019-11-19 12:07:54 -0500873 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
874 /// function. Here is the logic:
875 /// if (Cond) {
876 /// ThenGen();
877 /// } else {
878 /// ElseGen();
879 /// }
880 void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
881 const RegionCodeGenTy &ThenGen,
882 const RegionCodeGenTy &ElseGen);
883
Alexey Bataev5c427362019-04-10 19:11:33 +0000884 /// Checks if the \p Body is the \a CompoundStmt and returns its child
885 /// statement iff there is only one that is not evaluatable at the compile
886 /// time.
887 static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
888
Alexey Bataev18fa2322018-05-02 14:20:50 +0000889 /// Get the platform-specific name separator.
890 std::string getName(ArrayRef<StringRef> Parts) const;
891
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000892 /// Emit code for the specified user defined reduction construct.
893 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
894 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000895 /// Get combiner/initializer for the specified user-defined reduction, if any.
896 virtual std::pair<llvm::Function *, llvm::Function *>
897 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000898
Michael Krused47b9432019-08-05 18:43:21 +0000899 /// Emit the function for the user defined mapper construct.
900 void emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
901 CodeGenFunction *CGF = nullptr);
902
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000903 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000904 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
905 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000906 /// \param D OpenMP directive.
907 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000908 /// \param InnermostKind Kind of innermost directive (for simple directives it
909 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000910 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000911 virtual llvm::Function *emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000912 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
913 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
914
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000915 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000916 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
917 /// kmp_int32 BoundID, struct context_vars*).
918 /// \param D OpenMP directive.
919 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
920 /// \param InnermostKind Kind of innermost directive (for simple directives it
921 /// is a directive itself, for combined - its innermost directive).
922 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000923 virtual llvm::Function *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000924 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
925 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000926
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000927 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000928 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
929 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000930 /// \param D OpenMP directive.
931 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000932 /// \param PartIDVar Variable for partition id in the current OpenMP untied
933 /// task region.
934 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000935 /// \param InnermostKind Kind of innermost directive (for simple directives it
936 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000937 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000938 /// \param Tied true if task is generated for tied task, false otherwise.
939 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
940 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000941 ///
James Y Knight9871db02019-02-05 16:42:33 +0000942 virtual llvm::Function *emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000943 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000944 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
945 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
946 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000947
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000948 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000949 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000950 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000951
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000952 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000953 /// variables captured in a record which address is stored in \a
954 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000955 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000956 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000957 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000958 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000959 /// \param IfCond Condition in the associated 'if' clause, if it was
960 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000961 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000962 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +0000963 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000964 ArrayRef<llvm::Value *> CapturedVars,
965 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000966
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000967 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000968 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000969 /// \param CriticalOpGen Generator for the statement associated with the given
970 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000971 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000972 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000973 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000974 SourceLocation Loc,
975 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000976
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000977 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000978 /// \param MasterOpGen Generator for the statement associated with the given
979 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000980 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000981 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000982 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000983
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000984 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000985 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000986
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000987 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000988 /// \param TaskgroupOpGen Generator for the statement associated with the
989 /// given taskgroup region.
990 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
991 const RegionCodeGenTy &TaskgroupOpGen,
992 SourceLocation Loc);
993
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000994 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000995 /// \param SingleOpGen Generator for the statement associated with the given
996 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000997 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000998 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000999 SourceLocation Loc,
1000 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +00001001 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +00001002 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +00001003 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001004
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001005 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001006 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001007 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001008 virtual void emitOrderedRegion(CodeGenFunction &CGF,
1009 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +00001010 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001011
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001012 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +00001013 /// \param Kind Directive for which this implicit barrier call must be
1014 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001015 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1016 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1017 /// runtime class decides which one to emit (simple or with cancellation
1018 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001019 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001020 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001021 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001022 bool EmitChecks = true,
1023 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +00001024
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001025 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +00001026 /// This kind of worksharing directive is emitted without outer loop.
1027 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1028 /// \param Chunked True if chunk is specified in the clause.
1029 ///
1030 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
1031 bool Chunked) const;
1032
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001033 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001034 /// This kind of distribute directive is emitted without outer loop.
1035 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1036 /// \param Chunked True if chunk is specified in the clause.
1037 ///
1038 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
1039 bool Chunked) const;
1040
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00001041 /// Check if the specified \a ScheduleKind is static chunked.
1042 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1043 /// \param Chunked True if chunk is specified in the clause.
1044 ///
1045 virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
1046 bool Chunked) const;
1047
1048 /// Check if the specified \a ScheduleKind is static non-chunked.
1049 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1050 /// \param Chunked True if chunk is specified in the clause.
1051 ///
1052 virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
1053 bool Chunked) const;
1054
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001055 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001056 /// This kind of worksharing directive is emitted without outer loop.
1057 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
1058 ///
1059 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
1060
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001061 /// struct with the values to be passed to the dispatch runtime function
1062 struct DispatchRTInput {
1063 /// Loop lower bound
1064 llvm::Value *LB = nullptr;
1065 /// Loop upper bound
1066 llvm::Value *UB = nullptr;
1067 /// Chunk size specified using 'schedule' clause (nullptr if chunk
1068 /// was not specified)
1069 llvm::Value *Chunk = nullptr;
1070 DispatchRTInput() = default;
1071 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
1072 : LB(LB), UB(UB), Chunk(Chunk) {}
1073 };
1074
1075 /// Call the appropriate runtime routine to initialize it before start
1076 /// of loop.
1077
1078 /// This is used for non static scheduled types and when the ordered
1079 /// clause is present on the loop construct.
1080 /// Depending on the loop schedule, it is necessary to call some runtime
1081 /// routine before start of the OpenMP loop to get the loop upper / lower
1082 /// bounds \a LB and \a UB and stride \a ST.
1083 ///
1084 /// \param CGF Reference to current CodeGenFunction.
1085 /// \param Loc Clang source location.
1086 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1087 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001088 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001089 /// \param Ordered true if loop is ordered, false otherwise.
1090 /// \param DispatchValues struct containing llvm values for lower bound, upper
1091 /// bound, and chunk expression.
1092 /// For the default (nullptr) value, the chunk 1 will be used.
1093 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001094 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001095 const OpenMPScheduleTy &ScheduleKind,
1096 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001097 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001098
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001099 /// Struct with the values to be passed to the static runtime function
1100 struct StaticRTInput {
1101 /// Size of the iteration variable in bits.
1102 unsigned IVSize = 0;
1103 /// Sign of the iteration variable.
1104 bool IVSigned = false;
1105 /// true if loop is ordered, false otherwise.
1106 bool Ordered = false;
1107 /// Address of the output variable in which the flag of the last iteration
1108 /// is returned.
1109 Address IL = Address::invalid();
1110 /// Address of the output variable in which the lower iteration number is
1111 /// returned.
1112 Address LB = Address::invalid();
1113 /// Address of the output variable in which the upper iteration number is
1114 /// returned.
1115 Address UB = Address::invalid();
1116 /// Address of the output variable in which the stride value is returned
1117 /// necessary to generated the static_chunked scheduled loop.
1118 Address ST = Address::invalid();
1119 /// Value of the chunk for the static_chunked scheduled loop. For the
1120 /// default (nullptr) value, the chunk 1 will be used.
1121 llvm::Value *Chunk = nullptr;
1122 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1123 Address LB, Address UB, Address ST,
1124 llvm::Value *Chunk = nullptr)
1125 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1126 UB(UB), ST(ST), Chunk(Chunk) {}
1127 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001128 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +00001129 /// of loop.
1130 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001131 /// This is used only in case of static schedule, when the user did not
1132 /// specify a ordered clause on the loop construct.
1133 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +00001134 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001135 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +00001136 ///
1137 /// \param CGF Reference to current CodeGenFunction.
1138 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001139 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001140 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001141 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +00001142 ///
John McCall7f416cc2015-09-08 08:05:57 +00001143 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001144 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001145 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001146 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00001147
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001148 ///
1149 /// \param CGF Reference to current CodeGenFunction.
1150 /// \param Loc Clang source location.
1151 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001152 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001153 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001154 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1155 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001156 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001157 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001159 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001160 /// iteration of the ordered loop with the dynamic scheduling.
1161 ///
1162 /// \param CGF Reference to current CodeGenFunction.
1163 /// \param Loc Clang source location.
1164 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001165 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001166 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001167 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1168 SourceLocation Loc, unsigned IVSize,
1169 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001170
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001171 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001172 /// all the work with current loop.
1173 ///
1174 /// \param CGF Reference to current CodeGenFunction.
1175 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001176 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001177 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001178 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1179 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001180
Alexander Musman92bdaab2015-03-12 13:37:50 +00001181 /// Call __kmpc_dispatch_next(
1182 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1183 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1184 /// kmp_int[32|64] *p_stride);
1185 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001186 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001187 /// \param IL Address of the output variable in which the flag of the
1188 /// last iteration is returned.
1189 /// \param LB Address of the output variable in which the lower iteration
1190 /// number is returned.
1191 /// \param UB Address of the output variable in which the upper iteration
1192 /// number is returned.
1193 /// \param ST Address of the output variable in which the stride value is
1194 /// returned.
1195 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1196 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001197 Address IL, Address LB,
1198 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001200 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001201 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1202 /// clause.
1203 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001204 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1205 llvm::Value *NumThreads,
1206 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001208 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001209 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1210 virtual void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001211 llvm::omp::ProcBindKind ProcBind,
Alexey Bataev7f210c62015-06-18 13:40:03 +00001212 SourceLocation Loc);
1213
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001214 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001215 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001216 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001217 /// \param VDAddr Address of the global variable \a VD.
1218 /// \param Loc Location of the reference to threadprivate var.
1219 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001220 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1221 const VarDecl *VD,
1222 Address VDAddr,
1223 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001224
Alexey Bataev92327c52018-03-26 16:40:55 +00001225 /// Returns the address of the variable marked as declare target with link
Gheorghe-Teodor Bercea0034e842019-06-20 18:04:47 +00001226 /// clause OR as declare target with to clause and unified memory.
1227 virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001229 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001230 /// a call to runtime library which adds initial value to the newly created
1231 /// threadprivate variable (if it is not constant) and registers destructor
1232 /// for the variable (if any).
1233 /// \param VD Threadprivate variable.
1234 /// \param VDAddr Address of the global variable \a VD.
1235 /// \param Loc Location of threadprivate declaration.
1236 /// \param PerformInit true if initialization expression is not constant.
1237 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001238 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001239 SourceLocation Loc, bool PerformInit,
1240 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001242 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001243 /// \param VD Declare target variable.
1244 /// \param Addr Address of the global variable \a VD.
1245 /// \param PerformInit true if initialization expression is not constant.
1246 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1247 llvm::GlobalVariable *Addr,
1248 bool PerformInit);
1249
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001250 /// Creates artificial threadprivate variable with name \p Name and type \p
1251 /// VarType.
1252 /// \param VarType Type of the artificial threadprivate variable.
1253 /// \param Name Name of the artificial threadprivate variable.
1254 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1255 QualType VarType,
1256 StringRef Name);
1257
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001258 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001259 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001260 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
Alexey Bataeve8e05de2020-02-07 12:22:23 -05001261 SourceLocation Loc, llvm::AtomicOrdering AO);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001263 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001264 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001265 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1266 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1267 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1268 /// function:
1269 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1270 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1271 /// return 0;
1272 /// }
1273 /// 2. Copy a list of shared variables to field shareds of the resulting
1274 /// structure kmp_task_t returned by the previous call (if any).
1275 /// 3. Copy a pointer to destructions function to field destructions of the
1276 /// resulting structure kmp_task_t.
1277 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1278 /// kmp_task_t *new_task), where new_task is a resulting structure from
1279 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001280 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001281 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1282 /// /*part_id*/, captured_struct */*__context*/);
1283 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001284 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001285 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001286 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1287 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001288 /// \param Data Additional data for task generation like tiednsee, final
1289 /// state, list of privates etc.
1290 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1291 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001292 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001293 Address Shareds, const Expr *IfCond,
1294 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001295
Alexey Bataev7292c292016-04-25 12:22:29 +00001296 /// Emit task region for the taskloop directive. The taskloop region is
1297 /// emitted in several steps:
1298 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1299 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1300 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1301 /// function:
1302 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1303 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1304 /// return 0;
1305 /// }
1306 /// 2. Copy a list of shared variables to field shareds of the resulting
1307 /// structure kmp_task_t returned by the previous call (if any).
1308 /// 3. Copy a pointer to destructions function to field destructions of the
1309 /// resulting structure kmp_task_t.
1310 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1311 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1312 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1313 /// is a resulting structure from
1314 /// previous items.
1315 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001316 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1317 /// /*part_id*/, captured_struct */*__context*/);
1318 /// \param SharedsTy A type which contains references the shared variables.
1319 /// \param Shareds Context with the list of shared variables from the \p
1320 /// TaskFunction.
1321 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1322 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001323 /// \param Data Additional data for task generation like tiednsee, final
1324 /// state, list of privates etc.
James Y Knight9871db02019-02-05 16:42:33 +00001325 virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1326 const OMPLoopDirective &D,
1327 llvm::Function *TaskFunction,
1328 QualType SharedsTy, Address Shareds,
1329 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001330
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001331 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001332 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001333 /// \param InnermostKind Kind of innermost directive (for simple directives it
1334 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001335 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001336 /// \param HasCancel true if region has inner cancel directive, false
1337 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001338 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001339 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001340 const RegionCodeGenTy &CodeGen,
1341 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001342
1343 /// Emits reduction function.
1344 /// \param ArgsType Array type containing pointers to reduction variables.
1345 /// \param Privates List of private copies for original reduction arguments.
1346 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1347 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1348 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1349 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev982a35e2019-03-19 17:09:52 +00001350 llvm::Function *emitReductionFunction(SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001351 llvm::Type *ArgsType,
1352 ArrayRef<const Expr *> Privates,
1353 ArrayRef<const Expr *> LHSExprs,
1354 ArrayRef<const Expr *> RHSExprs,
1355 ArrayRef<const Expr *> ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001356
1357 /// Emits single reduction combiner
1358 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1359 const Expr *ReductionOp,
1360 const Expr *PrivateRef,
1361 const DeclRefExpr *LHS,
1362 const DeclRefExpr *RHS);
1363
1364 struct ReductionOptionsTy {
1365 bool WithNowait;
1366 bool SimpleReduction;
1367 OpenMPDirectiveKind ReductionKind;
1368 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001369 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001370 /// reduction:
1371 /// \code
1372 ///
1373 /// static kmp_critical_name lock = { 0 };
1374 ///
1375 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1376 /// ...
1377 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1378 /// ...
1379 /// }
1380 ///
1381 /// ...
1382 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1383 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1384 /// RedList, reduce_func, &<lock>)) {
1385 /// case 1:
1386 /// ...
1387 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1388 /// ...
1389 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1390 /// break;
1391 /// case 2:
1392 /// ...
1393 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1394 /// ...
1395 /// break;
1396 /// default:;
1397 /// }
1398 /// \endcode
1399 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001400 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001401 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1402 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1403 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1404 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001405 /// \param Options List of options for reduction codegen:
1406 /// WithNowait true if parent directive has also nowait clause, false
1407 /// otherwise.
1408 /// SimpleReduction Emit reduction operation only. Used for omp simd
1409 /// directive on the host.
1410 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001411 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001412 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001413 ArrayRef<const Expr *> LHSExprs,
1414 ArrayRef<const Expr *> RHSExprs,
1415 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001416 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001417
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001418 /// Emit a code for initialization of task reduction clause. Next code
1419 /// should be emitted for reduction:
1420 /// \code
1421 ///
1422 /// _task_red_item_t red_data[n];
1423 /// ...
1424 /// red_data[i].shar = &origs[i];
1425 /// red_data[i].size = sizeof(origs[i]);
1426 /// red_data[i].f_init = (void*)RedInit<i>;
1427 /// red_data[i].f_fini = (void*)RedDest<i>;
1428 /// red_data[i].f_comb = (void*)RedOp<i>;
1429 /// red_data[i].flags = <Flag_i>;
1430 /// ...
1431 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1432 /// \endcode
1433 ///
1434 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1435 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1436 /// \param Data Additional data for task generation like tiedness, final
1437 /// state, list of privates, reductions etc.
1438 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1439 SourceLocation Loc,
1440 ArrayRef<const Expr *> LHSExprs,
1441 ArrayRef<const Expr *> RHSExprs,
1442 const OMPTaskDataTy &Data);
1443
1444 /// Required to resolve existing problems in the runtime. Emits threadprivate
1445 /// variables to store the size of the VLAs/array sections for
1446 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1447 /// store the pointer to the original reduction item for the custom
1448 /// initializer defined by declare reduction construct.
1449 /// \param RCG Allows to reuse an existing data for the reductions.
1450 /// \param N Reduction item for which fixups must be emitted.
1451 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1452 ReductionCodeGen &RCG, unsigned N);
1453
1454 /// Get the address of `void *` type of the privatue copy of the reduction
1455 /// item specified by the \p SharedLVal.
1456 /// \param ReductionsPtr Pointer to the reduction data returned by the
1457 /// emitTaskReductionInit function.
1458 /// \param SharedLVal Address of the original reduction item.
1459 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1460 llvm::Value *ReductionsPtr,
1461 LValue SharedLVal);
1462
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001463 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001464 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001465
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001466 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001467 /// \param CancelRegion Region kind for which the cancellation point must be
1468 /// emitted.
1469 ///
1470 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1471 SourceLocation Loc,
1472 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001473
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001474 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001475 /// \param IfCond Condition in the associated 'if' clause, if it was
1476 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001477 /// \param CancelRegion Region kind for which the cancel must be emitted.
1478 ///
1479 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001480 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001481 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001482
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001483 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001484 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001485 /// \param ParentName Name of the function that encloses the target region.
1486 /// \param OutlinedFn Outlined function value to be defined by this call.
1487 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1488 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001489 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001490 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001491 /// evaluates to false.
1492 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1493 StringRef ParentName,
1494 llvm::Function *&OutlinedFn,
1495 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001496 bool IsOffloadEntry,
1497 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001498
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001499 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001500 /// code attempts offloading the execution to the device, an the event of
1501 /// a failure it executes the host version outlined in \a OutlinedFn.
1502 /// \param D Directive to emit.
1503 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001504 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001505 /// \param IfCond Expression evaluated in if clause associated with the target
1506 /// directive, or null if no if clause is used.
1507 /// \param Device Expression evaluated in device clause associated with the
Alexey Bataevf3c857f2020-03-18 17:52:41 -04001508 /// target directive, or null if no device clause is used and device modifier.
Alexey Bataevec7946e2019-09-23 14:06:51 +00001509 /// \param SizeEmitter Callback to emit number of iterations for loop-based
1510 /// directives.
Alexey Bataevf3c857f2020-03-18 17:52:41 -04001511 virtual void emitTargetCall(
1512 CodeGenFunction &CGF, const OMPExecutableDirective &D,
1513 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond,
1514 llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
1515 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
1516 const OMPLoopDirective &D)>
1517 SizeEmitter);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001518
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001519 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001520 /// the function itself in case it is a valid device function. Returns true if
1521 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001522 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001523 virtual bool emitTargetFunctions(GlobalDecl GD);
1524
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001525 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001526 /// Returns true if \a GD was dealt with successfully.
1527 /// \param GD Variable declaration to emit.
1528 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1529
Alexey Bataev03f270c2018-03-30 18:31:07 +00001530 /// Checks if the provided global decl \a GD is a declare target variable and
1531 /// registers it when emitting code for the host.
1532 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1533 llvm::Constant *Addr);
1534
Alexey Bataev1af5bd52019-03-05 17:47:18 +00001535 /// Registers provided target firstprivate variable as global on the
1536 /// target.
1537 llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1538 const VarDecl *VD);
1539
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001540 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001541 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001542 /// \param GD Global to scan.
1543 virtual bool emitTargetGlobal(GlobalDecl GD);
1544
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001545 /// Creates and returns a registration function for when at least one
1546 /// requires directives was used in the current module.
1547 llvm::Function *emitRequiresDirectiveRegFun();
1548
Sergey Dmitriev5836c352019-10-15 18:42:47 +00001549 /// Creates all the offload entries in the current compilation unit
1550 /// along with the associated metadata.
1551 void createOffloadEntriesAndInfoMetadata();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001552
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001553 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001554 /// variables captured in a record which address is stored in \a
1555 /// CapturedStruct.
1556 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1557 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1558 /// \param CapturedVars A pointer to the record with the references to
1559 /// variables used in \a OutlinedFn function.
1560 ///
1561 virtual void emitTeamsCall(CodeGenFunction &CGF,
1562 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001563 SourceLocation Loc, llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001564 ArrayRef<llvm::Value *> CapturedVars);
1565
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001566 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001567 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1568 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001569 /// \param NumTeams An integer expression of teams.
1570 /// \param ThreadLimit An integer expression of threads.
1571 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1572 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001573
Samuel Antaocc10b852016-07-28 14:23:26 +00001574 /// Struct that keeps all the relevant information that should be kept
1575 /// throughout a 'target data' region.
1576 class TargetDataInfo {
1577 /// Set to true if device pointer information have to be obtained.
1578 bool RequiresDevicePointerInfo = false;
1579
1580 public:
1581 /// The array of base pointer passed to the runtime library.
1582 llvm::Value *BasePointersArray = nullptr;
1583 /// The array of section pointers passed to the runtime library.
1584 llvm::Value *PointersArray = nullptr;
1585 /// The array of sizes passed to the runtime library.
1586 llvm::Value *SizesArray = nullptr;
1587 /// The array of map types passed to the runtime library.
1588 llvm::Value *MapTypesArray = nullptr;
1589 /// The total number of pointers passed to the runtime library.
1590 unsigned NumberOfPtrs = 0u;
1591 /// Map between the a declaration of a capture and the corresponding base
1592 /// pointer address where the runtime returns the device pointers.
1593 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1594
1595 explicit TargetDataInfo() {}
1596 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1597 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1598 /// Clear information about the data arrays.
1599 void clearArrayInfo() {
1600 BasePointersArray = nullptr;
1601 PointersArray = nullptr;
1602 SizesArray = nullptr;
1603 MapTypesArray = nullptr;
1604 NumberOfPtrs = 0u;
1605 }
1606 /// Return true if the current target data information has valid arrays.
1607 bool isValid() {
1608 return BasePointersArray && PointersArray && SizesArray &&
1609 MapTypesArray && NumberOfPtrs;
1610 }
1611 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1612 };
1613
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001614 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001615 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001616 /// \param IfCond Expression evaluated in if clause associated with the
1617 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001618 /// \param Device Expression evaluated in device clause associated with the
1619 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001620 /// \param Info A record used to store information that needs to be preserved
1621 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001622 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1623 const OMPExecutableDirective &D,
1624 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001625 const RegionCodeGenTy &CodeGen,
1626 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001627
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001628 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001629 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001630 /// \param D Directive to emit.
1631 /// \param IfCond Expression evaluated in if clause associated with the target
1632 /// directive, or null if no if clause is used.
1633 /// \param Device Expression evaluated in device clause associated with the
1634 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001635 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1636 const OMPExecutableDirective &D,
1637 const Expr *IfCond,
1638 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001639
1640 /// Marks function \a Fn with properly mangled versions of vector functions.
1641 /// \param FD Function marked as 'declare simd'.
1642 /// \param Fn LLVM function that must be marked with 'declare simd'
1643 /// attributes.
1644 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1645 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001646
1647 /// Emit initialization for doacross loop nesting support.
1648 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001649 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1650 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001651
1652 /// Emit code for doacross ordered directive with 'depend' clause.
1653 /// \param C 'depend' clause with 'sink|source' dependency kind.
1654 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1655 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001656
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001657 /// Translates the native parameter of outlined function if this is required
1658 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001659 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001660 /// \param NativeParam Parameter itself.
1661 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1662 const VarDecl *NativeParam) const {
1663 return NativeParam;
1664 }
1665
1666 /// Gets the address of the native argument basing on the address of the
1667 /// target-specific parameter.
1668 /// \param NativeParam Parameter itself.
1669 /// \param TargetParam Corresponding target-specific parameter.
1670 virtual Address getParameterAddress(CodeGenFunction &CGF,
1671 const VarDecl *NativeParam,
1672 const VarDecl *TargetParam) const;
1673
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001674 /// Choose default schedule type and chunk value for the
1675 /// dist_schedule clause.
1676 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1677 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1678 llvm::Value *&Chunk) const {}
1679
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001680 /// Choose default schedule type and chunk value for the
1681 /// schedule clause.
1682 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1683 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
Alexey Bataevf6a53d62019-03-18 18:40:00 +00001684 const Expr *&ChunkExpr) const;
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001685
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001686 /// Emits call of the outlined function with the provided arguments,
1687 /// translating these arguments to correct target-specific arguments.
1688 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001689 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001690 llvm::FunctionCallee OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001691 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001692
1693 /// Emits OpenMP-specific function prolog.
1694 /// Required for device constructs.
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001695 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001696
1697 /// Gets the OpenMP-specific address of the local variable.
1698 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1699 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001700
Raphael Isemannb23ccec2018-12-10 12:37:46 +00001701 /// Marks the declaration as already emitted for the device code and returns
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001702 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001703 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001704
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001705 /// Emit deferred declare target variables marked for deferred emission.
1706 void emitDeferredTargetDecls() const;
Alexey Bataev60705422018-10-30 15:50:12 +00001707
1708 /// Adjust some parameters for the target-based directives, like addresses of
1709 /// the variables captured by reference in lambdas.
1710 virtual void
1711 adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
1712 const OMPExecutableDirective &D) const;
Patrick Lyster8f7f5862018-11-19 15:09:33 +00001713
1714 /// Perform check on requires decl to ensure that target architecture
1715 /// supports unified addressing
Alexey Bataev2d4f80f2020-02-11 15:15:21 -05001716 virtual void processRequiresDirective(const OMPRequiresDecl *D);
1717
1718 /// Gets default memory ordering as specified in requires directive.
1719 llvm::AtomicOrdering getDefaultMemoryOrdering() const;
Alexey Bataevc5687252019-03-21 19:35:27 +00001720
1721 /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1722 /// the predefined allocator and translates it into the corresponding address
1723 /// space.
1724 virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00001725
1726 /// Return whether the unified_shared_memory has been specified.
1727 bool hasRequiresUnifiedSharedMemory() const;
Alexey Bataev2df5f122019-10-01 20:18:32 +00001728
1729 /// Emits the definition of the declare variant function.
1730 virtual bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition);
Alexey Bataev0860db92019-12-19 10:01:10 -05001731
1732 /// Checks if the \p VD variable is marked as nontemporal declaration in
1733 /// current context.
1734 bool isNontemporalDecl(const ValueDecl *VD) const;
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001735
Alexey Bataev46978742020-01-30 10:46:11 -05001736 /// Create specialized alloca to handle lastprivate conditionals.
1737 Address emitLastprivateConditionalInit(CodeGenFunction &CGF,
1738 const VarDecl *VD);
1739
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001740 /// Checks if the provided \p LVal is lastprivate conditional and emits the
1741 /// code to update the value of the original variable.
1742 /// \code
1743 /// lastprivate(conditional: a)
1744 /// ...
1745 /// <type> a;
1746 /// lp_a = ...;
1747 /// #pragma omp critical(a)
1748 /// if (last_iv_a <= iv) {
1749 /// last_iv_a = iv;
1750 /// global_a = lp_a;
1751 /// }
1752 /// \endcode
1753 virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF,
1754 const Expr *LHS);
1755
Alexey Bataev46978742020-01-30 10:46:11 -05001756 /// Checks if the lastprivate conditional was updated in inner region and
1757 /// writes the value.
1758 /// \code
1759 /// lastprivate(conditional: a)
1760 /// ...
1761 /// <type> a;bool Fired = false;
1762 /// #pragma omp ... shared(a)
1763 /// {
1764 /// lp_a = ...;
1765 /// Fired = true;
1766 /// }
1767 /// if (Fired) {
1768 /// #pragma omp critical(a)
1769 /// if (last_iv_a <= iv) {
1770 /// last_iv_a = iv;
1771 /// global_a = lp_a;
1772 /// }
1773 /// Fired = false;
1774 /// }
1775 /// \endcode
1776 virtual void checkAndEmitSharedLastprivateConditional(
1777 CodeGenFunction &CGF, const OMPExecutableDirective &D,
1778 const llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> &IgnoredDecls);
1779
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001780 /// Gets the address of the global copy used for lastprivate conditional
1781 /// update, if any.
1782 /// \param PrivLVal LValue for the private copy.
1783 /// \param VD Original lastprivate declaration.
1784 virtual void emitLastprivateConditionalFinalUpdate(CodeGenFunction &CGF,
1785 LValue PrivLVal,
1786 const VarDecl *VD,
1787 SourceLocation Loc);
Alexey Bataeve46f0fe2020-03-04 14:37:51 -05001788
1789 /// Emits list of dependecies based on the provided data (array of
1790 /// dependence/expression pairs).
1791 /// \param ForDepobj true if the memory for depencies is alloacted for depobj
1792 /// directive. In this case, the variable is allocated in dynamically.
1793 /// \returns Pointer to the first element of the array casted to VoidPtr type.
Alexey Bataev63093342020-03-09 17:18:19 -04001794 std::pair<llvm::Value *, Address> emitDependClause(
Alexey Bataeve46f0fe2020-03-04 14:37:51 -05001795 CodeGenFunction &CGF,
1796 ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependencies,
1797 bool ForDepobj, SourceLocation Loc);
Alexey Bataevb27ff4d2020-03-04 16:15:28 -05001798
1799 /// Emits the code to destroy the dependency object provided in depobj
1800 /// directive.
1801 void emitDestroyClause(CodeGenFunction &CGF, LValue DepobjLVal,
1802 SourceLocation Loc);
Alexey Bataev8d7b1182020-03-05 13:45:28 -05001803
1804 /// Updates the dependency kind in the specified depobj object.
1805 /// \param DepobjLVal LValue for the main depobj object.
1806 /// \param NewDepKind New dependency kind.
1807 void emitUpdateClause(CodeGenFunction &CGF, LValue DepobjLVal,
1808 OpenMPDependClauseKind NewDepKind, SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +00001809};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001810
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001811/// Class supports emissionof SIMD-only code.
1812class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1813public:
1814 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1815 ~CGOpenMPSIMDRuntime() override {}
1816
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001817 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001818 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1819 /// kmp_int32 BoundID, struct context_vars*).
1820 /// \param D OpenMP directive.
1821 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1822 /// \param InnermostKind Kind of innermost directive (for simple directives it
1823 /// is a directive itself, for combined - its innermost directive).
1824 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001825 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001826 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1827 const VarDecl *ThreadIDVar,
1828 OpenMPDirectiveKind InnermostKind,
1829 const RegionCodeGenTy &CodeGen) override;
1830
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001831 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001832 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1833 /// kmp_int32 BoundID, struct context_vars*).
1834 /// \param D OpenMP directive.
1835 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1836 /// \param InnermostKind Kind of innermost directive (for simple directives it
1837 /// is a directive itself, for combined - its innermost directive).
1838 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001839 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001840 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1841 const VarDecl *ThreadIDVar,
1842 OpenMPDirectiveKind InnermostKind,
1843 const RegionCodeGenTy &CodeGen) override;
1844
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001845 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001846 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1847 /// TaskT).
1848 /// \param D OpenMP directive.
1849 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1850 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1851 /// task region.
1852 /// \param TaskTVar Variable for task_t argument.
1853 /// \param InnermostKind Kind of innermost directive (for simple directives it
1854 /// is a directive itself, for combined - its innermost directive).
1855 /// \param CodeGen Code generation sequence for the \a D directive.
1856 /// \param Tied true if task is generated for tied task, false otherwise.
1857 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1858 /// tasks.
1859 ///
James Y Knight9871db02019-02-05 16:42:33 +00001860 llvm::Function *emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001861 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1862 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1863 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1864 bool Tied, unsigned &NumberOfParts) override;
1865
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001866 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001867 /// variables captured in a record which address is stored in \a
1868 /// CapturedStruct.
1869 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1870 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1871 /// \param CapturedVars A pointer to the record with the references to
1872 /// variables used in \a OutlinedFn function.
1873 /// \param IfCond Condition in the associated 'if' clause, if it was
1874 /// specified, nullptr otherwise.
1875 ///
1876 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001877 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001878 ArrayRef<llvm::Value *> CapturedVars,
1879 const Expr *IfCond) override;
1880
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001881 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001882 /// \param CriticalName Name of the critical region.
1883 /// \param CriticalOpGen Generator for the statement associated with the given
1884 /// critical region.
1885 /// \param Hint Value of the 'hint' clause (optional).
1886 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1887 const RegionCodeGenTy &CriticalOpGen,
1888 SourceLocation Loc,
1889 const Expr *Hint = nullptr) override;
1890
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001891 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001892 /// \param MasterOpGen Generator for the statement associated with the given
1893 /// master region.
1894 void emitMasterRegion(CodeGenFunction &CGF,
1895 const RegionCodeGenTy &MasterOpGen,
1896 SourceLocation Loc) override;
1897
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001898 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001899 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1900
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001901 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001902 /// \param TaskgroupOpGen Generator for the statement associated with the
1903 /// given taskgroup region.
1904 void emitTaskgroupRegion(CodeGenFunction &CGF,
1905 const RegionCodeGenTy &TaskgroupOpGen,
1906 SourceLocation Loc) override;
1907
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001908 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001909 /// \param SingleOpGen Generator for the statement associated with the given
1910 /// single region.
1911 void emitSingleRegion(CodeGenFunction &CGF,
1912 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1913 ArrayRef<const Expr *> CopyprivateVars,
1914 ArrayRef<const Expr *> DestExprs,
1915 ArrayRef<const Expr *> SrcExprs,
1916 ArrayRef<const Expr *> AssignmentOps) override;
1917
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001918 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001919 /// \param OrderedOpGen Generator for the statement associated with the given
1920 /// ordered region.
1921 void emitOrderedRegion(CodeGenFunction &CGF,
1922 const RegionCodeGenTy &OrderedOpGen,
1923 SourceLocation Loc, bool IsThreads) override;
1924
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001925 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001926 /// \param Kind Directive for which this implicit barrier call must be
1927 /// generated. Must be OMPD_barrier for explicit barrier generation.
1928 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1929 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1930 /// runtime class decides which one to emit (simple or with cancellation
1931 /// checks).
1932 ///
1933 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1934 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1935 bool ForceSimpleCall = false) override;
1936
1937 /// This is used for non static scheduled types and when the ordered
1938 /// clause is present on the loop construct.
1939 /// Depending on the loop schedule, it is necessary to call some runtime
1940 /// routine before start of the OpenMP loop to get the loop upper / lower
1941 /// bounds \a LB and \a UB and stride \a ST.
1942 ///
1943 /// \param CGF Reference to current CodeGenFunction.
1944 /// \param Loc Clang source location.
1945 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1946 /// \param IVSize Size of the iteration variable in bits.
1947 /// \param IVSigned Sign of the iteration variable.
1948 /// \param Ordered true if loop is ordered, false otherwise.
1949 /// \param DispatchValues struct containing llvm values for lower bound, upper
1950 /// bound, and chunk expression.
1951 /// For the default (nullptr) value, the chunk 1 will be used.
1952 ///
1953 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1954 const OpenMPScheduleTy &ScheduleKind,
1955 unsigned IVSize, bool IVSigned, bool Ordered,
1956 const DispatchRTInput &DispatchValues) override;
1957
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001958 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001959 /// of loop.
1960 ///
1961 /// This is used only in case of static schedule, when the user did not
1962 /// specify a ordered clause on the loop construct.
1963 /// Depending on the loop schedule, it is necessary to call some runtime
1964 /// routine before start of the OpenMP loop to get the loop upper / lower
1965 /// bounds LB and UB and stride ST.
1966 ///
1967 /// \param CGF Reference to current CodeGenFunction.
1968 /// \param Loc Clang source location.
1969 /// \param DKind Kind of the directive.
1970 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1971 /// \param Values Input arguments for the construct.
1972 ///
1973 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1974 OpenMPDirectiveKind DKind,
1975 const OpenMPScheduleTy &ScheduleKind,
1976 const StaticRTInput &Values) override;
1977
1978 ///
1979 /// \param CGF Reference to current CodeGenFunction.
1980 /// \param Loc Clang source location.
1981 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1982 /// \param Values Input arguments for the construct.
1983 ///
1984 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1985 OpenMPDistScheduleClauseKind SchedKind,
1986 const StaticRTInput &Values) override;
1987
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001988 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001989 /// iteration of the ordered loop with the dynamic scheduling.
1990 ///
1991 /// \param CGF Reference to current CodeGenFunction.
1992 /// \param Loc Clang source location.
1993 /// \param IVSize Size of the iteration variable in bits.
1994 /// \param IVSigned Sign of the iteration variable.
1995 ///
1996 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1997 unsigned IVSize, bool IVSigned) override;
1998
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001999 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002000 /// all the work with current loop.
2001 ///
2002 /// \param CGF Reference to current CodeGenFunction.
2003 /// \param Loc Clang source location.
2004 /// \param DKind Kind of the directive for which the static finish is emitted.
2005 ///
2006 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
2007 OpenMPDirectiveKind DKind) override;
2008
2009 /// Call __kmpc_dispatch_next(
2010 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
2011 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
2012 /// kmp_int[32|64] *p_stride);
2013 /// \param IVSize Size of the iteration variable in bits.
2014 /// \param IVSigned Sign of the iteration variable.
2015 /// \param IL Address of the output variable in which the flag of the
2016 /// last iteration is returned.
2017 /// \param LB Address of the output variable in which the lower iteration
2018 /// number is returned.
2019 /// \param UB Address of the output variable in which the upper iteration
2020 /// number is returned.
2021 /// \param ST Address of the output variable in which the stride value is
2022 /// returned.
2023 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
2024 unsigned IVSize, bool IVSigned, Address IL,
2025 Address LB, Address UB, Address ST) override;
2026
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002027 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002028 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
2029 /// clause.
2030 /// \param NumThreads An integer value of threads.
2031 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
2032 SourceLocation Loc) override;
2033
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002034 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002035 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
2036 void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06002037 llvm::omp::ProcBindKind ProcBind,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002038 SourceLocation Loc) override;
2039
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002040 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002041 /// thread.
2042 /// \param VD Threadprivate variable.
2043 /// \param VDAddr Address of the global variable \a VD.
2044 /// \param Loc Location of the reference to threadprivate var.
2045 /// \return Address of the threadprivate variable for the current thread.
2046 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
2047 Address VDAddr, SourceLocation Loc) override;
2048
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002049 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002050 /// a call to runtime library which adds initial value to the newly created
2051 /// threadprivate variable (if it is not constant) and registers destructor
2052 /// for the variable (if any).
2053 /// \param VD Threadprivate variable.
2054 /// \param VDAddr Address of the global variable \a VD.
2055 /// \param Loc Location of threadprivate declaration.
2056 /// \param PerformInit true if initialization expression is not constant.
2057 llvm::Function *
2058 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
2059 SourceLocation Loc, bool PerformInit,
2060 CodeGenFunction *CGF = nullptr) override;
2061
2062 /// Creates artificial threadprivate variable with name \p Name and type \p
2063 /// VarType.
2064 /// \param VarType Type of the artificial threadprivate variable.
2065 /// \param Name Name of the artificial threadprivate variable.
2066 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
2067 QualType VarType,
2068 StringRef Name) override;
2069
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002070 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002071 /// \param Vars List of variables to flush.
2072 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
Alexey Bataeve8e05de2020-02-07 12:22:23 -05002073 SourceLocation Loc, llvm::AtomicOrdering AO) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002074
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002075 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002076 /// emitted in several steps:
2077 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2078 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2079 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2080 /// function:
2081 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2082 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2083 /// return 0;
2084 /// }
2085 /// 2. Copy a list of shared variables to field shareds of the resulting
2086 /// structure kmp_task_t returned by the previous call (if any).
2087 /// 3. Copy a pointer to destructions function to field destructions of the
2088 /// resulting structure kmp_task_t.
2089 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
2090 /// kmp_task_t *new_task), where new_task is a resulting structure from
2091 /// previous items.
2092 /// \param D Current task directive.
2093 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2094 /// /*part_id*/, captured_struct */*__context*/);
2095 /// \param SharedsTy A type which contains references the shared variables.
2096 /// \param Shareds Context with the list of shared variables from the \p
2097 /// TaskFunction.
2098 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2099 /// otherwise.
2100 /// \param Data Additional data for task generation like tiednsee, final
2101 /// state, list of privates etc.
2102 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002103 const OMPExecutableDirective &D,
2104 llvm::Function *TaskFunction, QualType SharedsTy,
2105 Address Shareds, const Expr *IfCond,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002106 const OMPTaskDataTy &Data) override;
2107
2108 /// Emit task region for the taskloop directive. The taskloop region is
2109 /// emitted in several steps:
2110 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2111 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2112 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2113 /// function:
2114 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2115 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2116 /// return 0;
2117 /// }
2118 /// 2. Copy a list of shared variables to field shareds of the resulting
2119 /// structure kmp_task_t returned by the previous call (if any).
2120 /// 3. Copy a pointer to destructions function to field destructions of the
2121 /// resulting structure kmp_task_t.
2122 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
2123 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
2124 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
2125 /// is a resulting structure from
2126 /// previous items.
2127 /// \param D Current task directive.
2128 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2129 /// /*part_id*/, captured_struct */*__context*/);
2130 /// \param SharedsTy A type which contains references the shared variables.
2131 /// \param Shareds Context with the list of shared variables from the \p
2132 /// TaskFunction.
2133 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2134 /// otherwise.
2135 /// \param Data Additional data for task generation like tiednsee, final
2136 /// state, list of privates etc.
2137 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002138 const OMPLoopDirective &D, llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002139 QualType SharedsTy, Address Shareds, const Expr *IfCond,
2140 const OMPTaskDataTy &Data) override;
2141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002142 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002143 /// reduction:
2144 /// \code
2145 ///
2146 /// static kmp_critical_name lock = { 0 };
2147 ///
2148 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
2149 /// ...
2150 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
2151 /// ...
2152 /// }
2153 ///
2154 /// ...
2155 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
2156 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
2157 /// RedList, reduce_func, &<lock>)) {
2158 /// case 1:
2159 /// ...
2160 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
2161 /// ...
2162 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
2163 /// break;
2164 /// case 2:
2165 /// ...
2166 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
2167 /// ...
2168 /// break;
2169 /// default:;
2170 /// }
2171 /// \endcode
2172 ///
2173 /// \param Privates List of private copies for original reduction arguments.
2174 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
2175 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
2176 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
2177 /// or 'operator binop(LHS, RHS)'.
2178 /// \param Options List of options for reduction codegen:
2179 /// WithNowait true if parent directive has also nowait clause, false
2180 /// otherwise.
2181 /// SimpleReduction Emit reduction operation only. Used for omp simd
2182 /// directive on the host.
2183 /// ReductionKind The kind of reduction to perform.
2184 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2185 ArrayRef<const Expr *> Privates,
2186 ArrayRef<const Expr *> LHSExprs,
2187 ArrayRef<const Expr *> RHSExprs,
2188 ArrayRef<const Expr *> ReductionOps,
2189 ReductionOptionsTy Options) override;
2190
2191 /// Emit a code for initialization of task reduction clause. Next code
2192 /// should be emitted for reduction:
2193 /// \code
2194 ///
2195 /// _task_red_item_t red_data[n];
2196 /// ...
2197 /// red_data[i].shar = &origs[i];
2198 /// red_data[i].size = sizeof(origs[i]);
2199 /// red_data[i].f_init = (void*)RedInit<i>;
2200 /// red_data[i].f_fini = (void*)RedDest<i>;
2201 /// red_data[i].f_comb = (void*)RedOp<i>;
2202 /// red_data[i].flags = <Flag_i>;
2203 /// ...
2204 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2205 /// \endcode
2206 ///
2207 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2208 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2209 /// \param Data Additional data for task generation like tiedness, final
2210 /// state, list of privates, reductions etc.
2211 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2212 ArrayRef<const Expr *> LHSExprs,
2213 ArrayRef<const Expr *> RHSExprs,
2214 const OMPTaskDataTy &Data) override;
2215
2216 /// Required to resolve existing problems in the runtime. Emits threadprivate
2217 /// variables to store the size of the VLAs/array sections for
2218 /// initializer/combiner/finalizer functions + emits threadprivate variable to
2219 /// store the pointer to the original reduction item for the custom
2220 /// initializer defined by declare reduction construct.
2221 /// \param RCG Allows to reuse an existing data for the reductions.
2222 /// \param N Reduction item for which fixups must be emitted.
2223 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2224 ReductionCodeGen &RCG, unsigned N) override;
2225
2226 /// Get the address of `void *` type of the privatue copy of the reduction
2227 /// item specified by the \p SharedLVal.
2228 /// \param ReductionsPtr Pointer to the reduction data returned by the
2229 /// emitTaskReductionInit function.
2230 /// \param SharedLVal Address of the original reduction item.
2231 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2232 llvm::Value *ReductionsPtr,
2233 LValue SharedLVal) override;
2234
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002235 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002236 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2237
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002238 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002239 /// \param CancelRegion Region kind for which the cancellation point must be
2240 /// emitted.
2241 ///
2242 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2243 OpenMPDirectiveKind CancelRegion) override;
2244
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002245 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002246 /// \param IfCond Condition in the associated 'if' clause, if it was
2247 /// specified, nullptr otherwise.
2248 /// \param CancelRegion Region kind for which the cancel must be emitted.
2249 ///
2250 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2251 const Expr *IfCond,
2252 OpenMPDirectiveKind CancelRegion) override;
2253
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002254 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002255 /// \param D Directive to emit.
2256 /// \param ParentName Name of the function that encloses the target region.
2257 /// \param OutlinedFn Outlined function value to be defined by this call.
2258 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2259 /// \param IsOffloadEntry True if the outlined function is an offload entry.
2260 /// \param CodeGen Code generation sequence for the \a D directive.
2261 /// An outlined function may not be an entry if, e.g. the if clause always
2262 /// evaluates to false.
2263 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
2264 StringRef ParentName,
2265 llvm::Function *&OutlinedFn,
2266 llvm::Constant *&OutlinedFnID,
2267 bool IsOffloadEntry,
2268 const RegionCodeGenTy &CodeGen) override;
2269
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002270 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002271 /// code attempts offloading the execution to the device, an the event of
2272 /// a failure it executes the host version outlined in \a OutlinedFn.
2273 /// \param D Directive to emit.
2274 /// \param OutlinedFn Host version of the code to be offloaded.
2275 /// \param OutlinedFnID ID of host version of the code to be offloaded.
2276 /// \param IfCond Expression evaluated in if clause associated with the target
2277 /// directive, or null if no if clause is used.
2278 /// \param Device Expression evaluated in device clause associated with the
Alexey Bataevf3c857f2020-03-18 17:52:41 -04002279 /// target directive, or null if no device clause is used and device modifier.
2280 void emitTargetCall(
2281 CodeGenFunction &CGF, const OMPExecutableDirective &D,
2282 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond,
2283 llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
2284 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2285 const OMPLoopDirective &D)>
2286 SizeEmitter) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002287
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002288 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002289 /// the function itself in case it is a valid device function. Returns true if
2290 /// \a GD was dealt with successfully.
2291 /// \param GD Function to scan.
2292 bool emitTargetFunctions(GlobalDecl GD) override;
2293
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002294 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002295 /// Returns true if \a GD was dealt with successfully.
2296 /// \param GD Variable declaration to emit.
2297 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2298
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002299 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002300 /// if it was emitted successfully.
2301 /// \param GD Global to scan.
2302 bool emitTargetGlobal(GlobalDecl GD) override;
2303
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002304 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002305 /// variables captured in a record which address is stored in \a
2306 /// CapturedStruct.
2307 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2308 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2309 /// \param CapturedVars A pointer to the record with the references to
2310 /// variables used in \a OutlinedFn function.
2311 ///
2312 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002313 SourceLocation Loc, llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002314 ArrayRef<llvm::Value *> CapturedVars) override;
2315
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002316 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002317 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2318 /// for num_teams clause.
2319 /// \param NumTeams An integer expression of teams.
2320 /// \param ThreadLimit An integer expression of threads.
2321 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2322 const Expr *ThreadLimit, SourceLocation Loc) override;
2323
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002324 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002325 /// \param D Directive to emit.
2326 /// \param IfCond Expression evaluated in if clause associated with the
2327 /// target directive, or null if no device clause is used.
2328 /// \param Device Expression evaluated in device clause associated with the
2329 /// target directive, or null if no device clause is used.
2330 /// \param Info A record used to store information that needs to be preserved
2331 /// until the region is closed.
2332 void emitTargetDataCalls(CodeGenFunction &CGF,
2333 const OMPExecutableDirective &D, const Expr *IfCond,
2334 const Expr *Device, const RegionCodeGenTy &CodeGen,
2335 TargetDataInfo &Info) override;
2336
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002337 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002338 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2339 /// \param D Directive to emit.
2340 /// \param IfCond Expression evaluated in if clause associated with the target
2341 /// directive, or null if no if clause is used.
2342 /// \param Device Expression evaluated in device clause associated with the
2343 /// target directive, or null if no device clause is used.
2344 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2345 const OMPExecutableDirective &D,
2346 const Expr *IfCond,
2347 const Expr *Device) override;
2348
2349 /// Emit initialization for doacross loop nesting support.
2350 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002351 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2352 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002353
2354 /// Emit code for doacross ordered directive with 'depend' clause.
2355 /// \param C 'depend' clause with 'sink|source' dependency kind.
2356 void emitDoacrossOrdered(CodeGenFunction &CGF,
2357 const OMPDependClause *C) override;
2358
2359 /// Translates the native parameter of outlined function if this is required
2360 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002361 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002362 /// \param NativeParam Parameter itself.
2363 const VarDecl *translateParameter(const FieldDecl *FD,
2364 const VarDecl *NativeParam) const override;
2365
2366 /// Gets the address of the native argument basing on the address of the
2367 /// target-specific parameter.
2368 /// \param NativeParam Parameter itself.
2369 /// \param TargetParam Corresponding target-specific parameter.
2370 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2371 const VarDecl *TargetParam) const override;
Alexey Bataev4f680db2019-03-19 16:41:16 +00002372
2373 /// Gets the OpenMP-specific address of the local variable.
2374 Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2375 const VarDecl *VD) override {
2376 return Address::invalid();
2377 }
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002378};
2379
Alexey Bataev23b69422014-06-18 07:08:49 +00002380} // namespace CodeGen
2381} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002382
2383#endif