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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
//===- AMDGPURegBankLegalizeRules --------------------------------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUREGBANKLEGALIZERULES_H
#define LLVM_LIB_TARGET_AMDGPU_AMDGPUREGBANKLEGALIZERULES_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include <functional>
namespace llvm {
class LLT;
class MachineRegisterInfo;
class MachineInstr;
class GCNSubtarget;
class MachineFunction;
template <typename T> class GenericUniformityInfo;
template <typename T> class GenericSSAContext;
using MachineSSAContext = GenericSSAContext<MachineFunction>;
using MachineUniformityInfo = GenericUniformityInfo<MachineSSAContext>;
namespace AMDGPU {
/// \returns true if \p Ty is a pointer type with size \p Width.
bool isAnyPtr(LLT Ty, unsigned Width);
// IDs used to build predicate for RegBankLegalizeRule. Predicate can have one
// or more IDs and each represents a check for 'uniform or divergent' + LLT or
// just LLT on register operand.
// Most often checking one operand is enough to decide which RegBankLLTMapping
// to apply (see Fast Rules), IDs are useful when two or more operands need to
// be checked.
enum UniformityLLTOpPredicateID {
_,
// scalars
S1,
S16,
S32,
S64,
S128,
UniS1,
UniS16,
UniS32,
UniS64,
UniS128,
DivS1,
DivS16,
DivS32,
DivS64,
DivS128,
// pointers
P0,
P1,
P3,
P4,
P5,
Ptr32,
Ptr64,
Ptr128,
UniP0,
UniP1,
UniP3,
UniP4,
UniP5,
UniPtr32,
UniPtr64,
UniPtr128,
DivP0,
DivP1,
DivP3,
DivP4,
DivP5,
DivPtr32,
DivPtr64,
DivPtr128,
// vectors
V2S16,
V2S32,
V3S32,
V4S32,
UniV2S16,
DivV2S16,
// B types
B32,
B64,
B96,
B128,
B256,
B512,
UniB32,
UniB64,
UniB96,
UniB128,
UniB256,
UniB512,
DivB32,
DivB64,
DivB96,
DivB128,
DivB256,
DivB512,
};
// How to apply register bank on register operand.
// In most cases, this serves as a LLT and register bank assert.
// Can change operands and insert copies, extends, truncs, and read-any-lanes.
// Anything more complicated requires LoweringMethod.
enum RegBankLLTMappingApplyID {
InvalidMapping,
None,
IntrId,
Imm,
Vcc,
// sgpr scalars, pointers, vectors and B-types
Sgpr16,
Sgpr32,
Sgpr64,
Sgpr128,
SgprP1,
SgprP3,
SgprP4,
SgprP5,
SgprPtr32,
SgprPtr64,
SgprPtr128,
SgprV2S16,
SgprV4S32,
SgprV2S32,
SgprB32,
SgprB64,
SgprB96,
SgprB128,
SgprB256,
SgprB512,
// vgpr scalars, pointers, vectors and B-types
Vgpr16,
Vgpr32,
Vgpr64,
Vgpr128,
VgprP0,
VgprP1,
VgprP3,
VgprP4,
VgprP5,
VgprPtr32,
VgprPtr64,
VgprPtr128,
VgprV2S16,
VgprV2S32,
VgprB32,
VgprB64,
VgprB96,
VgprB128,
VgprB256,
VgprB512,
VgprV4S32,
// Dst only modifiers: read-any-lane and truncs
UniInVcc,
UniInVgprS32,
UniInVgprV2S16,
UniInVgprV4S32,
UniInVgprB32,
UniInVgprB64,
UniInVgprB96,
UniInVgprB128,
UniInVgprB256,
UniInVgprB512,
Sgpr32Trunc,
// Src only modifiers: waterfalls, extends
Sgpr32AExt,
Sgpr32AExtBoolInReg,
Sgpr32SExt,
Sgpr32ZExt,
Vgpr32SExt,
Vgpr32ZExt,
};
// Instruction needs to be replaced with sequence of instructions. Lowering was
// not done by legalizer since instructions is available in either sgpr or vgpr.
// For example S64 AND is available on sgpr, for that reason S64 AND is legal in
// context of Legalizer that only checks LLT. But S64 AND is not available on
// vgpr. Lower it to two S32 vgpr ANDs.
enum LoweringMethodID {
DoNotLower,
VccExtToSel,
UniExtToSel,
UnpackBitShift,
S_BFE,
V_BFE,
VgprToVccCopy,
SplitTo32,
SplitTo32Select,
SplitTo32SExtInReg,
Ext32To64,
UniCstExt,
SplitLoad,
WidenLoad,
};
enum FastRulesTypes {
NoFastRules,
Standard, // S16, S32, S64, V2S16
StandardB, // B32, B64, B96, B128
Vector, // S32, V2S32, V3S32, V4S32
};
struct RegBankLLTMapping {
SmallVector<RegBankLLTMappingApplyID, 2> DstOpMapping;
SmallVector<RegBankLLTMappingApplyID, 4> SrcOpMapping;
LoweringMethodID LoweringMethod;
RegBankLLTMapping(
std::initializer_list<RegBankLLTMappingApplyID> DstOpMappingList,
std::initializer_list<RegBankLLTMappingApplyID> SrcOpMappingList,
LoweringMethodID LoweringMethod = DoNotLower);
};
struct PredicateMapping {
SmallVector<UniformityLLTOpPredicateID, 4> OpUniformityAndTypes;
std::function<bool(const MachineInstr &)> TestFunc;
PredicateMapping(
std::initializer_list<UniformityLLTOpPredicateID> OpList,
std::function<bool(const MachineInstr &)> TestFunc = nullptr);
bool match(const MachineInstr &MI, const MachineUniformityInfo &MUI,
const MachineRegisterInfo &MRI) const;
};
struct RegBankLegalizeRule {
PredicateMapping Predicate;
RegBankLLTMapping OperandMapping;
};
class SetOfRulesForOpcode {
// "Slow Rules". More complex 'Rules[i].Predicate', check them one by one.
SmallVector<RegBankLegalizeRule, 4> Rules;
// "Fast Rules"
// Instead of testing each 'Rules[i].Predicate' we do direct access to
// RegBankLLTMapping using getFastPredicateSlot. For example if:
// - FastTypes == Standard Uni[0] holds Mapping in case Op 0 is uniform S32
// - FastTypes == Vector Div[3] holds Mapping in case Op 0 is divergent V4S32
FastRulesTypes FastTypes = NoFastRules;
#define InvMapping RegBankLLTMapping({InvalidMapping}, {InvalidMapping})
RegBankLLTMapping Uni[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
RegBankLLTMapping Div[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
public:
SetOfRulesForOpcode();
SetOfRulesForOpcode(FastRulesTypes FastTypes);
const RegBankLLTMapping &
findMappingForMI(const MachineInstr &MI, const MachineRegisterInfo &MRI,
const MachineUniformityInfo &MUI) const;
void addRule(RegBankLegalizeRule Rule);
void addFastRuleDivergent(UniformityLLTOpPredicateID Ty,
RegBankLLTMapping RuleApplyIDs);
void addFastRuleUniform(UniformityLLTOpPredicateID Ty,
RegBankLLTMapping RuleApplyIDs);
private:
int getFastPredicateSlot(UniformityLLTOpPredicateID Ty) const;
};
// Essentially 'map<Opcode(or intrinsic_opcode), SetOfRulesForOpcode>' but a
// little more efficient.
class RegBankLegalizeRules {
const GCNSubtarget *ST;
MachineRegisterInfo *MRI;
// Separate maps for G-opcodes and instrinsics since they are in different
// enums. Multiple opcodes can share same set of rules.
// RulesAlias = map<Opcode, KeyOpcode>
// Rules = map<KeyOpcode, SetOfRulesForOpcode>
SmallDenseMap<unsigned, unsigned, 256> GRulesAlias;
SmallDenseMap<unsigned, SetOfRulesForOpcode, 128> GRules;
SmallDenseMap<unsigned, unsigned, 128> IRulesAlias;
SmallDenseMap<unsigned, SetOfRulesForOpcode, 64> IRules;
class RuleSetInitializer {
SetOfRulesForOpcode *RuleSet;
public:
// Used for clang-format line breaks and to force writing all rules for
// opcode in same place.
template <class AliasMap, class RulesMap>
RuleSetInitializer(std::initializer_list<unsigned> OpcList,
AliasMap &RulesAlias, RulesMap &Rules,
FastRulesTypes FastTypes = NoFastRules) {
unsigned KeyOpcode = *OpcList.begin();
for (unsigned Opc : OpcList) {
[[maybe_unused]] auto [_, NewInput] =
RulesAlias.try_emplace(Opc, KeyOpcode);
assert(NewInput && "Can't redefine existing Rules");
}
auto [DenseMapIter, NewInput] = Rules.try_emplace(KeyOpcode, FastTypes);
assert(NewInput && "Can't redefine existing Rules");
RuleSet = &DenseMapIter->second;
}
RuleSetInitializer(const RuleSetInitializer &) = delete;
RuleSetInitializer &operator=(const RuleSetInitializer &) = delete;
RuleSetInitializer(RuleSetInitializer &&) = delete;
RuleSetInitializer &operator=(RuleSetInitializer &&) = delete;
~RuleSetInitializer() = default;
RuleSetInitializer &Div(UniformityLLTOpPredicateID Ty,
RegBankLLTMapping RuleApplyIDs,
bool STPred = true) {
if (STPred)
RuleSet->addFastRuleDivergent(Ty, RuleApplyIDs);
return *this;
}
RuleSetInitializer &Uni(UniformityLLTOpPredicateID Ty,
RegBankLLTMapping RuleApplyIDs,
bool STPred = true) {
if (STPred)
RuleSet->addFastRuleUniform(Ty, RuleApplyIDs);
return *this;
}
RuleSetInitializer &Any(RegBankLegalizeRule Init, bool STPred = true) {
if (STPred)
RuleSet->addRule(Init);
return *this;
}
};
RuleSetInitializer addRulesForGOpcs(std::initializer_list<unsigned> OpcList,
FastRulesTypes FastTypes = NoFastRules);
RuleSetInitializer addRulesForIOpcs(std::initializer_list<unsigned> OpcList,
FastRulesTypes FastTypes = NoFastRules);
public:
// Initialize rules for all opcodes.
RegBankLegalizeRules(const GCNSubtarget &ST, MachineRegisterInfo &MRI);
// In case we don't want to regenerate same rules, we can use already
// generated rules but need to refresh references to objects that are
// created for this run.
void refreshRefs(const GCNSubtarget &_ST, MachineRegisterInfo &_MRI) {
ST = &_ST;
MRI = &_MRI;
};
const SetOfRulesForOpcode &getRulesForOpc(MachineInstr &MI) const;
};
} // end namespace AMDGPU
} // end namespace llvm
#endif
|