Repository navigation
Implement cheaper wave64 mesh shaders on 32-lane GPUs, IDEA 2 - #69
almostxxx27 wants to merge 3 commits into
Conversation
|
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 the second is atomics. buffer, global and image atomics are emitted as two lines, and the first one ( the third is small: the guard check only looks at statements that start with 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 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
|
• (a) FirstLane takes AllUniform bits only when active.NonEmpty (the prelude's vw_read(x, 0) on an empty window). |
Part of #35 (Idea 2: prove more operations wave-size independent).
Three rules in
Translation/Waveslet the mesh-window analysis prove morev_readfirstlaneresults are the console's, so fewer programs whose only blocker was a first-lane read fall to the superstep form.Changes
Compound assignments keep their facts (
WindowStatement.Parts,StatementTransfer.Apply)v_mad_u64_u32and the 64-bit shifts emitif (active) { lo = (uint)_p; hi = (uint)(_p >> 32); } }. This parsed as an opaque statement and dropped every uniformity fact, so areadfirstlaneof the result counted as "of a per-lane value". Each half is now applied as the guarded assignment it is.Bits equal on all lanes are exact under any EXEC (
ValueRules.FirstLane)readfirstlane(x)returns some lane'sxeven when the window holds no active lane or its active lanes are not the console's. So the bits that areAllUniform(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.Opaque non-effect statements propagate uniformity (
StatementTransfer.Opaque)GetDimensions, class-switch loads andasuint(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 markslo/hias assigned. Before, they kept their declared "exact and uniform" fact, which was unsound.Tests
WaveEmulationTests: 3 new rows onA_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 theoryA_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).mainand pass with this change. The new.binprograms are committed intests/FiveStar.Shaders.Tests/Assembled/.FiveStar.Shaders.Testssuite: 588 passed, 0 failed, 14 skipped (with DXC).Docs
docs/shader-translation.md: updated thev_readfirstlanerow in the Mesh windows table.docs/issue-35-wave64-mesh.md: write-up of the three ideas and of this first step.