Skip to content

Implement cheaper wave64 mesh shaders on 32-lane GPUs, IDEA 2 - #69

Open
almostxxx27 wants to merge 3 commits into
ThatRecompDev:mainfrom
almostxxx27:issue-35-uniform-first-lane-reads
Open

almostxxx27 wants to merge 3 commits into
ThatRecompDev:mainfrom
almostxxx27:issue-35-uniform-first-lane-reads

Conversation

@almostxxx27

Copy link
Copy Markdown

Part of #35 (Idea 2: prove more operations wave-size independent).

Three rules in Translation/Waves let the mesh-window analysis prove more v_readfirstlane results are the console's, so fewer programs whose only blocker was a first-lane read fall to the superstep form.

Changes

  1. Compound assignments keep their facts (WindowStatement.Parts, StatementTransfer.Apply)
    v_mad_u64_u32 and the 64-bit shifts emit if (active) { lo = (uint)_p; hi = (uint)(_p >> 32); } }. This parsed as an opaque statement and dropped every uniformity fact, so a readfirstlane of the result counted as "of a per-lane value". Each half is now applied as the guarded assignment it is.

  2. Bits equal on all lanes are exact under any EXEC (ValueRules.FirstLane)
    readfirstlane(x) returns some lane's x even when the window holds no active lane or its active lanes are not the console's. So the bits that are AllUniform (an SGPR copy, the group's bits of the mesh thread id) are the console's regardless. Before, the result was unknown unless EXEC was exact and non-empty.

  3. Opaque non-effect statements propagate uniformity (StatementTransfer.Opaque)
    GetDimensions, class-switch loads and asuint(double, lo, hi) now give their results the uniformity of what they read. Effects (an atomic's old value) stay per-lane. asuint(d, lo, hi) now also marks lo/hi as assigned. Before, they kept their declared "exact and uniform" fact, which was unsound.

Tests

  • WaveEmulationTests: 3 new rows on A_mesh_program_states_whether_narrower_waves_give_console_results (64-bit multiply-add from SGPRs, the same with a VGPR operand, and a 64-bit shift), plus a new theory A_first_lane_read_of_bits_equal_on_all_lanes_is_the_consoles_whatever_lanes_the_wave_holds (mesh thread id masked to the group bits under an inexact EXEC).
  • The new floor rows fail on main and pass with this change. The new .bin programs are committed in tests/FiveStar.Shaders.Tests/Assembled/.
  • Full FiveStar.Shaders.Tests suite: 588 passed, 0 failed, 14 skipped (with DXC).

Docs

  • docs/shader-translation.md: updated the v_readfirstlane row in the Mesh windows table.
  • docs/issue-35-wave64-mesh.md: write-up of the three ideas and of this first step.

@ThatRecompDev

Copy link
Copy Markdown
Owner

hey, thanks a lot for picking this up, and for writing rows that actually fail on main. I went through it properly and ran it against the whole shader dump. (also re-assembled your seven test programs from the source here, they come out byte for byte the same as the .bin files you committed.)

the compound assignment part looks solid to me, and you're right that the asuint lo/hi thing was unsound before, nice catch.

I did run into three cases where the analysis now says more than is true though.

the first is the "any EXEC" rule: our readfirstlane in the prelude doesn't return some lane's value when the window has no active lane, it does vw_read(x, 0), and for a window that doesn't hold console lane 0 that comes back as 0. so the group bits of the thread id end up wrong for every group except group 0. your first row in the new theory is actually that case, it just doesn't show because nothing draws it (it does show on WARP's 4-lane waves with two groups, 2 pixel mismatches). if you only take those bits when the window is known to hold an active lane it's fine again.

the second is atomics. buffer, global and image atomics are emitted as two lines, and the first one (uint _at = 0; if (active) { RWByteAddressBuffer ...) isn't an effect, so the old value now counts as uniform and a readfirstlane of it turns into s23 = v21; in the superstep form. the Interlocked line needs to mark its last argument as assigned.

the third is small: the guard check only looks at statements that start with if (active), and v_movreld emits a switch with the if (active) inside each case, so it gets "equal on all lanes" under a partial EXEC. treating anything that reads active as guarded fixes it. that one fails a draw on WARP and on a 4090.

on the numbers: on the corpus (25,908 programs) this moves 3 mesh programs from 64 to 4 (MS_VS_TransformD_c5fd36bb and the two MirrorReflectionTransformD ones) and changes the reason comment on 22 others, nothing else differs, no compute program changes. funny detail, the win doesn't come from the mad/shift rule at all, no mesh program in the dump has one. it comes from the texture view declaration now passing uniformity on, so a Load at scalar coordinates is uniform. worth saying that in the description. with the three fixes below the output is byte for byte the same as yours, so you lose nothing.

a few housekeeping bits: the Architecture tests fail on two things in WindowStatement.cs (ParseText is 61 lines, the limit is 60, and the ? : in the initializer needs to be an if/else). commit messages here go Shaders: what changed. and I'd rather keep docs/issue-35-wave64-mesh.md out of docs/ and put it in the issue, docs only describe what the code does. one thing in there to double check too: NVIDIA cards report 32-32 for us and reject a 64 pin, so idea 1 doesn't work as written. the 2,548 figure it repeats from shader-translation.md is stale as well, that's on us (1,489 on this corpus today).

since any translator change makes everyone re-translate, this would go in with the next translator batch rather than on its own.

here's the small patch that closes the three holes, take it or do it your own way:

fix suggestion (15 lines in Translation/Waves)
diff --git a/src/FiveStar.Shaders/Translation/Waves/StatementTransfer.cs b/src/FiveStar.Shaders/Translation/Waves/StatementTransfer.cs
index 12f815de..9f05bb2c 100644
--- a/src/FiveStar.Shaders/Translation/Waves/StatementTransfer.cs
+++ b/src/FiveStar.Shaders/Translation/Waves/StatementTransfer.cs
@@ -160,7 +160,8 @@ internal sealed class StatementTransfer
             uniform = statement.Reads.All(name => _state.Get(name).IsActiveUniform);
 
             // A guarded write leaves the inactive lanes' values: equal on all lanes only when all are active.
-            allUniform = uniform && (!statement.IsGuarded || _state.Get(WindowNames.Active).Full)
+            var guarded = statement.IsGuarded || statement.Reads.Contains(WindowNames.Active);
+            allUniform = uniform && (!guarded || _state.Get(WindowNames.Active).Full)
                 && statement.Reads.All(name => _state.Get(name).AllUniform == ValueFact.AllBits);
         }
 
diff --git a/src/FiveStar.Shaders/Translation/Waves/ValueRules.cs b/src/FiveStar.Shaders/Translation/Waves/ValueRules.cs
index 5fffbff5..63e73f88 100644
--- a/src/FiveStar.Shaders/Translation/Waves/ValueRules.cs
+++ b/src/FiveStar.Shaders/Translation/Waves/ValueRules.cs
@@ -174,9 +174,15 @@ internal static partial class ValueRules
     /// </summary>
     private static ValueFact FirstLane(ValueFact source, ValueFact active)
     {
-        var exact = source.Exact & source.AllUniform;
+        // The prelude's readfirstlane gives 0 on a window without an active lane that does not hold console lane 0.
+        var exact = 0u;
         var held = source.Held & source.AllUniform;
 
+        if (active.NonEmpty)
+        {
+            exact = source.Exact & source.AllUniform;
+        }
+
         if (active.IsExact)
         {
             held |= source.Held & source.Uniform;
diff --git a/src/FiveStar.Shaders/Translation/Waves/WindowStatement.cs b/src/FiveStar.Shaders/Translation/Waves/WindowStatement.cs
index 2a879519..f8398c42 100644
--- a/src/FiveStar.Shaders/Translation/Waves/WindowStatement.cs
+++ b/src/FiveStar.Shaders/Translation/Waves/WindowStatement.cs
@@ -170,6 +170,9 @@ internal sealed partial class WindowStatement
             assigned = assigned.Concat(Names(text));
         }
 
+        // An atomic writes the old value to its last argument.
+        assigned = assigned.Concat(AtomicResultPattern().Matches(text).Select(match => match.Groups["out"].Value));
+
         // asuint(double, lo, hi) writes its last two arguments.
         assigned = assigned.Concat(SplitDoublePattern().Matches(text).SelectMany(match => new[] { match.Groups["lo"].Value, match.Groups["hi"].Value }));
 
@@ -304,6 +307,9 @@ internal sealed partial class WindowStatement
     [GeneratedRegex(@"\basuint\([^;]*, (?<lo>[A-Za-z_]\w*), (?<hi>[A-Za-z_]\w*)\)")]
     private static partial Regex SplitDoublePattern();
 
+    [GeneratedRegex(@"\bInterlocked\w*\([^;]*?, (?<out>[A-Za-z_]\w*)\)")]
+    private static partial Regex AtomicResultPattern();
+
     [GeneratedRegex(@"^(?<guard>if \(active\) )?\{ (?<body>[^{}]+;) \}(?: \})*$")]
     private static partial Regex CompoundPattern();
 }

the three failing tests need assembled programs, so they don't fit in a comment, tell me if you want them and I'll push them to a branch.

really appreciate the work, this is a good direction. fix those and I'm happy to merge.

…hatRecompDev#35)

Compound if (active) { lo; hi; } assignments keep their facts, non-effect
opaque statements (GetDimensions, texture view declarations, class-switch
loads, asuint splits) pass on the uniformity of their reads, and a
readfirstlane of bits equal on all lanes is exact when the window holds an
active lane. Atomics mark their result argument assigned and per-lane, and
a statement reading active (the v_movreld switch) counts as guarded.
…m/almostxxx27/five-star-recomp into issue-35-uniform-first-lane-reads

# Conflicts:
#	docs/shader-translation.md
#	src/FiveStar.Shaders/Translation/Waves/StatementTransfer.cs
#	src/FiveStar.Shaders/Translation/Waves/ValueRules.cs
#	src/FiveStar.Shaders/Translation/Waves/WindowStatement.cs
#	tests/FiveStar.Shaders.Tests/WaveEmulationTests.cs
@almostxxx27

Copy link
Copy Markdown
Author

• (a) FirstLane takes AllUniform bits only when active.NonEmpty (the prelude's vw_read(x, 0) on an empty window).
• (b) The Interlocked* result argument is marked assigned and per-lane.
• (c) A statement reading active counts as guarded (v_movreld switch).
• Regression tests for all three (each fails on the old commit). ParseText is under 60 lines with no ternary, the commit message uses Shaders:, and the issue notes moved out of docs/ (posting the corrected version in #35).
• Corpus impact as you measured: 3 mesh programs 64→4, and the win comes from texture view declarations passing uniformity on, not from the mad/shift rule.

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