CGLoopInfo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- 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 is the internal state used for llvm translation for loop statement
  10. // metadata.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
  14. #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/IR/DebugLoc.h"
  18. #include "llvm/IR/Value.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace llvm {
  21. class BasicBlock;
  22. class Instruction;
  23. class MDNode;
  24. } // end namespace llvm
  25. namespace clang {
  26. class Attr;
  27. class ASTContext;
  28. class CodeGenOptions;
  29. namespace CodeGen {
  30. /// Attributes that may be specified on loops.
  31. struct LoopAttributes {
  32. explicit LoopAttributes(bool IsParallel = false);
  33. void clear();
  34. /// Generate llvm.loop.parallel metadata for loads and stores.
  35. bool IsParallel;
  36. /// State of loop vectorization or unrolling.
  37. enum LVEnableState { Unspecified, Enable, Disable, Full };
  38. /// Value for llvm.loop.vectorize.enable metadata.
  39. LVEnableState VectorizeEnable;
  40. /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
  41. LVEnableState UnrollEnable;
  42. /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
  43. LVEnableState UnrollAndJamEnable;
  44. /// Value for llvm.loop.vectorize.predicate metadata
  45. LVEnableState VectorizePredicateEnable;
  46. /// Value for llvm.loop.vectorize.width metadata.
  47. unsigned VectorizeWidth;
  48. // Value for llvm.loop.vectorize.scalable.enable
  49. LVEnableState VectorizeScalable;
  50. /// Value for llvm.loop.interleave.count metadata.
  51. unsigned InterleaveCount;
  52. /// llvm.unroll.
  53. unsigned UnrollCount;
  54. /// llvm.unroll.
  55. unsigned UnrollAndJamCount;
  56. /// Value for llvm.loop.distribute.enable metadata.
  57. LVEnableState DistributeEnable;
  58. /// Value for llvm.loop.pipeline.disable metadata.
  59. bool PipelineDisabled;
  60. /// Value for llvm.loop.pipeline.iicount metadata.
  61. unsigned PipelineInitiationInterval;
  62. /// Value for whether the loop is required to make progress.
  63. bool MustProgress;
  64. };
  65. /// Information used when generating a structured loop.
  66. class LoopInfo {
  67. public:
  68. /// Construct a new LoopInfo for the loop with entry Header.
  69. LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
  70. const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
  71. LoopInfo *Parent);
  72. /// Get the loop id metadata for this loop.
  73. llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
  74. /// Get the header block of this loop.
  75. llvm::BasicBlock *getHeader() const { return Header; }
  76. /// Get the set of attributes active for this loop.
  77. const LoopAttributes &getAttributes() const { return Attrs; }
  78. /// Return this loop's access group or nullptr if it does not have one.
  79. llvm::MDNode *getAccessGroup() const { return AccGroup; }
  80. /// Create the loop's metadata. Must be called after its nested loops have
  81. /// been processed.
  82. void finish();
  83. private:
  84. /// Loop ID metadata.
  85. llvm::TempMDTuple TempLoopID;
  86. /// Header block of this loop.
  87. llvm::BasicBlock *Header;
  88. /// The attributes for this loop.
  89. LoopAttributes Attrs;
  90. /// The access group for memory accesses parallel to this loop.
  91. llvm::MDNode *AccGroup = nullptr;
  92. /// Start location of this loop.
  93. llvm::DebugLoc StartLoc;
  94. /// End location of this loop.
  95. llvm::DebugLoc EndLoc;
  96. /// The next outer loop, or nullptr if this is the outermost loop.
  97. LoopInfo *Parent;
  98. /// If this loop has unroll-and-jam metadata, this can be set by the inner
  99. /// loop's LoopInfo to set the llvm.loop.unroll_and_jam.followup_inner
  100. /// metadata.
  101. llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
  102. /// Create a LoopID without any transformations.
  103. llvm::MDNode *
  104. createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
  105. /// Create a LoopID for transformations.
  106. ///
  107. /// The methods call each other in case multiple transformations are applied
  108. /// to a loop. The transformation first to be applied will use LoopID of the
  109. /// next transformation in its followup attribute.
  110. ///
  111. /// @param Attrs The loop's transformations.
  112. /// @param LoopProperties Non-transformation properties such as debug
  113. /// location, parallel accesses and disabled
  114. /// transformations. These are added to the returned
  115. /// LoopID.
  116. /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
  117. /// at least one transformation.
  118. ///
  119. /// @return A LoopID (metadata node) that can be used for the llvm.loop
  120. /// annotation or followup-attribute.
  121. /// @{
  122. llvm::MDNode *
  123. createPipeliningMetadata(const LoopAttributes &Attrs,
  124. llvm::ArrayRef<llvm::Metadata *> LoopProperties,
  125. bool &HasUserTransforms);
  126. llvm::MDNode *
  127. createPartialUnrollMetadata(const LoopAttributes &Attrs,
  128. llvm::ArrayRef<llvm::Metadata *> LoopProperties,
  129. bool &HasUserTransforms);
  130. llvm::MDNode *
  131. createUnrollAndJamMetadata(const LoopAttributes &Attrs,
  132. llvm::ArrayRef<llvm::Metadata *> LoopProperties,
  133. bool &HasUserTransforms);
  134. llvm::MDNode *
  135. createLoopVectorizeMetadata(const LoopAttributes &Attrs,
  136. llvm::ArrayRef<llvm::Metadata *> LoopProperties,
  137. bool &HasUserTransforms);
  138. llvm::MDNode *
  139. createLoopDistributeMetadata(const LoopAttributes &Attrs,
  140. llvm::ArrayRef<llvm::Metadata *> LoopProperties,
  141. bool &HasUserTransforms);
  142. llvm::MDNode *
  143. createFullUnrollMetadata(const LoopAttributes &Attrs,
  144. llvm::ArrayRef<llvm::Metadata *> LoopProperties,
  145. bool &HasUserTransforms);
  146. /// @}
  147. /// Create a LoopID for this loop, including transformation-unspecific
  148. /// metadata such as debug location.
  149. ///
  150. /// @param Attrs This loop's attributes and transformations.
  151. /// @param LoopProperties Additional non-transformation properties to add
  152. /// to the LoopID, such as transformation-specific
  153. /// metadata that are not covered by @p Attrs.
  154. /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
  155. /// at least one transformation.
  156. ///
  157. /// @return A LoopID (metadata node) that can be used for the llvm.loop
  158. /// annotation.
  159. llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
  160. llvm::ArrayRef<llvm::Metadata *> LoopProperties,
  161. bool &HasUserTransforms);
  162. };
  163. /// A stack of loop information corresponding to loop nesting levels.
  164. /// This stack can be used to prepare attributes which are applied when a loop
  165. /// is emitted.
  166. class LoopInfoStack {
  167. LoopInfoStack(const LoopInfoStack &) = delete;
  168. void operator=(const LoopInfoStack &) = delete;
  169. public:
  170. LoopInfoStack() {}
  171. /// Begin a new structured loop. The set of staged attributes will be
  172. /// applied to the loop and then cleared.
  173. void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
  174. const llvm::DebugLoc &EndLoc);
  175. /// Begin a new structured loop. Stage attributes from the Attrs list.
  176. /// The staged attributes are applied to the loop and then cleared.
  177. void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
  178. const clang::CodeGenOptions &CGOpts,
  179. llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
  180. const llvm::DebugLoc &EndLoc, bool MustProgress = false);
  181. /// End the current loop.
  182. void pop();
  183. /// Return the top loop id metadata.
  184. llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
  185. /// Return true if the top loop is parallel.
  186. bool getCurLoopParallel() const {
  187. return hasInfo() ? getInfo().getAttributes().IsParallel : false;
  188. }
  189. /// Function called by the CodeGenFunction when an instruction is
  190. /// created.
  191. void InsertHelper(llvm::Instruction *I) const;
  192. /// Set the next pushed loop as parallel.
  193. void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
  194. /// Set the next pushed loop 'vectorize.enable'
  195. void setVectorizeEnable(bool Enable = true) {
  196. StagedAttrs.VectorizeEnable =
  197. Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
  198. }
  199. /// Set the next pushed loop as a distribution candidate.
  200. void setDistributeState(bool Enable = true) {
  201. StagedAttrs.DistributeEnable =
  202. Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
  203. }
  204. /// Set the next pushed loop unroll state.
  205. void setUnrollState(const LoopAttributes::LVEnableState &State) {
  206. StagedAttrs.UnrollEnable = State;
  207. }
  208. /// Set the next pushed vectorize predicate state.
  209. void setVectorizePredicateState(const LoopAttributes::LVEnableState &State) {
  210. StagedAttrs.VectorizePredicateEnable = State;
  211. }
  212. /// Set the next pushed loop unroll_and_jam state.
  213. void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
  214. StagedAttrs.UnrollAndJamEnable = State;
  215. }
  216. /// Set the vectorize width for the next loop pushed.
  217. void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
  218. void setVectorizeScalable(const LoopAttributes::LVEnableState &State) {
  219. StagedAttrs.VectorizeScalable = State;
  220. }
  221. /// Set the interleave count for the next loop pushed.
  222. void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
  223. /// Set the unroll count for the next loop pushed.
  224. void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
  225. /// \brief Set the unroll count for the next loop pushed.
  226. void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
  227. /// Set the pipeline disabled state.
  228. void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
  229. /// Set the pipeline initiation interval.
  230. void setPipelineInitiationInterval(unsigned C) {
  231. StagedAttrs.PipelineInitiationInterval = C;
  232. }
  233. /// Set no progress for the next loop pushed.
  234. void setMustProgress(bool P) { StagedAttrs.MustProgress = P; }
  235. private:
  236. /// Returns true if there is LoopInfo on the stack.
  237. bool hasInfo() const { return !Active.empty(); }
  238. /// Return the LoopInfo for the current loop. HasInfo should be called
  239. /// first to ensure LoopInfo is present.
  240. const LoopInfo &getInfo() const { return *Active.back(); }
  241. /// The set of attributes that will be applied to the next pushed loop.
  242. LoopAttributes StagedAttrs;
  243. /// Stack of active loops.
  244. llvm::SmallVector<std::unique_ptr<LoopInfo>, 4> Active;
  245. };
  246. } // end namespace CodeGen
  247. } // end namespace clang
  248. #endif