Skip to content

Commit 4b4e55c

Browse files
committed
[mlir][dxsa] Add dcl_resource instruction
Example: dxsa.dcl_resource <type = resource, components = 0, index = [0, 0, 3]>, <x = float, y = float, z = float, w = float>, <dim = texture3d, space = 1> Signed-off-by: Vladimir Shiryaev <tagolog@users.noreply.github.com>
1 parent ebce8ee commit 4b4e55c

6 files changed

Lines changed: 268 additions & 0 deletions

File tree

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,64 @@ def DXSA_SystemValueNameAttr :
255255
let assemblyFormat = "`<` $value `>`";
256256
}
257257

258+
def DXSA_ResourceDimension_Buffer : I32EnumAttrCase<"buffer", 1>;
259+
def DXSA_ResourceDimension_Texture1D : I32EnumAttrCase<"texture1d", 2>;
260+
def DXSA_ResourceDimension_Texture2D : I32EnumAttrCase<"texture2d", 3>;
261+
def DXSA_ResourceDimension_Texture2DMS : I32EnumAttrCase<"texture2dms", 4>;
262+
def DXSA_ResourceDimension_Texture3D : I32EnumAttrCase<"texture3d", 5>;
263+
def DXSA_ResourceDimension_TextureCube : I32EnumAttrCase<"texturecube", 6>;
264+
def DXSA_ResourceDimension_Texture1DArray : I32EnumAttrCase<"texture1darray", 7>;
265+
def DXSA_ResourceDimension_Texture2DArray : I32EnumAttrCase<"texture2darray", 8>;
266+
def DXSA_ResourceDimension_Texture2DMSArray : I32EnumAttrCase<"texture2dmsarray", 9>;
267+
def DXSA_ResourceDimension_TextureCubeArray : I32EnumAttrCase<"texturecubearray", 10>;
268+
269+
def DXSA_ResourceDimension : I32EnumAttr<
270+
"ResourceDimension", "input resource dimension", [
271+
DXSA_ResourceDimension_Buffer,
272+
DXSA_ResourceDimension_Texture1D,
273+
DXSA_ResourceDimension_Texture2D,
274+
DXSA_ResourceDimension_Texture2DMS,
275+
DXSA_ResourceDimension_Texture3D,
276+
DXSA_ResourceDimension_TextureCube,
277+
DXSA_ResourceDimension_Texture1DArray,
278+
DXSA_ResourceDimension_Texture2DArray,
279+
DXSA_ResourceDimension_Texture2DMSArray,
280+
DXSA_ResourceDimension_TextureCubeArray
281+
]> {
282+
let cppNamespace = "::mlir::dxsa";
283+
let genSpecializedAttr = 0;
284+
}
285+
286+
def DXSA_ResourceDimensionAttr :
287+
EnumAttr<DXSADialect, DXSA_ResourceDimension, "resource_dimension"> {
288+
let assemblyFormat = "$value";
289+
}
290+
291+
// `float` is a reserved C++ keyword, so the symbolic (C++ enum case) names use
292+
// PascalCase while the printable asm form stays lowercase.
293+
def DXSA_ResourceReturnType_Unorm : I32EnumAttrCase<"Unorm", 1, "unorm">;
294+
def DXSA_ResourceReturnType_Snorm : I32EnumAttrCase<"Snorm", 2, "snorm">;
295+
def DXSA_ResourceReturnType_Sint : I32EnumAttrCase<"Sint", 3, "sint">;
296+
def DXSA_ResourceReturnType_Uint : I32EnumAttrCase<"Uint", 4, "uint">;
297+
def DXSA_ResourceReturnType_Float : I32EnumAttrCase<"Float", 5, "float">;
298+
299+
def DXSA_ResourceReturnType : I32EnumAttr<
300+
"ResourceReturnType", "input resource per-component return type", [
301+
DXSA_ResourceReturnType_Unorm,
302+
DXSA_ResourceReturnType_Snorm,
303+
DXSA_ResourceReturnType_Sint,
304+
DXSA_ResourceReturnType_Uint,
305+
DXSA_ResourceReturnType_Float
306+
]> {
307+
let cppNamespace = "::mlir::dxsa";
308+
let genSpecializedAttr = 0;
309+
}
310+
311+
def DXSA_ResourceReturnTypeAttr :
312+
EnumAttr<DXSADialect, DXSA_ResourceReturnType, "resource_return_type"> {
313+
let assemblyFormat = "$value";
314+
}
315+
258316
//===----------------------------------------------------------------------===//
259317
// DXSA ComponentMask bit-enum (mask field of operand, normalized to bits 0..3)
260318
//===----------------------------------------------------------------------===//
@@ -922,4 +980,42 @@ def DXSA_DclTgsmStructured : DXSA_Op<"dcl_tgsm_structured"> {
922980
let hasVerifier = 1;
923981
}
924982

983+
def DXSA_DclResource : DXSA_Op<"dcl_resource"> {
984+
let summary = "declares a shader input resource bound to a register";
985+
let description = [{
986+
The `dxsa.dcl_resource` operation declares a shader input resource
987+
bound to a register.
988+
989+
Example:
990+
991+
```mlir
992+
dxsa.dcl_resource <type = resource, components = 0, index = [3]>,
993+
<x = unorm, y = unorm, z = unorm, w = unorm>, <dim = buffer>
994+
dxsa.dcl_resource <type = resource, components = 0, index = [0, 0, 3]>,
995+
<x = float, y = float, z = float, w = float>,
996+
<dim = texture3d, space = 1>
997+
```
998+
}];
999+
1000+
let arguments = (ins
1001+
DXSA_InlineOperandAttr:$operand,
1002+
DXSA_ResourceDimensionAttr:$dim,
1003+
DXSA_ResourceReturnTypeAttr:$x,
1004+
DXSA_ResourceReturnTypeAttr:$y,
1005+
DXSA_ResourceReturnTypeAttr:$z,
1006+
DXSA_ResourceReturnTypeAttr:$w,
1007+
OptionalAttr<ConfinedAttr<I32Attr,
1008+
[IntPositive, IntMaxValue<127>]>>:$sample_count,
1009+
OptionalAttr<I32Attr>:$space);
1010+
let assemblyFormat = [{
1011+
$operand `,`
1012+
`<` `x` `=` $x `,` `y` `=` $y `,` `z` `=` $z `,` `w` `=` $w `>` `,`
1013+
`<` `dim` `=` $dim
1014+
(`,` `sample_count` `=` $sample_count^)?
1015+
(`,` `space` `=` $space^)? `>`
1016+
attr-dict
1017+
}];
1018+
let hasVerifier = 1;
1019+
}
1020+
9251021
#endif // DXSA_OPS

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

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

87+
LogicalResult DclResource::verify() {
88+
auto operandType = getOperand().getType();
89+
if (operandType != InlineOperandType::resource)
90+
return emitOpError("operand must be a resource register, got ")
91+
<< stringifyInlineOperandType(operandType);
92+
auto dim = getDim();
93+
bool isMultisampled = dim == ResourceDimension::texture2dms ||
94+
dim == ResourceDimension::texture2dmsarray;
95+
if (isMultisampled && !getSampleCount())
96+
return emitOpError("missing sample_count for multisampled dimension ")
97+
<< stringifyResourceDimension(dim);
98+
if (!isMultisampled && getSampleCount())
99+
return emitOpError("sample_count is only valid for texture2dms and "
100+
"texture2dmsarray, got ")
101+
<< stringifyResourceDimension(dim);
102+
return success();
103+
}
104+
87105
//===----------------------------------------------------------------------===//
88106
// TableGen'd attribute method definitions
89107
//===----------------------------------------------------------------------===//

mlir/lib/Target/DXSA/BinaryParser.cpp

