blob: 3f842ce96407fea8a4c7778a83eff67ca682687d [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"
Alexey Bataev62b63b12015-03-10 07:28:44 +000018#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000019#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000020#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000021#include "llvm/ADT/DenseMap.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000022#include "llvm/ADT/StringMap.h"
Alexey Bataev2a6f3f52018-11-07 19:11:14 +000023#include "llvm/ADT/StringSet.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000024#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000025#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000026
27namespace llvm {
28class ArrayType;
29class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000030class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000031class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000032class StructType;
33class Type;
34class Value;
35} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000036
Alexey Bataev9959db52014-05-06 10:08:46 +000037namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000038class Expr;
Samuel Antaoee8fb302016-01-06 13:42:12 +000039class GlobalDecl;
Alexey Bataev8b427062016-05-25 12:36:08 +000040class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000041class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000042class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000043class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000044class OMPDeclareReductionDecl;
45class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000046
Alexey Bataev9959db52014-05-06 10:08:46 +000047namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000048class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000049class CodeGenFunction;
50class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000051
Alexey Bataev14fa1c62016-03-29 05:34:15 +000052/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
53/// region.
54class PrePostActionTy {
55public:
56 explicit PrePostActionTy() {}
57 virtual void Enter(CodeGenFunction &CGF) {}
58 virtual void Exit(CodeGenFunction &CGF) {}
59 virtual ~PrePostActionTy() {}
60};
61
62/// Class provides a way to call simple version of codegen for OpenMP region, or
63/// an advanced with possible pre|post-actions in codegen.
64class RegionCodeGenTy final {
65 intptr_t CodeGen;
66 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
67 CodeGenTy Callback;
68 mutable PrePostActionTy *PrePostAction;
69 RegionCodeGenTy() = delete;
70 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
71 template <typename Callable>
72 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
73 PrePostActionTy &Action) {
74 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
75 }
76
77public:
78 template <typename Callable>
79 RegionCodeGenTy(
80 Callable &&CodeGen,
81 typename std::enable_if<
82 !std::is_same<typename std::remove_reference<Callable>::type,
83 RegionCodeGenTy>::value>::type * = nullptr)
84 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
85 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
86 PrePostAction(nullptr) {}
87 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
88 void operator()(CodeGenFunction &CGF) const;
89};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000090
Alexey Bataev24b5bae2016-04-28 09:23:51 +000091struct OMPTaskDataTy final {
92 SmallVector<const Expr *, 4> PrivateVars;
93 SmallVector<const Expr *, 4> PrivateCopies;
94 SmallVector<const Expr *, 4> FirstprivateVars;
95 SmallVector<const Expr *, 4> FirstprivateCopies;
96 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000097 SmallVector<const Expr *, 4> LastprivateVars;
98 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +000099 SmallVector<const Expr *, 4> ReductionVars;
100 SmallVector<const Expr *, 4> ReductionCopies;
101 SmallVector<const Expr *, 4> ReductionOps;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000102 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
103 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
104 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000105 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000106 llvm::Value *Reductions = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000107 unsigned NumberOfParts = 0;
108 bool Tied = true;
109 bool Nogroup = false;
110};
111
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000112/// Class intended to support codegen of all kind of the reduction clauses.
113class ReductionCodeGen {
114private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000115 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000116 struct ReductionData {
117 /// Reference to the original shared item.
118 const Expr *Ref = nullptr;
119 /// Helper expression for generation of private copy.
120 const Expr *Private = nullptr;
121 /// Helper expression for generation reduction operation.
122 const Expr *ReductionOp = nullptr;
123 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
124 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
125 };
126 /// List of reduction-based clauses.
127 SmallVector<ReductionData, 4> ClausesData;
128
129 /// List of addresses of original shared variables/expressions.
130 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
131 /// Sizes of the reduction items in chars.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000132 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000133 /// Base declarations for the reduction items.
134 SmallVector<const VarDecl *, 4> BaseDecls;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000135
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000136 /// Emits lvalue for shared expression.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000137 LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
138 /// Emits upper bound for shared expression (if array section).
139 LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
140 /// Performs aggregate initialization.
141 /// \param N Number of reduction item in the common list.
142 /// \param PrivateAddr Address of the corresponding private item.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000143 /// \param SharedLVal Address of the original shared variable.
144 /// \param DRD Declare reduction construct used for reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000145 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000146 Address PrivateAddr, LValue SharedLVal,
147 const OMPDeclareReductionDecl *DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000148
149public:
150 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
151 ArrayRef<const Expr *> Privates,
152 ArrayRef<const Expr *> ReductionOps);
153 /// Emits lvalue for a reduction item.
154 /// \param N Number of the reduction item.
155 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
156 /// Emits the code for the variable-modified type, if required.
157 /// \param N Number of the reduction item.
158 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
159 /// Emits the code for the variable-modified type, if required.
160 /// \param N Number of the reduction item.
161 /// \param Size Size of the type in chars.
162 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
163 /// Performs initialization of the private copy for the reduction item.
164 /// \param N Number of the reduction item.
165 /// \param PrivateAddr Address of the corresponding private item.
166 /// \param DefaultInit Default initialization sequence that should be
167 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000168 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000169 void
170 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
171 LValue SharedLVal,
172 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000173 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000174 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000175 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000176 /// \param N Number of the reduction item.
177 /// \param PrivateAddr Address of the corresponding private item.
178 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000179 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000180 /// address in normal operations.
181 /// \param N Number of the reduction item.
182 /// \param PrivateAddr Address of the corresponding private item.
183 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
184 Address PrivateAddr);
185 /// Returns LValue for the reduction item.
186 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000187 /// Returns the size of the reduction item (in chars and total number of
188 /// elements in the item), or nullptr, if the size is a constant.
189 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
190 return Sizes[N];
191 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000192 /// Returns the base declaration of the reduction item.
193 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
Alexey Bataev1c44e152018-03-06 18:59:43 +0000194 /// Returns the base declaration of the reduction item.
195 const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000196 /// Returns true if the initialization of the reduction item uses initializer
197 /// from declare reduction construct.
198 bool usesReductionInitializer(unsigned N) const;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000199};
200
Alexey Bataev9959db52014-05-06 10:08:46 +0000201class CGOpenMPRuntime {
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000202public:
203 /// Allows to disable automatic handling of functions used in target regions
204 /// as those marked as `omp declare target`.
205 class DisableAutoDeclareTargetRAII {
206 CodeGenModule &CGM;
207 bool SavedShouldMarkAsGlobal;
208
209 public:
210 DisableAutoDeclareTargetRAII(CodeGenModule &CGM);
211 ~DisableAutoDeclareTargetRAII();
212 };
213
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000214protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000215 CodeGenModule &CGM;
Alexey Bataev18fa2322018-05-02 14:20:50 +0000216 StringRef FirstSeparator, Separator;
217
218 /// Constructor allowing to redefine the name separator for the variables.
219 explicit CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
220 StringRef Separator);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000221
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000222 /// Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000223 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000224 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000225 uint64_t Size, int32_t Flags,
226 llvm::GlobalValue::LinkageTypes Linkage);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000227
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000228 /// Helper to emit outlined function for 'target' directive.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000229 /// \param D Directive to emit.
230 /// \param ParentName Name of the function that encloses the target region.
231 /// \param OutlinedFn Outlined function value to be defined by this call.
232 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
233 /// \param IsOffloadEntry True if the outlined function is an offload entry.
234 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000235 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000236 /// evaluates to false.
237 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
238 StringRef ParentName,
239 llvm::Function *&OutlinedFn,
240 llvm::Constant *&OutlinedFnID,
241 bool IsOffloadEntry,
242 const RegionCodeGenTy &CodeGen);
243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000244 /// Emits code for OpenMP 'if' clause using specified \a CodeGen
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000245 /// function. Here is the logic:
246 /// if (Cond) {
247 /// ThenGen();
248 /// } else {
249 /// ElseGen();
250 /// }
251 void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
252 const RegionCodeGenTy &ThenGen,
253 const RegionCodeGenTy &ElseGen);
254
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000255 /// Emits object of ident_t type with info for source location.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000256 /// \param Flags Flags for OpenMP location.
257 ///
258 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
259 unsigned Flags = 0);
260
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000261 /// Returns pointer to ident_t type.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000262 llvm::Type *getIdentTyPointerTy();
263
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000264 /// Gets thread id value for the current thread.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000265 ///
266 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
267
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000268 /// Get the function name of an outlined region.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000269 // The name can be customized depending on the target.
270 //
271 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
272
Alexey Bataev3c595a62017-08-14 15:01:03 +0000273 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
James Y Knight9871db02019-02-05 16:42:33 +0000274 void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
275 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000276 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000277
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000278 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000279 /// stored.
280 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
281
Alexey Bataevfd006c42018-10-05 15:08:53 +0000282 void setLocThreadIdInsertPt(CodeGenFunction &CGF,
283 bool AtCurrentPoint = false);
284 void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
285
Alexey Bataevceeaa482018-11-21 21:04:34 +0000286 /// Check if the default location must be constant.
287 /// Default is false to support OMPT/OMPD.
288 virtual bool isDefaultLocationConstant() const { return false; }
289
290 /// Returns additional flags that can be stored in reserved_2 field of the
291 /// default location.
292 virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
293
Alexey Bataevc3028ca2018-12-04 15:03:25 +0000294 /// Returns default flags for the barriers depending on the directive, for
295 /// which this barier is going to be emitted.
296 static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
297
Alexey Bataeva1166022018-11-27 21:24:54 +0000298 /// Get the LLVM type for the critical name.
299 llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
300
301 /// Returns corresponding lock object for the specified critical region
302 /// name. If the lock object does not exist it is created, otherwise the
303 /// reference to the existing copy is returned.
304 /// \param CriticalName Name of the critical region.
305 ///
306 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
307
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000308private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000309 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000310 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000311 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataevceeaa482018-11-21 21:04:34 +0000312 using FlagsTy = std::pair<unsigned, unsigned>;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000313 /// Map of flags and corresponding default locations.
Alexey Bataevceeaa482018-11-21 21:04:34 +0000314 using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
Alexey Bataev15007ba2014-05-07 06:18:01 +0000315 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000316 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000317
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000318 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000319 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000320 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000321 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
322 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000323 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000324 /// Original representation is:
325 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000326 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000327 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000328 struct DebugLocThreadIdTy {
329 llvm::Value *DebugLoc;
330 llvm::Value *ThreadID;
Alexey Bataevfd006c42018-10-05 15:08:53 +0000331 /// Insert point for the service instructions.
332 llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000333 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000334 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000335 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
336 OpenMPLocThreadIDMapTy;
337 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000338 /// Map of UDRs and corresponding combiner/initializer.
339 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
340 std::pair<llvm::Function *, llvm::Function *>>
341 UDRMapTy;
342 UDRMapTy UDRMap;
343 /// Map of functions and locally defined UDRs.
344 typedef llvm::DenseMap<llvm::Function *,
345 SmallVector<const OMPDeclareReductionDecl *, 4>>
346 FunctionUDRMapTy;
347 FunctionUDRMapTy FunctionUDRMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000348 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000349 /// kmp_critical_name[8];
350 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000351 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000352 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
353 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
354 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
355 /// variables.
356 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
357 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000358 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000359 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000360 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000361 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000362 /// void * shareds; /**< pointer to block of pointers to
363 /// shared vars */
364 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
365 /// executing task */
366 /// kmp_int32 part_id; /**< part id for the task */
367 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
368 /// deconstructors of firstprivate C++ objects */
369 /// } kmp_task_t;
370 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000371 /// Saved kmp_task_t for task directive.
372 QualType SavedKmpTaskTQTy;
373 /// Saved kmp_task_t for taskloop-based directive.
374 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000375 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000376 /// kmp_intptr_t base_addr;
377 /// size_t len;
378 /// struct {
379 /// bool in:1;
380 /// bool out:1;
381 /// } flags;
382 /// } kmp_depend_info_t;
383 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000384 /// struct kmp_dim { // loop bounds info casted to kmp_int64
385 /// kmp_int64 lo; // lower
386 /// kmp_int64 up; // upper
387 /// kmp_int64 st; // stride
388 /// };
389 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000390 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000391 /// void *addr; // Pointer to the offload entry info.
392 /// // (function or global)
393 /// char *name; // Name of the function or global.
394 /// size_t size; // Size of the entry info (0 if it a function).
395 /// };
396 QualType TgtOffloadEntryQTy;
397 /// struct __tgt_device_image{
398 /// void *ImageStart; // Pointer to the target code start.
399 /// void *ImageEnd; // Pointer to the target code end.
400 /// // We also add the host entries to the device image, as it may be useful
401 /// // for the target runtime to have access to that information.
402 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
403 /// // the entries.
404 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
405 /// // entries (non inclusive).
406 /// };
407 QualType TgtDeviceImageQTy;
408 /// struct __tgt_bin_desc{
409 /// int32_t NumDevices; // Number of devices supported.
410 /// __tgt_device_image *DeviceImages; // Arrays of device images
411 /// // (one per device).
412 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
413 /// // entries.
414 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
415 /// // entries (non inclusive).
416 /// };
417 QualType TgtBinaryDescriptorQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000418 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000419 /// far.
420 class OffloadEntriesInfoManagerTy {
421 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000422
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000423 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000424 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000425
426 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000427 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000428 class OffloadEntryInfo {
429 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000430 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000431 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000432 /// Entry is a target region.
433 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000434 /// Entry is a declare target variable.
435 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000436 /// Invalid entry info.
437 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000438 };
439
Alexey Bataev03f270c2018-03-30 18:31:07 +0000440 protected:
441 OffloadEntryInfo() = delete;
442 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000443 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000444 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000445 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000446 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000447
Alexey Bataev03f270c2018-03-30 18:31:07 +0000448 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000449 bool isValid() const { return Order != ~0u; }
450 unsigned getOrder() const { return Order; }
451 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000452 uint32_t getFlags() const { return Flags; }
453 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
454 llvm::Constant *getAddress() const {
455 return cast_or_null<llvm::Constant>(Addr);
456 }
457 void setAddress(llvm::Constant *V) {
458 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
459 Addr = V;
460 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000461 static bool classof(const OffloadEntryInfo *Info) { return true; }
462
Samuel Antaof83efdb2017-01-05 16:02:49 +0000463 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000464 /// Address of the entity that has to be mapped for offloading.
465 llvm::WeakTrackingVH Addr;
466
Samuel Antaof83efdb2017-01-05 16:02:49 +0000467 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000468 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000469
470 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000471 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000472
Alexey Bataev03f270c2018-03-30 18:31:07 +0000473 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000474 };
475
Alexey Bataev03f270c2018-03-30 18:31:07 +0000476 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000477 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000478 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000479 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000480 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000481
Alexey Bataev03f270c2018-03-30 18:31:07 +0000482 //
483 // Target region entries related.
484 //
485
486 /// Kind of the target registry entry.
487 enum OMPTargetRegionEntryKind : uint32_t {
488 /// Mark the entry as target region.
489 OMPTargetRegionEntryTargetRegion = 0x0,
490 /// Mark the entry as a global constructor.
491 OMPTargetRegionEntryCtor = 0x02,
492 /// Mark the entry as a global destructor.
493 OMPTargetRegionEntryDtor = 0x04,
494 };
495
496 /// Target region entries info.
497 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
498 /// Address that can be used as the ID of the entry.
499 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000500
501 public:
502 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000503 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000504 explicit OffloadEntryInfoTargetRegion(unsigned Order,
505 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000506 llvm::Constant *ID,
507 OMPTargetRegionEntryKind Flags)
508 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000509 ID(ID) {
510 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000511 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000512
513 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000514 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000515 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000516 ID = V;
517 }
518 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000519 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000520 }
521 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000522
523 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000524 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
525 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000526 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000527 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000528 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
529 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000530 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000531 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000532 /// Return true if a target region entry with the provided information
533 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000534 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000535 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000536 /// brief Applies action \a Action on all registered entries.
537 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000538 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000539 OffloadTargetRegionEntryInfoActTy;
540 void actOnTargetRegionEntriesInfo(
541 const OffloadTargetRegionEntryInfoActTy &Action);
542
Alexey Bataev03f270c2018-03-30 18:31:07 +0000543 //
544 // Device global variable entries related.
545 //
546
547 /// Kind of the global variable entry..
548 enum OMPTargetGlobalVarEntryKind : uint32_t {
549 /// Mark the entry as a to declare target.
550 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000551 /// Mark the entry as a to declare target link.
552 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000553 };
554
555 /// Device global variable entries info.
556 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
557 /// Type of the global variable.
558 CharUnits VarSize;
559 llvm::GlobalValue::LinkageTypes Linkage;
560
561 public:
562 OffloadEntryInfoDeviceGlobalVar()
563 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
564 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
565 OMPTargetGlobalVarEntryKind Flags)
566 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
567 explicit OffloadEntryInfoDeviceGlobalVar(
568 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
569 OMPTargetGlobalVarEntryKind Flags,
570 llvm::GlobalValue::LinkageTypes Linkage)
571 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
572 VarSize(VarSize), Linkage(Linkage) {
573 setAddress(Addr);
574 }
575
576 CharUnits getVarSize() const { return VarSize; }
577 void setVarSize(CharUnits Size) { VarSize = Size; }
578 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
579 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
580 static bool classof(const OffloadEntryInfo *Info) {
581 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
582 }
583 };
584
585 /// Initialize device global variable entry.
586 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
587 OMPTargetGlobalVarEntryKind Flags,
588 unsigned Order);
589
590 /// Register device global variable entry.
591 void
592 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
593 CharUnits VarSize,
594 OMPTargetGlobalVarEntryKind Flags,
595 llvm::GlobalValue::LinkageTypes Linkage);
596 /// Checks if the variable with the given name has been registered already.
597 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
598 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
599 }
600 /// Applies action \a Action on all registered entries.
601 typedef llvm::function_ref<void(StringRef,
602 const OffloadEntryInfoDeviceGlobalVar &)>
603 OffloadDeviceGlobalVarEntryInfoActTy;
604 void actOnDeviceGlobalVarEntriesInfo(
605 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
606
Samuel Antaoee8fb302016-01-06 13:42:12 +0000607 private:
608 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000609 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000610 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000611 OffloadEntriesTargetRegionPerLine;
612 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
613 OffloadEntriesTargetRegionPerParentName;
614 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
615 OffloadEntriesTargetRegionPerFile;
616 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
617 OffloadEntriesTargetRegionPerDevice;
618 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
619 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000620 /// Storage for device global variable entries kind. The storage is to be
621 /// indexed by mangled name.
622 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
623 OffloadEntriesDeviceGlobalVarTy;
624 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000625 };
626 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
627
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000628 bool ShouldMarkAsGlobal = true;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000629 /// List of the emitted functions.
630 llvm::StringSet<> AlreadyEmittedTargetFunctions;
631 /// List of the global variables with their addresses that should not be
632 /// emitted for the target.
633 llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000634
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000635 /// List of variables that can become declare target implicitly and, thus,
636 /// must be emitted.
637 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
638
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +0000639 /// Flag for keeping track of weather a requires unified_shared_memory
640 /// directive is present.
641 bool HasRequiresUnifiedSharedMemory = false;
642
643 /// Flag for keeping track of weather a target region has been emitted.
644 bool HasEmittedTargetRegion = false;
645
646 /// Flag for keeping track of weather a device routine has been emitted.
647 /// Device routines are specific to the
648 bool HasEmittedDeclareTargetRegion = false;
649
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000650 /// Creates and registers offloading binary descriptor for the current
Samuel Antaoee8fb302016-01-06 13:42:12 +0000651 /// compilation unit. The function that does the registration is returned.
652 llvm::Function *createOffloadingBinaryDescriptorRegistration();
653
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000654 /// Creates all the offload entries in the current compilation unit
Samuel Antaoee8fb302016-01-06 13:42:12 +0000655 /// along with the associated metadata.
656 void createOffloadEntriesAndInfoMetadata();
657
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000658 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000659 /// metadata.
660 void loadOffloadInfoMetadata();
661
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000662 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000663 QualType getTgtOffloadEntryQTy();
664
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000665 /// Returns __tgt_device_image type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000666 QualType getTgtDeviceImageQTy();
667
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000668 /// Returns __tgt_bin_desc type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000669 QualType getTgtBinaryDescriptorQTy();
670
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000671 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000672 /// found along the way.
673 /// \param S Starting statement.
674 /// \param ParentName Name of the function declaration that is being scanned.
675 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000676
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000677 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000678 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000679
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000680 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000681 llvm::Type *getKmpc_MicroPointerTy();
682
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000683 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000684 /// \param Function OpenMP runtime function.
685 /// \return Specified function.
James Y Knight9871db02019-02-05 16:42:33 +0000686 llvm::FunctionCallee createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000687
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000688 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000689 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000690 llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
691 bool IVSigned);
Alexander Musman21212e42015-03-13 10:38:23 +0000692
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000693 /// Returns __kmpc_dispatch_init_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000694 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000695 llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize,
696 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000697
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000698 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000699 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000700 llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
701 bool IVSigned);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000702
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000703 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000704 /// size \a IVSize and sign \a IVSigned.
James Y Knight9871db02019-02-05 16:42:33 +0000705 llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
706 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000707
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000708 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000709 /// return threadprivate cache object. This object is a pointer's worth of
710 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000711 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000712 /// \return Cache variable for the specified threadprivate.
713 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
714
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000715 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000716 /// internal global variable with the specified Name. The created variable has
717 /// linkage CommonLinkage by default and is initialized by null value.
718 /// \param Ty Type of the global variable. If it is exist already the type
719 /// must be the same.
720 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000721 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000722 const llvm::Twine &Name,
723 unsigned AddressSpace = 0);
Alexey Bataev97720002014-11-11 04:05:39 +0000724
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000725 /// Set of threadprivate variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000726 llvm::StringSet<> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000727
Alexey Bataev34f8a702018-03-28 14:28:54 +0000728 /// Set of declare target variables with the generated initializer.
Alexey Bataev2a6f3f52018-11-07 19:11:14 +0000729 llvm::StringSet<> DeclareTargetWithDefinition;
Alexey Bataev34f8a702018-03-28 14:28:54 +0000730
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000731 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000732 /// \param VDAddr Address of the global variable \a VD.
733 /// \param Ctor Pointer to a global init function for \a VD.
734 /// \param CopyCtor Pointer to a global copy function for \a VD.
735 /// \param Dtor Pointer to a global destructor function for \a VD.
736 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000737 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000738 llvm::Value *Ctor, llvm::Value *CopyCtor,
739 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000740
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000741 struct TaskResultTy {
742 llvm::Value *NewTask = nullptr;
James Y Knight9871db02019-02-05 16:42:33 +0000743 llvm::Function *TaskEntry = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000744 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000745 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000746 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000747 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000748 };
749 /// Emit task region for the task directive. The task region is emitted in
750 /// several steps:
751 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
752 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
753 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
754 /// function:
755 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
756 /// TaskFunction(gtid, tt->part_id, tt->shareds);
757 /// return 0;
758 /// }
759 /// 2. Copy a list of shared variables to field shareds of the resulting
760 /// structure kmp_task_t returned by the previous call (if any).
761 /// 3. Copy a pointer to destructions function to field destructions of the
762 /// resulting structure kmp_task_t.
763 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000764 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
765 /// /*part_id*/, captured_struct */*__context*/);
766 /// \param SharedsTy A type which contains references the shared variables.
767 /// \param Shareds Context with the list of shared variables from the \p
768 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000769 /// \param Data Additional data for task generation like tiednsee, final
770 /// state, list of privates etc.
771 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
772 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +0000773 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000774 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000775
Alexey Bataev1af5bd52019-03-05 17:47:18 +0000776 /// Returns default address space for the constant firstprivates, 0 by
777 /// default.
778 virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; }
779
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000780public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000781 explicit CGOpenMPRuntime(CodeGenModule &CGM)
782 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000783 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000784 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000785
Alexey Bataev5c427362019-04-10 19:11:33 +0000786 /// Checks if the \p Body is the \a CompoundStmt and returns its child
787 /// statement iff there is only one that is not evaluatable at the compile
788 /// time.
789 static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
790
Alexey Bataev18fa2322018-05-02 14:20:50 +0000791 /// Get the platform-specific name separator.
792 std::string getName(ArrayRef<StringRef> Parts) const;
793
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000794 /// Emit code for the specified user defined reduction construct.
795 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
796 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000797 /// Get combiner/initializer for the specified user-defined reduction, if any.
798 virtual std::pair<llvm::Function *, llvm::Function *>
799 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000800
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000801 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000802 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
803 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000804 /// \param D OpenMP directive.
805 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000806 /// \param InnermostKind Kind of innermost directive (for simple directives it
807 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000808 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000809 virtual llvm::Function *emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000810 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
811 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
812
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000813 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000814 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
815 /// kmp_int32 BoundID, struct context_vars*).
816 /// \param D OpenMP directive.
817 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
818 /// \param InnermostKind Kind of innermost directive (for simple directives it
819 /// is a directive itself, for combined - its innermost directive).
820 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +0000821 virtual llvm::Function *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000822 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
823 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000824
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000825 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000826 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
827 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000828 /// \param D OpenMP directive.
829 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000830 /// \param PartIDVar Variable for partition id in the current OpenMP untied
831 /// task region.
832 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000833 /// \param InnermostKind Kind of innermost directive (for simple directives it
834 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000835 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000836 /// \param Tied true if task is generated for tied task, false otherwise.
837 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
838 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000839 ///
James Y Knight9871db02019-02-05 16:42:33 +0000840 virtual llvm::Function *emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000841 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000842 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
843 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
844 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000845
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000846 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000847 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000848 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000849
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000850 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000851 /// variables captured in a record which address is stored in \a
852 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000853 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000854 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000855 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000856 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000857 /// \param IfCond Condition in the associated 'if' clause, if it was
858 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000859 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000860 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +0000861 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000862 ArrayRef<llvm::Value *> CapturedVars,
863 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000864
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000865 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000866 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000867 /// \param CriticalOpGen Generator for the statement associated with the given
868 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000869 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000870 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000871 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000872 SourceLocation Loc,
873 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000874
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000875 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000876 /// \param MasterOpGen Generator for the statement associated with the given
877 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000878 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000879 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000880 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000881
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000882 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000883 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000884
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000885 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000886 /// \param TaskgroupOpGen Generator for the statement associated with the
887 /// given taskgroup region.
888 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
889 const RegionCodeGenTy &TaskgroupOpGen,
890 SourceLocation Loc);
891
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000892 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000893 /// \param SingleOpGen Generator for the statement associated with the given
894 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000895 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000896 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000897 SourceLocation Loc,
898 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000899 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000900 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000901 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000902
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000903 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000904 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000905 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000906 virtual void emitOrderedRegion(CodeGenFunction &CGF,
907 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000908 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000909
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000910 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +0000911 /// \param Kind Directive for which this implicit barrier call must be
912 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000913 /// \param EmitChecks true if need to emit checks for cancellation barriers.
914 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
915 /// runtime class decides which one to emit (simple or with cancellation
916 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000917 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000918 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000919 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000920 bool EmitChecks = true,
921 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000922
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000923 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +0000924 /// This kind of worksharing directive is emitted without outer loop.
925 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
926 /// \param Chunked True if chunk is specified in the clause.
927 ///
928 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
929 bool Chunked) const;
930
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000931 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000932 /// This kind of distribute directive is emitted without outer loop.
933 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
934 /// \param Chunked True if chunk is specified in the clause.
935 ///
936 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
937 bool Chunked) const;
938
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000939 /// Check if the specified \a ScheduleKind is static chunked.
940 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
941 /// \param Chunked True if chunk is specified in the clause.
942 ///
943 virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
944 bool Chunked) const;
945
946 /// Check if the specified \a ScheduleKind is static non-chunked.
947 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
948 /// \param Chunked True if chunk is specified in the clause.
949 ///
950 virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
951 bool Chunked) const;
952
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000953 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000954 /// This kind of worksharing directive is emitted without outer loop.
955 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
956 ///
957 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
958
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000959 /// struct with the values to be passed to the dispatch runtime function
960 struct DispatchRTInput {
961 /// Loop lower bound
962 llvm::Value *LB = nullptr;
963 /// Loop upper bound
964 llvm::Value *UB = nullptr;
965 /// Chunk size specified using 'schedule' clause (nullptr if chunk
966 /// was not specified)
967 llvm::Value *Chunk = nullptr;
968 DispatchRTInput() = default;
969 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
970 : LB(LB), UB(UB), Chunk(Chunk) {}
971 };
972
973 /// Call the appropriate runtime routine to initialize it before start
974 /// of loop.
975
976 /// This is used for non static scheduled types and when the ordered
977 /// clause is present on the loop construct.
978 /// Depending on the loop schedule, it is necessary to call some runtime
979 /// routine before start of the OpenMP loop to get the loop upper / lower
980 /// bounds \a LB and \a UB and stride \a ST.
981 ///
982 /// \param CGF Reference to current CodeGenFunction.
983 /// \param Loc Clang source location.
984 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
985 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000986 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000987 /// \param Ordered true if loop is ordered, false otherwise.
988 /// \param DispatchValues struct containing llvm values for lower bound, upper
989 /// bound, and chunk expression.
990 /// For the default (nullptr) value, the chunk 1 will be used.
991 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000992 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000993 const OpenMPScheduleTy &ScheduleKind,
994 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000995 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000996
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000997 /// Struct with the values to be passed to the static runtime function
998 struct StaticRTInput {
999 /// Size of the iteration variable in bits.
1000 unsigned IVSize = 0;
1001 /// Sign of the iteration variable.
1002 bool IVSigned = false;
1003 /// true if loop is ordered, false otherwise.
1004 bool Ordered = false;
1005 /// Address of the output variable in which the flag of the last iteration
1006 /// is returned.
1007 Address IL = Address::invalid();
1008 /// Address of the output variable in which the lower iteration number is
1009 /// returned.
1010 Address LB = Address::invalid();
1011 /// Address of the output variable in which the upper iteration number is
1012 /// returned.
1013 Address UB = Address::invalid();
1014 /// Address of the output variable in which the stride value is returned
1015 /// necessary to generated the static_chunked scheduled loop.
1016 Address ST = Address::invalid();
1017 /// Value of the chunk for the static_chunked scheduled loop. For the
1018 /// default (nullptr) value, the chunk 1 will be used.
1019 llvm::Value *Chunk = nullptr;
1020 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
1021 Address LB, Address UB, Address ST,
1022 llvm::Value *Chunk = nullptr)
1023 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
1024 UB(UB), ST(ST), Chunk(Chunk) {}
1025 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001026 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +00001027 /// of loop.
1028 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001029 /// This is used only in case of static schedule, when the user did not
1030 /// specify a ordered clause on the loop construct.
1031 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +00001032 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001033 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +00001034 ///
1035 /// \param CGF Reference to current CodeGenFunction.
1036 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001037 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001038 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001039 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +00001040 ///
John McCall7f416cc2015-09-08 08:05:57 +00001041 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001042 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001043 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001044 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00001045
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001046 ///
1047 /// \param CGF Reference to current CodeGenFunction.
1048 /// \param Loc Clang source location.
1049 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001050 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001051 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001052 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
1053 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001054 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001055 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001057 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001058 /// iteration of the ordered loop with the dynamic scheduling.
1059 ///
1060 /// \param CGF Reference to current CodeGenFunction.
1061 /// \param Loc Clang source location.
1062 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001063 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001064 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001065 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1066 SourceLocation Loc, unsigned IVSize,
1067 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001068
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001069 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001070 /// all the work with current loop.
1071 ///
1072 /// \param CGF Reference to current CodeGenFunction.
1073 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001074 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001075 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001076 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1077 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001078
Alexander Musman92bdaab2015-03-12 13:37:50 +00001079 /// Call __kmpc_dispatch_next(
1080 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1081 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1082 /// kmp_int[32|64] *p_stride);
1083 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001084 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001085 /// \param IL Address of the output variable in which the flag of the
1086 /// last iteration is returned.
1087 /// \param LB Address of the output variable in which the lower iteration
1088 /// number is returned.
1089 /// \param UB Address of the output variable in which the upper iteration
1090 /// number is returned.
1091 /// \param ST Address of the output variable in which the stride value is
1092 /// returned.
1093 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1094 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001095 Address IL, Address LB,
1096 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001097
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001098 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001099 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1100 /// clause.
1101 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001102 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1103 llvm::Value *NumThreads,
1104 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001105
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001106 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001107 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1108 virtual void emitProcBindClause(CodeGenFunction &CGF,
1109 OpenMPProcBindClauseKind ProcBind,
1110 SourceLocation Loc);
1111
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001112 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001113 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001114 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001115 /// \param VDAddr Address of the global variable \a VD.
1116 /// \param Loc Location of the reference to threadprivate var.
1117 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001118 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1119 const VarDecl *VD,
1120 Address VDAddr,
1121 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001122
Alexey Bataev92327c52018-03-26 16:40:55 +00001123 /// Returns the address of the variable marked as declare target with link
Gheorghe-Teodor Bercea0034e842019-06-20 18:04:47 +00001124 /// clause OR as declare target with to clause and unified memory.
1125 virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001126
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001127 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001128 /// a call to runtime library which adds initial value to the newly created
1129 /// threadprivate variable (if it is not constant) and registers destructor
1130 /// for the variable (if any).
1131 /// \param VD Threadprivate variable.
1132 /// \param VDAddr Address of the global variable \a VD.
1133 /// \param Loc Location of threadprivate declaration.
1134 /// \param PerformInit true if initialization expression is not constant.
1135 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001136 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001137 SourceLocation Loc, bool PerformInit,
1138 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001139
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001140 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001141 /// \param VD Declare target variable.
1142 /// \param Addr Address of the global variable \a VD.
1143 /// \param PerformInit true if initialization expression is not constant.
1144 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1145 llvm::GlobalVariable *Addr,
1146 bool PerformInit);
1147
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001148 /// Creates artificial threadprivate variable with name \p Name and type \p
1149 /// VarType.
1150 /// \param VarType Type of the artificial threadprivate variable.
1151 /// \param Name Name of the artificial threadprivate variable.
1152 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1153 QualType VarType,
1154 StringRef Name);
1155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001156 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001157 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001158 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1159 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001160
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001161 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001162 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001163 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1164 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1165 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1166 /// function:
1167 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1168 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1169 /// return 0;
1170 /// }
1171 /// 2. Copy a list of shared variables to field shareds of the resulting
1172 /// structure kmp_task_t returned by the previous call (if any).
1173 /// 3. Copy a pointer to destructions function to field destructions of the
1174 /// resulting structure kmp_task_t.
1175 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1176 /// kmp_task_t *new_task), where new_task is a resulting structure from
1177 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001178 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001179 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1180 /// /*part_id*/, captured_struct */*__context*/);
1181 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001182 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001183 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001184 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1185 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001186 /// \param Data Additional data for task generation like tiednsee, final
1187 /// state, list of privates etc.
1188 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1189 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001190 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001191 Address Shareds, const Expr *IfCond,
1192 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001193
Alexey Bataev7292c292016-04-25 12:22:29 +00001194 /// Emit task region for the taskloop directive. The taskloop region is
1195 /// emitted in several steps:
1196 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1197 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1198 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1199 /// function:
1200 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1201 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1202 /// return 0;
1203 /// }
1204 /// 2. Copy a list of shared variables to field shareds of the resulting
1205 /// structure kmp_task_t returned by the previous call (if any).
1206 /// 3. Copy a pointer to destructions function to field destructions of the
1207 /// resulting structure kmp_task_t.
1208 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1209 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1210 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1211 /// is a resulting structure from
1212 /// previous items.
1213 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001214 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1215 /// /*part_id*/, captured_struct */*__context*/);
1216 /// \param SharedsTy A type which contains references the shared variables.
1217 /// \param Shareds Context with the list of shared variables from the \p
1218 /// TaskFunction.
1219 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1220 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001221 /// \param Data Additional data for task generation like tiednsee, final
1222 /// state, list of privates etc.
James Y Knight9871db02019-02-05 16:42:33 +00001223 virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1224 const OMPLoopDirective &D,
1225 llvm::Function *TaskFunction,
1226 QualType SharedsTy, Address Shareds,
1227 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001229 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001230 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001231 /// \param InnermostKind Kind of innermost directive (for simple directives it
1232 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001233 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001234 /// \param HasCancel true if region has inner cancel directive, false
1235 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001236 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001237 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001238 const RegionCodeGenTy &CodeGen,
1239 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001240
1241 /// Emits reduction function.
1242 /// \param ArgsType Array type containing pointers to reduction variables.
1243 /// \param Privates List of private copies for original reduction arguments.
1244 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1245 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1246 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1247 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev982a35e2019-03-19 17:09:52 +00001248 llvm::Function *emitReductionFunction(SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001249 llvm::Type *ArgsType,
1250 ArrayRef<const Expr *> Privates,
1251 ArrayRef<const Expr *> LHSExprs,
1252 ArrayRef<const Expr *> RHSExprs,
1253 ArrayRef<const Expr *> ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001254
1255 /// Emits single reduction combiner
1256 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1257 const Expr *ReductionOp,
1258 const Expr *PrivateRef,
1259 const DeclRefExpr *LHS,
1260 const DeclRefExpr *RHS);
1261
1262 struct ReductionOptionsTy {
1263 bool WithNowait;
1264 bool SimpleReduction;
1265 OpenMPDirectiveKind ReductionKind;
1266 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001267 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001268 /// reduction:
1269 /// \code
1270 ///
1271 /// static kmp_critical_name lock = { 0 };
1272 ///
1273 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1274 /// ...
1275 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1276 /// ...
1277 /// }
1278 ///
1279 /// ...
1280 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1281 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1282 /// RedList, reduce_func, &<lock>)) {
1283 /// case 1:
1284 /// ...
1285 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1286 /// ...
1287 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1288 /// break;
1289 /// case 2:
1290 /// ...
1291 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1292 /// ...
1293 /// break;
1294 /// default:;
1295 /// }
1296 /// \endcode
1297 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001298 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001299 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1300 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1301 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1302 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001303 /// \param Options List of options for reduction codegen:
1304 /// WithNowait true if parent directive has also nowait clause, false
1305 /// otherwise.
1306 /// SimpleReduction Emit reduction operation only. Used for omp simd
1307 /// directive on the host.
1308 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001309 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001310 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001311 ArrayRef<const Expr *> LHSExprs,
1312 ArrayRef<const Expr *> RHSExprs,
1313 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001314 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001315
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001316 /// Emit a code for initialization of task reduction clause. Next code
1317 /// should be emitted for reduction:
1318 /// \code
1319 ///
1320 /// _task_red_item_t red_data[n];
1321 /// ...
1322 /// red_data[i].shar = &origs[i];
1323 /// red_data[i].size = sizeof(origs[i]);
1324 /// red_data[i].f_init = (void*)RedInit<i>;
1325 /// red_data[i].f_fini = (void*)RedDest<i>;
1326 /// red_data[i].f_comb = (void*)RedOp<i>;
1327 /// red_data[i].flags = <Flag_i>;
1328 /// ...
1329 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1330 /// \endcode
1331 ///
1332 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1333 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1334 /// \param Data Additional data for task generation like tiedness, final
1335 /// state, list of privates, reductions etc.
1336 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1337 SourceLocation Loc,
1338 ArrayRef<const Expr *> LHSExprs,
1339 ArrayRef<const Expr *> RHSExprs,
1340 const OMPTaskDataTy &Data);
1341
1342 /// Required to resolve existing problems in the runtime. Emits threadprivate
1343 /// variables to store the size of the VLAs/array sections for
1344 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1345 /// store the pointer to the original reduction item for the custom
1346 /// initializer defined by declare reduction construct.
1347 /// \param RCG Allows to reuse an existing data for the reductions.
1348 /// \param N Reduction item for which fixups must be emitted.
1349 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1350 ReductionCodeGen &RCG, unsigned N);
1351
1352 /// Get the address of `void *` type of the privatue copy of the reduction
1353 /// item specified by the \p SharedLVal.
1354 /// \param ReductionsPtr Pointer to the reduction data returned by the
1355 /// emitTaskReductionInit function.
1356 /// \param SharedLVal Address of the original reduction item.
1357 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1358 llvm::Value *ReductionsPtr,
1359 LValue SharedLVal);
1360
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001361 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001362 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001363
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001364 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001365 /// \param CancelRegion Region kind for which the cancellation point must be
1366 /// emitted.
1367 ///
1368 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1369 SourceLocation Loc,
1370 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001371
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001372 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001373 /// \param IfCond Condition in the associated 'if' clause, if it was
1374 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001375 /// \param CancelRegion Region kind for which the cancel must be emitted.
1376 ///
1377 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001378 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001379 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001380
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001381 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001382 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001383 /// \param ParentName Name of the function that encloses the target region.
1384 /// \param OutlinedFn Outlined function value to be defined by this call.
1385 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1386 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001387 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001388 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001389 /// evaluates to false.
1390 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1391 StringRef ParentName,
1392 llvm::Function *&OutlinedFn,
1393 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001394 bool IsOffloadEntry,
1395 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001396
Alexey Bataev7bb33532019-01-07 21:30:43 +00001397 /// Emit code that pushes the trip count of loops associated with constructs
1398 /// 'target teams distribute' and 'teams distribute parallel for'.
1399 /// \param SizeEmitter Emits the int64 value for the number of iterations of
1400 /// the associated loop.
1401 virtual void emitTargetNumIterationsCall(
1402 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *Device,
1403 const llvm::function_ref<llvm::Value *(
1404 CodeGenFunction &CGF, const OMPLoopDirective &D)> &SizeEmitter);
1405
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001406 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001407 /// code attempts offloading the execution to the device, an the event of
1408 /// a failure it executes the host version outlined in \a OutlinedFn.
1409 /// \param D Directive to emit.
1410 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001411 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001412 /// \param IfCond Expression evaluated in if clause associated with the target
1413 /// directive, or null if no if clause is used.
1414 /// \param Device Expression evaluated in device clause associated with the
1415 /// target directive, or null if no device clause is used.
Samuel Antaobed3c462015-10-02 16:14:20 +00001416 virtual void emitTargetCall(CodeGenFunction &CGF,
1417 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001418 llvm::Function *OutlinedFn,
Samuel Antaoee8fb302016-01-06 13:42:12 +00001419 llvm::Value *OutlinedFnID, const Expr *IfCond,
Alexey Bataev8451efa2018-01-15 19:06:12 +00001420 const Expr *Device);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001421
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001422 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001423 /// the function itself in case it is a valid device function. Returns true if
1424 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001425 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001426 virtual bool emitTargetFunctions(GlobalDecl GD);
1427
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001428 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001429 /// Returns true if \a GD was dealt with successfully.
1430 /// \param GD Variable declaration to emit.
1431 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1432
Alexey Bataev03f270c2018-03-30 18:31:07 +00001433 /// Checks if the provided global decl \a GD is a declare target variable and
1434 /// registers it when emitting code for the host.
1435 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1436 llvm::Constant *Addr);
1437
Alexey Bataev1af5bd52019-03-05 17:47:18 +00001438 /// Registers provided target firstprivate variable as global on the
1439 /// target.
1440 llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF,
1441 const VarDecl *VD);
1442
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001443 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001444 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001445 /// \param GD Global to scan.
1446 virtual bool emitTargetGlobal(GlobalDecl GD);
1447
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001448 /// Creates and returns a registration function for when at least one
1449 /// requires directives was used in the current module.
1450 llvm::Function *emitRequiresDirectiveRegFun();
1451
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001452 /// Creates the offloading descriptor in the event any target region
Samuel Antaoee8fb302016-01-06 13:42:12 +00001453 /// was emitted in the current module and return the function that registers
1454 /// it.
1455 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001456
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001457 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001458 /// variables captured in a record which address is stored in \a
1459 /// CapturedStruct.
1460 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1461 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1462 /// \param CapturedVars A pointer to the record with the references to
1463 /// variables used in \a OutlinedFn function.
1464 ///
1465 virtual void emitTeamsCall(CodeGenFunction &CGF,
1466 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00001467 SourceLocation Loc, llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001468 ArrayRef<llvm::Value *> CapturedVars);
1469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001470 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001471 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1472 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001473 /// \param NumTeams An integer expression of teams.
1474 /// \param ThreadLimit An integer expression of threads.
1475 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1476 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001477
Samuel Antaocc10b852016-07-28 14:23:26 +00001478 /// Struct that keeps all the relevant information that should be kept
1479 /// throughout a 'target data' region.
1480 class TargetDataInfo {
1481 /// Set to true if device pointer information have to be obtained.
1482 bool RequiresDevicePointerInfo = false;
1483
1484 public:
1485 /// The array of base pointer passed to the runtime library.
1486 llvm::Value *BasePointersArray = nullptr;
1487 /// The array of section pointers passed to the runtime library.
1488 llvm::Value *PointersArray = nullptr;
1489 /// The array of sizes passed to the runtime library.
1490 llvm::Value *SizesArray = nullptr;
1491 /// The array of map types passed to the runtime library.
1492 llvm::Value *MapTypesArray = nullptr;
1493 /// The total number of pointers passed to the runtime library.
1494 unsigned NumberOfPtrs = 0u;
1495 /// Map between the a declaration of a capture and the corresponding base
1496 /// pointer address where the runtime returns the device pointers.
1497 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1498
1499 explicit TargetDataInfo() {}
1500 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1501 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1502 /// Clear information about the data arrays.
1503 void clearArrayInfo() {
1504 BasePointersArray = nullptr;
1505 PointersArray = nullptr;
1506 SizesArray = nullptr;
1507 MapTypesArray = nullptr;
1508 NumberOfPtrs = 0u;
1509 }
1510 /// Return true if the current target data information has valid arrays.
1511 bool isValid() {
1512 return BasePointersArray && PointersArray && SizesArray &&
1513 MapTypesArray && NumberOfPtrs;
1514 }
1515 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1516 };
1517
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001518 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001519 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001520 /// \param IfCond Expression evaluated in if clause associated with the
1521 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001522 /// \param Device Expression evaluated in device clause associated with the
1523 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001524 /// \param Info A record used to store information that needs to be preserved
1525 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001526 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1527 const OMPExecutableDirective &D,
1528 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001529 const RegionCodeGenTy &CodeGen,
1530 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001531
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001532 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001533 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001534 /// \param D Directive to emit.
1535 /// \param IfCond Expression evaluated in if clause associated with the target
1536 /// directive, or null if no if clause is used.
1537 /// \param Device Expression evaluated in device clause associated with the
1538 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001539 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1540 const OMPExecutableDirective &D,
1541 const Expr *IfCond,
1542 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001543
1544 /// Marks function \a Fn with properly mangled versions of vector functions.
1545 /// \param FD Function marked as 'declare simd'.
1546 /// \param Fn LLVM function that must be marked with 'declare simd'
1547 /// attributes.
1548 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1549 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001550
1551 /// Emit initialization for doacross loop nesting support.
1552 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001553 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1554 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001555
1556 /// Emit code for doacross ordered directive with 'depend' clause.
1557 /// \param C 'depend' clause with 'sink|source' dependency kind.
1558 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1559 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001560
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001561 /// Translates the native parameter of outlined function if this is required
1562 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001563 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001564 /// \param NativeParam Parameter itself.
1565 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1566 const VarDecl *NativeParam) const {
1567 return NativeParam;
1568 }
1569
1570 /// Gets the address of the native argument basing on the address of the
1571 /// target-specific parameter.
1572 /// \param NativeParam Parameter itself.
1573 /// \param TargetParam Corresponding target-specific parameter.
1574 virtual Address getParameterAddress(CodeGenFunction &CGF,
1575 const VarDecl *NativeParam,
1576 const VarDecl *TargetParam) const;
1577
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001578 /// Choose default schedule type and chunk value for the
1579 /// dist_schedule clause.
1580 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1581 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1582 llvm::Value *&Chunk) const {}
1583
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001584 /// Choose default schedule type and chunk value for the
1585 /// schedule clause.
1586 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1587 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
Alexey Bataevf6a53d62019-03-18 18:40:00 +00001588 const Expr *&ChunkExpr) const;
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001589
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001590 /// Emits call of the outlined function with the provided arguments,
1591 /// translating these arguments to correct target-specific arguments.
1592 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001593 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001594 llvm::FunctionCallee OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001595 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001596
1597 /// Emits OpenMP-specific function prolog.
1598 /// Required for device constructs.
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001599 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001600
1601 /// Gets the OpenMP-specific address of the local variable.
1602 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1603 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001604
Raphael Isemannb23ccec2018-12-10 12:37:46 +00001605 /// Marks the declaration as already emitted for the device code and returns
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001606 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001607 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001608
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001609 /// Emit deferred declare target variables marked for deferred emission.
1610 void emitDeferredTargetDecls() const;
Alexey Bataev60705422018-10-30 15:50:12 +00001611
1612 /// Adjust some parameters for the target-based directives, like addresses of
1613 /// the variables captured by reference in lambdas.
1614 virtual void
1615 adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
1616 const OMPExecutableDirective &D) const;
Patrick Lyster8f7f5862018-11-19 15:09:33 +00001617
1618 /// Perform check on requires decl to ensure that target architecture
1619 /// supports unified addressing
Gheorghe-Teodor Bercea66cdbb472019-05-21 19:42:01 +00001620 virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D);
Alexey Bataevc5687252019-03-21 19:35:27 +00001621
1622 /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
1623 /// the predefined allocator and translates it into the corresponding address
1624 /// space.
1625 virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
Gheorghe-Teodor Bercea5254f0a2019-06-14 17:58:26 +00001626
1627 /// Return whether the unified_shared_memory has been specified.
1628 bool hasRequiresUnifiedSharedMemory() const;
Alexey Bataev9959db52014-05-06 10:08:46 +00001629};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001630
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001631/// Class supports emissionof SIMD-only code.
1632class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1633public:
1634 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1635 ~CGOpenMPSIMDRuntime() override {}
1636
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001637 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001638 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1639 /// kmp_int32 BoundID, struct context_vars*).
1640 /// \param D OpenMP directive.
1641 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1642 /// \param InnermostKind Kind of innermost directive (for simple directives it
1643 /// is a directive itself, for combined - its innermost directive).
1644 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001645 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001646 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1647 const VarDecl *ThreadIDVar,
1648 OpenMPDirectiveKind InnermostKind,
1649 const RegionCodeGenTy &CodeGen) override;
1650
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001651 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001652 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1653 /// kmp_int32 BoundID, struct context_vars*).
1654 /// \param D OpenMP directive.
1655 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1656 /// \param InnermostKind Kind of innermost directive (for simple directives it
1657 /// is a directive itself, for combined - its innermost directive).
1658 /// \param CodeGen Code generation sequence for the \a D directive.
James Y Knight9871db02019-02-05 16:42:33 +00001659 llvm::Function *
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001660 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1661 const VarDecl *ThreadIDVar,
1662 OpenMPDirectiveKind InnermostKind,
1663 const RegionCodeGenTy &CodeGen) override;
1664
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001665 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001666 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1667 /// TaskT).
1668 /// \param D OpenMP directive.
1669 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1670 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1671 /// task region.
1672 /// \param TaskTVar Variable for task_t argument.
1673 /// \param InnermostKind Kind of innermost directive (for simple directives it
1674 /// is a directive itself, for combined - its innermost directive).
1675 /// \param CodeGen Code generation sequence for the \a D directive.
1676 /// \param Tied true if task is generated for tied task, false otherwise.
1677 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1678 /// tasks.
1679 ///
James Y Knight9871db02019-02-05 16:42:33 +00001680 llvm::Function *emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001681 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1682 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1683 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1684 bool Tied, unsigned &NumberOfParts) override;
1685
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001686 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001687 /// variables captured in a record which address is stored in \a
1688 /// CapturedStruct.
1689 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1690 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1691 /// \param CapturedVars A pointer to the record with the references to
1692 /// variables used in \a OutlinedFn function.
1693 /// \param IfCond Condition in the associated 'if' clause, if it was
1694 /// specified, nullptr otherwise.
1695 ///
1696 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001697 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001698 ArrayRef<llvm::Value *> CapturedVars,
1699 const Expr *IfCond) override;
1700
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001701 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001702 /// \param CriticalName Name of the critical region.
1703 /// \param CriticalOpGen Generator for the statement associated with the given
1704 /// critical region.
1705 /// \param Hint Value of the 'hint' clause (optional).
1706 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1707 const RegionCodeGenTy &CriticalOpGen,
1708 SourceLocation Loc,
1709 const Expr *Hint = nullptr) override;
1710
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001711 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001712 /// \param MasterOpGen Generator for the statement associated with the given
1713 /// master region.
1714 void emitMasterRegion(CodeGenFunction &CGF,
1715 const RegionCodeGenTy &MasterOpGen,
1716 SourceLocation Loc) override;
1717
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001718 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001719 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1720
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001721 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001722 /// \param TaskgroupOpGen Generator for the statement associated with the
1723 /// given taskgroup region.
1724 void emitTaskgroupRegion(CodeGenFunction &CGF,
1725 const RegionCodeGenTy &TaskgroupOpGen,
1726 SourceLocation Loc) override;
1727
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001728 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001729 /// \param SingleOpGen Generator for the statement associated with the given
1730 /// single region.
1731 void emitSingleRegion(CodeGenFunction &CGF,
1732 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1733 ArrayRef<const Expr *> CopyprivateVars,
1734 ArrayRef<const Expr *> DestExprs,
1735 ArrayRef<const Expr *> SrcExprs,
1736 ArrayRef<const Expr *> AssignmentOps) override;
1737
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001738 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001739 /// \param OrderedOpGen Generator for the statement associated with the given
1740 /// ordered region.
1741 void emitOrderedRegion(CodeGenFunction &CGF,
1742 const RegionCodeGenTy &OrderedOpGen,
1743 SourceLocation Loc, bool IsThreads) override;
1744
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001745 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001746 /// \param Kind Directive for which this implicit barrier call must be
1747 /// generated. Must be OMPD_barrier for explicit barrier generation.
1748 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1749 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1750 /// runtime class decides which one to emit (simple or with cancellation
1751 /// checks).
1752 ///
1753 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1754 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1755 bool ForceSimpleCall = false) override;
1756
1757 /// This is used for non static scheduled types and when the ordered
1758 /// clause is present on the loop construct.
1759 /// Depending on the loop schedule, it is necessary to call some runtime
1760 /// routine before start of the OpenMP loop to get the loop upper / lower
1761 /// bounds \a LB and \a UB and stride \a ST.
1762 ///
1763 /// \param CGF Reference to current CodeGenFunction.
1764 /// \param Loc Clang source location.
1765 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1766 /// \param IVSize Size of the iteration variable in bits.
1767 /// \param IVSigned Sign of the iteration variable.
1768 /// \param Ordered true if loop is ordered, false otherwise.
1769 /// \param DispatchValues struct containing llvm values for lower bound, upper
1770 /// bound, and chunk expression.
1771 /// For the default (nullptr) value, the chunk 1 will be used.
1772 ///
1773 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1774 const OpenMPScheduleTy &ScheduleKind,
1775 unsigned IVSize, bool IVSigned, bool Ordered,
1776 const DispatchRTInput &DispatchValues) override;
1777
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001778 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001779 /// of loop.
1780 ///
1781 /// This is used only in case of static schedule, when the user did not
1782 /// specify a ordered clause on the loop construct.
1783 /// Depending on the loop schedule, it is necessary to call some runtime
1784 /// routine before start of the OpenMP loop to get the loop upper / lower
1785 /// bounds LB and UB and stride ST.
1786 ///
1787 /// \param CGF Reference to current CodeGenFunction.
1788 /// \param Loc Clang source location.
1789 /// \param DKind Kind of the directive.
1790 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1791 /// \param Values Input arguments for the construct.
1792 ///
1793 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1794 OpenMPDirectiveKind DKind,
1795 const OpenMPScheduleTy &ScheduleKind,
1796 const StaticRTInput &Values) override;
1797
1798 ///
1799 /// \param CGF Reference to current CodeGenFunction.
1800 /// \param Loc Clang source location.
1801 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1802 /// \param Values Input arguments for the construct.
1803 ///
1804 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1805 OpenMPDistScheduleClauseKind SchedKind,
1806 const StaticRTInput &Values) override;
1807
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001808 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001809 /// iteration of the ordered loop with the dynamic scheduling.
1810 ///
1811 /// \param CGF Reference to current CodeGenFunction.
1812 /// \param Loc Clang source location.
1813 /// \param IVSize Size of the iteration variable in bits.
1814 /// \param IVSigned Sign of the iteration variable.
1815 ///
1816 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1817 unsigned IVSize, bool IVSigned) override;
1818
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001819 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001820 /// all the work with current loop.
1821 ///
1822 /// \param CGF Reference to current CodeGenFunction.
1823 /// \param Loc Clang source location.
1824 /// \param DKind Kind of the directive for which the static finish is emitted.
1825 ///
1826 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1827 OpenMPDirectiveKind DKind) override;
1828
1829 /// Call __kmpc_dispatch_next(
1830 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1831 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1832 /// kmp_int[32|64] *p_stride);
1833 /// \param IVSize Size of the iteration variable in bits.
1834 /// \param IVSigned Sign of the iteration variable.
1835 /// \param IL Address of the output variable in which the flag of the
1836 /// last iteration is returned.
1837 /// \param LB Address of the output variable in which the lower iteration
1838 /// number is returned.
1839 /// \param UB Address of the output variable in which the upper iteration
1840 /// number is returned.
1841 /// \param ST Address of the output variable in which the stride value is
1842 /// returned.
1843 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1844 unsigned IVSize, bool IVSigned, Address IL,
1845 Address LB, Address UB, Address ST) override;
1846
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001847 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001848 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1849 /// clause.
1850 /// \param NumThreads An integer value of threads.
1851 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1852 SourceLocation Loc) override;
1853
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001854 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001855 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1856 void emitProcBindClause(CodeGenFunction &CGF,
1857 OpenMPProcBindClauseKind ProcBind,
1858 SourceLocation Loc) override;
1859
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001860 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001861 /// thread.
1862 /// \param VD Threadprivate variable.
1863 /// \param VDAddr Address of the global variable \a VD.
1864 /// \param Loc Location of the reference to threadprivate var.
1865 /// \return Address of the threadprivate variable for the current thread.
1866 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1867 Address VDAddr, SourceLocation Loc) override;
1868
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001869 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001870 /// a call to runtime library which adds initial value to the newly created
1871 /// threadprivate variable (if it is not constant) and registers destructor
1872 /// for the variable (if any).
1873 /// \param VD Threadprivate variable.
1874 /// \param VDAddr Address of the global variable \a VD.
1875 /// \param Loc Location of threadprivate declaration.
1876 /// \param PerformInit true if initialization expression is not constant.
1877 llvm::Function *
1878 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1879 SourceLocation Loc, bool PerformInit,
1880 CodeGenFunction *CGF = nullptr) override;
1881
1882 /// Creates artificial threadprivate variable with name \p Name and type \p
1883 /// VarType.
1884 /// \param VarType Type of the artificial threadprivate variable.
1885 /// \param Name Name of the artificial threadprivate variable.
1886 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1887 QualType VarType,
1888 StringRef Name) override;
1889
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001890 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001891 /// \param Vars List of variables to flush.
1892 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1893 SourceLocation Loc) override;
1894
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001895 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001896 /// emitted in several steps:
1897 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1898 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1899 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1900 /// function:
1901 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1902 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1903 /// return 0;
1904 /// }
1905 /// 2. Copy a list of shared variables to field shareds of the resulting
1906 /// structure kmp_task_t returned by the previous call (if any).
1907 /// 3. Copy a pointer to destructions function to field destructions of the
1908 /// resulting structure kmp_task_t.
1909 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1910 /// kmp_task_t *new_task), where new_task is a resulting structure from
1911 /// previous items.
1912 /// \param D Current task directive.
1913 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1914 /// /*part_id*/, captured_struct */*__context*/);
1915 /// \param SharedsTy A type which contains references the shared variables.
1916 /// \param Shareds Context with the list of shared variables from the \p
1917 /// TaskFunction.
1918 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1919 /// otherwise.
1920 /// \param Data Additional data for task generation like tiednsee, final
1921 /// state, list of privates etc.
1922 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001923 const OMPExecutableDirective &D,
1924 llvm::Function *TaskFunction, QualType SharedsTy,
1925 Address Shareds, const Expr *IfCond,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001926 const OMPTaskDataTy &Data) override;
1927
1928 /// Emit task region for the taskloop directive. The taskloop region is
1929 /// emitted in several steps:
1930 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1931 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1932 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1933 /// function:
1934 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1935 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1936 /// return 0;
1937 /// }
1938 /// 2. Copy a list of shared variables to field shareds of the resulting
1939 /// structure kmp_task_t returned by the previous call (if any).
1940 /// 3. Copy a pointer to destructions function to field destructions of the
1941 /// resulting structure kmp_task_t.
1942 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1943 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1944 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1945 /// is a resulting structure from
1946 /// previous items.
1947 /// \param D Current task directive.
1948 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1949 /// /*part_id*/, captured_struct */*__context*/);
1950 /// \param SharedsTy A type which contains references the shared variables.
1951 /// \param Shareds Context with the list of shared variables from the \p
1952 /// TaskFunction.
1953 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1954 /// otherwise.
1955 /// \param Data Additional data for task generation like tiednsee, final
1956 /// state, list of privates etc.
1957 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00001958 const OMPLoopDirective &D, llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001959 QualType SharedsTy, Address Shareds, const Expr *IfCond,
1960 const OMPTaskDataTy &Data) override;
1961
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001962 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001963 /// reduction:
1964 /// \code
1965 ///
1966 /// static kmp_critical_name lock = { 0 };
1967 ///
1968 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1969 /// ...
1970 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1971 /// ...
1972 /// }
1973 ///
1974 /// ...
1975 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1976 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1977 /// RedList, reduce_func, &<lock>)) {
1978 /// case 1:
1979 /// ...
1980 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1981 /// ...
1982 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1983 /// break;
1984 /// case 2:
1985 /// ...
1986 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1987 /// ...
1988 /// break;
1989 /// default:;
1990 /// }
1991 /// \endcode
1992 ///
1993 /// \param Privates List of private copies for original reduction arguments.
1994 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1995 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1996 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1997 /// or 'operator binop(LHS, RHS)'.
1998 /// \param Options List of options for reduction codegen:
1999 /// WithNowait true if parent directive has also nowait clause, false
2000 /// otherwise.
2001 /// SimpleReduction Emit reduction operation only. Used for omp simd
2002 /// directive on the host.
2003 /// ReductionKind The kind of reduction to perform.
2004 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
2005 ArrayRef<const Expr *> Privates,
2006 ArrayRef<const Expr *> LHSExprs,
2007 ArrayRef<const Expr *> RHSExprs,
2008 ArrayRef<const Expr *> ReductionOps,
2009 ReductionOptionsTy Options) override;
2010
2011 /// Emit a code for initialization of task reduction clause. Next code
2012 /// should be emitted for reduction:
2013 /// \code
2014 ///
2015 /// _task_red_item_t red_data[n];
2016 /// ...
2017 /// red_data[i].shar = &origs[i];
2018 /// red_data[i].size = sizeof(origs[i]);
2019 /// red_data[i].f_init = (void*)RedInit<i>;
2020 /// red_data[i].f_fini = (void*)RedDest<i>;
2021 /// red_data[i].f_comb = (void*)RedOp<i>;
2022 /// red_data[i].flags = <Flag_i>;
2023 /// ...
2024 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
2025 /// \endcode
2026 ///
2027 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
2028 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
2029 /// \param Data Additional data for task generation like tiedness, final
2030 /// state, list of privates, reductions etc.
2031 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
2032 ArrayRef<const Expr *> LHSExprs,
2033 ArrayRef<const Expr *> RHSExprs,
2034 const OMPTaskDataTy &Data) override;
2035
2036 /// Required to resolve existing problems in the runtime. Emits threadprivate
2037 /// variables to store the size of the VLAs/array sections for
2038 /// initializer/combiner/finalizer functions + emits threadprivate variable to
2039 /// store the pointer to the original reduction item for the custom
2040 /// initializer defined by declare reduction construct.
2041 /// \param RCG Allows to reuse an existing data for the reductions.
2042 /// \param N Reduction item for which fixups must be emitted.
2043 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
2044 ReductionCodeGen &RCG, unsigned N) override;
2045
2046 /// Get the address of `void *` type of the privatue copy of the reduction
2047 /// item specified by the \p SharedLVal.
2048 /// \param ReductionsPtr Pointer to the reduction data returned by the
2049 /// emitTaskReductionInit function.
2050 /// \param SharedLVal Address of the original reduction item.
2051 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
2052 llvm::Value *ReductionsPtr,
2053 LValue SharedLVal) override;
2054
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002055 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002056 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
2057
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002058 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002059 /// \param CancelRegion Region kind for which the cancellation point must be
2060 /// emitted.
2061 ///
2062 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
2063 OpenMPDirectiveKind CancelRegion) override;
2064
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002065 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002066 /// \param IfCond Condition in the associated 'if' clause, if it was
2067 /// specified, nullptr otherwise.
2068 /// \param CancelRegion Region kind for which the cancel must be emitted.
2069 ///
2070 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
2071 const Expr *IfCond,
2072 OpenMPDirectiveKind CancelRegion) override;
2073
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002074 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002075 /// \param D Directive to emit.
2076 /// \param ParentName Name of the function that encloses the target region.
2077 /// \param OutlinedFn Outlined function value to be defined by this call.
2078 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
2079 /// \param IsOffloadEntry True if the outlined function is an offload entry.
2080 /// \param CodeGen Code generation sequence for the \a D directive.
2081 /// An outlined function may not be an entry if, e.g. the if clause always
2082 /// evaluates to false.
2083 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
2084 StringRef ParentName,
2085 llvm::Function *&OutlinedFn,
2086 llvm::Constant *&OutlinedFnID,
2087 bool IsOffloadEntry,
2088 const RegionCodeGenTy &CodeGen) override;
2089
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002090 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002091 /// code attempts offloading the execution to the device, an the event of
2092 /// a failure it executes the host version outlined in \a OutlinedFn.
2093 /// \param D Directive to emit.
2094 /// \param OutlinedFn Host version of the code to be offloaded.
2095 /// \param OutlinedFnID ID of host version of the code to be offloaded.
2096 /// \param IfCond Expression evaluated in if clause associated with the target
2097 /// directive, or null if no if clause is used.
2098 /// \param Device Expression evaluated in device clause associated with the
2099 /// target directive, or null if no device clause is used.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002100 void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002101 llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID,
Alexey Bataev8451efa2018-01-15 19:06:12 +00002102 const Expr *IfCond, const Expr *Device) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002103
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002104 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002105 /// the function itself in case it is a valid device function. Returns true if
2106 /// \a GD was dealt with successfully.
2107 /// \param GD Function to scan.
2108 bool emitTargetFunctions(GlobalDecl GD) override;
2109
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002110 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002111 /// Returns true if \a GD was dealt with successfully.
2112 /// \param GD Variable declaration to emit.
2113 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2114
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002115 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002116 /// if it was emitted successfully.
2117 /// \param GD Global to scan.
2118 bool emitTargetGlobal(GlobalDecl GD) override;
2119
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002120 /// Creates the offloading descriptor in the event any target region
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002121 /// was emitted in the current module and return the function that registers
2122 /// it.
2123 llvm::Function *emitRegistrationFunction() override;
2124
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002125 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002126 /// variables captured in a record which address is stored in \a
2127 /// CapturedStruct.
2128 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2129 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2130 /// \param CapturedVars A pointer to the record with the references to
2131 /// variables used in \a OutlinedFn function.
2132 ///
2133 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00002134 SourceLocation Loc, llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002135 ArrayRef<llvm::Value *> CapturedVars) override;
2136
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002137 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002138 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2139 /// for num_teams clause.
2140 /// \param NumTeams An integer expression of teams.
2141 /// \param ThreadLimit An integer expression of threads.
2142 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2143 const Expr *ThreadLimit, SourceLocation Loc) override;
2144
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002145 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002146 /// \param D Directive to emit.
2147 /// \param IfCond Expression evaluated in if clause associated with the
2148 /// target directive, or null if no device clause is used.
2149 /// \param Device Expression evaluated in device clause associated with the
2150 /// target directive, or null if no device clause is used.
2151 /// \param Info A record used to store information that needs to be preserved
2152 /// until the region is closed.
2153 void emitTargetDataCalls(CodeGenFunction &CGF,
2154 const OMPExecutableDirective &D, const Expr *IfCond,
2155 const Expr *Device, const RegionCodeGenTy &CodeGen,
2156 TargetDataInfo &Info) override;
2157
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002158 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002159 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2160 /// \param D Directive to emit.
2161 /// \param IfCond Expression evaluated in if clause associated with the target
2162 /// directive, or null if no if clause is used.
2163 /// \param Device Expression evaluated in device clause associated with the
2164 /// target directive, or null if no device clause is used.
2165 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2166 const OMPExecutableDirective &D,
2167 const Expr *IfCond,
2168 const Expr *Device) override;
2169
2170 /// Emit initialization for doacross loop nesting support.
2171 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002172 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2173 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002174
2175 /// Emit code for doacross ordered directive with 'depend' clause.
2176 /// \param C 'depend' clause with 'sink|source' dependency kind.
2177 void emitDoacrossOrdered(CodeGenFunction &CGF,
2178 const OMPDependClause *C) override;
2179
2180 /// Translates the native parameter of outlined function if this is required
2181 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002182 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002183 /// \param NativeParam Parameter itself.
2184 const VarDecl *translateParameter(const FieldDecl *FD,
2185 const VarDecl *NativeParam) const override;
2186
2187 /// Gets the address of the native argument basing on the address of the
2188 /// target-specific parameter.
2189 /// \param NativeParam Parameter itself.
2190 /// \param TargetParam Corresponding target-specific parameter.
2191 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2192 const VarDecl *TargetParam) const override;
Alexey Bataev4f680db2019-03-19 16:41:16 +00002193
2194 /// Gets the OpenMP-specific address of the local variable.
2195 Address getAddressOfLocalVariable(CodeGenFunction &CGF,
2196 const VarDecl *VD) override {
2197 return Address::invalid();
2198 }
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002199};
2200
Alexey Bataev23b69422014-06-18 07:08:49 +00002201} // namespace CodeGen
2202} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002203
2204#endif