Skip to content

Experimental: direct RDNA2 → SPIR-V shader emitter (not wired in) - #87

Closed
pablogonz12 wants to merge 2 commits into
mainfrom
spike/spirv-emit
Closed

pablogonz12 wants to merge 2 commits into
mainfrom
spike/spirv-emit

Conversation

@pablogonz12

Copy link
Copy Markdown
Owner

What

An experimental direct RDNA2 ISA → SPIR-V emitter for victory-shader: our own code from the
decoded ISA to a SPIR-V module (a hand-written SPIR-V binary writer), with no DXIL and no
dxil-spirv in between.

This is a spike branch. Nothing calls it at runtime: no pipeline path, no translation entry
point that runs by default; only the crate's own spike example binaries and the new tests touch it.
It is here to answer one question with numbers: can our decoder/IR produce valid SPIR-V that Mesa
accepts, and how much of the dump does that cover?

Coverage (offline: emit → spirv-val --target-env vulkan1.3 → real vkCreateComputePipelines on RADV)

round compute programs emitted, valid, pipelined what moved it
baseline 93 core: SOP/VOP, LDS, raw buffers, compares
images 167 image_sample*/image_load/image_store/image_gather*
source modifiers 200 SDWA, opsel/opsel_hi, DPP, VOP3 neg/abs, v_fma_mix
long tail (this PR) 223 s_add*/s_sub* with SCC, s_*_b64 shifts, s_bcnt1_i32_b64, s_ff1/s_flbit, v_med3

223 of 442 compute programs (50 %), 0 validation failures, 0 pipeline failures, 0 hangs.
The remaining 219 stop on a named gap each (compare kinds 41, s_getpc_b64 30, v_permlane* 27,
v_perm_b32 27, LDS shuffles 25, …), not on a failure of the approach.

The DXIL path on the same dump is unchanged: 707 translated of 751 (measured with the same
offline runner before and after).

How it works, in short

  • Every register dword is a function-local OpVariable; each machine instruction is
    load → op → store. No phis, no dominance, no structuriser: the whole CFG is one
    OpLoopMerge + OpSwitch dispatcher on a pc variable, which is valid structured SPIR-V for
    any program. Deliberately bad code quality; this measures coverage, not speed.
  • Memory is the console's own address space: s_load_*, buffer_*, global_*, flat_* become
    OpLoad/OpStore through 64-bit addresses the program computes itself
    (PhysicalStorageBuffer + BDA); root arguments arrive as push constants. No descriptor heaps.
  • Images are the one exception: one UniformConstant binding per static image site
    (set 0, binding = site index), OpTypeImage with ImageFormat Unknown for sampled and
    StorageImage*WithoutFormat for storage, the sampler as the binding after the images, joined with
    OpSampledImage. The layout follows from the module's decorations alone; nothing reads the console
    descriptor words.

