Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions docs/experiments/native-graphics-wqm-cross-quad.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Cross-quad WQM/EXECZ in a native pixel draw (#2907)

[The fixture](../../tests/native_graphics_wqm_check.py) draws source-built gfx1013 pixel stages through the
direct DXIL frontend. In each one, an `s_cbranch_execz` under WQM decides a scalar that every surviving pixel
exports. The branch is skipped only when EXEC is zero in the whole guest wave. A quad with no EXEC bit at the
branch therefore still sees whether another quad of its wave kept the wave from skipping.

## The accepted form

```
s_mov sE, exec ; s_wqm exec, exec
v_cmp_* vcc, ... ; s_andn2 sE, sE, vcc one or more coverage filters
v_cmp_* vcc, ... ; s_andn2 sM, sE|exec, vcc the branch mask, from the survivors or the whole-quad EXEC
v_cmp_* vcc, ... ; s_andn2 sM, sM, vcc optional further terms
s_wqm exec, sM ; s_cbranch_execz J the region writes only scalars
J: s_mov exec, sE then a terminal colour export
```

Following the maintainers' decision on the issue, the branch condition is `WaveActiveAnyTrue` of the
quad-complete EXEC bit:
- the modelled mask predicate is OR-ed with its `QuadReadAcrossX`, `QuadReadAcrossY` and
`QuadReadAcrossDiagonal`;
- quad reads see helper lanes, so a quad whose EXEC bits sit only on helpers or on covered pixels that did
not survive still votes, as it does on the guest;
- all of this uses Shader Model 6.0 operations, with no `[WaveOpsIncludeHelperLanes]`.

The calls are emitted before the first conditional branch, where the whole quad still runs. The IR test checks
this, and checks that their declarations carry the attributes DXC gives the HLSL intrinsics.

These still keep a refusal by name:
- inside the region: a vector or DPP instruction, an EXEC or VCC read, a hardware register access
(`s_setreg`/`s_getreg`), a branch, or an EXEC write other than the restore;
- a restore that is not the branch target;
- a second branch;
- anything after the restore other than lane-local ALU and colour exports.

`raster_program(2.5, cross=True)` keeps its existing refusal.

## Contract and what this shows

The lowering decides the branch once per **desktop** wave. It relies on the packing contract of #2835 and
#2854: a correct program cannot depend on which quads the console puts in one wave. A program can only see the
branch outcome through an effect outside EXEC, here an SGPR. This fixture builds such a dependency on purpose,
so its reference comes from the measured desktop layout.

**The draws show that the lowering votes over the desktop wave. They do not show equivalence with the console
for an output that depends on another quad.**

## Measured layout

Two HLSL pixel stages on the same draw report, for every covered pixel:
- its wave: the smallest covered id in it;
- the ids of its three quad peers;
- whether each peer is a helper (`IsHelperLane`, SM 6.6).

Wave operations leave helpers out, so the helper flags come from quad reads. The fixture checks that every
desktop quad is the aligned 2x2 block, and that all covered lanes of a quad report the same quad. Quads are
per primitive. On WARP the two middle quads of the 8x2 target are each shaded twice, once with each row covered
and the other row as helpers, as if the full-screen triangle were drawn as two primitives. The layout is
measured in separate draws with the same vertex stage and target. The reference follows the measured quads.

| Adapter | Lanes | Full draw | Diagonal draw |
|---|---|---|---|
| NVIDIA GeForce RTX 3070, driver 32.0.16.2101 | 32 | 4 quads in 1 wave, no helpers | 3 quads in 1 wave, 3 helpers |
| Microsoft Basic Render Driver, 10.0.26100.9818 | 4 | 6 quads in 6 waves, 8 helpers | 5 quads in 5 waves, 11 helpers |

## Cases and controls

| Draw | Mask | Shows |
|---|---|---|
| full | survivors with x <= 1 (14 survivors) | another quad's survivors keep the wave from skipping |
| full | survivors with x <= 4.5 (all 16) | as above, one quad without bits |
| full | x <= -1 | empty wave: the branch is taken, every vote agrees |
| full | whole-quad EXEC with x >= 4.5, survivors x <= 3.5 | bits only on covered pixels that did not survive |
| full | survivors with x >= 4.5 | no bits: WQM never makes a zero mask non-zero |
| diagonal | whole-quad EXEC, x >= 5, y >= 1 | the bit only on the diagonal helper of (4,0) |
| diagonal | whole-quad EXEC, x >= 5, y <= 1 | the bit only on the horizontal helper |
| diagonal | whole-quad EXEC, x = 4..5, y >= 1 | the bit only on the vertical helper |

Each case is drawn against the measured-layout reference. Each control redraws the same program against a
different reference and must differ:
- a quad-local vote;
- a wave vote without the quad OR;
- a vote over surviving pixels only.

Pixels outside exact coverage keep the clear value -1.0.

## Command and result

```powershell
cmake --build --preset dev --target compile_dxil offscreen_check native_store_replay native_pixel_ir_dump
ctest --preset dev --no-tests=error -V -R '^native_graphics_wqm_(hardware|warp|ir)$'
```

See the pull request for the measured counts.
- **On the RTX 3070** every control differs.
- **On WARP** (one quad per wave), the quad-local control cannot differ, and the test prints
`SKIPPED cross-quad quad-local control`. The three helper cases still differ from the no-OR and survivor
votes, at pixel (4,0).
- On a physical adapter with no wave holding two quads, the hardware entry exits 77 after the other checks.

## Limits

- **Helper lanes after the vote:** wave intrinsics are undefined on helper lanes, so the branch may diverge for
them. That is safe only while nothing after the branch reads another lane.
- **Adapters:** a 64-lane adapter has not been run. The RX 9070 XT is blocked by #2915. AMD and Intel have
not been run.
- **Programs:** only the form above is accepted. Real programs and captures have not been run.
- **Clipping:** a console that clips or splits primitives differently from the desktop forms different quads.
This holds for the existing quad-local path too.
38 changes: 37 additions & 1 deletion src/gfx1013_direct_graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
PixelWqm wqm;
if (pixel) {
wqm.raster_guard(program,stage.wave32);
wqm.raster_vote(program,stage.wave32);
wqm.numeric_guards(program,stage.wave32);
}
GraphicsMaskRegions regions(program, stage.wave32, !pixel);
Expand Down Expand Up @@ -1487,6 +1488,35 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
alu.write(128,{});
return;
}
if (auto vote=wqm.vote_masks.find(pc); vote!=wqm.vote_masks.end()) {
if (!whole_quad || exact_after_wqm || !alu.graphics_predicate.empty() || wqm.survivor.empty())
refuse(pc,"cross-quad EXECZ mask requires whole-quad EXEC after a coverage filter");
// Every lane of a covered quad, helpers included, is in the
// whole-quad EXEC; the filtered coverage holds only survivors.
const auto keep=alu.cmp("eq",alu.read(106),"0");
using Source=PixelWqm::MaskSource;
if (vote->second==Source::narrowed && wqm.vote_lane.empty())
refuse(pc,"cross-quad EXECZ mask narrowed before it is written");
wqm.vote_lane=vote->second==Source::whole_quad ? keep :
alu.ir.value("and i1 "+(vote->second==Source::survivors ? wqm.survivor : wqm.vote_lane)+", "+keep);
alu.write(i.dst,{}); if (!stage.wave32) alu.write(i.dst+1,{});
alu.write(128,{});
return;
}
if (pc==wqm.vote_wqm) {
if (wqm.vote_lane.empty()) refuse(pc,"cross-quad EXECZ vote has no modelled EXEC");
// EXEC at the branch is the WQM expansion of the mask: OR it over
// the quad (quad reads see helper lanes), then vote over the wave.
alu.ir.uses_wave=true;
auto bit=alu.ir.value("zext i1 "+wqm.vote_lane+" to i32");
auto quad=bit;
for (unsigned op=0;op<3;++op)
quad=alu.ir.binary("or",quad,
alu.ir.invoke("i32","quadOp.i32",123,{{"i32",bit},{"i8",to_string(op)}},"nounwind"));
auto vote=alu.ir.invoke("i1","waveAnyTrue",113,{{"i1",alu.cmp("ne",quad,"0")}},"nounwind");
alu.write(128,alu.ir.value("zext i1 "+vote+" to i32"));
return;
}
if (wqm.quad_guards.count(pc)) {
// Helpers have no input coverage bit. Broadcast the four survivor
// bits while the quad is intact. Only a terminal colour region
Expand Down Expand Up @@ -1999,7 +2029,13 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv =
auto condition = branch != regions.lane_branches.end() ?
alu.cmp(branch->second.second ? "ne" : "eq", alu.read(branch->second.first), "0") :
alu.cmp((last.op & 1) ? "ne" : "eq", alu.read(last.op >= 6 ? 106 : 128), "0");
if (branch == regions.lane_branches.end() && alu.ir.dependencies[condition].any())
// The cross-quad EXECZ vote is uniform over the desktop wave;
// its input dependencies still reach the values merged after it.
// Wave intrinsics are undefined on helper lanes, so a helper may
// take the other edge: safe only while nothing after the branch
// reads another lane (raster_vote keeps the tail lane-local).
if (branch == regions.lane_branches.end() && alu.ir.dependencies[condition].any() &&
last.pc != wqm.vote_skip)
refuse(last.pc, "graphics scalar condition depends on a varying input");
alu.ir.body << " br i1 " << condition << ", label %g" << last.target() << ", label %g" << last.next
<< "\n";
Expand Down
123 changes: 123 additions & 0 deletions src/gfx1013_graphics_wqm.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ struct PixelWqm {
std::set<unsigned> filters, quad_guards;
unsigned exact_restore = UINT_MAX;
string survivor;
// Cross-quad EXECZ (#2907): each write of the branch mask, taken from the
// filtered coverage or the whole-quad EXEC, or narrowing the mask again.
enum class MaskSource { survivors, whole_quad, narrowed };
std::map<unsigned, MaskSource> vote_masks;
unsigned vote_wqm = UINT_MAX, vote_skip = UINT_MAX;
string vote_lane;

static bool local(const Inst &i) {
return GraphicsMaskRegions::local_alu(i) || i.name == "s_waitcnt" || i.name == "s_nop" ||
Expand Down Expand Up @@ -139,6 +145,123 @@ struct PixelWqm {
p.at.erase(code.back().pc);
code.resize(null);
}
// A scalar instruction that neither reads nor writes EXEC or VCC.
static bool plain_scalar(const Inst &i) {
if (i.enc != Encoding::Sop1 && i.enc != Encoding::Sop2 && i.enc != Encoding::Sopc &&
i.enc != Encoding::Sopk)
return false;
return (i.enc == Encoding::Sopc || i.dst < 106) && i.name.find("exec") == Name::npos &&
std::none_of(i.src.begin(), i.src.end(), [](unsigned r) { return r >= 106 && r <= 127; });
}
// Cross-quad EXECZ under WQM (#2907):
// s_mov sE, exec; s_wqm exec, exec
// one or more coverage filters v_cmp_* vcc; s_andn2 sE, sE, vcc
// v_cmp_* vcc; s_andn2 sM, sX, vcc sX = sE (survivors) or exec (whole quads)
// s_wqm exec, sM; s_cbranch_execz J the region writes only scalars
// J: s_mov exec, sE then a terminal colour export
// Lanes are skipped only when EXEC is zero in the whole guest wave, so the
// branch is a vote over the wave. It is lowered over the desktop wave,
// under the #2835 packing contract: a correct program cannot depend on
// which quads share a guest wave. The vote reads EXEC as modelled at the
// branch, OR-ed across the quad, so helper lanes count as on the guest.
void raster_vote(Program &p, bool narrow) {
auto &code = p.code;
const auto mov = narrow ? "s_mov_b32" : "s_mov_b64", andn2 = narrow ? "s_andn2_b32" : "s_andn2_b64";
const unsigned last_reg = narrow ? 105u : 104u;
auto aligned = [&](unsigned r) { return r <= last_reg && (narrow || !(r & 1)); };
auto overlaps = [&](const Inst &i, unsigned r) {
const unsigned width = i.name.find("_b64") != Name::npos ? 2 : 1;
return i.enc != Encoding::Sopc && i.dst < r + (narrow ? 1u : 2u) && r < i.dst + width;
};
auto compare = [](const Inst &i) {
return i.enc == Encoding::Vopc && i.dst == 106 && i.name.substr(0, 6) == "v_cmp_" &&
i.name.find("class") == Name::npos;
};
if (!filters.empty() || code.size() < 10 || code[0].name != mov || code[0].src[0] != 126 ||
!aligned(code[0].dst) || !is_wqm(code[1], narrow) || code[1].dst != 126 || code[1].src[0] != 126)
return;
const unsigned entry = code[0].dst;
size_t n = 2;
while (n + 1 < code.size() && !code[n].branch() && !(is_wqm(code[n], narrow) && code[n].dst == 126))
++n;
if (n + 1 >= code.size() || code[n].branch() || !code[n + 1].branch() || code[n + 1].op != 8 ||
code[n + 1].target() <= code[n + 1].pc)
return;
const auto &wqm = code[n];
auto &skip = code[n + 1];
if (!aligned(wqm.src[0]) || wqm.src[0] == entry)
return;
const unsigned mask = wqm.src[0];
// Before the vote: filters on sE, the mask write and lane-local work.
std::vector<unsigned> filter_pcs;
std::map<unsigned, MaskSource> mask_writes;
bool vcc_from_compare = false;
for (size_t k = 2; k < n; ++k) {
const auto &i = code[k];
if (compare(i)) {
vcc_from_compare = true;
continue;
}
if (i.name == andn2 && i.src[1] == 106) {
if (!vcc_from_compare)
return;
if (i.dst == entry && i.src[0] == entry && mask_writes.empty())
filter_pcs.push_back(i.pc);
else if (i.dst == mask && mask_writes.empty() && (i.src[0] == entry || i.src[0] == 126))
mask_writes.emplace(i.pc, i.src[0] == 126 ? MaskSource::whole_quad : MaskSource::survivors);
else if (i.dst == mask && i.src[0] == mask && !mask_writes.empty())
mask_writes.emplace(i.pc, MaskSource::narrowed);
else
return;
vcc_from_compare = false;
continue;
}
if (local(i) && !(i.dst < 256 && (i.dst == 106 || i.dst == 107)))
continue;
if (plain_scalar(i) && !overlaps(i, entry) && !overlaps(i, mask))
continue;
return;
}
if (filter_pcs.empty() || mask_writes.empty())
return;
// The region: from here on the shape is claimed, so refuse by name.
size_t r = n + 2;
for (; r < code.size() && !(code[r].name == mov && code[r].dst == 126); ++r) {
const auto &i = code[r];
if (i.name == "s_nop" || i.name == "s_waitcnt")
continue;
if (i.branch())
refuse(i.pc, "cross-quad EXECZ region contains a branch");
if (i.name.find("s_setreg") == 0 || i.name.find("s_getreg") == 0)
refuse(i.pc, "cross-quad EXECZ region accesses a hardware register with " + string(i.name));
if (!plain_scalar(i))
refuse(i.pc, "cross-quad EXECZ region may only write scalars; " + string(i.name) +
" depends on the lanes the vote leaves out");
if (overlaps(i, entry))
refuse(i.pc, "cross-quad EXECZ region overwrites the saved coverage");
}
if (r == code.size() || code[r].src[0] != entry || skip.target() != code[r].pc)
refuse(skip.pc, "cross-quad EXECZ must branch to the exact coverage restore");
for (size_t j = r + 1; j < code.size(); ++j) {
const auto &i = code[j];
if (i.name == "s_endpgm" && j + 1 == code.size())
break;
if (!(GraphicsMaskRegions::local_alu(i) || i.name == "s_waitcnt" ||
(i.enc == Encoding::Export && ((i.a >> 4) & 63) < 8)))
refuse(i.pc, "cross-quad EXECZ needs a terminal colour export after the restore");
}
for (const auto &i : code)
if (i.branch() && i.pc != skip.pc)
refuse(i.pc, "cross-quad EXECZ program has another branch");
filters.insert(filter_pcs.begin(), filter_pcs.end());
vote_masks.insert(mask_writes.begin(), mask_writes.end());
vote_wqm = wqm.pc;
vote_skip = skip.pc;
exact_restore = code[r].pc;
// SCC carries the vote; it is uniform over the desktop wave.
skip.op = 4;
skip.name = "s_cbranch_scc0";
}
string expand(DxilModule &ir, const string &mask) const {
auto pair = ir.binary("or", mask, ir.binary("lshr", mask, "1"));
auto nibble = ir.binary("or", pair, ir.binary("lshr", pair, "2"));
Expand Down
11 changes: 11 additions & 0 deletions tests/cmake/native_graphics_wqm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Cross-quad WQM/EXECZ in a native pixel draw (#2907). native_graphics_wqm_hardware stays in CMakeLists.txt.
# The WARP entry runs every case that one quad per wave can decide; the IR entry checks, without a GPU, that the
# vote's quad and wave operations are emitted before any branch.
if(TARGET compile_dxil AND TARGET native_store_replay AND TARGET offscreen_check AND EXISTS "${INTAKE_PYTHON}")
add_test(NAME native_graphics_wqm_warp COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_graphics_wqm_check.py" "$<TARGET_FILE:compile_dxil>"
"$<TARGET_FILE:offscreen_check>" "${DXC_BIN_DIR}" warp)
set_tests_properties(native_graphics_wqm_warp PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60)
add_test(NAME native_graphics_wqm_ir COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/native_graphics_wqm_check.py" ir "$<TARGET_FILE:native_pixel_ir_dump>")
endif()
Loading