blob: 63616f13f80f28d5ba3a9f5ea302b44c41e099c5 [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 Bataev18095712014-10-10 12:19:54 +000040class OMPExecutableDirective;
Alexey Bataev7292c292016-04-25 12:22:29 +000041class OMPLoopDirective;
Alexey Bataev18095712014-10-10 12:19:54 +000042class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000043class OMPDeclareReductionDecl;
44class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000045
Alexey Bataev9959db52014-05-06 10:08:46 +000046namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000047class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000048class CodeGenFunction;
49class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000050
Alexey Bataev14fa1c62016-03-29 05:34:15 +000051/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
52/// region.
53class PrePostActionTy {
54public:
55 explicit PrePostActionTy() {}
56 virtual void Enter(CodeGenFunction &CGF) {}
57 virtual void Exit(CodeGenFunction &CGF) {}
58 virtual ~PrePostActionTy() {}
59};
60
61/// Class provides a way to call simple version of codegen for OpenMP region, or
62/// an advanced with possible pre|post-actions in codegen.
63class RegionCodeGenTy final {
64 intptr_t CodeGen;
65 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
66 CodeGenTy Callback;
67 mutable PrePostActionTy *PrePostAction;
68 RegionCodeGenTy() = delete;
69 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
70 template <typename Callable>
71 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
72 PrePostActionTy &Action) {
73 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
74 }
75
76public:
77 template <typename Callable>
78 RegionCodeGenTy(
79 Callable &&CodeGen,
80 typename std::enable_if<
81 !std::is_same<typename std::remove_reference<Callable>::type,
82 RegionCodeGenTy>::value>::type * = nullptr)
83 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
84 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
85 PrePostAction(nullptr) {}
86 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
87 void operator()(CodeGenFunction &CGF) const;
88};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000089
Alexey Bataev24b5bae2016-04-28 09:23:51 +000090struct OMPTaskDataTy final {
91 SmallVector<const Expr *, 4> PrivateVars;
92 SmallVector<const Expr *, 4> PrivateCopies;
93 SmallVector<const Expr *, 4> FirstprivateVars;
94 SmallVector<const Expr *, 4> FirstprivateCopies;
95 SmallVector<const Expr *, 4> FirstprivateInits;
Alexey Bataevf93095a2016-05-05 08:46:22 +000096 SmallVector<const Expr *, 4> LastprivateVars;
97 SmallVector<const Expr *, 4> LastprivateCopies;
Alexey Bataev24b5bae2016-04-28 09:23:51 +000098 SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> Dependences;
99 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
100 llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
Alexey Bataev1e1e2862016-05-10 12:21:02 +0000101 llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000102 unsigned NumberOfParts = 0;
103 bool Tied = true;
104 bool Nogroup = false;
105};
106
Alexey Bataev9959db52014-05-06 10:08:46 +0000107class CGOpenMPRuntime {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000108protected:
Alexey Bataev9959db52014-05-06 10:08:46 +0000109 CodeGenModule &CGM;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000110
111 /// \brief Creates offloading entry for the provided entry ID \a ID,
112 /// address \a Addr and size \a Size.
113 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
114 uint64_t Size);
115
116 /// \brief Helper to emit outlined function for 'target' directive.
117 /// \param D Directive to emit.
118 /// \param ParentName Name of the function that encloses the target region.
119 /// \param OutlinedFn Outlined function value to be defined by this call.
120 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
121 /// \param IsOffloadEntry True if the outlined function is an offload entry.
122 /// \param CodeGen Lambda codegen specific to an accelerator device.
123 /// An oulined function may not be an entry if, e.g. the if clause always
124 /// evaluates to false.
125 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
126 StringRef ParentName,
127 llvm::Function *&OutlinedFn,
128 llvm::Constant *&OutlinedFnID,
129 bool IsOffloadEntry,
130 const RegionCodeGenTy &CodeGen);
131
132private:
Alexey Bataev9959db52014-05-06 10:08:46 +0000133 /// \brief Default const ident_t object used for initialization of all other
134 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000135 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000136 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000137 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
138 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000139 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000140
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000141 llvm::StructType *IdentTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000142 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000143 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
144 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000145 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
146 /// Original representation is:
147 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000148 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000149 /// \brief Stores debug location and ThreadID for the function.
150 struct DebugLocThreadIdTy {
151 llvm::Value *DebugLoc;
152 llvm::Value *ThreadID;
153 };
154 /// \brief Map of local debug location, ThreadId and functions.
155 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
156 OpenMPLocThreadIDMapTy;
157 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000158 /// Map of UDRs and corresponding combiner/initializer.
159 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
160 std::pair<llvm::Function *, llvm::Function *>>
161 UDRMapTy;
162 UDRMapTy UDRMap;
163 /// Map of functions and locally defined UDRs.
164 typedef llvm::DenseMap<llvm::Function *,
165 SmallVector<const OMPDeclareReductionDecl *, 4>>
166 FunctionUDRMapTy;
167 FunctionUDRMapTy FunctionUDRMap;
168 IdentifierInfo *In = nullptr;
169 IdentifierInfo *Out = nullptr;
170 IdentifierInfo *Priv = nullptr;
171 IdentifierInfo *Orig = nullptr;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000172 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
173 /// kmp_critical_name[8];
174 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000175 /// \brief An ordered map of auto-generated variables to their unique names.
176 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
177 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
178 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
179 /// variables.
180 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
181 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000182 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000183 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000184 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000185 /// \brief Type typedef struct kmp_task {
186 /// void * shareds; /**< pointer to block of pointers to
187 /// shared vars */
188 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
189 /// executing task */
190 /// kmp_int32 part_id; /**< part id for the task */
191 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
192 /// deconstructors of firstprivate C++ objects */
193 /// } kmp_task_t;
194 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000195 /// \brief Type typedef struct kmp_depend_info {
196 /// kmp_intptr_t base_addr;
197 /// size_t len;
198 /// struct {
199 /// bool in:1;
200 /// bool out:1;
201 /// } flags;
202 /// } kmp_depend_info_t;
203 QualType KmpDependInfoTy;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000204 /// \brief Type struct __tgt_offload_entry{
205 /// void *addr; // Pointer to the offload entry info.
206 /// // (function or global)
207 /// char *name; // Name of the function or global.
208 /// size_t size; // Size of the entry info (0 if it a function).
209 /// };
210 QualType TgtOffloadEntryQTy;
211 /// struct __tgt_device_image{
212 /// void *ImageStart; // Pointer to the target code start.
213 /// void *ImageEnd; // Pointer to the target code end.
214 /// // We also add the host entries to the device image, as it may be useful
215 /// // for the target runtime to have access to that information.
216 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
217 /// // the entries.
218 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
219 /// // entries (non inclusive).
220 /// };
221 QualType TgtDeviceImageQTy;
222 /// struct __tgt_bin_desc{
223 /// int32_t NumDevices; // Number of devices supported.
224 /// __tgt_device_image *DeviceImages; // Arrays of device images
225 /// // (one per device).
226 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
227 /// // entries.
228 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
229 /// // entries (non inclusive).
230 /// };
231 QualType TgtBinaryDescriptorQTy;
232 /// \brief Entity that registers the offloading constants that were emitted so
233 /// far.
234 class OffloadEntriesInfoManagerTy {
235 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000236
Samuel Antaoee8fb302016-01-06 13:42:12 +0000237 /// \brief Number of entries registered so far.
238 unsigned OffloadingEntriesNum;
239
240 public:
241 /// \brief Base class of the entries info.
242 class OffloadEntryInfo {
243 public:
244 /// \brief Kind of a given entry. Currently, only target regions are
245 /// supported.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000246 enum OffloadingEntryInfoKinds : unsigned {
Samuel Antaoee8fb302016-01-06 13:42:12 +0000247 // Entry is a target region.
248 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
249 // Invalid entry info.
250 OFFLOAD_ENTRY_INFO_INVALID = ~0u
251 };
252
253 OffloadEntryInfo() : Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
254 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order)
255 : Order(Order), Kind(Kind) {}
256
257 bool isValid() const { return Order != ~0u; }
258 unsigned getOrder() const { return Order; }
259 OffloadingEntryInfoKinds getKind() const { return Kind; }
260 static bool classof(const OffloadEntryInfo *Info) { return true; }
261
262 protected:
263 // \brief Order this entry was emitted.
264 unsigned Order;
265
266 OffloadingEntryInfoKinds Kind;
267 };
268
269 /// \brief Return true if a there are no entries defined.
270 bool empty() const;
271 /// \brief Return number of entries defined so far.
272 unsigned size() const { return OffloadingEntriesNum; }
273 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
274 : CGM(CGM), OffloadingEntriesNum(0) {}
275
276 ///
277 /// Target region entries related.
278 ///
279 /// \brief Target region entries info.
280 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
281 // \brief Address of the entity that has to be mapped for offloading.
282 llvm::Constant *Addr;
283 // \brief Address that can be used as the ID of the entry.
284 llvm::Constant *ID;
285
286 public:
287 OffloadEntryInfoTargetRegion()
288 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u),
289 Addr(nullptr), ID(nullptr) {}
290 explicit OffloadEntryInfoTargetRegion(unsigned Order,
291 llvm::Constant *Addr,
292 llvm::Constant *ID)
293 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order),
294 Addr(Addr), ID(ID) {}
295
296 llvm::Constant *getAddress() const { return Addr; }
297 llvm::Constant *getID() const { return ID; }
298 void setAddress(llvm::Constant *V) {
299 assert(!Addr && "Address as been set before!");
300 Addr = V;
301 }
302 void setID(llvm::Constant *V) {
303 assert(!ID && "ID as been set before!");
304 ID = V;
305 }
306 static bool classof(const OffloadEntryInfo *Info) {
307 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
308 }
309 };
310 /// \brief Initialize target region entry.
311 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
312 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000313 unsigned Order);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000314 /// \brief Register target region entry.
315 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
316 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000317 llvm::Constant *Addr,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000318 llvm::Constant *ID);
319 /// \brief Return true if a target region entry with the provided
320 /// information exists.
321 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000322 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000323 /// brief Applies action \a Action on all registered entries.
324 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Samuel Antao2de62b02016-02-13 23:35:10 +0000325 OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000326 OffloadTargetRegionEntryInfoActTy;
327 void actOnTargetRegionEntriesInfo(
328 const OffloadTargetRegionEntryInfoActTy &Action);
329
330 private:
331 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000332 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000333 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000334 OffloadEntriesTargetRegionPerLine;
335 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
336 OffloadEntriesTargetRegionPerParentName;
337 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
338 OffloadEntriesTargetRegionPerFile;
339 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
340 OffloadEntriesTargetRegionPerDevice;
341 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
342 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
343 };
344 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
345
346 /// \brief Creates and registers offloading binary descriptor for the current
347 /// compilation unit. The function that does the registration is returned.
348 llvm::Function *createOffloadingBinaryDescriptorRegistration();
349
Samuel Antaoee8fb302016-01-06 13:42:12 +0000350 /// \brief Creates all the offload entries in the current compilation unit
351 /// along with the associated metadata.
352 void createOffloadEntriesAndInfoMetadata();
353
354 /// \brief Loads all the offload entries information from the host IR
355 /// metadata.
356 void loadOffloadInfoMetadata();
357
358 /// \brief Returns __tgt_offload_entry type.
359 QualType getTgtOffloadEntryQTy();
360
361 /// \brief Returns __tgt_device_image type.
362 QualType getTgtDeviceImageQTy();
363
364 /// \brief Returns __tgt_bin_desc type.
365 QualType getTgtBinaryDescriptorQTy();
366
367 /// \brief Start scanning from statement \a S and and emit all target regions
368 /// found along the way.
369 /// \param S Starting statement.
370 /// \param ParentName Name of the function declaration that is being scanned.
371 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000372
373 /// \brief Build type kmp_routine_entry_t (if not built yet).
374 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000375
Alexey Bataev9959db52014-05-06 10:08:46 +0000376 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000377 /// \param Flags Flags for OpenMP location.
378 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000379 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000380 unsigned Flags = 0);
Alexey Bataev9959db52014-05-06 10:08:46 +0000381
Alexey Bataevd74d0602014-10-13 06:02:40 +0000382 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000383 llvm::Type *getIdentTyPointerTy();
384
Alexey Bataevd74d0602014-10-13 06:02:40 +0000385 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000386 llvm::Type *getKmpc_MicroPointerTy();
387
388 /// \brief Returns specified OpenMP runtime function.
389 /// \param Function OpenMP runtime function.
390 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000391 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000392
Alexander Musman21212e42015-03-13 10:38:23 +0000393 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
394 /// size \a IVSize and sign \a IVSigned.
395 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
396
Alexander Musman92bdaab2015-03-12 13:37:50 +0000397 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
398 /// size \a IVSize and sign \a IVSigned.
399 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
400
401 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
402 /// size \a IVSize and sign \a IVSigned.
403 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
404
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000405 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
406 /// size \a IVSize and sign \a IVSigned.
407 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
408
Alexey Bataev97720002014-11-11 04:05:39 +0000409 /// \brief If the specified mangled name is not in the module, create and
410 /// return threadprivate cache object. This object is a pointer's worth of
411 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000412 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000413 /// \return Cache variable for the specified threadprivate.
414 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
415
Alexey Bataevd74d0602014-10-13 06:02:40 +0000416 /// \brief Emits address of the word in a memory where current thread id is
417 /// stored.
John McCall7f416cc2015-09-08 08:05:57 +0000418 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000419
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000420 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000421 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000422 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000423
Alexey Bataev97720002014-11-11 04:05:39 +0000424 /// \brief Gets (if variable with the given name already exist) or creates
425 /// internal global variable with the specified Name. The created variable has
426 /// linkage CommonLinkage by default and is initialized by null value.
427 /// \param Ty Type of the global variable. If it is exist already the type
428 /// must be the same.
429 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000430 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000431 const llvm::Twine &Name);
432
433 /// \brief Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000434 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000435
436 /// \brief Emits initialization code for the threadprivate variables.
437 /// \param VDAddr Address of the global variable \a VD.
438 /// \param Ctor Pointer to a global init function for \a VD.
439 /// \param CopyCtor Pointer to a global copy function for \a VD.
440 /// \param Dtor Pointer to a global destructor function for \a VD.
441 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000442 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000443 llvm::Value *Ctor, llvm::Value *CopyCtor,
444 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000445
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000446 /// \brief Returns corresponding lock object for the specified critical region
447 /// name. If the lock object does not exist it is created, otherwise the
448 /// reference to the existing copy is returned.
449 /// \param CriticalName Name of the critical region.
450 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000451 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000452
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000453 struct TaskResultTy {
454 llvm::Value *NewTask = nullptr;
455 llvm::Value *TaskEntry = nullptr;
456 llvm::Value *NewTaskNewTaskTTy = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000457 LValue TDBase;
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000458 RecordDecl *KmpTaskTQTyRD = nullptr;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000459 llvm::Value *TaskDupFn = nullptr;
Alexey Bataev7292c292016-04-25 12:22:29 +0000460 };
461 /// Emit task region for the task directive. The task region is emitted in
462 /// several steps:
463 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
464 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
465 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
466 /// function:
467 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
468 /// TaskFunction(gtid, tt->part_id, tt->shareds);
469 /// return 0;
470 /// }
471 /// 2. Copy a list of shared variables to field shareds of the resulting
472 /// structure kmp_task_t returned by the previous call (if any).
473 /// 3. Copy a pointer to destructions function to field destructions of the
474 /// resulting structure kmp_task_t.
475 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000476 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
477 /// /*part_id*/, captured_struct */*__context*/);
478 /// \param SharedsTy A type which contains references the shared variables.
479 /// \param Shareds Context with the list of shared variables from the \p
480 /// TaskFunction.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000481 /// \param Data Additional data for task generation like tiednsee, final
482 /// state, list of privates etc.
483 TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
484 const OMPExecutableDirective &D,
485 llvm::Value *TaskFunction, QualType SharedsTy,
486 Address Shareds, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000487
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000488public:
489 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000490 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000491 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000492
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000493 /// Emit code for the specified user defined reduction construct.
494 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
495 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000496 /// Get combiner/initializer for the specified user-defined reduction, if any.
497 virtual std::pair<llvm::Function *, llvm::Function *>
498 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000499 /// \brief Emits outlined function for the specified OpenMP parallel directive
500 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
501 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000502 /// \param D OpenMP directive.
503 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000504 /// \param InnermostKind Kind of innermost directive (for simple directives it
505 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000506 /// \param CodeGen Code generation sequence for the \a D directive.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000507 virtual llvm::Value *emitParallelOrTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000508 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
509 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000510
Alexey Bataev62b63b12015-03-10 07:28:44 +0000511 /// \brief Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000512 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
513 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000514 /// \param D OpenMP directive.
515 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000516 /// \param PartIDVar Variable for partition id in the current OpenMP untied
517 /// task region.
518 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000519 /// \param InnermostKind Kind of innermost directive (for simple directives it
520 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000521 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000522 /// \param Tied true if task is generated for tied task, false otherwise.
523 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
524 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000525 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000526 virtual llvm::Value *emitTaskOutlinedFunction(
527 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000528 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
529 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
530 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000531
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000532 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000533 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000534 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000535
Alexey Bataev1d677132015-04-22 13:57:31 +0000536 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
537 /// variables captured in a record which address is stored in \a
538 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000539 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000540 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000541 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000542 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000543 /// \param IfCond Condition in the associated 'if' clause, if it was
544 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000545 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000546 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
547 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000548 ArrayRef<llvm::Value *> CapturedVars,
549 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000550
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000551 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000552 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000553 /// \param CriticalOpGen Generator for the statement associated with the given
554 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000555 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000556 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000557 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000558 SourceLocation Loc,
559 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000560
Alexey Bataev8d690652014-12-04 07:23:53 +0000561 /// \brief Emits a master region.
562 /// \param MasterOpGen Generator for the statement associated with the given
563 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000564 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000565 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000566 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000567
Alexey Bataev9f797f32015-02-05 05:57:51 +0000568 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000569 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000570
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000571 /// \brief Emit a taskgroup region.
572 /// \param TaskgroupOpGen Generator for the statement associated with the
573 /// given taskgroup region.
574 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
575 const RegionCodeGenTy &TaskgroupOpGen,
576 SourceLocation Loc);
577
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000578 /// \brief Emits a single region.
579 /// \param SingleOpGen Generator for the statement associated with the given
580 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000581 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000582 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000583 SourceLocation Loc,
584 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000585 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000586 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000587 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000588
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000589 /// \brief Emit an ordered region.
590 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000591 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000592 virtual void emitOrderedRegion(CodeGenFunction &CGF,
593 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000594 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000595
Alexey Bataevf2685682015-03-30 04:30:22 +0000596 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
597 /// \param Kind Directive for which this implicit barrier call must be
598 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000599 /// \param EmitChecks true if need to emit checks for cancellation barriers.
600 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
601 /// runtime class decides which one to emit (simple or with cancellation
602 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000603 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000604 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000605 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000606 bool EmitChecks = true,
607 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000608
Alexander Musmanc6388682014-12-15 07:07:06 +0000609 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
610 /// This kind of worksharing directive is emitted without outer loop.
611 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
612 /// \param Chunked True if chunk is specified in the clause.
613 ///
614 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
615 bool Chunked) const;
616
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000617 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
618 /// This kind of distribute directive is emitted without outer loop.
619 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
620 /// \param Chunked True if chunk is specified in the clause.
621 ///
622 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
623 bool Chunked) const;
624
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000625 /// \brief Check if the specified \a ScheduleKind is dynamic.
626 /// This kind of worksharing directive is emitted without outer loop.
627 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
628 ///
629 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
630
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000631 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000632 const OpenMPScheduleTy &ScheduleKind,
633 unsigned IVSize, bool IVSigned, bool Ordered,
634 llvm::Value *UB,
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000635 llvm::Value *Chunk = nullptr);
636
Alexander Musmanc6388682014-12-15 07:07:06 +0000637 /// \brief Call the appropriate runtime routine to initialize it before start
638 /// of loop.
639 ///
640 /// Depending on the loop schedule, it is nesessary to call some runtime
641 /// routine before start of the OpenMP loop to get the loop upper / lower
642 /// bounds \a LB and \a UB and stride \a ST.
643 ///
644 /// \param CGF Reference to current CodeGenFunction.
645 /// \param Loc Clang source location.
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000646 /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000647 /// \param IVSize Size of the iteration variable in bits.
648 /// \param IVSigned Sign of the interation variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000649 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000650 /// \param IL Address of the output variable in which the flag of the
651 /// last iteration is returned.
652 /// \param LB Address of the output variable in which the lower iteration
653 /// number is returned.
654 /// \param UB Address of the output variable in which the upper iteration
655 /// number is returned.
656 /// \param ST Address of the output variable in which the stride value is
657 /// returned nesessary to generated the static_chunked scheduled loop.
658 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
659 /// For the default (nullptr) value, the chunk 1 will be used.
660 ///
John McCall7f416cc2015-09-08 08:05:57 +0000661 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000662 const OpenMPScheduleTy &ScheduleKind,
John McCall7f416cc2015-09-08 08:05:57 +0000663 unsigned IVSize, bool IVSigned, bool Ordered,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000664 Address IL, Address LB, Address UB, Address ST,
John McCall7f416cc2015-09-08 08:05:57 +0000665 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000666
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000667 ///
668 /// \param CGF Reference to current CodeGenFunction.
669 /// \param Loc Clang source location.
670 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
671 /// \param IVSize Size of the iteration variable in bits.
672 /// \param IVSigned Sign of the interation variable.
673 /// \param Ordered true if loop is ordered, false otherwise.
674 /// \param IL Address of the output variable in which the flag of the
675 /// last iteration is returned.
676 /// \param LB Address of the output variable in which the lower iteration
677 /// number is returned.
678 /// \param UB Address of the output variable in which the upper iteration
679 /// number is returned.
680 /// \param ST Address of the output variable in which the stride value is
681 /// returned nesessary to generated the static_chunked scheduled loop.
682 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
683 /// For the default (nullptr) value, the chunk 1 will be used.
684 ///
685 virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
686 OpenMPDistScheduleClauseKind SchedKind,
687 unsigned IVSize, bool IVSigned,
688 bool Ordered, Address IL, Address LB,
689 Address UB, Address ST,
690 llvm::Value *Chunk = nullptr);
691
Alexander Musmanc6388682014-12-15 07:07:06 +0000692 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000693 /// iteration of the ordered loop with the dynamic scheduling.
694 ///
695 /// \param CGF Reference to current CodeGenFunction.
696 /// \param Loc Clang source location.
697 /// \param IVSize Size of the iteration variable in bits.
698 /// \param IVSigned Sign of the interation variable.
699 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000700 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
701 SourceLocation Loc, unsigned IVSize,
702 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000703
704 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000705 /// all the work with current loop.
706 ///
707 /// \param CGF Reference to current CodeGenFunction.
708 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000709 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000710 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000711
Alexander Musman92bdaab2015-03-12 13:37:50 +0000712 /// Call __kmpc_dispatch_next(
713 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
714 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
715 /// kmp_int[32|64] *p_stride);
716 /// \param IVSize Size of the iteration variable in bits.
717 /// \param IVSigned Sign of the interation variable.
718 /// \param IL Address of the output variable in which the flag of the
719 /// last iteration is returned.
720 /// \param LB Address of the output variable in which the lower iteration
721 /// number is returned.
722 /// \param UB Address of the output variable in which the upper iteration
723 /// number is returned.
724 /// \param ST Address of the output variable in which the stride value is
725 /// returned.
726 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
727 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000728 Address IL, Address LB,
729 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000730
Alexey Bataevb2059782014-10-13 08:23:51 +0000731 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
732 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
733 /// clause.
734 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000735 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
736 llvm::Value *NumThreads,
737 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000738
Alexey Bataev7f210c62015-06-18 13:40:03 +0000739 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
740 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
741 virtual void emitProcBindClause(CodeGenFunction &CGF,
742 OpenMPProcBindClauseKind ProcBind,
743 SourceLocation Loc);
744
Alexey Bataev97720002014-11-11 04:05:39 +0000745 /// \brief Returns address of the threadprivate variable for the current
746 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000747 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000748 /// \param VDAddr Address of the global variable \a VD.
749 /// \param Loc Location of the reference to threadprivate var.
750 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000751 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
752 const VarDecl *VD,
753 Address VDAddr,
754 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000755
756 /// \brief Emit a code for initialization of threadprivate variable. It emits
757 /// a call to runtime library which adds initial value to the newly created
758 /// threadprivate variable (if it is not constant) and registers destructor
759 /// for the variable (if any).
760 /// \param VD Threadprivate variable.
761 /// \param VDAddr Address of the global variable \a VD.
762 /// \param Loc Location of threadprivate declaration.
763 /// \param PerformInit true if initialization expression is not constant.
764 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000765 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000766 SourceLocation Loc, bool PerformInit,
767 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000768
769 /// \brief Emit flush of the variables specified in 'omp flush' directive.
770 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000771 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
772 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000773
774 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000775 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000776 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
777 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
778 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
779 /// function:
780 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
781 /// TaskFunction(gtid, tt->part_id, tt->shareds);
782 /// return 0;
783 /// }
784 /// 2. Copy a list of shared variables to field shareds of the resulting
785 /// structure kmp_task_t returned by the previous call (if any).
786 /// 3. Copy a pointer to destructions function to field destructions of the
787 /// resulting structure kmp_task_t.
788 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
789 /// kmp_task_t *new_task), where new_task is a resulting structure from
790 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000791 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000792 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
793 /// /*part_id*/, captured_struct */*__context*/);
794 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000795 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000796 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000797 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
798 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000799 /// \param Data Additional data for task generation like tiednsee, final
800 /// state, list of privates etc.
801 virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
802 const OMPExecutableDirective &D,
803 llvm::Value *TaskFunction, QualType SharedsTy,
804 Address Shareds, const Expr *IfCond,
805 const OMPTaskDataTy &Data);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000806
Alexey Bataev7292c292016-04-25 12:22:29 +0000807 /// Emit task region for the taskloop directive. The taskloop region is
808 /// emitted in several steps:
809 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
810 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
811 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
812 /// function:
813 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
814 /// TaskFunction(gtid, tt->part_id, tt->shareds);
815 /// return 0;
816 /// }
817 /// 2. Copy a list of shared variables to field shareds of the resulting
818 /// structure kmp_task_t returned by the previous call (if any).
819 /// 3. Copy a pointer to destructions function to field destructions of the
820 /// resulting structure kmp_task_t.
821 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
822 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
823 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
824 /// is a resulting structure from
825 /// previous items.
826 /// \param D Current task directive.
Alexey Bataev7292c292016-04-25 12:22:29 +0000827 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
828 /// /*part_id*/, captured_struct */*__context*/);
829 /// \param SharedsTy A type which contains references the shared variables.
830 /// \param Shareds Context with the list of shared variables from the \p
831 /// TaskFunction.
832 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
833 /// otherwise.
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000834 /// \param Data Additional data for task generation like tiednsee, final
835 /// state, list of privates etc.
Alexey Bataev7292c292016-04-25 12:22:29 +0000836 virtual void emitTaskLoopCall(
837 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
Alexey Bataev24b5bae2016-04-28 09:23:51 +0000838 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
839 const Expr *IfCond, const OMPTaskDataTy &Data);
Alexey Bataev7292c292016-04-25 12:22:29 +0000840
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000841 /// \brief Emit code for the directive that does not require outlining.
842 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000843 /// \param InnermostKind Kind of innermost directive (for simple directives it
844 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000845 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000846 /// \param HasCancel true if region has inner cancel directive, false
847 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000848 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000849 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000850 const RegionCodeGenTy &CodeGen,
851 bool HasCancel = false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000852 /// \brief Emit a code for reduction clause. Next code should be emitted for
853 /// reduction:
854 /// \code
855 ///
856 /// static kmp_critical_name lock = { 0 };
857 ///
858 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
859 /// ...
860 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
861 /// ...
862 /// }
863 ///
864 /// ...
865 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
866 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
867 /// RedList, reduce_func, &<lock>)) {
868 /// case 1:
869 /// ...
870 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
871 /// ...
872 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
873 /// break;
874 /// case 2:
875 /// ...
876 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
877 /// ...
878 /// break;
879 /// default:;
880 /// }
881 /// \endcode
882 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000883 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000884 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
885 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
886 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
887 /// or 'operator binop(LHS, RHS)'.
888 /// \param WithNowait true if parent directive has also nowait clause, false
889 /// otherwise.
890 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000891 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000892 ArrayRef<const Expr *> LHSExprs,
893 ArrayRef<const Expr *> RHSExprs,
894 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000895 bool WithNowait, bool SimpleReduction);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000896
897 /// \brief Emit code for 'taskwait' directive.
898 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +0000899
900 /// \brief Emit code for 'cancellation point' construct.
901 /// \param CancelRegion Region kind for which the cancellation point must be
902 /// emitted.
903 ///
904 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
905 SourceLocation Loc,
906 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000907
908 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +0000909 /// \param IfCond Condition in the associated 'if' clause, if it was
910 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000911 /// \param CancelRegion Region kind for which the cancel must be emitted.
912 ///
913 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +0000914 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000915 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +0000916
917 /// \brief Emit outilined function for 'target' directive.
918 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000919 /// \param ParentName Name of the function that encloses the target region.
920 /// \param OutlinedFn Outlined function value to be defined by this call.
921 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
922 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000923 /// \param CodeGen Code generation sequence for the \a D directive.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000924 /// An oulined function may not be an entry if, e.g. the if clause always
925 /// evaluates to false.
926 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
927 StringRef ParentName,
928 llvm::Function *&OutlinedFn,
929 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000930 bool IsOffloadEntry,
931 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +0000932
933 /// \brief Emit the target offloading code associated with \a D. The emitted
934 /// code attempts offloading the execution to the device, an the event of
935 /// a failure it executes the host version outlined in \a OutlinedFn.
936 /// \param D Directive to emit.
937 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000938 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +0000939 /// \param IfCond Expression evaluated in if clause associated with the target
940 /// directive, or null if no if clause is used.
941 /// \param Device Expression evaluated in device clause associated with the
942 /// target directive, or null if no device clause is used.
943 /// \param CapturedVars Values captured in the current region.
944 virtual void emitTargetCall(CodeGenFunction &CGF,
945 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000946 llvm::Value *OutlinedFn,
947 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +0000948 const Expr *Device,
949 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000950
951 /// \brief Emit the target regions enclosed in \a GD function definition or
952 /// the function itself in case it is a valid device function. Returns true if
953 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +0000954 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000955 virtual bool emitTargetFunctions(GlobalDecl GD);
956
957 /// \brief Emit the global variable if it is a valid device global variable.
958 /// Returns true if \a GD was dealt with successfully.
959 /// \param GD Variable declaration to emit.
960 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
961
962 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
963 /// if it was emitted succesfully.
964 /// \param GD Global to scan.
965 virtual bool emitTargetGlobal(GlobalDecl GD);
966
967 /// \brief Creates the offloading descriptor in the event any target region
968 /// was emitted in the current module and return the function that registers
969 /// it.
970 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000971
972 /// \brief Emits code for teams call of the \a OutlinedFn with
973 /// variables captured in a record which address is stored in \a
974 /// CapturedStruct.
975 /// \param OutlinedFn Outlined function to be run by team masters. Type of
976 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
977 /// \param CapturedVars A pointer to the record with the references to
978 /// variables used in \a OutlinedFn function.
979 ///
980 virtual void emitTeamsCall(CodeGenFunction &CGF,
981 const OMPExecutableDirective &D,
982 SourceLocation Loc, llvm::Value *OutlinedFn,
983 ArrayRef<llvm::Value *> CapturedVars);
984
985 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
986 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
987 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +0000988 /// \param NumTeams An integer expression of teams.
989 /// \param ThreadLimit An integer expression of threads.
990 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
991 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +0000992
993 /// \brief Emit the target data mapping code associated with \a D.
994 /// \param D Directive to emit.
995 /// \param IfCond Expression evaluated in if clause associated with the target
996 /// directive, or null if no if clause is used.
997 /// \param Device Expression evaluated in device clause associated with the
998 /// target directive, or null if no device clause is used.
NAKAMURA Takumi01d07db2016-04-28 02:45:21 +0000999 /// \param CodeGen Function that emits the enclosed region.
Samuel Antaodf158d52016-04-27 22:58:19 +00001000 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1001 const OMPExecutableDirective &D,
1002 const Expr *IfCond, const Expr *Device,
1003 const RegionCodeGenTy &CodeGen);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001004
Samuel Antao8dd66282016-04-27 23:14:30 +00001005 /// \brief Emit the target enter or exit data mapping code associated with
1006 /// directive \a D.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001007 /// \param D Directive to emit.
1008 /// \param IfCond Expression evaluated in if clause associated with the target
1009 /// directive, or null if no if clause is used.
1010 /// \param Device Expression evaluated in device clause associated with the
1011 /// target directive, or null if no device clause is used.
Samuel Antao8dd66282016-04-27 23:14:30 +00001012 virtual void emitTargetEnterOrExitDataCall(CodeGenFunction &CGF,
1013 const OMPExecutableDirective &D,
1014 const Expr *IfCond,
1015 const Expr *Device);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00001016
1017 /// Marks function \a Fn with properly mangled versions of vector functions.
1018 /// \param FD Function marked as 'declare simd'.
1019 /// \param Fn LLVM function that must be marked with 'declare simd'
1020 /// attributes.
1021 virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
1022 llvm::Function *Fn);
Alexey Bataev9959db52014-05-06 10:08:46 +00001023};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001024
Alexey Bataev23b69422014-06-18 07:08:49 +00001025} // namespace CodeGen
1026} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00001027
1028#endif