CGOpenMPRuntime.h 117 KB

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