CGLoopInfo.h 10 KB

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