Skip to content

Commit 3a4488a

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 b593a96 commit 3a4488a

7 files changed

Lines changed: 208 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
@@ -1172,4 +1172,34 @@ def DXSA_DclResource : DXSA_Op<"dcl_resource"> {
11721172
let hasVerifier = 1;
11731173
}
11741174

1175+
def DXSA_DclResourceStructured : DXSA_Op<"dcl_resource_structured"> {
1176+
let summary = "declares a structured buffer shader input resource bound to a register";
1177+
let description = [{
1178+
The `dxsa.dcl_resource_structured` operation declares a structured
1179+
buffer shader input resource bound to a register.
1180+
1181+
Example:
1182+
1183+
```mlir
1184+
dxsa.dcl_resource_structured <id = 3, struct_byte_stride = 16>
1185+
dxsa.dcl_resource_structured
1186+
<id = 0, struct_byte_stride = 32, lbound = 0, ubound = 3, space = 1>
1187+
```
1188+
}];
1189+
1190+
let arguments = (ins
1191+
I32Attr:$id,
1192+
ConfinedAttr<I32Attr, [IntPositive]>:$struct_byte_stride,
1193+
OptionalAttr<I32Attr>:$lbound,
1194+
OptionalAttr<I32Attr>:$ubound,
1195+
OptionalAttr<I32Attr>:$space);
1196+
let assemblyFormat = [{
1197+
` ` `<` `id` `=` $id `,` `struct_byte_stride` `=` $struct_byte_stride
1198+
(`,` `lbound` `=` $lbound^ `,` `ubound` `=` $ubound
1199+
`,` `space` `=` $space)? `>`
1200+
attr-dict
1201+
}];
1202+
let hasVerifier = 1;
1203+
}
1204+
11751205
#endif // DXSA_OPS

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ LogicalResult DclResource::verify() {
113113
return emitOpError("sample_count is only valid for texture2dms and "
114114
"texture2dmsarray, got ")
115115
<< stringifyResourceDimension(dim);
116+
auto lbound = getLbound();
117+
auto ubound = getUbound();
118+
auto space = getSpace();
119+
if ((lbound || ubound || space) && !(lbound && ubound && space))
120+
return emitOpError(
121+
"lbound, ubound and space must be either all set or all absent");
122+
if (lbound && ubound && *lbound > *ubound)
123+
return emitOpError("expected lbound <= ubound, got lbound=")
124+
<< *lbound << ", ubound=" << *ubound;
125+
return success();
126+
}
127+
128+
LogicalResult DclResourceStructured::verify() {
129+
auto stride = getStructByteStride();
130+
if (stride % 4 != 0)
131+
return emitOpError("struct byte stride must be a multiple of 4, got ")
132+
<< stride;
133+
auto lbound = getLbound();
134+
auto ubound = getUbound();
135+
auto space = getSpace();
136+
if ((lbound || ubound || space) && !(lbound && ubound && space))
137+
return emitOpError(
138+
"lbound, ubound and space must be either all set or all absent");
139+
if (lbound && ubound && *lbound > *ubound)
140+
return emitOpError("expected lbound <= ubound, got lbound=")
141+
<< *lbound << ", ubound=" << *ubound;
142+
return success();
116143
}
117144

118145
//===----------------------------------------------------------------------===//

mlir/lib/Target/DXSA/BinaryParser.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,19 @@ class DXBuilder {
747747
toAttr(ubound), toAttr(space));
748748
}
749749

750+
Instruction buildDclResourceStructured(uint32_t id, uint32_t structByteStride,
751+
std::optional<uint32_t> lbound,
752+
std::optional<uint32_t> ubound,
753+
std::optional<uint32_t> space,
754+
Location loc) {
755+
auto toAttr = [&](std::optional<uint32_t> v) -> IntegerAttr {
756+
return v ? builder.getI32IntegerAttr(*v) : IntegerAttr();
757+
};
758+
return dxsa::DclResourceStructured::create(builder, loc, id,
759+
structByteStride, toAttr(lbound),
760+
toAttr(ubound), toAttr(space));
761+
}
762+
750763
private:
751764
MLIRContext *context;
752765
ModuleOp module;
@@ -1557,6 +1570,38 @@ class Parser {
15571570
lbound, ubound, space, loc);
15581571
}
15591572