Lines changed: 70 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
696+
buildDclResource(dxsa::InlineOperandAttr operand, dxsa::ResourceDimension dim,
697+
dxsa::ResourceReturnType x, dxsa::ResourceReturnType y,
698+
dxsa::ResourceReturnType z, dxsa::ResourceReturnType w,
699+
std::optional<uint32_t> sampleCount,
700+
std::optional<uint32_t> space, Location loc) {
701+
auto sampleCountAttr =
702+
sampleCount ? builder.getI32IntegerAttr(*sampleCount) : IntegerAttr();
703+
auto spaceAttr = space ? builder.getI32IntegerAttr(*space) : IntegerAttr();
704+
return dxsa::DclResource::create(builder, loc, operand, dim, x, y, z, w,
705+
sampleCountAttr, spaceAttr);
706+
}
707+
695708
private:
696709
MLIRContext *context;
697710
ModuleOp module;
@@ -1317,6 +1330,60 @@ class Parser {
13171330
*structCount, loc);
13181331
}
13191332

1333+
FailureOr<dxsa::ResourceReturnType>
1334+
parseResourceReturnType(uint32_t returnTypeToken, uint32_t component,
1335+
Location loc) {
1336+
auto rawReturnType =
1337+
DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(returnTypeToken, component);
1338+
auto returnType = dxsa::symbolizeResourceReturnType(rawReturnType);
1339+
if (!returnType)
1340+
return emitError(loc, "unknown resource return type: ") << rawReturnType;
1341+
return *returnType;
1342+
}
1343+
1344+
FailureOr<Instruction> parseDclResource(uint32_t opcodeToken, Location loc) {
1345+
auto rawDim = DECODE_D3D10_SB_RESOURCE_DIMENSION(opcodeToken);
1346+
auto dim = dxsa::symbolizeResourceDimension(rawDim);
1347+
if (!dim)
1348+
return emitError(loc, "unknown resource dimension: ") << rawDim;
1349+
1350+
std::optional<uint32_t> sampleCount;
1351+
if (*dim == dxsa::ResourceDimension::texture2dms ||
1352+
*dim == dxsa::ResourceDimension::texture2dmsarray) {
1353+
auto rawSampleCount = DECODE_D3D10_SB_RESOURCE_SAMPLE_COUNT(opcodeToken);
1354+
if (rawSampleCount == 0)
1355+
return emitError(loc, "sample count must be non-zero for multisampled "
1356+
"dimension ")
1357+
<< dxsa::stringifyResourceDimension(*dim);
1358+
sampleCount = rawSampleCount;
1359+
}
1360+
1361+
auto operand = parseInlineOperand();
1362+
FAILURE_IF_FAILED(operand);
1363+
1364+
auto returnTypeToken = parseToken();
1365+
FAILURE_IF_FAILED(returnTypeToken);
1366+
auto x = parseResourceReturnType(*returnTypeToken, 0, loc);
1367+
FAILURE_IF_FAILED(x);
1368+
auto y = parseResourceReturnType(*returnTypeToken, 1, loc);
1369+
FAILURE_IF_FAILED(y);
1370+
auto z = parseResourceReturnType(*returnTypeToken, 2, loc);
1371+
FAILURE_IF_FAILED(z);
1372+
auto w = parseResourceReturnType(*returnTypeToken, 3, loc);
1373+
FAILURE_IF_FAILED(w);
1374+
1375+
std::optional<uint32_t> space;
1376+
auto indexArray = operand->getIndex();
1377+
if (indexArray && indexArray.size() == 3) {
1378+
auto spaceToken = parseToken();
1379+
FAILURE_IF_FAILED(spaceToken);
1380+
space = *spaceToken;
1381+
}
1382+
1383+
return builder.buildDclResource(*operand, *dim, *x, *y, *z, *w, sampleCount,
1384+
space, loc);
1385+
}
1386+
13201387
OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
13211388
Instruction &out) {
13221389
FailureOr<Instruction> result;
@@ -1393,6 +1460,9 @@ class Parser {
13931460
case D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED:
13941461
result = parseDclTgsmStructured(loc);
13951462
break;
1463+
case D3D10_SB_OPCODE_DCL_RESOURCE:
1464+
result = parseDclResource(opcodeToken, loc);
1465+
break;
13961466
default:
13971467
return std::nullopt;
13981468
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: mlir-translate --import-dxsa-bin %S/inputs/dcl_resource.bin | FileCheck %s
2+
3+
// CHECK: module {
4+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = unorm, y = snorm, z = sint, w = uint>, <dim = buffer>
5+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [1]>, <x = float, y = float, z = float, w = float>, <dim = texture1d>
6+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [2]>, <x = float, y = float, z = float, w = float>, <dim = texture1darray>
7+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [3]>, <x = float, y = float, z = float, w = float>, <dim = texture2d>
8+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [4]>, <x = float, y = float, z = float, w = float>, <dim = texture2darray>
9+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [5]>, <x = float, y = float, z = float, w = float>, <dim = texture2dms, sample_count = 4>
10+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [6]>, <x = float, y = float, z = float, w = float>, <dim = texture2dmsarray, sample_count = 8>
11+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [7]>, <x = float, y = float, z = float, w = float>, <dim = texture3d>
12+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [8]>, <x = float, y = float, z = float, w = float>, <dim = texturecube>
13+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [9]>, <x = float, y = float, z = float, w = float>, <dim = texturecubearray>
14+
// CHECK-NEXT: dxsa.dcl_resource <type = resource, components = 0, index = [0, 0, 3]>, <x = float, y = float, z = float, w = float>, <dim = texture3d, space = 1>
15+
// CHECK-NEXT: }
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics
2+
3+
// expected-error@+1 {{'dxsa.dcl_resource' op operand must be a resource register, got temp}}
4+
dxsa.dcl_resource <type = temp, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = buffer>
5+
6+
// -----
7+
8+
// expected-error@+1 {{'dxsa.dcl_resource' op operand must be a resource register, got uav}}
9+
dxsa.dcl_resource <type = uav, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = buffer>
10+
11+
// -----
12+
13+
// expected-error@+1 {{'dxsa.dcl_resource' op missing sample_count for multisampled dimension texture2dms}}
14+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture2dms>
15+
16+
// -----
17+
18+
// expected-error@+1 {{'dxsa.dcl_resource' op missing sample_count for multisampled dimension texture2dmsarray}}
19+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture2dmsarray>
20+
21+
// -----
22+
23+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got buffer}}
24+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = unorm, y = snorm, z = sint, w = uint>, <dim = buffer, sample_count = 4>
25+
26+
// -----
27+
28+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got texture1d}}
29+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture1d, sample_count = 4>
30+
31+
// -----
32+
33+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got texture1darray}}
34+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture1darray, sample_count = 4>
35+
36+
// -----
37+
38+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got texture2d}}
39+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture2d, sample_count = 4>
40+
41+
// -----
42+
43+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got texture2darray}}
44+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture2darray, sample_count = 4>
45+
46+
// -----
47+
48+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got texture3d}}
49+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture3d, sample_count = 4>
50+
51+
// -----
52+
53+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got texturecube}}
54+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texturecube, sample_count = 4>
55+
56+
// -----
57+
58+
// expected-error@+1 {{'dxsa.dcl_resource' op sample_count is only valid for texture2dms and texture2dmsarray, got texturecubearray}}
59+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texturecubearray, sample_count = 4>
60+
61+
// -----
62+
63+
// expected-error@+1 {{attribute 'sample_count' failed to satisfy constraint: 32-bit signless integer attribute whose value is positive whose maximum value is 127}}
64+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture2dms, sample_count = 0>
65+
66+
// -----
67+
68+
// expected-error@+1 {{attribute 'sample_count' failed to satisfy constraint: 32-bit signless integer attribute whose value is positive whose maximum value is 127}}
69+
dxsa.dcl_resource <type = resource, components = 0, index = [0]>, <x = float, y = float, z = float, w = float>, <dim = texture2dms, sample_count = 128>
188 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)