Coverage Report

Created: 2024-01-17 10:31

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