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 "clang/CodeGenUtils/LoopAttributes.h"
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.
38using LoopAttributes = clang::CodeGenUtils::LoopAttributes;
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,
92 llvm::ArrayRef<llvm::Metadata *> LoopProperties);
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 /// @{
111 llvm::SmallVector<llvm::Metadata *, 4>
112 createPipeliningMetadata(const LoopAttributes &Attrs,
113 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
114 bool &HasUserTransforms);
115 llvm::SmallVector<llvm::Metadata *, 4>
116 createPartialUnrollMetadata(const LoopAttributes &Attrs,
117 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
118 bool &HasUserTransforms);
119 llvm::SmallVector<llvm::Metadata *, 4>
120 createUnrollAndJamMetadata(const LoopAttributes &Attrs,
121 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
122 bool &HasUserTransforms);
123 llvm::SmallVector<llvm::Metadata *, 4>
124 createLoopVectorizeMetadata(const LoopAttributes &Attrs,
125 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
126 bool &HasUserTransforms);
127 llvm::SmallVector<llvm::Metadata *, 4>
128 createLoopDistributeMetadata(const LoopAttributes &Attrs,
129 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
130 bool &HasUserTransforms);
131 llvm::SmallVector<llvm::Metadata *, 4>
132 createFullUnrollMetadata(const LoopAttributes &Attrs,
133 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
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.
149 llvm::SmallVector<llvm::Metadata *, 4>
150 createMetadata(const LoopAttributes &Attrs,
151 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
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:
163 LoopInfoStack() {}
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 =
198 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
199 }
200
201 /// Set the next pushed loop as a distribution candidate.
202 void setDistributeState(bool Enable = true) {
203 StagedAttrs.DistributeEnable =
204 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
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.
213 void setUnrollState(const LoopAttributes::LVEnableState &State) {
214 StagedAttrs.UnrollEnable = State;
215 }
216
217 /// Set the next pushed vectorize predicate state.
218 void setVectorizePredicateState(const LoopAttributes::LVEnableState &State) {
219 StagedAttrs.VectorizePredicateEnable = State;
220 }
221
222 /// Set the next pushed loop unroll_and_jam state.
223 void setUnrollAndJamState(const LoopAttributes::LVEnableState &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
230 void setVectorizeScalable(const LoopAttributes::LVEnableState &State) {
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.
247 void setPipelineInitiationInterval(unsigned C) {
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.
267 llvm::SmallVector<std::unique_ptr<LoopInfo>, 4> Active;
268};
269
270} // end namespace CodeGen
271} // end namespace clang
272
273#endif
274