blob: 68fe1ccc981a83fd7609cb2cdccdab91a30ae582 [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 Bataev9959db52014-05-06 10:08:46 +000090class CGOpenMPRuntime {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000091protected:
Alexey Bataev9959db52014-05-06 10:08:46 +000092 CodeGenModule &CGM;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000093
94 /// \brief Creates offloading entry for the provided entry ID \a ID,
95 /// address \a Addr and size \a Size.
96 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
97 uint64_t Size);
98
99 /// \brief Helper to emit outlined function for 'target' directive.
100 /// \param D Directive to emit.
101 /// \param ParentName Name of the function that encloses the target region.
102 /// \param OutlinedFn Outlined function value to be defined by this call.
103 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
104 /// \param IsOffloadEntry True if the outlined function is an offload entry.
105 /// \param CodeGen Lambda codegen specific to an accelerator device.
106 /// An oulined function may not be an entry if, e.g. the if clause always
107 /// evaluates to false.
108 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
109 StringRef ParentName,
110 llvm::Function *&OutlinedFn,
111 llvm::Constant *&OutlinedFnID,
112 bool IsOffloadEntry,
113 const RegionCodeGenTy &CodeGen);
114
115private:
Alexey Bataev9959db52014-05-06 10:08:46 +0000116 /// \brief Default const ident_t object used for initialization of all other
117 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000118 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000119 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000120 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
121 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000122 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000123
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000124 llvm::StructType *IdentTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000125 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000126 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
127 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000128 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
129 /// Original representation is:
130 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000131 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000132 /// \brief Stores debug location and ThreadID for the function.
133 struct DebugLocThreadIdTy {
134 llvm::Value *DebugLoc;
135 llvm::Value *ThreadID;
136 };
137 /// \brief Map of local debug location, ThreadId and functions.
138 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
139 OpenMPLocThreadIDMapTy;
140 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000141 /// Map of UDRs and corresponding combiner/initializer.
142 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
143 std::pair<llvm::Function *, llvm::Function *>>
144 UDRMapTy;
145 UDRMapTy UDRMap;
146 /// Map of functions and locally defined UDRs.
147 typedef llvm::DenseMap<llvm::Function *,
148 SmallVector<const OMPDeclareReductionDecl *, 4>>
149 FunctionUDRMapTy;
150 FunctionUDRMapTy FunctionUDRMap;
151 IdentifierInfo *In = nullptr;
152 IdentifierInfo *Out = nullptr;
153 IdentifierInfo *Priv = nullptr;
154 IdentifierInfo *Orig = nullptr;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000155 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
156 /// kmp_critical_name[8];
157 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000158 /// \brief An ordered map of auto-generated variables to their unique names.
159 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
160 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
161 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
162 /// variables.
163 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
164 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000165 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000166 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000167 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000168 /// \brief Type typedef struct kmp_task {
169 /// void * shareds; /**< pointer to block of pointers to
170 /// shared vars */
171 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
172 /// executing task */
173 /// kmp_int32 part_id; /**< part id for the task */
174 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
175 /// deconstructors of firstprivate C++ objects */
176 /// } kmp_task_t;
177 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000178 /// \brief Type typedef struct kmp_depend_info {
179 /// kmp_intptr_t base_addr;
180 /// size_t len;
181 /// struct {
182 /// bool in:1;
183 /// bool out:1;
184 /// } flags;
185 /// } kmp_depend_info_t;
186 QualType KmpDependInfoTy;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000187 /// \brief Type struct __tgt_offload_entry{
188 /// void *addr; // Pointer to the offload entry info.
189 /// // (function or global)
190 /// char *name; // Name of the function or global.
191 /// size_t size; // Size of the entry info (0 if it a function).
192 /// };
193 QualType TgtOffloadEntryQTy;
194 /// struct __tgt_device_image{
195 /// void *ImageStart; // Pointer to the target code start.
196 /// void *ImageEnd; // Pointer to the target code end.
197 /// // We also add the host entries to the device image, as it may be useful
198 /// // for the target runtime to have access to that information.
199 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
200 /// // the entries.
201 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
202 /// // entries (non inclusive).
203 /// };
204 QualType TgtDeviceImageQTy;
205 /// struct __tgt_bin_desc{
206 /// int32_t NumDevices; // Number of devices supported.
207 /// __tgt_device_image *DeviceImages; // Arrays of device images
208 /// // (one per device).
209 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
210 /// // entries.
211 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
212 /// // entries (non inclusive).
213 /// };
214 QualType TgtBinaryDescriptorQTy;
215 /// \brief Entity that registers the offloading constants that were emitted so
216 /// far.
217 class OffloadEntriesInfoManagerTy {
218 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000219
Samuel Antaoee8fb302016-01-06 13:42:12 +0000220 /// \brief Number of entries registered so far.
221 unsigned OffloadingEntriesNum;
222
223 public:
224 /// \brief Base class of the entries info.
225 class OffloadEntryInfo {
226 public:
227 /// \brief Kind of a given entry. Currently, only target regions are
228 /// supported.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000229 enum OffloadingEntryInfoKinds : unsigned {
Samuel Antaoee8fb302016-01-06 13:42:12 +0000230 // Entry is a target region.
231 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
232 // Invalid entry info.
233 OFFLOAD_ENTRY_INFO_INVALID = ~0u
234 };
235
236 OffloadEntryInfo() : Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
237 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order)
238 : Order(Order), Kind(Kind) {}
239
240 bool isValid() const { return Order != ~0u; }
241 unsigned getOrder() const { return Order; }
242 OffloadingEntryInfoKinds getKind() const { return Kind; }
243 static bool classof(const OffloadEntryInfo *Info) { return true; }
244
245 protected:
246 // \brief Order this entry was emitted.
247 unsigned Order;
248
249 OffloadingEntryInfoKinds Kind;
250 };
251
252 /// \brief Return true if a there are no entries defined.
253 bool empty() const;
254 /// \brief Return number of entries defined so far.
255 unsigned size() const { return OffloadingEntriesNum; }
256 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
257 : CGM(CGM), OffloadingEntriesNum(0) {}
258
259 ///
260 /// Target region entries related.
261 ///
262 /// \brief Target region entries info.
263 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
264 // \brief Address of the entity that has to be mapped for offloading.
265 llvm::Constant *Addr;
266 // \brief Address that can be used as the ID of the entry.
267 llvm::Constant *ID;
268
269 public:
270 OffloadEntryInfoTargetRegion()
271 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u),
272 Addr(nullptr), ID(nullptr) {}
273 explicit OffloadEntryInfoTargetRegion(unsigned Order,
274 llvm::Constant *Addr,
275 llvm::Constant *ID)
276 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order),
277 Addr(Addr), ID(ID) {}
278
279 llvm::Constant *getAddress() const { return Addr; }
280 llvm::Constant *getID() const { return ID; }
281 void setAddress(llvm::Constant *V) {
282 assert(!Addr && "Address as been set before!");
283 Addr = V;
284 }
285 void setID(llvm::Constant *V) {
286 assert(!ID && "ID as been set before!");
287 ID = V;
288 }
289 static bool classof(const OffloadEntryInfo *Info) {
290 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
291 }
292 };
293 /// \brief Initialize target region entry.
294 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
295 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000296 unsigned Order);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000297 /// \brief Register target region entry.
298 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
299 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000300 llvm::Constant *Addr,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000301 llvm::Constant *ID);
302 /// \brief Return true if a target region entry with the provided
303 /// information exists.
304 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000305 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000306 /// brief Applies action \a Action on all registered entries.
307 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Samuel Antao2de62b02016-02-13 23:35:10 +0000308 OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000309 OffloadTargetRegionEntryInfoActTy;
310 void actOnTargetRegionEntriesInfo(
311 const OffloadTargetRegionEntryInfoActTy &Action);
312
313 private:
314 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000315 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000316 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000317 OffloadEntriesTargetRegionPerLine;
318 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
319 OffloadEntriesTargetRegionPerParentName;
320 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
321 OffloadEntriesTargetRegionPerFile;
322 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
323 OffloadEntriesTargetRegionPerDevice;
324 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
325 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
326 };
327 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
328
329 /// \brief Creates and registers offloading binary descriptor for the current
330 /// compilation unit. The function that does the registration is returned.
331 llvm::Function *createOffloadingBinaryDescriptorRegistration();
332
Samuel Antaoee8fb302016-01-06 13:42:12 +0000333 /// \brief Creates all the offload entries in the current compilation unit
334 /// along with the associated metadata.
335 void createOffloadEntriesAndInfoMetadata();
336
337 /// \brief Loads all the offload entries information from the host IR
338 /// metadata.
339 void loadOffloadInfoMetadata();
340
341 /// \brief Returns __tgt_offload_entry type.
342 QualType getTgtOffloadEntryQTy();
343
344 /// \brief Returns __tgt_device_image type.
345 QualType getTgtDeviceImageQTy();
346
347 /// \brief Returns __tgt_bin_desc type.
348 QualType getTgtBinaryDescriptorQTy();
349
350 /// \brief Start scanning from statement \a S and and emit all target regions
351 /// found along the way.
352 /// \param S Starting statement.
353 /// \param ParentName Name of the function declaration that is being scanned.
354 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000355
356 /// \brief Build type kmp_routine_entry_t (if not built yet).
357 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000358
Alexey Bataev9959db52014-05-06 10:08:46 +0000359 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000360 /// \param Flags Flags for OpenMP location.
361 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000362 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000363 unsigned Flags = 0);
Alexey Bataev9959db52014-05-06 10:08:46 +0000364
Alexey Bataevd74d0602014-10-13 06:02:40 +0000365 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000366 llvm::Type *getIdentTyPointerTy();
367
Alexey Bataevd74d0602014-10-13 06:02:40 +0000368 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000369 llvm::Type *getKmpc_MicroPointerTy();
370
371 /// \brief Returns specified OpenMP runtime function.
372 /// \param Function OpenMP runtime function.
373 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000374 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000375
Alexander Musman21212e42015-03-13 10:38:23 +0000376 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
377 /// size \a IVSize and sign \a IVSigned.
378 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
379
Alexander Musman92bdaab2015-03-12 13:37:50 +0000380 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
381 /// size \a IVSize and sign \a IVSigned.
382 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
383
384 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
385 /// size \a IVSize and sign \a IVSigned.
386 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
387
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000388 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
389 /// size \a IVSize and sign \a IVSigned.
390 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
391
Alexey Bataev97720002014-11-11 04:05:39 +0000392 /// \brief If the specified mangled name is not in the module, create and
393 /// return threadprivate cache object. This object is a pointer's worth of
394 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000395 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000396 /// \return Cache variable for the specified threadprivate.
397 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
398
Alexey Bataevd74d0602014-10-13 06:02:40 +0000399 /// \brief Emits address of the word in a memory where current thread id is
400 /// stored.
John McCall7f416cc2015-09-08 08:05:57 +0000401 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000402
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000403 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000404 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000405 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000406
Alexey Bataev97720002014-11-11 04:05:39 +0000407 /// \brief Gets (if variable with the given name already exist) or creates
408 /// internal global variable with the specified Name. The created variable has
409 /// linkage CommonLinkage by default and is initialized by null value.
410 /// \param Ty Type of the global variable. If it is exist already the type
411 /// must be the same.
412 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000413 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000414 const llvm::Twine &Name);
415
416 /// \brief Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000417 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000418
419 /// \brief Emits initialization code for the threadprivate variables.
420 /// \param VDAddr Address of the global variable \a VD.
421 /// \param Ctor Pointer to a global init function for \a VD.
422 /// \param CopyCtor Pointer to a global copy function for \a VD.
423 /// \param Dtor Pointer to a global destructor function for \a VD.
424 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000425 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000426 llvm::Value *Ctor, llvm::Value *CopyCtor,
427 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000428
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000429 /// \brief Returns corresponding lock object for the specified critical region
430 /// name. If the lock object does not exist it is created, otherwise the
431 /// reference to the existing copy is returned.
432 /// \param CriticalName Name of the critical region.
433 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000434 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000435
Alexey Bataev7292c292016-04-25 12:22:29 +0000436 struct TaskDataTy {
437 llvm::Value *NewTask;
438 llvm::Value *TaskEntry;
439 llvm::Value *NewTaskNewTaskTTy;
440 LValue TDBase;
441 RecordDecl *KmpTaskTQTyRD;
442 };
443 /// Emit task region for the task directive. The task region is emitted in
444 /// several steps:
445 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
446 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
447 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
448 /// function:
449 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
450 /// TaskFunction(gtid, tt->part_id, tt->shareds);
451 /// return 0;
452 /// }
453 /// 2. Copy a list of shared variables to field shareds of the resulting
454 /// structure kmp_task_t returned by the previous call (if any).
455 /// 3. Copy a pointer to destructions function to field destructions of the
456 /// resulting structure kmp_task_t.
457 /// \param D Current task directive.
458 /// \param Tied true if the task is tied (the task is tied to the thread that
459 /// can suspend its task region), false - untied (the task is not tied to any
460 /// thread).
461 /// \param Final Contains either constant bool value, or llvm::Value * of i1
462 /// type for final clause. If the value is true, the task forces all of its
463 /// child tasks to become final and included tasks.
464 /// \param NumberOfParts Number of parts in untied tasks.
465 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
466 /// /*part_id*/, captured_struct */*__context*/);
467 /// \param SharedsTy A type which contains references the shared variables.
468 /// \param Shareds Context with the list of shared variables from the \p
469 /// TaskFunction.
Alexey Bataev7292c292016-04-25 12:22:29 +0000470 /// \param PrivateVars List of references to private variables for the task
471 /// directive.
472 /// \param PrivateCopies List of private copies for each private variable in
473 /// \p PrivateVars.
474 /// \param FirstprivateVars List of references to private variables for the
475 /// task directive.
476 /// \param FirstprivateCopies List of private copies for each private variable
477 /// in \p FirstprivateVars.
478 /// \param FirstprivateInits List of references to auto generated variables
479 /// used for initialization of a single array element. Used if firstprivate
480 /// variable is of array type.
481 TaskDataTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
482 const OMPExecutableDirective &D, bool Tied,
483 llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
484 unsigned NumberOfParts, llvm::Value *TaskFunction,
485 QualType SharedsTy, Address Shareds,
486 ArrayRef<const Expr *> PrivateVars,
487 ArrayRef<const Expr *> PrivateCopies,
488 ArrayRef<const Expr *> FirstprivateVars,
489 ArrayRef<const Expr *> FirstprivateCopies,
490 ArrayRef<const Expr *> FirstprivateInits);
491
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000492public:
493 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000494 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000495 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000496
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000497 /// Emit code for the specified user defined reduction construct.
498 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
499 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000500 /// Get combiner/initializer for the specified user-defined reduction, if any.
501 virtual std::pair<llvm::Function *, llvm::Function *>
502 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000503 /// \brief Emits outlined function for the specified OpenMP parallel directive
504 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
505 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000506 /// \param D OpenMP directive.
507 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000508 /// \param InnermostKind Kind of innermost directive (for simple directives it
509 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000510 /// \param CodeGen Code generation sequence for the \a D directive.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000511 virtual llvm::Value *emitParallelOrTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000512 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
513 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000514
Alexey Bataev62b63b12015-03-10 07:28:44 +0000515 /// \brief Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000516 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
517 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000518 /// \param D OpenMP directive.
519 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000520 /// \param PartIDVar Variable for partition id in the current OpenMP untied
521 /// task region.
522 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000523 /// \param InnermostKind Kind of innermost directive (for simple directives it
524 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000525 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000526 /// \param Tied true if task is generated for tied task, false otherwise.
527 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
528 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000529 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000530 virtual llvm::Value *emitTaskOutlinedFunction(
531 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000532 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
533 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
534 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000535
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000536 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000537 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000538 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000539
Alexey Bataev1d677132015-04-22 13:57:31 +0000540 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
541 /// variables captured in a record which address is stored in \a
542 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000543 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000544 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000545 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000546 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000547 /// \param IfCond Condition in the associated 'if' clause, if it was
548 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000549 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000550 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
551 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000552 ArrayRef<llvm::Value *> CapturedVars,
553 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000554
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000555 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000556 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000557 /// \param CriticalOpGen Generator for the statement associated with the given
558 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000559 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000560 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000561 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000562 SourceLocation Loc,
563 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000564
Alexey Bataev8d690652014-12-04 07:23:53 +0000565 /// \brief Emits a master region.
566 /// \param MasterOpGen Generator for the statement associated with the given
567 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000568 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000569 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000570 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000571
Alexey Bataev9f797f32015-02-05 05:57:51 +0000572 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000573 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000574
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000575 /// \brief Emit a taskgroup region.
576 /// \param TaskgroupOpGen Generator for the statement associated with the
577 /// given taskgroup region.
578 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
579 const RegionCodeGenTy &TaskgroupOpGen,
580 SourceLocation Loc);
581
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000582 /// \brief Emits a single region.
583 /// \param SingleOpGen Generator for the statement associated with the given
584 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000585 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000586 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000587 SourceLocation Loc,
588 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000589 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000590 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000591 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000592
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000593 /// \brief Emit an ordered region.
594 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000595 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000596 virtual void emitOrderedRegion(CodeGenFunction &CGF,
597 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000598 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000599
Alexey Bataevf2685682015-03-30 04:30:22 +0000600 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
601 /// \param Kind Directive for which this implicit barrier call must be
602 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000603 /// \param EmitChecks true if need to emit checks for cancellation barriers.
604 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
605 /// runtime class decides which one to emit (simple or with cancellation
606 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000607 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000608 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000609 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000610 bool EmitChecks = true,
611 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000612
Alexander Musmanc6388682014-12-15 07:07:06 +0000613 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
614 /// This kind of worksharing directive is emitted without outer loop.
615 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
616 /// \param Chunked True if chunk is specified in the clause.
617 ///
618 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
619 bool Chunked) const;
620
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000621 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
622 /// This kind of distribute directive is emitted without outer loop.
623 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
624 /// \param Chunked True if chunk is specified in the clause.
625 ///
626 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
627 bool Chunked) const;
628
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000629 /// \brief Check if the specified \a ScheduleKind is dynamic.
630 /// This kind of worksharing directive is emitted without outer loop.
631 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
632 ///
633 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
634
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000635 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
636 OpenMPScheduleClauseKind SchedKind,
637 unsigned IVSize, bool IVSigned,
638 bool Ordered, llvm::Value *UB,
639 llvm::Value *Chunk = nullptr);
640
Alexander Musmanc6388682014-12-15 07:07:06 +0000641 /// \brief Call the appropriate runtime routine to initialize it before start
642 /// of loop.
643 ///
644 /// Depending on the loop schedule, it is nesessary to call some runtime
645 /// routine before start of the OpenMP loop to get the loop upper / lower
646 /// bounds \a LB and \a UB and stride \a ST.
647 ///
648 /// \param CGF Reference to current CodeGenFunction.
649 /// \param Loc Clang source location.
NAKAMURA Takumieca08382014-12-17 14:47:06 +0000650 /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000651 /// \param IVSize Size of the iteration variable in bits.
652 /// \param IVSigned Sign of the interation variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000653 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000654 /// \param IL Address of the output variable in which the flag of the
655 /// last iteration is returned.
656 /// \param LB Address of the output variable in which the lower iteration
657 /// number is returned.
658 /// \param UB Address of the output variable in which the upper iteration
659 /// number is returned.
660 /// \param ST Address of the output variable in which the stride value is
661 /// returned nesessary to generated the static_chunked scheduled loop.
662 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
663 /// For the default (nullptr) value, the chunk 1 will be used.
664 ///
John McCall7f416cc2015-09-08 08:05:57 +0000665 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
666 OpenMPScheduleClauseKind SchedKind,
667 unsigned IVSize, bool IVSigned, bool Ordered,
668 Address IL, Address LB,
669 Address UB, Address ST,
670 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000671
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000672 ///
673 /// \param CGF Reference to current CodeGenFunction.
674 /// \param Loc Clang source location.
675 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
676 /// \param IVSize Size of the iteration variable in bits.
677 /// \param IVSigned Sign of the interation variable.
678 /// \param Ordered true if loop is ordered, false otherwise.
679 /// \param IL Address of the output variable in which the flag of the
680 /// last iteration is returned.
681 /// \param LB Address of the output variable in which the lower iteration
682 /// number is returned.
683 /// \param UB Address of the output variable in which the upper iteration
684 /// number is returned.
685 /// \param ST Address of the output variable in which the stride value is
686 /// returned nesessary to generated the static_chunked scheduled loop.
687 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
688 /// For the default (nullptr) value, the chunk 1 will be used.
689 ///
690 virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
691 OpenMPDistScheduleClauseKind SchedKind,
692 unsigned IVSize, bool IVSigned,
693 bool Ordered, Address IL, Address LB,
694 Address UB, Address ST,
695 llvm::Value *Chunk = nullptr);
696
Alexander Musmanc6388682014-12-15 07:07:06 +0000697 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000698 /// iteration of the ordered loop with the dynamic scheduling.
699 ///
700 /// \param CGF Reference to current CodeGenFunction.
701 /// \param Loc Clang source location.
702 /// \param IVSize Size of the iteration variable in bits.
703 /// \param IVSigned Sign of the interation variable.
704 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000705 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
706 SourceLocation Loc, unsigned IVSize,
707 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000708
709 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000710 /// all the work with current loop.
711 ///
712 /// \param CGF Reference to current CodeGenFunction.
713 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000714 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000715 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000716
Alexander Musman92bdaab2015-03-12 13:37:50 +0000717 /// Call __kmpc_dispatch_next(
718 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
719 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
720 /// kmp_int[32|64] *p_stride);
721 /// \param IVSize Size of the iteration variable in bits.
722 /// \param IVSigned Sign of the interation variable.
723 /// \param IL Address of the output variable in which the flag of the
724 /// last iteration is returned.
725 /// \param LB Address of the output variable in which the lower iteration
726 /// number is returned.
727 /// \param UB Address of the output variable in which the upper iteration
728 /// number is returned.
729 /// \param ST Address of the output variable in which the stride value is
730 /// returned.
731 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
732 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000733 Address IL, Address LB,
734 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000735
Alexey Bataevb2059782014-10-13 08:23:51 +0000736 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
737 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
738 /// clause.
739 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000740 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
741 llvm::Value *NumThreads,
742 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000743
Alexey Bataev7f210c62015-06-18 13:40:03 +0000744 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
745 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
746 virtual void emitProcBindClause(CodeGenFunction &CGF,
747 OpenMPProcBindClauseKind ProcBind,
748 SourceLocation Loc);
749
Alexey Bataev97720002014-11-11 04:05:39 +0000750 /// \brief Returns address of the threadprivate variable for the current
751 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000752 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000753 /// \param VDAddr Address of the global variable \a VD.
754 /// \param Loc Location of the reference to threadprivate var.
755 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000756 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
757 const VarDecl *VD,
758 Address VDAddr,
759 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000760
761 /// \brief Emit a code for initialization of threadprivate variable. It emits
762 /// a call to runtime library which adds initial value to the newly created
763 /// threadprivate variable (if it is not constant) and registers destructor
764 /// for the variable (if any).
765 /// \param VD Threadprivate variable.
766 /// \param VDAddr Address of the global variable \a VD.
767 /// \param Loc Location of threadprivate declaration.
768 /// \param PerformInit true if initialization expression is not constant.
769 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000770 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000771 SourceLocation Loc, bool PerformInit,
772 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000773
774 /// \brief Emit flush of the variables specified in 'omp flush' directive.
775 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000776 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
777 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000778
779 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000780 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000781 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
782 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
783 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
784 /// function:
785 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
786 /// TaskFunction(gtid, tt->part_id, tt->shareds);
787 /// return 0;
788 /// }
789 /// 2. Copy a list of shared variables to field shareds of the resulting
790 /// structure kmp_task_t returned by the previous call (if any).
791 /// 3. Copy a pointer to destructions function to field destructions of the
792 /// resulting structure kmp_task_t.
793 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
794 /// kmp_task_t *new_task), where new_task is a resulting structure from
795 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000796 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000797 /// \param Tied true if the task is tied (the task is tied to the thread that
798 /// can suspend its task region), false - untied (the task is not tied to any
799 /// thread).
Alexey Bataev48591dd2016-04-20 04:01:36 +0000800 /// \param NumberOfParts Number of parts for untied task.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000801 /// \param Final Contains either constant bool value, or llvm::Value * of i1
802 /// type for final clause. If the value is true, the task forces all of its
803 /// child tasks to become final and included tasks.
804 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
805 /// /*part_id*/, captured_struct */*__context*/);
806 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000807 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000808 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000809 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
810 /// otherwise.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000811 /// \param PrivateVars List of references to private variables for the task
812 /// directive.
813 /// \param PrivateCopies List of private copies for each private variable in
Alexey Bataev9e034042015-05-05 04:05:12 +0000814 /// \p PrivateVars.
815 /// \param FirstprivateVars List of references to private variables for the
816 /// task directive.
817 /// \param FirstprivateCopies List of private copies for each private variable
818 /// in \p FirstprivateVars.
819 /// \param FirstprivateInits List of references to auto generated variables
820 /// used for initialization of a single array element. Used if firstprivate
821 /// variable is of array type.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000822 /// \param Dependences List of dependences for the 'task' construct, including
823 /// original expression and dependency type.
824 virtual void emitTaskCall(
825 CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
826 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000827 unsigned NumberOfParts, llvm::Value *TaskFunction, QualType SharedsTy,
828 Address Shareds, const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000829 ArrayRef<const Expr *> PrivateCopies,
830 ArrayRef<const Expr *> FirstprivateVars,
831 ArrayRef<const Expr *> FirstprivateCopies,
832 ArrayRef<const Expr *> FirstprivateInits,
833 ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000834
Alexey Bataev7292c292016-04-25 12:22:29 +0000835 /// Emit task region for the taskloop directive. The taskloop region is
836 /// emitted in several steps:
837 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
838 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
839 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
840 /// function:
841 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
842 /// TaskFunction(gtid, tt->part_id, tt->shareds);
843 /// return 0;
844 /// }
845 /// 2. Copy a list of shared variables to field shareds of the resulting
846 /// structure kmp_task_t returned by the previous call (if any).
847 /// 3. Copy a pointer to destructions function to field destructions of the
848 /// resulting structure kmp_task_t.
849 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
850 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
851 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
852 /// is a resulting structure from
853 /// previous items.
854 /// \param D Current task directive.
855 /// \param Tied true if the task is tied (the task is tied to the thread that
856 /// can suspend its task region), false - untied (the task is not tied to any
857 /// thread).
858 /// \param Final Contains either constant bool value, or llvm::Value * of i1
859 /// type for final clause. If the value is true, the task forces all of its
860 /// child tasks to become final and included tasks.
861 /// \param Nogroup true if nogroup clause was specified, false otherwise.
862 /// \param NumberOfParts Number of parts in untied taskloops.
863 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
864 /// /*part_id*/, captured_struct */*__context*/);
865 /// \param SharedsTy A type which contains references the shared variables.
866 /// \param Shareds Context with the list of shared variables from the \p
867 /// TaskFunction.
868 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
869 /// otherwise.
870 /// \param PrivateVars List of references to private variables for the task
871 /// directive.
872 /// \param PrivateCopies List of private copies for each private variable in
873 /// \p PrivateVars.
874 /// \param FirstprivateVars List of references to private variables for the
875 /// task directive.
876 /// \param FirstprivateCopies List of private copies for each private variable
877 /// in \p FirstprivateVars.
878 /// \param FirstprivateInits List of references to auto generated variables
879 /// used for initialization of a single array element. Used if firstprivate
880 /// variable is of array type.
881 virtual void emitTaskLoopCall(
882 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
883 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
884 bool Nogroup, unsigned NumberOfParts, llvm::Value *TaskFunction,
885 QualType SharedsTy, Address Shareds, const Expr *IfCond,
886 ArrayRef<const Expr *> PrivateVars, ArrayRef<const Expr *> PrivateCopies,
887 ArrayRef<const Expr *> FirstprivateVars,
888 ArrayRef<const Expr *> FirstprivateCopies,
889 ArrayRef<const Expr *> FirstprivateInits);
890
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000891 /// \brief Emit code for the directive that does not require outlining.
892 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000893 /// \param InnermostKind Kind of innermost directive (for simple directives it
894 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000895 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000896 /// \param HasCancel true if region has inner cancel directive, false
897 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000898 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000899 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000900 const RegionCodeGenTy &CodeGen,
901 bool HasCancel = false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000902 /// \brief Emit a code for reduction clause. Next code should be emitted for
903 /// reduction:
904 /// \code
905 ///
906 /// static kmp_critical_name lock = { 0 };
907 ///
908 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
909 /// ...
910 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
911 /// ...
912 /// }
913 ///
914 /// ...
915 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
916 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
917 /// RedList, reduce_func, &<lock>)) {
918 /// case 1:
919 /// ...
920 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
921 /// ...
922 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
923 /// break;
924 /// case 2:
925 /// ...
926 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
927 /// ...
928 /// break;
929 /// default:;
930 /// }
931 /// \endcode
932 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000933 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000934 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
935 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
936 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
937 /// or 'operator binop(LHS, RHS)'.
938 /// \param WithNowait true if parent directive has also nowait clause, false
939 /// otherwise.
940 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000941 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000942 ArrayRef<const Expr *> LHSExprs,
943 ArrayRef<const Expr *> RHSExprs,
944 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000945 bool WithNowait, bool SimpleReduction);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000946
947 /// \brief Emit code for 'taskwait' directive.
948 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +0000949
950 /// \brief Emit code for 'cancellation point' construct.
951 /// \param CancelRegion Region kind for which the cancellation point must be
952 /// emitted.
953 ///
954 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
955 SourceLocation Loc,
956 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000957
958 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +0000959 /// \param IfCond Condition in the associated 'if' clause, if it was
960 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000961 /// \param CancelRegion Region kind for which the cancel must be emitted.
962 ///
963 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +0000964 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000965 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +0000966
967 /// \brief Emit outilined function for 'target' directive.
968 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000969 /// \param ParentName Name of the function that encloses the target region.
970 /// \param OutlinedFn Outlined function value to be defined by this call.
971 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
972 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000973 /// \param CodeGen Code generation sequence for the \a D directive.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000974 /// An oulined function may not be an entry if, e.g. the if clause always
975 /// evaluates to false.
976 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
977 StringRef ParentName,
978 llvm::Function *&OutlinedFn,
979 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000980 bool IsOffloadEntry,
981 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +0000982
983 /// \brief Emit the target offloading code associated with \a D. The emitted
984 /// code attempts offloading the execution to the device, an the event of
985 /// a failure it executes the host version outlined in \a OutlinedFn.
986 /// \param D Directive to emit.
987 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000988 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +0000989 /// \param IfCond Expression evaluated in if clause associated with the target
990 /// directive, or null if no if clause is used.
991 /// \param Device Expression evaluated in device clause associated with the
992 /// target directive, or null if no device clause is used.
993 /// \param CapturedVars Values captured in the current region.
994 virtual void emitTargetCall(CodeGenFunction &CGF,
995 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000996 llvm::Value *OutlinedFn,
997 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +0000998 const Expr *Device,
999 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001000
1001 /// \brief Emit the target regions enclosed in \a GD function definition or
1002 /// the function itself in case it is a valid device function. Returns true if
1003 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001004 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001005 virtual bool emitTargetFunctions(GlobalDecl GD);
1006
1007 /// \brief Emit the global variable if it is a valid device global variable.
1008 /// Returns true if \a GD was dealt with successfully.
1009 /// \param GD Variable declaration to emit.
1010 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1011
1012 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
1013 /// if it was emitted succesfully.
1014 /// \param GD Global to scan.
1015 virtual bool emitTargetGlobal(GlobalDecl GD);
1016
1017 /// \brief Creates the offloading descriptor in the event any target region
1018 /// was emitted in the current module and return the function that registers
1019 /// it.
1020 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001021
1022 /// \brief Emits code for teams call of the \a OutlinedFn with
1023 /// variables captured in a record which address is stored in \a
1024 /// CapturedStruct.
1025 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1026 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1027 /// \param CapturedVars A pointer to the record with the references to
1028 /// variables used in \a OutlinedFn function.
1029 ///
1030 virtual void emitTeamsCall(CodeGenFunction &CGF,
1031 const OMPExecutableDirective &D,
1032 SourceLocation Loc, llvm::Value *OutlinedFn,
1033 ArrayRef<llvm::Value *> CapturedVars);
1034
1035 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1036 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1037 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001038 /// \param NumTeams An integer expression of teams.
1039 /// \param ThreadLimit An integer expression of threads.
1040 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1041 const Expr *ThreadLimit, SourceLocation Loc);
Samuel Antaodf158d52016-04-27 22:58:19 +00001042
1043 /// \brief Emit the target data mapping code associated with \a D.
1044 /// \param D Directive to emit.
1045 /// \param IfCond Expression evaluated in if clause associated with the target
1046 /// directive, or null if no if clause is used.
1047 /// \param Device Expression evaluated in device clause associated with the
1048 /// target directive, or null if no device clause is used.
NAKAMURA Takumi01d07db2016-04-28 02:45:21 +00001049 /// \param CodeGen Function that emits the enclosed region.
Samuel Antaodf158d52016-04-27 22:58:19 +00001050 virtual void emitTargetDataCalls(CodeGenFunction &CGF,
1051 const OMPExecutableDirective &D,
1052 const Expr *IfCond, const Expr *Device,
1053 const RegionCodeGenTy &CodeGen);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001054
Samuel Antao8dd66282016-04-27 23:14:30 +00001055 /// \brief Emit the target enter or exit data mapping code associated with
1056 /// directive \a D.
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00001057 /// \param D Directive to emit.
1058 /// \param IfCond Expression evaluated in if clause associated with the target
1059 /// directive, or null if no if clause is used.
1060 /// \param Device Expression evaluated in device clause associated with the
1061 /// target directive, or null if no device clause is used.
Samuel Antao8dd66282016-04-27 23:14:30 +00001062 virtual void emitTargetEnterOrExitDataCall(CodeGenFunction &CGF,
1063 const OMPExecutableDirective &D,
1064 const Expr *IfCond,
1065 const Expr *Device);
Alexey Bataev9959db52014-05-06 10:08:46 +00001066};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001067
Alexey Bataev23b69422014-06-18 07:08:49 +00001068} // namespace CodeGen
1069} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00001070
1071#endif