Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
389 changes: 386 additions & 3 deletions mlir/include/mlir/Dialect/DXSA/IR/DXSAOps.td

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions mlir/lib/Dialect/DXSA/IR/DXSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,52 @@ void DXSADialect::initialize() {
#define GET_OP_CLASSES
#include "mlir/Dialect/DXSA/IR/DXSAOps.cpp.inc"

//===----------------------------------------------------------------------===//
// Op verifiers
//===----------------------------------------------------------------------===//

LogicalResult DclTgsmRaw::verify() {
auto byteCount = getByteCount();
if (byteCount % 4 != 0)
return emitOpError("byte count must be a multiple of 4, got ") << byteCount;
return success();
}

LogicalResult DclHsMaxTessfactor::verify() {
// Reject NaN, Inf, and anything outside [1.0, 64.0].
auto value = getMaxTessFactorAttr().getValue();
if (!value.isFinite() || value < llvm::APFloat(1.0f) ||
value > llvm::APFloat(64.0f))
return emitOpError("max tessellation factor must be in [1.0, 64.0], got ")
<< value.convertToFloat();
return success();
}

//===----------------------------------------------------------------------===//
// Attribute verifiers
//===----------------------------------------------------------------------===//

LogicalResult
InlineOperandAttr::verify(function_ref<InFlightDiagnostic()> emitError,
::mlir::dxsa::InlineOperandType /*type*/,
uint32_t components,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we verify that components can only be 0, 1, or 4?

::mlir::dxsa::ComponentMaskAttr mask,
::mlir::dxsa::ComponentSwizzleAttr swizzle,
::mlir::dxsa::ComponentNameAttr select,
::mlir::DenseI64ArrayAttr /*index*/) {
// mask / swizzle / select are the three mutually-exclusive 4-component
// selection modes. Operands with 0 or 1 components carry none of them.
auto set = (mask ? 1 : 0) + (swizzle ? 1 : 0) + (select ? 1 : 0);
if (set > 1)
return emitError() << "at most one of 'mask', 'swizzle', 'select' may "
"be set on an operand";
if (components != 4 && set != 0)
return emitError() << "component selection mode is only valid on a "
"4-component operand, got "
<< components << " components";
return success();
}

//===----------------------------------------------------------------------===//
// TableGen'd attribute method definitions
//===----------------------------------------------------------------------===//
Expand Down
266 changes: 262 additions & 4 deletions mlir/lib/Target/DXSA/BinaryParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mlir/IR/Location.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/bit.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLog.h"
#include "llvm/Support/Endian.h"
Expand All @@ -30,6 +31,10 @@ using UINT = unsigned int;
using namespace mlir;
using namespace llvm;

#define FAILURE_IF_FAILED(RES) \
if (failed(RES)) \
return failure();

enum OpcodeClass {
D3D10_SB_FLOAT_OP,
D3D10_SB_INT_OP,
Expand Down Expand Up @@ -400,11 +405,23 @@ struct OperandComponents {
};
};

static dxsa::ComponentMask decodeComponentMask(uint32_t rawComponentMask) {
auto componentMask = static_cast<dxsa::ComponentMask>(0);
if (rawComponentMask & D3D10_SB_OPERAND_4_COMPONENT_MASK_X)
componentMask |= dxsa::ComponentMask::x;
if (rawComponentMask & D3D10_SB_OPERAND_4_COMPONENT_MASK_Y)
componentMask |= dxsa::ComponentMask::y;
if (rawComponentMask & D3D10_SB_OPERAND_4_COMPONENT_MASK_Z)
componentMask |= dxsa::ComponentMask::z;
if (rawComponentMask & D3D10_SB_OPERAND_4_COMPONENT_MASK_W)
componentMask |= dxsa::ComponentMask::w;
return componentMask;
}

class DXBuilder {
public:
DXBuilder(MLIRContext *context, StringAttr name)
: context(context),
module(ModuleOp::create(builder, FileLineColLoc::get(name, 0, 0))),
DXBuilder(MLIRContext * /*context*/, StringAttr name)
: module(ModuleOp::create(builder, FileLineColLoc::get(name, 0, 0))),
builder(module.getRegion()) {}

using Index = mlir::Value;
Expand Down Expand Up @@ -597,8 +614,104 @@ class DXBuilder {
systemValueNameAttr);
}

dxsa::InlineOperandAttr
buildInlineOperandAttr(dxsa::InlineOperandType operandType,
const OperandComponents &comp,
ArrayRef<int64_t> indexArray) {
auto *ctx = builder.getContext();
dxsa::ComponentMaskAttr maskAttr;
dxsa::ComponentSwizzleAttr swizzleAttr;
dxsa::ComponentNameAttr selectAttr;
switch (comp.kind) {
case OperandComponentsKind::None:
break;
case OperandComponentsKind::Mask:
maskAttr =
dxsa::ComponentMaskAttr::get(ctx, decodeComponentMask(comp.mask));
break;
case OperandComponentsKind::Swizzle:
swizzleAttr = dxsa::ComponentSwizzleAttr::get(
ctx, static_cast<dxsa::ComponentName>(comp.swizzle[0]),
static_cast<dxsa::ComponentName>(comp.swizzle[1]),
static_cast<dxsa::ComponentName>(comp.swizzle[2]),
static_cast<dxsa::ComponentName>(comp.swizzle[3]));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use symbolize to verify range?

break;
case OperandComponentsKind::One:
selectAttr = dxsa::ComponentNameAttr::get(
ctx, static_cast<dxsa::ComponentName>(comp.one));
break;
}
auto indexAttr = indexArray.empty()
? DenseI64ArrayAttr()
: DenseI64ArrayAttr::get(ctx, indexArray);
return dxsa::InlineOperandAttr::get(ctx, operandType, comp.num, maskAttr,
swizzleAttr, selectAttr, indexAttr);
}

Instruction buildDclInput(dxsa::InlineOperandAttr operand, Location loc) {
return dxsa::DclInput::create(builder, loc, operand);
}

Instruction buildDclOutput(dxsa::InlineOperandAttr operand, Location loc) {
return dxsa::DclOutput::create(builder, loc, operand);
}

Instruction
buildDclConstantBuffer(dxsa::InlineOperandAttr operand,
dxsa::ConstantBufferAccessPattern accessPattern,
Location loc) {
auto accessPatternAttr = dxsa::ConstantBufferAccessPatternAttr::get(
builder.getContext(), accessPattern);
return dxsa::DclConstantBuffer::create(builder, loc, operand,
accessPatternAttr);
}

Instruction buildDclTgsmRaw(dxsa::InlineOperandAttr operand,
uint32_t byteCount, Location loc) {
return dxsa::DclTgsmRaw::create(builder, loc, operand,
builder.getI32IntegerAttr(byteCount));
}

Instruction buildDclIndexRange(dxsa::InlineOperandAttr operand,
uint32_t count, Location loc) {
return dxsa::DclIndexRange::create(builder, loc, operand,
builder.getI32IntegerAttr(count));
}

Instruction buildDclHsMaxTessfactor(float maxTessFactor, Location loc) {
return dxsa::DclHsMaxTessfactor::create(
builder, loc, builder.getF32FloatAttr(maxTessFactor));
}

Instruction buildDclHsForkPhaseInstanceCount(uint32_t count, Location loc) {
return dxsa::DclHsForkPhaseInstanceCount::create(
builder, loc, builder.getI32IntegerAttr(count));
}

Instruction buildDclHsJoinPhaseInstanceCount(uint32_t count, Location loc) {
return dxsa::DclHsJoinPhaseInstanceCount::create(
builder, loc, builder.getI32IntegerAttr(count));
}

Instruction buildDclOutputSiv(dxsa::InlineOperandAttr operand,
dxsa::SystemValueName systemValueName,
Location loc) {
auto systemValueNameAttr =
dxsa::SystemValueNameAttr::get(builder.getContext(), systemValueName);
return dxsa::DclOutputSiv::create(builder, loc, operand,
systemValueNameAttr);
}

Instruction buildDclOutputSgv(dxsa::InlineOperandAttr operand,
dxsa::SystemValueName systemValueName,
Location loc) {
auto systemValueNameAttr =
dxsa::SystemValueNameAttr::get(builder.getContext(), systemValueName);
return dxsa::DclOutputSgv::create(builder, loc, operand,
systemValueNameAttr);
}

private:
MLIRContext *context;
ModuleOp module;
OpBuilder builder;
};
Expand Down Expand Up @@ -1076,6 +1189,121 @@ class Parser {
return builder.buildDclInputPsSgv(*operand, *systemValueName, loc);
}

