Skip to content
Open
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
81 changes: 81 additions & 0 deletions docs/experiments/native-mesh-scalar-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Uniform scalar loops in the mesh frontend

The C++ mesh frontend (`lower_mesh`) emits DXIL in one forward pass, so it accepted only forward branches. It now also
accepts one narrow loop shape, a *uniform scalar loop*. The loop has one header and one back edge
(`s_cbranch_scc0`, `s_cbranch_scc1` or `s_branch`). The body contains only:

- the 32-bit scalar ALU already allowed between a diamond join and its EXEC restore;
- scalar compares;
- scalar loads (`s_load_dword*`, `s_buffer_load_dword*`);
- `s_waitcnt`, `s_nop` and `s_clause`;
- forward exits (`s_cbranch_scc0/scc1`) past the back edge, inside the enclosing EXECZ region.

The pass stays forward-only. A value the body writes is *carried* into the next iteration in two cases: one pass
reads it before that write, or an exit branch comes before that write, so the exit sees the previous iteration's
value. SCC counts as a value here, like an SGPR: the body defines it with a compare or with any of the scalar ALU
above except the moves, `s_cselect_b32`, the multiplies, `s_brev_b32`, `s_bfm_b32` and the packs. A carried value
lives in a local slot. The slot is stored before the header and at the back edge, and loaded in the header. In the
header a carried register becomes a plain number. A register the body writes only before every exit is unbound
there. A register the body never writes keeps its entry value. The exits use the existing forward-join merge. The
trip count comes from guest data, as on the GPU.

A value that only an exit sees can lack a value before the loop. It then stays unbound at that exit, and a read after
the loop refuses with `unbound numeric SGPR sN`.

The following keep a refusal:

| Case | Refusal |
|---|---|
| Two back edges to one header | `mesh loop needs exactly one back edge` |
| A vector, LDS, EXEC or export instruction in the body | `mesh loop body holds an unsupported instruction: <name>` |
| A back edge on VCC or EXEC | `mesh loop back edge needs SCC or an unconditional branch` |
| A branch into the body other than at the header | `mesh loop is entered other than at its header` |
| A loop after GS_ALLOC_REQ, under a per-lane else, or outside full EXEC and any EXECZ region | `mesh loop outside a uniform scalar region` |
| A register read in the body before the body writes it, without a numeric entry value | `mesh loop carries a register without a numeric entry value: sN` |
| SCC read in the body before the body defines it, without a known entry value | `mesh loop reads SCC before the body defines it, without a known entry value` |

## Checks

`tests/native_direct_mesh_check.py`, `scalar-loop`: a source-built loop counts s20 from 0 while it is below the root
constant s21, and leaves early when s20 is 5. The two exits set s20 to 7 or 9 and join before the export reads s20.
The IR check covers:

- only s20 gets a slot: the bound s21 is read in place, and the body defines SCC before reading it;
- s20 is stored twice, before the header and at the back edge;
- the back edge branches on SCC == 0;
- the body compares with 5 and with s21.

`scalar-loop-branch-back` uses an unconditional `s_branch` back edge. Its only exit is the early one, and it emits
no exit block after the back edge. The refusals above each have a case: `scalar-loop-vector`, `-vcc-back`,
`-two-back` and `-enter-body`.

SCC entering the loop:

- `scalar-loop-scc-unknown` exits on SCC at the top of the body, and nothing before the loop set SCC: it is refused.
- `scalar-loop-scc-after-move` puts `s_mov_b32` before that exit; a move leaves SCC alone, so the refusal stays.
- `scalar-loop-scc-known` sets SCC from the root constant before the loop. SCC gets a slot, and the slot's two stores
are IR values, never a literal 0.

A value seen only at an early exit: s22 = 7 before the loop, the header exits when s20 reaches the bound, the body
then sets s22 = 9, and only the export after the exit reads s22. The guest exports 7 after zero trips and 9 after
one. `scalar-loop-exit-read-{80,81,15}` (bound 0, bound 1 and the root constant s21) follow the exported x back to
the header's load of the s22 slot. They also check that the slot's only stores are 7 before the loop and 9 at the
back edge. `scalar-loop-exit-unset` leaves s22 unset before the loop: it translates while nothing reads s22, and
`-unset-read` refuses the read at the exit.

Controls (one changed line each, `native_direct_mesh` fails):

- Without the frontend change, `scalar-loop` fails with `unsupported mesh scalar branch destination`.
- With the back-edge stores removed, the store count assertion fails.
- Leaving an unknown SCC uncarried instead of refusing: `scalar-loop-scc-unknown` no longer gets its refusal.
- Carrying only values read before their write (the exit rule off): `scalar-loop-exit-read-80` fails with
`unbound numeric SGPR s22`.
- Carrying an exit-only value even without an entry value: `scalar-loop-exit-unset` is refused.
- Counting `s_mov_b32` as an SCC write: `scalar-loop-scc-after-move` loses its refusal.

## Local requests

In 18 captured slot 86 mesh requests, the same loop searches records of 0x130 bytes for a flag bit, bounded by dword
0 of root SRV 3 (#3703). With #3702 and #3704 applied, all 18 now pass the loop without any stand-in. Their next
refusal is `mesh image load lacks a remapped Texture1D heap contract` (item 4 of #3703). These are counts only, from
`compile_dxil <dxc> <out> gfx1013-container-ms-texture-heap <request>.m86 <out>.cso`.
146 changes: 134 additions & 12 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,123 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
exec_value[0]=mask.words[0]; exec_value[1]=mask.words[1];
};
std::optional<unsigned> pending_saveexec_restore;
// SGPRs, M0, null, inline and literal constants, and SCC; not VCC, EXEC, VCCZ or EXECZ.
const auto lane_free=[](unsigned s) { return s<106 || s==124 || s==125 || (s>=128 && s<=250) || s==253 || s==255; };
// 32-bit scalar ALU only: no 64-bit mask forms, calls, register writes or relative moves.
static const std::set<string> scalar_alu32{
"s_mov_b32","s_not_b32","s_brev_b32","s_movk_i32","s_addk_i32","s_mulk_i32",
"s_add_u32","s_sub_u32","s_add_i32","s_sub_i32","s_addc_u32","s_subb_u32","s_mul_i32",
"s_mul_hi_u32","s_mul_hi_i32","s_min_i32","s_min_u32","s_max_i32","s_max_u32","s_absdiff_i32",
"s_cselect_b32","s_and_b32","s_or_b32","s_xor_b32","s_andn2_b32","s_orn2_b32","s_nand_b32",
"s_nor_b32","s_xnor_b32","s_lshl_b32","s_lshr_b32","s_ashr_i32","s_bfm_b32","s_bfe_u32",
"s_bfe_i32","s_lshl1_add_u32","s_lshl2_add_u32","s_lshl3_add_u32","s_lshl4_add_u32",
"s_pack_ll_b32_b16","s_pack_lh_b32_b16","s_pack_hh_b32_b16"};
// A uniform scalar loop (#3703): one header, one back edge (s_cbranch_scc0/scc1 or s_branch), and a body of
// the scalar ALU above, scalar compares, scalar loads, waits and forward exits past the back edge. The pass
// stays forward-only: carried SGPRs and SCC live in local slots that are stored before the header and at the
// back edge and loaded in the header.
std::map<unsigned,std::vector<unsigned>> back_edges;
for (unsigned at=0;at<code.size();) {
const auto op=gfx1013::instruction(code,at);
if (!op.size) break;
const auto w=word(code,at);
const unsigned sopp=(w>>16)&127;
if ((w&0xff800000u)==0xbf800000u && (sopp==2 || (sopp>=4 && sopp<=9))) {
const int64_t target=int64_t(at)+4+4*int64_t(int16_t(w&0xffff));
if (target>=0 && target<=int64_t(at)) back_edges[unsigned(target)].push_back(at);
}
at+=op.size;
}
struct MeshLoop { unsigned head, back; std::vector<std::pair<unsigned,string>> slots; };
std::optional<MeshLoop> loop;
unsigned pc = 0;
auto open_loop=[&](const std::vector<unsigned> &backs) {
const unsigned head=pc, back=backs.front();
if (backs.size()!=1) refuse(head,"mesh loop needs exactly one back edge");
if (branch || allocated || (!full_exec && !exec_skip) || (exec_skip && back>=exec_skip->end))
refuse(head,"mesh loop outside a uniform scalar region");
for (const auto &edge : forward)
if (edge.first>head && edge.first<=back) refuse(head,"mesh loop is entered other than at its header");
// A value the body writes is carried into the next iteration when one pass reads it before that write,
// or when an exit branch comes before that write (the exit then sees the previous iteration's value).
// A value the body never writes keeps its entry value. SCC (128) is tracked like an SGPR.
std::set<unsigned> written, read_first, before_exit, carried;
bool exited=false;
// Operands name SGPRs below 106; 128 there is the inline constant 0, so SCC reads go through read_scc.
const auto read=[&](unsigned r) { if (r<106 && !written.count(r)) read_first.insert(r); };
const auto read_scc=[&] { if (!written.count(128)) read_first.insert(128); };
const auto write=[&](unsigned r) { written.insert(r); if (!exited) before_exit.insert(r); };
// Scalar ALU forms here that leave SCC unchanged; every other one writes it.
static const std::set<string> keeps_scc{"s_mov_b32","s_movk_i32","s_cselect_b32","s_mul_i32","s_mul_hi_u32",
"s_mul_hi_i32","s_mulk_i32","s_brev_b32","s_bfm_b32","s_pack_ll_b32_b16","s_pack_lh_b32_b16",
"s_pack_hh_b32_b16"};
unsigned at=head;
while (at<=back) {
const auto i=decode(code,at,true,true);
const string name(i.name);
if (i.enc==Encoding::Smem) {
const unsigned offset_reg=i.b>>25, count=memory_count(i);
if (!(i.op<=4 || (i.op>=8 && i.op<=12)) || i.dst+count>106 || i.src[0]>=104 ||
(offset_reg!=125 && offset_reg>=106))
refuse(at,"mesh loop body holds an unsupported scalar load");
for (unsigned j=0;j<(i.op>=8 ? 4u : 2u);++j) read(i.src[0]+j);
if (offset_reg!=125) read(offset_reg);
for (unsigned j=0;j<count;++j) write(i.dst+j);
} else if (i.enc==Encoding::Sopp && (name=="s_waitcnt" || name=="s_nop" || name=="s_clause")) {
} else if (i.enc==Encoding::Sopp && at==back) {
if (i.target()!=head || (name!="s_cbranch_scc0" && name!="s_cbranch_scc1" && name!="s_branch"))
refuse(at,"mesh loop back edge needs SCC or an unconditional branch");
if (name!="s_branch") read_scc();
} else if (i.enc==Encoding::Sopp && (name=="s_cbranch_scc0" || name=="s_cbranch_scc1")) {
if (i.target()<=back || i.target()>=code.size() || (exec_skip && i.target()>exec_skip->end))
refuse(at,"mesh loop exit must branch forward past the back edge");
read_scc();
exited=true;
} else if (i.enc==Encoding::Sopc && (name.rfind("s_cmp",0)==0 || name.rfind("s_bitcmp",0)==0) &&
lane_free(i.src[0]) && lane_free(i.src[1])) {
read(i.src[0]); read(i.src[1]);
write(128);
} else if ((i.enc==Encoding::Sop1 || i.enc==Encoding::Sop2 || i.enc==Encoding::Sopk) &&
scalar_alu32.count(name) && i.dst<106 && lane_free(i.src[0]) &&
(i.enc!=Encoding::Sop2 || lane_free(i.src[1]))) {
if (i.enc!=Encoding::Sopk) read(i.src[0]);
if (i.enc==Encoding::Sop2) read(i.src[1]);
if (name=="s_addk_i32" || name=="s_mulk_i32") read(i.dst);
if (name=="s_cselect_b32" || name=="s_addc_u32" || name=="s_subb_u32") read_scc();
write(i.dst);
if (!keeps_scc.count(name)) write(128);
} else {
refuse(at,"mesh loop body holds an unsupported instruction: "+name);
}
if (at==back) break;
at=i.next;
}
if (at!=back) refuse(head,"mesh loop back edge splits an instruction");
std::vector<std::pair<unsigned,string>> slots;
for (auto r : written) {
const bool read_early=read_first.count(r), exit_early=!before_exit.count(r);
if (!read_early && !exit_early) continue;
const auto value=alu.graphics_registers.find(r);
const bool known=value!=alu.graphics_registers.end() && !value->second.empty() &&
(r==128 || state[r].numeric());
// A value seen only at an early exit may stay unbound there: a later read of it refuses.
if (!known && !read_early) continue;
if (!known)
refuse(head,r==128 ? string("mesh loop reads SCC before the body defines it, without a known entry value")
: "mesh loop carries a register without a numeric entry value: s"+to_string(r));
carried.insert(r);
slots.emplace_back(r,"%mesh_loop"+to_string(head)+(r==128 ? string("_scc") : "_s"+to_string(r)));
}
for (const auto &[r,slot] : slots) {
ir.setup<<" "<<slot<<" = alloca i32, align 4\n";
ir.body<<" store i32 "<<alu.graphics_registers.at(r)<<", i32* "<<slot<<", align 4\n";
}
ir.body<<" br label %mesh_loop"<<head<<"\nmesh_loop"<<head<<":\n";
for (auto r : written) if (!carried.count(r)) alu.write(r,{});
for (const auto &[r,slot] : slots) alu.write(r,ir.value("load i32, i32* "+slot+", align 4"));
group_flow=false;
loop=MeshLoop{head,back,std::move(slots)};
};
while (pc < code.size()) {
// All handled scalar branches are forward, so every predecessor is
// available when we reach its target. Funnel arbitrary fan-in into
Expand Down Expand Up @@ -1116,8 +1232,6 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
if (diamond_joined) {
const auto next=decode(code,pc,true,true);
const string name(next.name);
// SGPRs, M0, null, inline and literal constants, and SCC; not VCC, EXEC, VCCZ or EXECZ.
auto lane_free=[](unsigned s) { return s<106 || s==124 || s==125 || (s>=128 && s<=250) || s==253 || s==255; };
bool independent=false;
if (next.enc==Encoding::Smem) // opcodes 0-4: s_load_dword{,x2..x16}; 8-12: s_buffer_load_dword{,...}
independent=(next.op<=4 || (next.op>=8 && next.op<=12)) && next.dst+memory_count(next)<=106;
Expand All @@ -1127,20 +1241,12 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
independent=(name.rfind("s_cmp",0)==0 || name.rfind("s_bitcmp",0)==0) &&
lane_free(next.src[0]) && lane_free(next.src[1]);
else if (next.enc==Encoding::Sop1 || next.enc==Encoding::Sop2 || next.enc==Encoding::Sopk) {
// 32-bit scalar ALU only: no 64-bit mask forms, calls, register writes or relative moves.
static const std::set<string> salu{
"s_mov_b32","s_not_b32","s_brev_b32","s_movk_i32","s_addk_i32","s_mulk_i32",
"s_add_u32","s_sub_u32","s_add_i32","s_sub_i32","s_addc_u32","s_subb_u32","s_mul_i32",
"s_mul_hi_u32","s_mul_hi_i32","s_min_i32","s_min_u32","s_max_i32","s_max_u32","s_absdiff_i32",
"s_cselect_b32","s_and_b32","s_or_b32","s_xor_b32","s_andn2_b32","s_orn2_b32","s_nand_b32",
"s_nor_b32","s_xnor_b32","s_lshl_b32","s_lshr_b32","s_ashr_i32","s_bfm_b32","s_bfe_u32",
"s_bfe_i32","s_lshl1_add_u32","s_lshl2_add_u32","s_lshl3_add_u32","s_lshl4_add_u32",
"s_pack_ll_b32_b16","s_pack_lh_b32_b16","s_pack_hh_b32_b16"};
independent=salu.count(name) && next.dst<106 && lane_free(next.src[0]) &&
independent=scalar_alu32.count(name) && next.dst<106 && lane_free(next.src[0]) &&
(next.enc==Encoding::Sopk || lane_free(next.src[1]));
}
if (!independent) diamond_joined=false;
}
if (const auto edges=back_edges.find(pc); edges!=back_edges.end() && !loop) open_loop(edges->second);
program.at[pc]=0;
alu.graphics_pc = pc;
const auto raw = word(code,pc);
Expand Down Expand Up @@ -1608,6 +1714,22 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
exec_skip=Skip{pc,i.target(),save_edge(),wave_active.empty(),lane_active,wave_active};
ir.body<<" br i1 "<<(wave_active.empty() ? lane_active : wave_active)<<", label %mesh_work"<<pc<<", label %mesh_empty"<<pc
<<"\nmesh_empty"<<pc<<":\n br label %mesh_resume"<<pc<<"\nmesh_work"<<pc<<":\n";
} else if (loop && pc==loop->back) {
// The back edge: store the carried registers, then repeat or fall through to the exit. An
// unconditional back edge leaves only through the body's forward exits.
const bool always=i.name=="s_branch";
const auto take=always ? string{} : alu.cmp(i.name=="s_cbranch_scc0" ? "eq" : "ne",alu.read(128),"0");
for (const auto &[r,slot] : loop->slots)
ir.body<<" store i32 "<<alu.read(r)<<", i32* "<<slot<<", align 4\n";
const auto head=to_string(loop->head);
if (always) {
ir.body<<" br label %mesh_loop"<<head<<"\n";
live=false;
} else {
ir.body<<" br i1 "<<take<<", label %mesh_loop"<<head<<", label %mesh_loop_exit"<<head
<<"\nmesh_loop_exit"<<head<<":\n";
}
loop.reset();
} else if ((branch && saveexec_vcc_tests.count(i.next)) ||
i.name=="s_cbranch_scc0" || i.name=="s_cbranch_scc1" ||
i.name=="s_cbranch_vccz" || i.name=="s_cbranch_vccnz") {
Expand Down
Loading
Loading