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