1573+
FailureOr<Instruction> parseDclResourceStructured(Location loc) {
1574+
auto operand = parseInlineOperand();
1575+
FAILURE_IF_FAILED(operand);
1576+
if (operand->getType() != dxsa::InlineOperandType::resource)
1577+
return emitError(loc, "operand must be a resource register, got ")
1578+
<< dxsa::stringifyInlineOperandType(operand->getType());
1579+
auto indexArray = operand->getIndex();
1580+
auto indexDim = indexArray ? indexArray.size() : 0;
1581+
if (indexDim != 1 && indexDim != 3)
1582+
return emitError(loc, "operand must have a 1D or 3D index, got ")
1583+
<< indexDim;
1584+
auto id = indexArray[0];
1585+
std::optional<uint32_t> lbound, ubound;
1586+
if (indexDim == 3) {
1587+
lbound = indexArray[1];
1588+
ubound = indexArray[2];
1589+
}
1590+
1591+
auto strideToken = parseToken();
1592+
FAILURE_IF_FAILED(strideToken);
1593+
1594+
std::optional<uint32_t> space;
1595+
if (indexDim == 3) {
1596+
auto spaceToken = parseToken();
1597+
FAILURE_IF_FAILED(spaceToken);
1598+
space = *spaceToken;
1599+
}
1600+
1601+
return builder.buildDclResourceStructured(id, *strideToken, lbound, ubound,
1602+
space, loc);
1603+
}
1604+
15601605
OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
15611606
Instruction &out) {
15621607
FailureOr<Instruction> result;
@@ -1651,6 +1696,9 @@ class Parser {
16511696
case D3D10_SB_OPCODE_DCL_RESOURCE:
16521697
result = parseDclResource(opcodeToken, loc);
16531698
break;
1699+
case D3D11_SB_OPCODE_DCL_RESOURCE_STRUCTURED:
1700+
result = parseDclResourceStructured(loc);
1701+
break;
16541702
default:
16551703
return std::nullopt;
16561704
}

mlir/test/Target/DXSA/dcl_resource_invalid.mlir

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,56 @@ dxsa.dcl_resource <id = 0>, <dim = texture2dms, sample_count = 128>, <x = float,
6262

6363
// expected-error@+1 {{'dxsa.dcl_resource' op expected lbound <= ubound, got lbound=5, ubound=3}}
6464
dxsa.dcl_resource <id = 0, lbound = 5, ubound = 3, space = 1>, <dim = buffer>, <x = float, y = float, z = float, w = float>
65+
66+
// -----
67+
68+
// expected-error@+1 {{'dxsa.dcl_resource' op expected lbound <= ubound, got lbound=5, ubound=3}}
69+
dxsa.dcl_resource <id = 0, lbound = 5, ubound = 3, space = 1>, <dim = buffer>, <x = float, y = float, z = float, w = float>
70+
71+
// -----
72+
73+
// expected-error@+1 {{'dxsa.dcl_resource' op lbound, ubound and space must be either all set or all absent}}
74+
"dxsa.dcl_resource"() {id = 0 : i32, dim = #dxsa<resource_dimension buffer>,
75+
x = #dxsa<resource_return_type float>, y = #dxsa<resource_return_type float>,
76+
z = #dxsa<resource_return_type float>, w = #dxsa<resource_return_type float>,
77+
lbound = 0 : i32} : () -> ()
78+
79+
// -----
80+
81+
// expected-error@+1 {{'dxsa.dcl_resource' op lbound, ubound and space must be either all set or all absent}}
82+
"dxsa.dcl_resource"() {id = 0 : i32, dim = #dxsa<resource_dimension buffer>,
83+
x = #dxsa<resource_return_type float>, y = #dxsa<resource_return_type float>,
84+
z = #dxsa<resource_return_type float>, w = #dxsa<resource_return_type float>,
85+
ubound = 3 : i32} : () -> ()
86+
87+
// -----
88+
89+
// expected-error@+1 {{'dxsa.dcl_resource' op lbound, ubound and space must be either all set or all absent}}
90+
"dxsa.dcl_resource"() {id = 0 : i32, dim = #dxsa<resource_dimension buffer>,
91+
x = #dxsa<resource_return_type float>, y = #dxsa<resource_return_type float>,
92+
z = #dxsa<resource_return_type float>, w = #dxsa<resource_return_type float>,
93+
space = 1 : i32} : () -> ()
94+
95+
// -----
96+
97+
// expected-error@+1 {{'dxsa.dcl_resource' op lbound, ubound and space must be either all set or all absent}}
98+
"dxsa.dcl_resource"() {id = 0 : i32, dim = #dxsa<resource_dimension buffer>,
99+
x = #dxsa<resource_return_type float>, y = #dxsa<resource_return_type float>,
100+
z = #dxsa<resource_return_type float>, w = #dxsa<resource_return_type float>,
101+
lbound = 0 : i32, ubound = 3 : i32} : () -> ()
102+
103+
// -----
104+
105+
// expected-error@+1 {{'dxsa.dcl_resource' op lbound, ubound and space must be either all set or all absent}}
106+
"dxsa.dcl_resource"() {id = 0 : i32, dim = #dxsa<resource_dimension buffer>,
107+
x = #dxsa<resource_return_type float>, y = #dxsa<resource_return_type float>,
108+
z = #dxsa<resource_return_type float>, w = #dxsa<resource_return_type float>,
109+
lbound = 0 : i32, space = 1 : i32} : () -> ()
110+
111+
// -----
112+
113+
// expected-error@+1 {{'dxsa.dcl_resource' op lbound, ubound and space must be either all set or all absent}}
114+
"dxsa.dcl_resource"() {id = 0 : i32, dim = #dxsa<resource_dimension buffer>,
115+
x = #dxsa<resource_return_type float>, y = #dxsa<resource_return_type float>,
116+
z = #dxsa<resource_return_type float>, w = #dxsa<resource_return_type float>,
117+
ubound = 3 : i32, space = 1 : i32} : () -> ()
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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>
15+
16+
// -----
17+
18+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op lbound, ubound and space must be either all set or all absent}}
19+
"dxsa.dcl_resource_structured"() {id = 0 : i32, struct_byte_stride = 16 : i32, lbound = 0 : i32} : () -> ()
20+
21+
// -----
22+
23+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op lbound, ubound and space must be either all set or all absent}}
24+
"dxsa.dcl_resource_structured"() {id = 0 : i32, struct_byte_stride = 16 : i32, ubound = 3 : i32} : () -> ()
25+
26+
// -----
27+
28+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op lbound, ubound and space must be either all set or all absent}}
29+
"dxsa.dcl_resource_structured"() {id = 0 : i32, struct_byte_stride = 16 : i32, space = 1 : i32} : () -> ()
30+
31+
// -----
32+
33+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op lbound, ubound and space must be either all set or all absent}}
34+
"dxsa.dcl_resource_structured"() {id = 0 : i32, struct_byte_stride = 16 : i32, lbound = 0 : i32, ubound = 3 : i32} : () -> ()
35+
36+
// -----
37+
38+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op lbound, ubound and space must be either all set or all absent}}
39+
"dxsa.dcl_resource_structured"() {id = 0 : i32, struct_byte_stride = 16 : i32, lbound = 0 : i32, space = 1 : i32} : () -> ()
40+
41+
// -----
42+
43+
// expected-error@+1 {{'dxsa.dcl_resource_structured' op lbound, ubound and space must be either all set or all absent}}
44+
"dxsa.dcl_resource_structured"() {id = 0 : i32, struct_byte_stride = 16 : i32, ubound = 3 : i32, space = 1 : i32} : () -> ()
44 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)