CGOpenMPRuntime.h 104 KB

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