blob: c40308ee74971de37c7e1e208c83966e6d501944 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataev9959db52014-05-06 10:08:46 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This provides a class for OpenMP runtime code generation.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
14#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
Alexey Bataev9959db52014-05-06 10:08:46 +000015
Alexey Bataev7292c292016-04-25 12:22:29 +000016#include "CGValue.h"
Richard Trieuf8b8b392019-01-11 01:32:35 +000017#include "clang/AST/DeclOpenMP.h"
Jordan Rupprecht52690912019-10-01 22:30:10 +000018#include "clang/AST/GlobalDecl.h"
Alexey Bataev62b63b12015-03-10 07:28:44 +000019#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000020#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000021#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "llvm/ADT/DenseMap.h"
Alexey Bataev45588422020-01-07 14:11:45 -050023#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000024#include "llvm/ADT/StringMap.h"
Alexey Bataev2a6f3f52018-11-07 19:11:14 +000025#include "llvm/ADT/StringSet.h"
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060026#include "llvm/Frontend/OpenMP/OMPConstants.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000027#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000028#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000029
30namespace llvm {
31class ArrayType;
32class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000033class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000034class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000035class StructType;
36class Type;
37class Value;
38} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000039
Alexey Bataev9959db52014-05-06 10:08:46 +000040namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000041class Expr;
Alexey Bataev8b427062016-05-25 12:36:08 +000042class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000043class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000044class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000045class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000046class OMPDeclareReductionDecl;
47class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000048
Alexey Bataev9959db52014-05-06 10:08:46 +000049namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000050class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000051class CodeGenFunction;
52class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000053
Alexey Bataev14fa1c62016-03-29 05:34:15 +000054/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
55/// region.
56class PrePostActionTy {
57public:
58 explicit PrePostActionTy() {}
59 virtual void Enter(CodeGenFunction &CGF) {}
60 virtual void Exit(CodeGenFunction &CGF) {}
61 virtual ~PrePostActionTy() {}
62};
63
64/// Class provides a way to call simple version of codegen for OpenMP region, or
65/// an advanced with possible pre|post-actions in codegen.
66class RegionCodeGenTy final {
67 intptr_t CodeGen;
68 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
69 CodeGenTy Callback;
70 mutable PrePostActionTy *PrePostAction;
71 RegionCodeGenTy() = delete;
72 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
73 template <typename Callable>
74 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
75 PrePostActionTy &Action) {
76 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
77 }
78
79public:
80 template <typename Callable>
81 RegionCodeGenTy(
82 Callable &&CodeGen,
83 typename std::enable_if<
84 !std::is_same<typename std::remove_reference<Callable>::type,
85 RegionCodeGenTy>::value>::type * = nullptr)
86 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
87 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
88 PrePostAction(nullptr) {}
89 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
90 void operator()(CodeGenFunction &CGF) const;
91};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000092
Alexey Bataev24b5bae2016-04-28 09:23:51 +000093struct OMPTaskDataTy final {
94 SmallVector<const Expr *, 4> PrivateVars;
95 SmallVector<const Expr *, 4> PrivateCopies;
96 SmallVector<const Expr *, 4> FirstprivateVars;
97 SmallVector<const Expr *, 4> FirstprivateCopies;
98 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000099 SmallVector<const Expr *, 4> LastprivateVars;
100 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000101 SmallVector<const Expr *, 4> ReductionVars;
102 SmallVector<const Expr *, 4> ReductionCopies;
103 SmallVector<const Expr *, 4> ReductionOps;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000104 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
105 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
106 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000107 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000108 llvm::Value *Reductions = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000109 unsigned NumberOfParts = 0;
110 bool Tied = true;
111 bool Nogroup = false;
112};
113
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000114/// Class intended to support codegen of all kind of the reduction clauses.
115class ReductionCodeGen {
116private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000117 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000118 struct ReductionData {
119 /// Reference to the original shared item.
120 const Expr *Ref = nullptr;
121 /// Helper expression for generation of private copy.
122 const Expr *Private = nullptr;
123 /// Helper expression for generation reduction operation.
124 const Expr *ReductionOp = nullptr;
125 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
126 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
127 };
128 /// List of reduction-based clauses.
129 SmallVector<ReductionData, 4> ClausesData;
130
131 /// List of addresses of original shared variables/expressions.
132 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
133 /// Sizes of the reduction items in chars.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000134 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000135 /// Base declarations for the reduction items.
136 SmallVector<const VarDecl *, 4> BaseDecls;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000137
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000138 /// Emits lvalue for shared expression.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000139 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
140 /// Emits upper bound for shared expression (if array section).
141 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
142 /// Performs aggregate initialization.
143 /// \param N Number of reduction item in the common list.
144 /// \param PrivateAddr Address of the corresponding private item.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000145 /// \param SharedLVal Address of the original shared variable.
146 /// \param DRD Declare reduction construct used for reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000147 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000148 Address PrivateAddr, LValue SharedLVal,
149 const OMPDeclareReductionDecl *DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000150
151public:
152 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
153 ArrayRef<const Expr *> Privates,
154 ArrayRef<const Expr *> ReductionOps);
155 /// Emits lvalue for a reduction item.
156 /// \param N Number of the reduction item.
157 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
158 /// Emits the code for the variable-modified type, if required.
159 /// \param N Number of the reduction item.
160 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
161 /// Emits the code for the variable-modified type, if required.
162 /// \param N Number of the reduction item.
163 /// \param Size Size of the type in chars.
164 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
165 /// Performs initialization of the private copy for the reduction item.
166 /// \param N Number of the reduction item.
167 /// \param PrivateAddr Address of the corresponding private item.
168 /// \param DefaultInit Default initialization sequence that should be
169 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000170 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000171 void
172 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
173 LValue SharedLVal,
174 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000175 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000176 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000177 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000178 /// \param N Number of the reduction item.
179 /// \param PrivateAddr Address of the corresponding private item.
180 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000181 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000182 /// address in normal operations.
183 /// \param N Number of the reduction item.
184 /// \param PrivateAddr Address of the corresponding private item.
185 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
186 Address PrivateAddr);
187 /// Returns LValue for the reduction item.
188 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000189 /// Returns the size of the reduction item (in chars and total number of
190 /// elements in the item), or nullptr, if the size is a constant.
191 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
192 return Sizes[N];
193 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000194 /// Returns the base declaration of the reduction item.
195 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
Alexey Bataev1c44e152018-03-06 18:59:43 +0000196 /// Returns the base declaration of the reduction item.
197 const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000198 /// Returns true if the initialization of the reduction item uses initializer
199 /// from declare reduction construct.
200 bool usesReductionInitializer(unsigned N) const;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000201};
202
Alexey Bataev9959db52014-05-06 10:08:46 +0000203class CGOpenMPRuntime {
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000204public:
205 /// Allows to disable automatic handling of functions used in target regions
206 /// as those marked as `omp declare target`.
207 class DisableAutoDeclareTargetRAII {
208 CodeGenModule &CGM;
209 bool SavedShouldMarkAsGlobal;
210
211 public:
212 DisableAutoDeclareTargetRAII(CodeGenModule &CGM);
213 ~DisableAutoDeclareTargetRAII();
214 };
215
Alexey Bataev0860db92019-12-19 10:01:10 -0500216 /// Manages list of nontemporal decls for the specified directive.
217 class NontemporalDeclsRAII {
218 CodeGenModule &CGM;
219 const bool NeedToPush;
220
221 public:
222 NontemporalDeclsRAII(CodeGenModule &CGM, const OMPLoopDirective &S);
223 ~NontemporalDeclsRAII();
224 };
225
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500226 /// Maps the expression for the lastprivate variable to the global copy used
227 /// to store new value because original variables are not mapped in inner
228 /// parallel regions. Only private copies are captured but we need also to
229 /// store private copy in shared address.
230 /// Also, stores the expression for the private loop counter and it
231 /// threaprivate name.
232 struct LastprivateConditionalData {
233 llvm::SmallDenseMap<CanonicalDeclPtr<const Decl>, SmallString<16>>
234 DeclToUniqeName;
235 LValue IVLVal;
236 SmallString<16> IVName;
237 /// True if original lvalue for loop counter can be used in codegen (simd
238 /// region or simd only mode) and no need to create threadprivate
239 /// references.
240 bool UseOriginalIV = false;
241 };
242 /// Manages list of lastprivate conditional decls for the specified directive.
243 class LastprivateConditionalRAII {
244 CodeGenModule &CGM;
245 const bool NeedToPush;
246
247 public:
248 LastprivateConditionalRAII(CodeGenFunction &CGF,
249 const OMPExecutableDirective &S, LValue IVLVal);
250 ~LastprivateConditionalRAII();
251 };
252
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000253protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000254 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000255 StringRef FirstSeparator, Separator;
256
257 /// Constructor allowing to redefine the name separator for the variables.
258 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
259 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000260
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000261 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000262 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000263 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000264 uint64_t Size, int32_t Flags,
265 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000266
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000267 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000268 /// \param D Directive to emit.
269 /// \param ParentName Name of the function that encloses the target region.
270 /// \param OutlinedFn Outlined function value to be defined by this call.
271 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
272 /// \param IsOffloadEntry True if the outlined function is an offload entry.
273 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000274 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000275 /// evaluates to false.
276 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
277 StringRef ParentName,
278 llvm::Function *&OutlinedFn,
279 llvm::Constant *&OutlinedFnID,
280 bool IsOffloadEntry,
281 const RegionCodeGenTy &CodeGen);
282
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000283 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000284 /// \param Flags Flags for OpenMP location.
285 ///
286 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
287 unsigned Flags = 0);
288
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000289 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000290 llvm::Type *getIdentTyPointerTy();
291
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000292 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000293 ///
294 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
295
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000296 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000297 // The name can be customized depending on the target.
298 //
299 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
300
Alexey Bataev3c595a62017-08-14 15:01:03 +0000301 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
James Y Knight9871db02019-02-05 16:42:33 +0000302 void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
303 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000304 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000305
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000306 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000307 /// stored.
308 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
309
Alexey Bataevfd006c42018-10-05 15:08:53 +0000310 void setLocThreadIdInsertPt(CodeGenFunction &CGF,
311 bool AtCurrentPoint = false);
312 void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
313
Alexey Bataevceeaa482018-11-21 21:04:34 +0000314 /// Check if the default location must be constant.
315 /// Default is false to support OMPT/OMPD.
316 virtual bool isDefaultLocationConstant() const { return false; }
317
318 /// Returns additional flags that can be stored in reserved_2 field of the
319 /// default location.
320 virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
321
Alexey Bataevc2cd2d42019-10-10 17:28:10 +0000322 /// Tries to emit declare variant function for \p OldGD from \p NewGD.
323 /// \param OrigAddr LLVM IR value for \p OldGD.
324 /// \param IsForDefinition true, if requested emission for the definition of
325 /// \p OldGD.
326 /// \returns true, was able to emit a definition function for \p OldGD, which
327 /// points to \p NewGD.
328 virtual bool tryEmitDeclareVariant(const GlobalDecl &NewGD,
329 const GlobalDecl &OldGD,
330 llvm::GlobalValue *OrigAddr,
331 bool IsForDefinition);
332
Alexey Bataevc3028ca2018-12-04 15:03:25 +0000333 /// Returns default flags for the barriers depending on the directive, for
334 /// which this barier is going to be emitted.
335 static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
336
Alexey Bataeva1166022018-11-27 21:24:54 +0000337 /// Get the LLVM type for the critical name.
338 llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
339
340 /// Returns corresponding lock object for the specified critical region
341 /// name. If the lock object does not exist it is created, otherwise the
342 /// reference to the existing copy is returned.
343 /// \param CriticalName Name of the critical region.
344 ///
345 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
346
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000347private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000348 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000349 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000350 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataevceeaa482018-11-21 21:04:34 +0000351 using FlagsTy = std::pair<unsigned, unsigned>;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000352 /// Map of flags and corresponding default locations.
Alexey Bataevceeaa482018-11-21 21:04:34 +0000353 using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
Alexey Bataev15007ba2014-05-07 06:18:01 +0000354 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000355 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000356
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000357 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000358 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000359 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000360 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
361 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000362 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000363 /// Original representation is:
364 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000365 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000366 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000367 struct DebugLocThreadIdTy {
368 llvm::Value *DebugLoc;
369 llvm::Value *ThreadID;
Alexey Bataevfd006c42018-10-05 15:08:53 +0000370 /// Insert point for the service instructions.
371 llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000372 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000373 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000374 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
375 OpenMPLocThreadIDMapTy;
376 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000377 /// Map of UDRs and corresponding combiner/initializer.
378 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
379 std::pair<llvm::Function *, llvm::Function *>>
380 UDRMapTy;
381 UDRMapTy UDRMap;
382 /// Map of functions and locally defined UDRs.
383 typedef llvm::DenseMap<llvm::Function *,
384 SmallVector<const OMPDeclareReductionDecl *, 4>>
385 FunctionUDRMapTy;
386 FunctionUDRMapTy FunctionUDRMap;
Michael Krused47b9432019-08-05 18:43:21 +0000387 /// Map from the user-defined mapper declaration to its corresponding
388 /// functions.
389 llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
390 /// Map of functions and their local user-defined mappers.
391 using FunctionUDMMapTy =
392 llvm::DenseMap<llvm::Function *,
393 SmallVector<const OMPDeclareMapperDecl *, 4>>;
394 FunctionUDMMapTy FunctionUDMMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000395 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000396 /// kmp_critical_name[8];
397 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000398 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000399 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
400 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
401 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
402 /// variables.
403 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
404 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000405 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000406 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000407 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000408 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000409 /// void * shareds; /**< pointer to block of pointers to
410 /// shared vars */
411 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
412 /// executing task */
413 /// kmp_int32 part_id; /**< part id for the task */
414 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
415 /// deconstructors of firstprivate C++ objects */
416 /// } kmp_task_t;
417 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000418 /// Saved kmp_task_t for task directive.
419 QualType SavedKmpTaskTQTy;
420 /// Saved kmp_task_t for taskloop-based directive.
421 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000422 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000423 /// kmp_intptr_t base_addr;
424 /// size_t len;
425 /// struct {
426 /// bool in:1;
427 /// bool out:1;
428 /// } flags;
429 /// } kmp_depend_info_t;
430 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000431 /// struct kmp_dim { // loop bounds info casted to kmp_int64
432 /// kmp_int64 lo; // lower
433 /// kmp_int64 up; // upper
434 /// kmp_int64 st; // stride
435 /// };
436 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000437 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000438 /// void *addr; // Pointer to the offload entry info.
439 /// // (function or global)
440 /// char *name; // Name of the function or global.
441 /// size_t size; // Size of the entry info (0 if it a function).
442 /// };
443 QualType TgtOffloadEntryQTy;
444 /// struct __tgt_device_image{
445 /// void *ImageStart; // Pointer to the target code start.
446 /// void *ImageEnd; // Pointer to the target code end.
447 /// // We also add the host entries to the device image, as it may be useful
448 /// // for the target runtime to have access to that information.
449 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
450 /// // the entries.
451 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
452 /// // entries (non inclusive).
453 /// };
454 QualType TgtDeviceImageQTy;
455 /// struct __tgt_bin_desc{
456 /// int32_t NumDevices; // Number of devices supported.
457 /// __tgt_device_image *DeviceImages; // Arrays of device images
458 /// // (one per device).
459 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
460 /// // entries.
461 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
462 /// // entries (non inclusive).
463 /// };
464 QualType TgtBinaryDescriptorQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000465 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000466 /// far.
467 class OffloadEntriesInfoManagerTy {
468 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000470 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000471 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000472
473 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000474 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000475 class OffloadEntryInfo {
476 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000477 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000478 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000479 /// Entry is a target region.
480 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000481 /// Entry is a declare target variable.
482 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000483 /// Invalid entry info.
484 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000485 };
486
Alexey Bataev03f270c2018-03-30 18:31:07 +0000487 protected:
488 OffloadEntryInfo() = delete;
489 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000490 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000491 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000492 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000493 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000494
Alexey Bataev03f270c2018-03-30 18:31:07 +0000495 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000496 bool isValid() const { return Order != ~0u; }
497 unsigned getOrder() const { return Order; }
498 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000499 uint32_t getFlags() const { return Flags; }
500 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
501 llvm::Constant *getAddress() const {
502 return cast_or_null<llvm::Constant>(Addr);
503 }
504 void setAddress(llvm::Constant *V) {
505 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
506 Addr = V;
507 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000508 static bool classof(const OffloadEntryInfo *Info) { return true; }
509
Samuel Antaof83efdb2017-01-05 16:02:49 +0000510 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000511 /// Address of the entity that has to be mapped for offloading.
512 llvm::WeakTrackingVH Addr;
513
Samuel Antaof83efdb2017-01-05 16:02:49 +0000514 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000515 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000516
517 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000518 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000519
Alexey Bataev03f270c2018-03-30 18:31:07 +0000520 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000521 };
522
Alexey Bataev03f270c2018-03-30 18:31:07 +0000523 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000524 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000525 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000526 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000527 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000528
Alexey Bataev03f270c2018-03-30 18:31:07 +0000529 //
530 // Target region entries related.
531 //
532
533 /// Kind of the target registry entry.
534 enum OMPTargetRegionEntryKind : uint32_t {
535 /// Mark the entry as target region.
536 OMPTargetRegionEntryTargetRegion = 0x0,
537 /// Mark the entry as a global constructor.
538 OMPTargetRegionEntryCtor = 0x02,
539 /// Mark the entry as a global destructor.
540 OMPTargetRegionEntryDtor = 0x04,
541 };
542
543 /// Target region entries info.
544 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
545 /// Address that can be used as the ID of the entry.
546 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000547
548 public:
549 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000550 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000551 explicit OffloadEntryInfoTargetRegion(unsigned Order,
552 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000553 llvm::Constant *ID,
554 OMPTargetRegionEntryKind Flags)
555 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000556 ID(ID) {
557 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000558 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000559
560 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000561 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000562 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000563 ID = V;
564 }
565 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000566 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000567 }
568 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000569
570 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000571 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
572 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000573 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000574 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000575 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
576 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000577 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000578 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000579 /// Return true if a target region entry with the provided information
580 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000581 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000582 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000583 /// brief Applies action \a Action on all registered entries.
584 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000585 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000586 OffloadTargetRegionEntryInfoActTy;
587 void actOnTargetRegionEntriesInfo(
588 const OffloadTargetRegionEntryInfoActTy &Action);
589
Alexey Bataev03f270c2018-03-30 18:31:07 +0000590 //
591 // Device global variable entries related.
592 //
593
594 /// Kind of the global variable entry..
595 enum OMPTargetGlobalVarEntryKind : uint32_t {
596 /// Mark the entry as a to declare target.
597 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000598 /// Mark the entry as a to declare target link.
599 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000600 };
601
602 /// Device global variable entries info.
603 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
604 /// Type of the global variable.
605 CharUnits VarSize;
606 llvm::GlobalValue::LinkageTypes Linkage;
607
608 public:
609 OffloadEntryInfoDeviceGlobalVar()
610 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
611 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
612 OMPTargetGlobalVarEntryKind Flags)
613 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
614 explicit OffloadEntryInfoDeviceGlobalVar(
615 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
616 OMPTargetGlobalVarEntryKind Flags,
617 llvm::GlobalValue::LinkageTypes Linkage)
618 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
619 VarSize(VarSize), Linkage(Linkage) {
620 setAddress(Addr);
621 }
622
623 CharUnits getVarSize() const { return VarSize; }
624 void setVarSize(CharUnits Size) { VarSize = Size; }
625 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
626 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
627 static bool classof(const OffloadEntryInfo *Info) {
628 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
629 }
630 };
631
632 /// Initialize device global variable entry.
633 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
634 OMPTargetGlobalVarEntryKind Flags,
635 unsigned Order);
636
637 /// Register device global variable entry.
638 void
639 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
640 CharUnits VarSize,
641 OMPTargetGlobalVarEntryKind Flags,
642 llvm::GlobalValue::LinkageTypes Linkage);
643 /// Checks if the variable with the given name has been registered already.
644 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
645 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
646 }
647 /// Applies action \a Action on all registered entries.
648 typedef llvm::function_ref<void(StringRef,
649 const OffloadEntryInfoDeviceGlobalVar &)>
650 OffloadDeviceGlobalVarEntryInfoActTy;
651 void actOnDeviceGlobalVarEntriesInfo(
652 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
653
Samuel Antaoee8fb302016-01-06 13:42:12 +0000654 private:
655 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000656 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000657 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000658 OffloadEntriesTargetRegionPerLine;
659 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
660 OffloadEntriesTargetRegionPerParentName;
661 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
662 OffloadEntriesTargetRegionPerFile;
663 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
664 OffloadEntriesTargetRegionPerDevice;
665 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
666 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000667 /// Storage for device global variable entries kind. The storage is to be
668 /// indexed by mangled name.
669 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
670 OffloadEntriesDeviceGlobalVarTy;
671 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000672 };
673 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
674
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000675 bool ShouldMarkAsGlobal = true;
Alexey Bataev45588422020-01-07 14:11:45 -0500676 /// List of the emitted declarations.
677 llvm::DenseSet<CanonicalDeclPtr<const Decl>> AlreadyEmittedTargetDecls;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000678 /// List of the global variables with their addresses that should not be
679 /// emitted for the target.
680 llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000681
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000682 /// List of variables that can become declare target implicitly and, thus,
683 /// must be emitted.
684 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
685
Alexey Bataev2df5f122019-10-01 20:18:32 +0000686 /// Mapping of the original functions to their variants and original global
687 /// decl.
688 llvm::MapVector<CanonicalDeclPtr<const FunctionDecl>,
689 std::pair<GlobalDecl, GlobalDecl>>
690 DeferredVariantFunction;
691
Alexey Bataev0860db92019-12-19 10:01:10 -0500692 using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
693 /// Stack for list of declarations in current context marked as nontemporal.
694 /// The set is the union of all current stack elements.
695 llvm::SmallVector<NontemporalDeclsSet, 4> NontemporalDeclsStack;
696
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500697 /// Stack for list of addresses of declarations in current context marked as
698 /// lastprivate conditional. The set is the union of all current stack
699 /// elements.
700 llvm::SmallVector<LastprivateConditionalData, 4> LastprivateConditionalStack;
701
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000702 /// Flag for keeping track of weather a requires unified_shared_memory
703 /// directive is present.
704 bool HasRequiresUnifiedSharedMemory = false;
705
706 /// Flag for keeping track of weather a target region has been emitted.
707 bool HasEmittedTargetRegion = false;
708
709 /// Flag for keeping track of weather a device routine has been emitted.
710 /// Device routines are specific to the
711 bool HasEmittedDeclareTargetRegion = false;
712
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000713 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000714 /// metadata.
715 void loadOffloadInfoMetadata();
716
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000717 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000718 QualType getTgtOffloadEntryQTy();
719
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000720 /// Returns __tgt_device_image type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000721 QualType getTgtDeviceImageQTy();
722
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000723 /// Returns __tgt_bin_desc type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000724 QualType getTgtBinaryDescriptorQTy();
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 Bataev4a5bb772014-10-08 14:01:46 +0000854public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000855 explicit CGOpenMPRuntime(CodeGenModule &CGM)
856 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000857 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000858 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000859
Alexey Bataevd08c0562019-11-19 12:07:54 -0500860 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
861 /// function. Here is the logic:
862 /// if (Cond) {
863 /// ThenGen();
864 /// } else {
865 /// ElseGen();
866 /// }
867 void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
868 const RegionCodeGenTy &ThenGen,
869 const RegionCodeGenTy &ElseGen);
870
Alexey Bataev5c427362019-04-10 19:11:33 +0000871 /// Checks if the \p Body is the \a CompoundStmt and returns its child
872 /// statement iff there is only one that is not evaluatable at the compile
873 /// time.
874 static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
875
Alexey Bataev18fa2322018-05-02 14:20:50 +0000876 /// Get the platform-specific name separator.
877 std::string getName(ArrayRef<StringRef> Parts) const;
878
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000879 /// Emit code for the specified user defined reduction construct.
880 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
881 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000882 /// Get combiner/initializer for the specified user-defined reduction, if any.
883 virtual std::pair<llvm::Function *, llvm::Function *>
884 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000885
Michael Krused47b9432019-08-05 18:43:21 +0000886 /// Emit the function for the user defined mapper construct.
887 void emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
888 CodeGenFunction *CGF = nullptr);
889
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000890 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000891 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
892 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000893 /// \param D OpenMP directive.
894 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000895 /// \param InnermostKind Kind of innermost directive (for simple directives it
896 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000897 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000898 virtual llvm::Function *emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000899 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
900 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
901
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000902 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000903 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
904 /// kmp_int32 BoundID, struct context_vars*).
905 /// \param D OpenMP directive.
906 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
907 /// \param InnermostKind Kind of innermost directive (for simple directives it
908 /// is a directive itself, for combined - its innermost directive).
909 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000910 virtual llvm::Function *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000911 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
912 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000913
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000914 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000915 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
916 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000917 /// \param D OpenMP directive.
918 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000919 /// \param PartIDVar Variable for partition id in the current OpenMP untied
920 /// task region.
921 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000922 /// \param InnermostKind Kind of innermost directive (for simple directives it
923 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000924 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000925 /// \param Tied true if task is generated for tied task, false otherwise.
926 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
927 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000928 ///
James Y Knight9871db02019-02-05 16:42:33 +0000929 virtual llvm::Function *emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000930 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000931 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
932 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
933 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000934
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000935 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000936 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000937 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000938
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000939 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000940 /// variables captured in a record which address is stored in \a
941 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000942 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000943 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000944 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000945 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000946 /// \param IfCond Condition in the associated 'if' clause, if it was
947 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000948 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000949 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +0000950 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000951 ArrayRef<llvm::Value *> CapturedVars,
952 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000953
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000954 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000955 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000956 /// \param CriticalOpGen Generator for the statement associated with the given
957 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000958 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000959 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000960 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000961 SourceLocation Loc,
962 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000963
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000964 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000965 /// \param MasterOpGen Generator for the statement associated with the given
966 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000967 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000968 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000969 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000970
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000971 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000972 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000973
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000974 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000975 /// \param TaskgroupOpGen Generator for the statement associated with the
976 /// given taskgroup region.
977 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
978 const RegionCodeGenTy &TaskgroupOpGen,
979 SourceLocation Loc);
980
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000981 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000982 /// \param SingleOpGen Generator for the statement associated with the given
983 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000984 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000985 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000986 SourceLocation Loc,
987 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000988 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000989 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000990 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000991
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000992 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000993 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000994 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000995 virtual void emitOrderedRegion(CodeGenFunction &CGF,
996 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000997 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000998
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000999 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +00001000 /// \param Kind Directive for which this implicit barrier call must be
1001 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001002 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1003 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1004 /// runtime class decides which one to emit (simple or with cancellation
1005 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001006 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001007 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001008 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001009 bool EmitChecks = true,
1010 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +00001011
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001012 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +00001013 /// This kind of worksharing directive is emitted without outer loop.
1014 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1015 /// \param Chunked True if chunk is specified in the clause.
1016 ///
1017 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
1018 bool Chunked) const;
1019
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001020 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001021 /// This kind of distribute directive is emitted without outer loop.
1022 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1023 /// \param Chunked True if chunk is specified in the clause.
1024 ///
1025 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
1026 bool Chunked) const;
1027
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00001028 /// Check if the specified \a ScheduleKind is static chunked.
1029 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1030 /// \param Chunked True if chunk is specified in the clause.
1031 ///
1032 virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
1033 bool Chunked) const;
1034
1035 /// Check if the specified \a ScheduleKind is static non-chunked.
1036 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1037 /// \param Chunked True if chunk is specified in the clause.
1038 ///
1039 virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
1040 bool Chunked) const;
1041
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001042 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001043 /// This kind of worksharing directive is emitted without outer loop.
1044 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
1045 ///
1046 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
1047
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001048 /// struct with the values to be passed to the dispatch runtime function
1049 struct DispatchRTInput {
1050 /// Loop lower bound
1051 llvm::Value *LB = nullptr;
1052 /// Loop upper bound
1053 llvm::Value *UB = nullptr;
1054 /// Chunk size specified using 'schedule' clause (nullptr if chunk
1055 /// was not specified)
1056 llvm::Value *Chunk = nullptr;
1057 DispatchRTInput() = default;
1058 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
1059 : LB(LB), UB(UB), Chunk(Chunk) {}
1060 };
1061
1062 /// Call the appropriate runtime routine to initialize it before start
1063 /// of loop.
1064
1065 /// This is used for non static scheduled types and when the ordered
1066 /// clause is present on the loop construct.
1067 /// Depending on the loop schedule, it is necessary to call some runtime
1068 /// routine before start of the OpenMP loop to get the loop upper / lower
1069 /// bounds \a LB and \a UB and stride \a ST.
1070 ///
1071 /// \param CGF Reference to current CodeGenFunction.
1072 /// \param Loc Clang source location.
1073 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1074 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001075 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001076 /// \param Ordered true if loop is ordered, false otherwise.
1077 /// \param DispatchValues struct containing llvm values for lower bound, upper
1078 /// bound, and chunk expression.
1079 /// For the default (nullptr) value, the chunk 1 will be used.
1080 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001081 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001082 const OpenMPScheduleTy &ScheduleKind,
1083 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001084 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001085
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001086 /// Struct with the values to be passed to the static runtime function
1087 struct StaticRTInput {
1088 /// Size of the iteration variable in bits.
1089 unsigned IVSize = 0;
1090 /// Sign of the iteration variable.
1091 bool IVSigned = false;
1092 /// true if loop is ordered, false otherwise.
1093 bool Ordered = false;
1094 /// Address of the output variable in which the flag of the last iteration
1095 /// is returned.
1096 Address IL = Address::invalid();
1097 /// Address of the output variable in which the lower iteration number is
1098 /// returned.
1099 Address LB = Address::invalid();
1100 /// Address of the output variable in which the upper iteration number is
1101 /// returned.
1102 Address UB = Address::invalid();
1103 /// Address of the output variable in which the stride value is returned
1104 /// necessary to generated the static_chunked scheduled loop.
1105 Address ST = Address::invalid();
1106 /// Value of the chunk for the static_chunked scheduled loop. For the
1107 /// default (nullptr) value, the chunk 1 will be used.
1108 llvm::Value *Chunk = nullptr;
1109 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1110 Address LB, Address UB, Address ST,
1111 llvm::Value *Chunk = nullptr)
1112 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1113 UB(UB), ST(ST), Chunk(Chunk) {}
1114 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001115 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +00001116 /// of loop.
1117 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001118 /// This is used only in case of static schedule, when the user did not
1119 /// specify a ordered clause on the loop construct.
1120 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +00001121 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001122 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +00001123 ///
1124 /// \param CGF Reference to current CodeGenFunction.
1125 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001126 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001127 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001128 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +00001129 ///
John McCall7f416cc2015-09-08 08:05:57 +00001130 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001131 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001132 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001133 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00001134
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001135 ///
1136 /// \param CGF Reference to current CodeGenFunction.
1137 /// \param Loc Clang source location.
1138 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001139 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001140 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001141 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1142 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001143 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001144 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001145
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001146 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001147 /// iteration of the ordered loop with the dynamic scheduling.
1148 ///
1149 /// \param CGF Reference to current CodeGenFunction.
1150 /// \param Loc Clang source location.
1151 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001152 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001153 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001154 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1155 SourceLocation Loc, unsigned IVSize,
1156 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001157
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001158 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001159 /// all the work with current loop.
1160 ///
1161 /// \param CGF Reference to current CodeGenFunction.
1162 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001163 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001164 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001165 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1166 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001167
Alexander Musman92bdaab2015-03-12 13:37:50 +00001168 /// Call __kmpc_dispatch_next(
1169 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1170 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1171 /// kmp_int[32|64] *p_stride);
1172 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001173 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001174 /// \param IL Address of the output variable in which the flag of the
1175 /// last iteration is returned.
1176 /// \param LB Address of the output variable in which the lower iteration
1177 /// number is returned.
1178 /// \param UB Address of the output variable in which the upper iteration
1179 /// number is returned.
1180 /// \param ST Address of the output variable in which the stride value is
1181 /// returned.
1182 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1183 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001184 Address IL, Address LB,
1185 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001186
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001187 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001188 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1189 /// clause.
1190 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001191 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1192 llvm::Value *NumThreads,
1193 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001194
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001195 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001196 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1197 virtual void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001198 llvm::omp::ProcBindKind ProcBind,
Alexey Bataev7f210c62015-06-18 13:40:03 +00001199 SourceLocation Loc);
1200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001201 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001202 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001203 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001204 /// \param VDAddr Address of the global variable \a VD.
1205 /// \param Loc Location of the reference to threadprivate var.
1206 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001207 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1208 const VarDecl *VD,
1209 Address VDAddr,
1210 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001211
Alexey Bataev92327c52018-03-26 16:40:55 +00001212 /// Returns the address of the variable marked as declare target with link
Gheorghe-Teodor Bercea0034e842019-06-20 18:04:47 +00001213 /// clause OR as declare target with to clause and unified memory.
1214 virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001216 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001217 /// a call to runtime library which adds initial value to the newly created
1218 /// threadprivate variable (if it is not constant) and registers destructor
1219 /// for the variable (if any).
1220 /// \param VD Threadprivate variable.
1221 /// \param VDAddr Address of the global variable \a VD.
1222 /// \param Loc Location of threadprivate declaration.
1223 /// \param PerformInit true if initialization expression is not constant.
1224 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001225 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001226 SourceLocation Loc, bool PerformInit,
1227 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001229 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001230 /// \param VD Declare target variable.
1231 /// \param Addr Address of the global variable \a VD.
1232 /// \param PerformInit true if initialization expression is not constant.
1233 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1234 llvm::GlobalVariable *Addr,
1235 bool PerformInit);
1236
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001237 /// Creates artificial threadprivate variable with name \p Name and type \p
1238 /// VarType.
1239 /// \param VarType Type of the artificial threadprivate variable.
1240 /// \param Name Name of the artificial threadprivate variable.
1241 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1242 QualType VarType,
1243 StringRef Name);
1244
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001245 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001246 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001247 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1248 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001250 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001251 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001252 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1253 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1254 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1255 /// function:
1256 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1257 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1258 /// return 0;
1259 /// }
1260 /// 2. Copy a list of shared variables to field shareds of the resulting
1261 /// structure kmp_task_t returned by the previous call (if any).
1262 /// 3. Copy a pointer to destructions function to field destructions of the
1263 /// resulting structure kmp_task_t.
1264 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1265 /// kmp_task_t *new_task), where new_task is a resulting structure from
1266 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001267 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001268 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1269 /// /*part_id*/, captured_struct */*__context*/);
1270 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001271 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001272 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001273 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1274 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001275 /// \param Data Additional data for task generation like tiednsee, final
1276 /// state, list of privates etc.
1277 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1278 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001279 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001280 Address Shareds, const Expr *IfCond,
1281 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001282
Alexey Bataev7292c292016-04-25 12:22:29 +00001283 /// Emit task region for the taskloop directive. The taskloop region is
1284 /// emitted in several steps:
1285 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1286 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1287 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1288 /// function:
1289 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1290 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1291 /// return 0;
1292 /// }
1293 /// 2. Copy a list of shared variables to field shareds of the resulting
1294 /// structure kmp_task_t returned by the previous call (if any).
1295 /// 3. Copy a pointer to destructions function to field destructions of the
1296 /// resulting structure kmp_task_t.
1297 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1298 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1299 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1300 /// is a resulting structure from
1301 /// previous items.
1302 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001303 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1304 /// /*part_id*/, captured_struct */*__context*/);
1305 /// \param SharedsTy A type which contains references the shared variables.
1306 /// \param Shareds Context with the list of shared variables from the \p
1307 /// TaskFunction.
1308 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1309 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001310 /// \param Data Additional data for task generation like tiednsee, final
1311 /// state, list of privates etc.
James Y Knight9871db02019-02-05 16:42:33 +00001312 virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1313 const OMPLoopDirective &D,
1314 llvm::Function *TaskFunction,
1315 QualType SharedsTy, Address Shareds,
1316 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001317
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001318 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001319 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001320 /// \param InnermostKind Kind of innermost directive (for simple directives it
1321 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001322 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001323 /// \param HasCancel true if region has inner cancel directive, false
1324 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001325 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001326 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001327 const RegionCodeGenTy &CodeGen,
1328 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001329
1330 /// Emits reduction function.
1331 /// \param ArgsType Array type containing pointers to reduction variables.
1332 /// \param Privates List of private copies for original reduction arguments.
1333 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1334 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1335 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1336 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev982a35e2019-03-19 17:09:52 +00001337 llvm::Function *emitReductionFunction(SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001338 llvm::Type *ArgsType,
1339 ArrayRef<const Expr *> Privates,
1340 ArrayRef<const Expr *> LHSExprs,
1341 ArrayRef<const Expr *> RHSExprs,
1342 ArrayRef<const Expr *> ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001343
1344 /// Emits single reduction combiner
1345 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1346 const Expr *ReductionOp,
1347 const Expr *PrivateRef,
1348 const DeclRefExpr *LHS,
1349 const DeclRefExpr *RHS);
1350
1351 struct ReductionOptionsTy {
1352 bool WithNowait;
1353 bool SimpleReduction;
1354 OpenMPDirectiveKind ReductionKind;
1355 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001356 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001357 /// reduction:
1358 /// \code
1359 ///
1360 /// static kmp_critical_name lock = { 0 };
1361 ///
1362 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1363 /// ...
1364 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1365 /// ...
1366 /// }
1367 ///
1368 /// ...
1369 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1370 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1371 /// RedList, reduce_func, &<lock>)) {
1372 /// case 1:
1373 /// ...
1374 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1375 /// ...
1376 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1377 /// break;
1378 /// case 2:
1379 /// ...
1380 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1381 /// ...
1382 /// break;
1383 /// default:;
1384 /// }
1385 /// \endcode
1386 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001387 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001388 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1389 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1390 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1391 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001392 /// \param Options List of options for reduction codegen:
1393 /// WithNowait true if parent directive has also nowait clause, false
1394 /// otherwise.
1395 /// SimpleReduction Emit reduction operation only. Used for omp simd
1396 /// directive on the host.
1397 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001398 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001399 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001400 ArrayRef<const Expr *> LHSExprs,
1401 ArrayRef<const Expr *> RHSExprs,
1402 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001403 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001404
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001405 /// Emit a code for initialization of task reduction clause. Next code
1406 /// should be emitted for reduction:
1407 /// \code
1408 ///
1409 /// _task_red_item_t red_data[n];
1410 /// ...
1411 /// red_data[i].shar = &origs[i];
1412 /// red_data[i].size = sizeof(origs[i]);
1413 /// red_data[i].f_init = (void*)RedInit<i>;
1414 /// red_data[i].f_fini = (void*)RedDest<i>;
1415 /// red_data[i].f_comb = (void*)RedOp<i>;
1416 /// red_data[i].flags = <Flag_i>;
1417 /// ...
1418 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1419 /// \endcode
1420 ///
1421 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1422 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1423 /// \param Data Additional data for task generation like tiedness, final
1424 /// state, list of privates, reductions etc.
1425 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1426 SourceLocation Loc,
1427 ArrayRef<const Expr *> LHSExprs,
1428 ArrayRef<const Expr *> RHSExprs,
1429 const OMPTaskDataTy &Data);
1430
1431 /// Required to resolve existing problems in the runtime. Emits threadprivate
1432 /// variables to store the size of the VLAs/array sections for
1433 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1434 /// store the pointer to the original reduction item for the custom
1435 /// initializer defined by declare reduction construct.
1436 /// \param RCG Allows to reuse an existing data for the reductions.
1437 /// \param N Reduction item for which fixups must be emitted.
1438 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1439 ReductionCodeGen &RCG, unsigned N);
1440
1441 /// Get the address of `void *` type of the privatue copy of the reduction
1442 /// item specified by the \p SharedLVal.
1443 /// \param ReductionsPtr Pointer to the reduction data returned by the
1444 /// emitTaskReductionInit function.
1445 /// \param SharedLVal Address of the original reduction item.
1446 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1447 llvm::Value *ReductionsPtr,
1448 LValue SharedLVal);
1449
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001450 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001451 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001452
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001453 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001454 /// \param CancelRegion Region kind for which the cancellation point must be
1455 /// emitted.
1456 ///
1457 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1458 SourceLocation Loc,
1459 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001460
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001461 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001462 /// \param IfCond Condition in the associated 'if' clause, if it was
1463 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001464 /// \param CancelRegion Region kind for which the cancel must be emitted.
1465 ///
1466 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001467 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001468 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001470 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001471 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001472 /// \param ParentName Name of the function that encloses the target region.
1473 /// \param OutlinedFn Outlined function value to be defined by this call.
1474 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1475 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001476 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001477 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001478 /// evaluates to false.
1479 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1480 StringRef ParentName,
1481 llvm::Function *&OutlinedFn,
1482 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001483 bool IsOffloadEntry,
1484 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001485
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001486 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001487 /// code attempts offloading the execution to the device, an the event of
1488 /// a failure it executes the host version outlined in \a OutlinedFn.
1489 /// \param D Directive to emit.
1490 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001491 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001492 /// \param IfCond Expression evaluated in if clause associated with the target
1493 /// directive, or null if no if clause is used.
1494 /// \param Device Expression evaluated in device clause associated with the
1495 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00001496 /// \param SizeEmitter Callback to emit number of iterations for loop-based
1497 /// directives.
1498 virtual void
1499 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1500 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
1501 const Expr *IfCond, const Expr *Device,
1502 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
1503 const OMPLoopDirective &D)>
1504 SizeEmitter);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001505
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001506 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001507 /// the function itself in case it is a valid device function. Returns true if
1508 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001509 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001510 virtual bool emitTargetFunctions(GlobalDecl GD);
1511
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001512 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001513 /// Returns true if \a GD was dealt with successfully.
1514 /// \param GD Variable declaration to emit.
1515 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1516
Alexey Bataev03f270c2018-03-30 18:31:07 +00001517 /// Checks if the provided global decl \a GD is a declare target variable and
1518 /// registers it when emitting code for the host.
1519 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1520 llvm::Constant *Addr);
1521
Alexey Bataev1af5bd52019-03-05 17:47:18 +00001522 /// Registers provided target firstprivate variable as global on the
1523 /// target.
1524 llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1525 const VarDecl *VD);
1526
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001527 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001528 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001529 /// \param GD Global to scan.
1530 virtual bool emitTargetGlobal(GlobalDecl GD);
1531
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001532 /// Creates and returns a registration function for when at least one
1533 /// requires directives was used in the current module.
1534 llvm::Function *emitRequiresDirectiveRegFun();
1535
Sergey Dmitriev5836c352019-10-15 18:42:47 +00001536 /// Creates all the offload entries in the current compilation unit
1537 /// along with the associated metadata.
1538 void createOffloadEntriesAndInfoMetadata();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001539
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001540 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001541 /// variables captured in a record which address is stored in \a
1542 /// CapturedStruct.
1543 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1544 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1545 /// \param CapturedVars A pointer to the record with the references to
1546 /// variables used in \a OutlinedFn function.
1547 ///
1548 virtual void emitTeamsCall(CodeGenFunction &CGF,
1549 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001550 SourceLocation Loc, llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001551 ArrayRef<llvm::Value *> CapturedVars);
1552
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001553 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001554 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1555 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001556 /// \param NumTeams An integer expression of teams.
1557 /// \param ThreadLimit An integer expression of threads.
1558 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1559 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001560
Samuel Antaocc10b852016-07-28 14:23:26 +00001561 /// Struct that keeps all the relevant information that should be kept
1562 /// throughout a 'target data' region.
1563 class TargetDataInfo {
1564 /// Set to true if device pointer information have to be obtained.
1565 bool RequiresDevicePointerInfo = false;
1566
1567 public:
1568 /// The array of base pointer passed to the runtime library.
1569 llvm::Value *BasePointersArray = nullptr;
1570 /// The array of section pointers passed to the runtime library.
1571 llvm::Value *PointersArray = nullptr;
1572 /// The array of sizes passed to the runtime library.
1573 llvm::Value *SizesArray = nullptr;
1574 /// The array of map types passed to the runtime library.
1575 llvm::Value *MapTypesArray = nullptr;
1576 /// The total number of pointers passed to the runtime library.
1577 unsigned NumberOfPtrs = 0u;
1578 /// Map between the a declaration of a capture and the corresponding base
1579 /// pointer address where the runtime returns the device pointers.
1580 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1581
1582 explicit TargetDataInfo() {}
1583 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1584 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1585 /// Clear information about the data arrays.
1586 void clearArrayInfo() {
1587 BasePointersArray = nullptr;
1588 PointersArray = nullptr;
1589 SizesArray = nullptr;
1590 MapTypesArray = nullptr;
1591 NumberOfPtrs = 0u;
1592 }
1593 /// Return true if the current target data information has valid arrays.
1594 bool isValid() {
1595 return BasePointersArray && PointersArray && SizesArray &&
1596 MapTypesArray && NumberOfPtrs;
1597 }
1598 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1599 };
1600
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001601 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001602 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001603 /// \param IfCond Expression evaluated in if clause associated with the
1604 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001605 /// \param Device Expression evaluated in device clause associated with the
1606 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001607 /// \param Info A record used to store information that needs to be preserved
1608 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001609 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1610 const OMPExecutableDirective &D,
1611 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001612 const RegionCodeGenTy &CodeGen,
1613 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001614
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001615 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001616 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001617 /// \param D Directive to emit.
1618 /// \param IfCond Expression evaluated in if clause associated with the target
1619 /// directive, or null if no if clause is used.
1620 /// \param Device Expression evaluated in device clause associated with the
1621 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001622 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1623 const OMPExecutableDirective &D,
1624 const Expr *IfCond,
1625 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001626
1627 /// Marks function \a Fn with properly mangled versions of vector functions.
1628 /// \param FD Function marked as 'declare simd'.
1629 /// \param Fn LLVM function that must be marked with 'declare simd'
1630 /// attributes.
1631 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1632 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001633
1634 /// Emit initialization for doacross loop nesting support.
1635 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001636 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1637 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001638
1639 /// Emit code for doacross ordered directive with 'depend' clause.
1640 /// \param C 'depend' clause with 'sink|source' dependency kind.
1641 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1642 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001643
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001644 /// Translates the native parameter of outlined function if this is required
1645 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001646 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001647 /// \param NativeParam Parameter itself.
1648 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1649 const VarDecl *NativeParam) const {
1650 return NativeParam;
1651 }
1652
1653 /// Gets the address of the native argument basing on the address of the
1654 /// target-specific parameter.
1655 /// \param NativeParam Parameter itself.
1656 /// \param TargetParam Corresponding target-specific parameter.
1657 virtual Address getParameterAddress(CodeGenFunction &CGF,
1658 const VarDecl *NativeParam,
1659 const VarDecl *TargetParam) const;
1660
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001661 /// Choose default schedule type and chunk value for the
1662 /// dist_schedule clause.
1663 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1664 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1665 llvm::Value *&Chunk) const {}
1666
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001667 /// Choose default schedule type and chunk value for the
1668 /// schedule clause.
1669 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1670 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
Alexey Bataevf6a53d62019-03-18 18:40:00 +00001671 const Expr *&ChunkExpr) const;
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001672
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001673 /// Emits call of the outlined function with the provided arguments,
1674 /// translating these arguments to correct target-specific arguments.
1675 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001676 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001677 llvm::FunctionCallee OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001678 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001679
1680 /// Emits OpenMP-specific function prolog.
1681 /// Required for device constructs.
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001682 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001683
1684 /// Gets the OpenMP-specific address of the local variable.
1685 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1686 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001687
Raphael Isemannb23ccec2018-12-10 12:37:46 +00001688 /// Marks the declaration as already emitted for the device code and returns
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001689 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001690 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001691
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001692 /// Emit deferred declare target variables marked for deferred emission.
1693 void emitDeferredTargetDecls() const;
Alexey Bataev60705422018-10-30 15:50:12 +00001694
1695 /// Adjust some parameters for the target-based directives, like addresses of
1696 /// the variables captured by reference in lambdas.
1697 virtual void
1698 adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
1699 const OMPExecutableDirective &D) const;
Patrick Lyster8f7f5862018-11-19 15:09:33 +00001700
1701 /// Perform check on requires decl to ensure that target architecture
1702 /// supports unified addressing
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001703 virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D);
Alexey Bataevc5687252019-03-21 19:35:27 +00001704
1705 /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1706 /// the predefined allocator and translates it into the corresponding address
1707 /// space.
1708 virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00001709
1710 /// Return whether the unified_shared_memory has been specified.
1711 bool hasRequiresUnifiedSharedMemory() const;
Alexey Bataev2df5f122019-10-01 20:18:32 +00001712
1713 /// Emits the definition of the declare variant function.
1714 virtual bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition);
Alexey Bataev0860db92019-12-19 10:01:10 -05001715
1716 /// Checks if the \p VD variable is marked as nontemporal declaration in
1717 /// current context.
1718 bool isNontemporalDecl(const ValueDecl *VD) const;
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001719
Alexey Bataev7b518dc2020-01-06 16:14:34 -05001720 /// Initializes global counter for lastprivate conditional.
1721 virtual void
1722 initLastprivateConditionalCounter(CodeGenFunction &CGF,
1723 const OMPExecutableDirective &S);
1724
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001725 /// Checks if the provided \p LVal is lastprivate conditional and emits the
1726 /// code to update the value of the original variable.
1727 /// \code
1728 /// lastprivate(conditional: a)
1729 /// ...
1730 /// <type> a;
1731 /// lp_a = ...;
1732 /// #pragma omp critical(a)
1733 /// if (last_iv_a <= iv) {
1734 /// last_iv_a = iv;
1735 /// global_a = lp_a;
1736 /// }
1737 /// \endcode
1738 virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF,
1739 const Expr *LHS);
1740
1741 /// Gets the address of the global copy used for lastprivate conditional
1742 /// update, if any.
1743 /// \param PrivLVal LValue for the private copy.
1744 /// \param VD Original lastprivate declaration.
1745 virtual void emitLastprivateConditionalFinalUpdate(CodeGenFunction &CGF,
1746 LValue PrivLVal,
1747 const VarDecl *VD,
1748 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +00001749};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001750
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001751/// Class supports emissionof SIMD-only code.
1752class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1753public:
1754 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1755 ~CGOpenMPSIMDRuntime() override {}
1756
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001757 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001758 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1759 /// kmp_int32 BoundID, struct context_vars*).
1760 /// \param D OpenMP directive.
1761 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1762 /// \param InnermostKind Kind of innermost directive (for simple directives it
1763 /// is a directive itself, for combined - its innermost directive).
1764 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001765 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001766 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1767 const VarDecl *ThreadIDVar,
1768 OpenMPDirectiveKind InnermostKind,
1769 const RegionCodeGenTy &CodeGen) override;
1770
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001771 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001772 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1773 /// kmp_int32 BoundID, struct context_vars*).
1774 /// \param D OpenMP directive.
1775 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1776 /// \param InnermostKind Kind of innermost directive (for simple directives it
1777 /// is a directive itself, for combined - its innermost directive).
1778 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001779 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001780 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1781 const VarDecl *ThreadIDVar,
1782 OpenMPDirectiveKind InnermostKind,
1783 const RegionCodeGenTy &CodeGen) override;
1784
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001785 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001786 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1787 /// TaskT).
1788 /// \param D OpenMP directive.
1789 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1790 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1791 /// task region.
1792 /// \param TaskTVar Variable for task_t argument.
1793 /// \param InnermostKind Kind of innermost directive (for simple directives it
1794 /// is a directive itself, for combined - its innermost directive).
1795 /// \param CodeGen Code generation sequence for the \a D directive.
1796 /// \param Tied true if task is generated for tied task, false otherwise.
1797 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1798 /// tasks.
1799 ///
James Y Knight9871db02019-02-05 16:42:33 +00001800 llvm::Function *emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001801 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1802 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1803 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1804 bool Tied, unsigned &NumberOfParts) override;
1805
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001806 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001807 /// variables captured in a record which address is stored in \a
1808 /// CapturedStruct.
1809 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1810 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1811 /// \param CapturedVars A pointer to the record with the references to
1812 /// variables used in \a OutlinedFn function.
1813 /// \param IfCond Condition in the associated 'if' clause, if it was
1814 /// specified, nullptr otherwise.
1815 ///
1816 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001817 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001818 ArrayRef<llvm::Value *> CapturedVars,
1819 const Expr *IfCond) override;
1820
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001821 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001822 /// \param CriticalName Name of the critical region.
1823 /// \param CriticalOpGen Generator for the statement associated with the given
1824 /// critical region.
1825 /// \param Hint Value of the 'hint' clause (optional).
1826 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1827 const RegionCodeGenTy &CriticalOpGen,
1828 SourceLocation Loc,
1829 const Expr *Hint = nullptr) override;
1830
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001831 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001832 /// \param MasterOpGen Generator for the statement associated with the given
1833 /// master region.
1834 void emitMasterRegion(CodeGenFunction &CGF,
1835 const RegionCodeGenTy &MasterOpGen,
1836 SourceLocation Loc) override;
1837
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001838 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001839 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1840
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001841 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001842 /// \param TaskgroupOpGen Generator for the statement associated with the
1843 /// given taskgroup region.
1844 void emitTaskgroupRegion(CodeGenFunction &CGF,
1845 const RegionCodeGenTy &TaskgroupOpGen,
1846 SourceLocation Loc) override;
1847
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001848 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001849 /// \param SingleOpGen Generator for the statement associated with the given
1850 /// single region.
1851 void emitSingleRegion(CodeGenFunction &CGF,
1852 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1853 ArrayRef<const Expr *> CopyprivateVars,
1854 ArrayRef<const Expr *> DestExprs,
1855 ArrayRef<const Expr *> SrcExprs,
1856 ArrayRef<const Expr *> AssignmentOps) override;
1857
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001858 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001859 /// \param OrderedOpGen Generator for the statement associated with the given
1860 /// ordered region.
1861 void emitOrderedRegion(CodeGenFunction &CGF,
1862 const RegionCodeGenTy &OrderedOpGen,
1863 SourceLocation Loc, bool IsThreads) override;
1864
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001865 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001866 /// \param Kind Directive for which this implicit barrier call must be
1867 /// generated. Must be OMPD_barrier for explicit barrier generation.
1868 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1869 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1870 /// runtime class decides which one to emit (simple or with cancellation
1871 /// checks).
1872 ///
1873 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1874 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1875 bool ForceSimpleCall = false) override;
1876
1877 /// This is used for non static scheduled types and when the ordered
1878 /// clause is present on the loop construct.
1879 /// Depending on the loop schedule, it is necessary to call some runtime
1880 /// routine before start of the OpenMP loop to get the loop upper / lower
1881 /// bounds \a LB and \a UB and stride \a ST.
1882 ///
1883 /// \param CGF Reference to current CodeGenFunction.
1884 /// \param Loc Clang source location.
1885 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1886 /// \param IVSize Size of the iteration variable in bits.
1887 /// \param IVSigned Sign of the iteration variable.
1888 /// \param Ordered true if loop is ordered, false otherwise.
1889 /// \param DispatchValues struct containing llvm values for lower bound, upper
1890 /// bound, and chunk expression.
1891 /// For the default (nullptr) value, the chunk 1 will be used.
1892 ///
1893 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1894 const OpenMPScheduleTy &ScheduleKind,
1895 unsigned IVSize, bool IVSigned, bool Ordered,
1896 const DispatchRTInput &DispatchValues) override;
1897
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001898 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001899 /// of loop.
1900 ///
1901 /// This is used only in case of static schedule, when the user did not
1902 /// specify a ordered clause on the loop construct.
1903 /// Depending on the loop schedule, it is necessary to call some runtime
1904 /// routine before start of the OpenMP loop to get the loop upper / lower
1905 /// bounds LB and UB and stride ST.
1906 ///
1907 /// \param CGF Reference to current CodeGenFunction.
1908 /// \param Loc Clang source location.
1909 /// \param DKind Kind of the directive.
1910 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1911 /// \param Values Input arguments for the construct.
1912 ///
1913 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1914 OpenMPDirectiveKind DKind,
1915 const OpenMPScheduleTy &ScheduleKind,
1916 const StaticRTInput &Values) override;
1917
1918 ///
1919 /// \param CGF Reference to current CodeGenFunction.
1920 /// \param Loc Clang source location.
1921 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1922 /// \param Values Input arguments for the construct.
1923 ///
1924 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1925 OpenMPDistScheduleClauseKind SchedKind,
1926 const StaticRTInput &Values) override;
1927
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001928 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001929 /// iteration of the ordered loop with the dynamic scheduling.
1930 ///
1931 /// \param CGF Reference to current CodeGenFunction.
1932 /// \param Loc Clang source location.
1933 /// \param IVSize Size of the iteration variable in bits.
1934 /// \param IVSigned Sign of the iteration variable.
1935 ///
1936 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1937 unsigned IVSize, bool IVSigned) override;
1938
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001939 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001940 /// all the work with current loop.
1941 ///
1942 /// \param CGF Reference to current CodeGenFunction.
1943 /// \param Loc Clang source location.
1944 /// \param DKind Kind of the directive for which the static finish is emitted.
1945 ///
1946 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1947 OpenMPDirectiveKind DKind) override;
1948
1949 /// Call __kmpc_dispatch_next(
1950 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1951 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1952 /// kmp_int[32|64] *p_stride);
1953 /// \param IVSize Size of the iteration variable in bits.
1954 /// \param IVSigned Sign of the iteration variable.
1955 /// \param IL Address of the output variable in which the flag of the
1956 /// last iteration is returned.
1957 /// \param LB Address of the output variable in which the lower iteration
1958 /// number is returned.
1959 /// \param UB Address of the output variable in which the upper iteration
1960 /// number is returned.
1961 /// \param ST Address of the output variable in which the stride value is
1962 /// returned.
1963 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1964 unsigned IVSize, bool IVSigned, Address IL,
1965 Address LB, Address UB, Address ST) override;
1966
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001967 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001968 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1969 /// clause.
1970 /// \param NumThreads An integer value of threads.
1971 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1972 SourceLocation Loc) override;
1973
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001974 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001975 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1976 void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001977 llvm::omp::ProcBindKind ProcBind,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001978 SourceLocation Loc) override;
1979
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001980 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001981 /// thread.
1982 /// \param VD Threadprivate variable.
1983 /// \param VDAddr Address of the global variable \a VD.
1984 /// \param Loc Location of the reference to threadprivate var.
1985 /// \return Address of the threadprivate variable for the current thread.
1986 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1987 Address VDAddr, SourceLocation Loc) override;
1988
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001989 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001990 /// a call to runtime library which adds initial value to the newly created
1991 /// threadprivate variable (if it is not constant) and registers destructor
1992 /// for the variable (if any).
1993 /// \param VD Threadprivate variable.
1994 /// \param VDAddr Address of the global variable \a VD.
1995 /// \param Loc Location of threadprivate declaration.
1996 /// \param PerformInit true if initialization expression is not constant.
1997 llvm::Function *
1998 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1999 SourceLocation Loc, bool PerformInit,
2000 CodeGenFunction *CGF = nullptr) override;
2001
2002 /// Creates artificial threadprivate variable with name \p Name and type \p
2003 /// VarType.
2004 /// \param VarType Type of the artificial threadprivate variable.
2005 /// \param Name Name of the artificial threadprivate variable.
2006 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
2007 QualType VarType,
2008 StringRef Name) override;
2009
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002010 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002011 /// \param Vars List of variables to flush.
2012 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
2013 SourceLocation Loc) override;
2014
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002015 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002016 /// emitted in several steps:
2017 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2018 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2019 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2020 /// function:
2021 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2022 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2023 /// return 0;
2024 /// }
2025 /// 2. Copy a list of shared variables to field shareds of the resulting
2026 /// structure kmp_task_t returned by the previous call (if any).
2027 /// 3. Copy a pointer to destructions function to field destructions of the
2028 /// resulting structure kmp_task_t.
2029 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
2030 /// kmp_task_t *new_task), where new_task is a resulting structure from
2031 /// previous items.
2032 /// \param D Current task directive.
2033 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2034 /// /*part_id*/, captured_struct */*__context*/);
2035 /// \param SharedsTy A type which contains references the shared variables.
2036 /// \param Shareds Context with the list of shared variables from the \p
2037 /// TaskFunction.
2038 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2039 /// otherwise.
2040 /// \param Data Additional data for task generation like tiednsee, final
2041 /// state, list of privates etc.
2042 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002043 const OMPExecutableDirective &D,
2044 llvm::Function *TaskFunction, QualType SharedsTy,
2045 Address Shareds, const Expr *IfCond,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002046 const OMPTaskDataTy &Data) override;
2047
2048 /// Emit task region for the taskloop directive. The taskloop region is
2049 /// emitted in several steps:
2050 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2051 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2052 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2053 /// function:
2054 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2055 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2056 /// return 0;
2057 /// }
2058 /// 2. Copy a list of shared variables to field shareds of the resulting
2059 /// structure kmp_task_t returned by the previous call (if any).
2060 /// 3. Copy a pointer to destructions function to field destructions of the
2061 /// resulting structure kmp_task_t.
2062 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
2063 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
2064 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
2065 /// is a resulting structure from
2066 /// previous items.
2067 /// \param D Current task directive.
2068 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2069 /// /*part_id*/, captured_struct */*__context*/);
2070 /// \param SharedsTy A type which contains references the shared variables.
2071 /// \param Shareds Context with the list of shared variables from the \p
2072 /// TaskFunction.
2073 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2074 /// otherwise.
2075 /// \param Data Additional data for task generation like tiednsee, final
2076 /// state, list of privates etc.
2077 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002078 const OMPLoopDirective &D, llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002079 QualType SharedsTy, Address Shareds, const Expr *IfCond,
2080 const OMPTaskDataTy &Data) override;
2081
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002082 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002083 /// reduction:
2084 /// \code
2085 ///
2086 /// static kmp_critical_name lock = { 0 };
2087 ///
2088 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
2089 /// ...
2090 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
2091 /// ...
2092 /// }
2093 ///
2094 /// ...
2095 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
2096 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
2097 /// RedList, reduce_func, &<lock>)) {
2098 /// case 1:
2099 /// ...
2100 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
2101 /// ...
2102 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
2103 /// break;
2104 /// case 2:
2105 /// ...
2106 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
2107 /// ...
2108 /// break;
2109 /// default:;
2110 /// }
2111 /// \endcode
2112 ///
2113 /// \param Privates List of private copies for original reduction arguments.
2114 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
2115 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
2116 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
2117 /// or 'operator binop(LHS, RHS)'.
2118 /// \param Options List of options for reduction codegen:
2119 /// WithNowait true if parent directive has also nowait clause, false
2120 /// otherwise.
2121 /// SimpleReduction Emit reduction operation only. Used for omp simd
2122 /// directive on the host.
2123 /// ReductionKind The kind of reduction to perform.
2124 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2125 ArrayRef<const Expr *> Privates,
2126 ArrayRef<const Expr *> LHSExprs,
2127 ArrayRef<const Expr *> RHSExprs,
2128 ArrayRef<const Expr *> ReductionOps,
2129 ReductionOptionsTy Options) override;
2130
2131 /// Emit a code for initialization of task reduction clause. Next code
2132 /// should be emitted for reduction:
2133 /// \code
2134 ///
2135 /// _task_red_item_t red_data[n];
2136 /// ...
2137 /// red_data[i].shar = &origs[i];
2138 /// red_data[i].size = sizeof(origs[i]);
2139 /// red_data[i].f_init = (void*)RedInit<i>;
2140 /// red_data[i].f_fini = (void*)RedDest<i>;
2141 /// red_data[i].f_comb = (void*)RedOp<i>;
2142 /// red_data[i].flags = <Flag_i>;
2143 /// ...
2144 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2145 /// \endcode
2146 ///
2147 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2148 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2149 /// \param Data Additional data for task generation like tiedness, final
2150 /// state, list of privates, reductions etc.
2151 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2152 ArrayRef<const Expr *> LHSExprs,
2153 ArrayRef<const Expr *> RHSExprs,
2154 const OMPTaskDataTy &Data) override;
2155
2156 /// Required to resolve existing problems in the runtime. Emits threadprivate
2157 /// variables to store the size of the VLAs/array sections for
2158 /// initializer/combiner/finalizer functions + emits threadprivate variable to
2159 /// store the pointer to the original reduction item for the custom
2160 /// initializer defined by declare reduction construct.
2161 /// \param RCG Allows to reuse an existing data for the reductions.
2162 /// \param N Reduction item for which fixups must be emitted.
2163 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2164 ReductionCodeGen &RCG, unsigned N) override;
2165
2166 /// Get the address of `void *` type of the privatue copy of the reduction
2167 /// item specified by the \p SharedLVal.
2168 /// \param ReductionsPtr Pointer to the reduction data returned by the
2169 /// emitTaskReductionInit function.
2170 /// \param SharedLVal Address of the original reduction item.
2171 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2172 llvm::Value *ReductionsPtr,
2173 LValue SharedLVal) override;
2174
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002175 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002176 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2177
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002178 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002179 /// \param CancelRegion Region kind for which the cancellation point must be
2180 /// emitted.
2181 ///
2182 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2183 OpenMPDirectiveKind CancelRegion) override;
2184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002185 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002186 /// \param IfCond Condition in the associated 'if' clause, if it was
2187 /// specified, nullptr otherwise.
2188 /// \param CancelRegion Region kind for which the cancel must be emitted.
2189 ///
2190 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2191 const Expr *IfCond,
2192 OpenMPDirectiveKind CancelRegion) override;
2193
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002194 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002195 /// \param D Directive to emit.
2196 /// \param ParentName Name of the function that encloses the target region.
2197 /// \param OutlinedFn Outlined function value to be defined by this call.
2198 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2199 /// \param IsOffloadEntry True if the outlined function is an offload entry.
2200 /// \param CodeGen Code generation sequence for the \a D directive.
2201 /// An outlined function may not be an entry if, e.g. the if clause always
2202 /// evaluates to false.
2203 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
2204 StringRef ParentName,
2205 llvm::Function *&OutlinedFn,
2206 llvm::Constant *&OutlinedFnID,
2207 bool IsOffloadEntry,
2208 const RegionCodeGenTy &CodeGen) override;
2209
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002210 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002211 /// code attempts offloading the execution to the device, an the event of
2212 /// a failure it executes the host version outlined in \a OutlinedFn.
2213 /// \param D Directive to emit.
2214 /// \param OutlinedFn Host version of the code to be offloaded.
2215 /// \param OutlinedFnID ID of host version of the code to be offloaded.
2216 /// \param IfCond Expression evaluated in if clause associated with the target
2217 /// directive, or null if no if clause is used.
2218 /// \param Device Expression evaluated in device clause associated with the
2219 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00002220 void
2221 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2222 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
2223 const Expr *IfCond, const Expr *Device,
2224 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2225 const OMPLoopDirective &D)>
2226 SizeEmitter) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002227
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002228 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002229 /// the function itself in case it is a valid device function. Returns true if
2230 /// \a GD was dealt with successfully.
2231 /// \param GD Function to scan.
2232 bool emitTargetFunctions(GlobalDecl GD) override;
2233
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002234 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002235 /// Returns true if \a GD was dealt with successfully.
2236 /// \param GD Variable declaration to emit.
2237 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2238
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002239 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002240 /// if it was emitted successfully.
2241 /// \param GD Global to scan.
2242 bool emitTargetGlobal(GlobalDecl GD) override;
2243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002244 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002245 /// variables captured in a record which address is stored in \a
2246 /// CapturedStruct.
2247 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2248 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2249 /// \param CapturedVars A pointer to the record with the references to
2250 /// variables used in \a OutlinedFn function.
2251 ///
2252 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002253 SourceLocation Loc, llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002254 ArrayRef<llvm::Value *> CapturedVars) override;
2255
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002256 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002257 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2258 /// for num_teams clause.
2259 /// \param NumTeams An integer expression of teams.
2260 /// \param ThreadLimit An integer expression of threads.
2261 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2262 const Expr *ThreadLimit, SourceLocation Loc) override;
2263
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002264 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002265 /// \param D Directive to emit.
2266 /// \param IfCond Expression evaluated in if clause associated with the
2267 /// target directive, or null if no device clause is used.
2268 /// \param Device Expression evaluated in device clause associated with the
2269 /// target directive, or null if no device clause is used.
2270 /// \param Info A record used to store information that needs to be preserved
2271 /// until the region is closed.
2272 void emitTargetDataCalls(CodeGenFunction &CGF,
2273 const OMPExecutableDirective &D, const Expr *IfCond,
2274 const Expr *Device, const RegionCodeGenTy &CodeGen,
2275 TargetDataInfo &Info) override;
2276
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002277 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002278 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2279 /// \param D Directive to emit.
2280 /// \param IfCond Expression evaluated in if clause associated with the target
2281 /// directive, or null if no if clause is used.
2282 /// \param Device Expression evaluated in device clause associated with the
2283 /// target directive, or null if no device clause is used.
2284 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2285 const OMPExecutableDirective &D,
2286 const Expr *IfCond,
2287 const Expr *Device) override;
2288
2289 /// Emit initialization for doacross loop nesting support.
2290 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002291 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2292 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002293
2294 /// Emit code for doacross ordered directive with 'depend' clause.
2295 /// \param C 'depend' clause with 'sink|source' dependency kind.
2296 void emitDoacrossOrdered(CodeGenFunction &CGF,
2297 const OMPDependClause *C) override;
2298
2299 /// Translates the native parameter of outlined function if this is required
2300 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002301 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002302 /// \param NativeParam Parameter itself.
2303 const VarDecl *translateParameter(const FieldDecl *FD,
2304 const VarDecl *NativeParam) const override;
2305
2306 /// Gets the address of the native argument basing on the address of the
2307 /// target-specific parameter.
2308 /// \param NativeParam Parameter itself.
2309 /// \param TargetParam Corresponding target-specific parameter.
2310 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2311 const VarDecl *TargetParam) const override;
Alexey Bataev4f680db2019-03-19 16:41:16 +00002312
2313 /// Gets the OpenMP-specific address of the local variable.
2314 Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2315 const VarDecl *VD) override {
2316 return Address::invalid();
2317 }
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002318};
2319
Alexey Bataev23b69422014-06-18 07:08:49 +00002320} // namespace CodeGen
2321} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002322
2323#endif