FailureOr<dxsa::InlineOperandAttr> parseInlineOperand() {
auto token = parseToken();
FAILURE_IF_FAILED(token);

auto rawOperandType = DECODE_D3D10_SB_OPERAND_TYPE(*token);
auto isExtended = DECODE_IS_D3D10_SB_OPERAND_EXTENDED(*token);
auto loc = getLocation();

if (isImmOperand(*token))
return emitError(loc, "immediate operand is not supported yet");

auto type = dxsa::symbolizeInlineOperandType(rawOperandType);
if (!type)
return emitError(loc, "unknown operand type: ") << rawOperandType;

auto components = parseOperandComponents(*token);
FAILURE_IF_FAILED(components);

auto indexTypes = parseOperandIndexTypes(*token);
FAILURE_IF_FAILED(indexTypes);

if (isExtended)
return emitError(loc, "extended operand tokens are not yet supported in "
"inline operand attribute");

SmallVector<int64_t, 3> indices;
for (auto indexType : *indexTypes) {
if (indexType != D3D10_SB_OPERAND_INDEX_IMMEDIATE32)
return emitError(getLocation(), "unsupported index representation: ")
<< indexType;
auto value = parseToken();
FAILURE_IF_FAILED(value);
indices.push_back(static_cast<int32_t>(*value));
}

return builder.buildInlineOperandAttr(*type, *components, indices);
}

