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.
36struct LoopAttributes {
37  explicit LoopAttributes(bool IsParallel = false);
38  void clear();
39
40  /// Generate llvm.loop.parallel metadata for loads and stores.
41  bool IsParallel;
42
43  /// State of loop vectorization or unrolling.
44  enum LVEnableState { Unspecified, Enable, Disable, Full };
45
46  /// Value for llvm.loop.vectorize.enable metadata.
47  LVEnableState VectorizeEnable;
48
49  /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
50  LVEnableState UnrollEnable;
51
52  /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
53  LVEnableState UnrollAndJamEnable;
54
55  /// Value for llvm.loop.vectorize.predicate metadata
56  LVEnableState VectorizePredicateEnable;
57
58  /// Value for llvm.loop.vectorize.width metadata.
59  unsigned VectorizeWidth;
60
61  /// Value for llvm.loop.interleave.count metadata.
62  unsigned InterleaveCount;
63
64  /// llvm.unroll.
65  unsigned UnrollCount;
66
67  /// llvm.unroll.
68  unsigned UnrollAndJamCount;
69
70  /// Value for llvm.loop.distribute.enable metadata.
71  LVEnableState DistributeEnable;
72
73  /// Value for llvm.loop.pipeline.disable metadata.
74  bool PipelineDisabled;
75
76  /// Value for llvm.loop.pipeline.iicount metadata.
77  unsigned PipelineInitiationInterval;
78};
79
80/// Information used when generating a structured loop.
81class LoopInfo {
82public:
83  /// Construct a new LoopInfo for the loop with entry Header.
84  LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
85           const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
86           LoopInfo *Parent);
87
88  /// Get the loop id metadata for this loop.
89  llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
90
91  /// Get the header block of this loop.
92  llvm::BasicBlock *getHeader() const { return Header; }
93
94  /// Get the set of attributes active for this loop.
95  const LoopAttributes &getAttributes() const { return Attrs; }
96
97  /// Return this loop's access group or nullptr if it does not have one.
98  llvm::MDNode *getAccessGroup() const { return AccGroup; }
99
100  /// Create the loop's metadata. Must be called after its nested loops have
101  /// been processed.
102  void finish();
103
104private:
105  /// Loop ID metadata.
106  llvm::TempMDTuple TempLoopID;
107  /// Header block of this loop.
108  llvm::BasicBlock *Header;
109  /// The attributes for this loop.
110  LoopAttributes Attrs;
111  /// The access group for memory accesses parallel to this loop.
112  llvm::MDNode *AccGroup = nullptr;
113  /// Start location of this loop.
114  llvm::DebugLoc StartLoc;
115  /// End location of this loop.
116  llvm::DebugLoc EndLoc;
117  /// The next outer loop, or nullptr if this is the outermost loop.
118  LoopInfo *Parent;
119  /// If this loop has unroll-and-jam metadata, this can be set by the inner
120  /// loop's LoopInfo to set the llvm.loop.unroll_and_jam.followup_inner
121  /// metadata.
122  llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
123
124  /// Create a LoopID without any transformations.
125  llvm::MDNode *
126  createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
127
128  /// Create a LoopID for transformations.
129  ///
130  /// The methods call each other in case multiple transformations are applied
131  /// to a loop. The transformation first to be applied will use LoopID of the
132  /// next transformation in its followup attribute.
133  ///
134  /// @param Attrs             The loop's transformations.
135  /// @param LoopProperties    Non-transformation properties such as debug
136  ///                          location, parallel accesses and disabled
137  ///                          transformations. These are added to the returned
138  ///                          LoopID.
139  /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
140  ///                          at least one transformation.
141  ///
142  /// @return A LoopID (metadata node) that can be used for the llvm.loop
143  ///         annotation or followup-attribute.
144  /// @{
145  llvm::MDNode *
146  createPipeliningMetadata(const LoopAttributes &Attrs,
147                           llvm::ArrayRef<llvm::Metadata *> LoopProperties,
148                           bool &HasUserTransforms);
149  llvm::MDNode *
150  createPartialUnrollMetadata(const LoopAttributes &Attrs,
151                              llvm::ArrayRef<llvm::Metadata *> LoopProperties,
152                              bool &HasUserTransforms);
153  llvm::MDNode *
154  createUnrollAndJamMetadata(const LoopAttributes &Attrs,
155                             llvm::ArrayRef<llvm::Metadata *> LoopProperties,
156                             bool &HasUserTransforms);
157  llvm::MDNode *
158  createLoopVectorizeMetadata(const LoopAttributes &Attrs,
159                              llvm::ArrayRef<llvm::Metadata *> LoopProperties,
160                              bool &HasUserTransforms);
161  llvm::MDNode *
162  createLoopDistributeMetadata(const LoopAttributes &Attrs,
163                               llvm::ArrayRef<llvm::Metadata *> LoopProperties,
164                               bool &HasUserTransforms);
165  llvm::MDNode *
166  createFullUnrollMetadata(const LoopAttributes &Attrs,
167                           llvm::ArrayRef<llvm::Metadata *> LoopProperties,
168                           bool &HasUserTransforms);
169  /// @}
170
171  /// Create a LoopID for this loop, including transformation-unspecific
172  /// metadata such as debug location.
173  ///
174  /// @param Attrs             This loop's attributes and transformations.
175  /// @param LoopProperties    Additional non-transformation properties to add
176  ///                          to the LoopID, such as transformation-specific
177  ///                          metadata that are not covered by @p Attrs.
178  /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
179  ///                          at least one transformation.
180  ///
181  /// @return A LoopID (metadata node) that can be used for the llvm.loop
182  ///         annotation.
183  llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
184                               llvm::ArrayRef<llvm::Metadata *> LoopProperties,
185                               bool &HasUserTransforms);
186};
187
188/// A stack of loop information corresponding to loop nesting levels.
189/// This stack can be used to prepare attributes which are applied when a loop
190/// is emitted.
191class LoopInfoStack {
192  LoopInfoStack(const LoopInfoStack &) = delete;
193  void operator=(const LoopInfoStack &) = delete;
194
195public:
196  LoopInfoStack() {}
197
198  /// Begin a new structured loop. The set of staged attributes will be
199  /// applied to the loop and then cleared.
200  void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
201            const llvm::DebugLoc &EndLoc);
202
203  /// Begin a new structured loop. Stage attributes from the Attrs list.
204  /// The staged attributes are applied to the loop and then cleared.
205  void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
206            const clang::CodeGenOptions &CGOpts,
207            llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
208            const llvm::DebugLoc &EndLoc);
209
210  /// End the current loop.
211  void pop();
212
213  /// Return the top loop id metadata.
214  llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
215
216  /// Return true if the top loop is parallel.
217  bool getCurLoopParallel() const {
218    return hasInfo() ? getInfo().getAttributes().IsParallel : false;
219  }
220
221  /// Function called by the CodeGenFunction when an instruction is
222  /// created.
223  void InsertHelper(llvm::Instruction *I) const;
224
225  /// Set the next pushed loop as parallel.
226  void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
227
228  /// Set the next pushed loop 'vectorize.enable'
229  void setVectorizeEnable(bool Enable = true) {
230    StagedAttrs.VectorizeEnable =
231        Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
232  }
233
234  /// Set the next pushed loop as a distribution candidate.
235  void setDistributeState(bool Enable = true) {
236    StagedAttrs.DistributeEnable =
237        Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
238  }
239
240  /// Set the next pushed loop unroll state.
241  void setUnrollState(const LoopAttributes::LVEnableState &State) {
242    StagedAttrs.UnrollEnable = State;
243  }
244
245  /// Set the next pushed vectorize predicate state.
246  void setVectorizePredicateState(const LoopAttributes::LVEnableState &State) {
247    StagedAttrs.VectorizePredicateEnable = State;
248  }
249
250  /// Set the next pushed loop unroll_and_jam state.
251  void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
252    StagedAttrs.UnrollAndJamEnable = State;
253  }
254
255  /// Set the vectorize width for the next loop pushed.
256  void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
257
258  /// Set the interleave count for the next loop pushed.
259  void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
260
261  /// Set the unroll count for the next loop pushed.
262  void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
263
264  /// \brief Set the unroll count for the next loop pushed.
265  void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
266
267  /// Set the pipeline disabled state.
268  void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
269
270  /// Set the pipeline initiation interval.
271  void setPipelineInitiationInterval(unsigned C) {
272    StagedAttrs.PipelineInitiationInterval = C;
273  }
274
275private:
276  /// Returns true if there is LoopInfo on the stack.
277  bool hasInfo() const { return !Active.empty(); }
278  /// Return the LoopInfo for the current loop. HasInfo should be called
279  /// first to ensure LoopInfo is present.
280  const LoopInfo &getInfo() const { return *Active.back(); }
281  /// The set of attributes that will be applied to the next pushed loop.
282  LoopAttributes StagedAttrs;
283  /// Stack of active loops.
284  llvm::SmallVector<std::unique_ptr<LoopInfo>, 4> Active;
285};
286
287} // end namespace CodeGen
288} // end namespace clang
289
290#endif
291