blob: 4739be8a73b779074c84a10b1cdde942e2ade46e [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataev9959db52014-05-06 10:08:46 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This provides a class for OpenMP runtime code generation.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
14#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
Alexey Bataev9959db52014-05-06 10:08:46 +000015
Alexey Bataev7292c292016-04-25 12:22:29 +000016#include "CGValue.h"
Richard Trieuf8b8b392019-01-11 01:32:35 +000017#include "clang/AST/DeclOpenMP.h"
Jordan Rupprecht52690912019-10-01 22:30:10 +000018#include "clang/AST/GlobalDecl.h"
Alexey Bataev62b63b12015-03-10 07:28:44 +000019#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000020#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000021#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "llvm/ADT/DenseMap.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000023#include "llvm/ADT/StringMap.h"
Alexey Bataev2a6f3f52018-11-07 19:11:14 +000024#include "llvm/ADT/StringSet.h"
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -060025#include "llvm/Frontend/OpenMP/OMPConstants.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000026#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000027#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000028
29namespace llvm {
30class ArrayType;
31class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000032class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000033class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000034class StructType;
35class Type;
36class Value;
37} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000038
Alexey Bataev9959db52014-05-06 10:08:46 +000039namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000040class Expr;
Alexey Bataev8b427062016-05-25 12:36:08 +000041class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000042class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000043class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000044class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000045class OMPDeclareReductionDecl;
46class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000047
Alexey Bataev9959db52014-05-06 10:08:46 +000048namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000049class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000050class CodeGenFunction;
51class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000052
Alexey Bataev14fa1c62016-03-29 05:34:15 +000053/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
54/// region.
55class PrePostActionTy {
56public:
57 explicit PrePostActionTy() {}
58 virtual void Enter(CodeGenFunction &CGF) {}
59 virtual void Exit(CodeGenFunction &CGF) {}
60 virtual ~PrePostActionTy() {}
61};
62
63/// Class provides a way to call simple version of codegen for OpenMP region, or
64/// an advanced with possible pre|post-actions in codegen.
65class RegionCodeGenTy final {
66 intptr_t CodeGen;
67 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
68 CodeGenTy Callback;
69 mutable PrePostActionTy *PrePostAction;
70 RegionCodeGenTy() = delete;
71 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
72 template <typename Callable>
73 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
74 PrePostActionTy &Action) {
75 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
76 }
77
78public:
79 template <typename Callable>
80 RegionCodeGenTy(
81 Callable &&CodeGen,
82 typename std::enable_if<
83 !std::is_same<typename std::remove_reference<Callable>::type,
84 RegionCodeGenTy>::value>::type * = nullptr)
85 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
86 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
87 PrePostAction(nullptr) {}
88 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
89 void operator()(CodeGenFunction &CGF) const;
90};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000091
Alexey Bataev24b5bae2016-04-28 09:23:51 +000092struct OMPTaskDataTy final {
93 SmallVector<const Expr *, 4> PrivateVars;
94 SmallVector<const Expr *, 4> PrivateCopies;
95 SmallVector<const Expr *, 4> FirstprivateVars;
96 SmallVector<const Expr *, 4> FirstprivateCopies;
97 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000098 SmallVector<const Expr *, 4> LastprivateVars;
99 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000100 SmallVector<const Expr *, 4> ReductionVars;
101 SmallVector<const Expr *, 4> ReductionCopies;
102 SmallVector<const Expr *, 4> ReductionOps;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000103 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
104 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
105 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000106 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000107 llvm::Value *Reductions = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000108 unsigned NumberOfParts = 0;
109 bool Tied = true;
110 bool Nogroup = false;
111};
112
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000113/// Class intended to support codegen of all kind of the reduction clauses.
114class ReductionCodeGen {
115private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000116 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000117 struct ReductionData {
118 /// Reference to the original shared item.
119 const Expr *Ref = nullptr;
120 /// Helper expression for generation of private copy.
121 const Expr *Private = nullptr;
122 /// Helper expression for generation reduction operation.
123 const Expr *ReductionOp = nullptr;
124 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
125 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
126 };
127 /// List of reduction-based clauses.
128 SmallVector<ReductionData, 4> ClausesData;
129
130 /// List of addresses of original shared variables/expressions.
131 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
132 /// Sizes of the reduction items in chars.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000133 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000134 /// Base declarations for the reduction items.
135 SmallVector<const VarDecl *, 4> BaseDecls;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000136
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000137 /// Emits lvalue for shared expression.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000138 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
139 /// Emits upper bound for shared expression (if array section).
140 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
141 /// Performs aggregate initialization.
142 /// \param N Number of reduction item in the common list.
143 /// \param PrivateAddr Address of the corresponding private item.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000144 /// \param SharedLVal Address of the original shared variable.
145 /// \param DRD Declare reduction construct used for reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000146 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000147 Address PrivateAddr, LValue SharedLVal,
148 const OMPDeclareReductionDecl *DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000149
150public:
151 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
152 ArrayRef<const Expr *> Privates,
153 ArrayRef<const Expr *> ReductionOps);
154 /// Emits lvalue for a reduction item.
155 /// \param N Number of the reduction item.
156 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
157 /// Emits the code for the variable-modified type, if required.
158 /// \param N Number of the reduction item.
159 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
160 /// Emits the code for the variable-modified type, if required.
161 /// \param N Number of the reduction item.
162 /// \param Size Size of the type in chars.
163 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
164 /// Performs initialization of the private copy for the reduction item.
165 /// \param N Number of the reduction item.
166 /// \param PrivateAddr Address of the corresponding private item.
167 /// \param DefaultInit Default initialization sequence that should be
168 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000169 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000170 void
171 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
172 LValue SharedLVal,
173 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000174 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000175 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000176 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000177 /// \param N Number of the reduction item.
178 /// \param PrivateAddr Address of the corresponding private item.
179 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000180 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000181 /// address in normal operations.
182 /// \param N Number of the reduction item.
183 /// \param PrivateAddr Address of the corresponding private item.
184 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
185 Address PrivateAddr);
186 /// Returns LValue for the reduction item.
187 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000188 /// Returns the size of the reduction item (in chars and total number of
189 /// elements in the item), or nullptr, if the size is a constant.
190 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
191 return Sizes[N];
192 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000193 /// Returns the base declaration of the reduction item.
194 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
Alexey Bataev1c44e152018-03-06 18:59:43 +0000195 /// Returns the base declaration of the reduction item.
196 const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000197 /// Returns true if the initialization of the reduction item uses initializer
198 /// from declare reduction construct.
199 bool usesReductionInitializer(unsigned N) const;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000200};
201
Alexey Bataev9959db52014-05-06 10:08:46 +0000202class CGOpenMPRuntime {
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000203public:
204 /// Allows to disable automatic handling of functions used in target regions
205 /// as those marked as `omp declare target`.
206 class DisableAutoDeclareTargetRAII {
207 CodeGenModule &CGM;
208 bool SavedShouldMarkAsGlobal;
209
210 public:
211 DisableAutoDeclareTargetRAII(CodeGenModule &CGM);
212 ~DisableAutoDeclareTargetRAII();
213 };
214
Alexey Bataev0860db92019-12-19 10:01:10 -0500215 /// Manages list of nontemporal decls for the specified directive.
216 class NontemporalDeclsRAII {
217 CodeGenModule &CGM;
218 const bool NeedToPush;
219
220 public:
221 NontemporalDeclsRAII(CodeGenModule &CGM, const OMPLoopDirective &S);
222 ~NontemporalDeclsRAII();
223 };
224
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500225 /// Maps the expression for the lastprivate variable to the global copy used
226 /// to store new value because original variables are not mapped in inner
227 /// parallel regions. Only private copies are captured but we need also to
228 /// store private copy in shared address.
229 /// Also, stores the expression for the private loop counter and it
230 /// threaprivate name.
231 struct LastprivateConditionalData {
232 llvm::SmallDenseMap<CanonicalDeclPtr<const Decl>, SmallString<16>>
233 DeclToUniqeName;
234 LValue IVLVal;
235 SmallString<16> IVName;
236 /// True if original lvalue for loop counter can be used in codegen (simd
237 /// region or simd only mode) and no need to create threadprivate
238 /// references.
239 bool UseOriginalIV = false;
240 };
241 /// Manages list of lastprivate conditional decls for the specified directive.
242 class LastprivateConditionalRAII {
243 CodeGenModule &CGM;
244 const bool NeedToPush;
245
246 public:
247 LastprivateConditionalRAII(CodeGenFunction &CGF,
248 const OMPExecutableDirective &S, LValue IVLVal);
249 ~LastprivateConditionalRAII();
250 };
251
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000252protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000253 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000254 StringRef FirstSeparator, Separator;
255
256 /// Constructor allowing to redefine the name separator for the variables.
257 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
258 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000259
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000260 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000261 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000262 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000263 uint64_t Size, int32_t Flags,
264 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000265
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000266 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000267 /// \param D Directive to emit.
268 /// \param ParentName Name of the function that encloses the target region.
269 /// \param OutlinedFn Outlined function value to be defined by this call.
270 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
271 /// \param IsOffloadEntry True if the outlined function is an offload entry.
272 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000273 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000274 /// evaluates to false.
275 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
276 StringRef ParentName,
277 llvm::Function *&OutlinedFn,
278 llvm::Constant *&OutlinedFnID,
279 bool IsOffloadEntry,
280 const RegionCodeGenTy &CodeGen);
281
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000282 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000283 /// \param Flags Flags for OpenMP location.
284 ///
285 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
286 unsigned Flags = 0);
287
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000288 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000289 llvm::Type *getIdentTyPointerTy();
290
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000291 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000292 ///
293 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
294
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000296 // The name can be customized depending on the target.
297 //
298 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
299
Alexey Bataev3c595a62017-08-14 15:01:03 +0000300 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
James Y Knight9871db02019-02-05 16:42:33 +0000301 void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
302 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000303 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000305 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000306 /// stored.
307 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
308
Alexey Bataevfd006c42018-10-05 15:08:53 +0000309 void setLocThreadIdInsertPt(CodeGenFunction &CGF,
310 bool AtCurrentPoint = false);
311 void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
312
Alexey Bataevceeaa482018-11-21 21:04:34 +0000313 /// Check if the default location must be constant.
314 /// Default is false to support OMPT/OMPD.
315 virtual bool isDefaultLocationConstant() const { return false; }
316
317 /// Returns additional flags that can be stored in reserved_2 field of the
318 /// default location.
319 virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
320
Alexey Bataevc2cd2d42019-10-10 17:28:10 +0000321 /// Tries to emit declare variant function for \p OldGD from \p NewGD.
322 /// \param OrigAddr LLVM IR value for \p OldGD.
323 /// \param IsForDefinition true, if requested emission for the definition of
324 /// \p OldGD.
325 /// \returns true, was able to emit a definition function for \p OldGD, which
326 /// points to \p NewGD.
327 virtual bool tryEmitDeclareVariant(const GlobalDecl &NewGD,
328 const GlobalDecl &OldGD,
329 llvm::GlobalValue *OrigAddr,
330 bool IsForDefinition);
331
Alexey Bataevc3028ca2018-12-04 15:03:25 +0000332 /// Returns default flags for the barriers depending on the directive, for
333 /// which this barier is going to be emitted.
334 static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
335
Alexey Bataeva1166022018-11-27 21:24:54 +0000336 /// Get the LLVM type for the critical name.
337 llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
338
339 /// Returns corresponding lock object for the specified critical region
340 /// name. If the lock object does not exist it is created, otherwise the
341 /// reference to the existing copy is returned.
342 /// \param CriticalName Name of the critical region.
343 ///
344 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
345
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000346private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000347 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000348 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000349 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataevceeaa482018-11-21 21:04:34 +0000350 using FlagsTy = std::pair<unsigned, unsigned>;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000351 /// Map of flags and corresponding default locations.
Alexey Bataevceeaa482018-11-21 21:04:34 +0000352 using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
Alexey Bataev15007ba2014-05-07 06:18:01 +0000353 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000354 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000355
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000356 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000357 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000358 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000359 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
360 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000361 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000362 /// Original representation is:
363 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000364 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000365 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000366 struct DebugLocThreadIdTy {
367 llvm::Value *DebugLoc;
368 llvm::Value *ThreadID;
Alexey Bataevfd006c42018-10-05 15:08:53 +0000369 /// Insert point for the service instructions.
370 llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000371 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000372 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000373 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
374 OpenMPLocThreadIDMapTy;
375 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000376 /// Map of UDRs and corresponding combiner/initializer.
377 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
378 std::pair<llvm::Function *, llvm::Function *>>
379 UDRMapTy;
380 UDRMapTy UDRMap;
381 /// Map of functions and locally defined UDRs.
382 typedef llvm::DenseMap<llvm::Function *,
383 SmallVector<const OMPDeclareReductionDecl *, 4>>
384 FunctionUDRMapTy;
385 FunctionUDRMapTy FunctionUDRMap;
Michael Krused47b9432019-08-05 18:43:21 +0000386 /// Map from the user-defined mapper declaration to its corresponding
387 /// functions.
388 llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
389 /// Map of functions and their local user-defined mappers.
390 using FunctionUDMMapTy =
391 llvm::DenseMap<llvm::Function *,
392 SmallVector<const OMPDeclareMapperDecl *, 4>>;
393 FunctionUDMMapTy FunctionUDMMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000394 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000395 /// kmp_critical_name[8];
396 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000397 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000398 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
399 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
400 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
401 /// variables.
402 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
403 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000404 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000405 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000406 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000407 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000408 /// void * shareds; /**< pointer to block of pointers to
409 /// shared vars */
410 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
411 /// executing task */
412 /// kmp_int32 part_id; /**< part id for the task */
413 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
414 /// deconstructors of firstprivate C++ objects */
415 /// } kmp_task_t;
416 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000417 /// Saved kmp_task_t for task directive.
418 QualType SavedKmpTaskTQTy;
419 /// Saved kmp_task_t for taskloop-based directive.
420 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000421 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000422 /// kmp_intptr_t base_addr;
423 /// size_t len;
424 /// struct {
425 /// bool in:1;
426 /// bool out:1;
427 /// } flags;
428 /// } kmp_depend_info_t;
429 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000430 /// struct kmp_dim { // loop bounds info casted to kmp_int64
431 /// kmp_int64 lo; // lower
432 /// kmp_int64 up; // upper
433 /// kmp_int64 st; // stride
434 /// };
435 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000436 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000437 /// void *addr; // Pointer to the offload entry info.
438 /// // (function or global)
439 /// char *name; // Name of the function or global.
440 /// size_t size; // Size of the entry info (0 if it a function).
441 /// };
442 QualType TgtOffloadEntryQTy;
443 /// struct __tgt_device_image{
444 /// void *ImageStart; // Pointer to the target code start.
445 /// void *ImageEnd; // Pointer to the target code end.
446 /// // We also add the host entries to the device image, as it may be useful
447 /// // for the target runtime to have access to that information.
448 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
449 /// // the entries.
450 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
451 /// // entries (non inclusive).
452 /// };
453 QualType TgtDeviceImageQTy;
454 /// struct __tgt_bin_desc{
455 /// int32_t NumDevices; // Number of devices supported.
456 /// __tgt_device_image *DeviceImages; // Arrays of device images
457 /// // (one per device).
458 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
459 /// // entries.
460 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
461 /// // entries (non inclusive).
462 /// };
463 QualType TgtBinaryDescriptorQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000464 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000465 /// far.
466 class OffloadEntriesInfoManagerTy {
467 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000468
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000469 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000470 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000471
472 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000473 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000474 class OffloadEntryInfo {
475 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000476 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000477 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000478 /// Entry is a target region.
479 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000480 /// Entry is a declare target variable.
481 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000482 /// Invalid entry info.
483 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000484 };
485
Alexey Bataev03f270c2018-03-30 18:31:07 +0000486 protected:
487 OffloadEntryInfo() = delete;
488 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000489 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000490 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000491 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000492 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000493
Alexey Bataev03f270c2018-03-30 18:31:07 +0000494 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000495 bool isValid() const { return Order != ~0u; }
496 unsigned getOrder() const { return Order; }
497 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000498 uint32_t getFlags() const { return Flags; }
499 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
500 llvm::Constant *getAddress() const {
501 return cast_or_null<llvm::Constant>(Addr);
502 }
503 void setAddress(llvm::Constant *V) {
504 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
505 Addr = V;
506 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000507 static bool classof(const OffloadEntryInfo *Info) { return true; }
508
Samuel Antaof83efdb2017-01-05 16:02:49 +0000509 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000510 /// Address of the entity that has to be mapped for offloading.
511 llvm::WeakTrackingVH Addr;
512
Samuel Antaof83efdb2017-01-05 16:02:49 +0000513 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000514 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000515
516 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000517 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000518
Alexey Bataev03f270c2018-03-30 18:31:07 +0000519 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000520 };
521
Alexey Bataev03f270c2018-03-30 18:31:07 +0000522 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000523 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000524 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000525 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000526 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000527
Alexey Bataev03f270c2018-03-30 18:31:07 +0000528 //
529 // Target region entries related.
530 //
531
532 /// Kind of the target registry entry.
533 enum OMPTargetRegionEntryKind : uint32_t {
534 /// Mark the entry as target region.
535 OMPTargetRegionEntryTargetRegion = 0x0,
536 /// Mark the entry as a global constructor.
537 OMPTargetRegionEntryCtor = 0x02,
538 /// Mark the entry as a global destructor.
539 OMPTargetRegionEntryDtor = 0x04,
540 };
541
542 /// Target region entries info.
543 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
544 /// Address that can be used as the ID of the entry.
545 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000546
547 public:
548 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000549 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000550 explicit OffloadEntryInfoTargetRegion(unsigned Order,
551 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000552 llvm::Constant *ID,
553 OMPTargetRegionEntryKind Flags)
554 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000555 ID(ID) {
556 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000557 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000558
559 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000560 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000561 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000562 ID = V;
563 }
564 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000565 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000566 }
567 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000568
569 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000570 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
571 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000572 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000573 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000574 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
575 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000576 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000577 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000578 /// Return true if a target region entry with the provided information
579 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000580 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000581 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000582 /// brief Applies action \a Action on all registered entries.
583 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000584 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000585 OffloadTargetRegionEntryInfoActTy;
586 void actOnTargetRegionEntriesInfo(
587 const OffloadTargetRegionEntryInfoActTy &Action);
588
Alexey Bataev03f270c2018-03-30 18:31:07 +0000589 //
590 // Device global variable entries related.
591 //
592
593 /// Kind of the global variable entry..
594 enum OMPTargetGlobalVarEntryKind : uint32_t {
595 /// Mark the entry as a to declare target.
596 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000597 /// Mark the entry as a to declare target link.
598 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000599 };
600
601 /// Device global variable entries info.
602 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
603 /// Type of the global variable.
604 CharUnits VarSize;
605 llvm::GlobalValue::LinkageTypes Linkage;
606
607 public:
608 OffloadEntryInfoDeviceGlobalVar()
609 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
610 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
611 OMPTargetGlobalVarEntryKind Flags)
612 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
613 explicit OffloadEntryInfoDeviceGlobalVar(
614 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
615 OMPTargetGlobalVarEntryKind Flags,
616 llvm::GlobalValue::LinkageTypes Linkage)
617 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
618 VarSize(VarSize), Linkage(Linkage) {
619 setAddress(Addr);
620 }
621
622 CharUnits getVarSize() const { return VarSize; }
623 void setVarSize(CharUnits Size) { VarSize = Size; }
624 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
625 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
626 static bool classof(const OffloadEntryInfo *Info) {
627 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
628 }
629 };
630
631 /// Initialize device global variable entry.
632 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
633 OMPTargetGlobalVarEntryKind Flags,
634 unsigned Order);
635
636 /// Register device global variable entry.
637 void
638 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
639 CharUnits VarSize,
640 OMPTargetGlobalVarEntryKind Flags,
641 llvm::GlobalValue::LinkageTypes Linkage);
642 /// Checks if the variable with the given name has been registered already.
643 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
644 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
645 }
646 /// Applies action \a Action on all registered entries.
647 typedef llvm::function_ref<void(StringRef,
648 const OffloadEntryInfoDeviceGlobalVar &)>
649 OffloadDeviceGlobalVarEntryInfoActTy;
650 void actOnDeviceGlobalVarEntriesInfo(
651 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
652
Samuel Antaoee8fb302016-01-06 13:42:12 +0000653 private:
654 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000655 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000656 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000657 OffloadEntriesTargetRegionPerLine;
658 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
659 OffloadEntriesTargetRegionPerParentName;
660 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
661 OffloadEntriesTargetRegionPerFile;
662 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
663 OffloadEntriesTargetRegionPerDevice;
664 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
665 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000666 /// Storage for device global variable entries kind. The storage is to be
667 /// indexed by mangled name.
668 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
669 OffloadEntriesDeviceGlobalVarTy;
670 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000671 };
672 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
673
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000674 bool ShouldMarkAsGlobal = true;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000675 /// List of the emitted functions.
676 llvm::StringSet<> AlreadyEmittedTargetFunctions;
677 /// List of the global variables with their addresses that should not be
678 /// emitted for the target.
679 llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000680
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000681 /// List of variables that can become declare target implicitly and, thus,
682 /// must be emitted.
683 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
684
Alexey Bataev2df5f122019-10-01 20:18:32 +0000685 /// Mapping of the original functions to their variants and original global
686 /// decl.
687 llvm::MapVector<CanonicalDeclPtr<const FunctionDecl>,
688 std::pair<GlobalDecl, GlobalDecl>>
689 DeferredVariantFunction;
690
Alexey Bataev0860db92019-12-19 10:01:10 -0500691 using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
692 /// Stack for list of declarations in current context marked as nontemporal.
693 /// The set is the union of all current stack elements.
694 llvm::SmallVector<NontemporalDeclsSet, 4> NontemporalDeclsStack;
695
Alexey Bataeva58da1a2019-12-27 09:44:43 -0500696 /// Stack for list of addresses of declarations in current context marked as
697 /// lastprivate conditional. The set is the union of all current stack
698 /// elements.
699 llvm::SmallVector<LastprivateConditionalData, 4> LastprivateConditionalStack;
700
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000701 /// Flag for keeping track of weather a requires unified_shared_memory
702 /// directive is present.
703 bool HasRequiresUnifiedSharedMemory = false;
704
705 /// Flag for keeping track of weather a target region has been emitted.
706 bool HasEmittedTargetRegion = false;
707
708 /// Flag for keeping track of weather a device routine has been emitted.
709 /// Device routines are specific to the
710 bool HasEmittedDeclareTargetRegion = false;
711
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000712 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000713 /// metadata.
714 void loadOffloadInfoMetadata();
715
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000716 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000717 QualType getTgtOffloadEntryQTy();
718
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000719 /// Returns __tgt_device_image type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000720 QualType getTgtDeviceImageQTy();
721
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000722 /// Returns __tgt_bin_desc type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000723 QualType getTgtBinaryDescriptorQTy();
724
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000725 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000726 /// found along the way.
727 /// \param S Starting statement.
728 /// \param ParentName Name of the function declaration that is being scanned.
729 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000730
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000731 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000732 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000733
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000734 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000735 llvm::Type *getKmpc_MicroPointerTy();
736
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000737 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000738 /// \param Function OpenMP runtime function.
739 /// \return Specified function.
James Y Knight9871db02019-02-05 16:42:33 +0000740 llvm::FunctionCallee createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000741
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000742 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000743 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000744 llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
745 bool IVSigned);
Alexander Musman21212e42015-03-13 10:38:23 +0000746
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000747 /// Returns __kmpc_dispatch_init_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000748 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000749 llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize,
750 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000751
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000752 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000753 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000754 llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
755 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000756
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000757 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000758 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000759 llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
760 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000761
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000762 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000763 /// return threadprivate cache object. This object is a pointer's worth of
764 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000765 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000766 /// \return Cache variable for the specified threadprivate.
767 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
768
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000769 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000770 /// internal global variable with the specified Name. The created variable has
771 /// linkage CommonLinkage by default and is initialized by null value.
772 /// \param Ty Type of the global variable. If it is exist already the type
773 /// must be the same.
774 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000775 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000776 const llvm::Twine &Name,
777 unsigned AddressSpace = 0);
Alexey Bataev97720002014-11-11 04:05:39 +0000778
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000779 /// Set of threadprivate variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000780 llvm::StringSet<> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000781
Alexey Bataev34f8a702018-03-28 14:28:54 +0000782 /// Set of declare target variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000783 llvm::StringSet<> DeclareTargetWithDefinition;
Alexey Bataev34f8a702018-03-28 14:28:54 +0000784
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000785 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000786 /// \param VDAddr Address of the global variable \a VD.
787 /// \param Ctor Pointer to a global init function for \a VD.
788 /// \param CopyCtor Pointer to a global copy function for \a VD.
789 /// \param Dtor Pointer to a global destructor function for \a VD.
790 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000791 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000792 llvm::Value *Ctor, llvm::Value *CopyCtor,
793 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000794
Michael Krused47b9432019-08-05 18:43:21 +0000795 /// Emit the array initialization or deletion portion for user-defined mapper
796 /// code generation.
797 void emitUDMapperArrayInitOrDel(CodeGenFunction &MapperCGF,
798 llvm::Value *Handle, llvm::Value *BasePtr,
799 llvm::Value *Ptr, llvm::Value *Size,
800 llvm::Value *MapType, CharUnits ElementSize,
801 llvm::BasicBlock *ExitBB, bool IsInit);
802
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000803 struct TaskResultTy {
804 llvm::Value *NewTask = nullptr;
James Y Knight9871db02019-02-05 16:42:33 +0000805 llvm::Function *TaskEntry = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000806 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000807 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000808 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000809 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000810 };
811 /// Emit task region for the task directive. The task region is emitted in
812 /// several steps:
813 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
814 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
815 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
816 /// function:
817 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
818 /// TaskFunction(gtid, tt->part_id, tt->shareds);
819 /// return 0;
820 /// }
821 /// 2. Copy a list of shared variables to field shareds of the resulting
822 /// structure kmp_task_t returned by the previous call (if any).
823 /// 3. Copy a pointer to destructions function to field destructions of the
824 /// resulting structure kmp_task_t.
825 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000826 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
827 /// /*part_id*/, captured_struct */*__context*/);
828 /// \param SharedsTy A type which contains references the shared variables.
829 /// \param Shareds Context with the list of shared variables from the \p
830 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000831 /// \param Data Additional data for task generation like tiednsee, final
832 /// state, list of privates etc.
833 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
834 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +0000835 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000836 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000837
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000838 /// Returns default address space for the constant firstprivates, 0 by
839 /// default.
840 virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; }
841
Alexey Bataevec7946e2019-09-23 14:06:51 +0000842 /// Emit code that pushes the trip count of loops associated with constructs
843 /// 'target teams distribute' and 'teams distribute parallel for'.
844 /// \param SizeEmitter Emits the int64 value for the number of iterations of
845 /// the associated loop.
846 void emitTargetNumIterationsCall(
847 CodeGenFunction &CGF, const OMPExecutableDirective &D,
848 llvm::Value *DeviceID,
849 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
850 const OMPLoopDirective &D)>
851 SizeEmitter);
852
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000853public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000854 explicit CGOpenMPRuntime(CodeGenModule &CGM)
855 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000856 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000857 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000858
Alexey Bataevd08c0562019-11-19 12:07:54 -0500859 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
860 /// function. Here is the logic:
861 /// if (Cond) {
862 /// ThenGen();
863 /// } else {
864 /// ElseGen();
865 /// }
866 void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
867 const RegionCodeGenTy &ThenGen,
868 const RegionCodeGenTy &ElseGen);
869
Alexey Bataev5c427362019-04-10 19:11:33 +0000870 /// Checks if the \p Body is the \a CompoundStmt and returns its child
871 /// statement iff there is only one that is not evaluatable at the compile
872 /// time.
873 static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
874
Alexey Bataev18fa2322018-05-02 14:20:50 +0000875 /// Get the platform-specific name separator.
876 std::string getName(ArrayRef<StringRef> Parts) const;
877
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000878 /// Emit code for the specified user defined reduction construct.
879 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
880 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000881 /// Get combiner/initializer for the specified user-defined reduction, if any.
882 virtual std::pair<llvm::Function *, llvm::Function *>
883 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000884
Michael Krused47b9432019-08-05 18:43:21 +0000885 /// Emit the function for the user defined mapper construct.
886 void emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
887 CodeGenFunction *CGF = nullptr);
888
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000889 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000890 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
891 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000892 /// \param D OpenMP directive.
893 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000894 /// \param InnermostKind Kind of innermost directive (for simple directives it
895 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000896 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000897 virtual llvm::Function *emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000898 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
899 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
900
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000901 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000902 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
903 /// kmp_int32 BoundID, struct context_vars*).
904 /// \param D OpenMP directive.
905 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
906 /// \param InnermostKind Kind of innermost directive (for simple directives it
907 /// is a directive itself, for combined - its innermost directive).
908 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000909 virtual llvm::Function *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000910 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
911 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000912
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000913 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000914 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
915 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000916 /// \param D OpenMP directive.
917 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000918 /// \param PartIDVar Variable for partition id in the current OpenMP untied
919 /// task region.
920 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000921 /// \param InnermostKind Kind of innermost directive (for simple directives it
922 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000923 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000924 /// \param Tied true if task is generated for tied task, false otherwise.
925 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
926 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000927 ///
James Y Knight9871db02019-02-05 16:42:33 +0000928 virtual llvm::Function *emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000929 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000930 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
931 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
932 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000933
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000934 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000935 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000936 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000937
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000938 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000939 /// variables captured in a record which address is stored in \a
940 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000941 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000942 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000943 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000944 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000945 /// \param IfCond Condition in the associated 'if' clause, if it was
946 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000947 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000948 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +0000949 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000950 ArrayRef<llvm::Value *> CapturedVars,
951 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000952
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000953 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000954 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000955 /// \param CriticalOpGen Generator for the statement associated with the given
956 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000957 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000958 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000959 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000960 SourceLocation Loc,
961 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000962
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000963 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000964 /// \param MasterOpGen Generator for the statement associated with the given
965 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000966 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000967 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000968 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000969
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000970 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000971 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000972
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000973 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000974 /// \param TaskgroupOpGen Generator for the statement associated with the
975 /// given taskgroup region.
976 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
977 const RegionCodeGenTy &TaskgroupOpGen,
978 SourceLocation Loc);
979
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000980 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000981 /// \param SingleOpGen Generator for the statement associated with the given
982 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000983 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000984 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000985 SourceLocation Loc,
986 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000987 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000988 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000989 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000990
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000991 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000992 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000993 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000994 virtual void emitOrderedRegion(CodeGenFunction &CGF,
995 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000996 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000997
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000998 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +0000999 /// \param Kind Directive for which this implicit barrier call must be
1000 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001001 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1002 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1003 /// runtime class decides which one to emit (simple or with cancellation
1004 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001005 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001006 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001007 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001008 bool EmitChecks = true,
1009 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +00001010
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001011 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +00001012 /// This kind of worksharing directive is emitted without outer loop.
1013 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1014 /// \param Chunked True if chunk is specified in the clause.
1015 ///
1016 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
1017 bool Chunked) const;
1018
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001019 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001020 /// This kind of distribute directive is emitted without outer loop.
1021 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1022 /// \param Chunked True if chunk is specified in the clause.
1023 ///
1024 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
1025 bool Chunked) const;
1026
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00001027 /// Check if the specified \a ScheduleKind is static chunked.
1028 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
1029 /// \param Chunked True if chunk is specified in the clause.
1030 ///
1031 virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
1032 bool Chunked) const;
1033
1034 /// Check if the specified \a ScheduleKind is static non-chunked.
1035 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
1036 /// \param Chunked True if chunk is specified in the clause.
1037 ///
1038 virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
1039 bool Chunked) const;
1040
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001041 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001042 /// This kind of worksharing directive is emitted without outer loop.
1043 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
1044 ///
1045 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
1046
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001047 /// struct with the values to be passed to the dispatch runtime function
1048 struct DispatchRTInput {
1049 /// Loop lower bound
1050 llvm::Value *LB = nullptr;
1051 /// Loop upper bound
1052 llvm::Value *UB = nullptr;
1053 /// Chunk size specified using 'schedule' clause (nullptr if chunk
1054 /// was not specified)
1055 llvm::Value *Chunk = nullptr;
1056 DispatchRTInput() = default;
1057 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
1058 : LB(LB), UB(UB), Chunk(Chunk) {}
1059 };
1060
1061 /// Call the appropriate runtime routine to initialize it before start
1062 /// of loop.
1063
1064 /// This is used for non static scheduled types and when the ordered
1065 /// clause is present on the loop construct.
1066 /// Depending on the loop schedule, it is necessary to call some runtime
1067 /// routine before start of the OpenMP loop to get the loop upper / lower
1068 /// bounds \a LB and \a UB and stride \a ST.
1069 ///
1070 /// \param CGF Reference to current CodeGenFunction.
1071 /// \param Loc Clang source location.
1072 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1073 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001074 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001075 /// \param Ordered true if loop is ordered, false otherwise.
1076 /// \param DispatchValues struct containing llvm values for lower bound, upper
1077 /// bound, and chunk expression.
1078 /// For the default (nullptr) value, the chunk 1 will be used.
1079 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001080 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001081 const OpenMPScheduleTy &ScheduleKind,
1082 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001083 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001084
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001085 /// Struct with the values to be passed to the static runtime function
1086 struct StaticRTInput {
1087 /// Size of the iteration variable in bits.
1088 unsigned IVSize = 0;
1089 /// Sign of the iteration variable.
1090 bool IVSigned = false;
1091 /// true if loop is ordered, false otherwise.
1092 bool Ordered = false;
1093 /// Address of the output variable in which the flag of the last iteration
1094 /// is returned.
1095 Address IL = Address::invalid();
1096 /// Address of the output variable in which the lower iteration number is
1097 /// returned.
1098 Address LB = Address::invalid();
1099 /// Address of the output variable in which the upper iteration number is
1100 /// returned.
1101 Address UB = Address::invalid();
1102 /// Address of the output variable in which the stride value is returned
1103 /// necessary to generated the static_chunked scheduled loop.
1104 Address ST = Address::invalid();
1105 /// Value of the chunk for the static_chunked scheduled loop. For the
1106 /// default (nullptr) value, the chunk 1 will be used.
1107 llvm::Value *Chunk = nullptr;
1108 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1109 Address LB, Address UB, Address ST,
1110 llvm::Value *Chunk = nullptr)
1111 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1112 UB(UB), ST(ST), Chunk(Chunk) {}
1113 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001114 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +00001115 /// of loop.
1116 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001117 /// This is used only in case of static schedule, when the user did not
1118 /// specify a ordered clause on the loop construct.
1119 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +00001120 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001121 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +00001122 ///
1123 /// \param CGF Reference to current CodeGenFunction.
1124 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001125 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001126 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001127 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +00001128 ///
John McCall7f416cc2015-09-08 08:05:57 +00001129 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001130 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001131 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001132 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00001133
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001134 ///
1135 /// \param CGF Reference to current CodeGenFunction.
1136 /// \param Loc Clang source location.
1137 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001138 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001139 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001140 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1141 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001142 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001143 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001144
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001145 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001146 /// iteration of the ordered loop with the dynamic scheduling.
1147 ///
1148 /// \param CGF Reference to current CodeGenFunction.
1149 /// \param Loc Clang source location.
1150 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001151 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001152 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001153 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1154 SourceLocation Loc, unsigned IVSize,
1155 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001156
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001157 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001158 /// all the work with current loop.
1159 ///
1160 /// \param CGF Reference to current CodeGenFunction.
1161 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001162 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001163 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001164 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1165 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001166
Alexander Musman92bdaab2015-03-12 13:37:50 +00001167 /// Call __kmpc_dispatch_next(
1168 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1169 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1170 /// kmp_int[32|64] *p_stride);
1171 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001172 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001173 /// \param IL Address of the output variable in which the flag of the
1174 /// last iteration is returned.
1175 /// \param LB Address of the output variable in which the lower iteration
1176 /// number is returned.
1177 /// \param UB Address of the output variable in which the upper iteration
1178 /// number is returned.
1179 /// \param ST Address of the output variable in which the stride value is
1180 /// returned.
1181 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1182 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001183 Address IL, Address LB,
1184 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001185
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001186 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001187 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1188 /// clause.
1189 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001190 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1191 llvm::Value *NumThreads,
1192 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001193
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001194 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001195 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1196 virtual void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001197 llvm::omp::ProcBindKind ProcBind,
Alexey Bataev7f210c62015-06-18 13:40:03 +00001198 SourceLocation Loc);
1199
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001200 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001201 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001202 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001203 /// \param VDAddr Address of the global variable \a VD.
1204 /// \param Loc Location of the reference to threadprivate var.
1205 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001206 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1207 const VarDecl *VD,
1208 Address VDAddr,
1209 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001210
Alexey Bataev92327c52018-03-26 16:40:55 +00001211 /// Returns the address of the variable marked as declare target with link
Gheorghe-Teodor Bercea0034e842019-06-20 18:04:47 +00001212 /// clause OR as declare target with to clause and unified memory.
1213 virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001214
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001215 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001216 /// a call to runtime library which adds initial value to the newly created
1217 /// threadprivate variable (if it is not constant) and registers destructor
1218 /// for the variable (if any).
1219 /// \param VD Threadprivate variable.
1220 /// \param VDAddr Address of the global variable \a VD.
1221 /// \param Loc Location of threadprivate declaration.
1222 /// \param PerformInit true if initialization expression is not constant.
1223 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001224 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001225 SourceLocation Loc, bool PerformInit,
1226 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001227
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001228 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001229 /// \param VD Declare target variable.
1230 /// \param Addr Address of the global variable \a VD.
1231 /// \param PerformInit true if initialization expression is not constant.
1232 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1233 llvm::GlobalVariable *Addr,
1234 bool PerformInit);
1235
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001236 /// Creates artificial threadprivate variable with name \p Name and type \p
1237 /// VarType.
1238 /// \param VarType Type of the artificial threadprivate variable.
1239 /// \param Name Name of the artificial threadprivate variable.
1240 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1241 QualType VarType,
1242 StringRef Name);
1243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001244 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001245 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001246 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1247 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001248
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001249 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001250 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001251 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1252 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1253 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1254 /// function:
1255 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1256 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1257 /// return 0;
1258 /// }
1259 /// 2. Copy a list of shared variables to field shareds of the resulting
1260 /// structure kmp_task_t returned by the previous call (if any).
1261 /// 3. Copy a pointer to destructions function to field destructions of the
1262 /// resulting structure kmp_task_t.
1263 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1264 /// kmp_task_t *new_task), where new_task is a resulting structure from
1265 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001266 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001267 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1268 /// /*part_id*/, captured_struct */*__context*/);
1269 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001270 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001271 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001272 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1273 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001274 /// \param Data Additional data for task generation like tiednsee, final
1275 /// state, list of privates etc.
1276 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1277 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001278 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001279 Address Shareds, const Expr *IfCond,
1280 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001281
Alexey Bataev7292c292016-04-25 12:22:29 +00001282 /// Emit task region for the taskloop directive. The taskloop region is
1283 /// emitted in several steps:
1284 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1285 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1286 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1287 /// function:
1288 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1289 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1290 /// return 0;
1291 /// }
1292 /// 2. Copy a list of shared variables to field shareds of the resulting
1293 /// structure kmp_task_t returned by the previous call (if any).
1294 /// 3. Copy a pointer to destructions function to field destructions of the
1295 /// resulting structure kmp_task_t.
1296 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1297 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1298 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1299 /// is a resulting structure from
1300 /// previous items.
1301 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001302 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1303 /// /*part_id*/, captured_struct */*__context*/);
1304 /// \param SharedsTy A type which contains references the shared variables.
1305 /// \param Shareds Context with the list of shared variables from the \p
1306 /// TaskFunction.
1307 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1308 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001309 /// \param Data Additional data for task generation like tiednsee, final
1310 /// state, list of privates etc.
James Y Knight9871db02019-02-05 16:42:33 +00001311 virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1312 const OMPLoopDirective &D,
1313 llvm::Function *TaskFunction,
1314 QualType SharedsTy, Address Shareds,
1315 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001316
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001317 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001318 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001319 /// \param InnermostKind Kind of innermost directive (for simple directives it
1320 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001321 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001322 /// \param HasCancel true if region has inner cancel directive, false
1323 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001324 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001325 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001326 const RegionCodeGenTy &CodeGen,
1327 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001328
1329 /// Emits reduction function.
1330 /// \param ArgsType Array type containing pointers to reduction variables.
1331 /// \param Privates List of private copies for original reduction arguments.
1332 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1333 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1334 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1335 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev982a35e2019-03-19 17:09:52 +00001336 llvm::Function *emitReductionFunction(SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001337 llvm::Type *ArgsType,
1338 ArrayRef<const Expr *> Privates,
1339 ArrayRef<const Expr *> LHSExprs,
1340 ArrayRef<const Expr *> RHSExprs,
1341 ArrayRef<const Expr *> ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001342
1343 /// Emits single reduction combiner
1344 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1345 const Expr *ReductionOp,
1346 const Expr *PrivateRef,
1347 const DeclRefExpr *LHS,
1348 const DeclRefExpr *RHS);
1349
1350 struct ReductionOptionsTy {
1351 bool WithNowait;
1352 bool SimpleReduction;
1353 OpenMPDirectiveKind ReductionKind;
1354 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001355 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001356 /// reduction:
1357 /// \code
1358 ///
1359 /// static kmp_critical_name lock = { 0 };
1360 ///
1361 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1362 /// ...
1363 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1364 /// ...
1365 /// }
1366 ///
1367 /// ...
1368 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1369 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1370 /// RedList, reduce_func, &<lock>)) {
1371 /// case 1:
1372 /// ...
1373 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1374 /// ...
1375 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1376 /// break;
1377 /// case 2:
1378 /// ...
1379 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1380 /// ...
1381 /// break;
1382 /// default:;
1383 /// }
1384 /// \endcode
1385 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001386 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001387 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1388 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1389 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1390 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001391 /// \param Options List of options for reduction codegen:
1392 /// WithNowait true if parent directive has also nowait clause, false
1393 /// otherwise.
1394 /// SimpleReduction Emit reduction operation only. Used for omp simd
1395 /// directive on the host.
1396 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001397 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001398 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001399 ArrayRef<const Expr *> LHSExprs,
1400 ArrayRef<const Expr *> RHSExprs,
1401 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001402 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001403
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001404 /// Emit a code for initialization of task reduction clause. Next code
1405 /// should be emitted for reduction:
1406 /// \code
1407 ///
1408 /// _task_red_item_t red_data[n];
1409 /// ...
1410 /// red_data[i].shar = &origs[i];
1411 /// red_data[i].size = sizeof(origs[i]);
1412 /// red_data[i].f_init = (void*)RedInit<i>;
1413 /// red_data[i].f_fini = (void*)RedDest<i>;
1414 /// red_data[i].f_comb = (void*)RedOp<i>;
1415 /// red_data[i].flags = <Flag_i>;
1416 /// ...
1417 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1418 /// \endcode
1419 ///
1420 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1421 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1422 /// \param Data Additional data for task generation like tiedness, final
1423 /// state, list of privates, reductions etc.
1424 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1425 SourceLocation Loc,
1426 ArrayRef<const Expr *> LHSExprs,
1427 ArrayRef<const Expr *> RHSExprs,
1428 const OMPTaskDataTy &Data);
1429
1430 /// Required to resolve existing problems in the runtime. Emits threadprivate
1431 /// variables to store the size of the VLAs/array sections for
1432 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1433 /// store the pointer to the original reduction item for the custom
1434 /// initializer defined by declare reduction construct.
1435 /// \param RCG Allows to reuse an existing data for the reductions.
1436 /// \param N Reduction item for which fixups must be emitted.
1437 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1438 ReductionCodeGen &RCG, unsigned N);
1439
1440 /// Get the address of `void *` type of the privatue copy of the reduction
1441 /// item specified by the \p SharedLVal.
1442 /// \param ReductionsPtr Pointer to the reduction data returned by the
1443 /// emitTaskReductionInit function.
1444 /// \param SharedLVal Address of the original reduction item.
1445 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1446 llvm::Value *ReductionsPtr,
1447 LValue SharedLVal);
1448
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001449 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001450 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001451
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001452 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001453 /// \param CancelRegion Region kind for which the cancellation point must be
1454 /// emitted.
1455 ///
1456 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1457 SourceLocation Loc,
1458 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001459
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001460 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001461 /// \param IfCond Condition in the associated 'if' clause, if it was
1462 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001463 /// \param CancelRegion Region kind for which the cancel must be emitted.
1464 ///
1465 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001466 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001467 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001468
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001469 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001470 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001471 /// \param ParentName Name of the function that encloses the target region.
1472 /// \param OutlinedFn Outlined function value to be defined by this call.
1473 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1474 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001475 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001476 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001477 /// evaluates to false.
1478 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1479 StringRef ParentName,
1480 llvm::Function *&OutlinedFn,
1481 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001482 bool IsOffloadEntry,
1483 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001484
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001485 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001486 /// code attempts offloading the execution to the device, an the event of
1487 /// a failure it executes the host version outlined in \a OutlinedFn.
1488 /// \param D Directive to emit.
1489 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001490 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001491 /// \param IfCond Expression evaluated in if clause associated with the target
1492 /// directive, or null if no if clause is used.
1493 /// \param Device Expression evaluated in device clause associated with the
1494 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00001495 /// \param SizeEmitter Callback to emit number of iterations for loop-based
1496 /// directives.
1497 virtual void
1498 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1499 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
1500 const Expr *IfCond, const Expr *Device,
1501 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
1502 const OMPLoopDirective &D)>
1503 SizeEmitter);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001504
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001505 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001506 /// the function itself in case it is a valid device function. Returns true if
1507 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001508 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001509 virtual bool emitTargetFunctions(GlobalDecl GD);
1510
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001511 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001512 /// Returns true if \a GD was dealt with successfully.
1513 /// \param GD Variable declaration to emit.
1514 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1515
Alexey Bataev03f270c2018-03-30 18:31:07 +00001516 /// Checks if the provided global decl \a GD is a declare target variable and
1517 /// registers it when emitting code for the host.
1518 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1519 llvm::Constant *Addr);
1520
Alexey Bataev1af5bd52019-03-05 17:47:18 +00001521 /// Registers provided target firstprivate variable as global on the
1522 /// target.
1523 llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1524 const VarDecl *VD);
1525
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001526 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001527 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001528 /// \param GD Global to scan.
1529 virtual bool emitTargetGlobal(GlobalDecl GD);
1530
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001531 /// Creates and returns a registration function for when at least one
1532 /// requires directives was used in the current module.
1533 llvm::Function *emitRequiresDirectiveRegFun();
1534
Sergey Dmitriev5836c352019-10-15 18:42:47 +00001535 /// Creates all the offload entries in the current compilation unit
1536 /// along with the associated metadata.
1537 void createOffloadEntriesAndInfoMetadata();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001538
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001539 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001540 /// variables captured in a record which address is stored in \a
1541 /// CapturedStruct.
1542 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1543 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1544 /// \param CapturedVars A pointer to the record with the references to
1545 /// variables used in \a OutlinedFn function.
1546 ///
1547 virtual void emitTeamsCall(CodeGenFunction &CGF,
1548 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001549 SourceLocation Loc, llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001550 ArrayRef<llvm::Value *> CapturedVars);
1551
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001552 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001553 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1554 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001555 /// \param NumTeams An integer expression of teams.
1556 /// \param ThreadLimit An integer expression of threads.
1557 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1558 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001559
Samuel Antaocc10b852016-07-28 14:23:26 +00001560 /// Struct that keeps all the relevant information that should be kept
1561 /// throughout a 'target data' region.
1562 class TargetDataInfo {
1563 /// Set to true if device pointer information have to be obtained.
1564 bool RequiresDevicePointerInfo = false;
1565
1566 public:
1567 /// The array of base pointer passed to the runtime library.
1568 llvm::Value *BasePointersArray = nullptr;
1569 /// The array of section pointers passed to the runtime library.
1570 llvm::Value *PointersArray = nullptr;
1571 /// The array of sizes passed to the runtime library.
1572 llvm::Value *SizesArray = nullptr;
1573 /// The array of map types passed to the runtime library.
1574 llvm::Value *MapTypesArray = nullptr;
1575 /// The total number of pointers passed to the runtime library.
1576 unsigned NumberOfPtrs = 0u;
1577 /// Map between the a declaration of a capture and the corresponding base
1578 /// pointer address where the runtime returns the device pointers.
1579 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1580
1581 explicit TargetDataInfo() {}
1582 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1583 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1584 /// Clear information about the data arrays.
1585 void clearArrayInfo() {
1586 BasePointersArray = nullptr;
1587 PointersArray = nullptr;
1588 SizesArray = nullptr;
1589 MapTypesArray = nullptr;
1590 NumberOfPtrs = 0u;
1591 }
1592 /// Return true if the current target data information has valid arrays.
1593 bool isValid() {
1594 return BasePointersArray && PointersArray && SizesArray &&
1595 MapTypesArray && NumberOfPtrs;
1596 }
1597 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1598 };
1599
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001600 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001601 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001602 /// \param IfCond Expression evaluated in if clause associated with the
1603 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001604 /// \param Device Expression evaluated in device clause associated with the
1605 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001606 /// \param Info A record used to store information that needs to be preserved
1607 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001608 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1609 const OMPExecutableDirective &D,
1610 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001611 const RegionCodeGenTy &CodeGen,
1612 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001613
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001614 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001615 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001616 /// \param D Directive to emit.
1617 /// \param IfCond Expression evaluated in if clause associated with the target
1618 /// directive, or null if no if clause is used.
1619 /// \param Device Expression evaluated in device clause associated with the
1620 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001621 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1622 const OMPExecutableDirective &D,
1623 const Expr *IfCond,
1624 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001625
1626 /// Marks function \a Fn with properly mangled versions of vector functions.
1627 /// \param FD Function marked as 'declare simd'.
1628 /// \param Fn LLVM function that must be marked with 'declare simd'
1629 /// attributes.
1630 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1631 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001632
1633 /// Emit initialization for doacross loop nesting support.
1634 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001635 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1636 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001637
1638 /// Emit code for doacross ordered directive with 'depend' clause.
1639 /// \param C 'depend' clause with 'sink|source' dependency kind.
1640 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1641 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001642
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001643 /// Translates the native parameter of outlined function if this is required
1644 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001645 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001646 /// \param NativeParam Parameter itself.
1647 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1648 const VarDecl *NativeParam) const {
1649 return NativeParam;
1650 }
1651
1652 /// Gets the address of the native argument basing on the address of the
1653 /// target-specific parameter.
1654 /// \param NativeParam Parameter itself.
1655 /// \param TargetParam Corresponding target-specific parameter.
1656 virtual Address getParameterAddress(CodeGenFunction &CGF,
1657 const VarDecl *NativeParam,
1658 const VarDecl *TargetParam) const;
1659
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001660 /// Choose default schedule type and chunk value for the
1661 /// dist_schedule clause.
1662 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1663 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1664 llvm::Value *&Chunk) const {}
1665
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001666 /// Choose default schedule type and chunk value for the
1667 /// schedule clause.
1668 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1669 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
Alexey Bataevf6a53d62019-03-18 18:40:00 +00001670 const Expr *&ChunkExpr) const;
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001671
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001672 /// Emits call of the outlined function with the provided arguments,
1673 /// translating these arguments to correct target-specific arguments.
1674 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001675 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001676 llvm::FunctionCallee OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001677 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001678
1679 /// Emits OpenMP-specific function prolog.
1680 /// Required for device constructs.
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001681 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001682
1683 /// Gets the OpenMP-specific address of the local variable.
1684 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1685 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001686
Raphael Isemannb23ccec2018-12-10 12:37:46 +00001687 /// Marks the declaration as already emitted for the device code and returns
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001688 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001689 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001690
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001691 /// Emit deferred declare target variables marked for deferred emission.
1692 void emitDeferredTargetDecls() const;
Alexey Bataev60705422018-10-30 15:50:12 +00001693
1694 /// Adjust some parameters for the target-based directives, like addresses of
1695 /// the variables captured by reference in lambdas.
1696 virtual void
1697 adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
1698 const OMPExecutableDirective &D) const;
Patrick Lyster8f7f5862018-11-19 15:09:33 +00001699
1700 /// Perform check on requires decl to ensure that target architecture
1701 /// supports unified addressing
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001702 virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D);
Alexey Bataevc5687252019-03-21 19:35:27 +00001703
1704 /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1705 /// the predefined allocator and translates it into the corresponding address
1706 /// space.
1707 virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00001708
1709 /// Return whether the unified_shared_memory has been specified.
1710 bool hasRequiresUnifiedSharedMemory() const;
Alexey Bataev2df5f122019-10-01 20:18:32 +00001711
1712 /// Emits the definition of the declare variant function.
1713 virtual bool emitDeclareVariant(GlobalDecl GD, bool IsForDefinition);
Alexey Bataev0860db92019-12-19 10:01:10 -05001714
1715 /// Checks if the \p VD variable is marked as nontemporal declaration in
1716 /// current context.
1717 bool isNontemporalDecl(const ValueDecl *VD) const;
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001718
Alexey Bataev7b518dc2020-01-06 16:14:34 -05001719 /// Initializes global counter for lastprivate conditional.
1720 virtual void
1721 initLastprivateConditionalCounter(CodeGenFunction &CGF,
1722 const OMPExecutableDirective &S);
1723
Alexey Bataeva58da1a2019-12-27 09:44:43 -05001724 /// Checks if the provided \p LVal is lastprivate conditional and emits the
1725 /// code to update the value of the original variable.
1726 /// \code
1727 /// lastprivate(conditional: a)
1728 /// ...
1729 /// <type> a;
1730 /// lp_a = ...;
1731 /// #pragma omp critical(a)
1732 /// if (last_iv_a <= iv) {
1733 /// last_iv_a = iv;
1734 /// global_a = lp_a;
1735 /// }
1736 /// \endcode
1737 virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF,
1738 const Expr *LHS);
1739
1740 /// Gets the address of the global copy used for lastprivate conditional
1741 /// update, if any.
1742 /// \param PrivLVal LValue for the private copy.
1743 /// \param VD Original lastprivate declaration.
1744 virtual void emitLastprivateConditionalFinalUpdate(CodeGenFunction &CGF,
1745 LValue PrivLVal,
1746 const VarDecl *VD,
1747 SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +00001748};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001749
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001750/// Class supports emissionof SIMD-only code.
1751class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1752public:
1753 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1754 ~CGOpenMPSIMDRuntime() override {}
1755
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001756 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001757 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1758 /// kmp_int32 BoundID, struct context_vars*).
1759 /// \param D OpenMP directive.
1760 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1761 /// \param InnermostKind Kind of innermost directive (for simple directives it
1762 /// is a directive itself, for combined - its innermost directive).
1763 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001764 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001765 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1766 const VarDecl *ThreadIDVar,
1767 OpenMPDirectiveKind InnermostKind,
1768 const RegionCodeGenTy &CodeGen) override;
1769
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001770 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001771 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1772 /// kmp_int32 BoundID, struct context_vars*).
1773 /// \param D OpenMP directive.
1774 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1775 /// \param InnermostKind Kind of innermost directive (for simple directives it
1776 /// is a directive itself, for combined - its innermost directive).
1777 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001778 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001779 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1780 const VarDecl *ThreadIDVar,
1781 OpenMPDirectiveKind InnermostKind,
1782 const RegionCodeGenTy &CodeGen) override;
1783
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001784 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001785 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1786 /// TaskT).
1787 /// \param D OpenMP directive.
1788 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1789 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1790 /// task region.
1791 /// \param TaskTVar Variable for task_t argument.
1792 /// \param InnermostKind Kind of innermost directive (for simple directives it
1793 /// is a directive itself, for combined - its innermost directive).
1794 /// \param CodeGen Code generation sequence for the \a D directive.
1795 /// \param Tied true if task is generated for tied task, false otherwise.
1796 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1797 /// tasks.
1798 ///
James Y Knight9871db02019-02-05 16:42:33 +00001799 llvm::Function *emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001800 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1801 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1802 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1803 bool Tied, unsigned &NumberOfParts) override;
1804
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001805 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001806 /// variables captured in a record which address is stored in \a
1807 /// CapturedStruct.
1808 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1809 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1810 /// \param CapturedVars A pointer to the record with the references to
1811 /// variables used in \a OutlinedFn function.
1812 /// \param IfCond Condition in the associated 'if' clause, if it was
1813 /// specified, nullptr otherwise.
1814 ///
1815 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001816 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001817 ArrayRef<llvm::Value *> CapturedVars,
1818 const Expr *IfCond) override;
1819
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001820 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001821 /// \param CriticalName Name of the critical region.
1822 /// \param CriticalOpGen Generator for the statement associated with the given
1823 /// critical region.
1824 /// \param Hint Value of the 'hint' clause (optional).
1825 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1826 const RegionCodeGenTy &CriticalOpGen,
1827 SourceLocation Loc,
1828 const Expr *Hint = nullptr) override;
1829
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001830 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001831 /// \param MasterOpGen Generator for the statement associated with the given
1832 /// master region.
1833 void emitMasterRegion(CodeGenFunction &CGF,
1834 const RegionCodeGenTy &MasterOpGen,
1835 SourceLocation Loc) override;
1836
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001837 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001838 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1839
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001840 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001841 /// \param TaskgroupOpGen Generator for the statement associated with the
1842 /// given taskgroup region.
1843 void emitTaskgroupRegion(CodeGenFunction &CGF,
1844 const RegionCodeGenTy &TaskgroupOpGen,
1845 SourceLocation Loc) override;
1846
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001847 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001848 /// \param SingleOpGen Generator for the statement associated with the given
1849 /// single region.
1850 void emitSingleRegion(CodeGenFunction &CGF,
1851 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1852 ArrayRef<const Expr *> CopyprivateVars,
1853 ArrayRef<const Expr *> DestExprs,
1854 ArrayRef<const Expr *> SrcExprs,
1855 ArrayRef<const Expr *> AssignmentOps) override;
1856
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001857 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001858 /// \param OrderedOpGen Generator for the statement associated with the given
1859 /// ordered region.
1860 void emitOrderedRegion(CodeGenFunction &CGF,
1861 const RegionCodeGenTy &OrderedOpGen,
1862 SourceLocation Loc, bool IsThreads) override;
1863
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001864 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001865 /// \param Kind Directive for which this implicit barrier call must be
1866 /// generated. Must be OMPD_barrier for explicit barrier generation.
1867 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1868 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1869 /// runtime class decides which one to emit (simple or with cancellation
1870 /// checks).
1871 ///
1872 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1873 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1874 bool ForceSimpleCall = false) override;
1875
1876 /// This is used for non static scheduled types and when the ordered
1877 /// clause is present on the loop construct.
1878 /// Depending on the loop schedule, it is necessary to call some runtime
1879 /// routine before start of the OpenMP loop to get the loop upper / lower
1880 /// bounds \a LB and \a UB and stride \a ST.
1881 ///
1882 /// \param CGF Reference to current CodeGenFunction.
1883 /// \param Loc Clang source location.
1884 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1885 /// \param IVSize Size of the iteration variable in bits.
1886 /// \param IVSigned Sign of the iteration variable.
1887 /// \param Ordered true if loop is ordered, false otherwise.
1888 /// \param DispatchValues struct containing llvm values for lower bound, upper
1889 /// bound, and chunk expression.
1890 /// For the default (nullptr) value, the chunk 1 will be used.
1891 ///
1892 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1893 const OpenMPScheduleTy &ScheduleKind,
1894 unsigned IVSize, bool IVSigned, bool Ordered,
1895 const DispatchRTInput &DispatchValues) override;
1896
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001897 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001898 /// of loop.
1899 ///
1900 /// This is used only in case of static schedule, when the user did not
1901 /// specify a ordered clause on the loop construct.
1902 /// Depending on the loop schedule, it is necessary to call some runtime
1903 /// routine before start of the OpenMP loop to get the loop upper / lower
1904 /// bounds LB and UB and stride ST.
1905 ///
1906 /// \param CGF Reference to current CodeGenFunction.
1907 /// \param Loc Clang source location.
1908 /// \param DKind Kind of the directive.
1909 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1910 /// \param Values Input arguments for the construct.
1911 ///
1912 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1913 OpenMPDirectiveKind DKind,
1914 const OpenMPScheduleTy &ScheduleKind,
1915 const StaticRTInput &Values) override;
1916
1917 ///
1918 /// \param CGF Reference to current CodeGenFunction.
1919 /// \param Loc Clang source location.
1920 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1921 /// \param Values Input arguments for the construct.
1922 ///
1923 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1924 OpenMPDistScheduleClauseKind SchedKind,
1925 const StaticRTInput &Values) override;
1926
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001927 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001928 /// iteration of the ordered loop with the dynamic scheduling.
1929 ///
1930 /// \param CGF Reference to current CodeGenFunction.
1931 /// \param Loc Clang source location.
1932 /// \param IVSize Size of the iteration variable in bits.
1933 /// \param IVSigned Sign of the iteration variable.
1934 ///
1935 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1936 unsigned IVSize, bool IVSigned) override;
1937
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001938 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001939 /// all the work with current loop.
1940 ///
1941 /// \param CGF Reference to current CodeGenFunction.
1942 /// \param Loc Clang source location.
1943 /// \param DKind Kind of the directive for which the static finish is emitted.
1944 ///
1945 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1946 OpenMPDirectiveKind DKind) override;
1947
1948 /// Call __kmpc_dispatch_next(
1949 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1950 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1951 /// kmp_int[32|64] *p_stride);
1952 /// \param IVSize Size of the iteration variable in bits.
1953 /// \param IVSigned Sign of the iteration variable.
1954 /// \param IL Address of the output variable in which the flag of the
1955 /// last iteration is returned.
1956 /// \param LB Address of the output variable in which the lower iteration
1957 /// number is returned.
1958 /// \param UB Address of the output variable in which the upper iteration
1959 /// number is returned.
1960 /// \param ST Address of the output variable in which the stride value is
1961 /// returned.
1962 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1963 unsigned IVSize, bool IVSigned, Address IL,
1964 Address LB, Address UB, Address ST) override;
1965
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001966 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001967 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1968 /// clause.
1969 /// \param NumThreads An integer value of threads.
1970 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1971 SourceLocation Loc) override;
1972
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001973 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001974 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1975 void emitProcBindClause(CodeGenFunction &CGF,
Johannes Doerfert6c5d1f402019-12-25 18:15:36 -06001976 llvm::omp::ProcBindKind ProcBind,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001977 SourceLocation Loc) override;
1978
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001979 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001980 /// thread.
1981 /// \param VD Threadprivate variable.
1982 /// \param VDAddr Address of the global variable \a VD.
1983 /// \param Loc Location of the reference to threadprivate var.
1984 /// \return Address of the threadprivate variable for the current thread.
1985 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1986 Address VDAddr, SourceLocation Loc) override;
1987
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001988 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001989 /// a call to runtime library which adds initial value to the newly created
1990 /// threadprivate variable (if it is not constant) and registers destructor
1991 /// for the variable (if any).
1992 /// \param VD Threadprivate variable.
1993 /// \param VDAddr Address of the global variable \a VD.
1994 /// \param Loc Location of threadprivate declaration.
1995 /// \param PerformInit true if initialization expression is not constant.
1996 llvm::Function *
1997 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1998 SourceLocation Loc, bool PerformInit,
1999 CodeGenFunction *CGF = nullptr) override;
2000
2001 /// Creates artificial threadprivate variable with name \p Name and type \p
2002 /// VarType.
2003 /// \param VarType Type of the artificial threadprivate variable.
2004 /// \param Name Name of the artificial threadprivate variable.
2005 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
2006 QualType VarType,
2007 StringRef Name) override;
2008
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002009 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002010 /// \param Vars List of variables to flush.
2011 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
2012 SourceLocation Loc) override;
2013
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002014 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002015 /// emitted in several steps:
2016 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2017 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2018 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2019 /// function:
2020 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2021 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2022 /// return 0;
2023 /// }
2024 /// 2. Copy a list of shared variables to field shareds of the resulting
2025 /// structure kmp_task_t returned by the previous call (if any).
2026 /// 3. Copy a pointer to destructions function to field destructions of the
2027 /// resulting structure kmp_task_t.
2028 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
2029 /// kmp_task_t *new_task), where new_task is a resulting structure from
2030 /// previous items.
2031 /// \param D Current task directive.
2032 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2033 /// /*part_id*/, captured_struct */*__context*/);
2034 /// \param SharedsTy A type which contains references the shared variables.
2035 /// \param Shareds Context with the list of shared variables from the \p
2036 /// TaskFunction.
2037 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2038 /// otherwise.
2039 /// \param Data Additional data for task generation like tiednsee, final
2040 /// state, list of privates etc.
2041 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002042 const OMPExecutableDirective &D,
2043 llvm::Function *TaskFunction, QualType SharedsTy,
2044 Address Shareds, const Expr *IfCond,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002045 const OMPTaskDataTy &Data) override;
2046
2047 /// Emit task region for the taskloop directive. The taskloop region is
2048 /// emitted in several steps:
2049 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
2050 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
2051 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
2052 /// function:
2053 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
2054 /// TaskFunction(gtid, tt->part_id, tt->shareds);
2055 /// return 0;
2056 /// }
2057 /// 2. Copy a list of shared variables to field shareds of the resulting
2058 /// structure kmp_task_t returned by the previous call (if any).
2059 /// 3. Copy a pointer to destructions function to field destructions of the
2060 /// resulting structure kmp_task_t.
2061 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
2062 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
2063 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
2064 /// is a resulting structure from
2065 /// previous items.
2066 /// \param D Current task directive.
2067 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
2068 /// /*part_id*/, captured_struct */*__context*/);
2069 /// \param SharedsTy A type which contains references the shared variables.
2070 /// \param Shareds Context with the list of shared variables from the \p
2071 /// TaskFunction.
2072 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
2073 /// otherwise.
2074 /// \param Data Additional data for task generation like tiednsee, final
2075 /// state, list of privates etc.
2076 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002077 const OMPLoopDirective &D, llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002078 QualType SharedsTy, Address Shareds, const Expr *IfCond,
2079 const OMPTaskDataTy &Data) override;
2080
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002081 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002082 /// reduction:
2083 /// \code
2084 ///
2085 /// static kmp_critical_name lock = { 0 };
2086 ///
2087 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
2088 /// ...
2089 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
2090 /// ...
2091 /// }
2092 ///
2093 /// ...
2094 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
2095 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
2096 /// RedList, reduce_func, &<lock>)) {
2097 /// case 1:
2098 /// ...
2099 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
2100 /// ...
2101 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
2102 /// break;
2103 /// case 2:
2104 /// ...
2105 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
2106 /// ...
2107 /// break;
2108 /// default:;
2109 /// }
2110 /// \endcode
2111 ///
2112 /// \param Privates List of private copies for original reduction arguments.
2113 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
2114 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
2115 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
2116 /// or 'operator binop(LHS, RHS)'.
2117 /// \param Options List of options for reduction codegen:
2118 /// WithNowait true if parent directive has also nowait clause, false
2119 /// otherwise.
2120 /// SimpleReduction Emit reduction operation only. Used for omp simd
2121 /// directive on the host.
2122 /// ReductionKind The kind of reduction to perform.
2123 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2124 ArrayRef<const Expr *> Privates,
2125 ArrayRef<const Expr *> LHSExprs,
2126 ArrayRef<const Expr *> RHSExprs,
2127 ArrayRef<const Expr *> ReductionOps,
2128 ReductionOptionsTy Options) override;
2129
2130 /// Emit a code for initialization of task reduction clause. Next code
2131 /// should be emitted for reduction:
2132 /// \code
2133 ///
2134 /// _task_red_item_t red_data[n];
2135 /// ...
2136 /// red_data[i].shar = &origs[i];
2137 /// red_data[i].size = sizeof(origs[i]);
2138 /// red_data[i].f_init = (void*)RedInit<i>;
2139 /// red_data[i].f_fini = (void*)RedDest<i>;
2140 /// red_data[i].f_comb = (void*)RedOp<i>;
2141 /// red_data[i].flags = <Flag_i>;
2142 /// ...
2143 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2144 /// \endcode
2145 ///
2146 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2147 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2148 /// \param Data Additional data for task generation like tiedness, final
2149 /// state, list of privates, reductions etc.
2150 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2151 ArrayRef<const Expr *> LHSExprs,
2152 ArrayRef<const Expr *> RHSExprs,
2153 const OMPTaskDataTy &Data) override;
2154
2155 /// Required to resolve existing problems in the runtime. Emits threadprivate
2156 /// variables to store the size of the VLAs/array sections for
2157 /// initializer/combiner/finalizer functions + emits threadprivate variable to
2158 /// store the pointer to the original reduction item for the custom
2159 /// initializer defined by declare reduction construct.
2160 /// \param RCG Allows to reuse an existing data for the reductions.
2161 /// \param N Reduction item for which fixups must be emitted.
2162 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2163 ReductionCodeGen &RCG, unsigned N) override;
2164
2165 /// Get the address of `void *` type of the privatue copy of the reduction
2166 /// item specified by the \p SharedLVal.
2167 /// \param ReductionsPtr Pointer to the reduction data returned by the
2168 /// emitTaskReductionInit function.
2169 /// \param SharedLVal Address of the original reduction item.
2170 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2171 llvm::Value *ReductionsPtr,
2172 LValue SharedLVal) override;
2173
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002174 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002175 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2176
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002177 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002178 /// \param CancelRegion Region kind for which the cancellation point must be
2179 /// emitted.
2180 ///
2181 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2182 OpenMPDirectiveKind CancelRegion) override;
2183
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002184 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002185 /// \param IfCond Condition in the associated 'if' clause, if it was
2186 /// specified, nullptr otherwise.
2187 /// \param CancelRegion Region kind for which the cancel must be emitted.
2188 ///
2189 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2190 const Expr *IfCond,
2191 OpenMPDirectiveKind CancelRegion) override;
2192
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002193 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002194 /// \param D Directive to emit.
2195 /// \param ParentName Name of the function that encloses the target region.
2196 /// \param OutlinedFn Outlined function value to be defined by this call.
2197 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2198 /// \param IsOffloadEntry True if the outlined function is an offload entry.
2199 /// \param CodeGen Code generation sequence for the \a D directive.
2200 /// An outlined function may not be an entry if, e.g. the if clause always
2201 /// evaluates to false.
2202 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
2203 StringRef ParentName,
2204 llvm::Function *&OutlinedFn,
2205 llvm::Constant *&OutlinedFnID,
2206 bool IsOffloadEntry,
2207 const RegionCodeGenTy &CodeGen) override;
2208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002209 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002210 /// code attempts offloading the execution to the device, an the event of
2211 /// a failure it executes the host version outlined in \a OutlinedFn.
2212 /// \param D Directive to emit.
2213 /// \param OutlinedFn Host version of the code to be offloaded.
2214 /// \param OutlinedFnID ID of host version of the code to be offloaded.
2215 /// \param IfCond Expression evaluated in if clause associated with the target
2216 /// directive, or null if no if clause is used.
2217 /// \param Device Expression evaluated in device clause associated with the
2218 /// target directive, or null if no device clause is used.
Alexey Bataevec7946e2019-09-23 14:06:51 +00002219 void
2220 emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2221 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
2222 const Expr *IfCond, const Expr *Device,
2223 llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2224 const OMPLoopDirective &D)>
2225 SizeEmitter) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002226
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002227 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002228 /// the function itself in case it is a valid device function. Returns true if
2229 /// \a GD was dealt with successfully.
2230 /// \param GD Function to scan.
2231 bool emitTargetFunctions(GlobalDecl GD) override;
2232
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002233 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002234 /// Returns true if \a GD was dealt with successfully.
2235 /// \param GD Variable declaration to emit.
2236 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2237
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002238 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002239 /// if it was emitted successfully.
2240 /// \param GD Global to scan.
2241 bool emitTargetGlobal(GlobalDecl GD) override;
2242
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002243 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002244 /// variables captured in a record which address is stored in \a
2245 /// CapturedStruct.
2246 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2247 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2248 /// \param CapturedVars A pointer to the record with the references to
2249 /// variables used in \a OutlinedFn function.
2250 ///
2251 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002252 SourceLocation Loc, llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002253 ArrayRef<llvm::Value *> CapturedVars) override;
2254
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002255 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002256 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2257 /// for num_teams clause.
2258 /// \param NumTeams An integer expression of teams.
2259 /// \param ThreadLimit An integer expression of threads.
2260 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2261 const Expr *ThreadLimit, SourceLocation Loc) override;
2262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002263 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002264 /// \param D Directive to emit.
2265 /// \param IfCond Expression evaluated in if clause associated with the
2266 /// target directive, or null if no device clause is used.
2267 /// \param Device Expression evaluated in device clause associated with the
2268 /// target directive, or null if no device clause is used.
2269 /// \param Info A record used to store information that needs to be preserved
2270 /// until the region is closed.
2271 void emitTargetDataCalls(CodeGenFunction &CGF,
2272 const OMPExecutableDirective &D, const Expr *IfCond,
2273 const Expr *Device, const RegionCodeGenTy &CodeGen,
2274 TargetDataInfo &Info) override;
2275
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002276 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002277 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2278 /// \param D Directive to emit.
2279 /// \param IfCond Expression evaluated in if clause associated with the target
2280 /// directive, or null if no if clause is used.
2281 /// \param Device Expression evaluated in device clause associated with the
2282 /// target directive, or null if no device clause is used.
2283 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2284 const OMPExecutableDirective &D,
2285 const Expr *IfCond,
2286 const Expr *Device) override;
2287
2288 /// Emit initialization for doacross loop nesting support.
2289 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002290 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2291 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002292
2293 /// Emit code for doacross ordered directive with 'depend' clause.
2294 /// \param C 'depend' clause with 'sink|source' dependency kind.
2295 void emitDoacrossOrdered(CodeGenFunction &CGF,
2296 const OMPDependClause *C) override;
2297
2298 /// Translates the native parameter of outlined function if this is required
2299 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002300 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002301 /// \param NativeParam Parameter itself.
2302 const VarDecl *translateParameter(const FieldDecl *FD,
2303 const VarDecl *NativeParam) const override;
2304
2305 /// Gets the address of the native argument basing on the address of the
2306 /// target-specific parameter.
2307 /// \param NativeParam Parameter itself.
2308 /// \param TargetParam Corresponding target-specific parameter.
2309 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2310 const VarDecl *TargetParam) const override;
Alexey Bataev4f680db2019-03-19 16:41:16 +00002311
2312 /// Gets the OpenMP-specific address of the local variable.
2313 Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2314 const VarDecl *VD) override {
2315 return Address::invalid();
2316 }
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002317};
2318
Alexey Bataev23b69422014-06-18 07:08:49 +00002319} // namespace CodeGen
2320} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002321
2322#endif