FailureOr<Instruction> parseDclInput(Location loc) {
auto operand = parseInlineOperand();
FAILURE_IF_FAILED(operand);
return builder.buildDclInput(*operand, loc);
}

FailureOr<Instruction> parseDclOutput(Location loc) {
auto operand = parseInlineOperand();
FAILURE_IF_FAILED(operand);
return builder.buildDclOutput(*operand, loc);
}

FailureOr<Instruction> parseDclConstantBuffer(uint32_t opcodeToken,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Shader Model 5.1 and later, constant buffer opcode is followed by 3 operands: range, size and index.
Looks like we only handle Shader Model 4.0 through 5.0.

Location loc) {
auto rawAccessPattern =
DECODE_D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN(opcodeToken);
auto accessPattern =
dxsa::symbolizeConstantBufferAccessPattern(rawAccessPattern);
if (!accessPattern)
return emitError(loc, "unknown constant buffer access pattern: ")
<< rawAccessPattern;
auto operand = parseInlineOperand();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The operand for dcl_constantBuffer is fake - it can only be D3D10_SB_OPERAND_TYPE_CONSTANT_BUFFER, and we only need indices from it. The second index is also fake: it defines size of a buffer. Other properties of operands are meaningless - components, mask, swizzle. I don't think they are used for this instruction.

FAILURE_IF_FAILED(operand);
return builder.buildDclConstantBuffer(*operand, *accessPattern, loc);
}

FailureOr<Instruction> parseDclTgsmRaw(Location loc) {
auto operand = parseInlineOperand();
FAILURE_IF_FAILED(operand);
auto byteCount = parseToken();
FAILURE_IF_FAILED(byteCount);
return builder.buildDclTgsmRaw(*operand, *byteCount, loc);
}

FailureOr<Instruction> parseDclIndexRange(Location loc) {
auto operand = parseInlineOperand();
FAILURE_IF_FAILED(operand);
auto count = parseToken();
FAILURE_IF_FAILED(count);
return builder.buildDclIndexRange(*operand, *count, loc);
}

FailureOr<Instruction> parseDclHsMaxTessfactor(Location loc) {
auto token = parseToken();
FAILURE_IF_FAILED(token);
float maxTessFactor = llvm::bit_cast<float>(*token);
return builder.buildDclHsMaxTessfactor(maxTessFactor, loc);
}

FailureOr<Instruction> parseDclHsForkPhaseInstanceCount(Location loc) {
auto count = parseToken();
FAILURE_IF_FAILED(count);
return builder.buildDclHsForkPhaseInstanceCount(*count, loc);
}