Approximations (deliberate, and listed so nobody mistakes this for a working shader path)

  • EXEC is a scalar all-ones/all-zeros mask, not per lane: a program whose lanes diverge inside a
    wave emits valid modules that compute wrong values.
  • DPP is emitted as an identity permutation (the source's own lane), not the real cross-lane one.
  • image_gather4: the level is dropped (SPIR-V's gather takes no Lod operand).
  • The _b sample bias is ignored rather than added to the level.
  • Sampled component type is f32 for every image; MSAA dimensions are emitted as their non-MSAA forms.
  • v_readlane/v_writelane/v_readfirstlane read this lane; s_wqm and v_mbcnt are approximated;
    s_setreg_imm32_b32 (rounding/denorm mode) is a no-op.
  • Nothing is diffed numerically against the DXIL path yet.

Tests

cargo test -p victory-shader → emit::spirv_tests: each family above is built from
hand-assembled GFX10 instruction words only (no game data, nothing derived from the dump), plus a
generic check that no operand of an arithmetic op has a different type than its result — the class of
the bug this round fixed (an i1 SCC reaching OpBitwiseAnd, which spirv-val rejects with
"operand index 2 expects int") — and spirv-val on every emitted module when the tool is installed.

cargo clippy -p victory-shader --all-targets is clean, cargo xtask policy passes.

Next

Main's shader core is being replaced by the fork line's in a unification PR, so the emitter will be
ported onto that core next rather than developed against code that is on its way out. After the
port, by expected coverage: compare kinds, lane permutes and LDS shuffles, the s_getpc_b64 constant
table, typed buffer formats, then per-lane EXEC and numeric diffs against the DXIL output as the
acceptance test.

No .spv files and no dumps are committed: the tests use hand-built words, and the numbers above are
counts from a local run.

pablogonz12 and others added 2 commits November 25, 2026 16:55
First end-to-end direct path for compute: the decoder and SSA IR feed a
hand-written SPIR-V writer, every register dword is a function variable and
the CFG is one dispatcher loop, so no structuriser or SSA plumbing is needed
for a valid module. Memory is the console's own address space through
physical_storage_buffer (buffer device address): the module has no
descriptors or heaps at all, root arguments are push constants.

Offline, on the 442 compute programs of the dump: 94 translate, 93 pass
spirv-val and create a real RADV compute pipeline (0 pipeline failures).
Gaps and the estimate to parity: xcl-bin/notes/spirv-spike.md (local).
The carry-in of s_addc_u32/s_subb_u32 is SCC, an i1 in the module: widen
it with OpSelect before it is added, which is what spirv-val refused
("OpBitwiseAnd operand index 2 expects int") on compute-7bb5b1b91d283a28.

Tests for the newer families, built from hand-assembled GFX10 words only
(crates/victory-shader/src/emit/spirv_tests.rs): the SCC carry chain,
64-bit shifts, both-halves popcount, first/last bit, v_med3, VOP3
neg/abs, SDWA, DPP and one image sample, plus a generic check that no
operand of an arithmetic op is another type than its result (the class of
the bug above) and spirv-val when the tool is installed.

spirv-check.sh: 223 of 442 compute programs emit, validate and create a
RADV compute pipeline, 0 validation and 0 pipeline failures (93 before
images, 167 after, 200 after source modifiers). DXIL path on the same
dump: 707 translated, unchanged.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@pablogonz12

Copy link
Copy Markdown
Owner Author

Superseded by #120, which is this branch's emitter rebuilt on the unified core (main as of #88) —
same design, same approximations list, and better coverage on the same dump: 232 of 442 compute
programs emit, pass spirv-val --target-env vulkan1.3 and create a real RADV compute pipeline,
0 validation and 0 pipeline failures
(223/442 here). The DXIL path is byte-identical on all 751
programs (offline-check.sh: 751/751, 0 failures).

Closing this one rather than rebasing it: the port needed the old branch's IR/API assumptions fixed
in four places (spill::build instead of ir::build_with, the Reg::Spill → parked-scalar slot
mapping, the s_cmpk_*/v_min3_u32/v_ffbl_b32 tail the new IR names differently, and the
lower.rs conflict), all of which #120 carries with cherry-pick -x and an authorship-preserving
fix-up commit. The branch spike/spirv-emit is kept, as is its spike note; #120 is a draft and
nothing calls the emitter at runtime.

@pablogonz12

Copy link
Copy Markdown
Owner Author

Superseded by #120 (same emitter on the unified core, 232/442 compute, DXIL unchanged); branch spike/spirv-emit kept.

@pablogonz12

Copy link
Copy Markdown
Owner Author

Restack note (25 Nov): this draft is still on the old shader core. Merging main (#88, #99) today conflicts in emit/mod.rs, and after a trial resolution the spike's test scaffolding no longer compiles against the unified core: 22 errors, all from old test helpers and the old emitter API (ir::build_with, super::tests::{compute, emit_for}, emit::Target, valu_tests::one_at_a_time, host::adapters). That is a port of the emitter and its tests, not a restack, so it is skipped here and stays on its base. It gets ported onto main later.

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.

1 participant