clang 20.0.0git
CGLoopInfo.h
Go to the documentation of this file.
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;
32class CodeGenOptions;
33namespace CodeGen {
34
35/// Attributes that may be specified on loops.
37 explicit LoopAttributes(bool IsParallel = false);
38 void clear();
39
40 /// Generate llvm.loop.parallel metadata for loads and stores.
42
43 /// State of loop vectorization or unrolling.
45
46 /// Value for llvm.loop.vectorize.enable metadata.
48
49 /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
51
52 /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
54
55 /// Value for llvm.loop.vectorize.predicate metadata
57
58 /// Value for llvm.loop.vectorize.width metadata.
60
61 // Value for llvm.loop.vectorize.scalable.enable
63
64 /// Value for llvm.loop.interleave.count metadata.
66
67 /// llvm.unroll.
68 unsigned UnrollCount;
69
70 /// llvm.unroll.
72
73 /// Value for llvm.loop.distribute.enable metadata.
75
76 /// Value for llvm.loop.pipeline.disable metadata.
78
79 /// Value for llvm.loop.pipeline.iicount metadata.
81
82 /// Value for 'llvm.loop.align' metadata.
83 unsigned CodeAlign;
84
85 /// Value for whether the loop is required to make progress.
87};
88
89/// Information used when generating a structured loop.
90class LoopInfo {
91public:
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.
98 llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
99
100 /// Get the header block of this loop.
101 llvm::BasicBlock *getHeader() const { return Header; }
102
103 /// Get the set of attributes active for this loop.
104 const LoopAttributes &getAttributes() const { return Attrs; }
105
106 /// Return this loop's access group or nullptr if it does not have one.
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.
115 const LoopInfo *getParent() const { return Parent; }
116
117private:
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.
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 llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
136
137 /// Create a LoopID without any transformations.
138 llvm::MDNode *
139 createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
140
141 /// Create a LoopID for transformations.
142 ///
143 /// The methods call each other in case multiple transformations are applied
144 /// to a loop. The transformation first to be applied will use LoopID of the
145 /// next transformation in its followup attribute.
146 ///
147 /// @param Attrs The loop's transformations.
148 /// @param LoopProperties Non-transformation properties such as debug
149 /// location, parallel accesses and disabled
150 /// transformations. These are added to the returned
151 /// LoopID.
152 /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
153 /// at least one transformation.
154 ///
155 /// @return A LoopID (metadata node) that can be used for the llvm.loop
156 /// annotation or followup-attribute.
157 /// @{
158 llvm::MDNode *
159 createPipeliningMetadata(const LoopAttributes &Attrs,
161 bool &HasUserTransforms);
162 llvm::MDNode *
163 createPartialUnrollMetadata(const LoopAttributes &Attrs,
165 bool &HasUserTransforms);
166 llvm::MDNode *
167 createUnrollAndJamMetadata(const LoopAttributes &Attrs,
169 bool &HasUserTransforms);
170 llvm::MDNode *
171 createLoopVectorizeMetadata(const LoopAttributes &Attrs,
173 bool &HasUserTransforms);
174 llvm::MDNode *
175 createLoopDistributeMetadata(const LoopAttributes &Attrs,
177 bool &HasUserTransforms);
178 llvm::MDNode *
179 createFullUnrollMetadata(const LoopAttributes &Attrs,
181 bool &HasUserTransforms);
182 /// @}
183
184 /// Create a LoopID for this loop, including transformation-unspecific
185 /// metadata such as debug location.
186 ///
187 /// @param Attrs This loop's attributes and transformations.
188 /// @param LoopProperties Additional non-transformation properties to add
189 /// to the LoopID, such as transformation-specific
190 /// metadata that are not covered by @p Attrs.
191 /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
192 /// at least one transformation.
193 ///
194 /// @return A LoopID (metadata node) that can be used for the llvm.loop
195 /// annotation.
196 llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
198 bool &HasUserTransforms);
199};
200
201/// A stack of loop information corresponding to loop nesting levels.
202/// This stack can be used to prepare attributes which are applied when a loop
203/// is emitted.
205 LoopInfoStack(const LoopInfoStack &) = delete;
206 void operator=(const LoopInfoStack &) = delete;
207
208public:
210
211 /// Begin a new structured loop. The set of staged attributes will be
212 /// applied to the loop and then cleared.
213 void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
214 const llvm::DebugLoc &EndLoc);
215
216 /// Begin a new structured loop. Stage attributes from the Attrs list.
217 /// The staged attributes are applied to the loop and then cleared.
218 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
219 const clang::CodeGenOptions &CGOpts,
220 llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
221 const llvm::DebugLoc &EndLoc, bool MustProgress = false);
222
223 /// End the current loop.
224 void pop();
225
226 /// Return the top loop id metadata.
227 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
228
229 /// Return true if the top loop is parallel.
230 bool getCurLoopParallel() const {
231 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
232 }
233
234 /// Function called by the CodeGenFunction when an instruction is
235 /// created.
236 void InsertHelper(llvm::Instruction *I) const;
237
238 /// Set the next pushed loop as parallel.
239 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
240
241 /// Set the next pushed loop 'vectorize.enable'
242 void setVectorizeEnable(bool Enable = true) {
243 StagedAttrs.VectorizeEnable =
245 }
246
247 /// Set the next pushed loop as a distribution candidate.
248 void setDistributeState(bool Enable = true) {
249 StagedAttrs.DistributeEnable =
251 }
252
253 /// Set the next pushed loop unroll state.
255 StagedAttrs.UnrollEnable = State;
256 }
257
258 /// Set the next pushed vectorize predicate state.
260 StagedAttrs.VectorizePredicateEnable = State;
261 }
262
263 /// Set the next pushed loop unroll_and_jam state.
265 StagedAttrs.UnrollAndJamEnable = State;
266 }
267
268 /// Set the vectorize width for the next loop pushed.
269 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
270
272 StagedAttrs.VectorizeScalable = State;
273 }
274
275 /// Set the interleave count for the next loop pushed.
276 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
277
278 /// Set the unroll count for the next loop pushed.
279 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
280
281 /// \brief Set the unroll count for the next loop pushed.
282 void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
283
284 /// Set the pipeline disabled state.
285 void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
286
287 /// Set the pipeline initiation interval.
289 StagedAttrs.PipelineInitiationInterval = C;
290 }
291
292 /// Set value of code align for the next loop pushed.
293 void setCodeAlign(unsigned C) { StagedAttrs.CodeAlign = C; }
294
295 /// Set no progress for the next loop pushed.
296 void setMustProgress(bool P) { StagedAttrs.MustProgress = P; }
297
298 /// Returns true if there is LoopInfo on the stack.
299 bool hasInfo() const { return !Active.empty(); }
300 /// Return the LoopInfo for the current loop. HasInfo should be called
301 /// first to ensure LoopInfo is present.
302 const LoopInfo &getInfo() const { return *Active.back(); }
303
304private:
305 /// The set of attributes that will be applied to the next pushed loop.
306 LoopAttributes StagedAttrs;
307 /// Stack of active loops.
309};
310
311} // end namespace CodeGen
312} // end namespace clang
313
314#endif
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
A stack of loop information corresponding to loop nesting levels.
Definition: CGLoopInfo.h:204
void setPipelineDisabled(bool S)
Set the pipeline disabled state.
Definition: CGLoopInfo.h:285
void setUnrollCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:279
bool hasInfo() const
Returns true if there is LoopInfo on the stack.
Definition: CGLoopInfo.h:299
void setVectorizeWidth(unsigned W)
Set the vectorize width for the next loop pushed.
Definition: CGLoopInfo.h:269
void InsertHelper(llvm::Instruction *I) const
Function called by the CodeGenFunction when an instruction is created.
Definition: CGLoopInfo.cpp:840
void setDistributeState(bool Enable=true)
Set the next pushed loop as a distribution candidate.
Definition: CGLoopInfo.h:248
void setParallel(bool Enable=true)
Set the next pushed loop as parallel.
Definition: CGLoopInfo.h:239
void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx, const clang::CodeGenOptions &CGOpts, llvm::ArrayRef< const Attr * > Attrs, const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc, bool MustProgress=false)
Begin a new structured loop.
void setInterleaveCount(unsigned C)
Set the interleave count for the next loop pushed.
Definition: CGLoopInfo.h:276
void setUnrollState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll state.
Definition: CGLoopInfo.h:254
llvm::MDNode * getCurLoopID() const
Return the top loop id metadata.
Definition: CGLoopInfo.h:227
void setVectorizeScalable(const LoopAttributes::LVEnableState &State)
Definition: CGLoopInfo.h:271
void setVectorizePredicateState(const LoopAttributes::LVEnableState &State)
Set the next pushed vectorize predicate state.
Definition: CGLoopInfo.h:259
void pop()
End the current loop.
Definition: CGLoopInfo.cpp:834
void setCodeAlign(unsigned C)
Set value of code align for the next loop pushed.
Definition: CGLoopInfo.h:293
void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc)
Begin a new structured loop.
bool getCurLoopParallel() const
Return true if the top loop is parallel.
Definition: CGLoopInfo.h:230
void setMustProgress(bool P)
Set no progress for the next loop pushed.
Definition: CGLoopInfo.h:296
void setUnrollAndJamState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll_and_jam state.
Definition: CGLoopInfo.h:264
void setUnrollAndJamCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:282
const LoopInfo & getInfo() const
Return the LoopInfo for the current loop.
Definition: CGLoopInfo.h:302
void setPipelineInitiationInterval(unsigned C)
Set the pipeline initiation interval.
Definition: CGLoopInfo.h:288
void setVectorizeEnable(bool Enable=true)
Set the next pushed loop 'vectorize.enable'.
Definition: CGLoopInfo.h:242
Information used when generating a structured loop.
Definition: CGLoopInfo.h:90
llvm::BasicBlock * getHeader() const
Get the header block of this loop.
Definition: CGLoopInfo.h:101
llvm::MDNode * getAccessGroup() const
Return this loop's access group or nullptr if it does not have one.
Definition: CGLoopInfo.h:107
llvm::MDNode * getLoopID() const
Get the loop id metadata for this loop.
Definition: CGLoopInfo.h:98
void finish()
Create the loop's metadata.
Definition: CGLoopInfo.cpp:512
const LoopInfo * getParent() const
Returns the first outer loop containing this loop if any, nullptr otherwise.
Definition: CGLoopInfo.h:115
const LoopAttributes & getAttributes() const
Get the set of attributes active for this loop.
Definition: CGLoopInfo.h:104
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
Attributes that may be specified on loops.
Definition: CGLoopInfo.h:36
unsigned UnrollCount
llvm.unroll.
Definition: CGLoopInfo.h:68
bool MustProgress
Value for whether the loop is required to make progress.
Definition: CGLoopInfo.h:86
unsigned InterleaveCount
Value for llvm.loop.interleave.count metadata.
Definition: CGLoopInfo.h:65
bool IsParallel
Generate llvm.loop.parallel metadata for loads and stores.
Definition: CGLoopInfo.h:41
LVEnableState VectorizeScalable
Definition: CGLoopInfo.h:62
LVEnableState UnrollAndJamEnable
Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:53
unsigned UnrollAndJamCount
llvm.unroll.
Definition: CGLoopInfo.h:71
LVEnableState
State of loop vectorization or unrolling.
Definition: CGLoopInfo.h:44
LVEnableState VectorizePredicateEnable
Value for llvm.loop.vectorize.predicate metadata.
Definition: CGLoopInfo.h:56
LVEnableState DistributeEnable
Value for llvm.loop.distribute.enable metadata.
Definition: CGLoopInfo.h:74
bool PipelineDisabled
Value for llvm.loop.pipeline.disable metadata.
Definition: CGLoopInfo.h:77
unsigned CodeAlign
Value for 'llvm.loop.align' metadata.
Definition: CGLoopInfo.h:83
LVEnableState UnrollEnable
Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:50
unsigned VectorizeWidth
Value for llvm.loop.vectorize.width metadata.
Definition: CGLoopInfo.h:59
unsigned PipelineInitiationInterval
Value for llvm.loop.pipeline.iicount metadata.
Definition: CGLoopInfo.h:80
LVEnableState VectorizeEnable
Value for llvm.loop.vectorize.enable metadata.
Definition: CGLoopInfo.h:47