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