Skip to content

[mlir][dxsa] Add AsmPrinter to translate from MLIR to DXSA text#130

Open
asavonic wants to merge 6 commits into
dxsa-mlir-writerfrom
dxsa-mlir-printer
Open

[mlir][dxsa] Add AsmPrinter to translate from MLIR to DXSA text#130
asavonic wants to merge 6 commits into
dxsa-mlir-writerfrom
dxsa-mlir-printer

Conversation

@asavonic
Copy link
Copy Markdown
Contributor

@asavonic asavonic commented May 8, 2026

AsmPrinter traverses all instructions in an module and prints them in "canonical" assembly syntax. There is no description of its grammar, so the patch follows examples from "Direct3D 11.3 Functional Specification" and output and tests from existing DirectX compiler tools, such as DXC.

Only standard instructions and some basic operand types are supported for now.

AsmPrinter traverses all instructions in an module and prints them in
"canonical" assembly syntax. There is no description of its grammar,
so the patch follows examples from "Direct3D 11.3 Functional
Specification" and output and tests from existing DirectX compiler
tools, such as DXC.

Only standard instructions and some basic operand types are supported
for now.
@asavonic asavonic requested review from asl and tagolog May 8, 2026 15:21
Comment thread mlir/lib/Target/DXSA/AsmPrinter.cpp Outdated

LogicalResult emitModule(ModuleOp source) {
Region &region = source.getRegion();
if (!region.hasOneBlock()) {
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 really need this check here? It seems to be something for verifier to enforce.

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.

@asavonic Was this comment forgotten?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. Can you suggest how to add a verifier? ModuleOp is a builtin operand. Do we need to make a dialect-specific operand?

Copy link
Copy Markdown
Contributor

@asl asl May 12, 2026

Choose a reason for hiding this comment

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

Well, then this is even more redundant :)

ModuleOp is single-region single-bb, this is already enforced: https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/IR/BuiltinOps.td#L58

Use source.getBody() to get that BB.

Overall, think about IR invariants. Enforcing them in asmprinter is a bad idea. Overall, you should never have emitError here unless you have not supported something yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, that is much better, thank you!
Now we only use emitError for unimplemented features or as a Default case in TypeSwitch for unsupported (new?) operations.

Comment thread mlir/lib/Target/DXSA/AsmPrinter.cpp Outdated
assert(index && "undefined index");

// Non-immediate indices always use subscript syntax.
if (!isa<dxsa::IndexImm>(*index)) {
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.

Maybe remove extra { per code style guide to reduce number of lines?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread mlir/lib/Target/DXSA/AsmPrinter.cpp Outdated
}

StringRef separator = "";
for (APInt v : attr) {
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.

no need to copy apint here, grab references.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Missed that, thank you!

auto attr = cast<DenseIntElementsAttr>(op.getImm());
auto elementType = cast<IntegerType>(attr.getType().getElementType());

if (elementType.getWidth() != 32)
Copy link
Copy Markdown
Contributor

@asl asl May 11, 2026

Choose a reason for hiding this comment

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

Again, this is something verifier should enforce. This is an IR invariant. You should not check it here.

Copy link
Copy Markdown
Contributor Author

@asavonic asavonic May 12, 2026

Choose a reason for hiding this comment

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

Sorry, that should be a FIXME. I don't know how to print 64-bit immediate operands yet, but they are supported in Shader Assembly.

Comment thread mlir/lib/Target/DXSA/AsmPrinter.cpp Outdated
outs << "l(";

StringRef separator = "";
for (const APInt &v : attr) {
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread mlir/lib/Target/DXSA/AsmPrinter.cpp Outdated
.Case<dxsa::IndexImm>(
[this](auto index) { return emitIndexImm(index); })
.Case<dxsa::IndexRel>(
[this](auto index) -> LogicalResult { return emitIndexRel(index); })
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.

Why do you need trailing result types here, but not in the first case? Do you need them at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! These are leftovers from an early iteration where return type was different. Removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants