Skip to content

Recompiler: v_fma_f32 is not fused on AMD's RDNA1 driver (mad() left unfused) #472

Backlog

Description

@Abdellah-Khanani

On AMD's RDNA1 Windows driver, the fused FP32 ops are translated to mad(), which DXIL lets the driver evaluate unfused, and this driver does. So v_fma_f32 (and presumably the rest of the fused family) rounds twice where the console rounds once. gpu::recomp::tests::gpu_nonfused_mad_and_mac_prevent_fma_contraction fails here on its fused control:

assertion `left == right` failed: v_fma_f32 overflow control should stay fused
  left: 2139095040   (0x7F800000, +inf: unfused)
 right: 1946157055   (0x73FFFFFF: fused)

The non-fused half of the test passes: mad_nf with precise works as #73 intended. Only the fused side depends on the driver.

Where

  • crates/xcl/src/gpu/recomp/emit.rs: "v_fma_f32" => fr(format!("mad({}, {}, {})", ...)), and the same mad(...) for v_fmac_f32, v_fmamk_f32, v_fmaak_f32, v_fma_f16, v_fmac_f16, v_pk_fma_f16 and the others. HLSL has no fused FP32 intrinsic, so mad() is fused only if the driver chooses to fuse it.

Measured

  • RX 5700 XT (RDNA1), driver 32.0.21045.29003, Windows 11 22631, main 2997c9a. This is the one failing test of cargo test --workspace --no-fail-fast -- --test-threads=4 on this adapter (675 passed, 1 failed, 41 ignored, with D3D12.X: map a one-slice title texture's packed mips where D3D12 addresses them #470 applied; the same failure on main).
  • I tried one option locally. float fma_f32(float a, float b, float c) { return (float)fma((double)a, (double)b, (double)c); } used for v_fma_f32 only makes the test pass, and all 182 gpu::recomp tests pass with it. I'm not proposing it as is:
    • fp64 is required: every shader with v_fma_f32 then needs double-precision shader ops. Some GPUs report none (Intel Arc), and their pipelines would fail.
    • Cost: RDNA1/2 consumer parts run fp64 at 1/16 rate, and v_fma_f32 is one of the most common instructions.
    • Not exact: the product is exact in double, but the double result is then rounded again to float. In rare cases that double rounding still differs from a true fp32 FMA.
    • Scope: it covers v_fma_f32 only, not the rest of the fused family.

Options I can see (not tested beyond the above)

  1. Probe at device creation whether mad fuses (this test's two vectors in one dispatch). Only on a driver that does not fuse, and only with fp64, use a fused path (double, or an exact FP32 emulation). Make that part of the translation cache key.
  2. Leave mad() and mark the test as SKIPPED on a driver that does not fuse it (the console behaviour is then not reproduced there).

Which one fits XCL is your call; I can test whichever you prefer on this RDNA1 card.

Found with Claude Code (Claude Opus 5.5), which also wrote this report; I ran the tests on my machine.

Activity

  1. PHeitor1 commented on Nov 24, 2026

    @PHeitor1

    NVIDIA data point: gpu::recomp::tests::gpu_nonfused_mad_and_mac_prevent_fma_contraction passes, fused control included, on an RTX 5060 Ti 8 GB (Blackwell), NVIDIA 621.24, Windows 11 Pro 26300, debug. It passed in the full-suite runs of 12 PRs, each merged onto main 3516080 (5) or 2997c9a (7). (Edited: the first version also claimed runs of those main commits by themselves, which I did not make.) So this driver fuses mad(), and a device-creation probe (option 1) would take the existing path here.

    Written with Claude Code (Claude Opus 5.5).

  2. Tjabooo commented on Nov 24, 2026

    @Tjabooo
    Contributor

    Another adapter where the fused control fails, and how it happens on Linux: RADV through vkd3d-proton, AMD RX 9070 XT (RDNA4, GFX1201), Mesa 26.2.6, Wine 11.21, vkd3d-proton v3.1 (with #257's patch), main dd2ae9a cross-built for x86_64-pc-windows-gnu, DXC 2026_09_29.

    assertion `left == right` failed: v_fma_f32 overflow control should stay fused
      left: 2139095040
     right: 1946157055
    

    It's the same failure as on the RDNA1 Windows driver. This one is the only failure of cargo test -p xcl --lib there once #530 is applied (649 passed, 1 failed, 0 GPU tests skipped).

    Why RADV runs it unfused. vkd3d-proton turns DXIL into SPIR-V with dxil-spirv, and emit_fmad_instruction (opcodes/opcodes_dxil_builtins.cpp, dxil-spirv 8b48f0e0, the one in vkd3d-proton v3.1) decides it:

    • a plain dx.op.fmad becomes GLSL.std.450 Fma, which RADV runs fused;
    • an fmad that carries dx.precise metadata (or force_precise), unless vkd3d-proton's precise_fma quirk is set, is split on purpose into OpFMul + OpFAdd, both NoContraction, so it is unfused. The comment there says Big Navi splits a no-contract fma into mul + add anyway.

    So on this stack the fused mad() only stays fused if DXC doesn't mark it precise. emit.rs doesn't mark it itself, but precise in HLSL also applies to every operation that feeds a precise value. If the test's fused v_fma_f32 result reaches a mad_nf (precise float p = a * b; precise float r = p + c;) or another precise value, DXC would put dx.precise on the fmad too. That's a hypothesis: I haven't looked at the DXIL of the test shader. If it holds, keeping the fused result out of precise data flow would fix RADV without fp64, and a disassembly of that one shader would show whether its fmad has !dx.precise.

    I'm happy to run any candidate fix on this RADV / RDNA4 setup. No game files used. AI-assisted (Claude); the results above are from runs on this machine.

  3. Tjabooo commented on Nov 24, 2026

    @Tjabooo
    Contributor

    Correction to my comment above: the hypothesis is wrong. I compiled the test's HLSL (from gpu_nonfused_mad_and_mac_prevent_fma_contraction at main dd2ae9a) with DXC 2026_09_29 (-T cs_6_6 -E main -HV 2021 -Fc). The fused control v16 = mad(v0, v1, v2) is one plain FMad, with no !dx.precise:

    %67 = call float @dx.op.tertiary.f32(i32 46, float %40, float %39, float %53)  ; FMad(a,b,c)
    

    The mad_nf lines are separate fmul / fadd without fast-math flags, as intended, and nothing spreads to the FMad. vkd3d-proton v3.1 doesn't set dxil-spirv's force_precise either (PRECISE_FMA is only a per-application quirk in device_workarounds.c). So dxil-spirv should emit GLSL.std.450 Fma for it, and the double rounding happens below that, in RADV / ACO, not in XCL's HLSL or DXC. I'll check the SPIR-V and the RADV side next. Sorry for the noise.

  4. Tjabooo commented on Nov 24, 2026

    @Tjabooo
    Contributor

    Found the RADV side: the SPIR-V vkd3d-proton gives RADV has a real Fma, but RADV's NIR folds the fma's v0 * v1 into the identical exact products of the test's mad_nf lines in the same program, so it is rounded twice. A lone v_fma_f32 is fused on RADV (both lanes pass). Details and a test-only fix (the control runs as its own program) in #676. Whether this is also what happens on the RDNA1 Windows driver is open: that needs a run of #676 there.

  5. joeypercia commented on Nov 24, 2026

    @joeypercia
    Contributor

    Data point for the open question of whether the RDNA1 Windows failure is the driver version: on an RDNA2 iGPU (Ryzen 7 7800X3D, AMD Radeon(TM) Graphics) with the same Windows driver, 32.0.21045.29003, gpu::recomp::tests::gpu_nonfused_mad_and_mac_prevent_fma_contraction passes, fused control included, in three full-suite runs today (main cb868b2, and dd2ae9a with #672 applied), not skipped. Windows 11 Pro 26200, DXC 1.8.2502.11, Rust 1.100.0 MSVC, debug, XCL_ADAPTER=radeon. It passes on an RTX 4070 Ti (616.56) in the same runs too.

    So with this driver release the combined program is fused on RDNA2 and not on RDNA1, which points at the RDNA1 compiler path rather than the release, consistent with @Tjabooo's contraction finding on RADV. I haven't run #676 here; on this adapter its control would pass either way.

    Run and report by Claude Code (Claude Opus 5.5).

  6. LAEGGYMODZ commented on Nov 24, 2026

    @LAEGGYMODZ

    Measurements on WARP for this issue. They confirm and extend what Marvisak/VIctory#284 reported: its description and two comments show WARP returning the unfused result for two vectors through an FMad-based lowering.

    Configuration — the conclusions below apply only to this:

    • Microsoft Basic Render Driver (WARP, 1414:008C); d3d10warp.dll and d3d12.dll 10.0.26100.9818.
    • Windows 11 25H2, build 26200.9974; Intel Core i7-12700K.
    • DXC 1.9.2609 (dxc.exe SHA-256 aa7965b3763d9c710baaa5a28cedcc633449962a75d1391d161a85304a41b3e2), with -T cs_6_0 -E main -HV 2021 -O3 -WX.

    Kernels: four standalone kernels, nothing else in each program, numthreads(64,1,1), raw f32 bits in (t0..t2) and out (u0).

    HLSL DXIL source SHA-256 CSO SHA-256
    P1 mad(a, b, c) one FMad 687a78242b67c13fee41ed3540506697b03e5311e358988aab96f33da32013ad 4dc6393a83eda93df5a31e7e496c131637f1027ca9d89ebec13372191836e958
    P2 mad(a,b,c) and precise p = a*b; precise r = p + c FMad + plain fmul/fadd 75bc62dbb961994bf0d6c744f08ce8faf9c9c5f9831f3e5259d65c95b58faee9 03c5d797c88fa0cc24ee34cd48e056d3a9aac5ac1884cb7f61cb22c076b6bf80
    P3 precise p = a*b; precise r = p + c plain fmul/fadd 5ab8d699a47da893132eaf2c76215e799abc699fb5206cb59e711ea4e8ac4d35 8e2bebb656f8b04ed18dd5b6e83d36f5785825add3b28373c78e39de49ab9274
    P4 a*b + c (no precise) fmul fast/fadd fast 6db05ec7fb713bab4d523ed0714187b5d97dcafeac10f4c679a08237925eff9c 1cc60e3113f9b0411fcdc149f06f89e32dc7f39f4602a3f84dca14121bc44f02

    Vectors: 8 batches × 4,096 = 32,768 per kernel. In every batch:

    • index 0 is a control: a = 18631·2^60, b = 1801·2^43, c = −2^104 (fused 0x7F7FFFFE, unfused 0x7F800000);
    • index 1 is this issue's control, 0x7F7FFFFF, 0x3F800001, 0xFF7FFFFF (fused 0x73FFFFFF, unfused 0x7F800000).

    That makes 16 control instances plus 32,752 generated vectors. Every generated triple is kept only if the fused result (one rounding) and the unfused result (two roundings) differ in bits and are both finite. Each output was compared bit for bit with both. The fused reference is Rust's f32::mul_add.

    Results

    Kernel equals fused equals unfused neither
    P1 mad 0 32,768 0
    P2 mad / precise twin 0 / 0 32,768 / 32,768 0
    P3 precise 0 32,768 0
    P4 plain 0 32,768 0

    On this issue's control, all four kernels return 0x7F800000 (the RX 5700 XT's value). Without the 16 controls: 32,752 / 32,752 generated vectors match the unfused result in each kernel.

    What this suggests for option 1 (an inference; I have not built or run XCL):

    • On this WARP build, a device-creation fusion probe would classify the adapter as non-fusing, so a fallback path could be exercised on the no-GPU CI runner or with XCL_ADAPTER=warp.
    • For the same reason I'd expect the fused control of gpu_nonfused_mad_and_mac_prevent_fma_contraction to fail under XCL_ADAPTER=warp.
    • Plain a*b + c is not contracted here either, so on WARP you can't see the precise in mad_nf mattering (Floating-point modes: the non-fused MAD/MAC family needs precise, and the s_round_mode NOOPs are not free #73); that needs a fusing driver.
    • No other WARP build, DXC version or driver was measured.
    P1 kernel
    RWByteAddressBuffer Out : register(u0);
    ByteAddressBuffer A : register(t0);
    ByteAddressBuffer B : register(t1);
    ByteAddressBuffer C : register(t2);
    [RootSignature("RootFlags(0), UAV(u0), SRV(t0), SRV(t1), SRV(t2)")]
    [numthreads(64, 1, 1)]
    void main(uint3 id : SV_DispatchThreadID)
    {
        uint o = id.x * 4;
        float a = asfloat(A.Load(o)), b = asfloat(B.Load(o)), c = asfloat(C.Load(o));
        Out.Store(o, asuint(mad(a, b, c)));
    }
    Vector generator (Python 3, standard library, exact arithmetic) — regenerates the 24 input buffers byte for byte
    # python fma_probe_vectors.py <batch 0..7> <out_prefix>  -> <prefix>.in0.bin / .in1.bin / .in2.bin (LE u32)
    import struct, sys
    from fractions import Fraction
    
    def dec(u):
        s, e, f = u >> 31, (u >> 23) & 0xFF, u & 0x7FFFFF
        v = Fraction(f, 1 << 149) if e == 0 else Fraction(f | 0x800000) * Fraction(2) ** (e - 150)
        return -v if s else v
    
    def rne(x):
        if x == 0: return 0
        s = 1 if x < 0 else 0; m = -x if s else x
        e = m.numerator.bit_length() - m.denominator.bit_length()
        if Fraction(2) ** e > m: e -= 1
        ulp = Fraction(2) ** (max(e, -126) - 23); q = m / ulp; n = q.numerator // q.denominator; r = q - n
        if r > Fraction(1, 2) or (r == Fraction(1, 2) and n % 2): n += 1
        v = n * ulp
        if v >= Fraction(2) ** 128: return (s << 31) | 0x7F800000
        if v < Fraction(2) ** -126: return (s << 31) | int(v / Fraction(1, 1 << 149))
        e = v.numerator.bit_length() - v.denominator.bit_length()
        if Fraction(2) ** e > v: e -= 1
        return (s << 31) | ((e + 127) << 23) | (int(v / Fraction(2) ** (e - 23)) - (1 << 23))
    
    finite = lambda u: (u >> 23) & 0xFF != 0xFF
    
    class XorShift32:
        def __init__(self, seed): self.x = seed or 0x9E3779B9
        def next(self):
            x = self.x; x ^= (x << 13) & 0xFFFFFFFF; x ^= x >> 17; x ^= (x << 5) & 0xFFFFFFFF
            self.x = x; return x
    
    def normal(r, span):
        e = r.next() % (2 * span + 1) - span; m = r.next() & 0x7FFFFF; s = r.next() & 0x80000000
        return s | ((e + 127) << 23) | m
    
    def vectors(batch, n=4096):
        r = XorShift32(0x6C1AF000 + batch); a, b, c = [], [], []
        x0, y0, z0 = rne(Fraction(18631) * 2 ** 60), rne(Fraction(1801) * 2 ** 43), rne(-Fraction(2) ** 104)
        p0 = rne(dec(x0) * dec(y0)); f0 = rne(dec(x0) * dec(y0) + dec(z0))
        u0 = rne(dec(p0) + dec(z0)) if finite(p0) else 0x7F800000
        if finite(f0) and f0 != u0: a.append(x0); b.append(y0); c.append(z0)
        a.append(0x7F7FFFFF); b.append(0x3F800001); c.append(0xFF7FFFFF)
        while len(a) < n:
            x, y = normal(r, 20), normal(r, 20); p = rne(dec(x) * dec(y)); k = r.next() % 3
            if k == 0: z = p ^ 0x80000000
            elif k == 1: z = rne(dec(p ^ 0x80000000) * dec(rne(1 + Fraction(r.next() % 8, 1 << 23))))
            else: z = normal(r, 20)
            fu, un = rne(dec(x) * dec(y) + dec(z)), rne(dec(p) + dec(z))
            if finite(fu) and finite(un) and fu != un: a.append(x); b.append(y); c.append(z)
        return a[:n], b[:n], c[:n]
    
    batch, prefix = int(sys.argv[1]), sys.argv[2]
    for i, v in enumerate(vectors(batch)):
        open("%s.in%d.bin" % (prefix, i), "wb").write(struct.pack("<%dI" % len(v), *v))
    SHA-256 of the 24 input buffers (batch.in0 = a, in1 = b, in2 = c)
    b0.in0 0182c87a2d019c251fcdab38fe83b5e7cd5bcda829589b2867e4bdfbadb50fe5
    b0.in1 4c20ef405b60f77c5a50a03c4550b75e1541fd860dd930c875f7737ebf7411e0
    b0.in2 ebdc89a8396fb3b7ebb86ae08957ac1a51bc1d0ffd9a1cf5c0637627c7d7854c
    b1.in0 6c1d9daf713aa6d6d5d1f4e9037a60c7b2cc6d2f5d28cbc68801f86cd17a0283
    b1.in1 028eb1b4ea7dd86644aa2bbeb1a535dc1387351973019bc1a4668b785525d801
    b1.in2 4cf2f4cfdd547aebb621b948e664c73b98257f13214a5bdfcf9a5a464fb6a87d
    b2.in0 3e02220ca8ebf946c93853aff39081a0a40e0f4605c53cf1f324cac6beda5681
    b2.in1 d4c7da4e9be0035442a5f84849658aaa31f92e4d8bcba428598e84396d55a2d2
    b2.in2 5c1ba78d1f8830a7ee5a230a5a3a2ddb3b3989a08b1f668dd9613cdd8250204d
    b3.in0 b7e9c32111c6b509a07cf38f686e466eca5eddea1d8f2854244af29848064d3d
    b3.in1 77dc28fd4d107cfc31e8e75704bb62b6bdeb0df7e3fff653c6d76c48d593496a
    b3.in2 72d56457a12373eb5c35f0bf6337769c3915ce48fe5fd6ed8bfad915b419a68a
    b4.in0 31a6b5ebe7ef2388a85ce9249220210cbcae3fe3a00264d3ddbbe39a8c8c3ce6
    b4.in1 10492953ceeb9d1757376f0f1b64feba1a5e2e05b95d51bf18fb4671a9cc4141
    b4.in2 9a32a90896c25a629a3c67d2b50ee455dc38dd5b40b60a07cedaa9d72934d295
    b5.in0 fdd8d307cab84932ce573a6ab4a4aabbf971470305610c2b29ad1f8c870741f7
    b5.in1 b69010763eab3ab82aebe25d52d2e76108a8ab1d9ad2179eb388a49d71388d72
    b5.in2 970b8a878840d5f6ea8aa39b12a95733f33740ca71e34ca225833b5154f75f7d
    b6.in0 e5706c16a396ad61bb3077a51de6f2f788b7f4631a2cbc554f9ab824212ca961
    b6.in1 bee28ad5f81885eab2b9c3766ff9ada0f996bb08585948f29c5b2e73f77e7257
    b6.in2 80d8e8a86e7dbeedc963a2cdb2d4da82972c1d462e7056fdcc7c7f2024bf7a4c
    b7.in0 f43dc8c661dfe580cfd381717e3de736dd932cb8c41d243742f47a996011bd59
    b7.in1 5aa001407925da39806ad90fde54b9a03f054718a65542917d32d73dbd39159b
    b7.in2 a3dc7b5b257f59c1d51ac587f4515c6c0471058d758ddc08508d2c36c735398f
    

    The test harness, kernels, generator, measurements and this text were produced with Claude Code (Claude Opus 5.5) on my machine, under my direction. I reviewed the comment before posting.

  7. Diskuss commented on Nov 24, 2026

    @Diskuss

    Native RDNA4 Windows data point: the fused control passes here. AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32015.7029; Windows 11 Pro 26300; DXC 1.9.2602.17; Rust 1.100.0 MSVC; debug; main c160d86ef2efe3a07e4dfb473e4fa72eafb413c0.

    Results

    • cargo test -p xcl --lib gpu_nonfused_mad_and_mac_prevent_fma_contraction -- --test-threads=1: 6/6 passed (HLSL/DXC path), fused control included (0x73FFFFFF and 0x28800000); the mad_nf lines stay unfused.
    • Same test with XCL_DIRECT_DXIL=1 (direct DXIL backend, dxil/lower_valu.rs v_fma_f32 -> fmad): 3/3 passed.

    DXIL of the test's HLSL (dumped from t.hlsl, compiled with -T cs_6_6 -E main -HV 2021 -Fc), same as @Tjabooo found with DXC 2026_09_29:

    %41 = fmul float %40, %39          ; shared a*b of the mad_nf lines (no fast flags)
    ...
    %67 = call float @dx.op.tertiary.f32(i32 46, float %40, float %39, float %53)  ; FMad(a,b,c)
    

    No dx.precise and no fast-math flags anywhere in the module (0 !dx.precise), so DXC is not what splits the mad(). emit.rs:3367 ("v_fma_f32" => mad(...)) gives one FMad, which this driver fuses even with the identical fmul %40, %39 in the same program.

    What this means

    • The same DXIL that fails on RDNA1 Windows (32.0.21045.29003) and on RDNA4 RADV is fused by AMD's Windows driver on RDNA4, and per @joeypercia also on RDNA2 with 32.0.21045.29003. So the unfused result is a backend compiler choice (RDNA1's path, RADV's NIR product CSE that @Tjabooo traced), not something in XCL's HLSL or DXC flags. I couldn't test other driver versions on this card.
    • Root cause on RDNA1 Windows is still unconfirmed; a run of Tests: run the v_fma_f32 control of the no-contraction test on its own #676 (control in its own program) there would tell CSE from "never fuses FMad".
    • No fix verified here (nothing fails to fix); I'm not suggesting a diff.

    Investigation by Claude Code (Claude Opus 5.5) on this machine.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions