CGOpenMPRuntime.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. //===----- 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. #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
  14. #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
  15. #include "clang/AST/Type.h"
  16. #include "clang/Basic/OpenMPKinds.h"
  17. #include "clang/Basic/SourceLocation.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/DenseSet.h"
  20. #include "llvm/ADT/StringMap.h"
  21. #include "llvm/IR/ValueHandle.h"
  22. namespace llvm {
  23. class ArrayType;
  24. class Constant;
  25. class Function;
  26. class FunctionType;
  27. class GlobalVariable;
  28. class StructType;
  29. class Type;
  30. class Value;
  31. } // namespace llvm
  32. namespace clang {
  33. class Expr;
  34. class OMPExecutableDirective;
  35. class VarDecl;
  36. namespace CodeGen {
  37. class CodeGenFunction;
  38. class CodeGenModule;
  39. typedef llvm::function_ref<void(CodeGenFunction &)> RegionCodeGenTy;
  40. class CGOpenMPRuntime {
  41. private:
  42. enum OpenMPRTLFunction {
  43. /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
  44. /// kmpc_micro microtask, ...);
  45. OMPRTL__kmpc_fork_call,
  46. /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
  47. /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
  48. OMPRTL__kmpc_threadprivate_cached,
  49. /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
  50. /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
  51. OMPRTL__kmpc_threadprivate_register,
  52. // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
  53. OMPRTL__kmpc_global_thread_num,
  54. // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
  55. // kmp_critical_name *crit);
  56. OMPRTL__kmpc_critical,
  57. // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
  58. // kmp_critical_name *crit);
  59. OMPRTL__kmpc_end_critical,
  60. // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
  61. // global_tid);
  62. OMPRTL__kmpc_cancel_barrier,
  63. // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
  64. OMPRTL__kmpc_barrier,
  65. // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
  66. OMPRTL__kmpc_for_static_fini,
  67. // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
  68. // global_tid);
  69. OMPRTL__kmpc_serialized_parallel,
  70. // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
  71. // global_tid);
  72. OMPRTL__kmpc_end_serialized_parallel,
  73. // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
  74. // kmp_int32 num_threads);
  75. OMPRTL__kmpc_push_num_threads,
  76. // Call to void __kmpc_flush(ident_t *loc);
  77. OMPRTL__kmpc_flush,
  78. // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
  79. OMPRTL__kmpc_master,
  80. // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
  81. OMPRTL__kmpc_end_master,
  82. // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
  83. // int end_part);
  84. OMPRTL__kmpc_omp_taskyield,
  85. // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
  86. OMPRTL__kmpc_single,
  87. // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
  88. OMPRTL__kmpc_end_single,
  89. // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
  90. // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
  91. // kmp_routine_entry_t *task_entry);
  92. OMPRTL__kmpc_omp_task_alloc,
  93. // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t *
  94. // new_task);
  95. OMPRTL__kmpc_omp_task,
  96. // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
  97. // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
  98. // kmp_int32 didit);
  99. OMPRTL__kmpc_copyprivate,
  100. // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
  101. // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
  102. // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
  103. OMPRTL__kmpc_reduce,
  104. // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
  105. // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
  106. // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
  107. // *lck);
  108. OMPRTL__kmpc_reduce_nowait,
  109. // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
  110. // kmp_critical_name *lck);
  111. OMPRTL__kmpc_end_reduce,
  112. // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
  113. // kmp_critical_name *lck);
  114. OMPRTL__kmpc_end_reduce_nowait,
  115. // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
  116. // kmp_task_t * new_task);
  117. OMPRTL__kmpc_omp_task_begin_if0,
  118. // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
  119. // kmp_task_t * new_task);
  120. OMPRTL__kmpc_omp_task_complete_if0,
  121. // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
  122. OMPRTL__kmpc_ordered,
  123. // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
  124. OMPRTL__kmpc_end_ordered,
  125. // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
  126. // global_tid);
  127. OMPRTL__kmpc_omp_taskwait,
  128. // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
  129. OMPRTL__kmpc_taskgroup,
  130. // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
  131. OMPRTL__kmpc_end_taskgroup,
  132. // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
  133. // int proc_bind);
  134. OMPRTL__kmpc_push_proc_bind,
  135. // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32
  136. // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t
  137. // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
  138. OMPRTL__kmpc_omp_task_with_deps,
  139. // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32
  140. // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
  141. // ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
  142. OMPRTL__kmpc_omp_wait_deps,
  143. // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
  144. // global_tid, kmp_int32 cncl_kind);
  145. OMPRTL__kmpc_cancellationpoint,
  146. // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
  147. // kmp_int32 cncl_kind);
  148. OMPRTL__kmpc_cancel,
  149. };
  150. /// \brief Values for bit flags used in the ident_t to describe the fields.
  151. /// All enumeric elements are named and described in accordance with the code
  152. /// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
  153. enum OpenMPLocationFlags {
  154. /// \brief Use trampoline for internal microtask.
  155. OMP_IDENT_IMD = 0x01,
  156. /// \brief Use c-style ident structure.
  157. OMP_IDENT_KMPC = 0x02,
  158. /// \brief Atomic reduction option for kmpc_reduce.
  159. OMP_ATOMIC_REDUCE = 0x10,
  160. /// \brief Explicit 'barrier' directive.
  161. OMP_IDENT_BARRIER_EXPL = 0x20,
  162. /// \brief Implicit barrier in code.
  163. OMP_IDENT_BARRIER_IMPL = 0x40,
  164. /// \brief Implicit barrier in 'for' directive.
  165. OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
  166. /// \brief Implicit barrier in 'sections' directive.
  167. OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
  168. /// \brief Implicit barrier in 'single' directive.
  169. OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
  170. };
  171. CodeGenModule &CGM;
  172. /// \brief Default const ident_t object used for initialization of all other
  173. /// ident_t objects.
  174. llvm::Constant *DefaultOpenMPPSource;
  175. /// \brief Map of flags and corresponding default locations.
  176. typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
  177. OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
  178. llvm::Value *getOrCreateDefaultLocation(OpenMPLocationFlags Flags);
  179. /// \brief Describes ident structure that describes a source location.
  180. /// All descriptions are taken from
  181. /// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
  182. /// Original structure:
  183. /// typedef struct ident {
  184. /// kmp_int32 reserved_1; /**< might be used in Fortran;
  185. /// see above */
  186. /// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
  187. /// KMP_IDENT_KMPC identifies this union
  188. /// member */
  189. /// kmp_int32 reserved_2; /**< not really used in Fortran any more;
  190. /// see above */
  191. ///#if USE_ITT_BUILD
  192. /// /* but currently used for storing
  193. /// region-specific ITT */
  194. /// /* contextual information. */
  195. ///#endif /* USE_ITT_BUILD */
  196. /// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
  197. /// C++ */
  198. /// char const *psource; /**< String describing the source location.
  199. /// The string is composed of semi-colon separated
  200. // fields which describe the source file,
  201. /// the function and a pair of line numbers that
  202. /// delimit the construct.
  203. /// */
  204. /// } ident_t;
  205. enum IdentFieldIndex {
  206. /// \brief might be used in Fortran
  207. IdentField_Reserved_1,
  208. /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
  209. IdentField_Flags,
  210. /// \brief Not really used in Fortran any more
  211. IdentField_Reserved_2,
  212. /// \brief Source[4] in Fortran, do not use for C++
  213. IdentField_Reserved_3,
  214. /// \brief String describing the source location. The string is composed of
  215. /// semi-colon separated fields which describe the source file, the function
  216. /// and a pair of line numbers that delimit the construct.
  217. IdentField_PSource
  218. };
  219. llvm::StructType *IdentTy;
  220. /// \brief Map for SourceLocation and OpenMP runtime library debug locations.
  221. typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDebugLocMapTy;
  222. OpenMPDebugLocMapTy OpenMPDebugLocMap;
  223. /// \brief The type for a microtask which gets passed to __kmpc_fork_call().
  224. /// Original representation is:
  225. /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
  226. llvm::FunctionType *Kmpc_MicroTy;
  227. /// \brief Stores debug location and ThreadID for the function.
  228. struct DebugLocThreadIdTy {
  229. llvm::Value *DebugLoc;
  230. llvm::Value *ThreadID;
  231. };
  232. /// \brief Map of local debug location, ThreadId and functions.
  233. typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
  234. OpenMPLocThreadIDMapTy;
  235. OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
  236. /// \brief Type kmp_critical_name, originally defined as typedef kmp_int32
  237. /// kmp_critical_name[8];
  238. llvm::ArrayType *KmpCriticalNameTy;
  239. /// \brief An ordered map of auto-generated variables to their unique names.
  240. /// It stores variables with the following names: 1) ".gomp_critical_user_" +
  241. /// <critical_section_name> + ".var" for "omp critical" directives; 2)
  242. /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
  243. /// variables.
  244. llvm::StringMap<llvm::AssertingVH<llvm::Constant>, llvm::BumpPtrAllocator>
  245. InternalVars;
  246. /// \brief Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
  247. llvm::Type *KmpRoutineEntryPtrTy;
  248. QualType KmpRoutineEntryPtrQTy;
  249. /// \brief Type typedef struct kmp_task {
  250. /// void * shareds; /**< pointer to block of pointers to
  251. /// shared vars */
  252. /// kmp_routine_entry_t routine; /**< pointer to routine to call for
  253. /// executing task */
  254. /// kmp_int32 part_id; /**< part id for the task */
  255. /// kmp_routine_entry_t destructors; /* pointer to function to invoke
  256. /// deconstructors of firstprivate C++ objects */
  257. /// } kmp_task_t;
  258. QualType KmpTaskTQTy;
  259. /// \brief Type typedef struct kmp_depend_info {
  260. /// kmp_intptr_t base_addr;
  261. /// size_t len;
  262. /// struct {
  263. /// bool in:1;
  264. /// bool out:1;
  265. /// } flags;
  266. /// } kmp_depend_info_t;
  267. QualType KmpDependInfoTy;
  268. /// \brief Build type kmp_routine_entry_t (if not built yet).
  269. void emitKmpRoutineEntryT(QualType KmpInt32Ty);
  270. /// \brief Emits object of ident_t type with info for source location.
  271. /// \param Flags Flags for OpenMP location.
  272. ///
  273. llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
  274. OpenMPLocationFlags Flags = OMP_IDENT_KMPC);
  275. /// \brief Returns pointer to ident_t type.
  276. llvm::Type *getIdentTyPointerTy();
  277. /// \brief Returns pointer to kmpc_micro type.
  278. llvm::Type *getKmpc_MicroPointerTy();
  279. /// \brief Returns specified OpenMP runtime function.
  280. /// \param Function OpenMP runtime function.
  281. /// \return Specified function.
  282. llvm::Constant *createRuntimeFunction(OpenMPRTLFunction Function);
  283. /// \brief Returns __kmpc_for_static_init_* runtime function for the specified
  284. /// size \a IVSize and sign \a IVSigned.
  285. llvm::Constant *createForStaticInitFunction(unsigned IVSize, bool IVSigned);
  286. /// \brief Returns __kmpc_dispatch_init_* runtime function for the specified
  287. /// size \a IVSize and sign \a IVSigned.
  288. llvm::Constant *createDispatchInitFunction(unsigned IVSize, bool IVSigned);
  289. /// \brief Returns __kmpc_dispatch_next_* runtime function for the specified
  290. /// size \a IVSize and sign \a IVSigned.
  291. llvm::Constant *createDispatchNextFunction(unsigned IVSize, bool IVSigned);
  292. /// \brief Returns __kmpc_dispatch_fini_* runtime function for the specified
  293. /// size \a IVSize and sign \a IVSigned.
  294. llvm::Constant *createDispatchFiniFunction(unsigned IVSize, bool IVSigned);
  295. /// \brief If the specified mangled name is not in the module, create and
  296. /// return threadprivate cache object. This object is a pointer's worth of
  297. /// storage that's reserved for use by the OpenMP runtime.
  298. /// \param VD Threadprivate variable.
  299. /// \return Cache variable for the specified threadprivate.
  300. llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
  301. /// \brief Emits address of the word in a memory where current thread id is
  302. /// stored.
  303. virtual llvm::Value *emitThreadIDAddress(CodeGenFunction &CGF,
  304. SourceLocation Loc);
  305. /// \brief Gets thread id value for the current thread.
  306. ///
  307. llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
  308. /// \brief Gets (if variable with the given name already exist) or creates
  309. /// internal global variable with the specified Name. The created variable has
  310. /// linkage CommonLinkage by default and is initialized by null value.
  311. /// \param Ty Type of the global variable. If it is exist already the type
  312. /// must be the same.
  313. /// \param Name Name of the variable.
  314. llvm::Constant *getOrCreateInternalVariable(llvm::Type *Ty,
  315. const llvm::Twine &Name);
  316. /// \brief Set of threadprivate variables with the generated initializer.
  317. llvm::DenseSet<const VarDecl *> ThreadPrivateWithDefinition;
  318. /// \brief Emits initialization code for the threadprivate variables.
  319. /// \param VDAddr Address of the global variable \a VD.
  320. /// \param Ctor Pointer to a global init function for \a VD.
  321. /// \param CopyCtor Pointer to a global copy function for \a VD.
  322. /// \param Dtor Pointer to a global destructor function for \a VD.
  323. /// \param Loc Location of threadprivate declaration.
  324. void emitThreadPrivateVarInit(CodeGenFunction &CGF, llvm::Value *VDAddr,
  325. llvm::Value *Ctor, llvm::Value *CopyCtor,
  326. llvm::Value *Dtor, SourceLocation Loc);
  327. /// \brief Returns corresponding lock object for the specified critical region
  328. /// name. If the lock object does not exist it is created, otherwise the
  329. /// reference to the existing copy is returned.
  330. /// \param CriticalName Name of the critical region.
  331. ///
  332. llvm::Value *getCriticalRegionLock(StringRef CriticalName);
  333. public:
  334. explicit CGOpenMPRuntime(CodeGenModule &CGM);
  335. virtual ~CGOpenMPRuntime() {}
  336. virtual void clear();
  337. /// \brief Emits outlined function for the specified OpenMP parallel directive
  338. /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
  339. /// kmp_int32 BoundID, struct context_vars*).
  340. /// \param D OpenMP directive.
  341. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  342. /// \param InnermostKind Kind of innermost directive (for simple directives it
  343. /// is a directive itself, for combined - its innermost directive).
  344. /// \param CodeGen Code generation sequence for the \a D directive.
  345. virtual llvm::Value *emitParallelOutlinedFunction(
  346. const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
  347. OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
  348. /// \brief Emits outlined function for the OpenMP task directive \a D. This
  349. /// outlined function has type void(*)(kmp_int32 ThreadID, kmp_int32
  350. /// PartID, struct context_vars*).
  351. /// \param D OpenMP directive.
  352. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  353. /// \param InnermostKind Kind of innermost directive (for simple directives it
  354. /// is a directive itself, for combined - its innermost directive).
  355. /// \param CodeGen Code generation sequence for the \a D directive.
  356. ///
  357. virtual llvm::Value *emitTaskOutlinedFunction(
  358. const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
  359. OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
  360. /// \brief Cleans up references to the objects in finished function.
  361. ///
  362. void functionFinished(CodeGenFunction &CGF);
  363. /// \brief Emits code for parallel or serial call of the \a OutlinedFn with
  364. /// variables captured in a record which address is stored in \a
  365. /// CapturedStruct.
  366. /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
  367. /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
  368. /// \param CapturedStruct A pointer to the record with the references to
  369. /// variables used in \a OutlinedFn function.
  370. /// \param IfCond Condition in the associated 'if' clause, if it was
  371. /// specified, nullptr otherwise.
  372. ///
  373. virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
  374. llvm::Value *OutlinedFn,
  375. llvm::Value *CapturedStruct,
  376. const Expr *IfCond);
  377. /// \brief Emits a critical region.
  378. /// \param CriticalName Name of the critical region.
  379. /// \param CriticalOpGen Generator for the statement associated with the given
  380. /// critical region.
  381. virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
  382. const RegionCodeGenTy &CriticalOpGen,
  383. SourceLocation Loc);
  384. /// \brief Emits a master region.
  385. /// \param MasterOpGen Generator for the statement associated with the given
  386. /// master region.
  387. virtual void emitMasterRegion(CodeGenFunction &CGF,
  388. const RegionCodeGenTy &MasterOpGen,
  389. SourceLocation Loc);
  390. /// \brief Emits code for a taskyield directive.
  391. virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
  392. /// \brief Emit a taskgroup region.
  393. /// \param TaskgroupOpGen Generator for the statement associated with the
  394. /// given taskgroup region.
  395. virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
  396. const RegionCodeGenTy &TaskgroupOpGen,
  397. SourceLocation Loc);
  398. /// \brief Emits a single region.
  399. /// \param SingleOpGen Generator for the statement associated with the given
  400. /// single region.
  401. virtual void emitSingleRegion(CodeGenFunction &CGF,
  402. const RegionCodeGenTy &SingleOpGen,
  403. SourceLocation Loc,
  404. ArrayRef<const Expr *> CopyprivateVars,
  405. ArrayRef<const Expr *> DestExprs,
  406. ArrayRef<const Expr *> SrcExprs,
  407. ArrayRef<const Expr *> AssignmentOps);
  408. /// \brief Emit an ordered region.
  409. /// \param OrderedOpGen Generator for the statement associated with the given
  410. /// ordered region.
  411. virtual void emitOrderedRegion(CodeGenFunction &CGF,
  412. const RegionCodeGenTy &OrderedOpGen,
  413. SourceLocation Loc);
  414. /// \brief Emit an implicit/explicit barrier for OpenMP threads.
  415. /// \param Kind Directive for which this implicit barrier call must be
  416. /// generated. Must be OMPD_barrier for explicit barrier generation.
  417. /// \param CheckForCancel true if check for possible cancellation must be
  418. /// performed, false otherwise.
  419. ///
  420. virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
  421. OpenMPDirectiveKind Kind,
  422. bool CheckForCancel = true);
  423. /// \brief Check if the specified \a ScheduleKind is static non-chunked.
  424. /// This kind of worksharing directive is emitted without outer loop.
  425. /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
  426. /// \param Chunked True if chunk is specified in the clause.
  427. ///
  428. virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
  429. bool Chunked) const;
  430. /// \brief Check if the specified \a ScheduleKind is dynamic.
  431. /// This kind of worksharing directive is emitted without outer loop.
  432. /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
  433. ///
  434. virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
  435. /// \brief Call the appropriate runtime routine to initialize it before start
  436. /// of loop.
  437. ///
  438. /// Depending on the loop schedule, it is nesessary to call some runtime
  439. /// routine before start of the OpenMP loop to get the loop upper / lower
  440. /// bounds \a LB and \a UB and stride \a ST.
  441. ///
  442. /// \param CGF Reference to current CodeGenFunction.
  443. /// \param Loc Clang source location.
  444. /// \param SchedKind Schedule kind, specified by the 'schedule' clause.
  445. /// \param IVSize Size of the iteration variable in bits.
  446. /// \param IVSigned Sign of the interation variable.
  447. /// \param Ordered true if loop is ordered, false otherwise.
  448. /// \param IL Address of the output variable in which the flag of the
  449. /// last iteration is returned.
  450. /// \param LB Address of the output variable in which the lower iteration
  451. /// number is returned.
  452. /// \param UB Address of the output variable in which the upper iteration
  453. /// number is returned.
  454. /// \param ST Address of the output variable in which the stride value is
  455. /// returned nesessary to generated the static_chunked scheduled loop.
  456. /// \param Chunk Value of the chunk for the static_chunked scheduled loop.
  457. /// For the default (nullptr) value, the chunk 1 will be used.
  458. ///
  459. virtual void emitForInit(CodeGenFunction &CGF, SourceLocation Loc,
  460. OpenMPScheduleClauseKind SchedKind, unsigned IVSize,
  461. bool IVSigned, bool Ordered, llvm::Value *IL,
  462. llvm::Value *LB, llvm::Value *UB, llvm::Value *ST,
  463. llvm::Value *Chunk = nullptr);
  464. /// \brief Call the appropriate runtime routine to notify that we finished
  465. /// iteration of the ordered loop with the dynamic scheduling.
  466. ///
  467. /// \param CGF Reference to current CodeGenFunction.
  468. /// \param Loc Clang source location.
  469. /// \param IVSize Size of the iteration variable in bits.
  470. /// \param IVSigned Sign of the interation variable.
  471. ///
  472. virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
  473. SourceLocation Loc, unsigned IVSize,
  474. bool IVSigned);
  475. /// \brief Call the appropriate runtime routine to notify that we finished
  476. /// all the work with current loop.
  477. ///
  478. /// \param CGF Reference to current CodeGenFunction.
  479. /// \param Loc Clang source location.
  480. ///
  481. virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc);
  482. /// Call __kmpc_dispatch_next(
  483. /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
  484. /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
  485. /// kmp_int[32|64] *p_stride);
  486. /// \param IVSize Size of the iteration variable in bits.
  487. /// \param IVSigned Sign of the interation variable.
  488. /// \param IL Address of the output variable in which the flag of the
  489. /// last iteration is returned.
  490. /// \param LB Address of the output variable in which the lower iteration
  491. /// number is returned.
  492. /// \param UB Address of the output variable in which the upper iteration
  493. /// number is returned.
  494. /// \param ST Address of the output variable in which the stride value is
  495. /// returned.
  496. virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
  497. unsigned IVSize, bool IVSigned,
  498. llvm::Value *IL, llvm::Value *LB,
  499. llvm::Value *UB, llvm::Value *ST);
  500. /// \brief Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
  501. /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
  502. /// clause.
  503. /// \param NumThreads An integer value of threads.
  504. virtual void emitNumThreadsClause(CodeGenFunction &CGF,
  505. llvm::Value *NumThreads,
  506. SourceLocation Loc);
  507. /// \brief Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
  508. /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
  509. virtual void emitProcBindClause(CodeGenFunction &CGF,
  510. OpenMPProcBindClauseKind ProcBind,
  511. SourceLocation Loc);
  512. /// \brief Returns address of the threadprivate variable for the current
  513. /// thread.
  514. /// \param VD Threadprivate variable.
  515. /// \param VDAddr Address of the global variable \a VD.
  516. /// \param Loc Location of the reference to threadprivate var.
  517. /// \return Address of the threadprivate variable for the current thread.
  518. virtual llvm::Value *getAddrOfThreadPrivate(CodeGenFunction &CGF,
  519. const VarDecl *VD,
  520. llvm::Value *VDAddr,
  521. SourceLocation Loc);
  522. /// \brief Emit a code for initialization of threadprivate variable. It emits
  523. /// a call to runtime library which adds initial value to the newly created
  524. /// threadprivate variable (if it is not constant) and registers destructor
  525. /// for the variable (if any).
  526. /// \param VD Threadprivate variable.
  527. /// \param VDAddr Address of the global variable \a VD.
  528. /// \param Loc Location of threadprivate declaration.
  529. /// \param PerformInit true if initialization expression is not constant.
  530. virtual llvm::Function *
  531. emitThreadPrivateVarDefinition(const VarDecl *VD, llvm::Value *VDAddr,
  532. SourceLocation Loc, bool PerformInit,
  533. CodeGenFunction *CGF = nullptr);
  534. /// \brief Emit flush of the variables specified in 'omp flush' directive.
  535. /// \param Vars List of variables to flush.
  536. virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
  537. SourceLocation Loc);
  538. /// \brief Emit task region for the task directive. The task region is
  539. /// emitted in several steps:
  540. /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
  541. /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
  542. /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
  543. /// function:
  544. /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
  545. /// TaskFunction(gtid, tt->part_id, tt->shareds);
  546. /// return 0;
  547. /// }
  548. /// 2. Copy a list of shared variables to field shareds of the resulting
  549. /// structure kmp_task_t returned by the previous call (if any).
  550. /// 3. Copy a pointer to destructions function to field destructions of the
  551. /// resulting structure kmp_task_t.
  552. /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
  553. /// kmp_task_t *new_task), where new_task is a resulting structure from
  554. /// previous items.
  555. /// \param D Current task directive.
  556. /// \param Tied true if the task is tied (the task is tied to the thread that
  557. /// can suspend its task region), false - untied (the task is not tied to any
  558. /// thread).
  559. /// \param Final Contains either constant bool value, or llvm::Value * of i1
  560. /// type for final clause. If the value is true, the task forces all of its
  561. /// child tasks to become final and included tasks.
  562. /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
  563. /// /*part_id*/, captured_struct */*__context*/);
  564. /// \param SharedsTy A type which contains references the shared variables.
  565. /// \param Shareds Context with the list of shared variables from the \p
  566. /// TaskFunction.
  567. /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
  568. /// otherwise.
  569. /// \param PrivateVars List of references to private variables for the task
  570. /// directive.
  571. /// \param PrivateCopies List of private copies for each private variable in
  572. /// \p PrivateVars.
  573. /// \param FirstprivateVars List of references to private variables for the
  574. /// task directive.
  575. /// \param FirstprivateCopies List of private copies for each private variable
  576. /// in \p FirstprivateVars.
  577. /// \param FirstprivateInits List of references to auto generated variables
  578. /// used for initialization of a single array element. Used if firstprivate
  579. /// variable is of array type.
  580. /// \param Dependences List of dependences for the 'task' construct, including
  581. /// original expression and dependency type.
  582. virtual void emitTaskCall(
  583. CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D,
  584. bool Tied, llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
  585. llvm::Value *TaskFunction, QualType SharedsTy, llvm::Value *Shareds,
  586. const Expr *IfCond, ArrayRef<const Expr *> PrivateVars,
  587. ArrayRef<const Expr *> PrivateCopies,
  588. ArrayRef<const Expr *> FirstprivateVars,
  589. ArrayRef<const Expr *> FirstprivateCopies,
  590. ArrayRef<const Expr *> FirstprivateInits,
  591. ArrayRef<std::pair<OpenMPDependClauseKind, const Expr *>> Dependences);
  592. /// \brief Emit code for the directive that does not require outlining.
  593. ///
  594. /// \param InnermostKind Kind of innermost directive (for simple directives it
  595. /// is a directive itself, for combined - its innermost directive).
  596. /// \param CodeGen Code generation sequence for the \a D directive.
  597. virtual void emitInlinedDirective(CodeGenFunction &CGF,
  598. OpenMPDirectiveKind InnermostKind,
  599. const RegionCodeGenTy &CodeGen);
  600. /// \brief Emit a code for reduction clause. Next code should be emitted for
  601. /// reduction:
  602. /// \code
  603. ///
  604. /// static kmp_critical_name lock = { 0 };
  605. ///
  606. /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
  607. /// ...
  608. /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
  609. /// ...
  610. /// }
  611. ///
  612. /// ...
  613. /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
  614. /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
  615. /// RedList, reduce_func, &<lock>)) {
  616. /// case 1:
  617. /// ...
  618. /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
  619. /// ...
  620. /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
  621. /// break;
  622. /// case 2:
  623. /// ...
  624. /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
  625. /// ...
  626. /// break;
  627. /// default:;
  628. /// }
  629. /// \endcode
  630. ///
  631. /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
  632. /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
  633. /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
  634. /// or 'operator binop(LHS, RHS)'.
  635. /// \param WithNowait true if parent directive has also nowait clause, false
  636. /// otherwise.
  637. virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
  638. ArrayRef<const Expr *> LHSExprs,
  639. ArrayRef<const Expr *> RHSExprs,
  640. ArrayRef<const Expr *> ReductionOps,
  641. bool WithNowait, bool SimpleReduction);
  642. /// \brief Emit code for 'taskwait' directive.
  643. virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc);
  644. /// \brief Emit code for 'cancellation point' construct.
  645. /// \param CancelRegion Region kind for which the cancellation point must be
  646. /// emitted.
  647. ///
  648. virtual void emitCancellationPointCall(CodeGenFunction &CGF,
  649. SourceLocation Loc,
  650. OpenMPDirectiveKind CancelRegion);
  651. /// \brief Emit code for 'cancel' construct.
  652. /// \param CancelRegion Region kind for which the cancel must be emitted.
  653. ///
  654. virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
  655. OpenMPDirectiveKind CancelRegion);
  656. };
  657. } // namespace CodeGen
  658. } // namespace clang
  659. #endif