Skip to content

Commit 326e3ae

Browse files
committed
[mlir][dxsa] Add dcl_sampler instruction
Example: dxsa.dcl_sampler <id = 3, mode = default> dxsa.dcl_sampler <id = 0, mode = comparison, lbound = 0, ubound = 3, space = 1> Signed-off-by: Vladimir Shiryaev <tagolog@users.noreply.github.com>
1 parent cdfb680 commit 326e3ae

6 files changed

Lines changed: 114 additions & 0 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,27 @@ def DXSA_ConstantBufferAccessPatternAttr :
299299
let assemblyFormat = "`<` $value `>`";
300300
}
301301

302+
// `default` is a reserved C++ keyword, so the symbolic (C++ enum case) names
303+
// use PascalCase while the printable asm form stays lowercase.
304+
def DXSA_SamplerMode_Default : I32EnumAttrCase<"Default", 0, "default">;
305+
def DXSA_SamplerMode_Comparison : I32EnumAttrCase<"Comparison", 1, "comparison">;
306+
def DXSA_SamplerMode_Mono : I32EnumAttrCase<"Mono", 2, "mono">;
307+
308+
def DXSA_SamplerMode : I32EnumAttr<
309+
"SamplerMode", "sampler mode", [
310+
DXSA_SamplerMode_Default,
311+
DXSA_SamplerMode_Comparison,
312+
DXSA_SamplerMode_Mono
313+
]> {
314+
let cppNamespace = "::mlir::dxsa";
315+
let genSpecializedAttr = 0;
316+
}
317+
318+
def DXSA_SamplerModeAttr :
319+
EnumAttr<DXSADialect, DXSA_SamplerMode, "sampler_mode"> {
320+
let assemblyFormat = "$value";
321+
}
322+
302323
//===----------------------------------------------------------------------===//
303324
// DXSA attribute Constraints
304325
//===----------------------------------------------------------------------===//
@@ -969,4 +990,32 @@ def DXSA_DclConstantBuffer : DXSA_Op<"dcl_constant_buffer"> {
969990
let hasVerifier = 1;
970991
}
971992

993+
def DXSA_DclSampler : DXSA_Op<"dcl_sampler"> {
994+
let summary = "declares a sampler that will be referenced in the shader";
995+
let description = [{
996+
The `dxsa.dcl_sampler` operation declares sampler that will be referenced in the shader.
997+
998+
Examples:
999+
1000+
```mlir
1001+
dxsa.dcl_sampler <id = 3, mode = default>
1002+
dxsa.dcl_sampler <id = 0, mode = comparison, lbound = 0, ubound = 3, space = 1>
1003+
```
1004+
}];
1005+
1006+
let arguments = (ins
1007+
I32Attr:$id,
1008+
DXSA_SamplerModeAttr:$mode,
1009+
OptionalAttr<I32Attr>:$lbound,
1010+
OptionalAttr<I32Attr>:$ubound,
1011+
OptionalAttr<I32Attr>:$space);
1012+
let assemblyFormat = [{
1013+
` ` `<` `id` `=` $id `,` `mode` `=` $mode
1014+
(`,` `lbound` `=` $lbound^ `,` `ubound` `=` $ubound
1015+
`,` `space` `=` $space)?
1016+
`>` attr-dict
1017+
}];
1018+
let hasVerifier = 1;
1019+
}
1020+
9721021
#endif // DXSA_OPS

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ LogicalResult DclConstantBuffer::verify() {
9393
return success();
9494
}
9595

96+
LogicalResult DclSampler::verify() {
97+
auto lbound = getLbound();
98+
auto ubound = getUbound();
99+
if (lbound && ubound && *lbound > *ubound)
100+
return emitOpError("expected lbound <= ubound, got lbound=")
101+
<< *lbound << ", ubound=" << *ubound;
102+
return success();
103+
}
104+
96105
//===----------------------------------------------------------------------===//
97106
// TableGen'd attribute method definitions
98107
//===----------------------------------------------------------------------===//

mlir/lib/Target/DXSA/BinaryParser.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,18 @@ class DXBuilder {
704704
optionalToAttr(space), accessPattern);
705705
}
706706

707+
Instruction buildDclSampler(uint32_t id, std::optional<uint32_t> lbound,
708+
std::optional<uint32_t> ubound,
709+
std::optional<uint32_t> space,
710+
dxsa::SamplerMode mode, Location loc) {
711+
auto optionalToAttr = [&](std::optional<uint32_t> v) -> IntegerAttr {
712+
return v ? builder.getI32IntegerAttr(*v) : IntegerAttr();
713+
};
714+
return dxsa::DclSampler::create(
715+
builder, loc, id, mode, optionalToAttr(lbound), optionalToAttr(ubound),
716+
optionalToAttr(space));
717+
}
718+
707719
private:
708720
MLIRContext *context;
709721
ModuleOp module;
@@ -1386,6 +1398,35 @@ class Parser {
13861398
}
13871399
}
13881400

1401+
FailureOr<Instruction> parseDclSampler(uint32_t opcodeToken, Location loc) {
1402+
auto rawMode = DECODE_D3D10_SB_SAMPLER_MODE(opcodeToken);
1403+
auto mode = dxsa::symbolizeSamplerMode(rawMode);
1404+
if (!mode)
1405+
return emitError(loc, "unknown sampler mode: ") << rawMode;
1406+
1407+
auto operand = parseInlineOperand();
1408+
FAILURE_IF_FAILED(operand);
1409+
if (operand->getType() != dxsa::InlineOperandType::sampler)
1410+
return emitError(loc, "operand must be a sampler register, got ")
1411+
<< dxsa::stringifyInlineOperandType(operand->getType());
1412+
auto indexArray = operand->getIndex();
1413+
auto indexDim = indexArray ? indexArray.size() : 0;
1414+
if (indexDim != 1 && indexDim != 3)
1415+
return emitError(loc, "operand must have a 1D or 3D index, got ")
1416+
<< indexDim;
1417+
auto id = indexArray[0];
1418+
std::optional<uint32_t> lbound, ubound, space;
1419+
if (indexDim == 3) {
1420+
lbound = indexArray[1];
1421+
ubound = indexArray[2];
1422+
auto spaceToken = parseToken();
1423+
FAILURE_IF_FAILED(spaceToken);
1424+
space = *spaceToken;
1425+
}
1426+
1427+
return builder.buildDclSampler(id, lbound, ubound, space, *mode, loc);
1428+
}
1429+
13891430
OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
13901431
Instruction &out) {
13911432
FailureOr<Instruction> result;
@@ -1465,6 +1506,9 @@ class Parser {
14651506
case D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER:
14661507
result = parseDclConstantBuffer(opcodeToken, loc);
14671508
break;
1509+
case D3D10_SB_OPCODE_DCL_SAMPLER:
1510+
result = parseDclSampler(opcodeToken, loc);
1511+
break;
14681512
default:
14691513
return std::nullopt;
14701514
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: mlir-translate --import-dxsa-bin %S/inputs/dcl_sampler.bin | FileCheck %s
2+
3+
// CHECK: module {
4+
// CHECK-NEXT: dxsa.dcl_sampler <id = 0, mode = default>
5+
// CHECK-NEXT: dxsa.dcl_sampler <id = 1, mode = comparison>
6+
// CHECK-NEXT: dxsa.dcl_sampler <id = 2, mode = mono>
7+
// CHECK-NEXT: dxsa.dcl_sampler <id = 0, mode = default, lbound = 0, ubound = 3, space = 1>
8+
// 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_sampler <id = 0, mode = default, lbound = 5, ubound = 3, space = 1>
60 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)