blob: 35f75a9ec0861654d82e561321e719733b630a80 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a class for OpenMP runtime code generation.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
Alexey Bataev9959db52014-05-06 10:08:46 +000016
Alexey Bataev7292c292016-04-25 12:22:29 +000017#include "CGValue.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"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000022#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000023#include "llvm/ADT/StringMap.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.
Alexey Bataev7ef47a62018-02-22 18:33:31 +0000274 void emitCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *Callee,
275 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev3c595a62017-08-14 15:01:03 +0000276
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000277 /// Emits address of the word in a memory where current thread id is
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000278 /// stored.
279 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
280
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000281private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000282 /// Default const ident_t object used for initialization of all other
Alexey Bataev9959db52014-05-06 10:08:46 +0000283 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000284 llvm::Constant *DefaultOpenMPPSource = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000285 /// Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000286 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
287 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000288 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000289
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000290 QualType IdentQTy;
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000291 llvm::StructType *IdentTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000292 /// Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000293 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
294 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295 /// The type for a microtask which gets passed to __kmpc_fork_call().
Alexey Bataev9959db52014-05-06 10:08:46 +0000296 /// Original representation is:
297 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000298 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000299 /// Stores debug location and ThreadID for the function.
Alexey Bataev18095712014-10-10 12:19:54 +0000300 struct DebugLocThreadIdTy {
301 llvm::Value *DebugLoc;
302 llvm::Value *ThreadID;
303 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000304 /// Map of local debug location, ThreadId and functions.
Alexey Bataev18095712014-10-10 12:19:54 +0000305 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
306 OpenMPLocThreadIDMapTy;
307 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000308 /// Map of UDRs and corresponding combiner/initializer.
309 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
310 std::pair<llvm::Function *, llvm::Function *>>
311 UDRMapTy;
312 UDRMapTy UDRMap;
313 /// Map of functions and locally defined UDRs.
314 typedef llvm::DenseMap<llvm::Function *,
315 SmallVector<const OMPDeclareReductionDecl *, 4>>
316 FunctionUDRMapTy;
317 FunctionUDRMapTy FunctionUDRMap;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000318 /// Type kmp_critical_name, originally defined as typedef kmp_int32
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000319 /// kmp_critical_name[8];
320 llvm::ArrayType *KmpCriticalNameTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000321 /// An ordered map of auto-generated variables to their unique names.
Alexey Bataev97720002014-11-11 04:05:39 +0000322 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
323 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
324 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
325 /// variables.
326 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
327 InternalVars;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000328 /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000329 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000330 QualType KmpRoutineEntryPtrQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000331 /// Type typedef struct kmp_task {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000332 /// void * shareds; /**< pointer to block of pointers to
333 /// shared vars */
334 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
335 /// executing task */
336 /// kmp_int32 part_id; /**< part id for the task */
337 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
338 /// deconstructors of firstprivate C++ objects */
339 /// } kmp_task_t;
340 QualType KmpTaskTQTy;
Alexey Bataeve213f3e2017-10-11 15:29:40 +0000341 /// Saved kmp_task_t for task directive.
342 QualType SavedKmpTaskTQTy;
343 /// Saved kmp_task_t for taskloop-based directive.
344 QualType SavedKmpTaskloopTQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000345 /// Type typedef struct kmp_depend_info {
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000346 /// kmp_intptr_t base_addr;
347 /// size_t len;
348 /// struct {
349 /// bool in:1;
350 /// bool out:1;
351 /// } flags;
352 /// } kmp_depend_info_t;
353 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000354 /// struct kmp_dim { // loop bounds info casted to kmp_int64
355 /// kmp_int64 lo; // lower
356 /// kmp_int64 up; // upper
357 /// kmp_int64 st; // stride
358 /// };
359 QualType KmpDimTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000360 /// Type struct __tgt_offload_entry{
Samuel Antaoee8fb302016-01-06 13:42:12 +0000361 /// void *addr; // Pointer to the offload entry info.
362 /// // (function or global)
363 /// char *name; // Name of the function or global.
364 /// size_t size; // Size of the entry info (0 if it a function).
365 /// };
366 QualType TgtOffloadEntryQTy;
367 /// struct __tgt_device_image{
368 /// void *ImageStart; // Pointer to the target code start.
369 /// void *ImageEnd; // Pointer to the target code end.
370 /// // We also add the host entries to the device image, as it may be useful
371 /// // for the target runtime to have access to that information.
372 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
373 /// // the entries.
374 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
375 /// // entries (non inclusive).
376 /// };
377 QualType TgtDeviceImageQTy;
378 /// struct __tgt_bin_desc{
379 /// int32_t NumDevices; // Number of devices supported.
380 /// __tgt_device_image *DeviceImages; // Arrays of device images
381 /// // (one per device).
382 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
383 /// // entries.
384 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
385 /// // entries (non inclusive).
386 /// };
387 QualType TgtBinaryDescriptorQTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000388 /// Entity that registers the offloading constants that were emitted so
Samuel Antaoee8fb302016-01-06 13:42:12 +0000389 /// far.
390 class OffloadEntriesInfoManagerTy {
391 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000392
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000393 /// Number of entries registered so far.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000394 unsigned OffloadingEntriesNum = 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000395
396 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000397 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000398 class OffloadEntryInfo {
399 public:
Alexey Bataev34f8a702018-03-28 14:28:54 +0000400 /// Kind of a given entry.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000401 enum OffloadingEntryInfoKinds : unsigned {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000402 /// Entry is a target region.
403 OffloadingEntryInfoTargetRegion = 0,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000404 /// Entry is a declare target variable.
405 OffloadingEntryInfoDeviceGlobalVar = 1,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000406 /// Invalid entry info.
407 OffloadingEntryInfoInvalid = ~0u
Samuel Antaoee8fb302016-01-06 13:42:12 +0000408 };
409
Alexey Bataev03f270c2018-03-30 18:31:07 +0000410 protected:
411 OffloadEntryInfo() = delete;
412 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {}
Samuel Antaof83efdb2017-01-05 16:02:49 +0000413 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000414 uint32_t Flags)
Samuel Antaof83efdb2017-01-05 16:02:49 +0000415 : Flags(Flags), Order(Order), Kind(Kind) {}
Alexey Bataev03f270c2018-03-30 18:31:07 +0000416 ~OffloadEntryInfo() = default;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000417
Alexey Bataev03f270c2018-03-30 18:31:07 +0000418 public:
Samuel Antaoee8fb302016-01-06 13:42:12 +0000419 bool isValid() const { return Order != ~0u; }
420 unsigned getOrder() const { return Order; }
421 OffloadingEntryInfoKinds getKind() const { return Kind; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000422 uint32_t getFlags() const { return Flags; }
423 void setFlags(uint32_t NewFlags) { Flags = NewFlags; }
424 llvm::Constant *getAddress() const {
425 return cast_or_null<llvm::Constant>(Addr);
426 }
427 void setAddress(llvm::Constant *V) {
428 assert(!Addr.pointsToAliveValue() && "Address has been set before!");
429 Addr = V;
430 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000431 static bool classof(const OffloadEntryInfo *Info) { return true; }
432
Samuel Antaof83efdb2017-01-05 16:02:49 +0000433 private:
Alexey Bataev03f270c2018-03-30 18:31:07 +0000434 /// Address of the entity that has to be mapped for offloading.
435 llvm::WeakTrackingVH Addr;
436
Samuel Antaof83efdb2017-01-05 16:02:49 +0000437 /// Flags associated with the device global.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000438 uint32_t Flags = 0u;
Samuel Antaof83efdb2017-01-05 16:02:49 +0000439
440 /// Order this entry was emitted.
Alexey Bataev03f270c2018-03-30 18:31:07 +0000441 unsigned Order = ~0u;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000442
Alexey Bataev03f270c2018-03-30 18:31:07 +0000443 OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000444 };
445
Alexey Bataev03f270c2018-03-30 18:31:07 +0000446 /// Return true if a there are no entries defined.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000447 bool empty() const;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000448 /// Return number of entries defined so far.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000449 unsigned size() const { return OffloadingEntriesNum; }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000450 OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000451
Alexey Bataev03f270c2018-03-30 18:31:07 +0000452 //
453 // Target region entries related.
454 //
455
456 /// Kind of the target registry entry.
457 enum OMPTargetRegionEntryKind : uint32_t {
458 /// Mark the entry as target region.
459 OMPTargetRegionEntryTargetRegion = 0x0,
460 /// Mark the entry as a global constructor.
461 OMPTargetRegionEntryCtor = 0x02,
462 /// Mark the entry as a global destructor.
463 OMPTargetRegionEntryDtor = 0x04,
464 };
465
466 /// Target region entries info.
467 class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo {
468 /// Address that can be used as the ID of the entry.
469 llvm::Constant *ID = nullptr;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000470
471 public:
472 OffloadEntryInfoTargetRegion()
Alexey Bataev03f270c2018-03-30 18:31:07 +0000473 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000474 explicit OffloadEntryInfoTargetRegion(unsigned Order,
475 llvm::Constant *Addr,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000476 llvm::Constant *ID,
477 OMPTargetRegionEntryKind Flags)
478 : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags),
Alexey Bataev03f270c2018-03-30 18:31:07 +0000479 ID(ID) {
480 setAddress(Addr);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000481 }
Alexey Bataev03f270c2018-03-30 18:31:07 +0000482
483 llvm::Constant *getID() const { return ID; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000484 void setID(llvm::Constant *V) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000485 assert(!ID && "ID has been set before!");
Samuel Antaoee8fb302016-01-06 13:42:12 +0000486 ID = V;
487 }
488 static bool classof(const OffloadEntryInfo *Info) {
Alexey Bataev34f8a702018-03-28 14:28:54 +0000489 return Info->getKind() == OffloadingEntryInfoTargetRegion;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000490 }
491 };
Alexey Bataev03f270c2018-03-30 18:31:07 +0000492
493 /// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000494 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
495 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000496 unsigned Order);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000497 /// Register target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000498 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
499 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000500 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +0000501 OMPTargetRegionEntryKind Flags);
Alexey Bataev03f270c2018-03-30 18:31:07 +0000502 /// Return true if a target region entry with the provided information
503 /// exists.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000504 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000505 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000506 /// brief Applies action \a Action on all registered entries.
507 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000508 const OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000509 OffloadTargetRegionEntryInfoActTy;
510 void actOnTargetRegionEntriesInfo(
511 const OffloadTargetRegionEntryInfoActTy &Action);
512
Alexey Bataev03f270c2018-03-30 18:31:07 +0000513 //
514 // Device global variable entries related.
515 //
516
517 /// Kind of the global variable entry..
518 enum OMPTargetGlobalVarEntryKind : uint32_t {
519 /// Mark the entry as a to declare target.
520 OMPTargetGlobalVarEntryTo = 0x0,
Alexey Bataevc52f01d2018-07-16 20:05:25 +0000521 /// Mark the entry as a to declare target link.
522 OMPTargetGlobalVarEntryLink = 0x1,
Alexey Bataev03f270c2018-03-30 18:31:07 +0000523 };
524
525 /// Device global variable entries info.
526 class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo {
527 /// Type of the global variable.
528 CharUnits VarSize;
529 llvm::GlobalValue::LinkageTypes Linkage;
530
531 public:
532 OffloadEntryInfoDeviceGlobalVar()
533 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {}
534 explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order,
535 OMPTargetGlobalVarEntryKind Flags)
536 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {}
537 explicit OffloadEntryInfoDeviceGlobalVar(
538 unsigned Order, llvm::Constant *Addr, CharUnits VarSize,
539 OMPTargetGlobalVarEntryKind Flags,
540 llvm::GlobalValue::LinkageTypes Linkage)
541 : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags),
542 VarSize(VarSize), Linkage(Linkage) {
543 setAddress(Addr);
544 }
545
546 CharUnits getVarSize() const { return VarSize; }
547 void setVarSize(CharUnits Size) { VarSize = Size; }
548 llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; }
549 void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; }
550 static bool classof(const OffloadEntryInfo *Info) {
551 return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar;
552 }
553 };
554
555 /// Initialize device global variable entry.
556 void initializeDeviceGlobalVarEntryInfo(StringRef Name,
557 OMPTargetGlobalVarEntryKind Flags,
558 unsigned Order);
559
560 /// Register device global variable entry.
561 void
562 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
563 CharUnits VarSize,
564 OMPTargetGlobalVarEntryKind Flags,
565 llvm::GlobalValue::LinkageTypes Linkage);
566 /// Checks if the variable with the given name has been registered already.
567 bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const {
568 return OffloadEntriesDeviceGlobalVar.count(VarName) > 0;
569 }
570 /// Applies action \a Action on all registered entries.
571 typedef llvm::function_ref<void(StringRef,
572 const OffloadEntryInfoDeviceGlobalVar &)>
573 OffloadDeviceGlobalVarEntryInfoActTy;
574 void actOnDeviceGlobalVarEntriesInfo(
575 const OffloadDeviceGlobalVarEntryInfoActTy &Action);
576
Samuel Antaoee8fb302016-01-06 13:42:12 +0000577 private:
578 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000579 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000580 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000581 OffloadEntriesTargetRegionPerLine;
582 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
583 OffloadEntriesTargetRegionPerParentName;
584 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
585 OffloadEntriesTargetRegionPerFile;
586 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
587 OffloadEntriesTargetRegionPerDevice;
588 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
589 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
Alexey Bataev03f270c2018-03-30 18:31:07 +0000590 /// Storage for device global variable entries kind. The storage is to be
591 /// indexed by mangled name.
592 typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar>
593 OffloadEntriesDeviceGlobalVarTy;
594 OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000595 };
596 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
597
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000598 bool ShouldMarkAsGlobal = true;
Alexey Bataeve6aa4692018-09-13 16:54:05 +0000599 llvm::SmallDenseSet<const Decl *> AlreadyEmittedTargetFunctions;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +0000600
Alexey Bataevbf8fe712018-08-07 16:14:36 +0000601 /// List of variables that can become declare target implicitly and, thus,
602 /// must be emitted.
603 llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
604
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000605 /// Creates and registers offloading binary descriptor for the current
Samuel Antaoee8fb302016-01-06 13:42:12 +0000606 /// compilation unit. The function that does the registration is returned.
607 llvm::Function *createOffloadingBinaryDescriptorRegistration();
608
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000609 /// Creates all the offload entries in the current compilation unit
Samuel Antaoee8fb302016-01-06 13:42:12 +0000610 /// along with the associated metadata.
611 void createOffloadEntriesAndInfoMetadata();
612
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000613 /// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +0000614 /// metadata.
615 void loadOffloadInfoMetadata();
616
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000617 /// Returns __tgt_offload_entry type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000618 QualType getTgtOffloadEntryQTy();
619
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000620 /// Returns __tgt_device_image type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000621 QualType getTgtDeviceImageQTy();
622
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000623 /// Returns __tgt_bin_desc type.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000624 QualType getTgtBinaryDescriptorQTy();
625
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000626 /// Start scanning from statement \a S and and emit all target regions
Samuel Antaoee8fb302016-01-06 13:42:12 +0000627 /// found along the way.
628 /// \param S Starting statement.
629 /// \param ParentName Name of the function declaration that is being scanned.
630 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000631
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000632 /// Build type kmp_routine_entry_t (if not built yet).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000633 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000634
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000635 /// Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000636 llvm::Type *getKmpc_MicroPointerTy();
637
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000638 /// Returns specified OpenMP runtime function.
Alexey Bataev9959db52014-05-06 10:08:46 +0000639 /// \param Function OpenMP runtime function.
640 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000641 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000642
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000643 /// Returns __kmpc_for_static_init_* runtime function for the specified
Alexander Musman21212e42015-03-13 10:38:23 +0000644 /// size \a IVSize and sign \a IVSigned.
645 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
646
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000647 /// Returns __kmpc_dispatch_init_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000648 /// size \a IVSize and sign \a IVSigned.
649 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
650
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000651 /// Returns __kmpc_dispatch_next_* runtime function for the specified
Alexander Musman92bdaab2015-03-12 13:37:50 +0000652 /// size \a IVSize and sign \a IVSigned.
653 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
654
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000655 /// Returns __kmpc_dispatch_fini_* runtime function for the specified
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000656 /// size \a IVSize and sign \a IVSigned.
657 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
658
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000659 /// If the specified mangled name is not in the module, create and
Alexey Bataev97720002014-11-11 04:05:39 +0000660 /// return threadprivate cache object. This object is a pointer's worth of
661 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000662 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000663 /// \return Cache variable for the specified threadprivate.
664 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
665
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000666 /// Gets (if variable with the given name already exist) or creates
Alexey Bataev97720002014-11-11 04:05:39 +0000667 /// internal global variable with the specified Name. The created variable has
668 /// linkage CommonLinkage by default and is initialized by null value.
669 /// \param Ty Type of the global variable. If it is exist already the type
670 /// must be the same.
671 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000672 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000673 const llvm::Twine &Name);
674
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000675 /// Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000676 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000677
Alexey Bataev34f8a702018-03-28 14:28:54 +0000678 /// Set of declare target variables with the generated initializer.
679 llvm::SmallPtrSet<const VarDecl *, 4> DeclareTargetWithDefinition;
680
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000681 /// Emits initialization code for the threadprivate variables.
Alexey Bataev97720002014-11-11 04:05:39 +0000682 /// \param VDAddr Address of the global variable \a VD.
683 /// \param Ctor Pointer to a global init function for \a VD.
684 /// \param CopyCtor Pointer to a global copy function for \a VD.
685 /// \param Dtor Pointer to a global destructor function for \a VD.
686 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000687 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000688 llvm::Value *Ctor, llvm::Value *CopyCtor,
689 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000690
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000691 /// Returns corresponding lock object for the specified critical region
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000692 /// name. If the lock object does not exist it is created, otherwise the
693 /// reference to the existing copy is returned.
694 /// \param CriticalName Name of the critical region.
695 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000696 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000697
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000698 struct TaskResultTy {
699 llvm::Value *NewTask = nullptr;
700 llvm::Value *TaskEntry = nullptr;
701 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000702 LValue TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000703 const RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000704 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000705 };
706 /// Emit task region for the task directive. The task region is emitted in
707 /// several steps:
708 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
709 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
710 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
711 /// function:
712 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
713 /// TaskFunction(gtid, tt->part_id, tt->shareds);
714 /// return 0;
715 /// }
716 /// 2. Copy a list of shared variables to field shareds of the resulting
717 /// structure kmp_task_t returned by the previous call (if any).
718 /// 3. Copy a pointer to destructions function to field destructions of the
719 /// resulting structure kmp_task_t.
720 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000721 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
722 /// /*part_id*/, captured_struct */*__context*/);
723 /// \param SharedsTy A type which contains references the shared variables.
724 /// \param Shareds Context with the list of shared variables from the \p
725 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000726 /// \param Data Additional data for task generation like tiednsee, final
727 /// state, list of privates etc.
728 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
729 const OMPExecutableDirective &D,
730 llvm::Value *TaskFunction, QualType SharedsTy,
731 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000732
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000733public:
Alexey Bataev18fa2322018-05-02 14:20:50 +0000734 explicit CGOpenMPRuntime(CodeGenModule &CGM)
735 : CGOpenMPRuntime(CGM, ".", ".") {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000736 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000737 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000738
Alexey Bataev18fa2322018-05-02 14:20:50 +0000739 /// Get the platform-specific name separator.
740 std::string getName(ArrayRef<StringRef> Parts) const;
741
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000742 /// Emit code for the specified user defined reduction construct.
743 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
744 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000745 /// Get combiner/initializer for the specified user-defined reduction, if any.
746 virtual std::pair<llvm::Function *, llvm::Function *>
747 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000748
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000749 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000750 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
751 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000752 /// \param D OpenMP directive.
753 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000754 /// \param InnermostKind Kind of innermost directive (for simple directives it
755 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000756 /// \param CodeGen Code generation sequence for the \a D directive.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000757 virtual llvm::Value *emitParallelOutlinedFunction(
758 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
759 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
760
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000761 /// Emits outlined function for the specified OpenMP teams directive
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000762 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
763 /// kmp_int32 BoundID, struct context_vars*).
764 /// \param D OpenMP directive.
765 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
766 /// \param InnermostKind Kind of innermost directive (for simple directives it
767 /// is a directive itself, for combined - its innermost directive).
768 /// \param CodeGen Code generation sequence for the \a D directive.
769 virtual llvm::Value *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000770 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
771 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000772
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000773 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000774 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
775 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000776 /// \param D OpenMP directive.
777 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000778 /// \param PartIDVar Variable for partition id in the current OpenMP untied
779 /// task region.
780 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000781 /// \param InnermostKind Kind of innermost directive (for simple directives it
782 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000783 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000784 /// \param Tied true if task is generated for tied task, false otherwise.
785 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
786 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000787 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000788 virtual llvm::Value *emitTaskOutlinedFunction(
789 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000790 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
791 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
792 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000793
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000794 /// Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000795 ///
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000796 virtual void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000797
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000798 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataev1d677132015-04-22 13:57:31 +0000799 /// variables captured in a record which address is stored in \a
800 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000801 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000802 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000803 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000804 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000805 /// \param IfCond Condition in the associated 'if' clause, if it was
806 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000807 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000808 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
809 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000810 ArrayRef<llvm::Value *> CapturedVars,
811 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000812
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000813 /// Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000814 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000815 /// \param CriticalOpGen Generator for the statement associated with the given
816 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000817 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000818 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000819 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000820 SourceLocation Loc,
821 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000822
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000823 /// Emits a master region.
Alexey Bataev8d690652014-12-04 07:23:53 +0000824 /// \param MasterOpGen Generator for the statement associated with the given
825 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000826 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000827 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000828 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000829
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000830 /// Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000831 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000832
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000833 /// Emit a taskgroup region.
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000834 /// \param TaskgroupOpGen Generator for the statement associated with the
835 /// given taskgroup region.
836 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
837 const RegionCodeGenTy &TaskgroupOpGen,
838 SourceLocation Loc);
839
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000840 /// Emits a single region.
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000841 /// \param SingleOpGen Generator for the statement associated with the given
842 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000843 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000844 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000845 SourceLocation Loc,
846 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000847 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000848 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000849 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000850
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000851 /// Emit an ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000852 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000853 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000854 virtual void emitOrderedRegion(CodeGenFunction &CGF,
855 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000856 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000857
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000858 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataevf2685682015-03-30 04:30:22 +0000859 /// \param Kind Directive for which this implicit barrier call must be
860 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000861 /// \param EmitChecks true if need to emit checks for cancellation barriers.
862 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
863 /// runtime class decides which one to emit (simple or with cancellation
864 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000865 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000866 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000867 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000868 bool EmitChecks = true,
869 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000870
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000871 /// Check if the specified \a ScheduleKind is static non-chunked.
Alexander Musmanc6388682014-12-15 07:07:06 +0000872 /// This kind of worksharing directive is emitted without outer loop.
873 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
874 /// \param Chunked True if chunk is specified in the clause.
875 ///
876 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
877 bool Chunked) const;
878
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000879 /// Check if the specified \a ScheduleKind is static non-chunked.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000880 /// This kind of distribute directive is emitted without outer loop.
881 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
882 /// \param Chunked True if chunk is specified in the clause.
883 ///
884 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
885 bool Chunked) const;
886
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000887 /// Check if the specified \a ScheduleKind is dynamic.
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000888 /// This kind of worksharing directive is emitted without outer loop.
889 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
890 ///
891 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
892
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000893 /// struct with the values to be passed to the dispatch runtime function
894 struct DispatchRTInput {
895 /// Loop lower bound
896 llvm::Value *LB = nullptr;
897 /// Loop upper bound
898 llvm::Value *UB = nullptr;
899 /// Chunk size specified using 'schedule' clause (nullptr if chunk
900 /// was not specified)
901 llvm::Value *Chunk = nullptr;
902 DispatchRTInput() = default;
903 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
904 : LB(LB), UB(UB), Chunk(Chunk) {}
905 };
906
907 /// Call the appropriate runtime routine to initialize it before start
908 /// of loop.
909
910 /// This is used for non static scheduled types and when the ordered
911 /// clause is present on the loop construct.
912 /// Depending on the loop schedule, it is necessary to call some runtime
913 /// routine before start of the OpenMP loop to get the loop upper / lower
914 /// bounds \a LB and \a UB and stride \a ST.
915 ///
916 /// \param CGF Reference to current CodeGenFunction.
917 /// \param Loc Clang source location.
918 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
919 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000920 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000921 /// \param Ordered true if loop is ordered, false otherwise.
922 /// \param DispatchValues struct containing llvm values for lower bound, upper
923 /// bound, and chunk expression.
924 /// For the default (nullptr) value, the chunk 1 will be used.
925 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000926 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000927 const OpenMPScheduleTy &ScheduleKind,
928 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000929 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000930
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000931 /// Struct with the values to be passed to the static runtime function
932 struct StaticRTInput {
933 /// Size of the iteration variable in bits.
934 unsigned IVSize = 0;
935 /// Sign of the iteration variable.
936 bool IVSigned = false;
937 /// true if loop is ordered, false otherwise.
938 bool Ordered = false;
939 /// Address of the output variable in which the flag of the last iteration
940 /// is returned.
941 Address IL = Address::invalid();
942 /// Address of the output variable in which the lower iteration number is
943 /// returned.
944 Address LB = Address::invalid();
945 /// Address of the output variable in which the upper iteration number is
946 /// returned.
947 Address UB = Address::invalid();
948 /// Address of the output variable in which the stride value is returned
949 /// necessary to generated the static_chunked scheduled loop.
950 Address ST = Address::invalid();
951 /// Value of the chunk for the static_chunked scheduled loop. For the
952 /// default (nullptr) value, the chunk 1 will be used.
953 llvm::Value *Chunk = nullptr;
954 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
955 Address LB, Address UB, Address ST,
956 llvm::Value *Chunk = nullptr)
957 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
958 UB(UB), ST(ST), Chunk(Chunk) {}
959 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000960 /// Call the appropriate runtime routine to initialize it before start
Alexander Musmanc6388682014-12-15 07:07:06 +0000961 /// of loop.
962 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000963 /// This is used only in case of static schedule, when the user did not
964 /// specify a ordered clause on the loop construct.
965 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +0000966 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000967 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +0000968 ///
969 /// \param CGF Reference to current CodeGenFunction.
970 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000971 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000972 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000973 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +0000974 ///
John McCall7f416cc2015-09-08 08:05:57 +0000975 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000976 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000977 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000978 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +0000979
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000980 ///
981 /// \param CGF Reference to current CodeGenFunction.
982 /// \param Loc Clang source location.
983 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000984 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000985 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000986 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
987 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000988 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000989 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000990
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000991 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000992 /// iteration of the ordered loop with the dynamic scheduling.
993 ///
994 /// \param CGF Reference to current CodeGenFunction.
995 /// \param Loc Clang source location.
996 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000997 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000998 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000999 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
1000 SourceLocation Loc, unsigned IVSize,
1001 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001002
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001003 /// Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +00001004 /// all the work with current loop.
1005 ///
1006 /// \param CGF Reference to current CodeGenFunction.
1007 /// \param Loc Clang source location.
Alexey Bataevf43f7142017-09-06 16:17:35 +00001008 /// \param DKind Kind of the directive for which the static finish is emitted.
Alexander Musmanc6388682014-12-15 07:07:06 +00001009 ///
Alexey Bataevf43f7142017-09-06 16:17:35 +00001010 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1011 OpenMPDirectiveKind DKind);
Alexander Musmanc6388682014-12-15 07:07:06 +00001012
Alexander Musman92bdaab2015-03-12 13:37:50 +00001013 /// Call __kmpc_dispatch_next(
1014 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1015 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1016 /// kmp_int[32|64] *p_stride);
1017 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001018 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +00001019 /// \param IL Address of the output variable in which the flag of the
1020 /// last iteration is returned.
1021 /// \param LB Address of the output variable in which the lower iteration
1022 /// number is returned.
1023 /// \param UB Address of the output variable in which the upper iteration
1024 /// number is returned.
1025 /// \param ST Address of the output variable in which the stride value is
1026 /// returned.
1027 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1028 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +00001029 Address IL, Address LB,
1030 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001031
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001032 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataevb2059782014-10-13 08:23:51 +00001033 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1034 /// clause.
1035 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001036 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1037 llvm::Value *NumThreads,
1038 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001039
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001040 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataev7f210c62015-06-18 13:40:03 +00001041 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1042 virtual void emitProcBindClause(CodeGenFunction &CGF,
1043 OpenMPProcBindClauseKind ProcBind,
1044 SourceLocation Loc);
1045
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001046 /// Returns address of the threadprivate variable for the current
Alexey Bataev97720002014-11-11 04:05:39 +00001047 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +00001048 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +00001049 /// \param VDAddr Address of the global variable \a VD.
1050 /// \param Loc Location of the reference to threadprivate var.
1051 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +00001052 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
1053 const VarDecl *VD,
1054 Address VDAddr,
1055 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001056
Alexey Bataev92327c52018-03-26 16:40:55 +00001057 /// Returns the address of the variable marked as declare target with link
1058 /// clause.
Alexey Bataev03f270c2018-03-30 18:31:07 +00001059 virtual Address getAddrOfDeclareTargetLink(const VarDecl *VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001060
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001061 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataev97720002014-11-11 04:05:39 +00001062 /// a call to runtime library which adds initial value to the newly created
1063 /// threadprivate variable (if it is not constant) and registers destructor
1064 /// for the variable (if any).
1065 /// \param VD Threadprivate variable.
1066 /// \param VDAddr Address of the global variable \a VD.
1067 /// \param Loc Location of threadprivate declaration.
1068 /// \param PerformInit true if initialization expression is not constant.
1069 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +00001070 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001071 SourceLocation Loc, bool PerformInit,
1072 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001073
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001074 /// Emit a code for initialization of declare target variable.
Alexey Bataev34f8a702018-03-28 14:28:54 +00001075 /// \param VD Declare target variable.
1076 /// \param Addr Address of the global variable \a VD.
1077 /// \param PerformInit true if initialization expression is not constant.
1078 virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
1079 llvm::GlobalVariable *Addr,
1080 bool PerformInit);
1081
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001082 /// Creates artificial threadprivate variable with name \p Name and type \p
1083 /// VarType.
1084 /// \param VarType Type of the artificial threadprivate variable.
1085 /// \param Name Name of the artificial threadprivate variable.
1086 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1087 QualType VarType,
1088 StringRef Name);
1089
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001090 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001091 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001092 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1093 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001094
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001095 /// Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +00001096 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +00001097 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1098 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1099 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1100 /// function:
1101 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1102 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1103 /// return 0;
1104 /// }
1105 /// 2. Copy a list of shared variables to field shareds of the resulting
1106 /// structure kmp_task_t returned by the previous call (if any).
1107 /// 3. Copy a pointer to destructions function to field destructions of the
1108 /// resulting structure kmp_task_t.
1109 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1110 /// kmp_task_t *new_task), where new_task is a resulting structure from
1111 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001112 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +00001113 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1114 /// /*part_id*/, captured_struct */*__context*/);
1115 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001116 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +00001117 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +00001118 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1119 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001120 /// \param Data Additional data for task generation like tiednsee, final
1121 /// state, list of privates etc.
1122 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1123 const OMPExecutableDirective &D,
1124 llvm::Value *TaskFunction, QualType SharedsTy,
1125 Address Shareds, const Expr *IfCond,
1126 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001127
Alexey Bataev7292c292016-04-25 12:22:29 +00001128 /// Emit task region for the taskloop directive. The taskloop region is
1129 /// emitted in several steps:
1130 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1131 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1132 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1133 /// function:
1134 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1135 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1136 /// return 0;
1137 /// }
1138 /// 2. Copy a list of shared variables to field shareds of the resulting
1139 /// structure kmp_task_t returned by the previous call (if any).
1140 /// 3. Copy a pointer to destructions function to field destructions of the
1141 /// resulting structure kmp_task_t.
1142 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1143 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1144 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1145 /// is a resulting structure from
1146 /// previous items.
1147 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001148 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1149 /// /*part_id*/, captured_struct */*__context*/);
1150 /// \param SharedsTy A type which contains references the shared variables.
1151 /// \param Shareds Context with the list of shared variables from the \p
1152 /// TaskFunction.
1153 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1154 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001155 /// \param Data Additional data for task generation like tiednsee, final
1156 /// state, list of privates etc.
Alexey Bataev7292c292016-04-25 12:22:29 +00001157 virtual void emitTaskLoopCall(
1158 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001159 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
1160 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001161
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001162 /// Emit code for the directive that does not require outlining.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001163 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001164 /// \param InnermostKind Kind of innermost directive (for simple directives it
1165 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001166 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001167 /// \param HasCancel true if region has inner cancel directive, false
1168 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001169 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001170 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001171 const RegionCodeGenTy &CodeGen,
1172 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001173
1174 /// Emits reduction function.
1175 /// \param ArgsType Array type containing pointers to reduction variables.
1176 /// \param Privates List of private copies for original reduction arguments.
1177 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1178 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1179 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1180 /// or 'operator binop(LHS, RHS)'.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00001181 llvm::Value *emitReductionFunction(CodeGenModule &CGM, SourceLocation Loc,
1182 llvm::Type *ArgsType,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001183 ArrayRef<const Expr *> Privates,
1184 ArrayRef<const Expr *> LHSExprs,
1185 ArrayRef<const Expr *> RHSExprs,
1186 ArrayRef<const Expr *> ReductionOps);
1187
1188 /// Emits single reduction combiner
1189 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1190 const Expr *ReductionOp,
1191 const Expr *PrivateRef,
1192 const DeclRefExpr *LHS,
1193 const DeclRefExpr *RHS);
1194
1195 struct ReductionOptionsTy {
1196 bool WithNowait;
1197 bool SimpleReduction;
1198 OpenMPDirectiveKind ReductionKind;
1199 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001200 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001201 /// reduction:
1202 /// \code
1203 ///
1204 /// static kmp_critical_name lock = { 0 };
1205 ///
1206 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1207 /// ...
1208 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1209 /// ...
1210 /// }
1211 ///
1212 /// ...
1213 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1214 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1215 /// RedList, reduce_func, &<lock>)) {
1216 /// case 1:
1217 /// ...
1218 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1219 /// ...
1220 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1221 /// break;
1222 /// case 2:
1223 /// ...
1224 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1225 /// ...
1226 /// break;
1227 /// default:;
1228 /// }
1229 /// \endcode
1230 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001231 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001232 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1233 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1234 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1235 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001236 /// \param Options List of options for reduction codegen:
1237 /// WithNowait true if parent directive has also nowait clause, false
1238 /// otherwise.
1239 /// SimpleReduction Emit reduction operation only. Used for omp simd
1240 /// directive on the host.
1241 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001242 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001243 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001244 ArrayRef<const Expr *> LHSExprs,
1245 ArrayRef<const Expr *> RHSExprs,
1246 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001247 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001248
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001249 /// Emit a code for initialization of task reduction clause. Next code
1250 /// should be emitted for reduction:
1251 /// \code
1252 ///
1253 /// _task_red_item_t red_data[n];
1254 /// ...
1255 /// red_data[i].shar = &origs[i];
1256 /// red_data[i].size = sizeof(origs[i]);
1257 /// red_data[i].f_init = (void*)RedInit<i>;
1258 /// red_data[i].f_fini = (void*)RedDest<i>;
1259 /// red_data[i].f_comb = (void*)RedOp<i>;
1260 /// red_data[i].flags = <Flag_i>;
1261 /// ...
1262 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1263 /// \endcode
1264 ///
1265 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1266 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1267 /// \param Data Additional data for task generation like tiedness, final
1268 /// state, list of privates, reductions etc.
1269 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1270 SourceLocation Loc,
1271 ArrayRef<const Expr *> LHSExprs,
1272 ArrayRef<const Expr *> RHSExprs,
1273 const OMPTaskDataTy &Data);
1274
1275 /// Required to resolve existing problems in the runtime. Emits threadprivate
1276 /// variables to store the size of the VLAs/array sections for
1277 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1278 /// store the pointer to the original reduction item for the custom
1279 /// initializer defined by declare reduction construct.
1280 /// \param RCG Allows to reuse an existing data for the reductions.
1281 /// \param N Reduction item for which fixups must be emitted.
1282 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1283 ReductionCodeGen &RCG, unsigned N);
1284
1285 /// Get the address of `void *` type of the privatue copy of the reduction
1286 /// item specified by the \p SharedLVal.
1287 /// \param ReductionsPtr Pointer to the reduction data returned by the
1288 /// emitTaskReductionInit function.
1289 /// \param SharedLVal Address of the original reduction item.
1290 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1291 llvm::Value *ReductionsPtr,
1292 LValue SharedLVal);
1293
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001294 /// Emit code for 'taskwait' directive.
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001295 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001296
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001297 /// Emit code for 'cancellation point' construct.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001298 /// \param CancelRegion Region kind for which the cancellation point must be
1299 /// emitted.
1300 ///
1301 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1302 SourceLocation Loc,
1303 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001305 /// Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001306 /// \param IfCond Condition in the associated 'if' clause, if it was
1307 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001308 /// \param CancelRegion Region kind for which the cancel must be emitted.
1309 ///
1310 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001311 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001312 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001313
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001314 /// Emit outilined function for 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +00001315 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001316 /// \param ParentName Name of the function that encloses the target region.
1317 /// \param OutlinedFn Outlined function value to be defined by this call.
1318 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1319 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001320 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001321 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001322 /// evaluates to false.
1323 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1324 StringRef ParentName,
1325 llvm::Function *&OutlinedFn,
1326 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001327 bool IsOffloadEntry,
1328 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001329
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001330 /// Emit the target offloading code associated with \a D. The emitted
Samuel Antaobed3c462015-10-02 16:14:20 +00001331 /// code attempts offloading the execution to the device, an the event of
1332 /// a failure it executes the host version outlined in \a OutlinedFn.
1333 /// \param D Directive to emit.
1334 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001335 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001336 /// \param IfCond Expression evaluated in if clause associated with the target
1337 /// directive, or null if no if clause is used.
1338 /// \param Device Expression evaluated in device clause associated with the
1339 /// target directive, or null if no device clause is used.
Samuel Antaobed3c462015-10-02 16:14:20 +00001340 virtual void emitTargetCall(CodeGenFunction &CGF,
1341 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +00001342 llvm::Value *OutlinedFn,
1343 llvm::Value *OutlinedFnID, const Expr *IfCond,
Alexey Bataev8451efa2018-01-15 19:06:12 +00001344 const Expr *Device);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001345
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001346 /// Emit the target regions enclosed in \a GD function definition or
Samuel Antaoee8fb302016-01-06 13:42:12 +00001347 /// the function itself in case it is a valid device function. Returns true if
1348 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001349 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001350 virtual bool emitTargetFunctions(GlobalDecl GD);
1351
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001352 /// Emit the global variable if it is a valid device global variable.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001353 /// Returns true if \a GD was dealt with successfully.
1354 /// \param GD Variable declaration to emit.
1355 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1356
Alexey Bataev03f270c2018-03-30 18:31:07 +00001357 /// Checks if the provided global decl \a GD is a declare target variable and
1358 /// registers it when emitting code for the host.
1359 virtual void registerTargetGlobalVariable(const VarDecl *VD,
1360 llvm::Constant *Addr);
1361
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001362 /// Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001363 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001364 /// \param GD Global to scan.
1365 virtual bool emitTargetGlobal(GlobalDecl GD);
1366
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001367 /// Creates the offloading descriptor in the event any target region
Samuel Antaoee8fb302016-01-06 13:42:12 +00001368 /// was emitted in the current module and return the function that registers
1369 /// it.
1370 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001371
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001372 /// Emits code for teams call of the \a OutlinedFn with
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001373 /// variables captured in a record which address is stored in \a
1374 /// CapturedStruct.
1375 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1376 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1377 /// \param CapturedVars A pointer to the record with the references to
1378 /// variables used in \a OutlinedFn function.
1379 ///
1380 virtual void emitTeamsCall(CodeGenFunction &CGF,
1381 const OMPExecutableDirective &D,
1382 SourceLocation Loc, llvm::Value *OutlinedFn,
1383 ArrayRef<llvm::Value *> CapturedVars);
1384
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001385 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001386 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1387 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001388 /// \param NumTeams An integer expression of teams.
1389 /// \param ThreadLimit An integer expression of threads.
1390 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1391 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001392
Samuel Antaocc10b852016-07-28 14:23:26 +00001393 /// Struct that keeps all the relevant information that should be kept
1394 /// throughout a 'target data' region.
1395 class TargetDataInfo {
1396 /// Set to true if device pointer information have to be obtained.
1397 bool RequiresDevicePointerInfo = false;
1398
1399 public:
1400 /// The array of base pointer passed to the runtime library.
1401 llvm::Value *BasePointersArray = nullptr;
1402 /// The array of section pointers passed to the runtime library.
1403 llvm::Value *PointersArray = nullptr;
1404 /// The array of sizes passed to the runtime library.
1405 llvm::Value *SizesArray = nullptr;
1406 /// The array of map types passed to the runtime library.
1407 llvm::Value *MapTypesArray = nullptr;
1408 /// The total number of pointers passed to the runtime library.
1409 unsigned NumberOfPtrs = 0u;
1410 /// Map between the a declaration of a capture and the corresponding base
1411 /// pointer address where the runtime returns the device pointers.
1412 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1413
1414 explicit TargetDataInfo() {}
1415 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1416 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1417 /// Clear information about the data arrays.
1418 void clearArrayInfo() {
1419 BasePointersArray = nullptr;
1420 PointersArray = nullptr;
1421 SizesArray = nullptr;
1422 MapTypesArray = nullptr;
1423 NumberOfPtrs = 0u;
1424 }
1425 /// Return true if the current target data information has valid arrays.
1426 bool isValid() {
1427 return BasePointersArray && PointersArray && SizesArray &&
1428 MapTypesArray && NumberOfPtrs;
1429 }
1430 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1431 };
1432
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001433 /// Emit the target data mapping code associated with \a D.
Samuel Antaodf158d52016-04-27 22:58:19 +00001434 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001435 /// \param IfCond Expression evaluated in if clause associated with the
1436 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001437 /// \param Device Expression evaluated in device clause associated with the
1438 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001439 /// \param Info A record used to store information that needs to be preserved
1440 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001441 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1442 const OMPExecutableDirective &D,
1443 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001444 const RegionCodeGenTy &CodeGen,
1445 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001446
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001447 /// Emit the data mapping/movement code associated with the directive
Samuel Antao8d2d7302016-05-26 18:30:22 +00001448 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001449 /// \param D Directive to emit.
1450 /// \param IfCond Expression evaluated in if clause associated with the target
1451 /// directive, or null if no if clause is used.
1452 /// \param Device Expression evaluated in device clause associated with the
1453 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001454 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1455 const OMPExecutableDirective &D,
1456 const Expr *IfCond,
1457 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001458
1459 /// Marks function \a Fn with properly mangled versions of vector functions.
1460 /// \param FD Function marked as 'declare simd'.
1461 /// \param Fn LLVM function that must be marked with 'declare simd'
1462 /// attributes.
1463 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1464 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001465
1466 /// Emit initialization for doacross loop nesting support.
1467 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00001468 virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
1469 ArrayRef<Expr *> NumIterations);
Alexey Bataev8b427062016-05-25 12:36:08 +00001470
1471 /// Emit code for doacross ordered directive with 'depend' clause.
1472 /// \param C 'depend' clause with 'sink|source' dependency kind.
1473 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1474 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001475
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001476 /// Translates the native parameter of outlined function if this is required
1477 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001478 /// \param FD Field decl from captured record for the parameter.
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001479 /// \param NativeParam Parameter itself.
1480 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1481 const VarDecl *NativeParam) const {
1482 return NativeParam;
1483 }
1484
1485 /// Gets the address of the native argument basing on the address of the
1486 /// target-specific parameter.
1487 /// \param NativeParam Parameter itself.
1488 /// \param TargetParam Corresponding target-specific parameter.
1489 virtual Address getParameterAddress(CodeGenFunction &CGF,
1490 const VarDecl *NativeParam,
1491 const VarDecl *TargetParam) const;
1492
Gheorghe-Teodor Bercea02650d42018-09-27 19:22:56 +00001493 /// Choose default schedule type and chunk value for the
1494 /// dist_schedule clause.
1495 virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
1496 const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
1497 llvm::Value *&Chunk) const {}
1498
Gheorghe-Teodor Bercea8233af92018-09-27 20:29:00 +00001499 /// Choose default schedule type and chunk value for the
1500 /// schedule clause.
1501 virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
1502 const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
1503 llvm::Value *&Chunk) const {}
1504
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001505 /// Emits call of the outlined function with the provided arguments,
1506 /// translating these arguments to correct target-specific arguments.
1507 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001508 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
1509 llvm::Value *OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001510 ArrayRef<llvm::Value *> Args = llvm::None) const;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001511
1512 /// Emits OpenMP-specific function prolog.
1513 /// Required for device constructs.
1514 virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) {}
1515
1516 /// Gets the OpenMP-specific address of the local variable.
1517 virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
1518 const VarDecl *VD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001519
1520 /// Marks the declaration as alread emitted for the device code and returns
1521 /// true, if it was marked already, and false, otherwise.
Alexey Bataev6d944102018-05-02 15:45:28 +00001522 bool markAsGlobalTarget(GlobalDecl GD);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001523
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001524 /// Emit deferred declare target variables marked for deferred emission.
1525 void emitDeferredTargetDecls() const;
Alexey Bataev9959db52014-05-06 10:08:46 +00001526};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001527
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001528/// Class supports emissionof SIMD-only code.
1529class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
1530public:
1531 explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
1532 ~CGOpenMPSIMDRuntime() override {}
1533
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001534 /// Emits outlined function for the specified OpenMP parallel directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001535 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1536 /// kmp_int32 BoundID, struct context_vars*).
1537 /// \param D OpenMP directive.
1538 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1539 /// \param InnermostKind Kind of innermost directive (for simple directives it
1540 /// is a directive itself, for combined - its innermost directive).
1541 /// \param CodeGen Code generation sequence for the \a D directive.
1542 llvm::Value *
1543 emitParallelOutlinedFunction(const OMPExecutableDirective &D,
1544 const VarDecl *ThreadIDVar,
1545 OpenMPDirectiveKind InnermostKind,
1546 const RegionCodeGenTy &CodeGen) override;
1547
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001548 /// Emits outlined function for the specified OpenMP teams directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001549 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
1550 /// kmp_int32 BoundID, struct context_vars*).
1551 /// \param D OpenMP directive.
1552 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1553 /// \param InnermostKind Kind of innermost directive (for simple directives it
1554 /// is a directive itself, for combined - its innermost directive).
1555 /// \param CodeGen Code generation sequence for the \a D directive.
1556 llvm::Value *
1557 emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
1558 const VarDecl *ThreadIDVar,
1559 OpenMPDirectiveKind InnermostKind,
1560 const RegionCodeGenTy &CodeGen) override;
1561
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001562 /// Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001563 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
1564 /// TaskT).
1565 /// \param D OpenMP directive.
1566 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
1567 /// \param PartIDVar Variable for partition id in the current OpenMP untied
1568 /// task region.
1569 /// \param TaskTVar Variable for task_t argument.
1570 /// \param InnermostKind Kind of innermost directive (for simple directives it
1571 /// is a directive itself, for combined - its innermost directive).
1572 /// \param CodeGen Code generation sequence for the \a D directive.
1573 /// \param Tied true if task is generated for tied task, false otherwise.
1574 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
1575 /// tasks.
1576 ///
1577 llvm::Value *emitTaskOutlinedFunction(
1578 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1579 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1580 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1581 bool Tied, unsigned &NumberOfParts) override;
1582
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001583 /// Emits code for parallel or serial call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001584 /// variables captured in a record which address is stored in \a
1585 /// CapturedStruct.
1586 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
1587 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1588 /// \param CapturedVars A pointer to the record with the references to
1589 /// variables used in \a OutlinedFn function.
1590 /// \param IfCond Condition in the associated 'if' clause, if it was
1591 /// specified, nullptr otherwise.
1592 ///
1593 void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1594 llvm::Value *OutlinedFn,
1595 ArrayRef<llvm::Value *> CapturedVars,
1596 const Expr *IfCond) override;
1597
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001598 /// Emits a critical region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001599 /// \param CriticalName Name of the critical region.
1600 /// \param CriticalOpGen Generator for the statement associated with the given
1601 /// critical region.
1602 /// \param Hint Value of the 'hint' clause (optional).
1603 void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
1604 const RegionCodeGenTy &CriticalOpGen,
1605 SourceLocation Loc,
1606 const Expr *Hint = nullptr) override;
1607
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001608 /// Emits a master region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001609 /// \param MasterOpGen Generator for the statement associated with the given
1610 /// master region.
1611 void emitMasterRegion(CodeGenFunction &CGF,
1612 const RegionCodeGenTy &MasterOpGen,
1613 SourceLocation Loc) override;
1614
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001615 /// Emits code for a taskyield directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001616 void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1617
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001618 /// Emit a taskgroup region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001619 /// \param TaskgroupOpGen Generator for the statement associated with the
1620 /// given taskgroup region.
1621 void emitTaskgroupRegion(CodeGenFunction &CGF,
1622 const RegionCodeGenTy &TaskgroupOpGen,
1623 SourceLocation Loc) override;
1624
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001625 /// Emits a single region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001626 /// \param SingleOpGen Generator for the statement associated with the given
1627 /// single region.
1628 void emitSingleRegion(CodeGenFunction &CGF,
1629 const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
1630 ArrayRef<const Expr *> CopyprivateVars,
1631 ArrayRef<const Expr *> DestExprs,
1632 ArrayRef<const Expr *> SrcExprs,
1633 ArrayRef<const Expr *> AssignmentOps) override;
1634
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001635 /// Emit an ordered region.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001636 /// \param OrderedOpGen Generator for the statement associated with the given
1637 /// ordered region.
1638 void emitOrderedRegion(CodeGenFunction &CGF,
1639 const RegionCodeGenTy &OrderedOpGen,
1640 SourceLocation Loc, bool IsThreads) override;
1641
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001642 /// Emit an implicit/explicit barrier for OpenMP threads.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001643 /// \param Kind Directive for which this implicit barrier call must be
1644 /// generated. Must be OMPD_barrier for explicit barrier generation.
1645 /// \param EmitChecks true if need to emit checks for cancellation barriers.
1646 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
1647 /// runtime class decides which one to emit (simple or with cancellation
1648 /// checks).
1649 ///
1650 void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1651 OpenMPDirectiveKind Kind, bool EmitChecks = true,
1652 bool ForceSimpleCall = false) override;
1653
1654 /// This is used for non static scheduled types and when the ordered
1655 /// clause is present on the loop construct.
1656 /// Depending on the loop schedule, it is necessary to call some runtime
1657 /// routine before start of the OpenMP loop to get the loop upper / lower
1658 /// bounds \a LB and \a UB and stride \a ST.
1659 ///
1660 /// \param CGF Reference to current CodeGenFunction.
1661 /// \param Loc Clang source location.
1662 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1663 /// \param IVSize Size of the iteration variable in bits.
1664 /// \param IVSigned Sign of the iteration variable.
1665 /// \param Ordered true if loop is ordered, false otherwise.
1666 /// \param DispatchValues struct containing llvm values for lower bound, upper
1667 /// bound, and chunk expression.
1668 /// For the default (nullptr) value, the chunk 1 will be used.
1669 ///
1670 void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
1671 const OpenMPScheduleTy &ScheduleKind,
1672 unsigned IVSize, bool IVSigned, bool Ordered,
1673 const DispatchRTInput &DispatchValues) override;
1674
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001675 /// Call the appropriate runtime routine to initialize it before start
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001676 /// of loop.
1677 ///
1678 /// This is used only in case of static schedule, when the user did not
1679 /// specify a ordered clause on the loop construct.
1680 /// Depending on the loop schedule, it is necessary to call some runtime
1681 /// routine before start of the OpenMP loop to get the loop upper / lower
1682 /// bounds LB and UB and stride ST.
1683 ///
1684 /// \param CGF Reference to current CodeGenFunction.
1685 /// \param Loc Clang source location.
1686 /// \param DKind Kind of the directive.
1687 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
1688 /// \param Values Input arguments for the construct.
1689 ///
1690 void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1691 OpenMPDirectiveKind DKind,
1692 const OpenMPScheduleTy &ScheduleKind,
1693 const StaticRTInput &Values) override;
1694
1695 ///
1696 /// \param CGF Reference to current CodeGenFunction.
1697 /// \param Loc Clang source location.
1698 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
1699 /// \param Values Input arguments for the construct.
1700 ///
1701 void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
1702 OpenMPDistScheduleClauseKind SchedKind,
1703 const StaticRTInput &Values) override;
1704
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001705 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001706 /// iteration of the ordered loop with the dynamic scheduling.
1707 ///
1708 /// \param CGF Reference to current CodeGenFunction.
1709 /// \param Loc Clang source location.
1710 /// \param IVSize Size of the iteration variable in bits.
1711 /// \param IVSigned Sign of the iteration variable.
1712 ///
1713 void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
1714 unsigned IVSize, bool IVSigned) override;
1715
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001716 /// Call the appropriate runtime routine to notify that we finished
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001717 /// all the work with current loop.
1718 ///
1719 /// \param CGF Reference to current CodeGenFunction.
1720 /// \param Loc Clang source location.
1721 /// \param DKind Kind of the directive for which the static finish is emitted.
1722 ///
1723 void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
1724 OpenMPDirectiveKind DKind) override;
1725
1726 /// Call __kmpc_dispatch_next(
1727 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
1728 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
1729 /// kmp_int[32|64] *p_stride);
1730 /// \param IVSize Size of the iteration variable in bits.
1731 /// \param IVSigned Sign of the iteration variable.
1732 /// \param IL Address of the output variable in which the flag of the
1733 /// last iteration is returned.
1734 /// \param LB Address of the output variable in which the lower iteration
1735 /// number is returned.
1736 /// \param UB Address of the output variable in which the upper iteration
1737 /// number is returned.
1738 /// \param ST Address of the output variable in which the stride value is
1739 /// returned.
1740 llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
1741 unsigned IVSize, bool IVSigned, Address IL,
1742 Address LB, Address UB, Address ST) override;
1743
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001744 /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001745 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
1746 /// clause.
1747 /// \param NumThreads An integer value of threads.
1748 void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1749 SourceLocation Loc) override;
1750
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001751 /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001752 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
1753 void emitProcBindClause(CodeGenFunction &CGF,
1754 OpenMPProcBindClauseKind ProcBind,
1755 SourceLocation Loc) override;
1756
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001757 /// Returns address of the threadprivate variable for the current
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001758 /// thread.
1759 /// \param VD Threadprivate variable.
1760 /// \param VDAddr Address of the global variable \a VD.
1761 /// \param Loc Location of the reference to threadprivate var.
1762 /// \return Address of the threadprivate variable for the current thread.
1763 Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
1764 Address VDAddr, SourceLocation Loc) override;
1765
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001766 /// Emit a code for initialization of threadprivate variable. It emits
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001767 /// a call to runtime library which adds initial value to the newly created
1768 /// threadprivate variable (if it is not constant) and registers destructor
1769 /// for the variable (if any).
1770 /// \param VD Threadprivate variable.
1771 /// \param VDAddr Address of the global variable \a VD.
1772 /// \param Loc Location of threadprivate declaration.
1773 /// \param PerformInit true if initialization expression is not constant.
1774 llvm::Function *
1775 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
1776 SourceLocation Loc, bool PerformInit,
1777 CodeGenFunction *CGF = nullptr) override;
1778
1779 /// Creates artificial threadprivate variable with name \p Name and type \p
1780 /// VarType.
1781 /// \param VarType Type of the artificial threadprivate variable.
1782 /// \param Name Name of the artificial threadprivate variable.
1783 Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
1784 QualType VarType,
1785 StringRef Name) override;
1786
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001787 /// Emit flush of the variables specified in 'omp flush' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001788 /// \param Vars List of variables to flush.
1789 void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
1790 SourceLocation Loc) override;
1791
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001792 /// Emit task region for the task directive. The task region is
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001793 /// emitted in several steps:
1794 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1795 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1796 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1797 /// function:
1798 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1799 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1800 /// return 0;
1801 /// }
1802 /// 2. Copy a list of shared variables to field shareds of the resulting
1803 /// structure kmp_task_t returned by the previous call (if any).
1804 /// 3. Copy a pointer to destructions function to field destructions of the
1805 /// resulting structure kmp_task_t.
1806 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
1807 /// kmp_task_t *new_task), where new_task is a resulting structure from
1808 /// previous items.
1809 /// \param D Current task directive.
1810 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1811 /// /*part_id*/, captured_struct */*__context*/);
1812 /// \param SharedsTy A type which contains references the shared variables.
1813 /// \param Shareds Context with the list of shared variables from the \p
1814 /// TaskFunction.
1815 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1816 /// otherwise.
1817 /// \param Data Additional data for task generation like tiednsee, final
1818 /// state, list of privates etc.
1819 void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
1820 const OMPExecutableDirective &D, llvm::Value *TaskFunction,
1821 QualType SharedsTy, Address Shareds, const Expr *IfCond,
1822 const OMPTaskDataTy &Data) override;
1823
1824 /// Emit task region for the taskloop directive. The taskloop region is
1825 /// emitted in several steps:
1826 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
1827 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1828 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
1829 /// function:
1830 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1831 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1832 /// return 0;
1833 /// }
1834 /// 2. Copy a list of shared variables to field shareds of the resulting
1835 /// structure kmp_task_t returned by the previous call (if any).
1836 /// 3. Copy a pointer to destructions function to field destructions of the
1837 /// resulting structure kmp_task_t.
1838 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1839 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1840 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1841 /// is a resulting structure from
1842 /// previous items.
1843 /// \param D Current task directive.
1844 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1845 /// /*part_id*/, captured_struct */*__context*/);
1846 /// \param SharedsTy A type which contains references the shared variables.
1847 /// \param Shareds Context with the list of shared variables from the \p
1848 /// TaskFunction.
1849 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1850 /// otherwise.
1851 /// \param Data Additional data for task generation like tiednsee, final
1852 /// state, list of privates etc.
1853 void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
1854 const OMPLoopDirective &D, llvm::Value *TaskFunction,
1855 QualType SharedsTy, Address Shareds, const Expr *IfCond,
1856 const OMPTaskDataTy &Data) override;
1857
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001858 /// Emit a code for reduction clause. Next code should be emitted for
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001859 /// reduction:
1860 /// \code
1861 ///
1862 /// static kmp_critical_name lock = { 0 };
1863 ///
1864 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1865 /// ...
1866 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1867 /// ...
1868 /// }
1869 ///
1870 /// ...
1871 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1872 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1873 /// RedList, reduce_func, &<lock>)) {
1874 /// case 1:
1875 /// ...
1876 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1877 /// ...
1878 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1879 /// break;
1880 /// case 2:
1881 /// ...
1882 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1883 /// ...
1884 /// break;
1885 /// default:;
1886 /// }
1887 /// \endcode
1888 ///
1889 /// \param Privates List of private copies for original reduction arguments.
1890 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1891 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1892 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1893 /// or 'operator binop(LHS, RHS)'.
1894 /// \param Options List of options for reduction codegen:
1895 /// WithNowait true if parent directive has also nowait clause, false
1896 /// otherwise.
1897 /// SimpleReduction Emit reduction operation only. Used for omp simd
1898 /// directive on the host.
1899 /// ReductionKind The kind of reduction to perform.
1900 void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
1901 ArrayRef<const Expr *> Privates,
1902 ArrayRef<const Expr *> LHSExprs,
1903 ArrayRef<const Expr *> RHSExprs,
1904 ArrayRef<const Expr *> ReductionOps,
1905 ReductionOptionsTy Options) override;
1906
1907 /// Emit a code for initialization of task reduction clause. Next code
1908 /// should be emitted for reduction:
1909 /// \code
1910 ///
1911 /// _task_red_item_t red_data[n];
1912 /// ...
1913 /// red_data[i].shar = &origs[i];
1914 /// red_data[i].size = sizeof(origs[i]);
1915 /// red_data[i].f_init = (void*)RedInit<i>;
1916 /// red_data[i].f_fini = (void*)RedDest<i>;
1917 /// red_data[i].f_comb = (void*)RedOp<i>;
1918 /// red_data[i].flags = <Flag_i>;
1919 /// ...
1920 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1921 /// \endcode
1922 ///
1923 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1924 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1925 /// \param Data Additional data for task generation like tiedness, final
1926 /// state, list of privates, reductions etc.
1927 llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
1928 ArrayRef<const Expr *> LHSExprs,
1929 ArrayRef<const Expr *> RHSExprs,
1930 const OMPTaskDataTy &Data) override;
1931
1932 /// Required to resolve existing problems in the runtime. Emits threadprivate
1933 /// variables to store the size of the VLAs/array sections for
1934 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1935 /// store the pointer to the original reduction item for the custom
1936 /// initializer defined by declare reduction construct.
1937 /// \param RCG Allows to reuse an existing data for the reductions.
1938 /// \param N Reduction item for which fixups must be emitted.
1939 void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1940 ReductionCodeGen &RCG, unsigned N) override;
1941
1942 /// Get the address of `void *` type of the privatue copy of the reduction
1943 /// item specified by the \p SharedLVal.
1944 /// \param ReductionsPtr Pointer to the reduction data returned by the
1945 /// emitTaskReductionInit function.
1946 /// \param SharedLVal Address of the original reduction item.
1947 Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1948 llvm::Value *ReductionsPtr,
1949 LValue SharedLVal) override;
1950
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001951 /// Emit code for 'taskwait' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001952 void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override;
1953
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001954 /// Emit code for 'cancellation point' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001955 /// \param CancelRegion Region kind for which the cancellation point must be
1956 /// emitted.
1957 ///
1958 void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
1959 OpenMPDirectiveKind CancelRegion) override;
1960
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001961 /// Emit code for 'cancel' construct.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001962 /// \param IfCond Condition in the associated 'if' clause, if it was
1963 /// specified, nullptr otherwise.
1964 /// \param CancelRegion Region kind for which the cancel must be emitted.
1965 ///
1966 void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
1967 const Expr *IfCond,
1968 OpenMPDirectiveKind CancelRegion) override;
1969
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001970 /// Emit outilined function for 'target' directive.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001971 /// \param D Directive to emit.
1972 /// \param ParentName Name of the function that encloses the target region.
1973 /// \param OutlinedFn Outlined function value to be defined by this call.
1974 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1975 /// \param IsOffloadEntry True if the outlined function is an offload entry.
1976 /// \param CodeGen Code generation sequence for the \a D directive.
1977 /// An outlined function may not be an entry if, e.g. the if clause always
1978 /// evaluates to false.
1979 void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1980 StringRef ParentName,
1981 llvm::Function *&OutlinedFn,
1982 llvm::Constant *&OutlinedFnID,
1983 bool IsOffloadEntry,
1984 const RegionCodeGenTy &CodeGen) override;
1985
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001986 /// Emit the target offloading code associated with \a D. The emitted
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001987 /// code attempts offloading the execution to the device, an the event of
1988 /// a failure it executes the host version outlined in \a OutlinedFn.
1989 /// \param D Directive to emit.
1990 /// \param OutlinedFn Host version of the code to be offloaded.
1991 /// \param OutlinedFnID ID of host version of the code to be offloaded.
1992 /// \param IfCond Expression evaluated in if clause associated with the target
1993 /// directive, or null if no if clause is used.
1994 /// \param Device Expression evaluated in device clause associated with the
1995 /// target directive, or null if no device clause is used.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001996 void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
1997 llvm::Value *OutlinedFn, llvm::Value *OutlinedFnID,
Alexey Bataev8451efa2018-01-15 19:06:12 +00001998 const Expr *IfCond, const Expr *Device) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00001999
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002000 /// Emit the target regions enclosed in \a GD function definition or
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002001 /// the function itself in case it is a valid device function. Returns true if
2002 /// \a GD was dealt with successfully.
2003 /// \param GD Function to scan.
2004 bool emitTargetFunctions(GlobalDecl GD) override;
2005
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002006 /// Emit the global variable if it is a valid device global variable.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002007 /// Returns true if \a GD was dealt with successfully.
2008 /// \param GD Variable declaration to emit.
2009 bool emitTargetGlobalVariable(GlobalDecl GD) override;
2010
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002011 /// Emit the global \a GD if it is meaningful for the target. Returns
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002012 /// if it was emitted successfully.
2013 /// \param GD Global to scan.
2014 bool emitTargetGlobal(GlobalDecl GD) override;
2015
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002016 /// Creates the offloading descriptor in the event any target region
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002017 /// was emitted in the current module and return the function that registers
2018 /// it.
2019 llvm::Function *emitRegistrationFunction() override;
2020
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002021 /// Emits code for teams call of the \a OutlinedFn with
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002022 /// variables captured in a record which address is stored in \a
2023 /// CapturedStruct.
2024 /// \param OutlinedFn Outlined function to be run by team masters. Type of
2025 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
2026 /// \param CapturedVars A pointer to the record with the references to
2027 /// variables used in \a OutlinedFn function.
2028 ///
2029 void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
2030 SourceLocation Loc, llvm::Value *OutlinedFn,
2031 ArrayRef<llvm::Value *> CapturedVars) override;
2032
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002033 /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002034 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
2035 /// for num_teams clause.
2036 /// \param NumTeams An integer expression of teams.
2037 /// \param ThreadLimit An integer expression of threads.
2038 void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
2039 const Expr *ThreadLimit, SourceLocation Loc) override;
2040
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002041 /// Emit the target data mapping code associated with \a D.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002042 /// \param D Directive to emit.
2043 /// \param IfCond Expression evaluated in if clause associated with the
2044 /// target directive, or null if no device clause is used.
2045 /// \param Device Expression evaluated in device clause associated with the
2046 /// target directive, or null if no device clause is used.
2047 /// \param Info A record used to store information that needs to be preserved
2048 /// until the region is closed.
2049 void emitTargetDataCalls(CodeGenFunction &CGF,
2050 const OMPExecutableDirective &D, const Expr *IfCond,
2051 const Expr *Device, const RegionCodeGenTy &CodeGen,
2052 TargetDataInfo &Info) override;
2053
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002054 /// Emit the data mapping/movement code associated with the directive
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002055 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
2056 /// \param D Directive to emit.
2057 /// \param IfCond Expression evaluated in if clause associated with the target
2058 /// directive, or null if no if clause is used.
2059 /// \param Device Expression evaluated in device clause associated with the
2060 /// target directive, or null if no device clause is used.
2061 void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
2062 const OMPExecutableDirective &D,
2063 const Expr *IfCond,
2064 const Expr *Device) override;
2065
2066 /// Emit initialization for doacross loop nesting support.
2067 /// \param D Loop-based construct used in doacross nesting construct.
Alexey Bataevf138fda2018-08-13 19:04:24 +00002068 void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
2069 ArrayRef<Expr *> NumIterations) override;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002070
2071 /// Emit code for doacross ordered directive with 'depend' clause.
2072 /// \param C 'depend' clause with 'sink|source' dependency kind.
2073 void emitDoacrossOrdered(CodeGenFunction &CGF,
2074 const OMPDependClause *C) override;
2075
2076 /// Translates the native parameter of outlined function if this is required
2077 /// for target.
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002078 /// \param FD Field decl from captured record for the parameter.
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00002079 /// \param NativeParam Parameter itself.
2080 const VarDecl *translateParameter(const FieldDecl *FD,
2081 const VarDecl *NativeParam) const override;
2082
2083 /// Gets the address of the native argument basing on the address of the
2084 /// target-specific parameter.
2085 /// \param NativeParam Parameter itself.
2086 /// \param TargetParam Corresponding target-specific parameter.
2087 Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
2088 const VarDecl *TargetParam) const override;
2089};
2090
Alexey Bataev23b69422014-06-18 07:08:49 +00002091} // namespace CodeGen
2092} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00002093
2094#endif