FailureOr<Instruction> parseDclHsJoinPhaseInstanceCount(Location loc) {
auto count = parseToken();
FAILURE_IF_FAILED(count);
return builder.buildDclHsJoinPhaseInstanceCount(*count, loc);
}

FailureOr<Instruction> parseDclOutputSiv(Location loc) {
auto operand = parseInlineOperand();
FAILURE_IF_FAILED(operand);
auto name = parseSystemValueName(getLocation());
FAILURE_IF_FAILED(name);
return builder.buildDclOutputSiv(*operand, *name, loc);
}

FailureOr<Instruction> parseDclOutputSgv(Location loc) {
auto operand = parseInlineOperand();
FAILURE_IF_FAILED(operand);
auto name = parseSystemValueName(getLocation());
FAILURE_IF_FAILED(name);
return builder.buildDclOutputSgv(*operand, *name, loc);
}

OptionalParseResult parseDclInstruction(uint32_t opcodeToken, Location loc,
Instruction &out) {
FailureOr<Instruction> result;
Expand Down Expand Up @@ -1116,6 +1344,36 @@ class Parser {
case D3D10_SB_OPCODE_DCL_INPUT_PS_SGV:
result = parseDclInputPsSgv(loc);
break;
case D3D10_SB_OPCODE_DCL_INPUT:
result = parseDclInput(loc);
break;
case D3D10_SB_OPCODE_DCL_OUTPUT:
result = parseDclOutput(loc);
break;
case D3D10_SB_OPCODE_DCL_OUTPUT_SIV:
result = parseDclOutputSiv(loc);
break;
case D3D10_SB_OPCODE_DCL_OUTPUT_SGV:
result = parseDclOutputSgv(loc);
break;
case D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER:
result = parseDclConstantBuffer(opcodeToken, loc);
break;
case D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_RAW:
result = parseDclTgsmRaw(loc);
break;
case D3D10_SB_OPCODE_DCL_INDEX_RANGE:
result = parseDclIndexRange(loc);
break;
case D3D11_SB_OPCODE_DCL_HS_MAX_TESSFACTOR:
result = parseDclHsMaxTessfactor(loc);
break;
case D3D11_SB_OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT:
result = parseDclHsForkPhaseInstanceCount(loc);
break;
case D3D11_SB_OPCODE_DCL_HS_JOIN_PHASE_INSTANCE_COUNT:
result = parseDclHsJoinPhaseInstanceCount(loc);
break;
default:
return std::nullopt;
}
Expand Down
6 changes: 6 additions & 0 deletions mlir/test/Target/DXSA/dcl_constant_buffer.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: mlir-translate --import-dxsa-bin %S/inputs/dcl_constant_buffer.bin | FileCheck %s

// CHECK: module {
// CHECK-NEXT: dxsa.dcl_constant_buffer <type = constant_buffer, components = 4, swizzle = <x, y, z, w>, index = [0, 1]>, <immediateIndexed>
// CHECK-NEXT: dxsa.dcl_constant_buffer <type = constant_buffer, components = 4, swizzle = <x, y, z, w>, index = [3, 128]>, <dynamicIndexed>
// CHECK-NEXT: }
5 changes: 5 additions & 0 deletions mlir/test/Target/DXSA/dcl_hs_fork_phase_instance_count.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: mlir-translate --import-dxsa-bin %S/inputs/dcl_hs_fork_phase_instance_count.bin | FileCheck %s

// CHECK: module {
// CHECK-NEXT: dxsa.dcl_hs_fork_phase_instance_count 4
// CHECK-NEXT: }
5 changes: 5 additions & 0 deletions mlir/test/Target/DXSA/dcl_hs_join_phase_instance_count.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: mlir-translate --import-dxsa-bin %S/inputs/dcl_hs_join_phase_instance_count.bin | FileCheck %s

// CHECK: module {
// CHECK-NEXT: dxsa.dcl_hs_join_phase_instance_count 2
// CHECK-NEXT: }
5 changes: 5 additions & 0 deletions mlir/test/Target/DXSA/dcl_hs_max_tessfactor.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: mlir-translate --import-dxsa-bin %S/inputs/dcl_hs_max_tessfactor.bin | FileCheck %s

// CHECK: module {
// CHECK-NEXT: dxsa.dcl_hs_max_tessfactor 1.550000e+01
// CHECK-NEXT: }
Loading