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
31 changes: 24 additions & 7 deletions src/gfx1013_direct_mesh.inl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
const unsigned width = layout.wave32 ? 32 : 64;
const auto wave_lane = ir.binary("and",thread,to_string(width-1));
const auto wave_number = ir.binary("lshr",thread,layout.wave32 ? "5" : "6");
constexpr unsigned vcc_words=0x70000000, vcc_nonempty=vcc_words+2;
alu.graphics_input = [&](unsigned r) -> string {
refuse(alu.graphics_pc,"unproven mesh entry register " + to_string(r));
};
alu.after_graphics_write = [&](unsigned r) {
if (r<106) program.before.front()[r].type=alu.graphics_registers.at(r).empty() ?
Symbol::Type::unbound : Symbol::Type::unknown;
else if (r==106 || r==107) {
alu.write(vcc_words,{}); alu.write(vcc_words+1,{}); alu.write(vcc_nonempty,{});
}
};
auto &state=program.before.front();
auto inputs=layout.root_inputs;
Expand Down Expand Up @@ -144,7 +148,6 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
};
std::optional<Diamond> branch;
unsigned lane_else=0;
constexpr unsigned vcc_words=0x70000000, vcc_nonempty=vcc_words+2;
std::map<string,string> mask_parent;
auto covered_by=[&](string current,const string &ancestor) {
if (ancestor.empty()) return true;
Expand Down Expand Up @@ -212,6 +215,9 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
struct Skip { unsigned start,end; Edge entry; bool per_lane; string guard; };
std::vector<Skip> skip_parents;
std::optional<Skip> exec_skip;
auto exec_nonempty_known=[&] {
return full_exec || !wave_active.empty() || (exec_skip && exec_skip->guard==lane_active);
};
unsigned mask_generation=0x1000000;
auto merge_edges=[&](const Edge &first, const string &l, const string &r) {
auto phi=[&](const string &a,const string &b) {return ir.value("phi i32 [ "+a+", %"+l+" ], [ "+b+", %"+r+" ]");};
Expand Down Expand Up @@ -310,6 +316,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
auto now=alu.graphics_registers.find(r), old=exec_skip->entry.regs.find(r);
if (now!=alu.graphics_registers.end() && (old==exec_skip->entry.regs.end() || now->second!=old->second)) {
now->second.clear();
if (r==106 || r==107) alu.after_graphics_write(r);
if (r<128) state[r].type=Symbol::Type::unbound;
}
}
Expand Down Expand Up @@ -350,6 +357,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
};
if (differs(first.regs) || differs(alu.graphics_registers)) {
alu.graphics_registers[r].clear();
if (r==106 || r==107) alu.after_graphics_write(r);
if (r<106) state[r].type=Symbol::Type::unbound;
}
}
Expand All @@ -360,9 +368,6 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
alu.write(r,{});
if (r<state.size()) state[r].type=Symbol::Type::unbound;
}
if (lane_scalar_clobbers.count(106)) {
alu.write(vcc_words,{}); alu.write(vcc_words+1,{}); alu.write(vcc_nonempty,{});
}
lane_scalar_clobbers.clear();
branch.reset();
}
Expand Down Expand Up @@ -733,13 +738,25 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
auto pred=alu.comparison(i);
if (!full_exec) pred=ir.value("and i1 "+pred+", "+lane_active);
const auto bit=ir.value("zext i1 "+pred+" to i32");
string uniform_pred;
if (!i.subword) {
const auto lhs=uniform(alu.operand32(i,i.src[0])), rhs=uniform(alu.operand32(i,i.src[1]));
if (!lhs.empty() && !rhs.empty()) mask_uniform[bit]={lane_active,alu.comparison(i,lhs,rhs)};
if (!lhs.empty() && !rhs.empty()) {
uniform_pred=alu.comparison(i,lhs,rhs);
mask_uniform[bit]={lane_active,uniform_pred};
}
}
if (i.dst==106) {
alu.write(106,bit);
alu.write(vcc_words,{}); alu.write(vcc_words+1,{}); alu.write(vcc_nonempty,{});
if (!uniform_pred.empty() && exec_nonempty_known()) {
const auto any=wave_active.empty() ? uniform_pred : ir.value("and i1 "+uniform_pred+", "+wave_active);
alu.write(vcc_nonempty,ir.value("zext i1 "+any+" to i32"));
// The branch only needs nonemptiness. Expose mask words when EXEC's bits are known.
for (unsigned j=0;j<2;++j) {
const auto word=full_exec ? (j && layout.wave32 ? "0" : "4294967295") : exec_value[j];
if (!word.empty()) alu.write(vcc_words+j,alu.select(uniform_pred,word,"0"));
}
}
next[106].type=next[107].type=Symbol::Type::unbound;
} else {
alu.write(i.dst,{});
Expand Down Expand Up @@ -856,7 +873,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout
if (!lo.empty() && !hi.empty()) any=ir.binary("or",lo,hi);
else if (((i.src[0]==126) || (i.src[1]==126 && i.name=="s_and_b64")) && (i.name=="s_andn2_b64" || i.name=="s_and_b64") && mask_uniform.count(i.src[0]==126 ? b : a) &&
covered_by(lane_active,mask_uniform.at(i.src[0]==126 ? b : a).first) &&
(full_exec || !wave_active.empty() || (exec_skip && exec_skip->guard==lane_active))) {
exec_nonempty_known()) {
auto flag=mask_uniform.at(i.src[0]==126 ? b : a).second;
if (i.name=="s_andn2_b64") flag=ir.value("xor i1 "+flag+", true");
if (!wave_active.empty()) flag=ir.value("and i1 "+flag+", "+wave_active);
Expand Down
41 changes: 41 additions & 0 deletions tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Source-built mesh ISA, direct DXIL validation and fail-closed controls."""
from pathlib import Path
import re
import struct
import subprocess
import sys
Expand Down Expand Up @@ -128,6 +129,46 @@ def run(name, words, expected=None):
text = run('root-branch',words+controlled)
for marker in ('cbufferLoadLegacy','rawBufferLoad','phi i32','storeVertexOutput'):
assert marker in text, marker

# s20 = root0[0]; v_cmp_gt_u32_e64 vcc, s20, 6.
copy_root = [0xbe940315]
compare_root = [0xd4c4006a, 0x00010c14]
choose_output = [0xbf860002, 0xbe940381, 0xbf820001, 0xbe940382]
text = run('root-vcc-branch', copy_root+compare_root+choose_output+controlled)
assert 'cbufferLoadLegacy' in text and 'storeVertexOutput' in text
root_word = re.search(r'(%[\w.]+) = extractvalue %dx.types.CBufRet.i32 %[\w.]+, 0', text)
assert root_word, text
compares_root = r'(?P<cmp>%[\w.]+) = icmp u(?:gt|le) i32 ' + re.escape(root_word[1]) + r', 6\b'
branches_on_compare = r'br i1 (?P=cmp),'
branches_on_zero = (r'(?P<word>%[\w.]+) = zext i1 (?P=cmp) to i32.*?'
r'(?P<zero>%[\w.]+) = icmp eq i32 (?P=word), 0\b.*?br i1 (?P=zero),')
assert re.search(compares_root + r'.*?(?:' + branches_on_compare + '|' + branches_on_zero + ')',
text, re.DOTALL), text

vcc_refusal = 'mesh VCC branch lacks a proven guest whole-wave mask'
lane_id = program[3:5] # v0 = mbcnt_lo(-1, 0)
compare_lane = [0x7d880014] # v_cmp_gt_u32_e32 vcc, s20, v0
run('lane-vcc-branch', copy_root+lane_id+compare_lane+choose_output+controlled, vcc_refusal)
# v_cmpx_eq_u32 0, v0 narrows EXEC without proving wave nonemptiness.
# Restore full EXEC before the scalar branch to isolate the VCC check.
run('narrowed-vcc-branch', copy_root+lane_id+[0x7da40080]+compare_root+
[0xbefe04c1]+choose_output+controlled, vcc_refusal)
run('overwritten-vcc-branch', copy_root+lane_id+compare_root+compare_lane+
choose_output+controlled, vcc_refusal)

# Seed VCC so both edges have a value to merge. A VCC write inside
# a per-lane skip or diamond cannot supply a whole-wave branch mask.
seed_vcc = [0xbeea04c1] # s_mov_b64 vcc, -1
restore_full_exec = [0xbefe04c1]
run('skip-uniform-vcc', copy_root+lane_id+seed_vcc+[0x7da40080, 0xbf880002]+
compare_root+restore_full_exec+choose_output+controlled, vcc_refusal)

diamond_body = ([0xbe9e047e, 0x7d280014, 0xbf880002]+compare_root+
[0x8afe7e1e, 0xbf880001, 0xbf800000, 0xbefe041e]+choose_output)
wave_skip = [0xbf061414, 0x85a080c1, 0x87fe7e20, 0xbf880000 | len(diamond_body)]
run('diamond-known-vcc', copy_root+lane_id+seed_vcc+wave_skip+diamond_body+
restore_full_exec+controlled, vcc_refusal)

typed = [0xbe9e0317,0xbe9f0380, # copy the heap base; zero high address
0x8715ff15,31,0x8f158515, # (root0[0] & 31) * descriptor size
0xf408080f,0x2a000000, # load V# into s32:s35
Expand Down
Loading