1 //===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This is the internal state used for llvm translation for loop statement
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
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"
27 } // end namespace llvm
34 /// Attributes that may be specified on loops.
35 struct LoopAttributes {
36 explicit LoopAttributes(bool IsParallel = false);
39 /// Generate llvm.loop.parallel metadata for loads and stores.
42 /// State of loop vectorization or unrolling.
43 enum LVEnableState { Unspecified, Enable, Disable, Full };
45 /// Value for llvm.loop.vectorize.enable metadata.
46 LVEnableState VectorizeEnable;
48 /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
49 LVEnableState UnrollEnable;
51 /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
52 LVEnableState UnrollAndJamEnable;
54 /// Value for llvm.loop.vectorize.width metadata.
55 unsigned VectorizeWidth;
57 /// Value for llvm.loop.interleave.count metadata.
58 unsigned InterleaveCount;
64 unsigned UnrollAndJamCount;
66 /// Value for llvm.loop.distribute.enable metadata.
67 LVEnableState DistributeEnable;
69 /// Value for llvm.loop.pipeline.disable metadata.
70 bool PipelineDisabled;
72 /// Value for llvm.loop.pipeline.iicount metadata.
73 unsigned PipelineInitiationInterval;
76 /// Information used when generating a structured loop.
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,
84 /// Get the loop id metadata for this loop.
85 llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
87 /// Get the header block of this loop.
88 llvm::BasicBlock *getHeader() const { return Header; }
90 /// Get the set of attributes active for this loop.
91 const LoopAttributes &getAttributes() const { return Attrs; }
93 /// Return this loop's access group or nullptr if it does not have one.
94 llvm::MDNode *getAccessGroup() const { return AccGroup; }
96 /// Create the loop's metadata. Must be called after its nested loops have
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.
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
118 llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
120 /// Create a LoopID without any transformations.
122 createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
124 /// Create a LoopID for transformations.
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.
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
135 /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
136 /// at least one transformation.
138 /// @return A LoopID (metadata node) that can be used for the llvm.loop
139 /// annotation or followup-attribute.
142 createPipeliningMetadata(const LoopAttributes &Attrs,
143 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
144 bool &HasUserTransforms);
146 createPartialUnrollMetadata(const LoopAttributes &Attrs,
147 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
148 bool &HasUserTransforms);
150 createUnrollAndJamMetadata(const LoopAttributes &Attrs,
151 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
152 bool &HasUserTransforms);
154 createLoopVectorizeMetadata(const LoopAttributes &Attrs,
155 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
156 bool &HasUserTransforms);
158 createLoopDistributeMetadata(const LoopAttributes &Attrs,
159 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
160 bool &HasUserTransforms);
162 createFullUnrollMetadata(const LoopAttributes &Attrs,
163 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
164 bool &HasUserTransforms);
167 /// Create a LoopID for this loop, including transformation-unspecific
168 /// metadata such as debug location.
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.
177 /// @return A LoopID (metadata node) that can be used for the llvm.loop
179 llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
180 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
181 bool &HasUserTransforms);
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
187 class LoopInfoStack {
188 LoopInfoStack(const LoopInfoStack &) = delete;
189 void operator=(const LoopInfoStack &) = delete;
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);
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);
205 /// End the current loop.
208 /// Return the top loop id metadata.
209 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
211 /// Return true if the top loop is parallel.
212 bool getCurLoopParallel() const {
213 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
216 /// Function called by the CodeGenFunction when an instruction is
218 void InsertHelper(llvm::Instruction *I) const;
220 /// Set the next pushed loop as parallel.
221 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
223 /// Set the next pushed loop 'vectorize.enable'
224 void setVectorizeEnable(bool Enable = true) {
225 StagedAttrs.VectorizeEnable =
226 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
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;
235 /// Set the next pushed loop unroll state.
236 void setUnrollState(const LoopAttributes::LVEnableState &State) {
237 StagedAttrs.UnrollEnable = State;
240 /// Set the next pushed loop unroll_and_jam state.
241 void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
242 StagedAttrs.UnrollAndJamEnable = State;
245 /// Set the vectorize width for the next loop pushed.
246 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
248 /// Set the interleave count for the next loop pushed.
249 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
251 /// Set the unroll count for the next loop pushed.
252 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
254 /// \brief Set the unroll count for the next loop pushed.
255 void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
257 /// Set the pipeline disabled state.
258 void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
260 /// Set the pipeline initiation interval.
261 void setPipelineInitiationInterval(unsigned C) {
262 StagedAttrs.PipelineInitiationInterval = C;
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;
277 } // end namespace CodeGen
278 } // end namespace clang