Skip to content

Commit 9613125

Browse files
committed
[mlir][dxsa] Add dcl_resource_structured instruction
Example: dxsa.dcl_resource_structured <id = 3, struct_byte_stride = 16> dxsa.dcl_resource_structured <id = 0, struct_byte_stride = 32, lbound = 0, ubound = 3, space = 1> Signed-off-by: Vladimir Shiryaev <tagolog@users.noreply.github.com>
1 parent ebce8ee commit 9613125

6 files changed

Lines changed: 111 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,4 +922,34 @@ def DXSA_DclTgsmStructured : DXSA_Op<"dcl_tgsm_structured"> {
922922
let hasVerifier = 1;
923923
}
924924

925+
def DXSA_DclResourceStructured : DXSA_Op<"dcl_resource_structured"> {
926+
let summary = "declares a structured buffer shader input resource bound to a register";
927+
let description = [{
928+
The `dxsa.dcl_resource_structured` operation declares a structured
929+
buffer shader input resource bound to a register.
930+
931+
Example:
932+
933+
```mlir
934+
dxsa.dcl_resource_structured <id = 3, struct_byte_stride = 16>
935+
dxsa.dcl_resource_structured
936+
<id = 0, struct_byte_stride = 32, lbound = 0, ubound = 3, space = 1>
937+
```
938+
}];
939+
940+
let arguments = (ins
941+
I32Attr:$id,
942+
ConfinedAttr<I32Attr, [IntPositive]>:$struct_byte_stride,
943+
OptionalAttr<I32Attr>:$lbound,
944+
OptionalAttr<I32Attr>:$ubound,
945+
OptionalAttr<I32Attr>:$space);
946+
let assemblyFormat = [{
947+
` ` `<` `id` `=` $id `,` `struct_byte_stride` `=` $struct_byte_stride
948+
(`,` `lbound` `=` $lbound^ `,` `ubound` `=` $ubound
949+
`,` `space` `=` $space)? `>`
950+
attr-dict
951+
}];
952+
let hasVerifier = 1;
953+
}
954+
925955
#endif // DXSA_OPS

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ LogicalResult DclTgsmStructured::verify() {
8484
return success();
8585
}
8686

87+
LogicalResult DclResourceStructured::verify() {
88+
auto stride = getStructByteStride();
89+
if (stride % 4 != 0)
90+
return emitOpError("struct byte stride must be a multiple of 4, got ")
91+
<< stride;
92+
auto lbound = getLbound();
93+
auto ubound = getUbound();
94+
if (lbound && ubound && *lbound > *ubound)
95+
return emitOpError("expected lbound <= ubound, got lbound=")
96+
<< *lbound << ", ubound=" << *ubound;
97+
return success();
98+
}
99+
87100
//===----------------------------------------------------------------------===//
88101
// TableGen'd attribute method definitions
89102
//===----------------------------------------------------------------------===//

mlir/lib/Target/DXSA/BinaryParser.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,19 @@ class DXBuilder {
692692
builder.getI32IntegerAttr(structCount));
693693
}
694694

695+
Instruction buildDclResourceStructured(uint32_t id, uint32_t structByteStride,
696+
std::optional<uint32_t> lbound,
697+
std::optional<uint32_t> ubound,
698+
std::optional<uint32_t> space,
699+
Location loc) {
700+
auto toAttr = [&](std::optional<uint32_t> v) -> IntegerAttr {
701+
return v ? builder.getI32IntegerAttr(*v) : IntegerAttr();
702+
};
703+
return dxsa::DclResourceStructured::create(builder, loc, id,
704+
structByteStride, toAttr(lbound),
705+
toAttr(ubound), toAttr(space));
706+
}
707+
695708
private:
696709
MLIRContext *context;
697710
ModuleOp module;
@@ -1317,6 +1330,38 @@ class Parser {
13171330
*structCount, loc);
13181331
}
13191332

1333+
FailureOr<Instruction> parseDclResourceStructured(Location loc) {
1334+
auto operand = parseInlineOperand();
1335+
FAILURE_IF_FAILED(operand);
1336+
if (operand->getType() != dxsa::InlineOperandType::resource)
1337+
return emitError(loc, "operand must be a resource register, got ")
1338+
<< dxsa::stringifyInlineOperandType(operand->getType());
1339+
auto indexArray = operand->getIndex();
1340+
auto indexDim = indexArray ? indexArray.size() : 0;
1341+
if (indexDim != 1 && indexDim != 3)
1342+
return emitError(loc, "operand must have a 1D or 3D index, got ")
1343+
<< indexDim;
1344+
auto id = indexArray[0];
1345+
std::optional<uint32_t> lbound, ubound;
1346+
if (indexDim == 3) {
1347+
lbound = indexArray[1];
1348+
ubound = indexArray[2];
1349+
}
1350+
1351+
auto strideToken = parseToken();
1352+
FAILURE_IF_FAILED(strideToken);
1353+
1354+
std::optional<uint32_t> space;
1355+
if (indexDim == 3) {
1356+
auto spaceToken = parseToken();
1357+
FAILURE_IF_FAILED(spaceToken);
1358+
space = *spaceToken;
1359+
}
1360+
1361+
return builder.buildDclResourceStructured(id, *strideToken, lbound, ubound,
1362+
space, loc);
1363+
}
1364+
13201365
OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
13211366
Instruction &out) {
13221367
FailureOr<Instruction> result;
@@ -1393,6 +1438,9 @@ class Parser {
13931438
case D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED:
13941439
result = parseDclTgsmStructured(loc);
13951440
break;
1441+
case D3D11_SB_OPCODE_DCL_RESOURCE_STRUCTURED:
1442+
result = parseDclResourceStructured(loc);
1443+
break;
13961444
default:
13971445
return std::nullopt;
13981446
}
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_resource_structured.bin | FileCheck %s
2+
3+
// CHECK: module {
4+
// CHECK-NEXT: dxsa.dcl_resource_structured <id = 3, struct_byte_stride = 16>
5+
// CHECK-NEXT: dxsa.dcl_resource_structured <id = 0, struct_byte_stride = 32, lbound = 0, ubound = 3, space = 1>
6+
// CHECK-NEXT: }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics
2+
3+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op struct byte stride must be a multiple of 4, got 6}}
4+
dxsa.dcl_resource_structured <id = 0, struct_byte_stride = 6>
5+
6+
// -----
7+
8+
// expected-error@+1 {{attribute 'struct_byte_stride' failed to satisfy constraint: 32-bit signless integer attribute whose value is positive}}
9+
dxsa.dcl_resource_structured <id = 0, struct_byte_stride = 0>
10+
11+
// -----
12+
13+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op expected lbound <= ubound, got lbound=5, ubound=3}}
14+
dxsa.dcl_resource_structured <id = 0, struct_byte_stride = 16, lbound = 5, ubound = 3, space = 1>
44 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)