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