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