-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryParser.cpp
More file actions
649 lines (564 loc) · 19.9 KB
/
BinaryParser.cpp
File metadata and controls
649 lines (564 loc) · 19.9 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//===----------- BinaryParser.cpp - Parse DXSA binary to MLIR -------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "mlir/Target/DXSA/BinaryParser.h"
#include "mlir/Dialect/DXSA/IR/DXSA.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Location.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/LogicalResult.h"
#include <optional>
#include "d3d12TokenizedProgramFormat.hpp"
#define DEBUG_TYPE "import-dxsa-bin"
using namespace mlir;
using namespace llvm;
enum OpcodeClass {
D3D10_SB_FLOAT_OP,
D3D10_SB_INT_OP,
D3D10_SB_UINT_OP,
D3D10_SB_BIT_OP,
D3D10_SB_FLOW_OP,
D3D10_SB_TEX_OP,
D3D10_SB_DCL_OP,
D3D11_SB_ATOMIC_OP,
D3D11_SB_MEM_OP,
D3D11_SB_DOUBLE_OP,
D3D11_SB_FLOAT_TO_DOUBLE_OP,
D3D11_SB_DOUBLE_TO_FLOAT_OP,
D3D11_SB_DEBUG_OP,
};
struct InstructionInfo {
unsigned numOperands;
StringRef name;
OpcodeClass opClass;
uint32_t precisionFromOutMask;
};
static void initInstructionInfo(MutableArrayRef<InstructionInfo> instructions) {
#define SET(OpCode, Name, NumOperands, PrecMask, OpClass) \
instructions[OpCode] = InstructionInfo{NumOperands, Name, OpClass, PrecMask};
#include "InstrInfo.def"
#undef SET
}
struct InstructionModifier {
uint32_t preciseMask{0};
uint32_t saturate{0};
};
struct OperandModifier {
uint32_t modifier{0};
uint32_t minPrecision{0};
uint32_t nonUniform{0};
};
enum class OperandComponentsKind {
None,
Mask,
Swizzle,
One,
};
struct OperandComponents {
unsigned num;
OperandComponentsKind kind;
union {
uint32_t mask;
uint32_t swizzle[4];
uint32_t one;
};
};
class DXBuilder {
public:
DXBuilder(MLIRContext *context, StringAttr name)
: context(context),
module(ModuleOp::create(builder, FileLineColLoc::get(name, 0, 0))),
builder(module.getRegion()) {}
using Index = mlir::Value;
using Operand = mlir::Value;
using Instruction = mlir::Operation *;
using Module = mlir::ModuleOp;
Index buildIndexImm32(int32_t imm, FileLineColLoc loc) {
Operation *op =
dxsa::IndexImm::create(builder, loc, builder.getType<dxsa::IndexType>(),
builder.getI32IntegerAttr(imm));
return op->getResults()[0];
}
Index buildIndexImm64(int64_t imm, FileLineColLoc loc) {
Operation *op =
dxsa::IndexImm::create(builder, loc, builder.getType<dxsa::IndexType>(),
builder.getI64IntegerAttr(imm));
return op->getResults()[0];
}
Index buildIndexRelative(Operand operand, FileLineColLoc loc) {
Operation *op = dxsa::IndexRel::create(
builder, loc, builder.getType<dxsa::IndexType>(), operand);
return op->getResults()[0];
}
Index buildIndexImm32PlusRelative(int32_t imm, Operand operand,
FileLineColLoc loc) {
Operation *op = dxsa::IndexRelImm::create(
builder, loc, builder.getType<dxsa::IndexType>(), operand,
builder.getStringAttr("add"), builder.getI32IntegerAttr(imm));
return op->getResults()[0];
}
Operand buildOperandImm32(ArrayRef<int32_t> values, FileLineColLoc loc) {
Operation *op = dxsa::OperandImm::create(
builder, loc, builder.getType<dxsa::OperandType>(),
builder.getI32VectorAttr(values));
return op->getResults()[0];
}
Operand buildOperandImm64(ArrayRef<int64_t> values, FileLineColLoc loc) {
Operation *op = dxsa::OperandImm::create(
builder, loc, builder.getType<dxsa::OperandType>(),
builder.getI64VectorAttr(values));
return op->getResults()[0];
}
Operand buildOperand(uint32_t opType, const OperandComponents &components,
ArrayRef<Index> indices,
const std::optional<OperandModifier> &modifier,
FileLineColLoc loc) {
NamedAttrList attrs;
attrs.append("type", builder.getI32IntegerAttr(opType));
if (modifier) {
const OperandModifier &mod = modifier.value();
if (uint32_t modifier = mod.modifier) {
attrs.append("modifier", builder.getI32IntegerAttr(modifier));
}
if (uint32_t minPrecision = mod.minPrecision) {
attrs.append("min_precision", builder.getI32IntegerAttr(minPrecision));
}
if (uint32_t nonUniform = mod.nonUniform) {
attrs.append("non_uniform", builder.getI32IntegerAttr(nonUniform));
}
}
attrs.append("num_components", builder.getI32IntegerAttr(components.num));
switch (components.kind) {
case OperandComponentsKind::Mask: {
attrs.append("mask", builder.getI32IntegerAttr(components.mask));
break;
}
case OperandComponentsKind::Swizzle: {
SmallVector<int32_t, 4> values;
for (uint32_t i = 0; i < components.num; ++i) {
values.push_back(components.swizzle[i]);
}
attrs.append("swizzle", builder.getI32VectorAttr(values));
break;
}
case OperandComponentsKind::One: {
attrs.append("one", builder.getI32IntegerAttr(components.one));
break;
}
case OperandComponentsKind::None:
break;
}
Operation *op = dxsa::Operand::create(
builder, loc, builder.getType<dxsa::OperandType>(), indices, attrs);
return op->getResults()[0];
}
Instruction buildInstruction(StringRef name, ArrayRef<Operand> operands,
const InstructionModifier &modifier,
FileLineColLoc loc) {
return dxsa::Instruction::create(builder, loc, operands,
builder.getStringAttr(name));
}
Module buildModule(ArrayRef<Instruction> instructions, FileLineColLoc loc) {
return module;
}
Instruction buildDclGlobalFlags(uint32_t flags, Location loc) {
auto flagsAttr = dxsa::GlobalFlagsAttr::get(
builder.getContext(), static_cast<dxsa::GlobalFlags>(flags));
return dxsa::DclGlobalFlags::create(builder, loc, flagsAttr);
}
Instruction buildDclTemps(uint32_t count, Location loc) {
return dxsa::DclTemps::create(builder, loc,
builder.getI32IntegerAttr(count));
}
private:
MLIRContext *context;
ModuleOp module;
OpBuilder builder;
};
class Parser {
public:
Parser(DXBuilder &builder, StringAttr name, StringRef buffer)
: builder(builder), name(name), buffer(buffer) {
initInstructionInfo(instrInfo);
}
using Token = FailureOr<uint32_t>;
using Index = DXBuilder::Index;
using Operand = DXBuilder::Operand;
using Instruction = DXBuilder::Instruction;
using Module = DXBuilder::Module;
/// Parse the current token and move the cursor to the next one.
Token parseToken() {
constexpr size_t tokenSize = sizeof(uint32_t);
if ((currentTokenOffset + tokenSize) > buffer.size()) {
emitError(getLocation(), "unexpected end of file");
return failure();
}
uint32_t value = support::endian::read<uint32_t>(
buffer.begin() + currentTokenOffset, endianness::little);
currentTokenOffset += tokenSize;
return value;
}
/// Returns location where the last parsed token begins (at offset
/// -4 from the currentTokenOffset).
FileLineColLoc getLocation(int offset = -4) const {
return FileLineColLoc::get(name, 0, currentTokenOffset + offset);
}
bool isImmOperand(uint32_t token) {
switch (DECODE_D3D10_SB_OPERAND_TYPE(token)) {
case D3D10_SB_OPERAND_TYPE_IMMEDIATE32:
case D3D10_SB_OPERAND_TYPE_IMMEDIATE64:
return true;
default:
return false;
}
}
FailureOr<OperandComponents> parseOperandComponents(uint32_t token) {
OperandComponents components;
switch (DECODE_D3D10_SB_OPERAND_NUM_COMPONENTS(token)) {
case D3D10_SB_OPERAND_0_COMPONENT: {
components.num = 0;
break;
}
case D3D10_SB_OPERAND_1_COMPONENT: {
components.num = 1;
break;
}
case D3D10_SB_OPERAND_4_COMPONENT: {
components.num = 4;
break;
}
default:
emitError(getLocation(), "unexpected number of components");
return failure();
}
if (components.num != 4 || isImmOperand(token))
return components;
switch (DECODE_D3D10_SB_OPERAND_4_COMPONENT_SELECTION_MODE(token)) {
case D3D10_SB_OPERAND_4_COMPONENT_MASK_MODE: {
components.kind = OperandComponentsKind::Mask;
components.mask = DECODE_D3D10_SB_OPERAND_4_COMPONENT_MASK(token);
break;
}
case D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_MODE: {
components.kind = OperandComponentsKind::Swizzle;
components.swizzle[0] =
DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(token, 0);
components.swizzle[1] =
DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(token, 1);
components.swizzle[2] =
DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(token, 2);
components.swizzle[3] =
DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(token, 3);
break;
}
case D3D10_SB_OPERAND_4_COMPONENT_SELECT_1_MODE: {
components.kind = OperandComponentsKind::One;
components.one = DECODE_D3D10_SB_OPERAND_4_COMPONENT_SELECT_1(token);
break;
}
default:
emitError(getLocation(), "unexpected component selection");
return failure();
}
return components;
}
using OperandIndexTypes = SmallVector<uint32_t, 3>;
FailureOr<OperandIndexTypes> parseOperandIndexTypes(uint32_t token) {
SmallVector<uint32_t, 3> indexTypes;
if (isImmOperand(token))
return indexTypes; // none
uint32_t indexDimension = DECODE_D3D10_SB_OPERAND_INDEX_DIMENSION(token);
if (indexDimension > 3) {
emitError(getLocation(),
"invalid operand index dimension (must be <= 3)");
return failure();
}
if (indexDimension == D3D10_SB_OPERAND_INDEX_0D)
return indexTypes; // none
indexTypes.resize(indexDimension);
for (unsigned i = 0; i < indexTypes.size(); ++i) {
indexTypes[i] = DECODE_D3D10_SB_OPERAND_INDEX_REPRESENTATION(i, token);
}
return indexTypes;
}
FailureOr<Index> parseIndex(uint32_t indexType) {
switch (indexType) {
case D3D10_SB_OPERAND_INDEX_IMMEDIATE32: {
Token value = parseToken();
if (failed(value)) {
emitError(getLocation(), "expected an operand index imm32");
return failure();
}
return builder.buildIndexImm32(*value, getLocation());
}
case D3D10_SB_OPERAND_INDEX_IMMEDIATE64: {
Token value0 = parseToken();
if (failed(value0)) {
emitError(getLocation(), "expected an operand index imm64");
return failure();
}
FileLineColLoc loc = getLocation();
Token value1 = parseToken();
if (failed(value1)) {
emitError(getLocation(),
"expected an operand index imm64 (second token)");
return failure();
}
// TODO: check the order of tokens (MSB or LSB?)
return builder.buildIndexImm64((((uint64_t)*value0) << 32) | *value1,
loc);
}
case D3D10_SB_OPERAND_INDEX_RELATIVE: {
FailureOr<Operand> operand = parseOperand();
if (failed(operand)) {
emitError(getLocation(), "expected an index operand");
return failure();
}
return builder.buildIndexRelative(*operand, getLocation());
}
case D3D10_SB_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE: {
Token imm = parseToken();
if (failed(imm)) {
emitError(getLocation(), "expected an operand index relative (imm)");
return failure();
}
FileLineColLoc loc = getLocation();
FailureOr<Operand> operand = parseOperand();
if (failed(operand)) {
emitError(getLocation(),
"expected an operand index relative (operand)");
return failure();
}
return builder.buildIndexImm32PlusRelative(*imm, *operand, loc);
}
default:
emitError(getLocation(), "invalid operand index type");
return failure();
}
}
FailureOr<std::optional<OperandModifier>>
parseOperandExtendedModifier(uint32_t extToken) {
std::optional<OperandModifier> none;
if (D3D10_SB_EXTENDED_OPERAND_MODIFIER !=
DECODE_D3D10_SB_EXTENDED_OPERAND_TYPE(extToken))
return none;
OperandModifier modifier;
modifier.modifier = DECODE_D3D10_SB_OPERAND_MODIFIER(extToken);
modifier.minPrecision = DECODE_D3D11_SB_OPERAND_MIN_PRECISION(extToken);
modifier.nonUniform = DECODE_D3D12_SB_OPERAND_NON_UNIFORM(extToken);
return std::make_optional(modifier);
}
FailureOr<Operand> parseOperand() {
Token token = parseToken();
if (failed(token))
return failure();
FileLineColLoc loc = getLocation();
uint32_t opType = DECODE_D3D10_SB_OPERAND_TYPE(*token);
bool isExtended = DECODE_IS_D3D10_SB_OPERAND_EXTENDED(*token);
FailureOr<OperandComponents> components = parseOperandComponents(*token);
if (failed(components))
return failure();
FailureOr<OperandIndexTypes> indexTypes = parseOperandIndexTypes(*token);
if (failed(indexTypes))
return failure();
std::optional<OperandModifier> modifier;
if (isExtended) {
Token extToken = parseToken();
if (failed(extToken)) {
emitError(getLocation(), "unexpected an extended operand token");
return failure();
}
auto failureOrModifier = parseOperandExtendedModifier(*extToken);
if (failed(failureOrModifier))
return failure();
modifier = *failureOrModifier;
}
if (isImmOperand(*token)) {
switch (opType) {
case D3D10_SB_OPERAND_TYPE_IMMEDIATE32: {
SmallVector<int32_t, 4> values;
for (unsigned i = 0; i < components->num; ++i) {
Token value = parseToken();
if (failed(value)) {
emitError(getLocation(), "expected an immediate operand (imm32)");
return failure();
}
values.push_back(*value);
}
return builder.buildOperandImm32(values, loc);
}
case D3D10_SB_OPERAND_TYPE_IMMEDIATE64: {
if (components->num != 4) {
emitError(getLocation(), "imm64 operand must have 4 components");
return failure();
}
SmallVector<int64_t, 2> values;
for (unsigned i = 0; i < 2; ++i) {
Token high = parseToken();
if (failed(high)) {
emitError(getLocation(),
"expected an immediate operand (imm64 high)");
return failure();
}
Token low = parseToken();
if (failed(low)) {
emitError(getLocation(),
"expected an immediate operand (imm64 low)");
return failure();
}
values.push_back((((int64_t)*high) << 32) | *low);
}
return builder.buildOperandImm64(values, loc);
}
}
emitError(getLocation(), "unhandled immediate type");
return failure();
}
// Operand indices
SmallVector<Index, 3> indices;
for (uint32_t indexType : *indexTypes) {
FailureOr<Index> index = parseIndex(indexType);
if (failed(index))
return failure();
indices.push_back(*index);
}
return builder.buildOperand(opType, *components, indices, modifier,
getLocation());
}
FailureOr<Instruction> parseDclGlobalFlags(uint32_t opcodeToken,
Location loc) {
return builder.buildDclGlobalFlags(
DECODE_D3D10_SB_GLOBAL_FLAGS(opcodeToken), loc);
}
FailureOr<Instruction> parseDclTemps(Location loc) {
auto countToken = parseToken();
if (failed(countToken))
return failure();
auto count = *countToken;
if (count == 0) {
emitError(getLocation(), "temp register count cannot be zero");
return failure();
}
if (count > 4096) {
emitError(getLocation(), "invalid temp register count: ")
<< count << " (max 4096)";
return failure();
}
return builder.buildDclTemps(count, loc);
}
OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
Instruction &out) {
FailureOr<Instruction> result;
switch (DECODE_D3D10_SB_OPCODE_TYPE(opcodeToken)) {
case D3D10_SB_OPCODE_DCL_GLOBAL_FLAGS:
result = parseDclGlobalFlags(opcodeToken, loc);
break;
case D3D10_SB_OPCODE_DCL_TEMPS:
result = parseDclTemps(loc);
break;
default:
return std::nullopt;
}
if (failed(result))
return failure();
out = *result;
return success();
}
FailureOr<Instruction> parseInstruction() {
size_t beginOffset = currentTokenOffset;
Token token = parseToken();
if (failed(token))
return failure();
FileLineColLoc loc = getLocation();
uint32_t opcode = DECODE_D3D10_SB_OPCODE_TYPE(*token);
InstructionModifier modifier;
modifier.preciseMask = DECODE_D3D11_SB_INSTRUCTION_PRECISE_VALUES(*token);
modifier.saturate = DECODE_IS_D3D10_SB_INSTRUCTION_SATURATE_ENABLED(*token);
uint32_t length = DECODE_D3D10_SB_TOKENIZED_INSTRUCTION_LENGTH(*token);
// TODO: extended instructions:
// BOOL b51PlusShader =
// BOOL bExtended = DECODE_IS_D3D10_SB_OPCODE_EXTENDED(Token)
// ...
if (opcode >= D3D10_SB_NUM_OPCODES) {
emitError(getLocation(), "unknown opcode");
return failure();
}
auto opcodeToken = *token;
Instruction dclInstruction;
auto parseResult = parseDclInstruction(opcodeToken, loc, dclInstruction);
if (parseResult.has_value()) {
if (failed(*parseResult))
return failure();
if (failed(verifyInstructionLength(beginOffset, length)))
return failure();
return dclInstruction;
}
unsigned numOperands = instrInfo[opcode].numOperands;
SmallVector<Operand, 8> operands;
for (unsigned i = 0; i < numOperands; ++i) {
FailureOr<Operand> operand = parseOperand();
if (failed(operand))
return failure();
operands.push_back(*operand);
}
if (failed(verifyInstructionLength(beginOffset, length)))
return failure();
return builder.buildInstruction(instrInfo[opcode].name, operands, modifier,
loc);
}
FailureOr<Module> parseModule() {
FileLineColLoc loc = getLocation(0);
std::vector<Instruction> instructions;
while (currentTokenOffset < buffer.size()) {
FailureOr<Instruction> inst = parseInstruction();
if (failed(inst)) {
return failure();
}
instructions.push_back(*inst);
}
return builder.buildModule(instructions, loc);
}
LogicalResult verifyInstructionLength(size_t beginOffset, uint32_t length) {
if (((currentTokenOffset - beginOffset) / 4) != length) {
emitError(getLocation(), "instruction length mismatch");
return failure();
}
return success();
}
private:
DXBuilder &builder;
StringAttr name;
StringRef buffer;
size_t currentTokenOffset{0};
InstructionInfo instrInfo[D3D10_SB_NUM_OPCODES];
};
namespace mlir::dxsa {
OwningOpRef<ModuleOp> importDxsaBinaryToModule(llvm::SourceMgr &source,
MLIRContext *context) {
if (source.getNumBuffers() != 1) {
emitError(UnknownLoc::get(context), "one source file should be provided");
return nullptr;
}
uint32_t sourceBufId = source.getMainFileID();
StringRef buffer = source.getMemoryBuffer(sourceBufId)->getBuffer();
StringAttr name = StringAttr::get(
context, source.getMemoryBuffer(sourceBufId)->getBufferIdentifier());
// FIXME:
context->allowUnregisteredDialects();
context->loadAllAvailableDialects();
DXBuilder builder(context, name);
Parser parser(builder, name, buffer);
FailureOr<ModuleOp> mod = parser.parseModule();
if (failed(mod))
return {nullptr};
return {*mod};
}
} // namespace mlir::dxsa