Skip to content

Commit d14c5f0

Browse files
committed
[mlir][dxsa] Add dcl_constant_buffer instruction
Example: dxsa.dcl_constant_buffer <slot = 0, size = 1>, <immediateIndexed> dxsa.dcl_constant_buffer <id = 0, lbound = 0, ubound = 3, size = 4, space = 1>, <dynamicIndexed> Signed-off-by: Vladimir Shiryaev <tagolog@users.noreply.github.com>
1 parent 014c7b3 commit d14c5f0

8 files changed

Lines changed: 154 additions & 11 deletions

File tree

mlir/include/mlir/Dialect/DXSA/IR/DXSAOps.td

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def DXSA_SystemValueName : I32EnumAttr<
252252

253253
def DXSA_SystemValueNameAttr :
254254
EnumAttr<DXSADialect, DXSA_SystemValueName, "system_value_name"> {
255-
let assemblyFormat = "$value";
255+
let assemblyFormat = "`<` $value `>`";
256256
}
257257

258258
//===----------------------------------------------------------------------===//
@@ -281,6 +281,24 @@ def DXSA_ComponentMaskAttr :
281281
let assemblyFormat = "`<` $value `>`";
282282
}
283283

284+
def DXSA_ConstantBufferAccessPattern_ImmediateIndexed : I32EnumAttrCase<"immediateIndexed", 0>;
285+
def DXSA_ConstantBufferAccessPattern_DynamicIndexed : I32EnumAttrCase<"dynamicIndexed", 1>;
286+
287+
def DXSA_ConstantBufferAccessPattern : I32EnumAttr<
288+
"ConstantBufferAccessPattern", "constant buffer access pattern", [
289+
DXSA_ConstantBufferAccessPattern_ImmediateIndexed,
290+
DXSA_ConstantBufferAccessPattern_DynamicIndexed
291+
]> {
292+
let cppNamespace = "::mlir::dxsa";
293+
let genSpecializedAttr = 0;
294+
}
295+
296+
def DXSA_ConstantBufferAccessPatternAttr :
297+
EnumAttr<DXSADialect, DXSA_ConstantBufferAccessPattern,
298+
"constant_buffer_access_pattern"> {
299+
let assemblyFormat = "`<` $value `>`";
300+
}
301+
284302
//===----------------------------------------------------------------------===//
285303
// DXSA op definitions
286304
//===----------------------------------------------------------------------===//
@@ -657,7 +675,7 @@ def DXSA_DclInputPsSiv : DXSA_Op<"dcl_input_ps_siv"> {
657675
Example:
658676

659677
```mlir
660-
dxsa.dcl_input_ps_siv linear %v0, position
678+
dxsa.dcl_input_ps_siv linear %v0, <position>
661679
```
662680
}];
663681
let arguments = (ins DXSA_InterpolationModeAttr:$mode,
@@ -675,7 +693,7 @@ def DXSA_DclInputPsSgv : DXSA_Op<"dcl_input_ps_sgv"> {
675693
Example:
676694

677695
```mlir
678-
dxsa.dcl_input_ps_sgv %v0, sampleIndex
696+
dxsa.dcl_input_ps_sgv %v0, <sampleIndex>
679697
```
680698
}];
681699
let arguments = (ins DXSA_OperandType:$operand,
@@ -719,4 +737,33 @@ def DXSA_DclOutput : DXSA_Op<"dcl_output"> {
719737
let assemblyFormat = "$operand attr-dict";
720738
}
721739

740+
def DXSA_DclConstantBuffer : DXSA_Op<"dcl_constant_buffer"> {
741+
let summary = "declares a constant buffer";
742+
let description = [{
743+
The `dxsa.dcl_constant_buffer` operation declares a constant buffer
744+
with its access pattern.
745+
746+
Examples:
747+
748+
```mlir
749+
dxsa.dcl_constant_buffer <id = 0, size = 1>, <immediateIndexed>
750+
dxsa.dcl_constant_buffer <id = 0, size = 4, lbound = 0, ubound = 3, space = 1>, <dynamicIndexed>
751+
```
752+
}];
753+
let arguments = (ins
754+
I32Attr:$id,
755+
I32Attr:$size,
756+
OptionalAttr<I32Attr>:$lbound,
757+
OptionalAttr<I32Attr>:$ubound,
758+
OptionalAttr<I32Attr>:$space,
759+
DXSA_ConstantBufferAccessPatternAttr:$access_pattern);
760+
let assemblyFormat = [{
761+
` ` `<` `id` `=` $id `,` `size` `=` $size
762+
(`,` `lbound` `=` $lbound^ `,` `ubound` `=` $ubound
763+
`,` `space` `=` $space)?
764+
`>` `,` $access_pattern attr-dict
765+
}];
766+
let hasVerifier = 1;
767+
}
768+
722769
#endif // DXSA_OPS

mlir/lib/Dialect/DXSA/IR/DXSA.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ void DXSADialect::initialize() {
4141
#define GET_OP_CLASSES
4242
#include "mlir/Dialect/DXSA/IR/DXSAOps.cpp.inc"
4343

44+
//===----------------------------------------------------------------------===//
45+
// Op verifiers
46+
//===----------------------------------------------------------------------===//
47+
48+
LogicalResult DclConstantBuffer::verify() {
49+
auto lbound = getLbound();
50+
auto ubound = getUbound();
51+
if (lbound && ubound && *lbound > *ubound)
52+
return emitOpError("expected lbound <= ubound, got lbound=")
53+
<< *lbound << ", ubound=" << *ubound;
54+
return success();
55+
}
56+
4457
//===----------------------------------------------------------------------===//
4558
// TableGen'd attribute method definitions
4659
//===----------------------------------------------------------------------===//

mlir/lib/Target/DXSA/BinaryParser.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mlir/IR/Location.h"
1313
#include "llvm/ADT/ArrayRef.h"
1414
#include "llvm/ADT/SmallVector.h"
15+
#include "llvm/ADT/bit.h"
1516
#include "llvm/Support/Debug.h"
1617
#include "llvm/Support/DebugLog.h"
1718
#include "llvm/Support/Endian.h"
@@ -635,6 +636,18 @@ class DXBuilder {
635636
return dxsa::DclOutput::create(builder, loc, operand);
636637
}
637638

639+
Instruction buildDclConstantBuffer(
640+
uint32_t id, uint32_t size, std::optional<uint32_t> lbound,
641+
std::optional<uint32_t> ubound, std::optional<uint32_t> space,
642+
dxsa::ConstantBufferAccessPattern accessPattern, Location loc) {
643+
auto optionalToAttr = [&](std::optional<uint32_t> v) -> IntegerAttr {
644+
return v ? builder.getI32IntegerAttr(*v) : IntegerAttr();
645+
};
646+
return dxsa::DclConstantBuffer::create(
647+
builder, loc, id, size, optionalToAttr(lbound), optionalToAttr(ubound),
648+
optionalToAttr(space), accessPattern);
649+
}
650+
638651
private:
639652
MLIRContext *context;
640653
ModuleOp module;
@@ -1174,6 +1187,63 @@ class Parser {
11741187
return builder.buildDclOutput(*operand, loc);
11751188
}
11761189

1190+
FailureOr<Instruction> parseDclConstantBuffer(uint32_t opcodeToken,
1191+
Location loc) {
1192+
auto rawAccessPattern =
1193+
DECODE_D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN(opcodeToken);
1194+
auto accessPattern =
1195+
dxsa::symbolizeConstantBufferAccessPattern(rawAccessPattern);
1196+
if (!accessPattern)
1197+
return emitError(loc, "unknown constant buffer access pattern: ")
1198+
<< rawAccessPattern;
1199+
1200+
auto operandToken = parseToken();
1201+
FAILURE_IF_FAILED(operandToken);
1202+
1203+
auto operandType = DECODE_D3D10_SB_OPERAND_TYPE(*operandToken);
1204+
if (operandType != D3D10_SB_OPERAND_TYPE_CONSTANT_BUFFER)
1205+
return emitError(loc, "unexpected operand type: ") << operandType;
1206+
1207+
if (DECODE_IS_D3D10_SB_OPERAND_EXTENDED(*operandToken))
1208+
return emitError(loc, "extended operand tokens are not supported");
1209+
1210+
auto indexDim = DECODE_D3D10_SB_OPERAND_INDEX_DIMENSION(*operandToken);
1211+
if (indexDim != D3D10_SB_OPERAND_INDEX_2D &&
1212+
indexDim != D3D10_SB_OPERAND_INDEX_3D)
1213+
return emitError(loc, "unsupported index dimension: ") << indexDim;
1214+
1215+
SmallVector<uint32_t, 3> indices;
1216+
indices.reserve(indexDim);
1217+
for (uint32_t i = 0; i < indexDim; ++i) {
1218+
auto indexRepesentation =
1219+
DECODE_D3D10_SB_OPERAND_INDEX_REPRESENTATION(i, *operandToken);
1220+
if (indexRepesentation != D3D10_SB_OPERAND_INDEX_IMMEDIATE32)
1221+
return emitError(loc, "unsupported index representation: ")
1222+
<< indexRepesentation;
1223+
auto value = parseToken();
1224+
FAILURE_IF_FAILED(value);
1225+
indices.push_back(*value);
1226+
}
1227+
1228+
switch (indexDim) {
1229+
case D3D10_SB_OPERAND_INDEX_2D:
1230+
return builder.buildDclConstantBuffer(
1231+
/*id=*/indices[0], /*size=*/indices[1], /*lbound=*/std::nullopt,
1232+
/*ubound=*/std::nullopt, /*space=*/std::nullopt, *accessPattern, loc);
1233+
case D3D10_SB_OPERAND_INDEX_3D: {
1234+
auto sizeToken = parseToken();
1235+
FAILURE_IF_FAILED(sizeToken);
1236+
auto spaceToken = parseToken();
1237+
FAILURE_IF_FAILED(spaceToken);
1238+
return builder.buildDclConstantBuffer(
1239+
/*id=*/indices[0], /*size=*/*sizeToken, /*lbound=*/indices[1],
1240+
/*ubound=*/indices[2], /*space=*/*spaceToken, *accessPattern, loc);
1241+
}
1242+
default:
1243+
return emitError(loc, "unsupported index dimension: ") << indexDim;
1244+
}
1245+
}
1246+
11771247
OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
11781248
Instruction &out) {
11791249
FailureOr<Instruction> result;
@@ -1220,6 +1290,9 @@ class Parser {
12201290
case D3D10_SB_OPCODE_DCL_OUTPUT:
12211291
result = parseDclOutput(loc);
12221292
break;
1293+
case D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER:
1294+
result = parseDclConstantBuffer(opcodeToken, loc);
1295+
break;
12231296
default:
12241297
return std::nullopt;
12251298
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: mlir-translate --import-dxsa-bin %S/inputs/dcl_constant_buffer.bin | FileCheck %s
2+
3+
// CHECK: module {
4+
// CHECK-NEXT: dxsa.dcl_constant_buffer <id = 0, size = 1>, <immediateIndexed>
5+
// CHECK-NEXT: dxsa.dcl_constant_buffer <id = 0, size = 4, lbound = 0, ubound = 3, space = 1>, <dynamicIndexed>
6+
// CHECK-NEXT: }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics
2+
3+
// expected-error@+1 {{expected lbound <= ubound, got lbound=5, ubound=3}}
4+
dxsa.dcl_constant_buffer <id = 0, size = 4, lbound = 5, ubound = 3, space = 1>, <dynamicIndexed>

mlir/test/Target/DXSA/dcl_input_ps_sgv.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ module {
55
// dcl_input_ps_sgv v0.x, primitiveID
66
// CHECK: %0 = dxsa.index.imm {imm = 0 : i32}
77
// CHECK-NEXT: %1 = dxsa.operand %0 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
8-
// CHECK-NEXT: dxsa.dcl_input_ps_sgv %1, primitiveID
8+
// CHECK-NEXT: dxsa.dcl_input_ps_sgv %1, <primitiveID>
99

1010
// dcl_input_ps_sgv v1.x, isFrontFace
1111
// CHECK-NEXT: %2 = dxsa.index.imm {imm = 1 : i32}
1212
// CHECK-NEXT: %3 = dxsa.operand %2 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
13-
// CHECK-NEXT: dxsa.dcl_input_ps_sgv %3, isFrontFace
13+
// CHECK-NEXT: dxsa.dcl_input_ps_sgv %3, <isFrontFace>
1414

1515
// dcl_input_ps_sgv v2.x, sampleIndex
1616
// CHECK-NEXT: %4 = dxsa.index.imm {imm = 2 : i32}
1717
// CHECK-NEXT: %5 = dxsa.operand %4 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
18-
// CHECK-NEXT: dxsa.dcl_input_ps_sgv %5, sampleIndex
18+
// CHECK-NEXT: dxsa.dcl_input_ps_sgv %5, <sampleIndex>
1919
}

mlir/test/Target/DXSA/dcl_input_ps_siv.mlir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ module {
55
// dcl_input_ps_siv linear v0.x, position
66
// CHECK: %0 = dxsa.index.imm {imm = 0 : i32}
77
// CHECK-NEXT: %1 = dxsa.operand %0 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
8-
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %1, position
8+
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %1, <position>
99

1010
// dcl_input_ps_siv linear v1.x, clipDistance
1111
// CHECK-NEXT: %2 = dxsa.index.imm {imm = 1 : i32}
1212
// CHECK-NEXT: %3 = dxsa.operand %2 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
13-
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %3, clipDistance
13+
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %3, <clipDistance>
1414

1515
// dcl_input_ps_siv linear v2.x, cullDistance
1616
// CHECK-NEXT: %4 = dxsa.index.imm {imm = 2 : i32}
1717
// CHECK-NEXT: %5 = dxsa.operand %4 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
18-
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %5, cullDistance
18+
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %5, <cullDistance>
1919

2020
// dcl_input_ps_siv linear v3.x, renderTargetArrayIndex
2121
// CHECK-NEXT: %6 = dxsa.index.imm {imm = 3 : i32}
2222
// CHECK-NEXT: %7 = dxsa.operand %6 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
23-
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %7, renderTargetArrayIndex
23+
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %7, <renderTargetArrayIndex>
2424

2525
// dcl_input_ps_siv linear v4.x, viewportArrayIndex
2626
// CHECK-NEXT: %8 = dxsa.index.imm {imm = 4 : i32}
2727
// CHECK-NEXT: %9 = dxsa.operand %8 {mask = 16 : i32, num_components = 4 : i32, type = 1 : i32}
28-
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %9, viewportArrayIndex
28+
// CHECK-NEXT: dxsa.dcl_input_ps_siv linear %9, <viewportArrayIndex>
2929
}
44 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)