blob: 3b277d1a9a397dcd1f6392181de61527a75cdbbb [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a class for OpenMP runtime code generation.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
15#define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
Alexey Bataev9959db52014-05-06 10:08:46 +000016
Alexey Bataev7292c292016-04-25 12:22:29 +000017#include "CGValue.h"
Alexey Bataev62b63b12015-03-10 07:28:44 +000018#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000019#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000020#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000021#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000022#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000023#include "llvm/ADT/StringMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000024#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000025#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000026
27namespace llvm {
28class ArrayType;
29class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000030class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000031class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000032class StructType;
33class Type;
34class Value;
35} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000036
Alexey Bataev9959db52014-05-06 10:08:46 +000037namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000038class Expr;
Samuel Antaoee8fb302016-01-06 13:42:12 +000039class GlobalDecl;
Alexey Bataev8b427062016-05-25 12:36:08 +000040class OMPDependClause;
Alexey Bataev18095712014-10-10 12:19:54 +000041class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000042class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000043class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000044class OMPDeclareReductionDecl;
45class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000046
Alexey Bataev9959db52014-05-06 10:08:46 +000047namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000048class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000049class CodeGenFunction;
50class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000051
Alexey Bataev14fa1c62016-03-29 05:34:15 +000052/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
53/// region.
54class PrePostActionTy {
55public:
56 explicit PrePostActionTy() {}
57 virtual void Enter(CodeGenFunction &CGF) {}
58 virtual void Exit(CodeGenFunction &CGF) {}
59 virtual ~PrePostActionTy() {}
60};
61
62/// Class provides a way to call simple version of codegen for OpenMP region, or
63/// an advanced with possible pre|post-actions in codegen.
64class RegionCodeGenTy final {
65 intptr_t CodeGen;
66 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
67 CodeGenTy Callback;
68 mutable PrePostActionTy *PrePostAction;
69 RegionCodeGenTy() = delete;
70 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
71 template <typename Callable>
72 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
73 PrePostActionTy &Action) {
74 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
75 }
76
77public:
78 template <typename Callable>
79 RegionCodeGenTy(
80 Callable &&CodeGen,
81 typename std::enable_if<
82 !std::is_same<typename std::remove_reference<Callable>::type,
83 RegionCodeGenTy>::value>::type * = nullptr)
84 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
85 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
86 PrePostAction(nullptr) {}
87 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
88 void operator()(CodeGenFunction &CGF) const;
89};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000090
Alexey Bataev24b5bae2016-04-28 09:23:51 +000091struct OMPTaskDataTy final {
92 SmallVector<const Expr *, 4> PrivateVars;
93 SmallVector<const Expr *, 4> PrivateCopies;
94 SmallVector<const Expr *, 4> FirstprivateVars;
95 SmallVector<const Expr *, 4> FirstprivateCopies;
96 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000097 SmallVector<const Expr *, 4> LastprivateVars;
98 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +000099 SmallVector<const Expr *, 4> ReductionVars;
100 SmallVector<const Expr *, 4> ReductionCopies;
101 SmallVector<const Expr *, 4> ReductionOps;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000102 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
103 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
104 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000105 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000106 llvm::Value *Reductions = nullptr;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000107 unsigned NumberOfParts = 0;
108 bool Tied = true;
109 bool Nogroup = false;
110};
111
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000112/// Class intended to support codegen of all kind of the reduction clauses.
113class ReductionCodeGen {
114private:
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000115 /// Data required for codegen of reduction clauses.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000116 struct ReductionData {
117 /// Reference to the original shared item.
118 const Expr *Ref = nullptr;
119 /// Helper expression for generation of private copy.
120 const Expr *Private = nullptr;
121 /// Helper expression for generation reduction operation.
122 const Expr *ReductionOp = nullptr;
123 ReductionData(const Expr *Ref, const Expr *Private, const Expr *ReductionOp)
124 : Ref(Ref), Private(Private), ReductionOp(ReductionOp) {}
125 };
126 /// List of reduction-based clauses.
127 SmallVector<ReductionData, 4> ClausesData;
128
129 /// List of addresses of original shared variables/expressions.
130 SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
131 /// Sizes of the reduction items in chars.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000132 SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000133 /// Base declarations for the reduction items.
134 SmallVector<const VarDecl *, 4> BaseDecls;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000135
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000136 /// Emits lvalue for shared expresion.
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.
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000143 /// \param SharedLVal Address of the original shared variable.
144 /// \param DRD Declare reduction construct used for reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000145 void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000146 Address PrivateAddr, LValue SharedLVal,
147 const OMPDeclareReductionDecl *DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000148
149public:
150 ReductionCodeGen(ArrayRef<const Expr *> Shareds,
151 ArrayRef<const Expr *> Privates,
152 ArrayRef<const Expr *> ReductionOps);
153 /// Emits lvalue for a reduction item.
154 /// \param N Number of the reduction item.
155 void emitSharedLValue(CodeGenFunction &CGF, unsigned N);
156 /// Emits the code for the variable-modified type, if required.
157 /// \param N Number of the reduction item.
158 void emitAggregateType(CodeGenFunction &CGF, unsigned N);
159 /// Emits the code for the variable-modified type, if required.
160 /// \param N Number of the reduction item.
161 /// \param Size Size of the type in chars.
162 void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
163 /// Performs initialization of the private copy for the reduction item.
164 /// \param N Number of the reduction item.
165 /// \param PrivateAddr Address of the corresponding private item.
166 /// \param DefaultInit Default initialization sequence that should be
167 /// performed if no reduction specific initialization is found.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000168 /// \param SharedLVal Address of the original shared variable.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000169 void
170 emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
171 LValue SharedLVal,
172 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000173 /// Returns true if the private copy requires cleanups.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000174 bool needCleanups(unsigned N);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000175 /// Emits cleanup code for the reduction item.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000176 /// \param N Number of the reduction item.
177 /// \param PrivateAddr Address of the corresponding private item.
178 void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000179 /// Adjusts \p PrivatedAddr for using instead of the original variable
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000180 /// address in normal operations.
181 /// \param N Number of the reduction item.
182 /// \param PrivateAddr Address of the corresponding private item.
183 Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
184 Address PrivateAddr);
185 /// Returns LValue for the reduction item.
186 LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000187 /// Returns the size of the reduction item (in chars and total number of
188 /// elements in the item), or nullptr, if the size is a constant.
189 std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
190 return Sizes[N];
191 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000192 /// Returns the base declaration of the reduction item.
193 const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000194 /// Returns true if the initialization of the reduction item uses initializer
195 /// from declare reduction construct.
196 bool usesReductionInitializer(unsigned N) const;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000197};
198
Alexey Bataev9959db52014-05-06 10:08:46 +0000199class CGOpenMPRuntime {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000200protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000201 CodeGenModule &CGM;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000202
203 /// \brief Creates offloading entry for the provided entry ID \a ID,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000204 /// address \a Addr, size \a Size, and flags \a Flags.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000205 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000206 uint64_t Size, int32_t Flags = 0);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000207
208 /// \brief Helper to emit outlined function for 'target' directive.
209 /// \param D Directive to emit.
210 /// \param ParentName Name of the function that encloses the target region.
211 /// \param OutlinedFn Outlined function value to be defined by this call.
212 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
213 /// \param IsOffloadEntry True if the outlined function is an offload entry.
214 /// \param CodeGen Lambda codegen specific to an accelerator device.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000215 /// An outlined function may not be an entry if, e.g. the if clause always
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000216 /// evaluates to false.
217 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
218 StringRef ParentName,
219 llvm::Function *&OutlinedFn,
220 llvm::Constant *&OutlinedFnID,
221 bool IsOffloadEntry,
222 const RegionCodeGenTy &CodeGen);
223
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000224 /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
225 /// function. Here is the logic:
226 /// if (Cond) {
227 /// ThenGen();
228 /// } else {
229 /// ElseGen();
230 /// }
231 void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
232 const RegionCodeGenTy &ThenGen,
233 const RegionCodeGenTy &ElseGen);
234
235 /// \brief Emits object of ident_t type with info for source location.
236 /// \param Flags Flags for OpenMP location.
237 ///
238 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
239 unsigned Flags = 0);
240
241 /// \brief Returns pointer to ident_t type.
242 llvm::Type *getIdentTyPointerTy();
243
244 /// \brief Gets thread id value for the current thread.
245 ///
246 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
247
248 /// \brief Get the function name of an outlined region.
249 // The name can be customized depending on the target.
250 //
251 virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
252
Alexey Bataev3c595a62017-08-14 15:01:03 +0000253 /// Emits \p Callee function call with arguments \p Args with location \p Loc.
254 void emitCall(CodeGenFunction &CGF, llvm::Value *Callee,
255 ArrayRef<llvm::Value *> Args = llvm::None,
256 SourceLocation Loc = SourceLocation()) const;
257
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000258private:
Alexey Bataev9959db52014-05-06 10:08:46 +0000259 /// \brief Default const ident_t object used for initialization of all other
260 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000261 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000262 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000263 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
264 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000265 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000266
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000267 llvm::StructType *IdentTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000268 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000269 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
270 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000271 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
272 /// Original representation is:
273 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000274 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000275 /// \brief Stores debug location and ThreadID for the function.
276 struct DebugLocThreadIdTy {
277 llvm::Value *DebugLoc;
278 llvm::Value *ThreadID;
279 };
280 /// \brief Map of local debug location, ThreadId and functions.
281 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
282 OpenMPLocThreadIDMapTy;
283 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000284 /// Map of UDRs and corresponding combiner/initializer.
285 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
286 std::pair<llvm::Function *, llvm::Function *>>
287 UDRMapTy;
288 UDRMapTy UDRMap;
289 /// Map of functions and locally defined UDRs.
290 typedef llvm::DenseMap<llvm::Function *,
291 SmallVector<const OMPDeclareReductionDecl *, 4>>
292 FunctionUDRMapTy;
293 FunctionUDRMapTy FunctionUDRMap;
294 IdentifierInfo *In = nullptr;
295 IdentifierInfo *Out = nullptr;
296 IdentifierInfo *Priv = nullptr;
297 IdentifierInfo *Orig = nullptr;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000298 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
299 /// kmp_critical_name[8];
300 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000301 /// \brief An ordered map of auto-generated variables to their unique names.
302 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
303 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
304 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
305 /// variables.
306 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
307 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000308 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000309 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000310 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000311 /// \brief Type typedef struct kmp_task {
312 /// void * shareds; /**< pointer to block of pointers to
313 /// shared vars */
314 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
315 /// executing task */
316 /// kmp_int32 part_id; /**< part id for the task */
317 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
318 /// deconstructors of firstprivate C++ objects */
319 /// } kmp_task_t;
320 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000321 /// \brief Type typedef struct kmp_depend_info {
322 /// kmp_intptr_t base_addr;
323 /// size_t len;
324 /// struct {
325 /// bool in:1;
326 /// bool out:1;
327 /// } flags;
328 /// } kmp_depend_info_t;
329 QualType KmpDependInfoTy;
Alexey Bataev8b427062016-05-25 12:36:08 +0000330 /// struct kmp_dim { // loop bounds info casted to kmp_int64
331 /// kmp_int64 lo; // lower
332 /// kmp_int64 up; // upper
333 /// kmp_int64 st; // stride
334 /// };
335 QualType KmpDimTy;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000336 /// \brief Type struct __tgt_offload_entry{
337 /// void *addr; // Pointer to the offload entry info.
338 /// // (function or global)
339 /// char *name; // Name of the function or global.
340 /// size_t size; // Size of the entry info (0 if it a function).
341 /// };
342 QualType TgtOffloadEntryQTy;
343 /// struct __tgt_device_image{
344 /// void *ImageStart; // Pointer to the target code start.
345 /// void *ImageEnd; // Pointer to the target code end.
346 /// // We also add the host entries to the device image, as it may be useful
347 /// // for the target runtime to have access to that information.
348 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
349 /// // the entries.
350 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
351 /// // entries (non inclusive).
352 /// };
353 QualType TgtDeviceImageQTy;
354 /// struct __tgt_bin_desc{
355 /// int32_t NumDevices; // Number of devices supported.
356 /// __tgt_device_image *DeviceImages; // Arrays of device images
357 /// // (one per device).
358 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
359 /// // entries.
360 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
361 /// // entries (non inclusive).
362 /// };
363 QualType TgtBinaryDescriptorQTy;
364 /// \brief Entity that registers the offloading constants that were emitted so
365 /// far.
366 class OffloadEntriesInfoManagerTy {
367 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000368
Samuel Antaoee8fb302016-01-06 13:42:12 +0000369 /// \brief Number of entries registered so far.
370 unsigned OffloadingEntriesNum;
371
372 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000373 /// Base class of the entries info.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000374 class OffloadEntryInfo {
375 public:
Samuel Antaof83efdb2017-01-05 16:02:49 +0000376 /// Kind of a given entry. Currently, only target regions are
Samuel Antaoee8fb302016-01-06 13:42:12 +0000377 /// supported.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000378 enum OffloadingEntryInfoKinds : unsigned {
Samuel Antaoee8fb302016-01-06 13:42:12 +0000379 // Entry is a target region.
380 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
381 // Invalid entry info.
382 OFFLOAD_ENTRY_INFO_INVALID = ~0u
383 };
384
Samuel Antaof83efdb2017-01-05 16:02:49 +0000385 OffloadEntryInfo()
386 : Flags(0), Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
387 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
388 int32_t Flags)
389 : Flags(Flags), Order(Order), Kind(Kind) {}
Samuel Antaoee8fb302016-01-06 13:42:12 +0000390
391 bool isValid() const { return Order != ~0u; }
392 unsigned getOrder() const { return Order; }
393 OffloadingEntryInfoKinds getKind() const { return Kind; }
Samuel Antaof83efdb2017-01-05 16:02:49 +0000394 int32_t getFlags() const { return Flags; }
395 void setFlags(int32_t NewFlags) { Flags = NewFlags; }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000396 static bool classof(const OffloadEntryInfo *Info) { return true; }
397
Samuel Antaof83efdb2017-01-05 16:02:49 +0000398 private:
399 /// Flags associated with the device global.
400 int32_t Flags;
401
402 /// Order this entry was emitted.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000403 unsigned Order;
404
405 OffloadingEntryInfoKinds Kind;
406 };
407
408 /// \brief Return true if a there are no entries defined.
409 bool empty() const;
410 /// \brief Return number of entries defined so far.
411 unsigned size() const { return OffloadingEntriesNum; }
412 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
413 : CGM(CGM), OffloadingEntriesNum(0) {}
414
415 ///
416 /// Target region entries related.
417 ///
418 /// \brief Target region entries info.
419 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
420 // \brief Address of the entity that has to be mapped for offloading.
421 llvm::Constant *Addr;
422 // \brief Address that can be used as the ID of the entry.
423 llvm::Constant *ID;
424
425 public:
426 OffloadEntryInfoTargetRegion()
Samuel Antaof83efdb2017-01-05 16:02:49 +0000427 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u,
428 /*Flags=*/0),
Samuel Antaoee8fb302016-01-06 13:42:12 +0000429 Addr(nullptr), ID(nullptr) {}
430 explicit OffloadEntryInfoTargetRegion(unsigned Order,
431 llvm::Constant *Addr,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000432 llvm::Constant *ID, int32_t Flags)
433 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order, Flags),
Samuel Antaoee8fb302016-01-06 13:42:12 +0000434 Addr(Addr), ID(ID) {}
435
436 llvm::Constant *getAddress() const { return Addr; }
437 llvm::Constant *getID() const { return ID; }
438 void setAddress(llvm::Constant *V) {
439 assert(!Addr && "Address as been set before!");
440 Addr = V;
441 }
442 void setID(llvm::Constant *V) {
443 assert(!ID && "ID as been set before!");
444 ID = V;
445 }
446 static bool classof(const OffloadEntryInfo *Info) {
447 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
448 }
449 };
450 /// \brief Initialize target region entry.
451 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
452 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000453 unsigned Order);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000454 /// \brief Register target region entry.
455 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
456 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +0000457 llvm::Constant *Addr, llvm::Constant *ID,
458 int32_t Flags);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000459 /// \brief Return true if a target region entry with the provided
460 /// information exists.
461 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000462 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000463 /// brief Applies action \a Action on all registered entries.
464 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Samuel Antao2de62b02016-02-13 23:35:10 +0000465 OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000466 OffloadTargetRegionEntryInfoActTy;
467 void actOnTargetRegionEntriesInfo(
468 const OffloadTargetRegionEntryInfoActTy &Action);
469
470 private:
471 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000472 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000473 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000474 OffloadEntriesTargetRegionPerLine;
475 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
476 OffloadEntriesTargetRegionPerParentName;
477 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
478 OffloadEntriesTargetRegionPerFile;
479 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
480 OffloadEntriesTargetRegionPerDevice;
481 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
482 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
483 };
484 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
485
486 /// \brief Creates and registers offloading binary descriptor for the current
487 /// compilation unit. The function that does the registration is returned.
488 llvm::Function *createOffloadingBinaryDescriptorRegistration();
489
Samuel Antaoee8fb302016-01-06 13:42:12 +0000490 /// \brief Creates all the offload entries in the current compilation unit
491 /// along with the associated metadata.
492 void createOffloadEntriesAndInfoMetadata();
493
494 /// \brief Loads all the offload entries information from the host IR
495 /// metadata.
496 void loadOffloadInfoMetadata();
497
498 /// \brief Returns __tgt_offload_entry type.
499 QualType getTgtOffloadEntryQTy();
500
501 /// \brief Returns __tgt_device_image type.
502 QualType getTgtDeviceImageQTy();
503
504 /// \brief Returns __tgt_bin_desc type.
505 QualType getTgtBinaryDescriptorQTy();
506
507 /// \brief Start scanning from statement \a S and and emit all target regions
508 /// found along the way.
509 /// \param S Starting statement.
510 /// \param ParentName Name of the function declaration that is being scanned.
511 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000512
513 /// \brief Build type kmp_routine_entry_t (if not built yet).
514 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000515
Alexey Bataevd74d0602014-10-13 06:02:40 +0000516 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000517 llvm::Type *getKmpc_MicroPointerTy();
518
519 /// \brief Returns specified OpenMP runtime function.
520 /// \param Function OpenMP runtime function.
521 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000522 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000523
Alexander Musman21212e42015-03-13 10:38:23 +0000524 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
525 /// size \a IVSize and sign \a IVSigned.
526 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
527
Alexander Musman92bdaab2015-03-12 13:37:50 +0000528 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
529 /// size \a IVSize and sign \a IVSigned.
530 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
531
532 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
533 /// size \a IVSize and sign \a IVSigned.
534 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
535
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000536 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
537 /// size \a IVSize and sign \a IVSigned.
538 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
539
Alexey Bataev97720002014-11-11 04:05:39 +0000540 /// \brief If the specified mangled name is not in the module, create and
541 /// return threadprivate cache object. This object is a pointer's worth of
542 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000543 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000544 /// \return Cache variable for the specified threadprivate.
545 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
546
Alexey Bataevd74d0602014-10-13 06:02:40 +0000547 /// \brief Emits address of the word in a memory where current thread id is
548 /// stored.
John McCall7f416cc2015-09-08 08:05:57 +0000549 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000550
Alexey Bataev97720002014-11-11 04:05:39 +0000551 /// \brief Gets (if variable with the given name already exist) or creates
552 /// internal global variable with the specified Name. The created variable has
553 /// linkage CommonLinkage by default and is initialized by null value.
554 /// \param Ty Type of the global variable. If it is exist already the type
555 /// must be the same.
556 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000557 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000558 const llvm::Twine &Name);
559
560 /// \brief Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000561 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000562
563 /// \brief Emits initialization code for the threadprivate variables.
564 /// \param VDAddr Address of the global variable \a VD.
565 /// \param Ctor Pointer to a global init function for \a VD.
566 /// \param CopyCtor Pointer to a global copy function for \a VD.
567 /// \param Dtor Pointer to a global destructor function for \a VD.
568 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000569 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000570 llvm::Value *Ctor, llvm::Value *CopyCtor,
571 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000572
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000573 /// \brief Returns corresponding lock object for the specified critical region
574 /// name. If the lock object does not exist it is created, otherwise the
575 /// reference to the existing copy is returned.
576 /// \param CriticalName Name of the critical region.
577 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000578 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000579
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000580 struct TaskResultTy {
581 llvm::Value *NewTask = nullptr;
582 llvm::Value *TaskEntry = nullptr;
583 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000584 LValue TDBase;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000585 RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000586 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000587 };
588 /// Emit task region for the task directive. The task region is emitted in
589 /// several steps:
590 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
591 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
592 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
593 /// function:
594 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
595 /// TaskFunction(gtid, tt->part_id, tt->shareds);
596 /// return 0;
597 /// }
598 /// 2. Copy a list of shared variables to field shareds of the resulting
599 /// structure kmp_task_t returned by the previous call (if any).
600 /// 3. Copy a pointer to destructions function to field destructions of the
601 /// resulting structure kmp_task_t.
602 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000603 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
604 /// /*part_id*/, captured_struct */*__context*/);
605 /// \param SharedsTy A type which contains references the shared variables.
606 /// \param Shareds Context with the list of shared variables from the \p
607 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000608 /// \param Data Additional data for task generation like tiednsee, final
609 /// state, list of privates etc.
610 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
611 const OMPExecutableDirective &D,
612 llvm::Value *TaskFunction, QualType SharedsTy,
613 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000614
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000615public:
616 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000617 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000618 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000619
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000620 /// Emit code for the specified user defined reduction construct.
621 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
622 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000623 /// Get combiner/initializer for the specified user-defined reduction, if any.
624 virtual std::pair<llvm::Function *, llvm::Function *>
625 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000626
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000627 /// \brief Emits outlined function for the specified OpenMP parallel directive
628 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
629 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000630 /// \param D OpenMP directive.
631 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000632 /// \param InnermostKind Kind of innermost directive (for simple directives it
633 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000634 /// \param CodeGen Code generation sequence for the \a D directive.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +0000635 virtual llvm::Value *emitParallelOutlinedFunction(
636 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
637 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
638
639 /// \brief Emits outlined function for the specified OpenMP teams directive
640 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
641 /// kmp_int32 BoundID, struct context_vars*).
642 /// \param D OpenMP directive.
643 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
644 /// \param InnermostKind Kind of innermost directive (for simple directives it
645 /// is a directive itself, for combined - its innermost directive).
646 /// \param CodeGen Code generation sequence for the \a D directive.
647 virtual llvm::Value *emitTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000648 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
649 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000650
Alexey Bataev62b63b12015-03-10 07:28:44 +0000651 /// \brief Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000652 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
653 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000654 /// \param D OpenMP directive.
655 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000656 /// \param PartIDVar Variable for partition id in the current OpenMP untied
657 /// task region.
658 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000659 /// \param InnermostKind Kind of innermost directive (for simple directives it
660 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000661 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000662 /// \param Tied true if task is generated for tied task, false otherwise.
663 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
664 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000665 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000666 virtual llvm::Value *emitTaskOutlinedFunction(
667 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000668 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
669 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
670 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000671
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000672 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000673 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000674 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000675
Alexey Bataev1d677132015-04-22 13:57:31 +0000676 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
677 /// variables captured in a record which address is stored in \a
678 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000679 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000680 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000681 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000682 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000683 /// \param IfCond Condition in the associated 'if' clause, if it was
684 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000685 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000686 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
687 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000688 ArrayRef<llvm::Value *> CapturedVars,
689 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000690
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000691 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000692 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000693 /// \param CriticalOpGen Generator for the statement associated with the given
694 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000695 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000696 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000697 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000698 SourceLocation Loc,
699 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000700
Alexey Bataev8d690652014-12-04 07:23:53 +0000701 /// \brief Emits a master region.
702 /// \param MasterOpGen Generator for the statement associated with the given
703 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000704 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000705 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000706 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000707
Alexey Bataev9f797f32015-02-05 05:57:51 +0000708 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000709 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000710
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000711 /// \brief Emit a taskgroup region.
712 /// \param TaskgroupOpGen Generator for the statement associated with the
713 /// given taskgroup region.
714 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
715 const RegionCodeGenTy &TaskgroupOpGen,
716 SourceLocation Loc);
717
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000718 /// \brief Emits a single region.
719 /// \param SingleOpGen Generator for the statement associated with the given
720 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000721 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000722 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000723 SourceLocation Loc,
724 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000725 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000726 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000727 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000728
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000729 /// \brief Emit an ordered region.
730 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000731 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000732 virtual void emitOrderedRegion(CodeGenFunction &CGF,
733 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000734 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000735
Alexey Bataevf2685682015-03-30 04:30:22 +0000736 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
737 /// \param Kind Directive for which this implicit barrier call must be
738 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000739 /// \param EmitChecks true if need to emit checks for cancellation barriers.
740 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
741 /// runtime class decides which one to emit (simple or with cancellation
742 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000743 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000744 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000745 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000746 bool EmitChecks = true,
747 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000748
Alexander Musmanc6388682014-12-15 07:07:06 +0000749 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
750 /// This kind of worksharing directive is emitted without outer loop.
751 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
752 /// \param Chunked True if chunk is specified in the clause.
753 ///
754 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
755 bool Chunked) const;
756
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000757 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
758 /// This kind of distribute directive is emitted without outer loop.
759 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
760 /// \param Chunked True if chunk is specified in the clause.
761 ///
762 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
763 bool Chunked) const;
764
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000765 /// \brief Check if the specified \a ScheduleKind is dynamic.
766 /// This kind of worksharing directive is emitted without outer loop.
767 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
768 ///
769 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
770
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000771 /// struct with the values to be passed to the dispatch runtime function
772 struct DispatchRTInput {
773 /// Loop lower bound
774 llvm::Value *LB = nullptr;
775 /// Loop upper bound
776 llvm::Value *UB = nullptr;
777 /// Chunk size specified using 'schedule' clause (nullptr if chunk
778 /// was not specified)
779 llvm::Value *Chunk = nullptr;
780 DispatchRTInput() = default;
781 DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
782 : LB(LB), UB(UB), Chunk(Chunk) {}
783 };
784
785 /// Call the appropriate runtime routine to initialize it before start
786 /// of loop.
787
788 /// This is used for non static scheduled types and when the ordered
789 /// clause is present on the loop construct.
790 /// Depending on the loop schedule, it is necessary to call some runtime
791 /// routine before start of the OpenMP loop to get the loop upper / lower
792 /// bounds \a LB and \a UB and stride \a ST.
793 ///
794 /// \param CGF Reference to current CodeGenFunction.
795 /// \param Loc Clang source location.
796 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
797 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000798 /// \param IVSigned Sign of the iteration variable.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000799 /// \param Ordered true if loop is ordered, false otherwise.
800 /// \param DispatchValues struct containing llvm values for lower bound, upper
801 /// bound, and chunk expression.
802 /// For the default (nullptr) value, the chunk 1 will be used.
803 ///
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000804 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000805 const OpenMPScheduleTy &ScheduleKind,
806 unsigned IVSize, bool IVSigned, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000807 const DispatchRTInput &DispatchValues);
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000808
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000809 /// Struct with the values to be passed to the static runtime function
810 struct StaticRTInput {
811 /// Size of the iteration variable in bits.
812 unsigned IVSize = 0;
813 /// Sign of the iteration variable.
814 bool IVSigned = false;
815 /// true if loop is ordered, false otherwise.
816 bool Ordered = false;
817 /// Address of the output variable in which the flag of the last iteration
818 /// is returned.
819 Address IL = Address::invalid();
820 /// Address of the output variable in which the lower iteration number is
821 /// returned.
822 Address LB = Address::invalid();
823 /// Address of the output variable in which the upper iteration number is
824 /// returned.
825 Address UB = Address::invalid();
826 /// Address of the output variable in which the stride value is returned
827 /// necessary to generated the static_chunked scheduled loop.
828 Address ST = Address::invalid();
829 /// Value of the chunk for the static_chunked scheduled loop. For the
830 /// default (nullptr) value, the chunk 1 will be used.
831 llvm::Value *Chunk = nullptr;
832 StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
833 Address LB, Address UB, Address ST,
834 llvm::Value *Chunk = nullptr)
835 : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
836 UB(UB), ST(ST), Chunk(Chunk) {}
837 };
Alexander Musmanc6388682014-12-15 07:07:06 +0000838 /// \brief Call the appropriate runtime routine to initialize it before start
839 /// of loop.
840 ///
Carlo Bertollib0ff0a62017-04-25 17:52:12 +0000841 /// This is used only in case of static schedule, when the user did not
842 /// specify a ordered clause on the loop construct.
843 /// Depending on the loop schedule, it is necessary to call some runtime
Alexander Musmanc6388682014-12-15 07:07:06 +0000844 /// routine before start of the OpenMP loop to get the loop upper / lower
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000845 /// bounds LB and UB and stride ST.
Alexander Musmanc6388682014-12-15 07:07:06 +0000846 ///
847 /// \param CGF Reference to current CodeGenFunction.
848 /// \param Loc Clang source location.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000849 /// \param DKind Kind of the directive.
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000850 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000851 /// \param Values Input arguments for the construct.
Alexander Musmanc6388682014-12-15 07:07:06 +0000852 ///
John McCall7f416cc2015-09-08 08:05:57 +0000853 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000854 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000855 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000856 const StaticRTInput &Values);
Alexander Musmanc6388682014-12-15 07:07:06 +0000857
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000858 ///
859 /// \param CGF Reference to current CodeGenFunction.
860 /// \param Loc Clang source location.
861 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000862 /// \param Values Input arguments for the construct.
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000863 ///
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000864 virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
865 SourceLocation Loc,
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000866 OpenMPDistScheduleClauseKind SchedKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000867 const StaticRTInput &Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000868
Alexander Musmanc6388682014-12-15 07:07:06 +0000869 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000870 /// iteration of the ordered loop with the dynamic scheduling.
871 ///
872 /// \param CGF Reference to current CodeGenFunction.
873 /// \param Loc Clang source location.
874 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000875 /// \param IVSigned Sign of the iteration variable.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000876 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000877 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
878 SourceLocation Loc, unsigned IVSize,
879 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000880
881 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000882 /// all the work with current loop.
883 ///
884 /// \param CGF Reference to current CodeGenFunction.
885 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000886 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000887 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000888
Alexander Musman92bdaab2015-03-12 13:37:50 +0000889 /// Call __kmpc_dispatch_next(
890 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
891 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
892 /// kmp_int[32|64] *p_stride);
893 /// \param IVSize Size of the iteration variable in bits.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +0000894 /// \param IVSigned Sign of the iteration variable.
Alexander Musman92bdaab2015-03-12 13:37:50 +0000895 /// \param IL Address of the output variable in which the flag of the
896 /// last iteration is returned.
897 /// \param LB Address of the output variable in which the lower iteration
898 /// number is returned.
899 /// \param UB Address of the output variable in which the upper iteration
900 /// number is returned.
901 /// \param ST Address of the output variable in which the stride value is
902 /// returned.
903 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
904 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000905 Address IL, Address LB,
906 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000907
Alexey Bataevb2059782014-10-13 08:23:51 +0000908 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
909 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
910 /// clause.
911 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000912 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
913 llvm::Value *NumThreads,
914 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000915
Alexey Bataev7f210c62015-06-18 13:40:03 +0000916 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
917 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
918 virtual void emitProcBindClause(CodeGenFunction &CGF,
919 OpenMPProcBindClauseKind ProcBind,
920 SourceLocation Loc);
921
Alexey Bataev97720002014-11-11 04:05:39 +0000922 /// \brief Returns address of the threadprivate variable for the current
923 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000924 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000925 /// \param VDAddr Address of the global variable \a VD.
926 /// \param Loc Location of the reference to threadprivate var.
927 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000928 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
929 const VarDecl *VD,
930 Address VDAddr,
931 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000932
933 /// \brief Emit a code for initialization of threadprivate variable. It emits
934 /// a call to runtime library which adds initial value to the newly created
935 /// threadprivate variable (if it is not constant) and registers destructor
936 /// for the variable (if any).
937 /// \param VD Threadprivate variable.
938 /// \param VDAddr Address of the global variable \a VD.
939 /// \param Loc Location of threadprivate declaration.
940 /// \param PerformInit true if initialization expression is not constant.
941 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000942 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000943 SourceLocation Loc, bool PerformInit,
944 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000945
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000946 /// Creates artificial threadprivate variable with name \p Name and type \p
947 /// VarType.
948 /// \param VarType Type of the artificial threadprivate variable.
949 /// \param Name Name of the artificial threadprivate variable.
950 virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
951 QualType VarType,
952 StringRef Name);
953
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000954 /// \brief Emit flush of the variables specified in 'omp flush' directive.
955 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000956 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
957 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000958
959 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000960 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000961 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
962 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
963 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
964 /// function:
965 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
966 /// TaskFunction(gtid, tt->part_id, tt->shareds);
967 /// return 0;
968 /// }
969 /// 2. Copy a list of shared variables to field shareds of the resulting
970 /// structure kmp_task_t returned by the previous call (if any).
971 /// 3. Copy a pointer to destructions function to field destructions of the
972 /// resulting structure kmp_task_t.
973 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
974 /// kmp_task_t *new_task), where new_task is a resulting structure from
975 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000976 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000977 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
978 /// /*part_id*/, captured_struct */*__context*/);
979 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000980 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000981 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000982 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
983 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000984 /// \param Data Additional data for task generation like tiednsee, final
985 /// state, list of privates etc.
986 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
987 const OMPExecutableDirective &D,
988 llvm::Value *TaskFunction, QualType SharedsTy,
989 Address Shareds, const Expr *IfCond,
990 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000991
Alexey Bataev7292c292016-04-25 12:22:29 +0000992 /// Emit task region for the taskloop directive. The taskloop region is
993 /// emitted in several steps:
994 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
995 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
996 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
997 /// function:
998 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
999 /// TaskFunction(gtid, tt->part_id, tt->shareds);
1000 /// return 0;
1001 /// }
1002 /// 2. Copy a list of shared variables to field shareds of the resulting
1003 /// structure kmp_task_t returned by the previous call (if any).
1004 /// 3. Copy a pointer to destructions function to field destructions of the
1005 /// resulting structure kmp_task_t.
1006 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
1007 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
1008 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
1009 /// is a resulting structure from
1010 /// previous items.
1011 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +00001012 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
1013 /// /*part_id*/, captured_struct */*__context*/);
1014 /// \param SharedsTy A type which contains references the shared variables.
1015 /// \param Shareds Context with the list of shared variables from the \p
1016 /// TaskFunction.
1017 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
1018 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001019 /// \param Data Additional data for task generation like tiednsee, final
1020 /// state, list of privates etc.
Alexey Bataev7292c292016-04-25 12:22:29 +00001021 virtual void emitTaskLoopCall(
1022 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00001023 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
1024 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00001025
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001026 /// \brief Emit code for the directive that does not require outlining.
1027 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001028 /// \param InnermostKind Kind of innermost directive (for simple directives it
1029 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001030 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001031 /// \param HasCancel true if region has inner cancel directive, false
1032 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001033 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001034 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00001035 const RegionCodeGenTy &CodeGen,
1036 bool HasCancel = false);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001037
1038 /// Emits reduction function.
1039 /// \param ArgsType Array type containing pointers to reduction variables.
1040 /// \param Privates List of private copies for original reduction arguments.
1041 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1042 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1043 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1044 /// or 'operator binop(LHS, RHS)'.
1045 llvm::Value *emitReductionFunction(CodeGenModule &CGM, llvm::Type *ArgsType,
1046 ArrayRef<const Expr *> Privates,
1047 ArrayRef<const Expr *> LHSExprs,
1048 ArrayRef<const Expr *> RHSExprs,
1049 ArrayRef<const Expr *> ReductionOps);
1050
1051 /// Emits single reduction combiner
1052 void emitSingleReductionCombiner(CodeGenFunction &CGF,
1053 const Expr *ReductionOp,
1054 const Expr *PrivateRef,
1055 const DeclRefExpr *LHS,
1056 const DeclRefExpr *RHS);
1057
1058 struct ReductionOptionsTy {
1059 bool WithNowait;
1060 bool SimpleReduction;
1061 OpenMPDirectiveKind ReductionKind;
1062 };
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001063 /// \brief Emit a code for reduction clause. Next code should be emitted for
1064 /// reduction:
1065 /// \code
1066 ///
1067 /// static kmp_critical_name lock = { 0 };
1068 ///
1069 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
1070 /// ...
1071 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
1072 /// ...
1073 /// }
1074 ///
1075 /// ...
1076 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
1077 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
1078 /// RedList, reduce_func, &<lock>)) {
1079 /// case 1:
1080 /// ...
1081 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
1082 /// ...
1083 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
1084 /// break;
1085 /// case 2:
1086 /// ...
1087 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
1088 /// ...
1089 /// break;
1090 /// default:;
1091 /// }
1092 /// \endcode
1093 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001094 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001095 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
1096 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
1097 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
1098 /// or 'operator binop(LHS, RHS)'.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001099 /// \param Options List of options for reduction codegen:
1100 /// WithNowait true if parent directive has also nowait clause, false
1101 /// otherwise.
1102 /// SimpleReduction Emit reduction operation only. Used for omp simd
1103 /// directive on the host.
1104 /// ReductionKind The kind of reduction to perform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001105 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001106 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001107 ArrayRef<const Expr *> LHSExprs,
1108 ArrayRef<const Expr *> RHSExprs,
1109 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001110 ReductionOptionsTy Options);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001111
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001112 /// Emit a code for initialization of task reduction clause. Next code
1113 /// should be emitted for reduction:
1114 /// \code
1115 ///
1116 /// _task_red_item_t red_data[n];
1117 /// ...
1118 /// red_data[i].shar = &origs[i];
1119 /// red_data[i].size = sizeof(origs[i]);
1120 /// red_data[i].f_init = (void*)RedInit<i>;
1121 /// red_data[i].f_fini = (void*)RedDest<i>;
1122 /// red_data[i].f_comb = (void*)RedOp<i>;
1123 /// red_data[i].flags = <Flag_i>;
1124 /// ...
1125 /// void* tg1 = __kmpc_task_reduction_init(gtid, n, red_data);
1126 /// \endcode
1127 ///
1128 /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
1129 /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
1130 /// \param Data Additional data for task generation like tiedness, final
1131 /// state, list of privates, reductions etc.
1132 virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
1133 SourceLocation Loc,
1134 ArrayRef<const Expr *> LHSExprs,
1135 ArrayRef<const Expr *> RHSExprs,
1136 const OMPTaskDataTy &Data);
1137
1138 /// Required to resolve existing problems in the runtime. Emits threadprivate
1139 /// variables to store the size of the VLAs/array sections for
1140 /// initializer/combiner/finalizer functions + emits threadprivate variable to
1141 /// store the pointer to the original reduction item for the custom
1142 /// initializer defined by declare reduction construct.
1143 /// \param RCG Allows to reuse an existing data for the reductions.
1144 /// \param N Reduction item for which fixups must be emitted.
1145 virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
1146 ReductionCodeGen &RCG, unsigned N);
1147
1148 /// Get the address of `void *` type of the privatue copy of the reduction
1149 /// item specified by the \p SharedLVal.
1150 /// \param ReductionsPtr Pointer to the reduction data returned by the
1151 /// emitTaskReductionInit function.
1152 /// \param SharedLVal Address of the original reduction item.
1153 virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
1154 llvm::Value *ReductionsPtr,
1155 LValue SharedLVal);
1156
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001157 /// \brief Emit code for 'taskwait' directive.
1158 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +00001159
1160 /// \brief Emit code for 'cancellation point' construct.
1161 /// \param CancelRegion Region kind for which the cancellation point must be
1162 /// emitted.
1163 ///
1164 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
1165 SourceLocation Loc,
1166 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001167
1168 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +00001169 /// \param IfCond Condition in the associated 'if' clause, if it was
1170 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001171 /// \param CancelRegion Region kind for which the cancel must be emitted.
1172 ///
1173 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00001174 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001175 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00001176
1177 /// \brief Emit outilined function for 'target' directive.
1178 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001179 /// \param ParentName Name of the function that encloses the target region.
1180 /// \param OutlinedFn Outlined function value to be defined by this call.
1181 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
1182 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001183 /// \param CodeGen Code generation sequence for the \a D directive.
Simon Pilgrim6c0eeff2017-07-13 17:34:44 +00001184 /// An outlined function may not be an entry if, e.g. the if clause always
Samuel Antaoee8fb302016-01-06 13:42:12 +00001185 /// evaluates to false.
1186 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
1187 StringRef ParentName,
1188 llvm::Function *&OutlinedFn,
1189 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001190 bool IsOffloadEntry,
1191 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +00001192
1193 /// \brief Emit the target offloading code associated with \a D. The emitted
1194 /// code attempts offloading the execution to the device, an the event of
1195 /// a failure it executes the host version outlined in \a OutlinedFn.
1196 /// \param D Directive to emit.
1197 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001198 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +00001199 /// \param IfCond Expression evaluated in if clause associated with the target
1200 /// directive, or null if no if clause is used.
1201 /// \param Device Expression evaluated in device clause associated with the
1202 /// target directive, or null if no device clause is used.
1203 /// \param CapturedVars Values captured in the current region.
1204 virtual void emitTargetCall(CodeGenFunction &CGF,
1205 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +00001206 llvm::Value *OutlinedFn,
1207 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +00001208 const Expr *Device,
1209 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001210
1211 /// \brief Emit the target regions enclosed in \a GD function definition or
1212 /// the function itself in case it is a valid device function. Returns true if
1213 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001214 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001215 virtual bool emitTargetFunctions(GlobalDecl GD);
1216
1217 /// \brief Emit the global variable if it is a valid device global variable.
1218 /// Returns true if \a GD was dealt with successfully.
1219 /// \param GD Variable declaration to emit.
1220 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1221
1222 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
Simon Pilgrim2c518802017-03-30 14:13:19 +00001223 /// if it was emitted successfully.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001224 /// \param GD Global to scan.
1225 virtual bool emitTargetGlobal(GlobalDecl GD);
1226
1227 /// \brief Creates the offloading descriptor in the event any target region
1228 /// was emitted in the current module and return the function that registers
1229 /// it.
1230 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001231
1232 /// \brief Emits code for teams call of the \a OutlinedFn with
1233 /// variables captured in a record which address is stored in \a
1234 /// CapturedStruct.
1235 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1236 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1237 /// \param CapturedVars A pointer to the record with the references to
1238 /// variables used in \a OutlinedFn function.
1239 ///
1240 virtual void emitTeamsCall(CodeGenFunction &CGF,
1241 const OMPExecutableDirective &D,
1242 SourceLocation Loc, llvm::Value *OutlinedFn,
1243 ArrayRef<llvm::Value *> CapturedVars);
1244
1245 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1246 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1247 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001248 /// \param NumTeams An integer expression of teams.
1249 /// \param ThreadLimit An integer expression of threads.
1250 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1251 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001252
Samuel Antaocc10b852016-07-28 14:23:26 +00001253 /// Struct that keeps all the relevant information that should be kept
1254 /// throughout a 'target data' region.
1255 class TargetDataInfo {
1256 /// Set to true if device pointer information have to be obtained.
1257 bool RequiresDevicePointerInfo = false;
1258
1259 public:
1260 /// The array of base pointer passed to the runtime library.
1261 llvm::Value *BasePointersArray = nullptr;
1262 /// The array of section pointers passed to the runtime library.
1263 llvm::Value *PointersArray = nullptr;
1264 /// The array of sizes passed to the runtime library.
1265 llvm::Value *SizesArray = nullptr;
1266 /// The array of map types passed to the runtime library.
1267 llvm::Value *MapTypesArray = nullptr;
1268 /// The total number of pointers passed to the runtime library.
1269 unsigned NumberOfPtrs = 0u;
1270 /// Map between the a declaration of a capture and the corresponding base
1271 /// pointer address where the runtime returns the device pointers.
1272 llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
1273
1274 explicit TargetDataInfo() {}
1275 explicit TargetDataInfo(bool RequiresDevicePointerInfo)
1276 : RequiresDevicePointerInfo(RequiresDevicePointerInfo) {}
1277 /// Clear information about the data arrays.
1278 void clearArrayInfo() {
1279 BasePointersArray = nullptr;
1280 PointersArray = nullptr;
1281 SizesArray = nullptr;
1282 MapTypesArray = nullptr;
1283 NumberOfPtrs = 0u;
1284 }
1285 /// Return true if the current target data information has valid arrays.
1286 bool isValid() {
1287 return BasePointersArray && PointersArray && SizesArray &&
1288 MapTypesArray && NumberOfPtrs;
1289 }
1290 bool requiresDevicePointerInfo() { return RequiresDevicePointerInfo; }
1291 };
1292
Samuel Antaodf158d52016-04-27 22:58:19 +00001293 /// \brief Emit the target data mapping code associated with \a D.
1294 /// \param D Directive to emit.
Samuel Antaocc10b852016-07-28 14:23:26 +00001295 /// \param IfCond Expression evaluated in if clause associated with the
1296 /// target directive, or null if no device clause is used.
Samuel Antaodf158d52016-04-27 22:58:19 +00001297 /// \param Device Expression evaluated in device clause associated with the
1298 /// target directive, or null if no device clause is used.
Samuel Antaocc10b852016-07-28 14:23:26 +00001299 /// \param Info A record used to store information that needs to be preserved
1300 /// until the region is closed.
Samuel Antaodf158d52016-04-27 22:58:19 +00001301 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1302 const OMPExecutableDirective &D,
1303 const Expr *IfCond, const Expr *Device,
Samuel Antaocc10b852016-07-28 14:23:26 +00001304 const RegionCodeGenTy &CodeGen,
1305 TargetDataInfo &Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001306
Samuel Antao8d2d7302016-05-26 18:30:22 +00001307 /// \brief Emit the data mapping/movement code associated with the directive
1308 /// \a D that should be of the form 'target [{enter|exit} data | update]'.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001309 /// \param D Directive to emit.
1310 /// \param IfCond Expression evaluated in if clause associated with the target
1311 /// directive, or null if no if clause is used.
1312 /// \param Device Expression evaluated in device clause associated with the
1313 /// target directive, or null if no device clause is used.
Samuel Antao8d2d7302016-05-26 18:30:22 +00001314 virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
1315 const OMPExecutableDirective &D,
1316 const Expr *IfCond,
1317 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001318
1319 /// Marks function \a Fn with properly mangled versions of vector functions.
1320 /// \param FD Function marked as 'declare simd'.
1321 /// \param Fn LLVM function that must be marked with 'declare simd'
1322 /// attributes.
1323 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1324 llvm::Function *Fn);
Alexey Bataev8b427062016-05-25 12:36:08 +00001325
1326 /// Emit initialization for doacross loop nesting support.
1327 /// \param D Loop-based construct used in doacross nesting construct.
1328 virtual void emitDoacrossInit(CodeGenFunction &CGF,
1329 const OMPLoopDirective &D);
1330
1331 /// Emit code for doacross ordered directive with 'depend' clause.
1332 /// \param C 'depend' clause with 'sink|source' dependency kind.
1333 virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
1334 const OMPDependClause *C);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001335
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001336 /// Translates the native parameter of outlined function if this is required
1337 /// for target.
1338 /// \param FD Field decl from captured record for the paramater.
1339 /// \param NativeParam Parameter itself.
1340 virtual const VarDecl *translateParameter(const FieldDecl *FD,
1341 const VarDecl *NativeParam) const {
1342 return NativeParam;
1343 }
1344
1345 /// Gets the address of the native argument basing on the address of the
1346 /// target-specific parameter.
1347 /// \param NativeParam Parameter itself.
1348 /// \param TargetParam Corresponding target-specific parameter.
1349 virtual Address getParameterAddress(CodeGenFunction &CGF,
1350 const VarDecl *NativeParam,
1351 const VarDecl *TargetParam) const;
1352
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001353 /// Emits call of the outlined function with the provided arguments,
1354 /// translating these arguments to correct target-specific arguments.
1355 virtual void
Alexey Bataev3c595a62017-08-14 15:01:03 +00001356 emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
1357 llvm::Value *OutlinedFn,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00001358 ArrayRef<llvm::Value *> Args = llvm::None) const;
Alexey Bataev9959db52014-05-06 10:08:46 +00001359};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001360
Alexey Bataev23b69422014-06-18 07:08:49 +00001361} // namespace CodeGen
1362} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00001363
1364#endif