1 | //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This provides a class for OpenMP runtime code generation. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H |
14 | #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H |
15 | |
16 | #include "CGValue.h" |
17 | #include "clang/AST/DeclOpenMP.h" |
18 | #include "clang/AST/Type.h" |
19 | #include "clang/Basic/OpenMPKinds.h" |
20 | #include "clang/Basic/SourceLocation.h" |
21 | #include "llvm/ADT/DenseMap.h" |
22 | #include "llvm/ADT/StringMap.h" |
23 | #include "llvm/ADT/StringSet.h" |
24 | #include "llvm/IR/Function.h" |
25 | #include "llvm/IR/ValueHandle.h" |
26 | |
27 | namespace llvm { |
28 | class ArrayType; |
29 | class Constant; |
30 | class FunctionType; |
31 | class GlobalVariable; |
32 | class StructType; |
33 | class Type; |
34 | class Value; |
35 | } // namespace llvm |
36 | |
37 | namespace clang { |
38 | class Expr; |
39 | class GlobalDecl; |
40 | class OMPDependClause; |
41 | class OMPExecutableDirective; |
42 | class OMPLoopDirective; |
43 | class VarDecl; |
44 | class OMPDeclareReductionDecl; |
45 | class IdentifierInfo; |
46 | |
47 | namespace CodeGen { |
48 | class Address; |
49 | class CodeGenFunction; |
50 | class CodeGenModule; |
51 | |
52 | /// A basic class for pre|post-action for advanced codegen sequence for OpenMP |
53 | /// region. |
54 | class PrePostActionTy { |
55 | public: |
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. |
64 | class 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 | |
77 | public: |
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 | }; |
90 | |
91 | struct 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; |
97 | SmallVector<const Expr *, 4> LastprivateVars; |
98 | SmallVector<const Expr *, 4> LastprivateCopies; |
99 | SmallVector<const Expr *, 4> ReductionVars; |
100 | SmallVector<const Expr *, 4> ReductionCopies; |
101 | SmallVector<const Expr *, 4> ReductionOps; |
102 | SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences; |
103 | llvm::PointerIntPair<llvm::Value *, 1, bool> Final; |
104 | llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule; |
105 | llvm::PointerIntPair<llvm::Value *, 1, bool> Priority; |
106 | llvm::Value *Reductions = nullptr; |
107 | unsigned NumberOfParts = 0; |
108 | bool Tied = true; |
109 | bool Nogroup = false; |
110 | }; |
111 | |
112 | /// Class intended to support codegen of all kind of the reduction clauses. |
113 | class ReductionCodeGen { |
114 | private: |
115 | /// Data required for codegen of reduction clauses. |
116 | 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> ; |
131 | /// Sizes of the reduction items in chars. |
132 | SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes; |
133 | /// Base declarations for the reduction items. |
134 | SmallVector<const VarDecl *, 4> BaseDecls; |
135 | |
136 | /// Emits lvalue for shared expression. |
137 | 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. |
143 | /// \param SharedLVal Address of the original shared variable. |
144 | /// \param DRD Declare reduction construct used for reduction item. |
145 | void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N, |
146 | Address PrivateAddr, LValue , |
147 | const OMPDeclareReductionDecl *DRD); |
148 | |
149 | public: |
150 | ReductionCodeGen(ArrayRef<const Expr *> , |
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. |
168 | /// \param SharedLVal Address of the original shared variable. |
169 | void |
170 | emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr, |
171 | LValue , |
172 | llvm::function_ref<bool(CodeGenFunction &)> DefaultInit); |
173 | /// Returns true if the private copy requires cleanups. |
174 | bool needCleanups(unsigned N); |
175 | /// Emits cleanup code for the reduction item. |
176 | /// \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); |
179 | /// Adjusts \p PrivatedAddr for using instead of the original variable |
180 | /// 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 (unsigned N) const { return SharedAddresses[N].first; } |
187 | /// 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 | } |
192 | /// Returns the base declaration of the reduction item. |
193 | const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; } |
194 | /// Returns the base declaration of the reduction item. |
195 | const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; } |
196 | /// Returns true if the initialization of the reduction item uses initializer |
197 | /// from declare reduction construct. |
198 | bool usesReductionInitializer(unsigned N) const; |
199 | }; |
200 | |
201 | class CGOpenMPRuntime { |
202 | public: |
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 | |
214 | protected: |
215 | CodeGenModule &CGM; |
216 | 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); |
221 | |
222 | /// Creates offloading entry for the provided entry ID \a ID, |
223 | /// address \a Addr, size \a Size, and flags \a Flags. |
224 | virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr, |
225 | uint64_t Size, int32_t Flags, |
226 | llvm::GlobalValue::LinkageTypes Linkage); |
227 | |
228 | /// Helper to emit outlined function for 'target' directive. |
229 | /// \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. |
235 | /// An outlined function may not be an entry if, e.g. the if clause always |
236 | /// 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 | |
244 | /// Emits code for OpenMP 'if' clause using specified \a CodeGen |
245 | /// 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 | |
255 | /// Emits object of ident_t type with info for source location. |
256 | /// \param Flags Flags for OpenMP location. |
257 | /// |
258 | llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc, |
259 | unsigned Flags = 0); |
260 | |
261 | /// Returns pointer to ident_t type. |
262 | llvm::Type *getIdentTyPointerTy(); |
263 | |
264 | /// Gets thread id value for the current thread. |
265 | /// |
266 | llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc); |
267 | |
268 | /// Get the function name of an outlined region. |
269 | // The name can be customized depending on the target. |
270 | // |
271 | virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; } |
272 | |
273 | /// Emits \p Callee function call with arguments \p Args with location \p Loc. |
274 | void emitCall(CodeGenFunction &CGF, SourceLocation Loc, |
275 | llvm::FunctionCallee Callee, |
276 | ArrayRef<llvm::Value *> Args = llvm::None) const; |
277 | |
278 | /// Emits address of the word in a memory where current thread id is |
279 | /// stored. |
280 | virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc); |
281 | |
282 | void setLocThreadIdInsertPt(CodeGenFunction &CGF, |
283 | bool AtCurrentPoint = false); |
284 | void clearLocThreadIdInsertPt(CodeGenFunction &CGF); |
285 | |
286 | /// Check if the default location must be constant. |
287 | /// Default is false to support OMPT/OMPD. |
288 | virtual bool isDefaultLocationConstant() const { return false; } |
289 | |
290 | /// Returns additional flags that can be stored in reserved_2 field of the |
291 | /// default location. |
292 | virtual unsigned getDefaultLocationReserved2Flags() const { return 0; } |
293 | |
294 | /// Returns default flags for the barriers depending on the directive, for |
295 | /// which this barier is going to be emitted. |
296 | static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind); |
297 | |
298 | /// Get the LLVM type for the critical name. |
299 | llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;} |
300 | |
301 | /// Returns corresponding lock object for the specified critical region |
302 | /// name. If the lock object does not exist it is created, otherwise the |
303 | /// reference to the existing copy is returned. |
304 | /// \param CriticalName Name of the critical region. |
305 | /// |
306 | llvm::Value *getCriticalRegionLock(StringRef CriticalName); |
307 | |
308 | private: |
309 | /// Default const ident_t object used for initialization of all other |
310 | /// ident_t objects. |
311 | llvm::Constant *DefaultOpenMPPSource = nullptr; |
312 | using FlagsTy = std::pair<unsigned, unsigned>; |
313 | /// Map of flags and corresponding default locations. |
314 | using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>; |
315 | OpenMPDefaultLocMapTy OpenMPDefaultLocMap; |
316 | Address getOrCreateDefaultLocation(unsigned Flags); |
317 | |
318 | QualType IdentQTy; |
319 | llvm::StructType *IdentTy = nullptr; |
320 | /// Map for SourceLocation and OpenMP runtime library debug locations. |
321 | typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy; |
322 | OpenMPDebugLocMapTy OpenMPDebugLocMap; |
323 | /// The type for a microtask which gets passed to __kmpc_fork_call(). |
324 | /// Original representation is: |
325 | /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...); |
326 | llvm::FunctionType *Kmpc_MicroTy = nullptr; |
327 | /// Stores debug location and ThreadID for the function. |
328 | struct DebugLocThreadIdTy { |
329 | llvm::Value *DebugLoc; |
330 | llvm::Value *ThreadID; |
331 | /// Insert point for the service instructions. |
332 | llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr; |
333 | }; |
334 | /// Map of local debug location, ThreadId and functions. |
335 | typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy> |
336 | OpenMPLocThreadIDMapTy; |
337 | OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap; |
338 | /// Map of UDRs and corresponding combiner/initializer. |
339 | typedef llvm::DenseMap<const OMPDeclareReductionDecl *, |
340 | std::pair<llvm::Function *, llvm::Function *>> |
341 | UDRMapTy; |
342 | UDRMapTy UDRMap; |
343 | /// Map of functions and locally defined UDRs. |
344 | typedef llvm::DenseMap<llvm::Function *, |
345 | SmallVector<const OMPDeclareReductionDecl *, 4>> |
346 | FunctionUDRMapTy; |
347 | FunctionUDRMapTy FunctionUDRMap; |
348 | /// Type kmp_critical_name, originally defined as typedef kmp_int32 |
349 | /// kmp_critical_name[8]; |
350 | llvm::ArrayType *KmpCriticalNameTy; |
351 | /// An ordered map of auto-generated variables to their unique names. |
352 | /// It stores variables with the following names: 1) ".gomp_critical_user_" + |
353 | /// <critical_section_name> + ".var" for "omp critical" directives; 2) |
354 | /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate |
355 | /// variables. |
356 | llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator> |
357 | InternalVars; |
358 | /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); |
359 | llvm::Type *KmpRoutineEntryPtrTy = nullptr; |
360 | QualType KmpRoutineEntryPtrQTy; |
361 | /// Type typedef struct kmp_task { |
362 | /// void * shareds; /**< pointer to block of pointers to |
363 | /// shared vars */ |
364 | /// kmp_routine_entry_t routine; /**< pointer to routine to call for |
365 | /// executing task */ |
366 | /// kmp_int32 part_id; /**< part id for the task */ |
367 | /// kmp_routine_entry_t destructors; /* pointer to function to invoke |
368 | /// deconstructors of firstprivate C++ objects */ |
369 | /// } kmp_task_t; |
370 | QualType KmpTaskTQTy; |
371 | /// Saved kmp_task_t for task directive. |
372 | QualType SavedKmpTaskTQTy; |
373 | /// Saved kmp_task_t for taskloop-based directive. |
374 | QualType SavedKmpTaskloopTQTy; |
375 | /// Type typedef struct kmp_depend_info { |
376 | /// kmp_intptr_t base_addr; |
377 | /// size_t len; |
378 | /// struct { |
379 | /// bool in:1; |
380 | /// bool out:1; |
381 | /// } flags; |
382 | /// } kmp_depend_info_t; |
383 | QualType KmpDependInfoTy; |
384 | /// struct kmp_dim { // loop bounds info casted to kmp_int64 |
385 | /// kmp_int64 lo; // lower |
386 | /// kmp_int64 up; // upper |
387 | /// kmp_int64 st; // stride |
388 | /// }; |
389 | QualType KmpDimTy; |
390 | /// Type struct __tgt_offload_entry{ |
391 | /// void *addr; // Pointer to the offload entry info. |
392 | /// // (function or global) |
393 | /// char *name; // Name of the function or global. |
394 | /// size_t size; // Size of the entry info (0 if it a function). |
395 | /// }; |
396 | QualType TgtOffloadEntryQTy; |
397 | /// struct __tgt_device_image{ |
398 | /// void *ImageStart; // Pointer to the target code start. |
399 | /// void *ImageEnd; // Pointer to the target code end. |
400 | /// // We also add the host entries to the device image, as it may be useful |
401 | /// // for the target runtime to have access to that information. |
402 | /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all |
403 | /// // the entries. |
404 | /// __tgt_offload_entry *EntriesEnd; // End of the table with all the |
405 | /// // entries (non inclusive). |
406 | /// }; |
407 | QualType TgtDeviceImageQTy; |
408 | /// struct __tgt_bin_desc{ |
409 | /// int32_t NumDevices; // Number of devices supported. |
410 | /// __tgt_device_image *DeviceImages; // Arrays of device images |
411 | /// // (one per device). |
412 | /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the |
413 | /// // entries. |
414 | /// __tgt_offload_entry *EntriesEnd; // End of the table with all the |
415 | /// // entries (non inclusive). |
416 | /// }; |
417 | QualType TgtBinaryDescriptorQTy; |
418 | /// Entity that registers the offloading constants that were emitted so |
419 | /// far. |
420 | class OffloadEntriesInfoManagerTy { |
421 | CodeGenModule &CGM; |
422 | |
423 | /// Number of entries registered so far. |
424 | unsigned OffloadingEntriesNum = 0; |
425 | |
426 | public: |
427 | /// Base class of the entries info. |
428 | class OffloadEntryInfo { |
429 | public: |
430 | /// Kind of a given entry. |
431 | enum OffloadingEntryInfoKinds : unsigned { |
432 | /// Entry is a target region. |
433 | OffloadingEntryInfoTargetRegion = 0, |
434 | /// Entry is a declare target variable. |
435 | OffloadingEntryInfoDeviceGlobalVar = 1, |
436 | /// Invalid entry info. |
437 | OffloadingEntryInfoInvalid = ~0u |
438 | }; |
439 | |
440 | protected: |
441 | OffloadEntryInfo() = delete; |
442 | explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind) : Kind(Kind) {} |
443 | explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order, |
444 | uint32_t Flags) |
445 | : Flags(Flags), Order(Order), Kind(Kind) {} |
446 | ~OffloadEntryInfo() = default; |
447 | |
448 | public: |
449 | bool isValid() const { return Order != ~0u; } |
450 | unsigned getOrder() const { return Order; } |
451 | OffloadingEntryInfoKinds getKind() const { return Kind; } |
452 | uint32_t getFlags() const { return Flags; } |
453 | void setFlags(uint32_t NewFlags) { Flags = NewFlags; } |
454 | llvm::Constant *getAddress() const { |
455 | return cast_or_null<llvm::Constant>(Addr); |
456 | } |
457 | void setAddress(llvm::Constant *V) { |
458 | (0) . __assert_fail ("!Addr.pointsToAliveValue() && \"Address has been set before!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGOpenMPRuntime.h", 458, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Addr.pointsToAliveValue() && "Address has been set before!"); |
459 | Addr = V; |
460 | } |
461 | static bool classof(const OffloadEntryInfo *Info) { return true; } |
462 | |
463 | private: |
464 | /// Address of the entity that has to be mapped for offloading. |
465 | llvm::WeakTrackingVH Addr; |
466 | |
467 | /// Flags associated with the device global. |
468 | uint32_t Flags = 0u; |
469 | |
470 | /// Order this entry was emitted. |
471 | unsigned Order = ~0u; |
472 | |
473 | OffloadingEntryInfoKinds Kind = OffloadingEntryInfoInvalid; |
474 | }; |
475 | |
476 | /// Return true if a there are no entries defined. |
477 | bool empty() const; |
478 | /// Return number of entries defined so far. |
479 | unsigned size() const { return OffloadingEntriesNum; } |
480 | OffloadEntriesInfoManagerTy(CodeGenModule &CGM) : CGM(CGM) {} |
481 | |
482 | // |
483 | // Target region entries related. |
484 | // |
485 | |
486 | /// Kind of the target registry entry. |
487 | enum OMPTargetRegionEntryKind : uint32_t { |
488 | /// Mark the entry as target region. |
489 | OMPTargetRegionEntryTargetRegion = 0x0, |
490 | /// Mark the entry as a global constructor. |
491 | OMPTargetRegionEntryCtor = 0x02, |
492 | /// Mark the entry as a global destructor. |
493 | OMPTargetRegionEntryDtor = 0x04, |
494 | }; |
495 | |
496 | /// Target region entries info. |
497 | class OffloadEntryInfoTargetRegion final : public OffloadEntryInfo { |
498 | /// Address that can be used as the ID of the entry. |
499 | llvm::Constant *ID = nullptr; |
500 | |
501 | public: |
502 | OffloadEntryInfoTargetRegion() |
503 | : OffloadEntryInfo(OffloadingEntryInfoTargetRegion) {} |
504 | explicit OffloadEntryInfoTargetRegion(unsigned Order, |
505 | llvm::Constant *Addr, |
506 | llvm::Constant *ID, |
507 | OMPTargetRegionEntryKind Flags) |
508 | : OffloadEntryInfo(OffloadingEntryInfoTargetRegion, Order, Flags), |
509 | ID(ID) { |
510 | setAddress(Addr); |
511 | } |
512 | |
513 | llvm::Constant *getID() const { return ID; } |
514 | void setID(llvm::Constant *V) { |
515 | (0) . __assert_fail ("!ID && \"ID has been set before!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGOpenMPRuntime.h", 515, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ID && "ID has been set before!"); |
516 | ID = V; |
517 | } |
518 | static bool classof(const OffloadEntryInfo *Info) { |
519 | return Info->getKind() == OffloadingEntryInfoTargetRegion; |
520 | } |
521 | }; |
522 | |
523 | /// Initialize target region entry. |
524 | void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
525 | StringRef ParentName, unsigned LineNum, |
526 | unsigned Order); |
527 | /// Register target region entry. |
528 | void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
529 | StringRef ParentName, unsigned LineNum, |
530 | llvm::Constant *Addr, llvm::Constant *ID, |
531 | OMPTargetRegionEntryKind Flags); |
532 | /// Return true if a target region entry with the provided information |
533 | /// exists. |
534 | bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID, |
535 | StringRef ParentName, unsigned LineNum) const; |
536 | /// brief Applies action \a Action on all registered entries. |
537 | typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned, |
538 | const OffloadEntryInfoTargetRegion &)> |
539 | OffloadTargetRegionEntryInfoActTy; |
540 | void actOnTargetRegionEntriesInfo( |
541 | const OffloadTargetRegionEntryInfoActTy &Action); |
542 | |
543 | // |
544 | // Device global variable entries related. |
545 | // |
546 | |
547 | /// Kind of the global variable entry.. |
548 | enum OMPTargetGlobalVarEntryKind : uint32_t { |
549 | /// Mark the entry as a to declare target. |
550 | OMPTargetGlobalVarEntryTo = 0x0, |
551 | /// Mark the entry as a to declare target link. |
552 | OMPTargetGlobalVarEntryLink = 0x1, |
553 | }; |
554 | |
555 | /// Device global variable entries info. |
556 | class OffloadEntryInfoDeviceGlobalVar final : public OffloadEntryInfo { |
557 | /// Type of the global variable. |
558 | CharUnits VarSize; |
559 | llvm::GlobalValue::LinkageTypes Linkage; |
560 | |
561 | public: |
562 | OffloadEntryInfoDeviceGlobalVar() |
563 | : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar) {} |
564 | explicit OffloadEntryInfoDeviceGlobalVar(unsigned Order, |
565 | OMPTargetGlobalVarEntryKind Flags) |
566 | : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags) {} |
567 | explicit OffloadEntryInfoDeviceGlobalVar( |
568 | unsigned Order, llvm::Constant *Addr, CharUnits VarSize, |
569 | OMPTargetGlobalVarEntryKind Flags, |
570 | llvm::GlobalValue::LinkageTypes Linkage) |
571 | : OffloadEntryInfo(OffloadingEntryInfoDeviceGlobalVar, Order, Flags), |
572 | VarSize(VarSize), Linkage(Linkage) { |
573 | setAddress(Addr); |
574 | } |
575 | |
576 | CharUnits getVarSize() const { return VarSize; } |
577 | void setVarSize(CharUnits Size) { VarSize = Size; } |
578 | llvm::GlobalValue::LinkageTypes getLinkage() const { return Linkage; } |
579 | void setLinkage(llvm::GlobalValue::LinkageTypes LT) { Linkage = LT; } |
580 | static bool classof(const OffloadEntryInfo *Info) { |
581 | return Info->getKind() == OffloadingEntryInfoDeviceGlobalVar; |
582 | } |
583 | }; |
584 | |
585 | /// Initialize device global variable entry. |
586 | void initializeDeviceGlobalVarEntryInfo(StringRef Name, |
587 | OMPTargetGlobalVarEntryKind Flags, |
588 | unsigned Order); |
589 | |
590 | /// Register device global variable entry. |
591 | void |
592 | registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr, |
593 | CharUnits VarSize, |
594 | OMPTargetGlobalVarEntryKind Flags, |
595 | llvm::GlobalValue::LinkageTypes Linkage); |
596 | /// Checks if the variable with the given name has been registered already. |
597 | bool hasDeviceGlobalVarEntryInfo(StringRef VarName) const { |
598 | return OffloadEntriesDeviceGlobalVar.count(VarName) > 0; |
599 | } |
600 | /// Applies action \a Action on all registered entries. |
601 | typedef llvm::function_ref<void(StringRef, |
602 | const OffloadEntryInfoDeviceGlobalVar &)> |
603 | OffloadDeviceGlobalVarEntryInfoActTy; |
604 | void actOnDeviceGlobalVarEntriesInfo( |
605 | const OffloadDeviceGlobalVarEntryInfoActTy &Action); |
606 | |
607 | private: |
608 | // Storage for target region entries kind. The storage is to be indexed by |
609 | // file ID, device ID, parent function name and line number. |
610 | typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion> |
611 | OffloadEntriesTargetRegionPerLine; |
612 | typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine> |
613 | OffloadEntriesTargetRegionPerParentName; |
614 | typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName> |
615 | OffloadEntriesTargetRegionPerFile; |
616 | typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile> |
617 | OffloadEntriesTargetRegionPerDevice; |
618 | typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy; |
619 | OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion; |
620 | /// Storage for device global variable entries kind. The storage is to be |
621 | /// indexed by mangled name. |
622 | typedef llvm::StringMap<OffloadEntryInfoDeviceGlobalVar> |
623 | OffloadEntriesDeviceGlobalVarTy; |
624 | OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar; |
625 | }; |
626 | OffloadEntriesInfoManagerTy OffloadEntriesInfoManager; |
627 | |
628 | bool ShouldMarkAsGlobal = true; |
629 | /// List of the emitted functions. |
630 | llvm::StringSet<> AlreadyEmittedTargetFunctions; |
631 | /// List of the global variables with their addresses that should not be |
632 | /// emitted for the target. |
633 | llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables; |
634 | |
635 | /// List of variables that can become declare target implicitly and, thus, |
636 | /// must be emitted. |
637 | llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables; |
638 | |
639 | /// Creates and registers offloading binary descriptor for the current |
640 | /// compilation unit. The function that does the registration is returned. |
641 | llvm::Function *createOffloadingBinaryDescriptorRegistration(); |
642 | |
643 | /// Creates all the offload entries in the current compilation unit |
644 | /// along with the associated metadata. |
645 | void createOffloadEntriesAndInfoMetadata(); |
646 | |
647 | /// Loads all the offload entries information from the host IR |
648 | /// metadata. |
649 | void loadOffloadInfoMetadata(); |
650 | |
651 | /// Returns __tgt_offload_entry type. |
652 | QualType getTgtOffloadEntryQTy(); |
653 | |
654 | /// Returns __tgt_device_image type. |
655 | QualType getTgtDeviceImageQTy(); |
656 | |
657 | /// Returns __tgt_bin_desc type. |
658 | QualType getTgtBinaryDescriptorQTy(); |
659 | |
660 | /// Start scanning from statement \a S and and emit all target regions |
661 | /// found along the way. |
662 | /// \param S Starting statement. |
663 | /// \param ParentName Name of the function declaration that is being scanned. |
664 | void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName); |
665 | |
666 | /// Build type kmp_routine_entry_t (if not built yet). |
667 | void emitKmpRoutineEntryT(QualType KmpInt32Ty); |
668 | |
669 | /// Returns pointer to kmpc_micro type. |
670 | llvm::Type *getKmpc_MicroPointerTy(); |
671 | |
672 | /// Returns specified OpenMP runtime function. |
673 | /// \param Function OpenMP runtime function. |
674 | /// \return Specified function. |
675 | llvm::FunctionCallee createRuntimeFunction(unsigned Function); |
676 | |
677 | /// Returns __kmpc_for_static_init_* runtime function for the specified |
678 | /// size \a IVSize and sign \a IVSigned. |
679 | llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize, |
680 | bool IVSigned); |
681 | |
682 | /// Returns __kmpc_dispatch_init_* runtime function for the specified |
683 | /// size \a IVSize and sign \a IVSigned. |
684 | llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize, |
685 | bool IVSigned); |
686 | |
687 | /// Returns __kmpc_dispatch_next_* runtime function for the specified |
688 | /// size \a IVSize and sign \a IVSigned. |
689 | llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize, |
690 | bool IVSigned); |
691 | |
692 | /// Returns __kmpc_dispatch_fini_* runtime function for the specified |
693 | /// size \a IVSize and sign \a IVSigned. |
694 | llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize, |
695 | bool IVSigned); |
696 | |
697 | /// If the specified mangled name is not in the module, create and |
698 | /// return threadprivate cache object. This object is a pointer's worth of |
699 | /// storage that's reserved for use by the OpenMP runtime. |
700 | /// \param VD Threadprivate variable. |
701 | /// \return Cache variable for the specified threadprivate. |
702 | llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD); |
703 | |
704 | /// Gets (if variable with the given name already exist) or creates |
705 | /// internal global variable with the specified Name. The created variable has |
706 | /// linkage CommonLinkage by default and is initialized by null value. |
707 | /// \param Ty Type of the global variable. If it is exist already the type |
708 | /// must be the same. |
709 | /// \param Name Name of the variable. |
710 | llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty, |
711 | const llvm::Twine &Name, |
712 | unsigned AddressSpace = 0); |
713 | |
714 | /// Set of threadprivate variables with the generated initializer. |
715 | llvm::StringSet<> ThreadPrivateWithDefinition; |
716 | |
717 | /// Set of declare target variables with the generated initializer. |
718 | llvm::StringSet<> DeclareTargetWithDefinition; |
719 | |
720 | /// Emits initialization code for the threadprivate variables. |
721 | /// \param VDAddr Address of the global variable \a VD. |
722 | /// \param Ctor Pointer to a global init function for \a VD. |
723 | /// \param CopyCtor Pointer to a global copy function for \a VD. |
724 | /// \param Dtor Pointer to a global destructor function for \a VD. |
725 | /// \param Loc Location of threadprivate declaration. |
726 | void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr, |
727 | llvm::Value *Ctor, llvm::Value *CopyCtor, |
728 | llvm::Value *Dtor, SourceLocation Loc); |
729 | |
730 | struct TaskResultTy { |
731 | llvm::Value *NewTask = nullptr; |
732 | llvm::Function *TaskEntry = nullptr; |
733 | llvm::Value *NewTaskNewTaskTTy = nullptr; |
734 | LValue TDBase; |
735 | const RecordDecl *KmpTaskTQTyRD = nullptr; |
736 | llvm::Value *TaskDupFn = nullptr; |
737 | }; |
738 | /// Emit task region for the task directive. The task region is emitted in |
739 | /// several steps: |
740 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
741 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
742 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
743 | /// function: |
744 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
745 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
746 | /// return 0; |
747 | /// } |
748 | /// 2. Copy a list of shared variables to field shareds of the resulting |
749 | /// structure kmp_task_t returned by the previous call (if any). |
750 | /// 3. Copy a pointer to destructions function to field destructions of the |
751 | /// resulting structure kmp_task_t. |
752 | /// \param D Current task directive. |
753 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
754 | /// /*part_id*/, captured_struct */*__context*/); |
755 | /// \param SharedsTy A type which contains references the shared variables. |
756 | /// \param Shareds Context with the list of shared variables from the \p |
757 | /// TaskFunction. |
758 | /// \param Data Additional data for task generation like tiednsee, final |
759 | /// state, list of privates etc. |
760 | TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, |
761 | const OMPExecutableDirective &D, |
762 | llvm::Function *TaskFunction, QualType , |
763 | Address , const OMPTaskDataTy &Data); |
764 | |
765 | /// Returns default address space for the constant firstprivates, 0 by |
766 | /// default. |
767 | virtual unsigned getDefaultFirstprivateAddressSpace() const { return 0; } |
768 | |
769 | public: |
770 | explicit CGOpenMPRuntime(CodeGenModule &CGM) |
771 | : CGOpenMPRuntime(CGM, ".", ".") {} |
772 | virtual ~CGOpenMPRuntime() {} |
773 | virtual void clear(); |
774 | |
775 | /// Get the platform-specific name separator. |
776 | std::string getName(ArrayRef<StringRef> Parts) const; |
777 | |
778 | /// Emit code for the specified user defined reduction construct. |
779 | virtual void emitUserDefinedReduction(CodeGenFunction *CGF, |
780 | const OMPDeclareReductionDecl *D); |
781 | /// Get combiner/initializer for the specified user-defined reduction, if any. |
782 | virtual std::pair<llvm::Function *, llvm::Function *> |
783 | getUserDefinedReduction(const OMPDeclareReductionDecl *D); |
784 | |
785 | /// Emits outlined function for the specified OpenMP parallel directive |
786 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
787 | /// kmp_int32 BoundID, struct context_vars*). |
788 | /// \param D OpenMP directive. |
789 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
790 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
791 | /// is a directive itself, for combined - its innermost directive). |
792 | /// \param CodeGen Code generation sequence for the \a D directive. |
793 | virtual llvm::Function *emitParallelOutlinedFunction( |
794 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
795 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen); |
796 | |
797 | /// Emits outlined function for the specified OpenMP teams directive |
798 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
799 | /// kmp_int32 BoundID, struct context_vars*). |
800 | /// \param D OpenMP directive. |
801 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
802 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
803 | /// is a directive itself, for combined - its innermost directive). |
804 | /// \param CodeGen Code generation sequence for the \a D directive. |
805 | virtual llvm::Function *emitTeamsOutlinedFunction( |
806 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
807 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen); |
808 | |
809 | /// Emits outlined function for the OpenMP task directive \a D. This |
810 | /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t* |
811 | /// TaskT). |
812 | /// \param D OpenMP directive. |
813 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
814 | /// \param PartIDVar Variable for partition id in the current OpenMP untied |
815 | /// task region. |
816 | /// \param TaskTVar Variable for task_t argument. |
817 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
818 | /// is a directive itself, for combined - its innermost directive). |
819 | /// \param CodeGen Code generation sequence for the \a D directive. |
820 | /// \param Tied true if task is generated for tied task, false otherwise. |
821 | /// \param NumberOfParts Number of parts in untied task. Ignored for tied |
822 | /// tasks. |
823 | /// |
824 | virtual llvm::Function *emitTaskOutlinedFunction( |
825 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
826 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
827 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
828 | bool Tied, unsigned &NumberOfParts); |
829 | |
830 | /// Cleans up references to the objects in finished function. |
831 | /// |
832 | virtual void functionFinished(CodeGenFunction &CGF); |
833 | |
834 | /// Emits code for parallel or serial call of the \a OutlinedFn with |
835 | /// variables captured in a record which address is stored in \a |
836 | /// CapturedStruct. |
837 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
838 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
839 | /// \param CapturedVars A pointer to the record with the references to |
840 | /// variables used in \a OutlinedFn function. |
841 | /// \param IfCond Condition in the associated 'if' clause, if it was |
842 | /// specified, nullptr otherwise. |
843 | /// |
844 | virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
845 | llvm::Function *OutlinedFn, |
846 | ArrayRef<llvm::Value *> CapturedVars, |
847 | const Expr *IfCond); |
848 | |
849 | /// Emits a critical region. |
850 | /// \param CriticalName Name of the critical region. |
851 | /// \param CriticalOpGen Generator for the statement associated with the given |
852 | /// critical region. |
853 | /// \param Hint Value of the 'hint' clause (optional). |
854 | virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName, |
855 | const RegionCodeGenTy &CriticalOpGen, |
856 | SourceLocation Loc, |
857 | const Expr *Hint = nullptr); |
858 | |
859 | /// Emits a master region. |
860 | /// \param MasterOpGen Generator for the statement associated with the given |
861 | /// master region. |
862 | virtual void emitMasterRegion(CodeGenFunction &CGF, |
863 | const RegionCodeGenTy &MasterOpGen, |
864 | SourceLocation Loc); |
865 | |
866 | /// Emits code for a taskyield directive. |
867 | virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc); |
868 | |
869 | /// Emit a taskgroup region. |
870 | /// \param TaskgroupOpGen Generator for the statement associated with the |
871 | /// given taskgroup region. |
872 | virtual void emitTaskgroupRegion(CodeGenFunction &CGF, |
873 | const RegionCodeGenTy &TaskgroupOpGen, |
874 | SourceLocation Loc); |
875 | |
876 | /// Emits a single region. |
877 | /// \param SingleOpGen Generator for the statement associated with the given |
878 | /// single region. |
879 | virtual void emitSingleRegion(CodeGenFunction &CGF, |
880 | const RegionCodeGenTy &SingleOpGen, |
881 | SourceLocation Loc, |
882 | ArrayRef<const Expr *> CopyprivateVars, |
883 | ArrayRef<const Expr *> DestExprs, |
884 | ArrayRef<const Expr *> SrcExprs, |
885 | ArrayRef<const Expr *> AssignmentOps); |
886 | |
887 | /// Emit an ordered region. |
888 | /// \param OrderedOpGen Generator for the statement associated with the given |
889 | /// ordered region. |
890 | virtual void emitOrderedRegion(CodeGenFunction &CGF, |
891 | const RegionCodeGenTy &OrderedOpGen, |
892 | SourceLocation Loc, bool IsThreads); |
893 | |
894 | /// Emit an implicit/explicit barrier for OpenMP threads. |
895 | /// \param Kind Directive for which this implicit barrier call must be |
896 | /// generated. Must be OMPD_barrier for explicit barrier generation. |
897 | /// \param EmitChecks true if need to emit checks for cancellation barriers. |
898 | /// \param ForceSimpleCall true simple barrier call must be emitted, false if |
899 | /// runtime class decides which one to emit (simple or with cancellation |
900 | /// checks). |
901 | /// |
902 | virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
903 | OpenMPDirectiveKind Kind, |
904 | bool EmitChecks = true, |
905 | bool ForceSimpleCall = false); |
906 | |
907 | /// Check if the specified \a ScheduleKind is static non-chunked. |
908 | /// This kind of worksharing directive is emitted without outer loop. |
909 | /// \param ScheduleKind Schedule kind specified in the 'schedule' clause. |
910 | /// \param Chunked True if chunk is specified in the clause. |
911 | /// |
912 | virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind, |
913 | bool Chunked) const; |
914 | |
915 | /// Check if the specified \a ScheduleKind is static non-chunked. |
916 | /// This kind of distribute directive is emitted without outer loop. |
917 | /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause. |
918 | /// \param Chunked True if chunk is specified in the clause. |
919 | /// |
920 | virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind, |
921 | bool Chunked) const; |
922 | |
923 | /// Check if the specified \a ScheduleKind is static chunked. |
924 | /// \param ScheduleKind Schedule kind specified in the 'schedule' clause. |
925 | /// \param Chunked True if chunk is specified in the clause. |
926 | /// |
927 | virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind, |
928 | bool Chunked) const; |
929 | |
930 | /// Check if the specified \a ScheduleKind is static non-chunked. |
931 | /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause. |
932 | /// \param Chunked True if chunk is specified in the clause. |
933 | /// |
934 | virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind, |
935 | bool Chunked) const; |
936 | |
937 | /// Check if the specified \a ScheduleKind is dynamic. |
938 | /// This kind of worksharing directive is emitted without outer loop. |
939 | /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause. |
940 | /// |
941 | virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const; |
942 | |
943 | /// struct with the values to be passed to the dispatch runtime function |
944 | struct DispatchRTInput { |
945 | /// Loop lower bound |
946 | llvm::Value *LB = nullptr; |
947 | /// Loop upper bound |
948 | llvm::Value *UB = nullptr; |
949 | /// Chunk size specified using 'schedule' clause (nullptr if chunk |
950 | /// was not specified) |
951 | llvm::Value *Chunk = nullptr; |
952 | DispatchRTInput() = default; |
953 | DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk) |
954 | : LB(LB), UB(UB), Chunk(Chunk) {} |
955 | }; |
956 | |
957 | /// Call the appropriate runtime routine to initialize it before start |
958 | /// of loop. |
959 | |
960 | /// This is used for non static scheduled types and when the ordered |
961 | /// clause is present on the loop construct. |
962 | /// Depending on the loop schedule, it is necessary to call some runtime |
963 | /// routine before start of the OpenMP loop to get the loop upper / lower |
964 | /// bounds \a LB and \a UB and stride \a ST. |
965 | /// |
966 | /// \param CGF Reference to current CodeGenFunction. |
967 | /// \param Loc Clang source location. |
968 | /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. |
969 | /// \param IVSize Size of the iteration variable in bits. |
970 | /// \param IVSigned Sign of the iteration variable. |
971 | /// \param Ordered true if loop is ordered, false otherwise. |
972 | /// \param DispatchValues struct containing llvm values for lower bound, upper |
973 | /// bound, and chunk expression. |
974 | /// For the default (nullptr) value, the chunk 1 will be used. |
975 | /// |
976 | virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc, |
977 | const OpenMPScheduleTy &ScheduleKind, |
978 | unsigned IVSize, bool IVSigned, bool Ordered, |
979 | const DispatchRTInput &DispatchValues); |
980 | |
981 | /// Struct with the values to be passed to the static runtime function |
982 | struct StaticRTInput { |
983 | /// Size of the iteration variable in bits. |
984 | unsigned IVSize = 0; |
985 | /// Sign of the iteration variable. |
986 | bool IVSigned = false; |
987 | /// true if loop is ordered, false otherwise. |
988 | bool Ordered = false; |
989 | /// Address of the output variable in which the flag of the last iteration |
990 | /// is returned. |
991 | Address IL = Address::invalid(); |
992 | /// Address of the output variable in which the lower iteration number is |
993 | /// returned. |
994 | Address LB = Address::invalid(); |
995 | /// Address of the output variable in which the upper iteration number is |
996 | /// returned. |
997 | Address UB = Address::invalid(); |
998 | /// Address of the output variable in which the stride value is returned |
999 | /// necessary to generated the static_chunked scheduled loop. |
1000 | Address ST = Address::invalid(); |
1001 | /// Value of the chunk for the static_chunked scheduled loop. For the |
1002 | /// default (nullptr) value, the chunk 1 will be used. |
1003 | llvm::Value *Chunk = nullptr; |
1004 | StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL, |
1005 | Address LB, Address UB, Address ST, |
1006 | llvm::Value *Chunk = nullptr) |
1007 | : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB), |
1008 | UB(UB), ST(ST), Chunk(Chunk) {} |
1009 | }; |
1010 | /// Call the appropriate runtime routine to initialize it before start |
1011 | /// of loop. |
1012 | /// |
1013 | /// This is used only in case of static schedule, when the user did not |
1014 | /// specify a ordered clause on the loop construct. |
1015 | /// Depending on the loop schedule, it is necessary to call some runtime |
1016 | /// routine before start of the OpenMP loop to get the loop upper / lower |
1017 | /// bounds LB and UB and stride ST. |
1018 | /// |
1019 | /// \param CGF Reference to current CodeGenFunction. |
1020 | /// \param Loc Clang source location. |
1021 | /// \param DKind Kind of the directive. |
1022 | /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. |
1023 | /// \param Values Input arguments for the construct. |
1024 | /// |
1025 | virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc, |
1026 | OpenMPDirectiveKind DKind, |
1027 | const OpenMPScheduleTy &ScheduleKind, |
1028 | const StaticRTInput &Values); |
1029 | |
1030 | /// |
1031 | /// \param CGF Reference to current CodeGenFunction. |
1032 | /// \param Loc Clang source location. |
1033 | /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause. |
1034 | /// \param Values Input arguments for the construct. |
1035 | /// |
1036 | virtual void emitDistributeStaticInit(CodeGenFunction &CGF, |
1037 | SourceLocation Loc, |
1038 | OpenMPDistScheduleClauseKind SchedKind, |
1039 | const StaticRTInput &Values); |
1040 | |
1041 | /// Call the appropriate runtime routine to notify that we finished |
1042 | /// iteration of the ordered loop with the dynamic scheduling. |
1043 | /// |
1044 | /// \param CGF Reference to current CodeGenFunction. |
1045 | /// \param Loc Clang source location. |
1046 | /// \param IVSize Size of the iteration variable in bits. |
1047 | /// \param IVSigned Sign of the iteration variable. |
1048 | /// |
1049 | virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF, |
1050 | SourceLocation Loc, unsigned IVSize, |
1051 | bool IVSigned); |
1052 | |
1053 | /// Call the appropriate runtime routine to notify that we finished |
1054 | /// all the work with current loop. |
1055 | /// |
1056 | /// \param CGF Reference to current CodeGenFunction. |
1057 | /// \param Loc Clang source location. |
1058 | /// \param DKind Kind of the directive for which the static finish is emitted. |
1059 | /// |
1060 | virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc, |
1061 | OpenMPDirectiveKind DKind); |
1062 | |
1063 | /// Call __kmpc_dispatch_next( |
1064 | /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
1065 | /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
1066 | /// kmp_int[32|64] *p_stride); |
1067 | /// \param IVSize Size of the iteration variable in bits. |
1068 | /// \param IVSigned Sign of the iteration variable. |
1069 | /// \param IL Address of the output variable in which the flag of the |
1070 | /// last iteration is returned. |
1071 | /// \param LB Address of the output variable in which the lower iteration |
1072 | /// number is returned. |
1073 | /// \param UB Address of the output variable in which the upper iteration |
1074 | /// number is returned. |
1075 | /// \param ST Address of the output variable in which the stride value is |
1076 | /// returned. |
1077 | virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc, |
1078 | unsigned IVSize, bool IVSigned, |
1079 | Address IL, Address LB, |
1080 | Address UB, Address ST); |
1081 | |
1082 | /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 |
1083 | /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' |
1084 | /// clause. |
1085 | /// \param NumThreads An integer value of threads. |
1086 | virtual void emitNumThreadsClause(CodeGenFunction &CGF, |
1087 | llvm::Value *NumThreads, |
1088 | SourceLocation Loc); |
1089 | |
1090 | /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 |
1091 | /// global_tid, int proc_bind) to generate code for 'proc_bind' clause. |
1092 | virtual void emitProcBindClause(CodeGenFunction &CGF, |
1093 | OpenMPProcBindClauseKind ProcBind, |
1094 | SourceLocation Loc); |
1095 | |
1096 | /// Returns address of the threadprivate variable for the current |
1097 | /// thread. |
1098 | /// \param VD Threadprivate variable. |
1099 | /// \param VDAddr Address of the global variable \a VD. |
1100 | /// \param Loc Location of the reference to threadprivate var. |
1101 | /// \return Address of the threadprivate variable for the current thread. |
1102 | virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF, |
1103 | const VarDecl *VD, |
1104 | Address VDAddr, |
1105 | SourceLocation Loc); |
1106 | |
1107 | /// Returns the address of the variable marked as declare target with link |
1108 | /// clause. |
1109 | virtual Address getAddrOfDeclareTargetLink(const VarDecl *VD); |
1110 | |
1111 | /// Emit a code for initialization of threadprivate variable. It emits |
1112 | /// a call to runtime library which adds initial value to the newly created |
1113 | /// threadprivate variable (if it is not constant) and registers destructor |
1114 | /// for the variable (if any). |
1115 | /// \param VD Threadprivate variable. |
1116 | /// \param VDAddr Address of the global variable \a VD. |
1117 | /// \param Loc Location of threadprivate declaration. |
1118 | /// \param PerformInit true if initialization expression is not constant. |
1119 | virtual llvm::Function * |
1120 | emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr, |
1121 | SourceLocation Loc, bool PerformInit, |
1122 | CodeGenFunction *CGF = nullptr); |
1123 | |
1124 | /// Emit a code for initialization of declare target variable. |
1125 | /// \param VD Declare target variable. |
1126 | /// \param Addr Address of the global variable \a VD. |
1127 | /// \param PerformInit true if initialization expression is not constant. |
1128 | virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD, |
1129 | llvm::GlobalVariable *Addr, |
1130 | bool PerformInit); |
1131 | |
1132 | /// Creates artificial threadprivate variable with name \p Name and type \p |
1133 | /// VarType. |
1134 | /// \param VarType Type of the artificial threadprivate variable. |
1135 | /// \param Name Name of the artificial threadprivate variable. |
1136 | virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
1137 | QualType VarType, |
1138 | StringRef Name); |
1139 | |
1140 | /// Emit flush of the variables specified in 'omp flush' directive. |
1141 | /// \param Vars List of variables to flush. |
1142 | virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars, |
1143 | SourceLocation Loc); |
1144 | |
1145 | /// Emit task region for the task directive. The task region is |
1146 | /// emitted in several steps: |
1147 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
1148 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
1149 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
1150 | /// function: |
1151 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
1152 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
1153 | /// return 0; |
1154 | /// } |
1155 | /// 2. Copy a list of shared variables to field shareds of the resulting |
1156 | /// structure kmp_task_t returned by the previous call (if any). |
1157 | /// 3. Copy a pointer to destructions function to field destructions of the |
1158 | /// resulting structure kmp_task_t. |
1159 | /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, |
1160 | /// kmp_task_t *new_task), where new_task is a resulting structure from |
1161 | /// previous items. |
1162 | /// \param D Current task directive. |
1163 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
1164 | /// /*part_id*/, captured_struct */*__context*/); |
1165 | /// \param SharedsTy A type which contains references the shared variables. |
1166 | /// \param Shareds Context with the list of shared variables from the \p |
1167 | /// TaskFunction. |
1168 | /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr |
1169 | /// otherwise. |
1170 | /// \param Data Additional data for task generation like tiednsee, final |
1171 | /// state, list of privates etc. |
1172 | virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
1173 | const OMPExecutableDirective &D, |
1174 | llvm::Function *TaskFunction, QualType , |
1175 | Address , const Expr *IfCond, |
1176 | const OMPTaskDataTy &Data); |
1177 | |
1178 | /// Emit task region for the taskloop directive. The taskloop region is |
1179 | /// emitted in several steps: |
1180 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
1181 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
1182 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
1183 | /// function: |
1184 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
1185 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
1186 | /// return 0; |
1187 | /// } |
1188 | /// 2. Copy a list of shared variables to field shareds of the resulting |
1189 | /// structure kmp_task_t returned by the previous call (if any). |
1190 | /// 3. Copy a pointer to destructions function to field destructions of the |
1191 | /// resulting structure kmp_task_t. |
1192 | /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t |
1193 | /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int |
1194 | /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task |
1195 | /// is a resulting structure from |
1196 | /// previous items. |
1197 | /// \param D Current task directive. |
1198 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
1199 | /// /*part_id*/, captured_struct */*__context*/); |
1200 | /// \param SharedsTy A type which contains references the shared variables. |
1201 | /// \param Shareds Context with the list of shared variables from the \p |
1202 | /// TaskFunction. |
1203 | /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr |
1204 | /// otherwise. |
1205 | /// \param Data Additional data for task generation like tiednsee, final |
1206 | /// state, list of privates etc. |
1207 | virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
1208 | const OMPLoopDirective &D, |
1209 | llvm::Function *TaskFunction, |
1210 | QualType , Address , |
1211 | const Expr *IfCond, const OMPTaskDataTy &Data); |
1212 | |
1213 | /// Emit code for the directive that does not require outlining. |
1214 | /// |
1215 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
1216 | /// is a directive itself, for combined - its innermost directive). |
1217 | /// \param CodeGen Code generation sequence for the \a D directive. |
1218 | /// \param HasCancel true if region has inner cancel directive, false |
1219 | /// otherwise. |
1220 | virtual void emitInlinedDirective(CodeGenFunction &CGF, |
1221 | OpenMPDirectiveKind InnermostKind, |
1222 | const RegionCodeGenTy &CodeGen, |
1223 | bool HasCancel = false); |
1224 | |
1225 | /// Emits reduction function. |
1226 | /// \param ArgsType Array type containing pointers to reduction variables. |
1227 | /// \param Privates List of private copies for original reduction arguments. |
1228 | /// \param LHSExprs List of LHS in \a ReductionOps reduction operations. |
1229 | /// \param RHSExprs List of RHS in \a ReductionOps reduction operations. |
1230 | /// \param ReductionOps List of reduction operations in form 'LHS binop RHS' |
1231 | /// or 'operator binop(LHS, RHS)'. |
1232 | llvm::Function *emitReductionFunction(SourceLocation Loc, |
1233 | llvm::Type *ArgsType, |
1234 | ArrayRef<const Expr *> Privates, |
1235 | ArrayRef<const Expr *> LHSExprs, |
1236 | ArrayRef<const Expr *> RHSExprs, |
1237 | ArrayRef<const Expr *> ReductionOps); |
1238 | |
1239 | /// Emits single reduction combiner |
1240 | void emitSingleReductionCombiner(CodeGenFunction &CGF, |
1241 | const Expr *ReductionOp, |
1242 | const Expr *PrivateRef, |
1243 | const DeclRefExpr *LHS, |
1244 | const DeclRefExpr *RHS); |
1245 | |
1246 | struct ReductionOptionsTy { |
1247 | bool WithNowait; |
1248 | bool SimpleReduction; |
1249 | OpenMPDirectiveKind ReductionKind; |
1250 | }; |
1251 | /// Emit a code for reduction clause. Next code should be emitted for |
1252 | /// reduction: |
1253 | /// \code |
1254 | /// |
1255 | /// static kmp_critical_name lock = { 0 }; |
1256 | /// |
1257 | /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
1258 | /// ... |
1259 | /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
1260 | /// ... |
1261 | /// } |
1262 | /// |
1263 | /// ... |
1264 | /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
1265 | /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
1266 | /// RedList, reduce_func, &<lock>)) { |
1267 | /// case 1: |
1268 | /// ... |
1269 | /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
1270 | /// ... |
1271 | /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
1272 | /// break; |
1273 | /// case 2: |
1274 | /// ... |
1275 | /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
1276 | /// ... |
1277 | /// break; |
1278 | /// default:; |
1279 | /// } |
1280 | /// \endcode |
1281 | /// |
1282 | /// \param Privates List of private copies for original reduction arguments. |
1283 | /// \param LHSExprs List of LHS in \a ReductionOps reduction operations. |
1284 | /// \param RHSExprs List of RHS in \a ReductionOps reduction operations. |
1285 | /// \param ReductionOps List of reduction operations in form 'LHS binop RHS' |
1286 | /// or 'operator binop(LHS, RHS)'. |
1287 | /// \param Options List of options for reduction codegen: |
1288 | /// WithNowait true if parent directive has also nowait clause, false |
1289 | /// otherwise. |
1290 | /// SimpleReduction Emit reduction operation only. Used for omp simd |
1291 | /// directive on the host. |
1292 | /// ReductionKind The kind of reduction to perform. |
1293 | virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
1294 | ArrayRef<const Expr *> Privates, |
1295 | ArrayRef<const Expr *> LHSExprs, |
1296 | ArrayRef<const Expr *> RHSExprs, |
1297 | ArrayRef<const Expr *> ReductionOps, |
1298 | ReductionOptionsTy Options); |
1299 | |
1300 | /// Emit a code for initialization of task reduction clause. Next code |
1301 | /// should be emitted for reduction: |
1302 | /// \code |
1303 | /// |
1304 | /// _task_red_item_t red_data[n]; |
1305 | /// ... |
1306 | /// red_data[i].shar = &origs[i]; |
1307 | /// red_data[i].size = sizeof(origs[i]); |
1308 | /// red_data[i].f_init = (void*)RedInit<i>; |
1309 | /// red_data[i].f_fini = (void*)RedDest<i>; |
1310 | /// red_data[i].f_comb = (void*)RedOp<i>; |
1311 | /// red_data[i].flags = <Flag_i>; |
1312 | /// ... |
1313 | /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data); |
1314 | /// \endcode |
1315 | /// |
1316 | /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations. |
1317 | /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations. |
1318 | /// \param Data Additional data for task generation like tiedness, final |
1319 | /// state, list of privates, reductions etc. |
1320 | virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, |
1321 | SourceLocation Loc, |
1322 | ArrayRef<const Expr *> LHSExprs, |
1323 | ArrayRef<const Expr *> RHSExprs, |
1324 | const OMPTaskDataTy &Data); |
1325 | |
1326 | /// Required to resolve existing problems in the runtime. Emits threadprivate |
1327 | /// variables to store the size of the VLAs/array sections for |
1328 | /// initializer/combiner/finalizer functions + emits threadprivate variable to |
1329 | /// store the pointer to the original reduction item for the custom |
1330 | /// initializer defined by declare reduction construct. |
1331 | /// \param RCG Allows to reuse an existing data for the reductions. |
1332 | /// \param N Reduction item for which fixups must be emitted. |
1333 | virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc, |
1334 | ReductionCodeGen &RCG, unsigned N); |
1335 | |
1336 | /// Get the address of `void *` type of the privatue copy of the reduction |
1337 | /// item specified by the \p SharedLVal. |
1338 | /// \param ReductionsPtr Pointer to the reduction data returned by the |
1339 | /// emitTaskReductionInit function. |
1340 | /// \param SharedLVal Address of the original reduction item. |
1341 | virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc, |
1342 | llvm::Value *ReductionsPtr, |
1343 | LValue ); |
1344 | |
1345 | /// Emit code for 'taskwait' directive. |
1346 | virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc); |
1347 | |
1348 | /// Emit code for 'cancellation point' construct. |
1349 | /// \param CancelRegion Region kind for which the cancellation point must be |
1350 | /// emitted. |
1351 | /// |
1352 | virtual void emitCancellationPointCall(CodeGenFunction &CGF, |
1353 | SourceLocation Loc, |
1354 | OpenMPDirectiveKind CancelRegion); |
1355 | |
1356 | /// Emit code for 'cancel' construct. |
1357 | /// \param IfCond Condition in the associated 'if' clause, if it was |
1358 | /// specified, nullptr otherwise. |
1359 | /// \param CancelRegion Region kind for which the cancel must be emitted. |
1360 | /// |
1361 | virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
1362 | const Expr *IfCond, |
1363 | OpenMPDirectiveKind CancelRegion); |
1364 | |
1365 | /// Emit outilined function for 'target' directive. |
1366 | /// \param D Directive to emit. |
1367 | /// \param ParentName Name of the function that encloses the target region. |
1368 | /// \param OutlinedFn Outlined function value to be defined by this call. |
1369 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
1370 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
1371 | /// \param CodeGen Code generation sequence for the \a D directive. |
1372 | /// An outlined function may not be an entry if, e.g. the if clause always |
1373 | /// evaluates to false. |
1374 | virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D, |
1375 | StringRef ParentName, |
1376 | llvm::Function *&OutlinedFn, |
1377 | llvm::Constant *&OutlinedFnID, |
1378 | bool IsOffloadEntry, |
1379 | const RegionCodeGenTy &CodeGen); |
1380 | |
1381 | /// Emit code that pushes the trip count of loops associated with constructs |
1382 | /// 'target teams distribute' and 'teams distribute parallel for'. |
1383 | /// \param SizeEmitter Emits the int64 value for the number of iterations of |
1384 | /// the associated loop. |
1385 | virtual void emitTargetNumIterationsCall( |
1386 | CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *Device, |
1387 | const llvm::function_ref<llvm::Value *( |
1388 | CodeGenFunction &CGF, const OMPLoopDirective &D)> &SizeEmitter); |
1389 | |
1390 | /// Emit the target offloading code associated with \a D. The emitted |
1391 | /// code attempts offloading the execution to the device, an the event of |
1392 | /// a failure it executes the host version outlined in \a OutlinedFn. |
1393 | /// \param D Directive to emit. |
1394 | /// \param OutlinedFn Host version of the code to be offloaded. |
1395 | /// \param OutlinedFnID ID of host version of the code to be offloaded. |
1396 | /// \param IfCond Expression evaluated in if clause associated with the target |
1397 | /// directive, or null if no if clause is used. |
1398 | /// \param Device Expression evaluated in device clause associated with the |
1399 | /// target directive, or null if no device clause is used. |
1400 | virtual void emitTargetCall(CodeGenFunction &CGF, |
1401 | const OMPExecutableDirective &D, |
1402 | llvm::Function *OutlinedFn, |
1403 | llvm::Value *OutlinedFnID, const Expr *IfCond, |
1404 | const Expr *Device); |
1405 | |
1406 | /// Emit the target regions enclosed in \a GD function definition or |
1407 | /// the function itself in case it is a valid device function. Returns true if |
1408 | /// \a GD was dealt with successfully. |
1409 | /// \param GD Function to scan. |
1410 | virtual bool emitTargetFunctions(GlobalDecl GD); |
1411 | |
1412 | /// Emit the global variable if it is a valid device global variable. |
1413 | /// Returns true if \a GD was dealt with successfully. |
1414 | /// \param GD Variable declaration to emit. |
1415 | virtual bool emitTargetGlobalVariable(GlobalDecl GD); |
1416 | |
1417 | /// Checks if the provided global decl \a GD is a declare target variable and |
1418 | /// registers it when emitting code for the host. |
1419 | virtual void registerTargetGlobalVariable(const VarDecl *VD, |
1420 | llvm::Constant *Addr); |
1421 | |
1422 | /// Registers provided target firstprivate variable as global on the |
1423 | /// target. |
1424 | llvm::Constant *registerTargetFirstprivateCopy(CodeGenFunction &CGF, |
1425 | const VarDecl *VD); |
1426 | |
1427 | /// Emit the global \a GD if it is meaningful for the target. Returns |
1428 | /// if it was emitted successfully. |
1429 | /// \param GD Global to scan. |
1430 | virtual bool emitTargetGlobal(GlobalDecl GD); |
1431 | |
1432 | /// Creates the offloading descriptor in the event any target region |
1433 | /// was emitted in the current module and return the function that registers |
1434 | /// it. |
1435 | virtual llvm::Function *emitRegistrationFunction(); |
1436 | |
1437 | /// Emits code for teams call of the \a OutlinedFn with |
1438 | /// variables captured in a record which address is stored in \a |
1439 | /// CapturedStruct. |
1440 | /// \param OutlinedFn Outlined function to be run by team masters. Type of |
1441 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
1442 | /// \param CapturedVars A pointer to the record with the references to |
1443 | /// variables used in \a OutlinedFn function. |
1444 | /// |
1445 | virtual void emitTeamsCall(CodeGenFunction &CGF, |
1446 | const OMPExecutableDirective &D, |
1447 | SourceLocation Loc, llvm::Function *OutlinedFn, |
1448 | ArrayRef<llvm::Value *> CapturedVars); |
1449 | |
1450 | /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 |
1451 | /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code |
1452 | /// for num_teams clause. |
1453 | /// \param NumTeams An integer expression of teams. |
1454 | /// \param ThreadLimit An integer expression of threads. |
1455 | virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, |
1456 | const Expr *ThreadLimit, SourceLocation Loc); |
1457 | |
1458 | /// Struct that keeps all the relevant information that should be kept |
1459 | /// throughout a 'target data' region. |
1460 | class TargetDataInfo { |
1461 | /// Set to true if device pointer information have to be obtained. |
1462 | bool RequiresDevicePointerInfo = false; |
1463 | |
1464 | public: |
1465 | /// The array of base pointer passed to the runtime library. |
1466 | llvm::Value *BasePointersArray = nullptr; |
1467 | /// The array of section pointers passed to the runtime library. |
1468 | llvm::Value *PointersArray = nullptr; |
1469 | /// The array of sizes passed to the runtime library. |
1470 | llvm::Value *SizesArray = nullptr; |
1471 | /// The array of map types passed to the runtime library. |
1472 | llvm::Value *MapTypesArray = nullptr; |
1473 | /// The total number of pointers passed to the runtime library. |
1474 | unsigned NumberOfPtrs = 0u; |
1475 | /// Map between the a declaration of a capture and the corresponding base |
1476 | /// pointer address where the runtime returns the device pointers. |
1477 | llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap; |
1478 | |
1479 | explicit TargetDataInfo() {} |
1480 | explicit TargetDataInfo(bool RequiresDevicePointerInfo) |
1481 | : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {} |
1482 | /// Clear information about the data arrays. |
1483 | void clearArrayInfo() { |
1484 | BasePointersArray = nullptr; |
1485 | PointersArray = nullptr; |
1486 | SizesArray = nullptr; |
1487 | MapTypesArray = nullptr; |
1488 | NumberOfPtrs = 0u; |
1489 | } |
1490 | /// Return true if the current target data information has valid arrays. |
1491 | bool isValid() { |
1492 | return BasePointersArray && PointersArray && SizesArray && |
1493 | MapTypesArray && NumberOfPtrs; |
1494 | } |
1495 | bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; } |
1496 | }; |
1497 | |
1498 | /// Emit the target data mapping code associated with \a D. |
1499 | /// \param D Directive to emit. |
1500 | /// \param IfCond Expression evaluated in if clause associated with the |
1501 | /// target directive, or null if no device clause is used. |
1502 | /// \param Device Expression evaluated in device clause associated with the |
1503 | /// target directive, or null if no device clause is used. |
1504 | /// \param Info A record used to store information that needs to be preserved |
1505 | /// until the region is closed. |
1506 | virtual void emitTargetDataCalls(CodeGenFunction &CGF, |
1507 | const OMPExecutableDirective &D, |
1508 | const Expr *IfCond, const Expr *Device, |
1509 | const RegionCodeGenTy &CodeGen, |
1510 | TargetDataInfo &Info); |
1511 | |
1512 | /// Emit the data mapping/movement code associated with the directive |
1513 | /// \a D that should be of the form 'target [{enter|exit} data | update]'. |
1514 | /// \param D Directive to emit. |
1515 | /// \param IfCond Expression evaluated in if clause associated with the target |
1516 | /// directive, or null if no if clause is used. |
1517 | /// \param Device Expression evaluated in device clause associated with the |
1518 | /// target directive, or null if no device clause is used. |
1519 | virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF, |
1520 | const OMPExecutableDirective &D, |
1521 | const Expr *IfCond, |
1522 | const Expr *Device); |
1523 | |
1524 | /// Marks function \a Fn with properly mangled versions of vector functions. |
1525 | /// \param FD Function marked as 'declare simd'. |
1526 | /// \param Fn LLVM function that must be marked with 'declare simd' |
1527 | /// attributes. |
1528 | virtual void emitDeclareSimdFunction(const FunctionDecl *FD, |
1529 | llvm::Function *Fn); |
1530 | |
1531 | /// Emit initialization for doacross loop nesting support. |
1532 | /// \param D Loop-based construct used in doacross nesting construct. |
1533 | virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D, |
1534 | ArrayRef<Expr *> NumIterations); |
1535 | |
1536 | /// Emit code for doacross ordered directive with 'depend' clause. |
1537 | /// \param C 'depend' clause with 'sink|source' dependency kind. |
1538 | virtual void emitDoacrossOrdered(CodeGenFunction &CGF, |
1539 | const OMPDependClause *C); |
1540 | |
1541 | /// Translates the native parameter of outlined function if this is required |
1542 | /// for target. |
1543 | /// \param FD Field decl from captured record for the parameter. |
1544 | /// \param NativeParam Parameter itself. |
1545 | virtual const VarDecl *translateParameter(const FieldDecl *FD, |
1546 | const VarDecl *NativeParam) const { |
1547 | return NativeParam; |
1548 | } |
1549 | |
1550 | /// Gets the address of the native argument basing on the address of the |
1551 | /// target-specific parameter. |
1552 | /// \param NativeParam Parameter itself. |
1553 | /// \param TargetParam Corresponding target-specific parameter. |
1554 | virtual Address getParameterAddress(CodeGenFunction &CGF, |
1555 | const VarDecl *NativeParam, |
1556 | const VarDecl *TargetParam) const; |
1557 | |
1558 | /// Choose default schedule type and chunk value for the |
1559 | /// dist_schedule clause. |
1560 | virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF, |
1561 | const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind, |
1562 | llvm::Value *&Chunk) const {} |
1563 | |
1564 | /// Choose default schedule type and chunk value for the |
1565 | /// schedule clause. |
1566 | virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF, |
1567 | const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind, |
1568 | const Expr *&ChunkExpr) const; |
1569 | |
1570 | /// Emits call of the outlined function with the provided arguments, |
1571 | /// translating these arguments to correct target-specific arguments. |
1572 | virtual void |
1573 | emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc, |
1574 | llvm::FunctionCallee OutlinedFn, |
1575 | ArrayRef<llvm::Value *> Args = llvm::None) const; |
1576 | |
1577 | /// Emits OpenMP-specific function prolog. |
1578 | /// Required for device constructs. |
1579 | virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D) {} |
1580 | |
1581 | /// Gets the OpenMP-specific address of the local variable. |
1582 | virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF, |
1583 | const VarDecl *VD); |
1584 | |
1585 | /// Marks the declaration as already emitted for the device code and returns |
1586 | /// true, if it was marked already, and false, otherwise. |
1587 | bool markAsGlobalTarget(GlobalDecl GD); |
1588 | |
1589 | /// Emit deferred declare target variables marked for deferred emission. |
1590 | void emitDeferredTargetDecls() const; |
1591 | |
1592 | /// Adjust some parameters for the target-based directives, like addresses of |
1593 | /// the variables captured by reference in lambdas. |
1594 | virtual void |
1595 | adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF, |
1596 | const OMPExecutableDirective &D) const; |
1597 | |
1598 | /// Perform check on requires decl to ensure that target architecture |
1599 | /// supports unified addressing |
1600 | virtual void checkArchForUnifiedAddressing(const OMPRequiresDecl *D) const {} |
1601 | |
1602 | /// Checks if the variable has associated OMPAllocateDeclAttr attribute with |
1603 | /// the predefined allocator and translates it into the corresponding address |
1604 | /// space. |
1605 | virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS); |
1606 | }; |
1607 | |
1608 | /// Class supports emissionof SIMD-only code. |
1609 | class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime { |
1610 | public: |
1611 | explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {} |
1612 | ~CGOpenMPSIMDRuntime() override {} |
1613 | |
1614 | /// Emits outlined function for the specified OpenMP parallel directive |
1615 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
1616 | /// kmp_int32 BoundID, struct context_vars*). |
1617 | /// \param D OpenMP directive. |
1618 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
1619 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
1620 | /// is a directive itself, for combined - its innermost directive). |
1621 | /// \param CodeGen Code generation sequence for the \a D directive. |
1622 | llvm::Function * |
1623 | emitParallelOutlinedFunction(const OMPExecutableDirective &D, |
1624 | const VarDecl *ThreadIDVar, |
1625 | OpenMPDirectiveKind InnermostKind, |
1626 | const RegionCodeGenTy &CodeGen) override; |
1627 | |
1628 | /// Emits outlined function for the specified OpenMP teams directive |
1629 | /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID, |
1630 | /// kmp_int32 BoundID, struct context_vars*). |
1631 | /// \param D OpenMP directive. |
1632 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
1633 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
1634 | /// is a directive itself, for combined - its innermost directive). |
1635 | /// \param CodeGen Code generation sequence for the \a D directive. |
1636 | llvm::Function * |
1637 | emitTeamsOutlinedFunction(const OMPExecutableDirective &D, |
1638 | const VarDecl *ThreadIDVar, |
1639 | OpenMPDirectiveKind InnermostKind, |
1640 | const RegionCodeGenTy &CodeGen) override; |
1641 | |
1642 | /// Emits outlined function for the OpenMP task directive \a D. This |
1643 | /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t* |
1644 | /// TaskT). |
1645 | /// \param D OpenMP directive. |
1646 | /// \param ThreadIDVar Variable for thread id in the current OpenMP region. |
1647 | /// \param PartIDVar Variable for partition id in the current OpenMP untied |
1648 | /// task region. |
1649 | /// \param TaskTVar Variable for task_t argument. |
1650 | /// \param InnermostKind Kind of innermost directive (for simple directives it |
1651 | /// is a directive itself, for combined - its innermost directive). |
1652 | /// \param CodeGen Code generation sequence for the \a D directive. |
1653 | /// \param Tied true if task is generated for tied task, false otherwise. |
1654 | /// \param NumberOfParts Number of parts in untied task. Ignored for tied |
1655 | /// tasks. |
1656 | /// |
1657 | llvm::Function *emitTaskOutlinedFunction( |
1658 | const OMPExecutableDirective &D, const VarDecl *ThreadIDVar, |
1659 | const VarDecl *PartIDVar, const VarDecl *TaskTVar, |
1660 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
1661 | bool Tied, unsigned &NumberOfParts) override; |
1662 | |
1663 | /// Emits code for parallel or serial call of the \a OutlinedFn with |
1664 | /// variables captured in a record which address is stored in \a |
1665 | /// CapturedStruct. |
1666 | /// \param OutlinedFn Outlined function to be run in parallel threads. Type of |
1667 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
1668 | /// \param CapturedVars A pointer to the record with the references to |
1669 | /// variables used in \a OutlinedFn function. |
1670 | /// \param IfCond Condition in the associated 'if' clause, if it was |
1671 | /// specified, nullptr otherwise. |
1672 | /// |
1673 | void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc, |
1674 | llvm::Function *OutlinedFn, |
1675 | ArrayRef<llvm::Value *> CapturedVars, |
1676 | const Expr *IfCond) override; |
1677 | |
1678 | /// Emits a critical region. |
1679 | /// \param CriticalName Name of the critical region. |
1680 | /// \param CriticalOpGen Generator for the statement associated with the given |
1681 | /// critical region. |
1682 | /// \param Hint Value of the 'hint' clause (optional). |
1683 | void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName, |
1684 | const RegionCodeGenTy &CriticalOpGen, |
1685 | SourceLocation Loc, |
1686 | const Expr *Hint = nullptr) override; |
1687 | |
1688 | /// Emits a master region. |
1689 | /// \param MasterOpGen Generator for the statement associated with the given |
1690 | /// master region. |
1691 | void emitMasterRegion(CodeGenFunction &CGF, |
1692 | const RegionCodeGenTy &MasterOpGen, |
1693 | SourceLocation Loc) override; |
1694 | |
1695 | /// Emits code for a taskyield directive. |
1696 | void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override; |
1697 | |
1698 | /// Emit a taskgroup region. |
1699 | /// \param TaskgroupOpGen Generator for the statement associated with the |
1700 | /// given taskgroup region. |
1701 | void emitTaskgroupRegion(CodeGenFunction &CGF, |
1702 | const RegionCodeGenTy &TaskgroupOpGen, |
1703 | SourceLocation Loc) override; |
1704 | |
1705 | /// Emits a single region. |
1706 | /// \param SingleOpGen Generator for the statement associated with the given |
1707 | /// single region. |
1708 | void emitSingleRegion(CodeGenFunction &CGF, |
1709 | const RegionCodeGenTy &SingleOpGen, SourceLocation Loc, |
1710 | ArrayRef<const Expr *> CopyprivateVars, |
1711 | ArrayRef<const Expr *> DestExprs, |
1712 | ArrayRef<const Expr *> SrcExprs, |
1713 | ArrayRef<const Expr *> AssignmentOps) override; |
1714 | |
1715 | /// Emit an ordered region. |
1716 | /// \param OrderedOpGen Generator for the statement associated with the given |
1717 | /// ordered region. |
1718 | void emitOrderedRegion(CodeGenFunction &CGF, |
1719 | const RegionCodeGenTy &OrderedOpGen, |
1720 | SourceLocation Loc, bool IsThreads) override; |
1721 | |
1722 | /// Emit an implicit/explicit barrier for OpenMP threads. |
1723 | /// \param Kind Directive for which this implicit barrier call must be |
1724 | /// generated. Must be OMPD_barrier for explicit barrier generation. |
1725 | /// \param EmitChecks true if need to emit checks for cancellation barriers. |
1726 | /// \param ForceSimpleCall true simple barrier call must be emitted, false if |
1727 | /// runtime class decides which one to emit (simple or with cancellation |
1728 | /// checks). |
1729 | /// |
1730 | void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc, |
1731 | OpenMPDirectiveKind Kind, bool EmitChecks = true, |
1732 | bool ForceSimpleCall = false) override; |
1733 | |
1734 | /// This is used for non static scheduled types and when the ordered |
1735 | /// clause is present on the loop construct. |
1736 | /// Depending on the loop schedule, it is necessary to call some runtime |
1737 | /// routine before start of the OpenMP loop to get the loop upper / lower |
1738 | /// bounds \a LB and \a UB and stride \a ST. |
1739 | /// |
1740 | /// \param CGF Reference to current CodeGenFunction. |
1741 | /// \param Loc Clang source location. |
1742 | /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. |
1743 | /// \param IVSize Size of the iteration variable in bits. |
1744 | /// \param IVSigned Sign of the iteration variable. |
1745 | /// \param Ordered true if loop is ordered, false otherwise. |
1746 | /// \param DispatchValues struct containing llvm values for lower bound, upper |
1747 | /// bound, and chunk expression. |
1748 | /// For the default (nullptr) value, the chunk 1 will be used. |
1749 | /// |
1750 | void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc, |
1751 | const OpenMPScheduleTy &ScheduleKind, |
1752 | unsigned IVSize, bool IVSigned, bool Ordered, |
1753 | const DispatchRTInput &DispatchValues) override; |
1754 | |
1755 | /// Call the appropriate runtime routine to initialize it before start |
1756 | /// of loop. |
1757 | /// |
1758 | /// This is used only in case of static schedule, when the user did not |
1759 | /// specify a ordered clause on the loop construct. |
1760 | /// Depending on the loop schedule, it is necessary to call some runtime |
1761 | /// routine before start of the OpenMP loop to get the loop upper / lower |
1762 | /// bounds LB and UB and stride ST. |
1763 | /// |
1764 | /// \param CGF Reference to current CodeGenFunction. |
1765 | /// \param Loc Clang source location. |
1766 | /// \param DKind Kind of the directive. |
1767 | /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause. |
1768 | /// \param Values Input arguments for the construct. |
1769 | /// |
1770 | void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc, |
1771 | OpenMPDirectiveKind DKind, |
1772 | const OpenMPScheduleTy &ScheduleKind, |
1773 | const StaticRTInput &Values) override; |
1774 | |
1775 | /// |
1776 | /// \param CGF Reference to current CodeGenFunction. |
1777 | /// \param Loc Clang source location. |
1778 | /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause. |
1779 | /// \param Values Input arguments for the construct. |
1780 | /// |
1781 | void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc, |
1782 | OpenMPDistScheduleClauseKind SchedKind, |
1783 | const StaticRTInput &Values) override; |
1784 | |
1785 | /// Call the appropriate runtime routine to notify that we finished |
1786 | /// iteration of the ordered loop with the dynamic scheduling. |
1787 | /// |
1788 | /// \param CGF Reference to current CodeGenFunction. |
1789 | /// \param Loc Clang source location. |
1790 | /// \param IVSize Size of the iteration variable in bits. |
1791 | /// \param IVSigned Sign of the iteration variable. |
1792 | /// |
1793 | void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc, |
1794 | unsigned IVSize, bool IVSigned) override; |
1795 | |
1796 | /// Call the appropriate runtime routine to notify that we finished |
1797 | /// all the work with current loop. |
1798 | /// |
1799 | /// \param CGF Reference to current CodeGenFunction. |
1800 | /// \param Loc Clang source location. |
1801 | /// \param DKind Kind of the directive for which the static finish is emitted. |
1802 | /// |
1803 | void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc, |
1804 | OpenMPDirectiveKind DKind) override; |
1805 | |
1806 | /// Call __kmpc_dispatch_next( |
1807 | /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter, |
1808 | /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper, |
1809 | /// kmp_int[32|64] *p_stride); |
1810 | /// \param IVSize Size of the iteration variable in bits. |
1811 | /// \param IVSigned Sign of the iteration variable. |
1812 | /// \param IL Address of the output variable in which the flag of the |
1813 | /// last iteration is returned. |
1814 | /// \param LB Address of the output variable in which the lower iteration |
1815 | /// number is returned. |
1816 | /// \param UB Address of the output variable in which the upper iteration |
1817 | /// number is returned. |
1818 | /// \param ST Address of the output variable in which the stride value is |
1819 | /// returned. |
1820 | llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc, |
1821 | unsigned IVSize, bool IVSigned, Address IL, |
1822 | Address LB, Address UB, Address ST) override; |
1823 | |
1824 | /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 |
1825 | /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads' |
1826 | /// clause. |
1827 | /// \param NumThreads An integer value of threads. |
1828 | void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads, |
1829 | SourceLocation Loc) override; |
1830 | |
1831 | /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 |
1832 | /// global_tid, int proc_bind) to generate code for 'proc_bind' clause. |
1833 | void emitProcBindClause(CodeGenFunction &CGF, |
1834 | OpenMPProcBindClauseKind ProcBind, |
1835 | SourceLocation Loc) override; |
1836 | |
1837 | /// Returns address of the threadprivate variable for the current |
1838 | /// thread. |
1839 | /// \param VD Threadprivate variable. |
1840 | /// \param VDAddr Address of the global variable \a VD. |
1841 | /// \param Loc Location of the reference to threadprivate var. |
1842 | /// \return Address of the threadprivate variable for the current thread. |
1843 | Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD, |
1844 | Address VDAddr, SourceLocation Loc) override; |
1845 | |
1846 | /// Emit a code for initialization of threadprivate variable. It emits |
1847 | /// a call to runtime library which adds initial value to the newly created |
1848 | /// threadprivate variable (if it is not constant) and registers destructor |
1849 | /// for the variable (if any). |
1850 | /// \param VD Threadprivate variable. |
1851 | /// \param VDAddr Address of the global variable \a VD. |
1852 | /// \param Loc Location of threadprivate declaration. |
1853 | /// \param PerformInit true if initialization expression is not constant. |
1854 | llvm::Function * |
1855 | emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr, |
1856 | SourceLocation Loc, bool PerformInit, |
1857 | CodeGenFunction *CGF = nullptr) override; |
1858 | |
1859 | /// Creates artificial threadprivate variable with name \p Name and type \p |
1860 | /// VarType. |
1861 | /// \param VarType Type of the artificial threadprivate variable. |
1862 | /// \param Name Name of the artificial threadprivate variable. |
1863 | Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, |
1864 | QualType VarType, |
1865 | StringRef Name) override; |
1866 | |
1867 | /// Emit flush of the variables specified in 'omp flush' directive. |
1868 | /// \param Vars List of variables to flush. |
1869 | void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars, |
1870 | SourceLocation Loc) override; |
1871 | |
1872 | /// Emit task region for the task directive. The task region is |
1873 | /// emitted in several steps: |
1874 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
1875 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
1876 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
1877 | /// function: |
1878 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
1879 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
1880 | /// return 0; |
1881 | /// } |
1882 | /// 2. Copy a list of shared variables to field shareds of the resulting |
1883 | /// structure kmp_task_t returned by the previous call (if any). |
1884 | /// 3. Copy a pointer to destructions function to field destructions of the |
1885 | /// resulting structure kmp_task_t. |
1886 | /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, |
1887 | /// kmp_task_t *new_task), where new_task is a resulting structure from |
1888 | /// previous items. |
1889 | /// \param D Current task directive. |
1890 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
1891 | /// /*part_id*/, captured_struct */*__context*/); |
1892 | /// \param SharedsTy A type which contains references the shared variables. |
1893 | /// \param Shareds Context with the list of shared variables from the \p |
1894 | /// TaskFunction. |
1895 | /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr |
1896 | /// otherwise. |
1897 | /// \param Data Additional data for task generation like tiednsee, final |
1898 | /// state, list of privates etc. |
1899 | void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, |
1900 | const OMPExecutableDirective &D, |
1901 | llvm::Function *TaskFunction, QualType , |
1902 | Address , const Expr *IfCond, |
1903 | const OMPTaskDataTy &Data) override; |
1904 | |
1905 | /// Emit task region for the taskloop directive. The taskloop region is |
1906 | /// emitted in several steps: |
1907 | /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 |
1908 | /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds, |
1909 | /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the |
1910 | /// function: |
1911 | /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) { |
1912 | /// TaskFunction(gtid, tt->part_id, tt->shareds); |
1913 | /// return 0; |
1914 | /// } |
1915 | /// 2. Copy a list of shared variables to field shareds of the resulting |
1916 | /// structure kmp_task_t returned by the previous call (if any). |
1917 | /// 3. Copy a pointer to destructions function to field destructions of the |
1918 | /// resulting structure kmp_task_t. |
1919 | /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t |
1920 | /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int |
1921 | /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task |
1922 | /// is a resulting structure from |
1923 | /// previous items. |
1924 | /// \param D Current task directive. |
1925 | /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32 |
1926 | /// /*part_id*/, captured_struct */*__context*/); |
1927 | /// \param SharedsTy A type which contains references the shared variables. |
1928 | /// \param Shareds Context with the list of shared variables from the \p |
1929 | /// TaskFunction. |
1930 | /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr |
1931 | /// otherwise. |
1932 | /// \param Data Additional data for task generation like tiednsee, final |
1933 | /// state, list of privates etc. |
1934 | void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc, |
1935 | const OMPLoopDirective &D, llvm::Function *TaskFunction, |
1936 | QualType , Address , const Expr *IfCond, |
1937 | const OMPTaskDataTy &Data) override; |
1938 | |
1939 | /// Emit a code for reduction clause. Next code should be emitted for |
1940 | /// reduction: |
1941 | /// \code |
1942 | /// |
1943 | /// static kmp_critical_name lock = { 0 }; |
1944 | /// |
1945 | /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) { |
1946 | /// ... |
1947 | /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]); |
1948 | /// ... |
1949 | /// } |
1950 | /// |
1951 | /// ... |
1952 | /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]}; |
1953 | /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList), |
1954 | /// RedList, reduce_func, &<lock>)) { |
1955 | /// case 1: |
1956 | /// ... |
1957 | /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]); |
1958 | /// ... |
1959 | /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>); |
1960 | /// break; |
1961 | /// case 2: |
1962 | /// ... |
1963 | /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i])); |
1964 | /// ... |
1965 | /// break; |
1966 | /// default:; |
1967 | /// } |
1968 | /// \endcode |
1969 | /// |
1970 | /// \param Privates List of private copies for original reduction arguments. |
1971 | /// \param LHSExprs List of LHS in \a ReductionOps reduction operations. |
1972 | /// \param RHSExprs List of RHS in \a ReductionOps reduction operations. |
1973 | /// \param ReductionOps List of reduction operations in form 'LHS binop RHS' |
1974 | /// or 'operator binop(LHS, RHS)'. |
1975 | /// \param Options List of options for reduction codegen: |
1976 | /// WithNowait true if parent directive has also nowait clause, false |
1977 | /// otherwise. |
1978 | /// SimpleReduction Emit reduction operation only. Used for omp simd |
1979 | /// directive on the host. |
1980 | /// ReductionKind The kind of reduction to perform. |
1981 | void emitReduction(CodeGenFunction &CGF, SourceLocation Loc, |
1982 | ArrayRef<const Expr *> Privates, |
1983 | ArrayRef<const Expr *> LHSExprs, |
1984 | ArrayRef<const Expr *> RHSExprs, |
1985 | ArrayRef<const Expr *> ReductionOps, |
1986 | ReductionOptionsTy Options) override; |
1987 | |
1988 | /// Emit a code for initialization of task reduction clause. Next code |
1989 | /// should be emitted for reduction: |
1990 | /// \code |
1991 | /// |
1992 | /// _task_red_item_t red_data[n]; |
1993 | /// ... |
1994 | /// red_data[i].shar = &origs[i]; |
1995 | /// red_data[i].size = sizeof(origs[i]); |
1996 | /// red_data[i].f_init = (void*)RedInit<i>; |
1997 | /// red_data[i].f_fini = (void*)RedDest<i>; |
1998 | /// red_data[i].f_comb = (void*)RedOp<i>; |
1999 | /// red_data[i].flags = <Flag_i>; |
2000 | /// ... |
2001 | /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data); |
2002 | /// \endcode |
2003 | /// |
2004 | /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations. |
2005 | /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations. |
2006 | /// \param Data Additional data for task generation like tiedness, final |
2007 | /// state, list of privates, reductions etc. |
2008 | llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc, |
2009 | ArrayRef<const Expr *> LHSExprs, |
2010 | ArrayRef<const Expr *> RHSExprs, |
2011 | const OMPTaskDataTy &Data) override; |
2012 | |
2013 | /// Required to resolve existing problems in the runtime. Emits threadprivate |
2014 | /// variables to store the size of the VLAs/array sections for |
2015 | /// initializer/combiner/finalizer functions + emits threadprivate variable to |
2016 | /// store the pointer to the original reduction item for the custom |
2017 | /// initializer defined by declare reduction construct. |
2018 | /// \param RCG Allows to reuse an existing data for the reductions. |
2019 | /// \param N Reduction item for which fixups must be emitted. |
2020 | void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc, |
2021 | ReductionCodeGen &RCG, unsigned N) override; |
2022 | |
2023 | /// Get the address of `void *` type of the privatue copy of the reduction |
2024 | /// item specified by the \p SharedLVal. |
2025 | /// \param ReductionsPtr Pointer to the reduction data returned by the |
2026 | /// emitTaskReductionInit function. |
2027 | /// \param SharedLVal Address of the original reduction item. |
2028 | Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc, |
2029 | llvm::Value *ReductionsPtr, |
2030 | LValue ) override; |
2031 | |
2032 | /// Emit code for 'taskwait' directive. |
2033 | void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc) override; |
2034 | |
2035 | /// Emit code for 'cancellation point' construct. |
2036 | /// \param CancelRegion Region kind for which the cancellation point must be |
2037 | /// emitted. |
2038 | /// |
2039 | void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc, |
2040 | OpenMPDirectiveKind CancelRegion) override; |
2041 | |
2042 | /// Emit code for 'cancel' construct. |
2043 | /// \param IfCond Condition in the associated 'if' clause, if it was |
2044 | /// specified, nullptr otherwise. |
2045 | /// \param CancelRegion Region kind for which the cancel must be emitted. |
2046 | /// |
2047 | void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc, |
2048 | const Expr *IfCond, |
2049 | OpenMPDirectiveKind CancelRegion) override; |
2050 | |
2051 | /// Emit outilined function for 'target' directive. |
2052 | /// \param D Directive to emit. |
2053 | /// \param ParentName Name of the function that encloses the target region. |
2054 | /// \param OutlinedFn Outlined function value to be defined by this call. |
2055 | /// \param OutlinedFnID Outlined function ID value to be defined by this call. |
2056 | /// \param IsOffloadEntry True if the outlined function is an offload entry. |
2057 | /// \param CodeGen Code generation sequence for the \a D directive. |
2058 | /// An outlined function may not be an entry if, e.g. the if clause always |
2059 | /// evaluates to false. |
2060 | void emitTargetOutlinedFunction(const OMPExecutableDirective &D, |
2061 | StringRef ParentName, |
2062 | llvm::Function *&OutlinedFn, |
2063 | llvm::Constant *&OutlinedFnID, |
2064 | bool IsOffloadEntry, |
2065 | const RegionCodeGenTy &CodeGen) override; |
2066 | |
2067 | /// Emit the target offloading code associated with \a D. The emitted |
2068 | /// code attempts offloading the execution to the device, an the event of |
2069 | /// a failure it executes the host version outlined in \a OutlinedFn. |
2070 | /// \param D Directive to emit. |
2071 | /// \param OutlinedFn Host version of the code to be offloaded. |
2072 | /// \param OutlinedFnID ID of host version of the code to be offloaded. |
2073 | /// \param IfCond Expression evaluated in if clause associated with the target |
2074 | /// directive, or null if no if clause is used. |
2075 | /// \param Device Expression evaluated in device clause associated with the |
2076 | /// target directive, or null if no device clause is used. |
2077 | void emitTargetCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, |
2078 | llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, |
2079 | const Expr *IfCond, const Expr *Device) override; |
2080 | |
2081 | /// Emit the target regions enclosed in \a GD function definition or |
2082 | /// the function itself in case it is a valid device function. Returns true if |
2083 | /// \a GD was dealt with successfully. |
2084 | /// \param GD Function to scan. |
2085 | bool emitTargetFunctions(GlobalDecl GD) override; |
2086 | |
2087 | /// Emit the global variable if it is a valid device global variable. |
2088 | /// Returns true if \a GD was dealt with successfully. |
2089 | /// \param GD Variable declaration to emit. |
2090 | bool emitTargetGlobalVariable(GlobalDecl GD) override; |
2091 | |
2092 | /// Emit the global \a GD if it is meaningful for the target. Returns |
2093 | /// if it was emitted successfully. |
2094 | /// \param GD Global to scan. |
2095 | bool emitTargetGlobal(GlobalDecl GD) override; |
2096 | |
2097 | /// Creates the offloading descriptor in the event any target region |
2098 | /// was emitted in the current module and return the function that registers |
2099 | /// it. |
2100 | llvm::Function *emitRegistrationFunction() override; |
2101 | |
2102 | /// Emits code for teams call of the \a OutlinedFn with |
2103 | /// variables captured in a record which address is stored in \a |
2104 | /// CapturedStruct. |
2105 | /// \param OutlinedFn Outlined function to be run by team masters. Type of |
2106 | /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*). |
2107 | /// \param CapturedVars A pointer to the record with the references to |
2108 | /// variables used in \a OutlinedFn function. |
2109 | /// |
2110 | void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D, |
2111 | SourceLocation Loc, llvm::Function *OutlinedFn, |
2112 | ArrayRef<llvm::Value *> CapturedVars) override; |
2113 | |
2114 | /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 |
2115 | /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code |
2116 | /// for num_teams clause. |
2117 | /// \param NumTeams An integer expression of teams. |
2118 | /// \param ThreadLimit An integer expression of threads. |
2119 | void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams, |
2120 | const Expr *ThreadLimit, SourceLocation Loc) override; |
2121 | |
2122 | /// Emit the target data mapping code associated with \a D. |
2123 | /// \param D Directive to emit. |
2124 | /// \param IfCond Expression evaluated in if clause associated with the |
2125 | /// target directive, or null if no device clause is used. |
2126 | /// \param Device Expression evaluated in device clause associated with the |
2127 | /// target directive, or null if no device clause is used. |
2128 | /// \param Info A record used to store information that needs to be preserved |
2129 | /// until the region is closed. |
2130 | void emitTargetDataCalls(CodeGenFunction &CGF, |
2131 | const OMPExecutableDirective &D, const Expr *IfCond, |
2132 | const Expr *Device, const RegionCodeGenTy &CodeGen, |
2133 | TargetDataInfo &Info) override; |
2134 | |
2135 | /// Emit the data mapping/movement code associated with the directive |
2136 | /// \a D that should be of the form 'target [{enter|exit} data | update]'. |
2137 | /// \param D Directive to emit. |
2138 | /// \param IfCond Expression evaluated in if clause associated with the target |
2139 | /// directive, or null if no if clause is used. |
2140 | /// \param Device Expression evaluated in device clause associated with the |
2141 | /// target directive, or null if no device clause is used. |
2142 | void emitTargetDataStandAloneCall(CodeGenFunction &CGF, |
2143 | const OMPExecutableDirective &D, |
2144 | const Expr *IfCond, |
2145 | const Expr *Device) override; |
2146 | |
2147 | /// Emit initialization for doacross loop nesting support. |
2148 | /// \param D Loop-based construct used in doacross nesting construct. |
2149 | void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D, |
2150 | ArrayRef<Expr *> NumIterations) override; |
2151 | |
2152 | /// Emit code for doacross ordered directive with 'depend' clause. |
2153 | /// \param C 'depend' clause with 'sink|source' dependency kind. |
2154 | void emitDoacrossOrdered(CodeGenFunction &CGF, |
2155 | const OMPDependClause *C) override; |
2156 | |
2157 | /// Translates the native parameter of outlined function if this is required |
2158 | /// for target. |
2159 | /// \param FD Field decl from captured record for the parameter. |
2160 | /// \param NativeParam Parameter itself. |
2161 | const VarDecl *translateParameter(const FieldDecl *FD, |
2162 | const VarDecl *NativeParam) const override; |
2163 | |
2164 | /// Gets the address of the native argument basing on the address of the |
2165 | /// target-specific parameter. |
2166 | /// \param NativeParam Parameter itself. |
2167 | /// \param TargetParam Corresponding target-specific parameter. |
2168 | Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam, |
2169 | const VarDecl *TargetParam) const override; |
2170 | |
2171 | /// Gets the OpenMP-specific address of the local variable. |
2172 | Address getAddressOfLocalVariable(CodeGenFunction &CGF, |
2173 | const VarDecl *VD) override { |
2174 | return Address::invalid(); |
2175 | } |
2176 | }; |
2177 | |
2178 | } // namespace CodeGen |
2179 | } // namespace clang |
2180 | |
2181 | #endif |
2182 |