blob: d2a1a243a8c1815048ab98650e6cbc105a3d53e0 [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 Bataev62b63b12015-03-10 07:28:44 +000017#include "clang/AST/Type.h"
Alexander Musmanc6388682014-12-15 07:07:06 +000018#include "clang/Basic/OpenMPKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000019#include "clang/Basic/SourceLocation.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000020#include "llvm/ADT/DenseMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000021#include "llvm/ADT/SmallPtrSet.h"
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000022#include "llvm/ADT/StringMap.h"
Benjamin Kramer8fdba912016-02-02 14:24:21 +000023#include "llvm/IR/Function.h"
Alexey Bataev97720002014-11-11 04:05:39 +000024#include "llvm/IR/ValueHandle.h"
Alexey Bataev18095712014-10-10 12:19:54 +000025
26namespace llvm {
27class ArrayType;
28class Constant;
Alexey Bataev18095712014-10-10 12:19:54 +000029class FunctionType;
Alexey Bataev97720002014-11-11 04:05:39 +000030class GlobalVariable;
Alexey Bataev18095712014-10-10 12:19:54 +000031class StructType;
32class Type;
33class Value;
34} // namespace llvm
Alexey Bataev9959db52014-05-06 10:08:46 +000035
Alexey Bataev9959db52014-05-06 10:08:46 +000036namespace clang {
Alexey Bataevcc37cc12014-11-20 04:34:54 +000037class Expr;
Samuel Antaoee8fb302016-01-06 13:42:12 +000038class GlobalDecl;
Alexey Bataev18095712014-10-10 12:19:54 +000039class OMPExecutableDirective;
40class VarDecl;
Alexey Bataevc5b1d322016-03-04 09:22:22 +000041class OMPDeclareReductionDecl;
42class IdentifierInfo;
Alexey Bataev18095712014-10-10 12:19:54 +000043
Alexey Bataev9959db52014-05-06 10:08:46 +000044namespace CodeGen {
John McCall7f416cc2015-09-08 08:05:57 +000045class Address;
Alexey Bataev18095712014-10-10 12:19:54 +000046class CodeGenFunction;
47class CodeGenModule;
Alexey Bataev9959db52014-05-06 10:08:46 +000048
Alexey Bataev14fa1c62016-03-29 05:34:15 +000049/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
50/// region.
51class PrePostActionTy {
52public:
53 explicit PrePostActionTy() {}
54 virtual void Enter(CodeGenFunction &CGF) {}
55 virtual void Exit(CodeGenFunction &CGF) {}
56 virtual ~PrePostActionTy() {}
57};
58
59/// Class provides a way to call simple version of codegen for OpenMP region, or
60/// an advanced with possible pre|post-actions in codegen.
61class RegionCodeGenTy final {
62 intptr_t CodeGen;
63 typedef void (*CodeGenTy)(intptr_t, CodeGenFunction &, PrePostActionTy &);
64 CodeGenTy Callback;
65 mutable PrePostActionTy *PrePostAction;
66 RegionCodeGenTy() = delete;
67 RegionCodeGenTy &operator=(const RegionCodeGenTy &) = delete;
68 template <typename Callable>
69 static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
70 PrePostActionTy &Action) {
71 return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
72 }
73
74public:
75 template <typename Callable>
76 RegionCodeGenTy(
77 Callable &&CodeGen,
78 typename std::enable_if<
79 !std::is_same<typename std::remove_reference<Callable>::type,
80 RegionCodeGenTy>::value>::type * = nullptr)
81 : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
82 Callback(CallbackFn<typename std::remove_reference<Callable>::type>),
83 PrePostAction(nullptr) {}
84 void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
85 void operator()(CodeGenFunction &CGF) const;
86};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000087
Alexey Bataev9959db52014-05-06 10:08:46 +000088class CGOpenMPRuntime {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000089protected:
Alexey Bataev9959db52014-05-06 10:08:46 +000090 CodeGenModule &CGM;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +000091
92 /// \brief Creates offloading entry for the provided entry ID \a ID,
93 /// address \a Addr and size \a Size.
94 virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
95 uint64_t Size);
96
97 /// \brief Helper to emit outlined function for 'target' directive.
98 /// \param D Directive to emit.
99 /// \param ParentName Name of the function that encloses the target region.
100 /// \param OutlinedFn Outlined function value to be defined by this call.
101 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
102 /// \param IsOffloadEntry True if the outlined function is an offload entry.
103 /// \param CodeGen Lambda codegen specific to an accelerator device.
104 /// An oulined function may not be an entry if, e.g. the if clause always
105 /// evaluates to false.
106 virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
107 StringRef ParentName,
108 llvm::Function *&OutlinedFn,
109 llvm::Constant *&OutlinedFnID,
110 bool IsOffloadEntry,
111 const RegionCodeGenTy &CodeGen);
112
113private:
Alexey Bataev9959db52014-05-06 10:08:46 +0000114 /// \brief Default const ident_t object used for initialization of all other
115 /// ident_t objects.
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000116 llvm::Constant *DefaultOpenMPPSource = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000117 /// \brief Map of flags and corresponding default locations.
Alexey Bataev15007ba2014-05-07 06:18:01 +0000118 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
119 OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
Alexey Bataev50b3c952016-02-19 10:38:26 +0000120 Address getOrCreateDefaultLocation(unsigned Flags);
John McCall7f416cc2015-09-08 08:05:57 +0000121
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000122 llvm::StructType *IdentTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000123 /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
Alexey Bataevf002aca2014-05-30 05:48:40 +0000124 typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
125 OpenMPDebugLocMapTy OpenMPDebugLocMap;
Alexey Bataev9959db52014-05-06 10:08:46 +0000126 /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
127 /// Original representation is:
128 /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000129 llvm::FunctionType *Kmpc_MicroTy = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000130 /// \brief Stores debug location and ThreadID for the function.
131 struct DebugLocThreadIdTy {
132 llvm::Value *DebugLoc;
133 llvm::Value *ThreadID;
134 };
135 /// \brief Map of local debug location, ThreadId and functions.
136 typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
137 OpenMPLocThreadIDMapTy;
138 OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000139 /// Map of UDRs and corresponding combiner/initializer.
140 typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
141 std::pair<llvm::Function *, llvm::Function *>>
142 UDRMapTy;
143 UDRMapTy UDRMap;
144 /// Map of functions and locally defined UDRs.
145 typedef llvm::DenseMap<llvm::Function *,
146 SmallVector<const OMPDeclareReductionDecl *, 4>>
147 FunctionUDRMapTy;
148 FunctionUDRMapTy FunctionUDRMap;
149 IdentifierInfo *In = nullptr;
150 IdentifierInfo *Out = nullptr;
151 IdentifierInfo *Priv = nullptr;
152 IdentifierInfo *Orig = nullptr;
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000153 /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
154 /// kmp_critical_name[8];
155 llvm::ArrayType *KmpCriticalNameTy;
Alexey Bataev97720002014-11-11 04:05:39 +0000156 /// \brief An ordered map of auto-generated variables to their unique names.
157 /// It stores variables with the following names: 1) ".gomp_critical_user_" +
158 /// <critical_section_name> + ".var" for "omp critical" directives; 2)
159 /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
160 /// variables.
161 llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
162 InternalVars;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000163 /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000164 llvm::Type *KmpRoutineEntryPtrTy = nullptr;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000165 QualType KmpRoutineEntryPtrQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +0000166 /// \brief Type typedef struct kmp_task {
167 /// void * shareds; /**< pointer to block of pointers to
168 /// shared vars */
169 /// kmp_routine_entry_t routine; /**< pointer to routine to call for
170 /// executing task */
171 /// kmp_int32 part_id; /**< part id for the task */
172 /// kmp_routine_entry_t destructors; /* pointer to function to invoke
173 /// deconstructors of firstprivate C++ objects */
174 /// } kmp_task_t;
175 QualType KmpTaskTQTy;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000176 /// \brief Type typedef struct kmp_depend_info {
177 /// kmp_intptr_t base_addr;
178 /// size_t len;
179 /// struct {
180 /// bool in:1;
181 /// bool out:1;
182 /// } flags;
183 /// } kmp_depend_info_t;
184 QualType KmpDependInfoTy;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000185 /// \brief Type struct __tgt_offload_entry{
186 /// void *addr; // Pointer to the offload entry info.
187 /// // (function or global)
188 /// char *name; // Name of the function or global.
189 /// size_t size; // Size of the entry info (0 if it a function).
190 /// };
191 QualType TgtOffloadEntryQTy;
192 /// struct __tgt_device_image{
193 /// void *ImageStart; // Pointer to the target code start.
194 /// void *ImageEnd; // Pointer to the target code end.
195 /// // We also add the host entries to the device image, as it may be useful
196 /// // for the target runtime to have access to that information.
197 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all
198 /// // the entries.
199 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
200 /// // entries (non inclusive).
201 /// };
202 QualType TgtDeviceImageQTy;
203 /// struct __tgt_bin_desc{
204 /// int32_t NumDevices; // Number of devices supported.
205 /// __tgt_device_image *DeviceImages; // Arrays of device images
206 /// // (one per device).
207 /// __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
208 /// // entries.
209 /// __tgt_offload_entry *EntriesEnd; // End of the table with all the
210 /// // entries (non inclusive).
211 /// };
212 QualType TgtBinaryDescriptorQTy;
213 /// \brief Entity that registers the offloading constants that were emitted so
214 /// far.
215 class OffloadEntriesInfoManagerTy {
216 CodeGenModule &CGM;
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000217
Samuel Antaoee8fb302016-01-06 13:42:12 +0000218 /// \brief Number of entries registered so far.
219 unsigned OffloadingEntriesNum;
220
221 public:
222 /// \brief Base class of the entries info.
223 class OffloadEntryInfo {
224 public:
225 /// \brief Kind of a given entry. Currently, only target regions are
226 /// supported.
Reid Klecknerdc78f952016-01-11 20:55:16 +0000227 enum OffloadingEntryInfoKinds : unsigned {
Samuel Antaoee8fb302016-01-06 13:42:12 +0000228 // Entry is a target region.
229 OFFLOAD_ENTRY_INFO_TARGET_REGION = 0,
230 // Invalid entry info.
231 OFFLOAD_ENTRY_INFO_INVALID = ~0u
232 };
233
234 OffloadEntryInfo() : Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
235 explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order)
236 : Order(Order), Kind(Kind) {}
237
238 bool isValid() const { return Order != ~0u; }
239 unsigned getOrder() const { return Order; }
240 OffloadingEntryInfoKinds getKind() const { return Kind; }
241 static bool classof(const OffloadEntryInfo *Info) { return true; }
242
243 protected:
244 // \brief Order this entry was emitted.
245 unsigned Order;
246
247 OffloadingEntryInfoKinds Kind;
248 };
249
250 /// \brief Return true if a there are no entries defined.
251 bool empty() const;
252 /// \brief Return number of entries defined so far.
253 unsigned size() const { return OffloadingEntriesNum; }
254 OffloadEntriesInfoManagerTy(CodeGenModule &CGM)
255 : CGM(CGM), OffloadingEntriesNum(0) {}
256
257 ///
258 /// Target region entries related.
259 ///
260 /// \brief Target region entries info.
261 class OffloadEntryInfoTargetRegion : public OffloadEntryInfo {
262 // \brief Address of the entity that has to be mapped for offloading.
263 llvm::Constant *Addr;
264 // \brief Address that can be used as the ID of the entry.
265 llvm::Constant *ID;
266
267 public:
268 OffloadEntryInfoTargetRegion()
269 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u),
270 Addr(nullptr), ID(nullptr) {}
271 explicit OffloadEntryInfoTargetRegion(unsigned Order,
272 llvm::Constant *Addr,
273 llvm::Constant *ID)
274 : OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order),
275 Addr(Addr), ID(ID) {}
276
277 llvm::Constant *getAddress() const { return Addr; }
278 llvm::Constant *getID() const { return ID; }
279 void setAddress(llvm::Constant *V) {
280 assert(!Addr && "Address as been set before!");
281 Addr = V;
282 }
283 void setID(llvm::Constant *V) {
284 assert(!ID && "ID as been set before!");
285 ID = V;
286 }
287 static bool classof(const OffloadEntryInfo *Info) {
288 return Info->getKind() == OFFLOAD_ENTRY_INFO_TARGET_REGION;
289 }
290 };
291 /// \brief Initialize target region entry.
292 void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
293 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000294 unsigned Order);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000295 /// \brief Register target region entry.
296 void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
297 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +0000298 llvm::Constant *Addr,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000299 llvm::Constant *ID);
300 /// \brief Return true if a target region entry with the provided
301 /// information exists.
302 bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +0000303 StringRef ParentName, unsigned LineNum) const;
Samuel Antaoee8fb302016-01-06 13:42:12 +0000304 /// brief Applies action \a Action on all registered entries.
305 typedef llvm::function_ref<void(unsigned, unsigned, StringRef, unsigned,
Samuel Antao2de62b02016-02-13 23:35:10 +0000306 OffloadEntryInfoTargetRegion &)>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000307 OffloadTargetRegionEntryInfoActTy;
308 void actOnTargetRegionEntriesInfo(
309 const OffloadTargetRegionEntryInfoActTy &Action);
310
311 private:
312 // Storage for target region entries kind. The storage is to be indexed by
Samuel Antao2de62b02016-02-13 23:35:10 +0000313 // file ID, device ID, parent function name and line number.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000314 typedef llvm::DenseMap<unsigned, OffloadEntryInfoTargetRegion>
Samuel Antaoee8fb302016-01-06 13:42:12 +0000315 OffloadEntriesTargetRegionPerLine;
316 typedef llvm::StringMap<OffloadEntriesTargetRegionPerLine>
317 OffloadEntriesTargetRegionPerParentName;
318 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
319 OffloadEntriesTargetRegionPerFile;
320 typedef llvm::DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
321 OffloadEntriesTargetRegionPerDevice;
322 typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
323 OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
324 };
325 OffloadEntriesInfoManagerTy OffloadEntriesInfoManager;
326
327 /// \brief Creates and registers offloading binary descriptor for the current
328 /// compilation unit. The function that does the registration is returned.
329 llvm::Function *createOffloadingBinaryDescriptorRegistration();
330
Samuel Antaoee8fb302016-01-06 13:42:12 +0000331 /// \brief Creates all the offload entries in the current compilation unit
332 /// along with the associated metadata.
333 void createOffloadEntriesAndInfoMetadata();
334
335 /// \brief Loads all the offload entries information from the host IR
336 /// metadata.
337 void loadOffloadInfoMetadata();
338
339 /// \brief Returns __tgt_offload_entry type.
340 QualType getTgtOffloadEntryQTy();
341
342 /// \brief Returns __tgt_device_image type.
343 QualType getTgtDeviceImageQTy();
344
345 /// \brief Returns __tgt_bin_desc type.
346 QualType getTgtBinaryDescriptorQTy();
347
348 /// \brief Start scanning from statement \a S and and emit all target regions
349 /// found along the way.
350 /// \param S Starting statement.
351 /// \param ParentName Name of the function declaration that is being scanned.
352 void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000353
354 /// \brief Build type kmp_routine_entry_t (if not built yet).
355 void emitKmpRoutineEntryT(QualType KmpInt32Ty);
Alexey Bataev9959db52014-05-06 10:08:46 +0000356
Alexey Bataev9959db52014-05-06 10:08:46 +0000357 /// \brief Emits object of ident_t type with info for source location.
Alexey Bataev9959db52014-05-06 10:08:46 +0000358 /// \param Flags Flags for OpenMP location.
359 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000360 llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000361 unsigned Flags = 0);
Alexey Bataev9959db52014-05-06 10:08:46 +0000362
Alexey Bataevd74d0602014-10-13 06:02:40 +0000363 /// \brief Returns pointer to ident_t type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000364 llvm::Type *getIdentTyPointerTy();
365
Alexey Bataevd74d0602014-10-13 06:02:40 +0000366 /// \brief Returns pointer to kmpc_micro type.
Alexey Bataev9959db52014-05-06 10:08:46 +0000367 llvm::Type *getKmpc_MicroPointerTy();
368
369 /// \brief Returns specified OpenMP runtime function.
370 /// \param Function OpenMP runtime function.
371 /// \return Specified function.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000372 llvm::Constant *createRuntimeFunction(unsigned Function);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000373
Alexander Musman21212e42015-03-13 10:38:23 +0000374 /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
375 /// size \a IVSize and sign \a IVSigned.
376 llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
377
Alexander Musman92bdaab2015-03-12 13:37:50 +0000378 /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
379 /// size \a IVSize and sign \a IVSigned.
380 llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
381
382 /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
383 /// size \a IVSize and sign \a IVSigned.
384 llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
385
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000386 /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
387 /// size \a IVSize and sign \a IVSigned.
388 llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
389
Alexey Bataev97720002014-11-11 04:05:39 +0000390 /// \brief If the specified mangled name is not in the module, create and
391 /// return threadprivate cache object. This object is a pointer's worth of
392 /// storage that's reserved for use by the OpenMP runtime.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000393 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000394 /// \return Cache variable for the specified threadprivate.
395 llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
396
Alexey Bataevd74d0602014-10-13 06:02:40 +0000397 /// \brief Emits address of the word in a memory where current thread id is
398 /// stored.
John McCall7f416cc2015-09-08 08:05:57 +0000399 virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000400
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000401 /// \brief Gets thread id value for the current thread.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000402 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000403 llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000404
Alexey Bataev97720002014-11-11 04:05:39 +0000405 /// \brief Gets (if variable with the given name already exist) or creates
406 /// internal global variable with the specified Name. The created variable has
407 /// linkage CommonLinkage by default and is initialized by null value.
408 /// \param Ty Type of the global variable. If it is exist already the type
409 /// must be the same.
410 /// \param Name Name of the variable.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000411 llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000412 const llvm::Twine &Name);
413
414 /// \brief Set of threadprivate variables with the generated initializer.
Benjamin Kramer8fdba912016-02-02 14:24:21 +0000415 llvm::SmallPtrSet<const VarDecl *, 4> ThreadPrivateWithDefinition;
Alexey Bataev97720002014-11-11 04:05:39 +0000416
417 /// \brief Emits initialization code for the threadprivate variables.
418 /// \param VDAddr Address of the global variable \a VD.
419 /// \param Ctor Pointer to a global init function for \a VD.
420 /// \param CopyCtor Pointer to a global copy function for \a VD.
421 /// \param Dtor Pointer to a global destructor function for \a VD.
422 /// \param Loc Location of threadprivate declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000423 void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000424 llvm::Value *Ctor, llvm::Value *CopyCtor,
425 llvm::Value *Dtor, SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000426
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000427 /// \brief Returns corresponding lock object for the specified critical region
428 /// name. If the lock object does not exist it is created, otherwise the
429 /// reference to the existing copy is returned.
430 /// \param CriticalName Name of the critical region.
431 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000432 llvm::Value *getCriticalRegionLock(StringRef CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000433
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000434public:
435 explicit CGOpenMPRuntime(CodeGenModule &CGM);
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000436 virtual ~CGOpenMPRuntime() {}
Alexey Bataev91797552015-03-18 04:13:55 +0000437 virtual void clear();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000438
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000439 /// Emit code for the specified user defined reduction construct.
440 virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
441 const OMPDeclareReductionDecl *D);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000442 /// Get combiner/initializer for the specified user-defined reduction, if any.
443 virtual std::pair<llvm::Function *, llvm::Function *>
444 getUserDefinedReduction(const OMPDeclareReductionDecl *D);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000445 /// \brief Emits outlined function for the specified OpenMP parallel directive
446 /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
447 /// kmp_int32 BoundID, struct context_vars*).
Alexey Bataev18095712014-10-10 12:19:54 +0000448 /// \param D OpenMP directive.
449 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000450 /// \param InnermostKind Kind of innermost directive (for simple directives it
451 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000452 /// \param CodeGen Code generation sequence for the \a D directive.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000453 virtual llvm::Value *emitParallelOrTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000454 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
455 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev18095712014-10-10 12:19:54 +0000456
Alexey Bataev62b63b12015-03-10 07:28:44 +0000457 /// \brief Emits outlined function for the OpenMP task directive \a D. This
458 /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
459 /// PartID, struct context_vars*).
460 /// \param D OpenMP directive.
461 /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000462 /// \param InnermostKind Kind of innermost directive (for simple directives it
463 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000464 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000465 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000466 virtual llvm::Value *emitTaskOutlinedFunction(
467 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
468 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000469
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000470 /// \brief Cleans up references to the objects in finished function.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000471 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000472 void functionFinished(CodeGenFunction &CGF);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000473
Alexey Bataev1d677132015-04-22 13:57:31 +0000474 /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
475 /// variables captured in a record which address is stored in \a
476 /// CapturedStruct.
Alexey Bataev18095712014-10-10 12:19:54 +0000477 /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
Alexey Bataev62b63b12015-03-10 07:28:44 +0000478 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
NAKAMURA Takumi62f0eb52015-09-11 08:13:32 +0000479 /// \param CapturedVars A pointer to the record with the references to
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000480 /// variables used in \a OutlinedFn function.
Alexey Bataev1d677132015-04-22 13:57:31 +0000481 /// \param IfCond Condition in the associated 'if' clause, if it was
482 /// specified, nullptr otherwise.
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000483 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000484 virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
485 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +0000486 ArrayRef<llvm::Value *> CapturedVars,
487 const Expr *IfCond);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000488
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000489 /// \brief Emits a critical region.
Alexey Bataev18095712014-10-10 12:19:54 +0000490 /// \param CriticalName Name of the critical region.
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000491 /// \param CriticalOpGen Generator for the statement associated with the given
492 /// critical region.
Alexey Bataevfc57d162015-12-15 10:55:09 +0000493 /// \param Hint Value of the 'hint' clause (optional).
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000494 virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000495 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +0000496 SourceLocation Loc,
497 const Expr *Hint = nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000498
Alexey Bataev8d690652014-12-04 07:23:53 +0000499 /// \brief Emits a master region.
500 /// \param MasterOpGen Generator for the statement associated with the given
501 /// master region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000502 virtual void emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000503 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000504 SourceLocation Loc);
Alexey Bataev8d690652014-12-04 07:23:53 +0000505
Alexey Bataev9f797f32015-02-05 05:57:51 +0000506 /// \brief Emits code for a taskyield directive.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000507 virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000508
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000509 /// \brief Emit a taskgroup region.
510 /// \param TaskgroupOpGen Generator for the statement associated with the
511 /// given taskgroup region.
512 virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
513 const RegionCodeGenTy &TaskgroupOpGen,
514 SourceLocation Loc);
515
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000516 /// \brief Emits a single region.
517 /// \param SingleOpGen Generator for the statement associated with the given
518 /// single region.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000519 virtual void emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000520 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000521 SourceLocation Loc,
522 ArrayRef<const Expr *> CopyprivateVars,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000523 ArrayRef<const Expr *> DestExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000524 ArrayRef<const Expr *> SrcExprs,
Alexey Bataeva63048e2015-03-23 06:18:07 +0000525 ArrayRef<const Expr *> AssignmentOps);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000526
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000527 /// \brief Emit an ordered region.
528 /// \param OrderedOpGen Generator for the statement associated with the given
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000529 /// ordered region.
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000530 virtual void emitOrderedRegion(CodeGenFunction &CGF,
531 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +0000532 SourceLocation Loc, bool IsThreads);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000533
Alexey Bataevf2685682015-03-30 04:30:22 +0000534 /// \brief Emit an implicit/explicit barrier for OpenMP threads.
535 /// \param Kind Directive for which this implicit barrier call must be
536 /// generated. Must be OMPD_barrier for explicit barrier generation.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000537 /// \param EmitChecks true if need to emit checks for cancellation barriers.
538 /// \param ForceSimpleCall true simple barrier call must be emitted, false if
539 /// runtime class decides which one to emit (simple or with cancellation
540 /// checks).
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000541 ///
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000542 virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000543 OpenMPDirectiveKind Kind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000544 bool EmitChecks = true,
545 bool ForceSimpleCall = false);
Alexey Bataevb2059782014-10-13 08:23:51 +0000546
Alexander Musmanc6388682014-12-15 07:07:06 +0000547 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
548 /// This kind of worksharing directive is emitted without outer loop.
549 /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
550 /// \param Chunked True if chunk is specified in the clause.
551 ///
552 virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
553 bool Chunked) const;
554
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000555 /// \brief Check if the specified \a ScheduleKind is static non-chunked.
556 /// This kind of distribute directive is emitted without outer loop.
557 /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
558 /// \param Chunked True if chunk is specified in the clause.
559 ///
560 virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
561 bool Chunked) const;
562
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000563 /// \brief Check if the specified \a ScheduleKind is dynamic.
564 /// This kind of worksharing directive is emitted without outer loop.
565 /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
566 ///
567 virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
568
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +0000569 virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
570 OpenMPScheduleClauseKind SchedKind,
571 unsigned IVSize, bool IVSigned,
572 bool Ordered, llvm::Value *UB,
573 llvm::Value *Chunk = nullptr);
574
Alexander Musmanc6388682014-12-15 07:07:06 +0000575 /// \brief Call the appropriate runtime routine to initialize it before start
576 /// of loop.
577 ///
578 /// Depending on the loop schedule, it is nesessary to call some runtime
579 /// routine before start of the OpenMP loop to get the loop upper / lower
580 /// bounds \a LB and \a UB and stride \a ST.
581 ///
582 /// \param CGF Reference to current CodeGenFunction.
583 /// \param Loc Clang source location.
NAKAMURA Takumieca08382014-12-17 14:47:06 +0000584 /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
Alexander Musmanc6388682014-12-15 07:07:06 +0000585 /// \param IVSize Size of the iteration variable in bits.
586 /// \param IVSigned Sign of the interation variable.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000587 /// \param Ordered true if loop is ordered, false otherwise.
Alexander Musmanc6388682014-12-15 07:07:06 +0000588 /// \param IL Address of the output variable in which the flag of the
589 /// last iteration is returned.
590 /// \param LB Address of the output variable in which the lower iteration
591 /// number is returned.
592 /// \param UB Address of the output variable in which the upper iteration
593 /// number is returned.
594 /// \param ST Address of the output variable in which the stride value is
595 /// returned nesessary to generated the static_chunked scheduled loop.
596 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
597 /// For the default (nullptr) value, the chunk 1 will be used.
598 ///
John McCall7f416cc2015-09-08 08:05:57 +0000599 virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
600 OpenMPScheduleClauseKind SchedKind,
601 unsigned IVSize, bool IVSigned, bool Ordered,
602 Address IL, Address LB,
603 Address UB, Address ST,
604 llvm::Value *Chunk = nullptr);
Alexander Musmanc6388682014-12-15 07:07:06 +0000605
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000606 ///
607 /// \param CGF Reference to current CodeGenFunction.
608 /// \param Loc Clang source location.
609 /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
610 /// \param IVSize Size of the iteration variable in bits.
611 /// \param IVSigned Sign of the interation variable.
612 /// \param Ordered true if loop is ordered, false otherwise.
613 /// \param IL Address of the output variable in which the flag of the
614 /// last iteration is returned.
615 /// \param LB Address of the output variable in which the lower iteration
616 /// number is returned.
617 /// \param UB Address of the output variable in which the upper iteration
618 /// number is returned.
619 /// \param ST Address of the output variable in which the stride value is
620 /// returned nesessary to generated the static_chunked scheduled loop.
621 /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
622 /// For the default (nullptr) value, the chunk 1 will be used.
623 ///
624 virtual void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
625 OpenMPDistScheduleClauseKind SchedKind,
626 unsigned IVSize, bool IVSigned,
627 bool Ordered, Address IL, Address LB,
628 Address UB, Address ST,
629 llvm::Value *Chunk = nullptr);
630
Alexander Musmanc6388682014-12-15 07:07:06 +0000631 /// \brief Call the appropriate runtime routine to notify that we finished
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000632 /// iteration of the ordered loop with the dynamic scheduling.
633 ///
634 /// \param CGF Reference to current CodeGenFunction.
635 /// \param Loc Clang source location.
636 /// \param IVSize Size of the iteration variable in bits.
637 /// \param IVSigned Sign of the interation variable.
638 ///
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000639 virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
640 SourceLocation Loc, unsigned IVSize,
641 bool IVSigned);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000642
643 /// \brief Call the appropriate runtime routine to notify that we finished
Alexander Musmanc6388682014-12-15 07:07:06 +0000644 /// all the work with current loop.
645 ///
646 /// \param CGF Reference to current CodeGenFunction.
647 /// \param Loc Clang source location.
Alexander Musmanc6388682014-12-15 07:07:06 +0000648 ///
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000649 virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
Alexander Musmanc6388682014-12-15 07:07:06 +0000650
Alexander Musman92bdaab2015-03-12 13:37:50 +0000651 /// Call __kmpc_dispatch_next(
652 /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
653 /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
654 /// kmp_int[32|64] *p_stride);
655 /// \param IVSize Size of the iteration variable in bits.
656 /// \param IVSigned Sign of the interation variable.
657 /// \param IL Address of the output variable in which the flag of the
658 /// last iteration is returned.
659 /// \param LB Address of the output variable in which the lower iteration
660 /// number is returned.
661 /// \param UB Address of the output variable in which the upper iteration
662 /// number is returned.
663 /// \param ST Address of the output variable in which the stride value is
664 /// returned.
665 virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
666 unsigned IVSize, bool IVSigned,
John McCall7f416cc2015-09-08 08:05:57 +0000667 Address IL, Address LB,
668 Address UB, Address ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000669
Alexey Bataevb2059782014-10-13 08:23:51 +0000670 /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
671 /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
672 /// clause.
673 /// \param NumThreads An integer value of threads.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000674 virtual void emitNumThreadsClause(CodeGenFunction &CGF,
675 llvm::Value *NumThreads,
676 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000677
Alexey Bataev7f210c62015-06-18 13:40:03 +0000678 /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
679 /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
680 virtual void emitProcBindClause(CodeGenFunction &CGF,
681 OpenMPProcBindClauseKind ProcBind,
682 SourceLocation Loc);
683
Alexey Bataev97720002014-11-11 04:05:39 +0000684 /// \brief Returns address of the threadprivate variable for the current
685 /// thread.
NAKAMURA Takumicdcbfba2014-11-11 07:58:06 +0000686 /// \param VD Threadprivate variable.
Alexey Bataev97720002014-11-11 04:05:39 +0000687 /// \param VDAddr Address of the global variable \a VD.
688 /// \param Loc Location of the reference to threadprivate var.
689 /// \return Address of the threadprivate variable for the current thread.
John McCall7f416cc2015-09-08 08:05:57 +0000690 virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
691 const VarDecl *VD,
692 Address VDAddr,
693 SourceLocation Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000694
695 /// \brief Emit a code for initialization of threadprivate variable. It emits
696 /// a call to runtime library which adds initial value to the newly created
697 /// threadprivate variable (if it is not constant) and registers destructor
698 /// for the variable (if any).
699 /// \param VD Threadprivate variable.
700 /// \param VDAddr Address of the global variable \a VD.
701 /// \param Loc Location of threadprivate declaration.
702 /// \param PerformInit true if initialization expression is not constant.
703 virtual llvm::Function *
John McCall7f416cc2015-09-08 08:05:57 +0000704 emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000705 SourceLocation Loc, bool PerformInit,
706 CodeGenFunction *CGF = nullptr);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000707
708 /// \brief Emit flush of the variables specified in 'omp flush' directive.
709 /// \param Vars List of variables to flush.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000710 virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
711 SourceLocation Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000712
713 /// \brief Emit task region for the task directive. The task region is
Nico Weber20b0ce32015-04-28 18:19:18 +0000714 /// emitted in several steps:
Alexey Bataev62b63b12015-03-10 07:28:44 +0000715 /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
716 /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
717 /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
718 /// function:
719 /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
720 /// TaskFunction(gtid, tt->part_id, tt->shareds);
721 /// return 0;
722 /// }
723 /// 2. Copy a list of shared variables to field shareds of the resulting
724 /// structure kmp_task_t returned by the previous call (if any).
725 /// 3. Copy a pointer to destructions function to field destructions of the
726 /// resulting structure kmp_task_t.
727 /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
728 /// kmp_task_t *new_task), where new_task is a resulting structure from
729 /// previous items.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000730 /// \param D Current task directive.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000731 /// \param Tied true if the task is tied (the task is tied to the thread that
732 /// can suspend its task region), false - untied (the task is not tied to any
733 /// thread).
734 /// \param Final Contains either constant bool value, or llvm::Value * of i1
735 /// type for final clause. If the value is true, the task forces all of its
736 /// child tasks to become final and included tasks.
737 /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
738 /// /*part_id*/, captured_struct */*__context*/);
739 /// \param SharedsTy A type which contains references the shared variables.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000740 /// \param Shareds Context with the list of shared variables from the \p
Alexey Bataev62b63b12015-03-10 07:28:44 +0000741 /// TaskFunction.
Alexey Bataev1d677132015-04-22 13:57:31 +0000742 /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
743 /// otherwise.
Alexey Bataev36c1eb92015-04-30 06:51:57 +0000744 /// \param PrivateVars List of references to private variables for the task
745 /// directive.
746 /// \param PrivateCopies List of private copies for each private variable in
Alexey Bataev9e034042015-05-05 04:05:12 +0000747 /// \p PrivateVars.
748 /// \param FirstprivateVars List of references to private variables for the
749 /// task directive.
750 /// \param FirstprivateCopies List of private copies for each private variable
751 /// in \p FirstprivateVars.
752 /// \param FirstprivateInits List of references to auto generated variables
753 /// used for initialization of a single array element. Used if firstprivate
754 /// variable is of array type.
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000755 /// \param Dependences List of dependences for the 'task' construct, including
756 /// original expression and dependency type.
757 virtual void emitTaskCall(
758 CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
759 bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
John McCall7f416cc2015-09-08 08:05:57 +0000760 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
Alexey Bataev1d2353d2015-06-24 11:01:36 +0000761 const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
762 ArrayRef<const Expr *> PrivateCopies,
763 ArrayRef<const Expr *> FirstprivateVars,
764 ArrayRef<const Expr *> FirstprivateCopies,
765 ArrayRef<const Expr *> FirstprivateInits,
766 ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000767
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000768 /// \brief Emit code for the directive that does not require outlining.
769 ///
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000770 /// \param InnermostKind Kind of innermost directive (for simple directives it
771 /// is a directive itself, for combined - its innermost directive).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000772 /// \param CodeGen Code generation sequence for the \a D directive.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000773 /// \param HasCancel true if region has inner cancel directive, false
774 /// otherwise.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000775 virtual void emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000776 OpenMPDirectiveKind InnermostKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000777 const RegionCodeGenTy &CodeGen,
778 bool HasCancel = false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000779 /// \brief Emit a code for reduction clause. Next code should be emitted for
780 /// reduction:
781 /// \code
782 ///
783 /// static kmp_critical_name lock = { 0 };
784 ///
785 /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
786 /// ...
787 /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
788 /// ...
789 /// }
790 ///
791 /// ...
792 /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
793 /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
794 /// RedList, reduce_func, &<lock>)) {
795 /// case 1:
796 /// ...
797 /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
798 /// ...
799 /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
800 /// break;
801 /// case 2:
802 /// ...
803 /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
804 /// ...
805 /// break;
806 /// default:;
807 /// }
808 /// \endcode
809 ///
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000810 /// \param Privates List of private copies for original reduction arguments.
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000811 /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
812 /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
813 /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
814 /// or 'operator binop(LHS, RHS)'.
815 /// \param WithNowait true if parent directive has also nowait clause, false
816 /// otherwise.
817 virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000818 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000819 ArrayRef<const Expr *> LHSExprs,
820 ArrayRef<const Expr *> RHSExprs,
821 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000822 bool WithNowait, bool SimpleReduction);
Alexey Bataev8b8e2022015-04-27 05:22:09 +0000823
824 /// \brief Emit code for 'taskwait' directive.
825 virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
Alexey Bataev0f34da12015-07-02 04:17:07 +0000826
827 /// \brief Emit code for 'cancellation point' construct.
828 /// \param CancelRegion Region kind for which the cancellation point must be
829 /// emitted.
830 ///
831 virtual void emitCancellationPointCall(CodeGenFunction &CGF,
832 SourceLocation Loc,
833 OpenMPDirectiveKind CancelRegion);
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000834
835 /// \brief Emit code for 'cancel' construct.
Alexey Bataev87933c72015-09-18 08:07:34 +0000836 /// \param IfCond Condition in the associated 'if' clause, if it was
837 /// specified, nullptr otherwise.
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000838 /// \param CancelRegion Region kind for which the cancel must be emitted.
839 ///
840 virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +0000841 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +0000842 OpenMPDirectiveKind CancelRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +0000843
844 /// \brief Emit outilined function for 'target' directive.
845 /// \param D Directive to emit.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000846 /// \param ParentName Name of the function that encloses the target region.
847 /// \param OutlinedFn Outlined function value to be defined by this call.
848 /// \param OutlinedFnID Outlined function ID value to be defined by this call.
849 /// \param IsOffloadEntry True if the outlined function is an offload entry.
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000850 /// \param CodeGen Code generation sequence for the \a D directive.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000851 /// An oulined function may not be an entry if, e.g. the if clause always
852 /// evaluates to false.
853 virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
854 StringRef ParentName,
855 llvm::Function *&OutlinedFn,
856 llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000857 bool IsOffloadEntry,
858 const RegionCodeGenTy &CodeGen);
Samuel Antaobed3c462015-10-02 16:14:20 +0000859
860 /// \brief Emit the target offloading code associated with \a D. The emitted
861 /// code attempts offloading the execution to the device, an the event of
862 /// a failure it executes the host version outlined in \a OutlinedFn.
863 /// \param D Directive to emit.
864 /// \param OutlinedFn Host version of the code to be offloaded.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000865 /// \param OutlinedFnID ID of host version of the code to be offloaded.
Samuel Antaobed3c462015-10-02 16:14:20 +0000866 /// \param IfCond Expression evaluated in if clause associated with the target
867 /// directive, or null if no if clause is used.
868 /// \param Device Expression evaluated in device clause associated with the
869 /// target directive, or null if no device clause is used.
870 /// \param CapturedVars Values captured in the current region.
871 virtual void emitTargetCall(CodeGenFunction &CGF,
872 const OMPExecutableDirective &D,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000873 llvm::Value *OutlinedFn,
874 llvm::Value *OutlinedFnID, const Expr *IfCond,
Samuel Antaobed3c462015-10-02 16:14:20 +0000875 const Expr *Device,
876 ArrayRef<llvm::Value *> CapturedVars);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000877
878 /// \brief Emit the target regions enclosed in \a GD function definition or
879 /// the function itself in case it is a valid device function. Returns true if
880 /// \a GD was dealt with successfully.
Nico Webera2abe8c2016-01-06 19:13:49 +0000881 /// \param GD Function to scan.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000882 virtual bool emitTargetFunctions(GlobalDecl GD);
883
884 /// \brief Emit the global variable if it is a valid device global variable.
885 /// Returns true if \a GD was dealt with successfully.
886 /// \param GD Variable declaration to emit.
887 virtual bool emitTargetGlobalVariable(GlobalDecl GD);
888
889 /// \brief Emit the global \a GD if it is meaningful for the target. Returns
890 /// if it was emitted succesfully.
891 /// \param GD Global to scan.
892 virtual bool emitTargetGlobal(GlobalDecl GD);
893
894 /// \brief Creates the offloading descriptor in the event any target region
895 /// was emitted in the current module and return the function that registers
896 /// it.
897 virtual llvm::Function *emitRegistrationFunction();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000898
899 /// \brief Emits code for teams call of the \a OutlinedFn with
900 /// variables captured in a record which address is stored in \a
901 /// CapturedStruct.
902 /// \param OutlinedFn Outlined function to be run by team masters. Type of
903 /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
904 /// \param CapturedVars A pointer to the record with the references to
905 /// variables used in \a OutlinedFn function.
906 ///
907 virtual void emitTeamsCall(CodeGenFunction &CGF,
908 const OMPExecutableDirective &D,
909 SourceLocation Loc, llvm::Value *OutlinedFn,
910 ArrayRef<llvm::Value *> CapturedVars);
911
912 /// \brief Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
913 /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
914 /// for num_teams clause.
915 /// \param NumTeams An integer value of teams.
Simon Pilgrim1feae0d2016-03-05 22:35:55 +0000916 /// \param ThreadLimit An integer value of threads.
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000917 virtual void emitNumTeamsClause(CodeGenFunction &CGF, llvm::Value *NumTeams,
918 llvm::Value *ThreadLimit, SourceLocation Loc);
919
Alexey Bataev9959db52014-05-06 10:08:46 +0000920};
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000921
Alexey Bataev23b69422014-06-18 07:08:49 +0000922} // namespace CodeGen
923} // namespace clang
Alexey Bataev9959db52014-05-06 10:08:46 +0000924
925#endif