CGOpenMPRuntime.h 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  1. //===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This provides a class for OpenMP runtime code generation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
  13. #define LLVM_CLANG_LIB_CODEGEN_CGOPENMPRUNTIME_H
  14. #include "CGValue.h"
  15. #include "clang/AST/DeclOpenMP.h"
  16. #include "clang/AST/GlobalDecl.h"
  17. #include "clang/AST/Type.h"
  18. #include "clang/Basic/OpenMPKinds.h"
  19. #include "clang/Basic/SourceLocation.h"
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/PointerIntPair.h"
  22. #include "llvm/ADT/SmallPtrSet.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/ADT/StringSet.h"
  25. #include "llvm/Frontend/OpenMP/OMPConstants.h"
  26. #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
  27. #include "llvm/IR/Function.h"
  28. #include "llvm/IR/ValueHandle.h"
  29. #include "llvm/Support/AtomicOrdering.h"
  30. namespace llvm {
  31. class ArrayType;
  32. class Constant;
  33. class FunctionType;
  34. class GlobalVariable;
  35. class Type;
  36. class Value;
  37. class OpenMPIRBuilder;
  38. } // namespace llvm
  39. namespace clang {
  40. class Expr;
  41. class OMPDependClause;
  42. class OMPExecutableDirective;
  43. class OMPLoopDirective;
  44. class VarDecl;
  45. class OMPDeclareReductionDecl;
  46. namespace CodeGen {
  47. class Address;
  48. class CodeGenFunction;
  49. class CodeGenModule;
  50. /// A basic class for pre|post-action for advanced codegen sequence for OpenMP
  51. /// region.
  52. class PrePostActionTy {
  53. public:
  54. explicit PrePostActionTy() {}
  55. virtual void Enter(CodeGenFunction &CGF) {}
  56. virtual void Exit(CodeGenFunction &CGF) {}
  57. virtual ~PrePostActionTy() {}
  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.
  61. class 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. template <typename Callable>
  68. static void CallbackFn(intptr_t CodeGen, CodeGenFunction &CGF,
  69. PrePostActionTy &Action) {
  70. return (*reinterpret_cast<Callable *>(CodeGen))(CGF, Action);
  71. }
  72. public:
  73. template <typename Callable>
  74. RegionCodeGenTy(
  75. Callable &&CodeGen,
  76. std::enable_if_t<!std::is_same<std::remove_reference_t<Callable>,
  77. RegionCodeGenTy>::value> * = nullptr)
  78. : CodeGen(reinterpret_cast<intptr_t>(&CodeGen)),
  79. Callback(CallbackFn<std::remove_reference_t<Callable>>),
  80. PrePostAction(nullptr) {}
  81. void setAction(PrePostActionTy &Action) const { PrePostAction = &Action; }
  82. void operator()(CodeGenFunction &CGF) const;
  83. };
  84. struct OMPTaskDataTy final {
  85. SmallVector<const Expr *, 4> PrivateVars;
  86. SmallVector<const Expr *, 4> PrivateCopies;
  87. SmallVector<const Expr *, 4> FirstprivateVars;
  88. SmallVector<const Expr *, 4> FirstprivateCopies;
  89. SmallVector<const Expr *, 4> FirstprivateInits;
  90. SmallVector<const Expr *, 4> LastprivateVars;
  91. SmallVector<const Expr *, 4> LastprivateCopies;
  92. SmallVector<const Expr *, 4> ReductionVars;
  93. SmallVector<const Expr *, 4> ReductionOrigs;
  94. SmallVector<const Expr *, 4> ReductionCopies;
  95. SmallVector<const Expr *, 4> ReductionOps;
  96. SmallVector<CanonicalDeclPtr<const VarDecl>, 4> PrivateLocals;
  97. struct DependData {
  98. OpenMPDependClauseKind DepKind = OMPC_DEPEND_unknown;
  99. const Expr *IteratorExpr = nullptr;
  100. SmallVector<const Expr *, 4> DepExprs;
  101. explicit DependData() = default;
  102. DependData(OpenMPDependClauseKind DepKind, const Expr *IteratorExpr)
  103. : DepKind(DepKind), IteratorExpr(IteratorExpr) {}
  104. };
  105. SmallVector<DependData, 4> Dependences;
  106. llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
  107. llvm::PointerIntPair<llvm::Value *, 1, bool> Schedule;
  108. llvm::PointerIntPair<llvm::Value *, 1, bool> Priority;
  109. llvm::Value *Reductions = nullptr;
  110. unsigned NumberOfParts = 0;
  111. bool Tied = true;
  112. bool Nogroup = false;
  113. bool IsReductionWithTaskMod = false;
  114. bool IsWorksharingReduction = false;
  115. bool HasNowaitClause = false;
  116. };
  117. /// Class intended to support codegen of all kind of the reduction clauses.
  118. class ReductionCodeGen {
  119. private:
  120. /// Data required for codegen of reduction clauses.
  121. struct ReductionData {
  122. /// Reference to the item shared between tasks to reduce into.
  123. const Expr *Shared = nullptr;
  124. /// Reference to the original item.
  125. const Expr *Ref = nullptr;
  126. /// Helper expression for generation of private copy.
  127. const Expr *Private = nullptr;
  128. /// Helper expression for generation reduction operation.
  129. const Expr *ReductionOp = nullptr;
  130. ReductionData(const Expr *Shared, const Expr *Ref, const Expr *Private,
  131. const Expr *ReductionOp)
  132. : Shared(Shared), Ref(Ref), Private(Private), ReductionOp(ReductionOp) {
  133. }
  134. };
  135. /// List of reduction-based clauses.
  136. SmallVector<ReductionData, 4> ClausesData;
  137. /// List of addresses of shared variables/expressions.
  138. SmallVector<std::pair<LValue, LValue>, 4> SharedAddresses;
  139. /// List of addresses of original variables/expressions.
  140. SmallVector<std::pair<LValue, LValue>, 4> OrigAddresses;
  141. /// Sizes of the reduction items in chars.
  142. SmallVector<std::pair<llvm::Value *, llvm::Value *>, 4> Sizes;
  143. /// Base declarations for the reduction items.
  144. SmallVector<const VarDecl *, 4> BaseDecls;
  145. /// Emits lvalue for shared expression.
  146. LValue emitSharedLValue(CodeGenFunction &CGF, const Expr *E);
  147. /// Emits upper bound for shared expression (if array section).
  148. LValue emitSharedLValueUB(CodeGenFunction &CGF, const Expr *E);
  149. /// Performs aggregate initialization.
  150. /// \param N Number of reduction item in the common list.
  151. /// \param PrivateAddr Address of the corresponding private item.
  152. /// \param SharedAddr Address of the original shared variable.
  153. /// \param DRD Declare reduction construct used for reduction item.
  154. void emitAggregateInitialization(CodeGenFunction &CGF, unsigned N,
  155. Address PrivateAddr, Address SharedAddr,
  156. const OMPDeclareReductionDecl *DRD);
  157. public:
  158. ReductionCodeGen(ArrayRef<const Expr *> Shareds, ArrayRef<const Expr *> Origs,
  159. ArrayRef<const Expr *> Privates,
  160. ArrayRef<const Expr *> ReductionOps);
  161. /// Emits lvalue for the shared and original reduction item.
  162. /// \param N Number of the reduction item.
  163. void emitSharedOrigLValue(CodeGenFunction &CGF, unsigned N);
  164. /// Emits the code for the variable-modified type, if required.
  165. /// \param N Number of the reduction item.
  166. void emitAggregateType(CodeGenFunction &CGF, unsigned N);
  167. /// Emits the code for the variable-modified type, if required.
  168. /// \param N Number of the reduction item.
  169. /// \param Size Size of the type in chars.
  170. void emitAggregateType(CodeGenFunction &CGF, unsigned N, llvm::Value *Size);
  171. /// Performs initialization of the private copy for the reduction item.
  172. /// \param N Number of the reduction item.
  173. /// \param PrivateAddr Address of the corresponding private item.
  174. /// \param DefaultInit Default initialization sequence that should be
  175. /// performed if no reduction specific initialization is found.
  176. /// \param SharedAddr Address of the original shared variable.
  177. void
  178. emitInitialization(CodeGenFunction &CGF, unsigned N, Address PrivateAddr,
  179. Address SharedAddr,
  180. llvm::function_ref<bool(CodeGenFunction &)> DefaultInit);
  181. /// Returns true if the private copy requires cleanups.
  182. bool needCleanups(unsigned N);
  183. /// Emits cleanup code for the reduction item.
  184. /// \param N Number of the reduction item.
  185. /// \param PrivateAddr Address of the corresponding private item.
  186. void emitCleanups(CodeGenFunction &CGF, unsigned N, Address PrivateAddr);
  187. /// Adjusts \p PrivatedAddr for using instead of the original variable
  188. /// address in normal operations.
  189. /// \param N Number of the reduction item.
  190. /// \param PrivateAddr Address of the corresponding private item.
  191. Address adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
  192. Address PrivateAddr);
  193. /// Returns LValue for the reduction item.
  194. LValue getSharedLValue(unsigned N) const { return SharedAddresses[N].first; }
  195. /// Returns LValue for the original reduction item.
  196. LValue getOrigLValue(unsigned N) const { return OrigAddresses[N].first; }
  197. /// Returns the size of the reduction item (in chars and total number of
  198. /// elements in the item), or nullptr, if the size is a constant.
  199. std::pair<llvm::Value *, llvm::Value *> getSizes(unsigned N) const {
  200. return Sizes[N];
  201. }
  202. /// Returns the base declaration of the reduction item.
  203. const VarDecl *getBaseDecl(unsigned N) const { return BaseDecls[N]; }
  204. /// Returns the base declaration of the reduction item.
  205. const Expr *getRefExpr(unsigned N) const { return ClausesData[N].Ref; }
  206. /// Returns true if the initialization of the reduction item uses initializer
  207. /// from declare reduction construct.
  208. bool usesReductionInitializer(unsigned N) const;
  209. /// Return the type of the private item.
  210. QualType getPrivateType(unsigned N) const {
  211. return cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl())
  212. ->getType();
  213. }
  214. };
  215. class CGOpenMPRuntime {
  216. public:
  217. /// Allows to disable automatic handling of functions used in target regions
  218. /// as those marked as `omp declare target`.
  219. class DisableAutoDeclareTargetRAII {
  220. CodeGenModule &CGM;
  221. bool SavedShouldMarkAsGlobal;
  222. public:
  223. DisableAutoDeclareTargetRAII(CodeGenModule &CGM);
  224. ~DisableAutoDeclareTargetRAII();
  225. };
  226. /// Manages list of nontemporal decls for the specified directive.
  227. class NontemporalDeclsRAII {
  228. CodeGenModule &CGM;
  229. const bool NeedToPush;
  230. public:
  231. NontemporalDeclsRAII(CodeGenModule &CGM, const OMPLoopDirective &S);
  232. ~NontemporalDeclsRAII();
  233. };
  234. /// Manages list of nontemporal decls for the specified directive.
  235. class UntiedTaskLocalDeclsRAII {
  236. CodeGenModule &CGM;
  237. const bool NeedToPush;
  238. public:
  239. UntiedTaskLocalDeclsRAII(
  240. CodeGenFunction &CGF,
  241. const llvm::MapVector<CanonicalDeclPtr<const VarDecl>,
  242. std::pair<Address, Address>> &LocalVars);
  243. ~UntiedTaskLocalDeclsRAII();
  244. };
  245. /// Maps the expression for the lastprivate variable to the global copy used
  246. /// to store new value because original variables are not mapped in inner
  247. /// parallel regions. Only private copies are captured but we need also to
  248. /// store private copy in shared address.
  249. /// Also, stores the expression for the private loop counter and it
  250. /// threaprivate name.
  251. struct LastprivateConditionalData {
  252. llvm::MapVector<CanonicalDeclPtr<const Decl>, SmallString<16>>
  253. DeclToUniqueName;
  254. LValue IVLVal;
  255. llvm::Function *Fn = nullptr;
  256. bool Disabled = false;
  257. };
  258. /// Manages list of lastprivate conditional decls for the specified directive.
  259. class LastprivateConditionalRAII {
  260. enum class ActionToDo {
  261. DoNotPush,
  262. PushAsLastprivateConditional,
  263. DisableLastprivateConditional,
  264. };
  265. CodeGenModule &CGM;
  266. ActionToDo Action = ActionToDo::DoNotPush;
  267. /// Check and try to disable analysis of inner regions for changes in
  268. /// lastprivate conditional.
  269. void tryToDisableInnerAnalysis(const OMPExecutableDirective &S,
  270. llvm::DenseSet<CanonicalDeclPtr<const Decl>>
  271. &NeedToAddForLPCsAsDisabled) const;
  272. LastprivateConditionalRAII(CodeGenFunction &CGF,
  273. const OMPExecutableDirective &S);
  274. public:
  275. explicit LastprivateConditionalRAII(CodeGenFunction &CGF,
  276. const OMPExecutableDirective &S,
  277. LValue IVLVal);
  278. static LastprivateConditionalRAII disable(CodeGenFunction &CGF,
  279. const OMPExecutableDirective &S);
  280. ~LastprivateConditionalRAII();
  281. };
  282. llvm::OpenMPIRBuilder &getOMPBuilder() { return OMPBuilder; }
  283. protected:
  284. CodeGenModule &CGM;
  285. /// An OpenMP-IR-Builder instance.
  286. llvm::OpenMPIRBuilder OMPBuilder;
  287. /// Helper to emit outlined function for 'target' directive.
  288. /// \param D Directive to emit.
  289. /// \param ParentName Name of the function that encloses the target region.
  290. /// \param OutlinedFn Outlined function value to be defined by this call.
  291. /// \param OutlinedFnID Outlined function ID value to be defined by this call.
  292. /// \param IsOffloadEntry True if the outlined function is an offload entry.
  293. /// \param CodeGen Lambda codegen specific to an accelerator device.
  294. /// An outlined function may not be an entry if, e.g. the if clause always
  295. /// evaluates to false.
  296. virtual void emitTargetOutlinedFunctionHelper(const OMPExecutableDirective &D,
  297. StringRef ParentName,
  298. llvm::Function *&OutlinedFn,
  299. llvm::Constant *&OutlinedFnID,
  300. bool IsOffloadEntry,
  301. const RegionCodeGenTy &CodeGen);
  302. /// Emits object of ident_t type with info for source location.
  303. /// \param Flags Flags for OpenMP location.
  304. /// \param EmitLoc emit source location with debug-info is off.
  305. ///
  306. llvm::Value *emitUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
  307. unsigned Flags = 0, bool EmitLoc = false);
  308. /// Emit the number of teams for a target directive. Inspect the num_teams
  309. /// clause associated with a teams construct combined or closely nested
  310. /// with the target directive.
  311. ///
  312. /// Emit a team of size one for directives such as 'target parallel' that
  313. /// have no associated teams construct.
  314. ///
  315. /// Otherwise, return nullptr.
  316. const Expr *getNumTeamsExprForTargetDirective(CodeGenFunction &CGF,
  317. const OMPExecutableDirective &D,
  318. int32_t &DefaultVal);
  319. llvm::Value *emitNumTeamsForTargetDirective(CodeGenFunction &CGF,
  320. const OMPExecutableDirective &D);
  321. /// Emit the number of threads for a target directive. Inspect the
  322. /// thread_limit clause associated with a teams construct combined or closely
  323. /// nested with the target directive.
  324. ///
  325. /// Emit the num_threads clause for directives such as 'target parallel' that
  326. /// have no associated teams construct.
  327. ///
  328. /// Otherwise, return nullptr.
  329. const Expr *
  330. getNumThreadsExprForTargetDirective(CodeGenFunction &CGF,
  331. const OMPExecutableDirective &D,
  332. int32_t &DefaultVal);
  333. llvm::Value *
  334. emitNumThreadsForTargetDirective(CodeGenFunction &CGF,
  335. const OMPExecutableDirective &D);
  336. /// Returns pointer to ident_t type.
  337. llvm::Type *getIdentTyPointerTy();
  338. /// Gets thread id value for the current thread.
  339. ///
  340. llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
  341. /// Get the function name of an outlined region.
  342. // The name can be customized depending on the target.
  343. //
  344. virtual StringRef getOutlinedHelperName() const { return ".omp_outlined."; }
  345. /// Emits \p Callee function call with arguments \p Args with location \p Loc.
  346. void emitCall(CodeGenFunction &CGF, SourceLocation Loc,
  347. llvm::FunctionCallee Callee,
  348. ArrayRef<llvm::Value *> Args = std::nullopt) const;
  349. /// Emits address of the word in a memory where current thread id is
  350. /// stored.
  351. virtual Address emitThreadIDAddress(CodeGenFunction &CGF, SourceLocation Loc);
  352. void setLocThreadIdInsertPt(CodeGenFunction &CGF,
  353. bool AtCurrentPoint = false);
  354. void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
  355. /// Check if the default location must be constant.
  356. /// Default is false to support OMPT/OMPD.
  357. virtual bool isDefaultLocationConstant() const { return false; }
  358. /// Returns additional flags that can be stored in reserved_2 field of the
  359. /// default location.
  360. virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
  361. /// Returns default flags for the barriers depending on the directive, for
  362. /// which this barier is going to be emitted.
  363. static unsigned getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind);
  364. /// Get the LLVM type for the critical name.
  365. llvm::ArrayType *getKmpCriticalNameTy() const {return KmpCriticalNameTy;}
  366. /// Returns corresponding lock object for the specified critical region
  367. /// name. If the lock object does not exist it is created, otherwise the
  368. /// reference to the existing copy is returned.
  369. /// \param CriticalName Name of the critical region.
  370. ///
  371. llvm::Value *getCriticalRegionLock(StringRef CriticalName);
  372. protected:
  373. /// Map for SourceLocation and OpenMP runtime library debug locations.
  374. typedef llvm::DenseMap<SourceLocation, llvm::Value *> OpenMPDebugLocMapTy;
  375. OpenMPDebugLocMapTy OpenMPDebugLocMap;
  376. /// The type for a microtask which gets passed to __kmpc_fork_call().
  377. /// Original representation is:
  378. /// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
  379. llvm::FunctionType *Kmpc_MicroTy = nullptr;
  380. /// Stores debug location and ThreadID for the function.
  381. struct DebugLocThreadIdTy {
  382. llvm::Value *DebugLoc;
  383. llvm::Value *ThreadID;
  384. /// Insert point for the service instructions.
  385. llvm::AssertingVH<llvm::Instruction> ServiceInsertPt = nullptr;
  386. };
  387. /// Map of local debug location, ThreadId and functions.
  388. typedef llvm::DenseMap<llvm::Function *, DebugLocThreadIdTy>
  389. OpenMPLocThreadIDMapTy;
  390. OpenMPLocThreadIDMapTy OpenMPLocThreadIDMap;
  391. /// Map of UDRs and corresponding combiner/initializer.
  392. typedef llvm::DenseMap<const OMPDeclareReductionDecl *,
  393. std::pair<llvm::Function *, llvm::Function *>>
  394. UDRMapTy;
  395. UDRMapTy UDRMap;
  396. /// Map of functions and locally defined UDRs.
  397. typedef llvm::DenseMap<llvm::Function *,
  398. SmallVector<const OMPDeclareReductionDecl *, 4>>
  399. FunctionUDRMapTy;
  400. FunctionUDRMapTy FunctionUDRMap;
  401. /// Map from the user-defined mapper declaration to its corresponding
  402. /// functions.
  403. llvm::DenseMap<const OMPDeclareMapperDecl *, llvm::Function *> UDMMap;
  404. /// Map of functions and their local user-defined mappers.
  405. using FunctionUDMMapTy =
  406. llvm::DenseMap<llvm::Function *,
  407. SmallVector<const OMPDeclareMapperDecl *, 4>>;
  408. FunctionUDMMapTy FunctionUDMMap;
  409. /// Maps local variables marked as lastprivate conditional to their internal
  410. /// types.
  411. llvm::DenseMap<llvm::Function *,
  412. llvm::DenseMap<CanonicalDeclPtr<const Decl>,
  413. std::tuple<QualType, const FieldDecl *,
  414. const FieldDecl *, LValue>>>
  415. LastprivateConditionalToTypes;
  416. /// Maps function to the position of the untied task locals stack.
  417. llvm::DenseMap<llvm::Function *, unsigned> FunctionToUntiedTaskStackMap;
  418. /// Type kmp_critical_name, originally defined as typedef kmp_int32
  419. /// kmp_critical_name[8];
  420. llvm::ArrayType *KmpCriticalNameTy;
  421. /// An ordered map of auto-generated variables to their unique names.
  422. /// It stores variables with the following names: 1) ".gomp_critical_user_" +
  423. /// <critical_section_name> + ".var" for "omp critical" directives; 2)
  424. /// <mangled_name_for_global_var> + ".cache." for cache for threadprivate
  425. /// variables.
  426. llvm::StringMap<llvm::AssertingVH<llvm::GlobalVariable>,
  427. llvm::BumpPtrAllocator> InternalVars;
  428. /// Type typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *);
  429. llvm::Type *KmpRoutineEntryPtrTy = nullptr;
  430. QualType KmpRoutineEntryPtrQTy;
  431. /// Type typedef struct kmp_task {
  432. /// void * shareds; /**< pointer to block of pointers to
  433. /// shared vars */
  434. /// kmp_routine_entry_t routine; /**< pointer to routine to call for
  435. /// executing task */
  436. /// kmp_int32 part_id; /**< part id for the task */
  437. /// kmp_routine_entry_t destructors; /* pointer to function to invoke
  438. /// deconstructors of firstprivate C++ objects */
  439. /// } kmp_task_t;
  440. QualType KmpTaskTQTy;
  441. /// Saved kmp_task_t for task directive.
  442. QualType SavedKmpTaskTQTy;
  443. /// Saved kmp_task_t for taskloop-based directive.
  444. QualType SavedKmpTaskloopTQTy;
  445. /// Type typedef struct kmp_depend_info {
  446. /// kmp_intptr_t base_addr;
  447. /// size_t len;
  448. /// struct {
  449. /// bool in:1;
  450. /// bool out:1;
  451. /// } flags;
  452. /// } kmp_depend_info_t;
  453. QualType KmpDependInfoTy;
  454. /// Type typedef struct kmp_task_affinity_info {
  455. /// kmp_intptr_t base_addr;
  456. /// size_t len;
  457. /// struct {
  458. /// bool flag1 : 1;
  459. /// bool flag2 : 1;
  460. /// kmp_int32 reserved : 30;
  461. /// } flags;
  462. /// } kmp_task_affinity_info_t;
  463. QualType KmpTaskAffinityInfoTy;
  464. /// struct kmp_dim { // loop bounds info casted to kmp_int64
  465. /// kmp_int64 lo; // lower
  466. /// kmp_int64 up; // upper
  467. /// kmp_int64 st; // stride
  468. /// };
  469. QualType KmpDimTy;
  470. /// Entity that registers the offloading constants that were emitted so
  471. /// far.
  472. llvm::OffloadEntriesInfoManager OffloadEntriesInfoManager;
  473. bool ShouldMarkAsGlobal = true;
  474. /// List of the emitted declarations.
  475. llvm::DenseSet<CanonicalDeclPtr<const Decl>> AlreadyEmittedTargetDecls;
  476. /// List of the global variables with their addresses that should not be
  477. /// emitted for the target.
  478. llvm::StringMap<llvm::WeakTrackingVH> EmittedNonTargetVariables;
  479. /// List of variables that can become declare target implicitly and, thus,
  480. /// must be emitted.
  481. llvm::SmallDenseSet<const VarDecl *> DeferredGlobalVariables;
  482. using NontemporalDeclsSet = llvm::SmallDenseSet<CanonicalDeclPtr<const Decl>>;
  483. /// Stack for list of declarations in current context marked as nontemporal.
  484. /// The set is the union of all current stack elements.
  485. llvm::SmallVector<NontemporalDeclsSet, 4> NontemporalDeclsStack;
  486. using UntiedLocalVarsAddressesMap =
  487. llvm::MapVector<CanonicalDeclPtr<const VarDecl>,
  488. std::pair<Address, Address>>;
  489. llvm::SmallVector<UntiedLocalVarsAddressesMap, 4> UntiedLocalVarsStack;
  490. /// Stack for list of addresses of declarations in current context marked as
  491. /// lastprivate conditional. The set is the union of all current stack
  492. /// elements.
  493. llvm::SmallVector<LastprivateConditionalData, 4> LastprivateConditionalStack;
  494. /// Flag for keeping track of weather a requires unified_shared_memory
  495. /// directive is present.
  496. bool HasRequiresUnifiedSharedMemory = false;
  497. /// Atomic ordering from the omp requires directive.
  498. llvm::AtomicOrdering RequiresAtomicOrdering = llvm::AtomicOrdering::Monotonic;
  499. /// Flag for keeping track of weather a target region has been emitted.
  500. bool HasEmittedTargetRegion = false;
  501. /// Flag for keeping track of weather a device routine has been emitted.
  502. /// Device routines are specific to the
  503. bool HasEmittedDeclareTargetRegion = false;
  504. /// Loads all the offload entries information from the host IR
  505. /// metadata.
  506. void loadOffloadInfoMetadata();
  507. /// Start scanning from statement \a S and emit all target regions
  508. /// found along the way.
  509. /// \param S Starting statement.
  510. /// \param ParentName Name of the function declaration that is being scanned.
  511. void scanForTargetRegionsFunctions(const Stmt *S, StringRef ParentName);
  512. /// Build type kmp_routine_entry_t (if not built yet).
  513. void emitKmpRoutineEntryT(QualType KmpInt32Ty);
  514. /// Returns pointer to kmpc_micro type.
  515. llvm::Type *getKmpc_MicroPointerTy();
  516. /// Returns __kmpc_for_static_init_* runtime function for the specified
  517. /// size \a IVSize and sign \a IVSigned. Will create a distribute call
  518. /// __kmpc_distribute_static_init* if \a IsGPUDistribute is set.
  519. llvm::FunctionCallee createForStaticInitFunction(unsigned IVSize,
  520. bool IVSigned,
  521. bool IsGPUDistribute);
  522. /// Returns __kmpc_dispatch_init_* runtime function for the specified
  523. /// size \a IVSize and sign \a IVSigned.
  524. llvm::FunctionCallee createDispatchInitFunction(unsigned IVSize,
  525. bool IVSigned);
  526. /// Returns __kmpc_dispatch_next_* runtime function for the specified
  527. /// size \a IVSize and sign \a IVSigned.
  528. llvm::FunctionCallee createDispatchNextFunction(unsigned IVSize,
  529. bool IVSigned);
  530. /// Returns __kmpc_dispatch_fini_* runtime function for the specified
  531. /// size \a IVSize and sign \a IVSigned.
  532. llvm::FunctionCallee createDispatchFiniFunction(unsigned IVSize,
  533. bool IVSigned);
  534. /// If the specified mangled name is not in the module, create and
  535. /// return threadprivate cache object. This object is a pointer's worth of
  536. /// storage that's reserved for use by the OpenMP runtime.
  537. /// \param VD Threadprivate variable.
  538. /// \return Cache variable for the specified threadprivate.
  539. llvm::Constant *getOrCreateThreadPrivateCache(const VarDecl *VD);
  540. /// Set of threadprivate variables with the generated initializer.
  541. llvm::StringSet<> ThreadPrivateWithDefinition;
  542. /// Set of declare target variables with the generated initializer.
  543. llvm::StringSet<> DeclareTargetWithDefinition;
  544. /// Emits initialization code for the threadprivate variables.
  545. /// \param VDAddr Address of the global variable \a VD.
  546. /// \param Ctor Pointer to a global init function for \a VD.
  547. /// \param CopyCtor Pointer to a global copy function for \a VD.
  548. /// \param Dtor Pointer to a global destructor function for \a VD.
  549. /// \param Loc Location of threadprivate declaration.
  550. void emitThreadPrivateVarInit(CodeGenFunction &CGF, Address VDAddr,
  551. llvm::Value *Ctor, llvm::Value *CopyCtor,
  552. llvm::Value *Dtor, SourceLocation Loc);
  553. /// Emit the array initialization or deletion portion for user-defined mapper
  554. /// code generation.
  555. void emitUDMapperArrayInitOrDel(CodeGenFunction &MapperCGF,
  556. llvm::Value *Handle, llvm::Value *BasePtr,
  557. llvm::Value *Ptr, llvm::Value *Size,
  558. llvm::Value *MapType, llvm::Value *MapName,
  559. CharUnits ElementSize,
  560. llvm::BasicBlock *ExitBB, bool IsInit);
  561. struct TaskResultTy {
  562. llvm::Value *NewTask = nullptr;
  563. llvm::Function *TaskEntry = nullptr;
  564. llvm::Value *NewTaskNewTaskTTy = nullptr;
  565. LValue TDBase;
  566. const RecordDecl *KmpTaskTQTyRD = nullptr;
  567. llvm::Value *TaskDupFn = nullptr;
  568. };
  569. /// Emit task region for the task directive. The task region is emitted in
  570. /// several steps:
  571. /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
  572. /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
  573. /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
  574. /// function:
  575. /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
  576. /// TaskFunction(gtid, tt->part_id, tt->shareds);
  577. /// return 0;
  578. /// }
  579. /// 2. Copy a list of shared variables to field shareds of the resulting
  580. /// structure kmp_task_t returned by the previous call (if any).
  581. /// 3. Copy a pointer to destructions function to field destructions of the
  582. /// resulting structure kmp_task_t.
  583. /// \param D Current task directive.
  584. /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
  585. /// /*part_id*/, captured_struct */*__context*/);
  586. /// \param SharedsTy A type which contains references the shared variables.
  587. /// \param Shareds Context with the list of shared variables from the \p
  588. /// TaskFunction.
  589. /// \param Data Additional data for task generation like tiednsee, final
  590. /// state, list of privates etc.
  591. TaskResultTy emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
  592. const OMPExecutableDirective &D,
  593. llvm::Function *TaskFunction, QualType SharedsTy,
  594. Address Shareds, const OMPTaskDataTy &Data);
  595. /// Return the trip count of loops associated with constructs / 'target teams
  596. /// distribute' and 'teams distribute parallel for'. \param SizeEmitter Emits
  597. /// the int64 value for the number of iterations of the associated loop.
  598. llvm::Value *emitTargetNumIterationsCall(
  599. CodeGenFunction &CGF, const OMPExecutableDirective &D,
  600. llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
  601. const OMPLoopDirective &D)>
  602. SizeEmitter);
  603. /// Emit update for lastprivate conditional data.
  604. void emitLastprivateConditionalUpdate(CodeGenFunction &CGF, LValue IVLVal,
  605. StringRef UniqueDeclName, LValue LVal,
  606. SourceLocation Loc);
  607. /// Returns the number of the elements and the address of the depobj
  608. /// dependency array.
  609. /// \return Number of elements in depobj array and the pointer to the array of
  610. /// dependencies.
  611. std::pair<llvm::Value *, LValue> getDepobjElements(CodeGenFunction &CGF,
  612. LValue DepobjLVal,
  613. SourceLocation Loc);
  614. SmallVector<llvm::Value *, 4>
  615. emitDepobjElementsSizes(CodeGenFunction &CGF, QualType &KmpDependInfoTy,
  616. const OMPTaskDataTy::DependData &Data);
  617. void emitDepobjElements(CodeGenFunction &CGF, QualType &KmpDependInfoTy,
  618. LValue PosLVal, const OMPTaskDataTy::DependData &Data,
  619. Address DependenciesArray);
  620. public:
  621. explicit CGOpenMPRuntime(CodeGenModule &CGM);
  622. virtual ~CGOpenMPRuntime() {}
  623. virtual void clear();
  624. /// Returns true if the current target is a GPU.
  625. virtual bool isTargetCodegen() const { return false; }
  626. /// Emits code for OpenMP 'if' clause using specified \a CodeGen
  627. /// function. Here is the logic:
  628. /// if (Cond) {
  629. /// ThenGen();
  630. /// } else {
  631. /// ElseGen();
  632. /// }
  633. void emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
  634. const RegionCodeGenTy &ThenGen,
  635. const RegionCodeGenTy &ElseGen);
  636. /// Checks if the \p Body is the \a CompoundStmt and returns its child
  637. /// statement iff there is only one that is not evaluatable at the compile
  638. /// time.
  639. static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body);
  640. /// Get the platform-specific name separator.
  641. std::string getName(ArrayRef<StringRef> Parts) const;
  642. /// Emit code for the specified user defined reduction construct.
  643. virtual void emitUserDefinedReduction(CodeGenFunction *CGF,
  644. const OMPDeclareReductionDecl *D);
  645. /// Get combiner/initializer for the specified user-defined reduction, if any.
  646. virtual std::pair<llvm::Function *, llvm::Function *>
  647. getUserDefinedReduction(const OMPDeclareReductionDecl *D);
  648. /// Emit the function for the user defined mapper construct.
  649. void emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
  650. CodeGenFunction *CGF = nullptr);
  651. /// Get the function for the specified user-defined mapper. If it does not
  652. /// exist, create one.
  653. llvm::Function *
  654. getOrCreateUserDefinedMapperFunc(const OMPDeclareMapperDecl *D);
  655. /// Emits outlined function for the specified OpenMP parallel directive
  656. /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
  657. /// kmp_int32 BoundID, struct context_vars*).
  658. /// \param D OpenMP directive.
  659. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  660. /// \param InnermostKind Kind of innermost directive (for simple directives it
  661. /// is a directive itself, for combined - its innermost directive).
  662. /// \param CodeGen Code generation sequence for the \a D directive.
  663. virtual llvm::Function *emitParallelOutlinedFunction(
  664. const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
  665. OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
  666. /// Emits outlined function for the specified OpenMP teams directive
  667. /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
  668. /// kmp_int32 BoundID, struct context_vars*).
  669. /// \param D OpenMP directive.
  670. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  671. /// \param InnermostKind Kind of innermost directive (for simple directives it
  672. /// is a directive itself, for combined - its innermost directive).
  673. /// \param CodeGen Code generation sequence for the \a D directive.
  674. virtual llvm::Function *emitTeamsOutlinedFunction(
  675. const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
  676. OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen);
  677. /// Emits outlined function for the OpenMP task directive \a D. This
  678. /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
  679. /// TaskT).
  680. /// \param D OpenMP directive.
  681. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  682. /// \param PartIDVar Variable for partition id in the current OpenMP untied
  683. /// task region.
  684. /// \param TaskTVar Variable for task_t argument.
  685. /// \param InnermostKind Kind of innermost directive (for simple directives it
  686. /// is a directive itself, for combined - its innermost directive).
  687. /// \param CodeGen Code generation sequence for the \a D directive.
  688. /// \param Tied true if task is generated for tied task, false otherwise.
  689. /// \param NumberOfParts Number of parts in untied task. Ignored for tied
  690. /// tasks.
  691. ///
  692. virtual llvm::Function *emitTaskOutlinedFunction(
  693. const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
  694. const VarDecl *PartIDVar, const VarDecl *TaskTVar,
  695. OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
  696. bool Tied, unsigned &NumberOfParts);
  697. /// Cleans up references to the objects in finished function.
  698. ///
  699. virtual void functionFinished(CodeGenFunction &CGF);
  700. /// Emits code for parallel or serial call of the \a OutlinedFn with
  701. /// variables captured in a record which address is stored in \a
  702. /// CapturedStruct.
  703. /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
  704. /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
  705. /// \param CapturedVars A pointer to the record with the references to
  706. /// variables used in \a OutlinedFn function.
  707. /// \param IfCond Condition in the associated 'if' clause, if it was
  708. /// specified, nullptr otherwise.
  709. /// \param NumThreads The value corresponding to the num_threads clause, if
  710. /// any, or nullptr.
  711. ///
  712. virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
  713. llvm::Function *OutlinedFn,
  714. ArrayRef<llvm::Value *> CapturedVars,
  715. const Expr *IfCond, llvm::Value *NumThreads);
  716. /// Emits a critical region.
  717. /// \param CriticalName Name of the critical region.
  718. /// \param CriticalOpGen Generator for the statement associated with the given
  719. /// critical region.
  720. /// \param Hint Value of the 'hint' clause (optional).
  721. virtual void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
  722. const RegionCodeGenTy &CriticalOpGen,
  723. SourceLocation Loc,
  724. const Expr *Hint = nullptr);
  725. /// Emits a master region.
  726. /// \param MasterOpGen Generator for the statement associated with the given
  727. /// master region.
  728. virtual void emitMasterRegion(CodeGenFunction &CGF,
  729. const RegionCodeGenTy &MasterOpGen,
  730. SourceLocation Loc);
  731. /// Emits a masked region.
  732. /// \param MaskedOpGen Generator for the statement associated with the given
  733. /// masked region.
  734. virtual void emitMaskedRegion(CodeGenFunction &CGF,
  735. const RegionCodeGenTy &MaskedOpGen,
  736. SourceLocation Loc,
  737. const Expr *Filter = nullptr);
  738. /// Emits code for a taskyield directive.
  739. virtual void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc);
  740. /// Emit __kmpc_error call for error directive
  741. /// extern void __kmpc_error(ident_t *loc, int severity, const char *message);
  742. virtual void emitErrorCall(CodeGenFunction &CGF, SourceLocation Loc, Expr *ME,
  743. bool IsFatal);
  744. /// Emit a taskgroup region.
  745. /// \param TaskgroupOpGen Generator for the statement associated with the
  746. /// given taskgroup region.
  747. virtual void emitTaskgroupRegion(CodeGenFunction &CGF,
  748. const RegionCodeGenTy &TaskgroupOpGen,
  749. SourceLocation Loc);
  750. /// Emits a single region.
  751. /// \param SingleOpGen Generator for the statement associated with the given
  752. /// single region.
  753. virtual void emitSingleRegion(CodeGenFunction &CGF,
  754. const RegionCodeGenTy &SingleOpGen,
  755. SourceLocation Loc,
  756. ArrayRef<const Expr *> CopyprivateVars,
  757. ArrayRef<const Expr *> DestExprs,
  758. ArrayRef<const Expr *> SrcExprs,
  759. ArrayRef<const Expr *> AssignmentOps);
  760. /// Emit an ordered region.
  761. /// \param OrderedOpGen Generator for the statement associated with the given
  762. /// ordered region.
  763. virtual void emitOrderedRegion(CodeGenFunction &CGF,
  764. const RegionCodeGenTy &OrderedOpGen,
  765. SourceLocation Loc, bool IsThreads);
  766. /// Emit an implicit/explicit barrier for OpenMP threads.
  767. /// \param Kind Directive for which this implicit barrier call must be
  768. /// generated. Must be OMPD_barrier for explicit barrier generation.
  769. /// \param EmitChecks true if need to emit checks for cancellation barriers.
  770. /// \param ForceSimpleCall true simple barrier call must be emitted, false if
  771. /// runtime class decides which one to emit (simple or with cancellation
  772. /// checks).
  773. ///
  774. virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
  775. OpenMPDirectiveKind Kind,
  776. bool EmitChecks = true,
  777. bool ForceSimpleCall = false);
  778. /// Check if the specified \a ScheduleKind is static non-chunked.
  779. /// This kind of worksharing directive is emitted without outer loop.
  780. /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
  781. /// \param Chunked True if chunk is specified in the clause.
  782. ///
  783. virtual bool isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
  784. bool Chunked) const;
  785. /// Check if the specified \a ScheduleKind is static non-chunked.
  786. /// This kind of distribute directive is emitted without outer loop.
  787. /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
  788. /// \param Chunked True if chunk is specified in the clause.
  789. ///
  790. virtual bool isStaticNonchunked(OpenMPDistScheduleClauseKind ScheduleKind,
  791. bool Chunked) const;
  792. /// Check if the specified \a ScheduleKind is static chunked.
  793. /// \param ScheduleKind Schedule kind specified in the 'schedule' clause.
  794. /// \param Chunked True if chunk is specified in the clause.
  795. ///
  796. virtual bool isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
  797. bool Chunked) const;
  798. /// Check if the specified \a ScheduleKind is static non-chunked.
  799. /// \param ScheduleKind Schedule kind specified in the 'dist_schedule' clause.
  800. /// \param Chunked True if chunk is specified in the clause.
  801. ///
  802. virtual bool isStaticChunked(OpenMPDistScheduleClauseKind ScheduleKind,
  803. bool Chunked) const;
  804. /// Check if the specified \a ScheduleKind is dynamic.
  805. /// This kind of worksharing directive is emitted without outer loop.
  806. /// \param ScheduleKind Schedule Kind specified in the 'schedule' clause.
  807. ///
  808. virtual bool isDynamic(OpenMPScheduleClauseKind ScheduleKind) const;
  809. /// struct with the values to be passed to the dispatch runtime function
  810. struct DispatchRTInput {
  811. /// Loop lower bound
  812. llvm::Value *LB = nullptr;
  813. /// Loop upper bound
  814. llvm::Value *UB = nullptr;
  815. /// Chunk size specified using 'schedule' clause (nullptr if chunk
  816. /// was not specified)
  817. llvm::Value *Chunk = nullptr;
  818. DispatchRTInput() = default;
  819. DispatchRTInput(llvm::Value *LB, llvm::Value *UB, llvm::Value *Chunk)
  820. : LB(LB), UB(UB), Chunk(Chunk) {}
  821. };
  822. /// Call the appropriate runtime routine to initialize it before start
  823. /// of loop.
  824. /// This is used for non static scheduled types and when the ordered
  825. /// clause is present on the loop construct.
  826. /// Depending on the loop schedule, it is necessary to call some runtime
  827. /// routine before start of the OpenMP loop to get the loop upper / lower
  828. /// bounds \a LB and \a UB and stride \a ST.
  829. ///
  830. /// \param CGF Reference to current CodeGenFunction.
  831. /// \param Loc Clang source location.
  832. /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
  833. /// \param IVSize Size of the iteration variable in bits.
  834. /// \param IVSigned Sign of the iteration variable.
  835. /// \param Ordered true if loop is ordered, false otherwise.
  836. /// \param DispatchValues struct containing llvm values for lower bound, upper
  837. /// bound, and chunk expression.
  838. /// For the default (nullptr) value, the chunk 1 will be used.
  839. ///
  840. virtual void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
  841. const OpenMPScheduleTy &ScheduleKind,
  842. unsigned IVSize, bool IVSigned, bool Ordered,
  843. const DispatchRTInput &DispatchValues);
  844. /// Struct with the values to be passed to the static runtime function
  845. struct StaticRTInput {
  846. /// Size of the iteration variable in bits.
  847. unsigned IVSize = 0;
  848. /// Sign of the iteration variable.
  849. bool IVSigned = false;
  850. /// true if loop is ordered, false otherwise.
  851. bool Ordered = false;
  852. /// Address of the output variable in which the flag of the last iteration
  853. /// is returned.
  854. Address IL = Address::invalid();
  855. /// Address of the output variable in which the lower iteration number is
  856. /// returned.
  857. Address LB = Address::invalid();
  858. /// Address of the output variable in which the upper iteration number is
  859. /// returned.
  860. Address UB = Address::invalid();
  861. /// Address of the output variable in which the stride value is returned
  862. /// necessary to generated the static_chunked scheduled loop.
  863. Address ST = Address::invalid();
  864. /// Value of the chunk for the static_chunked scheduled loop. For the
  865. /// default (nullptr) value, the chunk 1 will be used.
  866. llvm::Value *Chunk = nullptr;
  867. StaticRTInput(unsigned IVSize, bool IVSigned, bool Ordered, Address IL,
  868. Address LB, Address UB, Address ST,
  869. llvm::Value *Chunk = nullptr)
  870. : IVSize(IVSize), IVSigned(IVSigned), Ordered(Ordered), IL(IL), LB(LB),
  871. UB(UB), ST(ST), Chunk(Chunk) {}
  872. };
  873. /// Call the appropriate runtime routine to initialize it before start
  874. /// of loop.
  875. ///
  876. /// This is used only in case of static schedule, when the user did not
  877. /// specify a ordered clause on the loop construct.
  878. /// Depending on the loop schedule, it is necessary to call some runtime
  879. /// routine before start of the OpenMP loop to get the loop upper / lower
  880. /// bounds LB and UB and stride ST.
  881. ///
  882. /// \param CGF Reference to current CodeGenFunction.
  883. /// \param Loc Clang source location.
  884. /// \param DKind Kind of the directive.
  885. /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
  886. /// \param Values Input arguments for the construct.
  887. ///
  888. virtual void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
  889. OpenMPDirectiveKind DKind,
  890. const OpenMPScheduleTy &ScheduleKind,
  891. const StaticRTInput &Values);
  892. ///
  893. /// \param CGF Reference to current CodeGenFunction.
  894. /// \param Loc Clang source location.
  895. /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
  896. /// \param Values Input arguments for the construct.
  897. ///
  898. virtual void emitDistributeStaticInit(CodeGenFunction &CGF,
  899. SourceLocation Loc,
  900. OpenMPDistScheduleClauseKind SchedKind,
  901. const StaticRTInput &Values);
  902. /// Call the appropriate runtime routine to notify that we finished
  903. /// iteration of the ordered loop with the dynamic scheduling.
  904. ///
  905. /// \param CGF Reference to current CodeGenFunction.
  906. /// \param Loc Clang source location.
  907. /// \param IVSize Size of the iteration variable in bits.
  908. /// \param IVSigned Sign of the iteration variable.
  909. ///
  910. virtual void emitForOrderedIterationEnd(CodeGenFunction &CGF,
  911. SourceLocation Loc, unsigned IVSize,
  912. bool IVSigned);
  913. /// Call the appropriate runtime routine to notify that we finished
  914. /// all the work with current loop.
  915. ///
  916. /// \param CGF Reference to current CodeGenFunction.
  917. /// \param Loc Clang source location.
  918. /// \param DKind Kind of the directive for which the static finish is emitted.
  919. ///
  920. virtual void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
  921. OpenMPDirectiveKind DKind);
  922. /// Call __kmpc_dispatch_next(
  923. /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
  924. /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
  925. /// kmp_int[32|64] *p_stride);
  926. /// \param IVSize Size of the iteration variable in bits.
  927. /// \param IVSigned Sign of the iteration variable.
  928. /// \param IL Address of the output variable in which the flag of the
  929. /// last iteration is returned.
  930. /// \param LB Address of the output variable in which the lower iteration
  931. /// number is returned.
  932. /// \param UB Address of the output variable in which the upper iteration
  933. /// number is returned.
  934. /// \param ST Address of the output variable in which the stride value is
  935. /// returned.
  936. virtual llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
  937. unsigned IVSize, bool IVSigned,
  938. Address IL, Address LB,
  939. Address UB, Address ST);
  940. /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
  941. /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
  942. /// clause.
  943. /// \param NumThreads An integer value of threads.
  944. virtual void emitNumThreadsClause(CodeGenFunction &CGF,
  945. llvm::Value *NumThreads,
  946. SourceLocation Loc);
  947. /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
  948. /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
  949. virtual void emitProcBindClause(CodeGenFunction &CGF,
  950. llvm::omp::ProcBindKind ProcBind,
  951. SourceLocation Loc);
  952. /// Returns address of the threadprivate variable for the current
  953. /// thread.
  954. /// \param VD Threadprivate variable.
  955. /// \param VDAddr Address of the global variable \a VD.
  956. /// \param Loc Location of the reference to threadprivate var.
  957. /// \return Address of the threadprivate variable for the current thread.
  958. virtual Address getAddrOfThreadPrivate(CodeGenFunction &CGF,
  959. const VarDecl *VD,
  960. Address VDAddr,
  961. SourceLocation Loc);
  962. /// Returns the address of the variable marked as declare target with link
  963. /// clause OR as declare target with to clause and unified memory.
  964. virtual Address getAddrOfDeclareTargetVar(const VarDecl *VD);
  965. /// Emit a code for initialization of threadprivate variable. It emits
  966. /// a call to runtime library which adds initial value to the newly created
  967. /// threadprivate variable (if it is not constant) and registers destructor
  968. /// for the variable (if any).
  969. /// \param VD Threadprivate variable.
  970. /// \param VDAddr Address of the global variable \a VD.
  971. /// \param Loc Location of threadprivate declaration.
  972. /// \param PerformInit true if initialization expression is not constant.
  973. virtual llvm::Function *
  974. emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
  975. SourceLocation Loc, bool PerformInit,
  976. CodeGenFunction *CGF = nullptr);
  977. /// Emit a code for initialization of declare target variable.
  978. /// \param VD Declare target variable.
  979. /// \param Addr Address of the global variable \a VD.
  980. /// \param PerformInit true if initialization expression is not constant.
  981. virtual bool emitDeclareTargetVarDefinition(const VarDecl *VD,
  982. llvm::GlobalVariable *Addr,
  983. bool PerformInit);
  984. /// Creates artificial threadprivate variable with name \p Name and type \p
  985. /// VarType.
  986. /// \param VarType Type of the artificial threadprivate variable.
  987. /// \param Name Name of the artificial threadprivate variable.
  988. virtual Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
  989. QualType VarType,
  990. StringRef Name);
  991. /// Emit flush of the variables specified in 'omp flush' directive.
  992. /// \param Vars List of variables to flush.
  993. virtual void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
  994. SourceLocation Loc, llvm::AtomicOrdering AO);
  995. /// Emit task region for the task directive. The task region is
  996. /// emitted in several steps:
  997. /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
  998. /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
  999. /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
  1000. /// function:
  1001. /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
  1002. /// TaskFunction(gtid, tt->part_id, tt->shareds);
  1003. /// return 0;
  1004. /// }
  1005. /// 2. Copy a list of shared variables to field shareds of the resulting
  1006. /// structure kmp_task_t returned by the previous call (if any).
  1007. /// 3. Copy a pointer to destructions function to field destructions of the
  1008. /// resulting structure kmp_task_t.
  1009. /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
  1010. /// kmp_task_t *new_task), where new_task is a resulting structure from
  1011. /// previous items.
  1012. /// \param D Current task directive.
  1013. /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
  1014. /// /*part_id*/, captured_struct */*__context*/);
  1015. /// \param SharedsTy A type which contains references the shared variables.
  1016. /// \param Shareds Context with the list of shared variables from the \p
  1017. /// TaskFunction.
  1018. /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
  1019. /// otherwise.
  1020. /// \param Data Additional data for task generation like tiednsee, final
  1021. /// state, list of privates etc.
  1022. virtual void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
  1023. const OMPExecutableDirective &D,
  1024. llvm::Function *TaskFunction, QualType SharedsTy,
  1025. Address Shareds, const Expr *IfCond,
  1026. const OMPTaskDataTy &Data);
  1027. /// Emit task region for the taskloop directive. The taskloop region is
  1028. /// emitted in several steps:
  1029. /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
  1030. /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
  1031. /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
  1032. /// function:
  1033. /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
  1034. /// TaskFunction(gtid, tt->part_id, tt->shareds);
  1035. /// return 0;
  1036. /// }
  1037. /// 2. Copy a list of shared variables to field shareds of the resulting
  1038. /// structure kmp_task_t returned by the previous call (if any).
  1039. /// 3. Copy a pointer to destructions function to field destructions of the
  1040. /// resulting structure kmp_task_t.
  1041. /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
  1042. /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
  1043. /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
  1044. /// is a resulting structure from
  1045. /// previous items.
  1046. /// \param D Current task directive.
  1047. /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
  1048. /// /*part_id*/, captured_struct */*__context*/);
  1049. /// \param SharedsTy A type which contains references the shared variables.
  1050. /// \param Shareds Context with the list of shared variables from the \p
  1051. /// TaskFunction.
  1052. /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
  1053. /// otherwise.
  1054. /// \param Data Additional data for task generation like tiednsee, final
  1055. /// state, list of privates etc.
  1056. virtual void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
  1057. const OMPLoopDirective &D,
  1058. llvm::Function *TaskFunction,
  1059. QualType SharedsTy, Address Shareds,
  1060. const Expr *IfCond, const OMPTaskDataTy &Data);
  1061. /// Emit code for the directive that does not require outlining.
  1062. ///
  1063. /// \param InnermostKind Kind of innermost directive (for simple directives it
  1064. /// is a directive itself, for combined - its innermost directive).
  1065. /// \param CodeGen Code generation sequence for the \a D directive.
  1066. /// \param HasCancel true if region has inner cancel directive, false
  1067. /// otherwise.
  1068. virtual void emitInlinedDirective(CodeGenFunction &CGF,
  1069. OpenMPDirectiveKind InnermostKind,
  1070. const RegionCodeGenTy &CodeGen,
  1071. bool HasCancel = false);
  1072. /// Emits reduction function.
  1073. /// \param ArgsElemType Array type containing pointers to reduction variables.
  1074. /// \param Privates List of private copies for original reduction arguments.
  1075. /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
  1076. /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
  1077. /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
  1078. /// or 'operator binop(LHS, RHS)'.
  1079. llvm::Function *emitReductionFunction(SourceLocation Loc,
  1080. llvm::Type *ArgsElemType,
  1081. ArrayRef<const Expr *> Privates,
  1082. ArrayRef<const Expr *> LHSExprs,
  1083. ArrayRef<const Expr *> RHSExprs,
  1084. ArrayRef<const Expr *> ReductionOps);
  1085. /// Emits single reduction combiner
  1086. void emitSingleReductionCombiner(CodeGenFunction &CGF,
  1087. const Expr *ReductionOp,
  1088. const Expr *PrivateRef,
  1089. const DeclRefExpr *LHS,
  1090. const DeclRefExpr *RHS);
  1091. struct ReductionOptionsTy {
  1092. bool WithNowait;
  1093. bool SimpleReduction;
  1094. OpenMPDirectiveKind ReductionKind;
  1095. };
  1096. /// Emit a code for reduction clause. Next code should be emitted for
  1097. /// reduction:
  1098. /// \code
  1099. ///
  1100. /// static kmp_critical_name lock = { 0 };
  1101. ///
  1102. /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
  1103. /// ...
  1104. /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
  1105. /// ...
  1106. /// }
  1107. ///
  1108. /// ...
  1109. /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
  1110. /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
  1111. /// RedList, reduce_func, &<lock>)) {
  1112. /// case 1:
  1113. /// ...
  1114. /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
  1115. /// ...
  1116. /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
  1117. /// break;
  1118. /// case 2:
  1119. /// ...
  1120. /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
  1121. /// ...
  1122. /// break;
  1123. /// default:;
  1124. /// }
  1125. /// \endcode
  1126. ///
  1127. /// \param Privates List of private copies for original reduction arguments.
  1128. /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
  1129. /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
  1130. /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
  1131. /// or 'operator binop(LHS, RHS)'.
  1132. /// \param Options List of options for reduction codegen:
  1133. /// WithNowait true if parent directive has also nowait clause, false
  1134. /// otherwise.
  1135. /// SimpleReduction Emit reduction operation only. Used for omp simd
  1136. /// directive on the host.
  1137. /// ReductionKind The kind of reduction to perform.
  1138. virtual void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
  1139. ArrayRef<const Expr *> Privates,
  1140. ArrayRef<const Expr *> LHSExprs,
  1141. ArrayRef<const Expr *> RHSExprs,
  1142. ArrayRef<const Expr *> ReductionOps,
  1143. ReductionOptionsTy Options);
  1144. /// Emit a code for initialization of task reduction clause. Next code
  1145. /// should be emitted for reduction:
  1146. /// \code
  1147. ///
  1148. /// _taskred_item_t red_data[n];
  1149. /// ...
  1150. /// red_data[i].shar = &shareds[i];
  1151. /// red_data[i].orig = &origs[i];
  1152. /// red_data[i].size = sizeof(origs[i]);
  1153. /// red_data[i].f_init = (void*)RedInit<i>;
  1154. /// red_data[i].f_fini = (void*)RedDest<i>;
  1155. /// red_data[i].f_comb = (void*)RedOp<i>;
  1156. /// red_data[i].flags = <Flag_i>;
  1157. /// ...
  1158. /// void* tg1 = __kmpc_taskred_init(gtid, n, red_data);
  1159. /// \endcode
  1160. /// For reduction clause with task modifier it emits the next call:
  1161. /// \code
  1162. ///
  1163. /// _taskred_item_t red_data[n];
  1164. /// ...
  1165. /// red_data[i].shar = &shareds[i];
  1166. /// red_data[i].orig = &origs[i];
  1167. /// red_data[i].size = sizeof(origs[i]);
  1168. /// red_data[i].f_init = (void*)RedInit<i>;
  1169. /// red_data[i].f_fini = (void*)RedDest<i>;
  1170. /// red_data[i].f_comb = (void*)RedOp<i>;
  1171. /// red_data[i].flags = <Flag_i>;
  1172. /// ...
  1173. /// void* tg1 = __kmpc_taskred_modifier_init(loc, gtid, is_worksharing, n,
  1174. /// red_data);
  1175. /// \endcode
  1176. /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
  1177. /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
  1178. /// \param Data Additional data for task generation like tiedness, final
  1179. /// state, list of privates, reductions etc.
  1180. virtual llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF,
  1181. SourceLocation Loc,
  1182. ArrayRef<const Expr *> LHSExprs,
  1183. ArrayRef<const Expr *> RHSExprs,
  1184. const OMPTaskDataTy &Data);
  1185. /// Emits the following code for reduction clause with task modifier:
  1186. /// \code
  1187. /// __kmpc_task_reduction_modifier_fini(loc, gtid, is_worksharing);
  1188. /// \endcode
  1189. virtual void emitTaskReductionFini(CodeGenFunction &CGF, SourceLocation Loc,
  1190. bool IsWorksharingReduction);
  1191. /// Required to resolve existing problems in the runtime. Emits threadprivate
  1192. /// variables to store the size of the VLAs/array sections for
  1193. /// initializer/combiner/finalizer functions.
  1194. /// \param RCG Allows to reuse an existing data for the reductions.
  1195. /// \param N Reduction item for which fixups must be emitted.
  1196. virtual void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
  1197. ReductionCodeGen &RCG, unsigned N);
  1198. /// Get the address of `void *` type of the privatue copy of the reduction
  1199. /// item specified by the \p SharedLVal.
  1200. /// \param ReductionsPtr Pointer to the reduction data returned by the
  1201. /// emitTaskReductionInit function.
  1202. /// \param SharedLVal Address of the original reduction item.
  1203. virtual Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
  1204. llvm::Value *ReductionsPtr,
  1205. LValue SharedLVal);
  1206. /// Emit code for 'taskwait' directive.
  1207. virtual void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc,
  1208. const OMPTaskDataTy &Data);
  1209. /// Emit code for 'cancellation point' construct.
  1210. /// \param CancelRegion Region kind for which the cancellation point must be
  1211. /// emitted.
  1212. ///
  1213. virtual void emitCancellationPointCall(CodeGenFunction &CGF,
  1214. SourceLocation Loc,
  1215. OpenMPDirectiveKind CancelRegion);
  1216. /// Emit code for 'cancel' construct.
  1217. /// \param IfCond Condition in the associated 'if' clause, if it was
  1218. /// specified, nullptr otherwise.
  1219. /// \param CancelRegion Region kind for which the cancel must be emitted.
  1220. ///
  1221. virtual void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
  1222. const Expr *IfCond,
  1223. OpenMPDirectiveKind CancelRegion);
  1224. /// Emit outilined function for 'target' directive.
  1225. /// \param D Directive to emit.
  1226. /// \param ParentName Name of the function that encloses the target region.
  1227. /// \param OutlinedFn Outlined function value to be defined by this call.
  1228. /// \param OutlinedFnID Outlined function ID value to be defined by this call.
  1229. /// \param IsOffloadEntry True if the outlined function is an offload entry.
  1230. /// \param CodeGen Code generation sequence for the \a D directive.
  1231. /// An outlined function may not be an entry if, e.g. the if clause always
  1232. /// evaluates to false.
  1233. virtual void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
  1234. StringRef ParentName,
  1235. llvm::Function *&OutlinedFn,
  1236. llvm::Constant *&OutlinedFnID,
  1237. bool IsOffloadEntry,
  1238. const RegionCodeGenTy &CodeGen);
  1239. /// Emit the target offloading code associated with \a D. The emitted
  1240. /// code attempts offloading the execution to the device, an the event of
  1241. /// a failure it executes the host version outlined in \a OutlinedFn.
  1242. /// \param D Directive to emit.
  1243. /// \param OutlinedFn Host version of the code to be offloaded.
  1244. /// \param OutlinedFnID ID of host version of the code to be offloaded.
  1245. /// \param IfCond Expression evaluated in if clause associated with the target
  1246. /// directive, or null if no if clause is used.
  1247. /// \param Device Expression evaluated in device clause associated with the
  1248. /// target directive, or null if no device clause is used and device modifier.
  1249. /// \param SizeEmitter Callback to emit number of iterations for loop-based
  1250. /// directives.
  1251. virtual void emitTargetCall(
  1252. CodeGenFunction &CGF, const OMPExecutableDirective &D,
  1253. llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond,
  1254. llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
  1255. llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
  1256. const OMPLoopDirective &D)>
  1257. SizeEmitter);
  1258. /// Emit the target regions enclosed in \a GD function definition or
  1259. /// the function itself in case it is a valid device function. Returns true if
  1260. /// \a GD was dealt with successfully.
  1261. /// \param GD Function to scan.
  1262. virtual bool emitTargetFunctions(GlobalDecl GD);
  1263. /// Emit the global variable if it is a valid device global variable.
  1264. /// Returns true if \a GD was dealt with successfully.
  1265. /// \param GD Variable declaration to emit.
  1266. virtual bool emitTargetGlobalVariable(GlobalDecl GD);
  1267. /// Checks if the provided global decl \a GD is a declare target variable and
  1268. /// registers it when emitting code for the host.
  1269. virtual void registerTargetGlobalVariable(const VarDecl *VD,
  1270. llvm::Constant *Addr);
  1271. /// Emit the global \a GD if it is meaningful for the target. Returns
  1272. /// if it was emitted successfully.
  1273. /// \param GD Global to scan.
  1274. virtual bool emitTargetGlobal(GlobalDecl GD);
  1275. /// Creates and returns a registration function for when at least one
  1276. /// requires directives was used in the current module.
  1277. llvm::Function *emitRequiresDirectiveRegFun();
  1278. /// Creates all the offload entries in the current compilation unit
  1279. /// along with the associated metadata.
  1280. void createOffloadEntriesAndInfoMetadata();
  1281. /// Emits code for teams call of the \a OutlinedFn with
  1282. /// variables captured in a record which address is stored in \a
  1283. /// CapturedStruct.
  1284. /// \param OutlinedFn Outlined function to be run by team masters. Type of
  1285. /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
  1286. /// \param CapturedVars A pointer to the record with the references to
  1287. /// variables used in \a OutlinedFn function.
  1288. ///
  1289. virtual void emitTeamsCall(CodeGenFunction &CGF,
  1290. const OMPExecutableDirective &D,
  1291. SourceLocation Loc, llvm::Function *OutlinedFn,
  1292. ArrayRef<llvm::Value *> CapturedVars);
  1293. /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
  1294. /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
  1295. /// for num_teams clause.
  1296. /// \param NumTeams An integer expression of teams.
  1297. /// \param ThreadLimit An integer expression of threads.
  1298. virtual void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
  1299. const Expr *ThreadLimit, SourceLocation Loc);
  1300. /// Struct that keeps all the relevant information that should be kept
  1301. /// throughout a 'target data' region.
  1302. class TargetDataInfo : public llvm::OpenMPIRBuilder::TargetDataInfo {
  1303. public:
  1304. explicit TargetDataInfo() : llvm::OpenMPIRBuilder::TargetDataInfo() {}
  1305. explicit TargetDataInfo(bool RequiresDevicePointerInfo,
  1306. bool SeparateBeginEndCalls)
  1307. : llvm::OpenMPIRBuilder::TargetDataInfo(RequiresDevicePointerInfo,
  1308. SeparateBeginEndCalls) {}
  1309. /// Map between the a declaration of a capture and the corresponding base
  1310. /// pointer address where the runtime returns the device pointers.
  1311. llvm::DenseMap<const ValueDecl *, Address> CaptureDeviceAddrMap;
  1312. };
  1313. /// Emit the target data mapping code associated with \a D.
  1314. /// \param D Directive to emit.
  1315. /// \param IfCond Expression evaluated in if clause associated with the
  1316. /// target directive, or null if no device clause is used.
  1317. /// \param Device Expression evaluated in device clause associated with the
  1318. /// target directive, or null if no device clause is used.
  1319. /// \param Info A record used to store information that needs to be preserved
  1320. /// until the region is closed.
  1321. virtual void emitTargetDataCalls(CodeGenFunction &CGF,
  1322. const OMPExecutableDirective &D,
  1323. const Expr *IfCond, const Expr *Device,
  1324. const RegionCodeGenTy &CodeGen,
  1325. CGOpenMPRuntime::TargetDataInfo &Info);
  1326. /// Emit the data mapping/movement code associated with the directive
  1327. /// \a D that should be of the form 'target [{enter|exit} data | update]'.
  1328. /// \param D Directive to emit.
  1329. /// \param IfCond Expression evaluated in if clause associated with the target
  1330. /// directive, or null if no if clause is used.
  1331. /// \param Device Expression evaluated in device clause associated with the
  1332. /// target directive, or null if no device clause is used.
  1333. virtual void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
  1334. const OMPExecutableDirective &D,
  1335. const Expr *IfCond,
  1336. const Expr *Device);
  1337. /// Marks function \a Fn with properly mangled versions of vector functions.
  1338. /// \param FD Function marked as 'declare simd'.
  1339. /// \param Fn LLVM function that must be marked with 'declare simd'
  1340. /// attributes.
  1341. virtual void emitDeclareSimdFunction(const FunctionDecl *FD,
  1342. llvm::Function *Fn);
  1343. /// Emit initialization for doacross loop nesting support.
  1344. /// \param D Loop-based construct used in doacross nesting construct.
  1345. virtual void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
  1346. ArrayRef<Expr *> NumIterations);
  1347. /// Emit code for doacross ordered directive with 'depend' clause.
  1348. /// \param C 'depend' clause with 'sink|source' dependency kind.
  1349. virtual void emitDoacrossOrdered(CodeGenFunction &CGF,
  1350. const OMPDependClause *C);
  1351. /// Translates the native parameter of outlined function if this is required
  1352. /// for target.
  1353. /// \param FD Field decl from captured record for the parameter.
  1354. /// \param NativeParam Parameter itself.
  1355. virtual const VarDecl *translateParameter(const FieldDecl *FD,
  1356. const VarDecl *NativeParam) const {
  1357. return NativeParam;
  1358. }
  1359. /// Gets the address of the native argument basing on the address of the
  1360. /// target-specific parameter.
  1361. /// \param NativeParam Parameter itself.
  1362. /// \param TargetParam Corresponding target-specific parameter.
  1363. virtual Address getParameterAddress(CodeGenFunction &CGF,
  1364. const VarDecl *NativeParam,
  1365. const VarDecl *TargetParam) const;
  1366. /// Choose default schedule type and chunk value for the
  1367. /// dist_schedule clause.
  1368. virtual void getDefaultDistScheduleAndChunk(CodeGenFunction &CGF,
  1369. const OMPLoopDirective &S, OpenMPDistScheduleClauseKind &ScheduleKind,
  1370. llvm::Value *&Chunk) const {}
  1371. /// Choose default schedule type and chunk value for the
  1372. /// schedule clause.
  1373. virtual void getDefaultScheduleAndChunk(CodeGenFunction &CGF,
  1374. const OMPLoopDirective &S, OpenMPScheduleClauseKind &ScheduleKind,
  1375. const Expr *&ChunkExpr) const;
  1376. /// Emits call of the outlined function with the provided arguments,
  1377. /// translating these arguments to correct target-specific arguments.
  1378. virtual void
  1379. emitOutlinedFunctionCall(CodeGenFunction &CGF, SourceLocation Loc,
  1380. llvm::FunctionCallee OutlinedFn,
  1381. ArrayRef<llvm::Value *> Args = std::nullopt) const;
  1382. /// Emits OpenMP-specific function prolog.
  1383. /// Required for device constructs.
  1384. virtual void emitFunctionProlog(CodeGenFunction &CGF, const Decl *D);
  1385. /// Gets the OpenMP-specific address of the local variable.
  1386. virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF,
  1387. const VarDecl *VD);
  1388. /// Marks the declaration as already emitted for the device code and returns
  1389. /// true, if it was marked already, and false, otherwise.
  1390. bool markAsGlobalTarget(GlobalDecl GD);
  1391. /// Emit deferred declare target variables marked for deferred emission.
  1392. void emitDeferredTargetDecls() const;
  1393. /// Adjust some parameters for the target-based directives, like addresses of
  1394. /// the variables captured by reference in lambdas.
  1395. virtual void
  1396. adjustTargetSpecificDataForLambdas(CodeGenFunction &CGF,
  1397. const OMPExecutableDirective &D) const;
  1398. /// Perform check on requires decl to ensure that target architecture
  1399. /// supports unified addressing
  1400. virtual void processRequiresDirective(const OMPRequiresDecl *D);
  1401. /// Gets default memory ordering as specified in requires directive.
  1402. llvm::AtomicOrdering getDefaultMemoryOrdering() const;
  1403. /// Checks if the variable has associated OMPAllocateDeclAttr attribute with
  1404. /// the predefined allocator and translates it into the corresponding address
  1405. /// space.
  1406. virtual bool hasAllocateAttributeForGlobalVar(const VarDecl *VD, LangAS &AS);
  1407. /// Return whether the unified_shared_memory has been specified.
  1408. bool hasRequiresUnifiedSharedMemory() const;
  1409. /// Checks if the \p VD variable is marked as nontemporal declaration in
  1410. /// current context.
  1411. bool isNontemporalDecl(const ValueDecl *VD) const;
  1412. /// Create specialized alloca to handle lastprivate conditionals.
  1413. Address emitLastprivateConditionalInit(CodeGenFunction &CGF,
  1414. const VarDecl *VD);
  1415. /// Checks if the provided \p LVal is lastprivate conditional and emits the
  1416. /// code to update the value of the original variable.
  1417. /// \code
  1418. /// lastprivate(conditional: a)
  1419. /// ...
  1420. /// <type> a;
  1421. /// lp_a = ...;
  1422. /// #pragma omp critical(a)
  1423. /// if (last_iv_a <= iv) {
  1424. /// last_iv_a = iv;
  1425. /// global_a = lp_a;
  1426. /// }
  1427. /// \endcode
  1428. virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF,
  1429. const Expr *LHS);
  1430. /// Checks if the lastprivate conditional was updated in inner region and
  1431. /// writes the value.
  1432. /// \code
  1433. /// lastprivate(conditional: a)
  1434. /// ...
  1435. /// <type> a;bool Fired = false;
  1436. /// #pragma omp ... shared(a)
  1437. /// {
  1438. /// lp_a = ...;
  1439. /// Fired = true;
  1440. /// }
  1441. /// if (Fired) {
  1442. /// #pragma omp critical(a)
  1443. /// if (last_iv_a <= iv) {
  1444. /// last_iv_a = iv;
  1445. /// global_a = lp_a;
  1446. /// }
  1447. /// Fired = false;
  1448. /// }
  1449. /// \endcode
  1450. virtual void checkAndEmitSharedLastprivateConditional(
  1451. CodeGenFunction &CGF, const OMPExecutableDirective &D,
  1452. const llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> &IgnoredDecls);
  1453. /// Gets the address of the global copy used for lastprivate conditional
  1454. /// update, if any.
  1455. /// \param PrivLVal LValue for the private copy.
  1456. /// \param VD Original lastprivate declaration.
  1457. virtual void emitLastprivateConditionalFinalUpdate(CodeGenFunction &CGF,
  1458. LValue PrivLVal,
  1459. const VarDecl *VD,
  1460. SourceLocation Loc);
  1461. /// Emits list of dependecies based on the provided data (array of
  1462. /// dependence/expression pairs).
  1463. /// \returns Pointer to the first element of the array casted to VoidPtr type.
  1464. std::pair<llvm::Value *, Address>
  1465. emitDependClause(CodeGenFunction &CGF,
  1466. ArrayRef<OMPTaskDataTy::DependData> Dependencies,
  1467. SourceLocation Loc);
  1468. /// Emits list of dependecies based on the provided data (array of
  1469. /// dependence/expression pairs) for depobj construct. In this case, the
  1470. /// variable is allocated in dynamically. \returns Pointer to the first
  1471. /// element of the array casted to VoidPtr type.
  1472. Address emitDepobjDependClause(CodeGenFunction &CGF,
  1473. const OMPTaskDataTy::DependData &Dependencies,
  1474. SourceLocation Loc);
  1475. /// Emits the code to destroy the dependency object provided in depobj
  1476. /// directive.
  1477. void emitDestroyClause(CodeGenFunction &CGF, LValue DepobjLVal,
  1478. SourceLocation Loc);
  1479. /// Updates the dependency kind in the specified depobj object.
  1480. /// \param DepobjLVal LValue for the main depobj object.
  1481. /// \param NewDepKind New dependency kind.
  1482. void emitUpdateClause(CodeGenFunction &CGF, LValue DepobjLVal,
  1483. OpenMPDependClauseKind NewDepKind, SourceLocation Loc);
  1484. /// Initializes user defined allocators specified in the uses_allocators
  1485. /// clauses.
  1486. void emitUsesAllocatorsInit(CodeGenFunction &CGF, const Expr *Allocator,
  1487. const Expr *AllocatorTraits);
  1488. /// Destroys user defined allocators specified in the uses_allocators clause.
  1489. void emitUsesAllocatorsFini(CodeGenFunction &CGF, const Expr *Allocator);
  1490. /// Returns true if the variable is a local variable in untied task.
  1491. bool isLocalVarInUntiedTask(CodeGenFunction &CGF, const VarDecl *VD) const;
  1492. };
  1493. /// Class supports emissionof SIMD-only code.
  1494. class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
  1495. public:
  1496. explicit CGOpenMPSIMDRuntime(CodeGenModule &CGM) : CGOpenMPRuntime(CGM) {}
  1497. ~CGOpenMPSIMDRuntime() override {}
  1498. /// Emits outlined function for the specified OpenMP parallel directive
  1499. /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
  1500. /// kmp_int32 BoundID, struct context_vars*).
  1501. /// \param D OpenMP directive.
  1502. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  1503. /// \param InnermostKind Kind of innermost directive (for simple directives it
  1504. /// is a directive itself, for combined - its innermost directive).
  1505. /// \param CodeGen Code generation sequence for the \a D directive.
  1506. llvm::Function *
  1507. emitParallelOutlinedFunction(const OMPExecutableDirective &D,
  1508. const VarDecl *ThreadIDVar,
  1509. OpenMPDirectiveKind InnermostKind,
  1510. const RegionCodeGenTy &CodeGen) override;
  1511. /// Emits outlined function for the specified OpenMP teams directive
  1512. /// \a D. This outlined function has type void(*)(kmp_int32 *ThreadID,
  1513. /// kmp_int32 BoundID, struct context_vars*).
  1514. /// \param D OpenMP directive.
  1515. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  1516. /// \param InnermostKind Kind of innermost directive (for simple directives it
  1517. /// is a directive itself, for combined - its innermost directive).
  1518. /// \param CodeGen Code generation sequence for the \a D directive.
  1519. llvm::Function *
  1520. emitTeamsOutlinedFunction(const OMPExecutableDirective &D,
  1521. const VarDecl *ThreadIDVar,
  1522. OpenMPDirectiveKind InnermostKind,
  1523. const RegionCodeGenTy &CodeGen) override;
  1524. /// Emits outlined function for the OpenMP task directive \a D. This
  1525. /// outlined function has type void(*)(kmp_int32 ThreadID, struct task_t*
  1526. /// TaskT).
  1527. /// \param D OpenMP directive.
  1528. /// \param ThreadIDVar Variable for thread id in the current OpenMP region.
  1529. /// \param PartIDVar Variable for partition id in the current OpenMP untied
  1530. /// task region.
  1531. /// \param TaskTVar Variable for task_t argument.
  1532. /// \param InnermostKind Kind of innermost directive (for simple directives it
  1533. /// is a directive itself, for combined - its innermost directive).
  1534. /// \param CodeGen Code generation sequence for the \a D directive.
  1535. /// \param Tied true if task is generated for tied task, false otherwise.
  1536. /// \param NumberOfParts Number of parts in untied task. Ignored for tied
  1537. /// tasks.
  1538. ///
  1539. llvm::Function *emitTaskOutlinedFunction(
  1540. const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
  1541. const VarDecl *PartIDVar, const VarDecl *TaskTVar,
  1542. OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
  1543. bool Tied, unsigned &NumberOfParts) override;
  1544. /// Emits code for parallel or serial call of the \a OutlinedFn with
  1545. /// variables captured in a record which address is stored in \a
  1546. /// CapturedStruct.
  1547. /// \param OutlinedFn Outlined function to be run in parallel threads. Type of
  1548. /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
  1549. /// \param CapturedVars A pointer to the record with the references to
  1550. /// variables used in \a OutlinedFn function.
  1551. /// \param IfCond Condition in the associated 'if' clause, if it was
  1552. /// specified, nullptr otherwise.
  1553. /// \param NumThreads The value corresponding to the num_threads clause, if
  1554. /// any, or nullptr.
  1555. ///
  1556. void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
  1557. llvm::Function *OutlinedFn,
  1558. ArrayRef<llvm::Value *> CapturedVars,
  1559. const Expr *IfCond, llvm::Value *NumThreads) override;
  1560. /// Emits a critical region.
  1561. /// \param CriticalName Name of the critical region.
  1562. /// \param CriticalOpGen Generator for the statement associated with the given
  1563. /// critical region.
  1564. /// \param Hint Value of the 'hint' clause (optional).
  1565. void emitCriticalRegion(CodeGenFunction &CGF, StringRef CriticalName,
  1566. const RegionCodeGenTy &CriticalOpGen,
  1567. SourceLocation Loc,
  1568. const Expr *Hint = nullptr) override;
  1569. /// Emits a master region.
  1570. /// \param MasterOpGen Generator for the statement associated with the given
  1571. /// master region.
  1572. void emitMasterRegion(CodeGenFunction &CGF,
  1573. const RegionCodeGenTy &MasterOpGen,
  1574. SourceLocation Loc) override;
  1575. /// Emits a masked region.
  1576. /// \param MaskedOpGen Generator for the statement associated with the given
  1577. /// masked region.
  1578. void emitMaskedRegion(CodeGenFunction &CGF,
  1579. const RegionCodeGenTy &MaskedOpGen, SourceLocation Loc,
  1580. const Expr *Filter = nullptr) override;
  1581. /// Emits a masked region.
  1582. /// \param MaskedOpGen Generator for the statement associated with the given
  1583. /// masked region.
  1584. /// Emits code for a taskyield directive.
  1585. void emitTaskyieldCall(CodeGenFunction &CGF, SourceLocation Loc) override;
  1586. /// Emit a taskgroup region.
  1587. /// \param TaskgroupOpGen Generator for the statement associated with the
  1588. /// given taskgroup region.
  1589. void emitTaskgroupRegion(CodeGenFunction &CGF,
  1590. const RegionCodeGenTy &TaskgroupOpGen,
  1591. SourceLocation Loc) override;
  1592. /// Emits a single region.
  1593. /// \param SingleOpGen Generator for the statement associated with the given
  1594. /// single region.
  1595. void emitSingleRegion(CodeGenFunction &CGF,
  1596. const RegionCodeGenTy &SingleOpGen, SourceLocation Loc,
  1597. ArrayRef<const Expr *> CopyprivateVars,
  1598. ArrayRef<const Expr *> DestExprs,
  1599. ArrayRef<const Expr *> SrcExprs,
  1600. ArrayRef<const Expr *> AssignmentOps) override;
  1601. /// Emit an ordered region.
  1602. /// \param OrderedOpGen Generator for the statement associated with the given
  1603. /// ordered region.
  1604. void emitOrderedRegion(CodeGenFunction &CGF,
  1605. const RegionCodeGenTy &OrderedOpGen,
  1606. SourceLocation Loc, bool IsThreads) override;
  1607. /// Emit an implicit/explicit barrier for OpenMP threads.
  1608. /// \param Kind Directive for which this implicit barrier call must be
  1609. /// generated. Must be OMPD_barrier for explicit barrier generation.
  1610. /// \param EmitChecks true if need to emit checks for cancellation barriers.
  1611. /// \param ForceSimpleCall true simple barrier call must be emitted, false if
  1612. /// runtime class decides which one to emit (simple or with cancellation
  1613. /// checks).
  1614. ///
  1615. void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
  1616. OpenMPDirectiveKind Kind, bool EmitChecks = true,
  1617. bool ForceSimpleCall = false) override;
  1618. /// This is used for non static scheduled types and when the ordered
  1619. /// clause is present on the loop construct.
  1620. /// Depending on the loop schedule, it is necessary to call some runtime
  1621. /// routine before start of the OpenMP loop to get the loop upper / lower
  1622. /// bounds \a LB and \a UB and stride \a ST.
  1623. ///
  1624. /// \param CGF Reference to current CodeGenFunction.
  1625. /// \param Loc Clang source location.
  1626. /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
  1627. /// \param IVSize Size of the iteration variable in bits.
  1628. /// \param IVSigned Sign of the iteration variable.
  1629. /// \param Ordered true if loop is ordered, false otherwise.
  1630. /// \param DispatchValues struct containing llvm values for lower bound, upper
  1631. /// bound, and chunk expression.
  1632. /// For the default (nullptr) value, the chunk 1 will be used.
  1633. ///
  1634. void emitForDispatchInit(CodeGenFunction &CGF, SourceLocation Loc,
  1635. const OpenMPScheduleTy &ScheduleKind,
  1636. unsigned IVSize, bool IVSigned, bool Ordered,
  1637. const DispatchRTInput &DispatchValues) override;
  1638. /// Call the appropriate runtime routine to initialize it before start
  1639. /// of loop.
  1640. ///
  1641. /// This is used only in case of static schedule, when the user did not
  1642. /// specify a ordered clause on the loop construct.
  1643. /// Depending on the loop schedule, it is necessary to call some runtime
  1644. /// routine before start of the OpenMP loop to get the loop upper / lower
  1645. /// bounds LB and UB and stride ST.
  1646. ///
  1647. /// \param CGF Reference to current CodeGenFunction.
  1648. /// \param Loc Clang source location.
  1649. /// \param DKind Kind of the directive.
  1650. /// \param ScheduleKind Schedule kind, specified by the 'schedule' clause.
  1651. /// \param Values Input arguments for the construct.
  1652. ///
  1653. void emitForStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
  1654. OpenMPDirectiveKind DKind,
  1655. const OpenMPScheduleTy &ScheduleKind,
  1656. const StaticRTInput &Values) override;
  1657. ///
  1658. /// \param CGF Reference to current CodeGenFunction.
  1659. /// \param Loc Clang source location.
  1660. /// \param SchedKind Schedule kind, specified by the 'dist_schedule' clause.
  1661. /// \param Values Input arguments for the construct.
  1662. ///
  1663. void emitDistributeStaticInit(CodeGenFunction &CGF, SourceLocation Loc,
  1664. OpenMPDistScheduleClauseKind SchedKind,
  1665. const StaticRTInput &Values) override;
  1666. /// Call the appropriate runtime routine to notify that we finished
  1667. /// iteration of the ordered loop with the dynamic scheduling.
  1668. ///
  1669. /// \param CGF Reference to current CodeGenFunction.
  1670. /// \param Loc Clang source location.
  1671. /// \param IVSize Size of the iteration variable in bits.
  1672. /// \param IVSigned Sign of the iteration variable.
  1673. ///
  1674. void emitForOrderedIterationEnd(CodeGenFunction &CGF, SourceLocation Loc,
  1675. unsigned IVSize, bool IVSigned) override;
  1676. /// Call the appropriate runtime routine to notify that we finished
  1677. /// all the work with current loop.
  1678. ///
  1679. /// \param CGF Reference to current CodeGenFunction.
  1680. /// \param Loc Clang source location.
  1681. /// \param DKind Kind of the directive for which the static finish is emitted.
  1682. ///
  1683. void emitForStaticFinish(CodeGenFunction &CGF, SourceLocation Loc,
  1684. OpenMPDirectiveKind DKind) override;
  1685. /// Call __kmpc_dispatch_next(
  1686. /// ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
  1687. /// kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
  1688. /// kmp_int[32|64] *p_stride);
  1689. /// \param IVSize Size of the iteration variable in bits.
  1690. /// \param IVSigned Sign of the iteration variable.
  1691. /// \param IL Address of the output variable in which the flag of the
  1692. /// last iteration is returned.
  1693. /// \param LB Address of the output variable in which the lower iteration
  1694. /// number is returned.
  1695. /// \param UB Address of the output variable in which the upper iteration
  1696. /// number is returned.
  1697. /// \param ST Address of the output variable in which the stride value is
  1698. /// returned.
  1699. llvm::Value *emitForNext(CodeGenFunction &CGF, SourceLocation Loc,
  1700. unsigned IVSize, bool IVSigned, Address IL,
  1701. Address LB, Address UB, Address ST) override;
  1702. /// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
  1703. /// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
  1704. /// clause.
  1705. /// \param NumThreads An integer value of threads.
  1706. void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
  1707. SourceLocation Loc) override;
  1708. /// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
  1709. /// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
  1710. void emitProcBindClause(CodeGenFunction &CGF,
  1711. llvm::omp::ProcBindKind ProcBind,
  1712. SourceLocation Loc) override;
  1713. /// Returns address of the threadprivate variable for the current
  1714. /// thread.
  1715. /// \param VD Threadprivate variable.
  1716. /// \param VDAddr Address of the global variable \a VD.
  1717. /// \param Loc Location of the reference to threadprivate var.
  1718. /// \return Address of the threadprivate variable for the current thread.
  1719. Address getAddrOfThreadPrivate(CodeGenFunction &CGF, const VarDecl *VD,
  1720. Address VDAddr, SourceLocation Loc) override;
  1721. /// Emit a code for initialization of threadprivate variable. It emits
  1722. /// a call to runtime library which adds initial value to the newly created
  1723. /// threadprivate variable (if it is not constant) and registers destructor
  1724. /// for the variable (if any).
  1725. /// \param VD Threadprivate variable.
  1726. /// \param VDAddr Address of the global variable \a VD.
  1727. /// \param Loc Location of threadprivate declaration.
  1728. /// \param PerformInit true if initialization expression is not constant.
  1729. llvm::Function *
  1730. emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr,
  1731. SourceLocation Loc, bool PerformInit,
  1732. CodeGenFunction *CGF = nullptr) override;
  1733. /// Creates artificial threadprivate variable with name \p Name and type \p
  1734. /// VarType.
  1735. /// \param VarType Type of the artificial threadprivate variable.
  1736. /// \param Name Name of the artificial threadprivate variable.
  1737. Address getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
  1738. QualType VarType,
  1739. StringRef Name) override;
  1740. /// Emit flush of the variables specified in 'omp flush' directive.
  1741. /// \param Vars List of variables to flush.
  1742. void emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *> Vars,
  1743. SourceLocation Loc, llvm::AtomicOrdering AO) override;
  1744. /// Emit task region for the task directive. The task region is
  1745. /// emitted in several steps:
  1746. /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
  1747. /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
  1748. /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
  1749. /// function:
  1750. /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
  1751. /// TaskFunction(gtid, tt->part_id, tt->shareds);
  1752. /// return 0;
  1753. /// }
  1754. /// 2. Copy a list of shared variables to field shareds of the resulting
  1755. /// structure kmp_task_t returned by the previous call (if any).
  1756. /// 3. Copy a pointer to destructions function to field destructions of the
  1757. /// resulting structure kmp_task_t.
  1758. /// 4. Emit a call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid,
  1759. /// kmp_task_t *new_task), where new_task is a resulting structure from
  1760. /// previous items.
  1761. /// \param D Current task directive.
  1762. /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
  1763. /// /*part_id*/, captured_struct */*__context*/);
  1764. /// \param SharedsTy A type which contains references the shared variables.
  1765. /// \param Shareds Context with the list of shared variables from the \p
  1766. /// TaskFunction.
  1767. /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
  1768. /// otherwise.
  1769. /// \param Data Additional data for task generation like tiednsee, final
  1770. /// state, list of privates etc.
  1771. void emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
  1772. const OMPExecutableDirective &D,
  1773. llvm::Function *TaskFunction, QualType SharedsTy,
  1774. Address Shareds, const Expr *IfCond,
  1775. const OMPTaskDataTy &Data) override;
  1776. /// Emit task region for the taskloop directive. The taskloop region is
  1777. /// emitted in several steps:
  1778. /// 1. Emit a call to kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32
  1779. /// gtid, kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
  1780. /// kmp_routine_entry_t *task_entry). Here task_entry is a pointer to the
  1781. /// function:
  1782. /// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
  1783. /// TaskFunction(gtid, tt->part_id, tt->shareds);
  1784. /// return 0;
  1785. /// }
  1786. /// 2. Copy a list of shared variables to field shareds of the resulting
  1787. /// structure kmp_task_t returned by the previous call (if any).
  1788. /// 3. Copy a pointer to destructions function to field destructions of the
  1789. /// resulting structure kmp_task_t.
  1790. /// 4. Emit a call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t
  1791. /// *task, int if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int
  1792. /// nogroup, int sched, kmp_uint64 grainsize, void *task_dup ), where new_task
  1793. /// is a resulting structure from
  1794. /// previous items.
  1795. /// \param D Current task directive.
  1796. /// \param TaskFunction An LLVM function with type void (*)(i32 /*gtid*/, i32
  1797. /// /*part_id*/, captured_struct */*__context*/);
  1798. /// \param SharedsTy A type which contains references the shared variables.
  1799. /// \param Shareds Context with the list of shared variables from the \p
  1800. /// TaskFunction.
  1801. /// \param IfCond Not a nullptr if 'if' clause was specified, nullptr
  1802. /// otherwise.
  1803. /// \param Data Additional data for task generation like tiednsee, final
  1804. /// state, list of privates etc.
  1805. void emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
  1806. const OMPLoopDirective &D, llvm::Function *TaskFunction,
  1807. QualType SharedsTy, Address Shareds, const Expr *IfCond,
  1808. const OMPTaskDataTy &Data) override;
  1809. /// Emit a code for reduction clause. Next code should be emitted for
  1810. /// reduction:
  1811. /// \code
  1812. ///
  1813. /// static kmp_critical_name lock = { 0 };
  1814. ///
  1815. /// void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
  1816. /// ...
  1817. /// *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
  1818. /// ...
  1819. /// }
  1820. ///
  1821. /// ...
  1822. /// void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
  1823. /// switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
  1824. /// RedList, reduce_func, &<lock>)) {
  1825. /// case 1:
  1826. /// ...
  1827. /// <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
  1828. /// ...
  1829. /// __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
  1830. /// break;
  1831. /// case 2:
  1832. /// ...
  1833. /// Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
  1834. /// ...
  1835. /// break;
  1836. /// default:;
  1837. /// }
  1838. /// \endcode
  1839. ///
  1840. /// \param Privates List of private copies for original reduction arguments.
  1841. /// \param LHSExprs List of LHS in \a ReductionOps reduction operations.
  1842. /// \param RHSExprs List of RHS in \a ReductionOps reduction operations.
  1843. /// \param ReductionOps List of reduction operations in form 'LHS binop RHS'
  1844. /// or 'operator binop(LHS, RHS)'.
  1845. /// \param Options List of options for reduction codegen:
  1846. /// WithNowait true if parent directive has also nowait clause, false
  1847. /// otherwise.
  1848. /// SimpleReduction Emit reduction operation only. Used for omp simd
  1849. /// directive on the host.
  1850. /// ReductionKind The kind of reduction to perform.
  1851. void emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
  1852. ArrayRef<const Expr *> Privates,
  1853. ArrayRef<const Expr *> LHSExprs,
  1854. ArrayRef<const Expr *> RHSExprs,
  1855. ArrayRef<const Expr *> ReductionOps,
  1856. ReductionOptionsTy Options) override;
  1857. /// Emit a code for initialization of task reduction clause. Next code
  1858. /// should be emitted for reduction:
  1859. /// \code
  1860. ///
  1861. /// _taskred_item_t red_data[n];
  1862. /// ...
  1863. /// red_data[i].shar = &shareds[i];
  1864. /// red_data[i].orig = &origs[i];
  1865. /// red_data[i].size = sizeof(origs[i]);
  1866. /// red_data[i].f_init = (void*)RedInit<i>;
  1867. /// red_data[i].f_fini = (void*)RedDest<i>;
  1868. /// red_data[i].f_comb = (void*)RedOp<i>;
  1869. /// red_data[i].flags = <Flag_i>;
  1870. /// ...
  1871. /// void* tg1 = __kmpc_taskred_init(gtid, n, red_data);
  1872. /// \endcode
  1873. /// For reduction clause with task modifier it emits the next call:
  1874. /// \code
  1875. ///
  1876. /// _taskred_item_t red_data[n];
  1877. /// ...
  1878. /// red_data[i].shar = &shareds[i];
  1879. /// red_data[i].orig = &origs[i];
  1880. /// red_data[i].size = sizeof(origs[i]);
  1881. /// red_data[i].f_init = (void*)RedInit<i>;
  1882. /// red_data[i].f_fini = (void*)RedDest<i>;
  1883. /// red_data[i].f_comb = (void*)RedOp<i>;
  1884. /// red_data[i].flags = <Flag_i>;
  1885. /// ...
  1886. /// void* tg1 = __kmpc_taskred_modifier_init(loc, gtid, is_worksharing, n,
  1887. /// red_data);
  1888. /// \endcode
  1889. /// \param LHSExprs List of LHS in \a Data.ReductionOps reduction operations.
  1890. /// \param RHSExprs List of RHS in \a Data.ReductionOps reduction operations.
  1891. /// \param Data Additional data for task generation like tiedness, final
  1892. /// state, list of privates, reductions etc.
  1893. llvm::Value *emitTaskReductionInit(CodeGenFunction &CGF, SourceLocation Loc,
  1894. ArrayRef<const Expr *> LHSExprs,
  1895. ArrayRef<const Expr *> RHSExprs,
  1896. const OMPTaskDataTy &Data) override;
  1897. /// Emits the following code for reduction clause with task modifier:
  1898. /// \code
  1899. /// __kmpc_task_reduction_modifier_fini(loc, gtid, is_worksharing);
  1900. /// \endcode
  1901. void emitTaskReductionFini(CodeGenFunction &CGF, SourceLocation Loc,
  1902. bool IsWorksharingReduction) override;
  1903. /// Required to resolve existing problems in the runtime. Emits threadprivate
  1904. /// variables to store the size of the VLAs/array sections for
  1905. /// initializer/combiner/finalizer functions + emits threadprivate variable to
  1906. /// store the pointer to the original reduction item for the custom
  1907. /// initializer defined by declare reduction construct.
  1908. /// \param RCG Allows to reuse an existing data for the reductions.
  1909. /// \param N Reduction item for which fixups must be emitted.
  1910. void emitTaskReductionFixups(CodeGenFunction &CGF, SourceLocation Loc,
  1911. ReductionCodeGen &RCG, unsigned N) override;
  1912. /// Get the address of `void *` type of the privatue copy of the reduction
  1913. /// item specified by the \p SharedLVal.
  1914. /// \param ReductionsPtr Pointer to the reduction data returned by the
  1915. /// emitTaskReductionInit function.
  1916. /// \param SharedLVal Address of the original reduction item.
  1917. Address getTaskReductionItem(CodeGenFunction &CGF, SourceLocation Loc,
  1918. llvm::Value *ReductionsPtr,
  1919. LValue SharedLVal) override;
  1920. /// Emit code for 'taskwait' directive.
  1921. void emitTaskwaitCall(CodeGenFunction &CGF, SourceLocation Loc,
  1922. const OMPTaskDataTy &Data) override;
  1923. /// Emit code for 'cancellation point' construct.
  1924. /// \param CancelRegion Region kind for which the cancellation point must be
  1925. /// emitted.
  1926. ///
  1927. void emitCancellationPointCall(CodeGenFunction &CGF, SourceLocation Loc,
  1928. OpenMPDirectiveKind CancelRegion) override;
  1929. /// Emit code for 'cancel' construct.
  1930. /// \param IfCond Condition in the associated 'if' clause, if it was
  1931. /// specified, nullptr otherwise.
  1932. /// \param CancelRegion Region kind for which the cancel must be emitted.
  1933. ///
  1934. void emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
  1935. const Expr *IfCond,
  1936. OpenMPDirectiveKind CancelRegion) override;
  1937. /// Emit outilined function for 'target' directive.
  1938. /// \param D Directive to emit.
  1939. /// \param ParentName Name of the function that encloses the target region.
  1940. /// \param OutlinedFn Outlined function value to be defined by this call.
  1941. /// \param OutlinedFnID Outlined function ID value to be defined by this call.
  1942. /// \param IsOffloadEntry True if the outlined function is an offload entry.
  1943. /// \param CodeGen Code generation sequence for the \a D directive.
  1944. /// An outlined function may not be an entry if, e.g. the if clause always
  1945. /// evaluates to false.
  1946. void emitTargetOutlinedFunction(const OMPExecutableDirective &D,
  1947. StringRef ParentName,
  1948. llvm::Function *&OutlinedFn,
  1949. llvm::Constant *&OutlinedFnID,
  1950. bool IsOffloadEntry,
  1951. const RegionCodeGenTy &CodeGen) override;
  1952. /// Emit the target offloading code associated with \a D. The emitted
  1953. /// code attempts offloading the execution to the device, an the event of
  1954. /// a failure it executes the host version outlined in \a OutlinedFn.
  1955. /// \param D Directive to emit.
  1956. /// \param OutlinedFn Host version of the code to be offloaded.
  1957. /// \param OutlinedFnID ID of host version of the code to be offloaded.
  1958. /// \param IfCond Expression evaluated in if clause associated with the target
  1959. /// directive, or null if no if clause is used.
  1960. /// \param Device Expression evaluated in device clause associated with the
  1961. /// target directive, or null if no device clause is used and device modifier.
  1962. void emitTargetCall(
  1963. CodeGenFunction &CGF, const OMPExecutableDirective &D,
  1964. llvm::Function *OutlinedFn, llvm::Value *OutlinedFnID, const Expr *IfCond,
  1965. llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
  1966. llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
  1967. const OMPLoopDirective &D)>
  1968. SizeEmitter) override;
  1969. /// Emit the target regions enclosed in \a GD function definition or
  1970. /// the function itself in case it is a valid device function. Returns true if
  1971. /// \a GD was dealt with successfully.
  1972. /// \param GD Function to scan.
  1973. bool emitTargetFunctions(GlobalDecl GD) override;
  1974. /// Emit the global variable if it is a valid device global variable.
  1975. /// Returns true if \a GD was dealt with successfully.
  1976. /// \param GD Variable declaration to emit.
  1977. bool emitTargetGlobalVariable(GlobalDecl GD) override;
  1978. /// Emit the global \a GD if it is meaningful for the target. Returns
  1979. /// if it was emitted successfully.
  1980. /// \param GD Global to scan.
  1981. bool emitTargetGlobal(GlobalDecl GD) override;
  1982. /// Emits code for teams call of the \a OutlinedFn with
  1983. /// variables captured in a record which address is stored in \a
  1984. /// CapturedStruct.
  1985. /// \param OutlinedFn Outlined function to be run by team masters. Type of
  1986. /// this function is void(*)(kmp_int32 *, kmp_int32, struct context_vars*).
  1987. /// \param CapturedVars A pointer to the record with the references to
  1988. /// variables used in \a OutlinedFn function.
  1989. ///
  1990. void emitTeamsCall(CodeGenFunction &CGF, const OMPExecutableDirective &D,
  1991. SourceLocation Loc, llvm::Function *OutlinedFn,
  1992. ArrayRef<llvm::Value *> CapturedVars) override;
  1993. /// Emits call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32
  1994. /// global_tid, kmp_int32 num_teams, kmp_int32 thread_limit) to generate code
  1995. /// for num_teams clause.
  1996. /// \param NumTeams An integer expression of teams.
  1997. /// \param ThreadLimit An integer expression of threads.
  1998. void emitNumTeamsClause(CodeGenFunction &CGF, const Expr *NumTeams,
  1999. const Expr *ThreadLimit, SourceLocation Loc) override;
  2000. /// Emit the target data mapping code associated with \a D.
  2001. /// \param D Directive to emit.
  2002. /// \param IfCond Expression evaluated in if clause associated with the
  2003. /// target directive, or null if no device clause is used.
  2004. /// \param Device Expression evaluated in device clause associated with the
  2005. /// target directive, or null if no device clause is used.
  2006. /// \param Info A record used to store information that needs to be preserved
  2007. /// until the region is closed.
  2008. void emitTargetDataCalls(CodeGenFunction &CGF,
  2009. const OMPExecutableDirective &D, const Expr *IfCond,
  2010. const Expr *Device, const RegionCodeGenTy &CodeGen,
  2011. CGOpenMPRuntime::TargetDataInfo &Info) override;
  2012. /// Emit the data mapping/movement code associated with the directive
  2013. /// \a D that should be of the form 'target [{enter|exit} data | update]'.
  2014. /// \param D Directive to emit.
  2015. /// \param IfCond Expression evaluated in if clause associated with the target
  2016. /// directive, or null if no if clause is used.
  2017. /// \param Device Expression evaluated in device clause associated with the
  2018. /// target directive, or null if no device clause is used.
  2019. void emitTargetDataStandAloneCall(CodeGenFunction &CGF,
  2020. const OMPExecutableDirective &D,
  2021. const Expr *IfCond,
  2022. const Expr *Device) override;
  2023. /// Emit initialization for doacross loop nesting support.
  2024. /// \param D Loop-based construct used in doacross nesting construct.
  2025. void emitDoacrossInit(CodeGenFunction &CGF, const OMPLoopDirective &D,
  2026. ArrayRef<Expr *> NumIterations) override;
  2027. /// Emit code for doacross ordered directive with 'depend' clause.
  2028. /// \param C 'depend' clause with 'sink|source' dependency kind.
  2029. void emitDoacrossOrdered(CodeGenFunction &CGF,
  2030. const OMPDependClause *C) override;
  2031. /// Translates the native parameter of outlined function if this is required
  2032. /// for target.
  2033. /// \param FD Field decl from captured record for the parameter.
  2034. /// \param NativeParam Parameter itself.
  2035. const VarDecl *translateParameter(const FieldDecl *FD,
  2036. const VarDecl *NativeParam) const override;
  2037. /// Gets the address of the native argument basing on the address of the
  2038. /// target-specific parameter.
  2039. /// \param NativeParam Parameter itself.
  2040. /// \param TargetParam Corresponding target-specific parameter.
  2041. Address getParameterAddress(CodeGenFunction &CGF, const VarDecl *NativeParam,
  2042. const VarDecl *TargetParam) const override;
  2043. /// Gets the OpenMP-specific address of the local variable.
  2044. Address getAddressOfLocalVariable(CodeGenFunction &CGF,
  2045. const VarDecl *VD) override {
  2046. return Address::invalid();
  2047. }
  2048. };
  2049. } // namespace CodeGen
  2050. } // namespace clang
  2051. #endif