Skip to content

Commit cdfb680

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 ebce8ee commit cdfb680

6 files changed

Lines changed: 138 additions & 0 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 attribute Constraints
286304
//===----------------------------------------------------------------------===//
@@ -922,4 +940,33 @@ def DXSA_DclTgsmStructured : DXSA_Op<"dcl_tgsm_structured"> {
922940
let hasVerifier = 1;
923941
}
924942

943+
def DXSA_DclConstantBuffer : DXSA_Op<"dcl_constant_buffer"> {
944+
let summary = "declares a constant buffer";
945+
let description = [{
946+
The `dxsa.dcl_constant_buffer` operation declares a constant buffer
947+
with its access pattern.
948+
949+
Examples:
950+
951+
```mlir
952+
dxsa.dcl_constant_buffer <id = 0, size = 1>, <immediateIndexed>
953+
dxsa.dcl_constant_buffer <id = 0, size = 4, lbound = 0, ubound = 3, space = 1>, <dynamicIndexed>
954+
```
955+
}];
956+
let arguments = (ins
957+
I32Attr:$id,
958+
I32Attr:$size,
959+
OptionalAttr<I32Attr>:$lbound,
960+
OptionalAttr<I32Attr>:$ubound,
961+
OptionalAttr<I32Attr>:$space,
962+
DXSA_ConstantBufferAccessPatternAttr:$access_pattern);
963+
let assemblyFormat = [{
964+
` ` `<` `id` `=` $id `,` `size` `=` $size
965+
(`,` `lbound` `=` $lbound^ `,` `ubound` `=` $ubound
966+
`,` `space` `=` $space)?
967+
`>` `,` $access_pattern attr-dict
968+
}];
969+
let hasVerifier = 1;
970+
}
971+
925972
#endif // DXSA_OPS

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

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

87+
LogicalResult DclConstantBuffer::verify() {
88+
auto lbound = getLbound();
89+
auto ubound = getUbound();
90+
if (lbound && ubound && *lbound > *ubound)
91+
return emitOpError("expected lbound <= ubound, got lbound=")
92+
<< *lbound << ", ubound=" << *ubound;
93+
return success();
94+
}
95+
8796
//===----------------------------------------------------------------------===//
8897
// TableGen'd attribute method definitions
8998
//===----------------------------------------------------------------------===//

mlir/lib/Target/DXSA/BinaryParser.cpp

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

695+
Instruction buildDclConstantBuffer(
696+
uint32_t id, uint32_t size, std::optional<uint32_t> lbound,
697+
std::optional<uint32_t> ubound, std::optional<uint32_t> space,
698+
dxsa::ConstantBufferAccessPattern accessPattern, Location loc) {
699+
auto optionalToAttr = [&](std::optional<uint32_t> v) -> IntegerAttr {
700+
return v ? builder.getI32IntegerAttr(*v) : IntegerAttr();
701+
};
702+
return dxsa::DclConstantBuffer::create(
703+
builder, loc, id, size, optionalToAttr(lbound), optionalToAttr(ubound),
704+
optionalToAttr(space), accessPattern);
705+
}
706+
695707
private:
696708
MLIRContext *context;
697709
ModuleOp module;
@@ -1317,6 +1329,63 @@ class Parser {
13171329
*structCount, loc);
13181330
}
13191331

1332+
FailureOr<Instruction> parseDclConstantBuffer(uint32_t opcodeToken,
1333+
Location loc) {
1334+
auto rawAccessPattern =
1335+
DECODE_D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN(opcodeToken);
1336+
auto accessPattern =
1337+
dxsa::symbolizeConstantBufferAccessPattern(rawAccessPattern);
1338+
if (!accessPattern)
1339+
return emitError(loc, "unknown constant buffer access pattern: ")
1340+
<< rawAccessPattern;
1341+
1342+
auto operandToken = parseToken();
1343+
FAILURE_IF_FAILED(operandToken);
1344+
1345+
auto operandType = DECODE_D3D10_SB_OPERAND_TYPE(*operandToken);
1346+
if (operandType != D3D10_SB_OPERAND_TYPE_CONSTANT_BUFFER)
1347+
return emitError(loc, "unexpected operand type: ") << operandType;
1348+
1349+
if (DECODE_IS_D3D10_SB_OPERAND_EXTENDED(*operandToken))
1350+
return emitError(loc, "extended operand tokens are not supported");
1351+
1352+
auto indexDim = DECODE_D3D10_SB_OPERAND_INDEX_DIMENSION(*operandToken);
1353+
if (indexDim != D3D10_SB_OPERAND_INDEX_2D &&
1354+
indexDim != D3D10_SB_OPERAND_INDEX_3D)
1355+
return emitError(loc, "unsupported index dimension: ") << indexDim;
1356+
1357+
SmallVector<uint32_t, 3> indices;
1358+
indices.reserve(indexDim);
1359+
for (uint32_t i = 0; i < indexDim; ++i) {
1360+
auto indexRepesentation =
1361+
DECODE_D3D10_SB_OPERAND_INDEX_REPRESENTATION(i, *operandToken);
1362+
if (indexRepesentation != D3D10_SB_OPERAND_INDEX_IMMEDIATE32)
1363+
return emitError(loc, "unsupported index representation: ")
1364+
<< indexRepesentation;
1365+
auto value = parseToken();
1366+
FAILURE_IF_FAILED(value);
1367+
indices.push_back(*value);
1368+
}
1369+
1370+
switch (indexDim) {
1371+
case D3D10_SB_OPERAND_INDEX_2D:
1372+
return builder.buildDclConstantBuffer(
1373+
/*id=*/indices[0], /*size=*/indices[1], /*lbound=*/std::nullopt,
1374+
/*ubound=*/std::nullopt, /*space=*/std::nullopt, *accessPattern, loc);
1375+
case D3D10_SB_OPERAND_INDEX_3D: {
1376+
auto sizeToken = parseToken();
1377+
FAILURE_IF_FAILED(sizeToken);
1378+
auto spaceToken = parseToken();
1379+
FAILURE_IF_FAILED(spaceToken);
1380+
return builder.buildDclConstantBuffer(
1381+
/*id=*/indices[0], /*size=*/*sizeToken, /*lbound=*/indices[1],
1382+
/*ubound=*/indices[2], /*space=*/*spaceToken, *accessPattern, loc);
1383+
}
1384+
default:
1385+
llvm_unreachable("indexDim was validated above");
1386+
}
1387+
}
1388+
13201389
OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
13211390
Instruction &out) {
13221391
FailureOr<Instruction> result;
@@ -1393,6 +1462,9 @@ class Parser {
13931462
case D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED:
13941463
result = parseDclTgsmStructured(loc);
13951464
break;
1465+
case D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER:
1466+
result = parseDclConstantBuffer(opcodeToken, loc);
1467+
break;
13961468
default:
13971469
return std::nullopt;
13981470
}
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>
44 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)