blob: 028e46dec7ac2af0322b1e2d847bb7a6268c4928 [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.
470 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
471 /// otherwise.
472 /// \param PrivateVars List of references to private variables for the task
473 /// directive.
474 /// \param PrivateCopies List of private copies for each private variable in
475 /// \p PrivateVars.
476 /// \param FirstprivateVars List of references to private variables for the
477 /// task directive.
478 /// \param FirstprivateCopies List of private copies for each private variable
479 /// in \p FirstprivateVars.
480 /// \param FirstprivateInits List of references to auto generated variables
481 /// used for initialization of a single array element. Used if firstprivate
482 /// variable is of array type.
483 TaskDataTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
484 const OMPExecutableDirective &D, bool Tied,
485 llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
486 unsigned NumberOfParts, llvm::Value *TaskFunction,
487 QualType SharedsTy, Address Shareds,
488 ArrayRef<const Expr *> PrivateVars,
489 ArrayRef<const Expr *> PrivateCopies,
490 ArrayRef<const Expr *> FirstprivateVars,
491 ArrayRef<const Expr *> FirstprivateCopies,
492 ArrayRef<const Expr *> FirstprivateInits);
493
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000494public:
495 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000496 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000497 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000498
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000499 /// Emit code for the specified user defined reduction construct.
500 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
501 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000502 /// Get combiner/initializer for the specified user-defined reduction, if any.
503 virtual std::pair<llvm::Function *, llvm::Function *>
504 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000505 /// \brief Emits outlined function for the specified OpenMP parallel directive
506 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
507 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000508 /// \param D OpenMP directive.
509 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000510 /// \param InnermostKind Kind of innermost directive (for simple directives it
511 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000512 /// \param CodeGen Code generation sequence for the \a D directive.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000513 virtual llvm::Value *emitParallelOrTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000514 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
515 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000516
Alexey Bataev62b63b12015-03-10 07:28:44 +0000517 /// \brief Emits outlined function for the OpenMP task directive \a D. This
Alexey Bataev48591dd2016-04-20 04:01:36 +0000518 /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
519 /// TaskT).
Alexey Bataev62b63b12015-03-10 07:28:44 +0000520 /// \param D OpenMP directive.
521 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000522 /// \param PartIDVar Variable for partition id in the current OpenMP untied
523 /// task region.
524 /// \param TaskTVar Variable for task_t argument.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000525 /// \param InnermostKind Kind of innermost directive (for simple directives it
526 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000527 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000528 /// \param Tied true if task is generated for tied task, false otherwise.
529 /// \param NumberOfParts Number of parts in untied task. Ignored for tied
530 /// tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000531 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000532 virtual llvm::Value *emitTaskOutlinedFunction(
533 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000534 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
535 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
536 bool Tied, unsigned &NumberOfParts);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000537
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000538 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000539 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000540 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000541
Alexey Bataev1d677132015-04-22 13:57:31 +0000542 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
543 /// variables captured in a record which address is stored in \a
544 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000545 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000546 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000547 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000548 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000549 /// \param IfCond Condition in the associated 'if' clause, if it was
550 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000551 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000552 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
553 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000554 ArrayRef<llvm::Value *> CapturedVars,
555 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000556
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000557 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000558 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000559 /// \param CriticalOpGen Generator for the statement associated with the given
560 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000561 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000562 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000563 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000564 SourceLocation Loc,
565 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000566
Alexey Bataev8d690652014-12-04 07:23:53 +0000567 /// \brief Emits a master region.
568 /// \param MasterOpGen Generator for the statement associated with the given
569 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000570 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000571 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000572 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000573
Alexey Bataev9f797f32015-02-05 05:57:51 +0000574 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000575 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000576
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000577 /// \brief Emit a taskgroup region.
578 /// \param TaskgroupOpGen Generator for the statement associated with the
579 /// given taskgroup region.
580 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
581 const RegionCodeGenTy &TaskgroupOpGen,
582 SourceLocation Loc);
583
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000584 /// \brief Emits a single region.
585 /// \param SingleOpGen Generator for the statement associated with the given
586 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000587 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000588 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000589 SourceLocation Loc,
590 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000591 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000592 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000593 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000594
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000595 /// \brief Emit an ordered region.
596 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000597 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000598 virtual void emitOrderedRegion(CodeGenFunction &CGF,
599 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000600 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000601
Alexey Bataevf2685682015-03-30 04:30:22 +0000602 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
603 /// \param Kind Directive for which this implicit barrier call must be
604 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000605 /// \param EmitChecks true if need to emit checks for cancellation barriers.
606 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
607 /// runtime class decides which one to emit (simple or with cancellation
608 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000609 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000610 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000611 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000612 bool EmitChecks = true,
613 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000614
Alexander Musmanc6388682014-12-15 07:07:06 +0000615 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
616 /// This kind of worksharing directive is emitted without outer loop.
617 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
618 /// \param Chunked True if chunk is specified in the clause.
619 ///
620 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
621 bool Chunked) const;
622
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000623 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
624 /// This kind of distribute directive is emitted without outer loop.
625 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
626 /// \param Chunked True if chunk is specified in the clause.
627 ///
628 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
629 bool Chunked) const;
630
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000631 /// \brief Check if the specified \a ScheduleKind is dynamic.
632 /// This kind of worksharing directive is emitted without outer loop.
633 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
634 ///
635 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
636
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000637 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
638 OpenMPScheduleClauseKind SchedKind,
639 unsigned IVSize, bool IVSigned,
640 bool Ordered, llvm::Value *UB,
641 llvm::Value *Chunk = nullptr);
642
Alexander Musmanc6388682014-12-15 07:07:06 +0000643 /// \brief Call the appropriate runtime routine to initialize it before start
644 /// of loop.
645 ///
646 /// Depending on the loop schedule, it is nesessary to call some runtime
647 /// routine before start of the OpenMP loop to get the loop upper / lower
648 /// bounds \a LB and \a UB and stride \a ST.
649 ///
650 /// \param CGF Reference to current CodeGenFunction.
651 /// \param Loc Clang source location.
NAKAMURA Takumieca08382014-12-17 14:47:06 +0000652 /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000653 /// \param IVSize Size of the iteration variable in bits.
654 /// \param IVSigned Sign of the interation variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000655 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000656 /// \param IL Address of the output variable in which the flag of the
657 /// last iteration is returned.
658 /// \param LB Address of the output variable in which the lower iteration
659 /// number is returned.
660 /// \param UB Address of the output variable in which the upper iteration
661 /// number is returned.
662 /// \param ST Address of the output variable in which the stride value is
663 /// returned nesessary to generated the static_chunked scheduled loop.
664 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
665 /// For the default (nullptr) value, the chunk 1 will be used.
666 ///
John McCall7f416cc2015-09-08 08:05:57 +0000667 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
668 OpenMPScheduleClauseKind SchedKind,
669 unsigned IVSize, bool IVSigned, bool Ordered,
670 Address IL, Address LB,
671 Address UB, Address ST,
672 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000673
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000674 ///
675 /// \param CGF Reference to current CodeGenFunction.
676 /// \param Loc Clang source location.
677 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
678 /// \param IVSize Size of the iteration variable in bits.
679 /// \param IVSigned Sign of the interation variable.
680 /// \param Ordered true if loop is ordered, false otherwise.
681 /// \param IL Address of the output variable in which the flag of the
682 /// last iteration is returned.
683 /// \param LB Address of the output variable in which the lower iteration
684 /// number is returned.
685 /// \param UB Address of the output variable in which the upper iteration
686 /// number is returned.
687 /// \param ST Address of the output variable in which the stride value is
688 /// returned nesessary to generated the static_chunked scheduled loop.
689 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
690 /// For the default (nullptr) value, the chunk 1 will be used.
691 ///
692 virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
693 OpenMPDistScheduleClauseKind SchedKind,
694 unsigned IVSize, bool IVSigned,
695 bool Ordered, Address IL, Address LB,
696 Address UB, Address ST,
697 llvm::Value *Chunk = nullptr);
698
Alexander Musmanc6388682014-12-15 07:07:06 +0000699 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000700 /// iteration of the ordered loop with the dynamic scheduling.
701 ///
702 /// \param CGF Reference to current CodeGenFunction.
703 /// \param Loc Clang source location.
704 /// \param IVSize Size of the iteration variable in bits.
705 /// \param IVSigned Sign of the interation variable.
706 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000707 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
708 SourceLocation Loc, unsigned IVSize,
709 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000710
711 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000712 /// all the work with current loop.
713 ///
714 /// \param CGF Reference to current CodeGenFunction.
715 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000716 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000717 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000718
Alexander Musman92bdaab2015-03-12 13:37:50 +0000719 /// Call __kmpc_dispatch_next(
720 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
721 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
722 /// kmp_int[32|64] *p_stride);
723 /// \param IVSize Size of the iteration variable in bits.
724 /// \param IVSigned Sign of the interation variable.
725 /// \param IL Address of the output variable in which the flag of the
726 /// last iteration is returned.
727 /// \param LB Address of the output variable in which the lower iteration
728 /// number is returned.
729 /// \param UB Address of the output variable in which the upper iteration
730 /// number is returned.
731 /// \param ST Address of the output variable in which the stride value is
732 /// returned.
733 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
734 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000735 Address IL, Address LB,
736 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000737
Alexey Bataevb2059782014-10-13 08:23:51 +0000738 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
739 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
740 /// clause.
741 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000742 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
743 llvm::Value *NumThreads,
744 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000745
Alexey Bataev7f210c62015-06-18 13:40:03 +0000746 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
747 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
748 virtual void emitProcBindClause(CodeGenFunction &CGF,
749 OpenMPProcBindClauseKind ProcBind,
750 SourceLocation Loc);
751
Alexey Bataev97720002014-11-11 04:05:39 +0000752 /// \brief Returns address of the threadprivate variable for the current
753 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000754 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000755 /// \param VDAddr Address of the global variable \a VD.
756 /// \param Loc Location of the reference to threadprivate var.
757 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000758 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
759 const VarDecl *VD,
760 Address VDAddr,
761 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000762
763 /// \brief Emit a code for initialization of threadprivate variable. It emits
764 /// a call to runtime library which adds initial value to the newly created
765 /// threadprivate variable (if it is not constant) and registers destructor
766 /// for the variable (if any).
767 /// \param VD Threadprivate variable.
768 /// \param VDAddr Address of the global variable \a VD.
769 /// \param Loc Location of threadprivate declaration.
770 /// \param PerformInit true if initialization expression is not constant.
771 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000772 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000773 SourceLocation Loc, bool PerformInit,
774 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000775
776 /// \brief Emit flush of the variables specified in 'omp flush' directive.
777 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000778 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
779 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000780
781 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000782 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000783 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
784 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
785 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
786 /// function:
787 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
788 /// TaskFunction(gtid, tt->part_id, tt->shareds);
789 /// return 0;
790 /// }
791 /// 2. Copy a list of shared variables to field shareds of the resulting
792 /// structure kmp_task_t returned by the previous call (if any).
793 /// 3. Copy a pointer to destructions function to field destructions of the
794 /// resulting structure kmp_task_t.
795 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
796 /// kmp_task_t *new_task), where new_task is a resulting structure from
797 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000798 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000799 /// \param Tied true if the task is tied (the task is tied to the thread that
800 /// can suspend its task region), false - untied (the task is not tied to any
801 /// thread).
Alexey Bataev48591dd2016-04-20 04:01:36 +0000802 /// \param NumberOfParts Number of parts for untied task.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000803 /// \param Final Contains either constant bool value, or llvm::Value * of i1
804 /// type for final clause. If the value is true, the task forces all of its
805 /// child tasks to become final and included tasks.
806 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
807 /// /*part_id*/, captured_struct */*__context*/);
808 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000809 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000810 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000811 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
812 /// otherwise.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000813 /// \param PrivateVars List of references to private variables for the task
814 /// directive.
815 /// \param PrivateCopies List of private copies for each private variable in
Alexey Bataev9e034042015-05-05 04:05:12 +0000816 /// \p PrivateVars.
817 /// \param FirstprivateVars List of references to private variables for the
818 /// task directive.
819 /// \param FirstprivateCopies List of private copies for each private variable
820 /// in \p FirstprivateVars.
821 /// \param FirstprivateInits List of references to auto generated variables
822 /// used for initialization of a single array element. Used if firstprivate
823 /// variable is of array type.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000824 /// \param Dependences List of dependences for the 'task' construct, including
825 /// original expression and dependency type.
826 virtual void emitTaskCall(
827 CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
828 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000829 unsigned NumberOfParts, llvm::Value *TaskFunction, QualType SharedsTy,
830 Address Shareds, const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000831 ArrayRef<const Expr *> PrivateCopies,
832 ArrayRef<const Expr *> FirstprivateVars,
833 ArrayRef<const Expr *> FirstprivateCopies,
834 ArrayRef<const Expr *> FirstprivateInits,
835 ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000836
Alexey Bataev7292c292016-04-25 12:22:29 +0000837 /// Emit task region for the taskloop directive. The taskloop region is
838 /// emitted in several steps:
839 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
840 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
841 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
842 /// function:
843 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
844 /// TaskFunction(gtid, tt->part_id, tt->shareds);
845 /// return 0;
846 /// }
847 /// 2. Copy a list of shared variables to field shareds of the resulting
848 /// structure kmp_task_t returned by the previous call (if any).
849 /// 3. Copy a pointer to destructions function to field destructions of the
850 /// resulting structure kmp_task_t.
851 /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
852 /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
853 /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
854 /// is a resulting structure from
855 /// previous items.
856 /// \param D Current task directive.
857 /// \param Tied true if the task is tied (the task is tied to the thread that
858 /// can suspend its task region), false - untied (the task is not tied to any
859 /// thread).
860 /// \param Final Contains either constant bool value, or llvm::Value * of i1
861 /// type for final clause. If the value is true, the task forces all of its
862 /// child tasks to become final and included tasks.
863 /// \param Nogroup true if nogroup clause was specified, false otherwise.
864 /// \param NumberOfParts Number of parts in untied taskloops.
865 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
866 /// /*part_id*/, captured_struct */*__context*/);
867 /// \param SharedsTy A type which contains references the shared variables.
868 /// \param Shareds Context with the list of shared variables from the \p
869 /// TaskFunction.
870 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
871 /// otherwise.
872 /// \param PrivateVars List of references to private variables for the task
873 /// directive.
874 /// \param PrivateCopies List of private copies for each private variable in
875 /// \p PrivateVars.
876 /// \param FirstprivateVars List of references to private variables for the
877 /// task directive.
878 /// \param FirstprivateCopies List of private copies for each private variable
879 /// in \p FirstprivateVars.
880 /// \param FirstprivateInits List of references to auto generated variables
881 /// used for initialization of a single array element. Used if firstprivate
882 /// variable is of array type.
883 virtual void emitTaskLoopCall(
884 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
885 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
886 bool Nogroup, unsigned NumberOfParts, llvm::Value *TaskFunction,
887 QualType SharedsTy, Address Shareds, const Expr *IfCond,
888 ArrayRef<const Expr *> PrivateVars, ArrayRef<const Expr *> PrivateCopies,
889 ArrayRef<const Expr *> FirstprivateVars,
890 ArrayRef<const Expr *> FirstprivateCopies,
891 ArrayRef<const Expr *> FirstprivateInits);
892
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000893 /// \brief Emit code for the directive that does not require outlining.
894 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000895 /// \param InnermostKind Kind of innermost directive (for simple directives it
896 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000897 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000898 /// \param HasCancel true if region has inner cancel directive, false
899 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000900 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000901 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000902 const RegionCodeGenTy &CodeGen,
903 bool HasCancel = false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000904 /// \brief Emit a code for reduction clause. Next code should be emitted for
905 /// reduction:
906 /// \code
907 ///
908 /// static kmp_critical_name lock = { 0 };
909 ///
910 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
911 /// ...
912 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
913 /// ...
914 /// }
915 ///
916 /// ...
917 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
918 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
919 /// RedList, reduce_func, &<lock>)) {
920 /// case 1:
921 /// ...
922 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
923 /// ...
924 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
925 /// break;
926 /// case 2:
927 /// ...
928 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
929 /// ...
930 /// break;
931 /// default:;
932 /// }
933 /// \endcode
934 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000935 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000936 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
937 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
938 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
939 /// or 'operator binop(LHS, RHS)'.
940 /// \param WithNowait true if parent directive has also nowait clause, false
941 /// otherwise.
942 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000943 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000944 ArrayRef<const Expr *> LHSExprs,
945 ArrayRef<const Expr *> RHSExprs,
946 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000947 bool WithNowait, bool SimpleReduction);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000948
949 /// \brief Emit code for 'taskwait' directive.
950 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +0000951
952 /// \brief Emit code for 'cancellation point' construct.
953 /// \param CancelRegion Region kind for which the cancellation point must be
954 /// emitted.
955 ///
956 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
957 SourceLocation Loc,
958 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000959
960 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +0000961 /// \param IfCond Condition in the associated 'if' clause, if it was
962 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000963 /// \param CancelRegion Region kind for which the cancel must be emitted.
964 ///
965 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +0000966 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000967 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +0000968
969 /// \brief Emit outilined function for 'target' directive.
970 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000971 /// \param ParentName Name of the function that encloses the target region.
972 /// \param OutlinedFn Outlined function value to be defined by this call.
973 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
974 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000975 /// \param CodeGen Code generation sequence for the \a D directive.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000976 /// An oulined function may not be an entry if, e.g. the if clause always
977 /// evaluates to false.
978 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
979 StringRef ParentName,
980 llvm::Function *&OutlinedFn,
981 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000982 bool IsOffloadEntry,
983 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +0000984
985 /// \brief Emit the target offloading code associated with \a D. The emitted
986 /// code attempts offloading the execution to the device, an the event of
987 /// a failure it executes the host version outlined in \a OutlinedFn.
988 /// \param D Directive to emit.
989 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000990 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +0000991 /// \param IfCond Expression evaluated in if clause associated with the target
992 /// directive, or null if no if clause is used.
993 /// \param Device Expression evaluated in device clause associated with the
994 /// target directive, or null if no device clause is used.
995 /// \param CapturedVars Values captured in the current region.
996 virtual void emitTargetCall(CodeGenFunction &CGF,
997 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000998 llvm::Value *OutlinedFn,
999 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +00001000 const Expr *Device,
1001 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001002
1003 /// \brief Emit the target regions enclosed in \a GD function definition or
1004 /// the function itself in case it is a valid device function. Returns true if
1005 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +00001006 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +00001007 virtual bool emitTargetFunctions(GlobalDecl GD);
1008
1009 /// \brief Emit the global variable if it is a valid device global variable.
1010 /// Returns true if \a GD was dealt with successfully.
1011 /// \param GD Variable declaration to emit.
1012 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
1013
1014 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
1015 /// if it was emitted succesfully.
1016 /// \param GD Global to scan.
1017 virtual bool emitTargetGlobal(GlobalDecl GD);
1018
1019 /// \brief Creates the offloading descriptor in the event any target region
1020 /// was emitted in the current module and return the function that registers
1021 /// it.
1022 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001023
1024 /// \brief Emits code for teams call of the \a OutlinedFn with
1025 /// variables captured in a record which address is stored in \a
1026 /// CapturedStruct.
1027 /// \param OutlinedFn Outlined function to be run by team masters. Type of
1028 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
1029 /// \param CapturedVars A pointer to the record with the references to
1030 /// variables used in \a OutlinedFn function.
1031 ///
1032 virtual void emitTeamsCall(CodeGenFunction &CGF,
1033 const OMPExecutableDirective &D,
1034 SourceLocation Loc, llvm::Value *OutlinedFn,
1035 ArrayRef<llvm::Value *> CapturedVars);
1036
1037 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
1038 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
1039 /// for num_teams clause.
Carlo Bertollic6872252016-04-04 15:55:02 +00001040 /// \param NumTeams An integer expression of teams.
1041 /// \param ThreadLimit An integer expression of threads.
1042 virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
1043 const Expr *ThreadLimit, SourceLocation Loc);
Alexey Bataev9959db52014-05-06 10:08:46 +00001044};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001045
Alexey Bataev23b69422014-06-18 07:08:49 +00001046} // namespace CodeGen
1047} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +00001048
1049#endif