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
|
//===-- SICodeEmitter.cpp - SI Code Emitter -------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// The SI code emitter produces machine code that can be executed directly on
// the GPU device.
//
//===----------------------------------------------------------------------===//
#include "AMDGPU.h"
#include "AMDGPUCodeEmitter.h"
#include "AMDGPUUtil.h"
#include "SIInstrInfo.h"
#include "SIMachineFunctionInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Target/TargetMachine.h"
#include <map>
#include <stdio.h>
#define LITERAL_REG 255
#define VGPR_BIT(src_idx) (1ULL << (9 * src_idx - 1))
using namespace llvm;
namespace {
class SICodeEmitter : public MachineFunctionPass, public AMDGPUCodeEmitter {
private:
static char ID;
formatted_raw_ostream &_OS;
const TargetMachine *TM;
//Program Info
unsigned MaxSGPR;
unsigned MaxVGPR;
unsigned CurrentInstrIndex;
std::map<int, unsigned> BBIndexes;
void InitProgramInfo(MachineFunction &MF);
void EmitState(MachineFunction & MF);
void emitInstr(MachineInstr &MI);
void outputBytes(uint64_t value, unsigned bytes);
unsigned GPRAlign(const MachineInstr &MI, unsigned OpNo, unsigned shift)
const;
public:
SICodeEmitter(formatted_raw_ostream &OS) : MachineFunctionPass(ID),
_OS(OS), TM(NULL), MaxSGPR(0), MaxVGPR(0), CurrentInstrIndex(0) { }
const char *getPassName() const { return "SI Code Emitter"; }
bool runOnMachineFunction(MachineFunction &MF);
/// getMachineOpValue - Return the encoding for MO
virtual uint64_t getMachineOpValue(const MachineInstr &MI,
const MachineOperand &MO) const;
/// GPR4AlignEncode - Encoding for when 4 consectuive registers are used
virtual unsigned GPR4AlignEncode(const MachineInstr &MI, unsigned OpNo)
const;
/// GPR2AlignEncode - Encoding for when 2 consecutive registers are used
virtual unsigned GPR2AlignEncode(const MachineInstr &MI, unsigned OpNo)
const;
/// i32LiteralEncode - Encode an i32 literal this is used as an operand
/// for an instruction in place of a register.
virtual uint64_t i32LiteralEncode(const MachineInstr &MI, unsigned OpNo)
const;
/// SMRDmemriEncode - Encoding for SMRD indexed loads
virtual uint32_t SMRDmemriEncode(const MachineInstr &MI, unsigned OpNo)
const;
/// VOPPostEncode - Post-Encoder method for VOP instructions
virtual uint64_t VOPPostEncode(const MachineInstr &MI,
uint64_t Value) const;
};
}
char SICodeEmitter::ID = 0;
FunctionPass *llvm::createSICodeEmitterPass(formatted_raw_ostream &OS) {
return new SICodeEmitter(OS);
}
void SICodeEmitter::EmitState(MachineFunction & MF) {
SIMachineFunctionInfo * MFI = MF.getInfo<SIMachineFunctionInfo>();
outputBytes(MaxSGPR + 1, 4);
outputBytes(MaxVGPR + 1, 4);
outputBytes(MFI->spi_ps_input_addr, 4);
}
void SICodeEmitter::InitProgramInfo(MachineFunction &MF) {
unsigned InstrIndex = 0;
bool VCCUsed = false;
const SIRegisterInfo * RI =
static_cast<const SIRegisterInfo*>(TM->getRegisterInfo());
for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
BB != BB_E; ++BB) {
MachineBasicBlock &MBB = *BB;
BBIndexes[MBB.getNumber()] = InstrIndex;
InstrIndex += MBB.size();
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
I != E; ++I) {
MachineInstr &MI = *I;
unsigned numOperands = MI.getNumOperands();
for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
MachineOperand & MO = MI.getOperand(op_idx);
unsigned maxUsed;
unsigned width = 0;
bool isSGPR = false;
unsigned reg;
unsigned hwReg;
if (!MO.isReg()) {
continue;
}
reg = MO.getReg();
if (reg == AMDGPU::VCC) {
VCCUsed = true;
continue;
}
if (AMDGPU::SReg_32RegClass.contains(reg)) {
isSGPR = true;
width = 1;
} else if (AMDGPU::VReg_32RegClass.contains(reg)) {
isSGPR = false;
width = 1;
} else if (AMDGPU::SReg_64RegClass.contains(reg)) {
isSGPR = true;
width = 2;
} else if (AMDGPU::VReg_64RegClass.contains(reg)) {
isSGPR = false;
width = 2;
} else if (AMDGPU::SReg_128RegClass.contains(reg)) {
isSGPR = true;
width = 4;
} else if (AMDGPU::VReg_128RegClass.contains(reg)) {
isSGPR = false;
width = 4;
} else if (AMDGPU::SReg_256RegClass.contains(reg)) {
isSGPR = true;
width = 8;
} else {
assert("!Unknown register class");
}
hwReg = RI->getHWRegNum(reg);
maxUsed = hwReg + width - 1;
if (isSGPR) {
MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
} else {
MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
}
}
}
}
if (VCCUsed) {
MaxSGPR += 2;
}
}
bool SICodeEmitter::runOnMachineFunction(MachineFunction &MF)
{
TM = &MF.getTarget();
const AMDGPUSubtarget &STM = TM->getSubtarget<AMDGPUSubtarget>();
if (STM.dumpCode()) {
MF.dump();
}
InitProgramInfo(MF);
EmitState(MF);
for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
BB != BB_E; ++BB) {
MachineBasicBlock &MBB = *BB;
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
I != E; ++I) {
MachineInstr &MI = *I;
if (MI.getOpcode() != AMDGPU::KILL && MI.getOpcode() != AMDGPU::RETURN) {
emitInstr(MI);
CurrentInstrIndex++;
}
}
}
// Emit S_END_PGM
MachineInstr * End = BuildMI(MF, DebugLoc(),
TM->getInstrInfo()->get(AMDGPU::S_ENDPGM));
emitInstr(*End);
return false;
}
void SICodeEmitter::emitInstr(MachineInstr &MI)
{
const SIInstrInfo * SII = static_cast<const SIInstrInfo*>(TM->getInstrInfo());
uint64_t hwInst = getBinaryCodeForInstr(MI);
if ((hwInst & 0xffffffff) == 0xffffffff) {
fprintf(stderr, "Unsupported Instruction: \n");
MI.dump();
abort();
}
unsigned bytes = SII->getEncodingBytes(MI);
outputBytes(hwInst, bytes);
}
uint64_t SICodeEmitter::getMachineOpValue(const MachineInstr &MI,
const MachineOperand &MO) const
{
const SIRegisterInfo * RI =
static_cast<const SIRegisterInfo*>(TM->getRegisterInfo());
switch(MO.getType()) {
case MachineOperand::MO_Register:
return RI->getBinaryCode(MO.getReg());
case MachineOperand::MO_Immediate:
return MO.getImm();
case MachineOperand::MO_FPImmediate:
// XXX: Not all instructions can use inline literals
// XXX: We should make sure this is a 32-bit constant
return LITERAL_REG;
case MachineOperand::MO_MachineBasicBlock:
return (*BBIndexes.find(MI.getParent()->getNumber())).second -
CurrentInstrIndex - 1;
default:
llvm_unreachable("Encoding of this operand type is not supported yet.");
break;
}
}
unsigned SICodeEmitter::GPRAlign(const MachineInstr &MI, unsigned OpNo,
unsigned shift) const
{
const SIRegisterInfo * RI =
static_cast<const SIRegisterInfo*>(TM->getRegisterInfo());
unsigned regCode = RI->getHWRegNum(MI.getOperand(OpNo).getReg());
return regCode >> shift;
}
unsigned SICodeEmitter::GPR4AlignEncode(const MachineInstr &MI,
unsigned OpNo) const
{
return GPRAlign(MI, OpNo, 2);
}
unsigned SICodeEmitter::GPR2AlignEncode(const MachineInstr &MI,
unsigned OpNo) const
{
return GPRAlign(MI, OpNo, 1);
}
uint64_t SICodeEmitter::i32LiteralEncode(const MachineInstr &MI,
unsigned OpNo) const
{
return LITERAL_REG | (MI.getOperand(OpNo).getImm() << 32);
}
#define SMRD_OFFSET_MASK 0xff
#define SMRD_IMM_SHIFT 8
#define SMRD_SBASE_MASK 0x3f
#define SMRD_SBASE_SHIFT 9
/// SMRDmemriEncode - This function is responsibe for encoding the offset
/// and the base ptr for SMRD instructions it should return a bit string in
/// this format:
///
/// OFFSET = bits{7-0}
/// IMM = bits{8}
/// SBASE = bits{14-9}
///
uint32_t SICodeEmitter::SMRDmemriEncode(const MachineInstr &MI,
unsigned OpNo) const
{
uint32_t encoding;
const MachineOperand &OffsetOp = MI.getOperand(OpNo + 1);
//XXX: Use this function for SMRD loads with register offsets
assert(OffsetOp.isImm());
encoding =
(getMachineOpValue(MI, OffsetOp) & SMRD_OFFSET_MASK)
| (1 << SMRD_IMM_SHIFT) //XXX If the Offset is a register we shouldn't set this bit
| ((GPR2AlignEncode(MI, OpNo) & SMRD_SBASE_MASK) << SMRD_SBASE_SHIFT)
;
return encoding;
}
/// Set the "VGPR" bit for VOP args that can take either a VGPR or a SGPR.
/// XXX: It would be nice if we could handle this without a PostEncode function.
uint64_t SICodeEmitter::VOPPostEncode(const MachineInstr &MI,
uint64_t Value) const
{
const SIInstrInfo * SII = static_cast<const SIInstrInfo*>(TM->getInstrInfo());
unsigned encodingType = SII->getEncodingType(MI);
unsigned numSrcOps;
unsigned vgprBitOffset;
if (encodingType == SIInstrEncodingType::VOP3) {
numSrcOps = 3;
vgprBitOffset = 32;
} else {
numSrcOps = 1;
vgprBitOffset = 0;
}
// Add one to skip over the destination reg operand.
for (unsigned opIdx = 1; opIdx < numSrcOps + 1; opIdx++) {
const MachineOperand &MO = MI.getOperand(opIdx);
switch(MO.getType()) {
case MachineOperand::MO_Register:
{
unsigned reg = MI.getOperand(opIdx).getReg();
if (AMDGPU::VReg_32RegClass.contains(reg)
|| AMDGPU::VReg_64RegClass.contains(reg)) {
Value |= (VGPR_BIT(opIdx)) << vgprBitOffset;
}
}
break;
case MachineOperand::MO_FPImmediate:
// XXX: Not all instructions can use inline literals
// XXX: We should make sure this is a 32-bit constant
Value |= (MO.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue() << 32);
continue;
default:
break;
}
}
return Value;
}
void SICodeEmitter::outputBytes(uint64_t value, unsigned bytes)
{
for (unsigned i = 0; i < bytes; i++) {
_OS.write((uint8_t) ((value >> (8 * i)) & 0xff));
}
}
|