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