The title now boots through every first-boot and menu screen at its own 30 fps, drawing with translated shaders, with input, sound, saves and loading served; it reaches the world, where the host GPU path still fails (see "Not working yet"). Loader and runtime - Crash reports unwind properly, name C++ throws and host GPU failures, carry the title's last debug lines and are appended to crashes.log. - Thread notices on our own threads, reentrant late DLL loading, exit in the console's order, relaunch on a launch-new-game request. - Offline guard: the title cannot reach the network (sockets, name lookups, WinHTTP, the media source resolver); by-name lookups and loads follow the import rules. - Every runtime area the title calls is served (networking hint, packages, store, saves, display, UI, speech stubs, user, async pool). Saves persist; an update is one rename and survives a kill at any step. - DirectStorage reads for real: shared worker pool, priorities, zlib and BCPack. BCPack is our own decoder; the format's constant tables ship in crates/victory-formats/src/bcpack-tables.bin (the asset policy's one exception, see docs/asset-policy.md). - GameInput from XInput pads and the keyboard, with a keyboard stand-in pad; console timers (1 ms) for the whole process. - Audio: XMA2 decoding on the emulated contexts, Opus on the decoder queues, the DSP interface as a stub; title memory placed below 2^40. - Console texture layouts (sizes, tiling, detiling) and the layout library's imports. GPU - Draws, mesh dispatches and compute dispatches run on the host with shaders translated at first use and cached on disk; descriptor heaps, views, root signatures, barriers and host texture states; rectangle lists; comparison samplers. - Title memory reaches the host through persistent mirrors that copy only pages written since the last copy; shader-written buffers are copied back. - Presentation with frame pacing per event kind; framerate in the window caption. Shaders - DXIL emission for vertex, pixel, compute and mesh programs from the console's native code, with a differential test suite against the PC compile. World set: mesh 8,191 of 8,506, vertex/pixel 10,617 of 12,346, compute 1,643 of 2,539; utility set complete. Build - Debugging aids (debug logs, frame capture, stack dumps, scripted keys, the host debug layer, the software adapter) exist only with the `diagnostics` feature; `[profile.diag]` keeps that build apart. Not working yet - In the world the host device is removed within seconds (descriptor handling fixes are in, unproven by a surviving run) and frames present black. - 64-bit image atomics, merged geometry stages and the BVH builder's own shaders do not translate; reverb is silent. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1117 lines
38 KiB
Rust
1117 lines
38 KiB
Rust
//! Resource tracking (decision 0003): where each descriptor a program uses
|
|
//! comes from, in terms of the root signature.
|
|
//!
|
|
//! Root parameters arrive in user SGPRs ([`RootParam`]): a descriptor table
|
|
//! as the 32-bit heap address of its range (the code sets the high half to
|
|
//! 0), a root descriptor as a 64-bit buffer address, root constants as
|
|
//! themselves. A root signature with dynamic resources (SM 6.6) adds the
|
|
//! 32-bit base addresses of both heaps; a descriptor is then loaded from
|
|
//! one of them at an index the code computes (from a root constant, say). The code loads table descriptors from heap memory with
|
|
//! `s_load_dword*`, and builds a buffer descriptor around a root
|
|
//! descriptor's address itself. Each descriptor operand and each scalar
|
|
//! memory address is traced through the SSA values back to those
|
|
//! parameters, by data flow: copies, phis that agree, and additions.
|
|
|
|
use crate::ShaderError;
|
|
use crate::gfx10::{Fields, NULL, Operand};
|
|
use crate::ir::{Inst, Program, Reg, Value};
|
|
use crate::native::{HwStage, RootParam, RootType};
|
|
|
|
/// An instruction: block and index in [`crate::ir::Block::insts`].
|
|
pub type Site = (usize, usize);
|
|
|
|
/// A byte address: a root parameter's, plus a constant and at most one
|
|
/// value the program computes (an index times a stride, say).
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct Address {
|
|
/// Index into the layout, or [`CODE`].
|
|
pub param: usize,
|
|
/// Added modulo 2^32 (with carry into the high half of a root
|
|
/// descriptor's address).
|
|
pub offset: u32,
|
|
pub dynamic: Option<Value>,
|
|
}
|
|
|
|
/// [`Address::param`] of the program's own code: a constant table the
|
|
/// compiler appends after the code and reads through `s_getpc_b64`; the
|
|
/// offset is then the byte offset in the code.
|
|
pub const CODE: usize = usize::MAX;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum Source {
|
|
/// Dwords `first..` of what the scalar load at `load` reads (see
|
|
/// [`Resources::scalar`] for its address).
|
|
Loaded { load: Site, first: u8 },
|
|
/// A buffer descriptor built around an address: `bits` OR-ed into
|
|
/// dword 1 (stride, swizzle), dwords 2 and 3 as given.
|
|
Built {
|
|
address: Address,
|
|
bits: u32,
|
|
words: [u32; 2],
|
|
},
|
|
/// Fixed in the code (an immediate sampler, say).
|
|
Constant(Vec<u32>),
|
|
/// Chosen by control flow: one source per predecessor of `block`, in
|
|
/// [`crate::ir::Block::preds`] order.
|
|
Phi { block: usize, args: Vec<Source> },
|
|
}
|
|
|
|
impl Source {
|
|
/// The sources this one picks from (itself unless a phi).
|
|
pub fn leaves(&self) -> Vec<&Source> {
|
|
match self {
|
|
Source::Phi { args, .. } => args.iter().flat_map(Source::leaves).collect(),
|
|
s => vec![s],
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum Role {
|
|
/// Image, buffer or `s_buffer_load` resource.
|
|
Resource,
|
|
Sampler,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct Descriptor {
|
|
pub site: Site,
|
|
pub role: Role,
|
|
pub source: Source,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
pub struct Resources {
|
|
/// Every scalar memory access by address (`s_load_*`, `s_store_*`), and
|
|
/// every global memory access (`global_*`, the dynamic term per lane).
|
|
pub scalar: Vec<(Site, Address)>,
|
|
pub descriptors: Vec<Descriptor>,
|
|
/// User SGPRs holding root parameters: value on entry, layout index,
|
|
/// dword within the parameter.
|
|
pub params: Vec<(Value, usize, u32)>,
|
|
}
|
|
|
|
/// Traces every descriptor and scalar address of `p`, which runs as
|
|
/// `stage`, to the root parameters in `layout`. Fails on the first one it
|
|
/// cannot trace.
|
|
pub fn track(p: &Program, layout: &[RootParam], stage: HwStage) -> Result<Resources, ShaderError> {
|
|
let t = Tracer::new(p, layout, stage)?;
|
|
let mut out = Resources {
|
|
params: p
|
|
.inputs
|
|
.iter()
|
|
.filter_map(|&(reg, v)| {
|
|
let (param, dword) = t.param_of(reg)?;
|
|
Some((v, param, dword))
|
|
})
|
|
.collect(),
|
|
..Resources::default()
|
|
};
|
|
for (b, block) in p.blocks.iter().enumerate() {
|
|
for (i, inst) in block.insts.iter().enumerate() {
|
|
let site = (b, i);
|
|
let fail = |what| ShaderError::Unsupported {
|
|
offset: inst.machine.offset,
|
|
what,
|
|
};
|
|
let mut descriptor = |op: &Operand, role| -> Result<(), ShaderError> {
|
|
let source = t.descriptor(inst, op).map_err(fail)?;
|
|
out.descriptors.push(Descriptor { site, role, source });
|
|
Ok(())
|
|
};
|
|
match &inst.fields {
|
|
Fields::Image { rsrc, sampler, .. } => {
|
|
descriptor(rsrc, Role::Resource)?;
|
|
if let Some(s) = sampler {
|
|
descriptor(s, Role::Sampler)?;
|
|
}
|
|
}
|
|
Fields::Buffer { rsrc, .. } => descriptor(rsrc, Role::Resource)?,
|
|
Fields::Smem { base, .. } if inst.name().starts_with("s_buffer_") => {
|
|
descriptor(base, Role::Resource)?;
|
|
}
|
|
Fields::Smem {
|
|
base,
|
|
offset,
|
|
soffset,
|
|
..
|
|
} if inst.name().contains("_load_") || inst.name().contains("_store_") => {
|
|
let address = t.smem_address(inst, base, *offset, soffset).map_err(fail)?;
|
|
out.scalar.push((site, address));
|
|
}
|
|
Fields::Flat {
|
|
addr,
|
|
saddr,
|
|
offset,
|
|
..
|
|
} if inst.name().starts_with("global_") => {
|
|
let address = t
|
|
.flat_address(inst, addr.as_ref(), saddr.as_ref(), *offset)
|
|
.map_err(fail)?;
|
|
out.scalar.push((site, address));
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// A dword after copy propagation.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum Term {
|
|
Const(u32),
|
|
Value(Value),
|
|
}
|
|
|
|
/// A 32-bit sum: optional root parameter (as its first dword), constant,
|
|
/// optional computed term.
|
|
struct Sum {
|
|
base: Option<Value>,
|
|
offset: u32,
|
|
dynamic: Option<Value>,
|
|
}
|
|
|
|
struct Tracer<'a> {
|
|
p: &'a Program,
|
|
layout: &'a [RootParam],
|
|
stage: HwStage,
|
|
/// Defining instruction per value (phis and inputs have none).
|
|
defs: Vec<Option<Site>>,
|
|
/// Block and index of each phi value.
|
|
phis: Vec<Option<(usize, usize)>>,
|
|
/// Register per input value.
|
|
inputs: Vec<Option<Reg>>,
|
|
origin: Vec<Term>,
|
|
}
|
|
|
|
const MAX_ROUNDS: usize = 64;
|
|
const MAX_JOINS: usize = 8;
|
|
|
|
impl<'a> Tracer<'a> {
|
|
fn new(p: &'a Program, layout: &'a [RootParam], stage: HwStage) -> Result<Self, ShaderError> {
|
|
let all = || {
|
|
p.inputs
|
|
.iter()
|
|
.map(|i| i.1)
|
|
.chain(p.blocks.iter().flat_map(|b| {
|
|
b.phis
|
|
.iter()
|
|
.map(|phi| phi.value)
|
|
.chain(b.insts.iter().flat_map(|i| i.writes.iter().map(|w| w.1)))
|
|
}))
|
|
};
|
|
let n = all().map(|v| v.0 as usize + 1).max().unwrap_or(0);
|
|
let mut defs = vec![None; n];
|
|
let mut phis = vec![None; n];
|
|
let mut inputs = vec![None; n];
|
|
for &(reg, v) in &p.inputs {
|
|
inputs[v.0 as usize] = Some(reg);
|
|
}
|
|
for (b, block) in p.blocks.iter().enumerate() {
|
|
for (i, phi) in block.phis.iter().enumerate() {
|
|
phis[phi.value.0 as usize] = Some((b, i));
|
|
}
|
|
for (i, inst) in block.insts.iter().enumerate() {
|
|
for &(_, v) in &inst.writes {
|
|
defs[v.0 as usize] = Some((b, i));
|
|
}
|
|
}
|
|
}
|
|
let mut t = Tracer {
|
|
p,
|
|
layout,
|
|
stage,
|
|
defs,
|
|
phis,
|
|
inputs,
|
|
origin: Vec::new(),
|
|
};
|
|
t.origin = t.propagate_copies(n)?;
|
|
Ok(t)
|
|
}
|
|
|
|
/// Optimistic copy propagation: a move's result is its source's
|
|
/// origin, a phi's the one origin its arguments agree on.
|
|
fn propagate_copies(&self, n: usize) -> Result<Vec<Term>, ShaderError> {
|
|
let mut o: Vec<Option<Term>> = vec![None; n];
|
|
for &(_, v) in &self.p.inputs {
|
|
o[v.0 as usize] = Some(Term::Value(v));
|
|
}
|
|
let order = crate::ir::reverse_postorder(&self.p.blocks);
|
|
for _ in 0..MAX_ROUNDS {
|
|
let mut changed = false;
|
|
for &b in &order {
|
|
let block = &self.p.blocks[b];
|
|
for phi in &block.phis {
|
|
let own = Term::Value(phi.value);
|
|
let mut args = phi
|
|
.args
|
|
.iter()
|
|
.filter_map(|a| o[a.0 as usize])
|
|
.filter(|&t| t != own);
|
|
let new = args
|
|
.next()
|
|
.map(|first| if args.all(|t| t == first) { first } else { own });
|
|
if new.is_some() && new != o[phi.value.0 as usize] {
|
|
o[phi.value.0 as usize] = new;
|
|
changed = true;
|
|
}
|
|
}
|
|
for inst in &block.insts {
|
|
for (k, &(_, v)) in inst.writes.iter().enumerate() {
|
|
let new = match copy_source(inst, k).or_else(|| self.restored(inst)) {
|
|
Some(Moved::Const(c)) => Some(Term::Const(c)),
|
|
Some(Moved::Value(s)) => o[s.0 as usize],
|
|
None => Some(Term::Value(v)),
|
|
};
|
|
if new.is_some() && new != o[v.0 as usize] {
|
|
o[v.0 as usize] = new;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if !changed {
|
|
return Ok(o
|
|
.into_iter()
|
|
.enumerate()
|
|
.map(|(v, t)| t.unwrap_or(Term::Value(Value(v as u32))))
|
|
.collect());
|
|
}
|
|
}
|
|
Err(ShaderError::Unsupported {
|
|
offset: 0,
|
|
what: "copies that do not settle",
|
|
})
|
|
}
|
|
|
|
fn inst(&self, (b, i): Site) -> &'a Inst {
|
|
&self.p.blocks[b].insts[i]
|
|
}
|
|
|
|
fn def(&self, v: Value) -> Option<&'a Inst> {
|
|
self.defs[v.0 as usize].map(|s| self.inst(s))
|
|
}
|
|
|
|
fn origin(&self, v: Value) -> Term {
|
|
self.origin[v.0 as usize]
|
|
}
|
|
|
|
/// The origin of a one-dword operand as `inst` reads it.
|
|
fn term(&self, inst: &Inst, op: &Operand) -> Option<Term> {
|
|
match *op {
|
|
Operand::Scalar { code: NULL, .. } => Some(Term::Const(0)),
|
|
Operand::Scalar { code, .. } => Some(self.origin(inst.read(Reg::S(code))?)),
|
|
Operand::Const(c) => inline(c).map(Term::Const),
|
|
Operand::Literal(x) => Some(Term::Const(x)),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Layout index and dword of the root parameter `reg` holds on entry.
|
|
fn param_of(&self, reg: Reg) -> Option<(usize, u32)> {
|
|
let Reg::S(code) = reg else { return None };
|
|
self.layout.iter().enumerate().find_map(|(i, p)| {
|
|
let first = self.stage.user_sgpr(p.user_data);
|
|
let dword = u32::from(code).checked_sub(first.into())?;
|
|
(dword < p.sgprs).then_some((i, dword))
|
|
})
|
|
}
|
|
|
|
/// The SOP2 operands of `v` if an `s_add` with one of `names` wrote it.
|
|
fn sop2(&self, v: Value, names: &[&str]) -> Option<(&'a Inst, [Term; 2])> {
|
|
let inst = self.def(v)?;
|
|
if !names.contains(&inst.name()) || inst.writes.first()?.1 != v {
|
|
return None;
|
|
}
|
|
let Fields::Sop { src, .. } = &inst.fields else {
|
|
return None;
|
|
};
|
|
Some((
|
|
inst,
|
|
[
|
|
self.term(inst, src.first()?)?,
|
|
self.term(inst, src.get(1)?)?,
|
|
],
|
|
))
|
|
}
|
|
|
|
fn sum(&self, t: Term) -> Sum {
|
|
let v = match t {
|
|
Term::Const(c) => {
|
|
return Sum {
|
|
base: None,
|
|
offset: c,
|
|
dynamic: None,
|
|
};
|
|
}
|
|
Term::Value(v) => v,
|
|
};
|
|
let dynamic = Sum {
|
|
base: None,
|
|
offset: 0,
|
|
dynamic: Some(v),
|
|
};
|
|
if let Some(reg) = self.inputs[v.0 as usize] {
|
|
return match self.param_of(reg) {
|
|
Some((_, 0)) => Sum {
|
|
base: Some(v),
|
|
offset: 0,
|
|
dynamic: None,
|
|
},
|
|
_ => dynamic,
|
|
};
|
|
}
|
|
if self.getpc(v).is_some() {
|
|
return Sum {
|
|
base: Some(v),
|
|
offset: 0,
|
|
dynamic: None,
|
|
};
|
|
}
|
|
let Some([a, b]) = self
|
|
.sop2(v, &["s_add_u32", "s_add_i32"])
|
|
.map(|(_, t)| t)
|
|
.or_else(|| self.vadd(v))
|
|
else {
|
|
return dynamic;
|
|
};
|
|
let (x, y) = (self.sum(a), self.sum(b));
|
|
if x.base.is_some() && y.base.is_some() || x.dynamic.is_some() && y.dynamic.is_some() {
|
|
return dynamic;
|
|
}
|
|
Sum {
|
|
base: x.base.or(y.base),
|
|
offset: x.offset.wrapping_add(y.offset),
|
|
dynamic: x.dynamic.or(y.dynamic),
|
|
}
|
|
}
|
|
|
|
/// The `s_getpc_b64` writing `lo` as its low dword.
|
|
fn getpc(&self, lo: Value) -> Option<&'a Inst> {
|
|
let inst = self.def(lo)?;
|
|
(inst.name() == "s_getpc_b64" && inst.writes.first().is_some_and(|w| w.1 == lo))
|
|
.then_some(inst)
|
|
}
|
|
|
|
/// The operands of a VALU addition (`v_add_co_u32`, `v_add_nc_u32`)
|
|
/// writing `v`, through VGPR copies of scalars.
|
|
fn vadd(&self, v: Value) -> Option<[Term; 2]> {
|
|
let inst = self.def(v)?;
|
|
let Fields::Valu(f) = &inst.fields else {
|
|
return None;
|
|
};
|
|
if !matches!(inst.name(), "v_add_co_u32" | "v_add_nc_u32")
|
|
|| f.dst.is_none_or(|d| !matches!(d, Operand::Vector { .. }))
|
|
|| inst.write(Reg::V(match f.dst? {
|
|
Operand::Vector { reg, .. } => reg,
|
|
_ => return None,
|
|
})) != Some(v)
|
|
|| f.abs != 0
|
|
|| f.neg != 0
|
|
|| f.sdwa.is_some()
|
|
|| f.dpp.is_some()
|
|
{
|
|
return None;
|
|
}
|
|
Some([
|
|
self.vterm(inst, f.src.first()?)?,
|
|
self.vterm(inst, f.src.get(1)?)?,
|
|
])
|
|
}
|
|
|
|
/// The origin of a VALU operand.
|
|
fn vterm(&self, inst: &Inst, op: &Operand) -> Option<Term> {
|
|
match *op {
|
|
Operand::Vector { reg, .. } => Some(self.vorigin(inst.read(Reg::V(reg))?)),
|
|
op => self.term(inst, &op),
|
|
}
|
|
}
|
|
|
|
/// A VGPR value's origin through `v_mov_b32` copies.
|
|
fn vorigin(&self, v: Value) -> Term {
|
|
if let Some(inst) = self.def(v)
|
|
&& inst.name() == "v_mov_b32"
|
|
&& let Fields::Valu(f) = &inst.fields
|
|
&& f.abs == 0
|
|
&& f.neg == 0
|
|
&& f.sdwa.is_none()
|
|
&& f.dpp.is_none()
|
|
&& let Some(t) = f.src.first().and_then(|s| self.vterm(inst, s))
|
|
{
|
|
return t;
|
|
}
|
|
self.origin(v)
|
|
}
|
|
|
|
/// A global memory address: an SGPR pair plus a VGPR offset, or a VGPR
|
|
/// pair.
|
|
fn flat_address(
|
|
&self,
|
|
inst: &Inst,
|
|
addr: Option<&Operand>,
|
|
saddr: Option<&Operand>,
|
|
offset: i16,
|
|
) -> Result<Address, &'static str> {
|
|
let Some(&Operand::Vector { reg, .. }) = addr else {
|
|
return Err("a global address not in VGPRs");
|
|
};
|
|
let off = offset as i32 as u32;
|
|
match saddr {
|
|
Some(&Operand::Scalar { code, .. }) if code != NULL => {
|
|
let word = |k| Some(self.origin(inst.read(Reg::S(code + k))?));
|
|
let (Some(lo), Some(hi)) = (word(0), word(1)) else {
|
|
return Err("an address not in SGPRs");
|
|
};
|
|
let v = inst.read(Reg::V(reg)).ok_or("a global address")?;
|
|
let mut extra = self.sum(self.vorigin(v));
|
|
if extra.base.is_some() {
|
|
return Err("an offset from a root parameter");
|
|
}
|
|
extra.offset = extra.offset.wrapping_add(off);
|
|
self.address(lo, hi, extra)
|
|
}
|
|
_ => {
|
|
let word = |k| inst.read(Reg::V(reg + k)).map(|v| self.vorigin(v));
|
|
let (Some(lo), Some(hi)) = (word(0), word(1)) else {
|
|
return Err("a global address");
|
|
};
|
|
self.address(
|
|
lo,
|
|
hi,
|
|
Sum {
|
|
base: None,
|
|
offset: off,
|
|
dynamic: None,
|
|
},
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The address `lo` and `hi` hold, plus `extra` (an instruction's
|
|
/// immediate and offset register).
|
|
fn address(&self, lo: Term, hi: Term, extra: Sum) -> Result<Address, &'static str> {
|
|
let s = self.sum(lo);
|
|
if s.dynamic.is_some() && extra.dynamic.is_some() {
|
|
return Err("an address with two computed terms");
|
|
}
|
|
let base = s.base.ok_or_else(|| self.why_no_base(s.dynamic))?;
|
|
if let Some(pc) = self.getpc(base) {
|
|
// A table in the code: the base is the next instruction's offset.
|
|
let high = pc.writes.get(1).map(|w| w.1);
|
|
if !self.high_half(hi, high) && !high.is_some_and(|h| self.carried(hi, h)) {
|
|
return Err("an address with an untracked high half");
|
|
}
|
|
return Ok(Address {
|
|
param: CODE,
|
|
offset: (pc.machine.offset as u32 + 4)
|
|
.wrapping_add(s.offset)
|
|
.wrapping_add(extra.offset),
|
|
dynamic: s.dynamic.or(extra.dynamic),
|
|
});
|
|
}
|
|
let reg = self.inputs[base.0 as usize].ok_or("an address not from a root parameter")?;
|
|
let (param, _) = self
|
|
.param_of(reg)
|
|
.ok_or("an address not from a root parameter")?;
|
|
let high = match self.layout[param].root {
|
|
RootType::Table | RootType::ResourceHeap | RootType::SamplerHeap => None,
|
|
RootType::Descriptor => {
|
|
let high = Reg::S(self.stage.user_sgpr(self.layout[param].user_data) + 1);
|
|
let input = self.p.inputs.iter().find(|i| i.0 == high);
|
|
Some(input.ok_or("a root descriptor without its high half")?.1)
|
|
}
|
|
_ => return Err("an address from a root parameter that is no address"),
|
|
};
|
|
// shortcut: with a computed term, a high half that adds anything to the
|
|
// base's is taken as the carry of that term (offsets below 4 GB).
|
|
let dynamic = s.dynamic.or(extra.dynamic);
|
|
if !self.high_half(hi, high)
|
|
&& !(dynamic.is_some() && high.is_some_and(|h| self.carried(hi, h)))
|
|
{
|
|
return Err("an address with an untracked high half");
|
|
}
|
|
Ok(Address {
|
|
param,
|
|
offset: s.offset.wrapping_add(extra.offset),
|
|
dynamic: s.dynamic.or(extra.dynamic),
|
|
})
|
|
}
|
|
|
|
/// `hi` is `high` (0 for a table), possibly plus a carry.
|
|
fn high_half(&self, hi: Term, high: Option<Value>) -> bool {
|
|
let want = high.map_or(Term::Const(0), Term::Value);
|
|
hi == want
|
|
|| matches!(hi, Term::Value(v) if self.sop2(v, &["s_addc_u32"]).is_some_and(
|
|
|(_, [a, b])| a == want && b == Term::Const(0) || b == want && a == Term::Const(0)
|
|
))
|
|
}
|
|
|
|
/// `hi` adds something (with carry) to `high`: `s_addc_u32` or
|
|
/// `v_add_co_ci_u32` with `high` as an operand.
|
|
fn carried(&self, hi: Term, high: Value) -> bool {
|
|
let Term::Value(v) = hi else { return false };
|
|
if v == high {
|
|
return true;
|
|
}
|
|
if let Some((_, [a, b])) = self.sop2(v, &["s_addc_u32"]) {
|
|
return self.carried(a, high) || self.carried(b, high);
|
|
}
|
|
let Some(inst) = self.def(v) else {
|
|
return false;
|
|
};
|
|
let Fields::Valu(f) = &inst.fields else {
|
|
return false;
|
|
};
|
|
inst.name() == "v_add_co_ci_u32"
|
|
&& f.src[..2.min(f.src.len())]
|
|
.iter()
|
|
.any(|o| self.vterm(inst, o).is_some_and(|t| self.carried(t, high)))
|
|
}
|
|
|
|
fn why_no_base(&self, dynamic: Option<Value>) -> &'static str {
|
|
let Some(v) = dynamic else {
|
|
return "a constant address";
|
|
};
|
|
if self.inputs[v.0 as usize].is_some() {
|
|
return "an address in an SGPR that is no root parameter";
|
|
}
|
|
if self
|
|
.leaves(v)
|
|
.any(|l| self.def(l).is_some_and(|i| i.name() == "s_getpc_b64"))
|
|
{
|
|
return "an address relative to the program counter";
|
|
}
|
|
match self.def(v).map(|i| i.name()) {
|
|
Some(n) if n.starts_with("s_load_") => "an address loaded from memory",
|
|
None => "an address from a phi",
|
|
_ => "an address not from a root parameter",
|
|
}
|
|
}
|
|
|
|
/// The terms of the additions that compute `v`.
|
|
fn leaves(&self, v: Value) -> Box<dyn Iterator<Item = Value> + '_> {
|
|
match self.sop2(v, &["s_add_u32", "s_add_i32"]) {
|
|
Some((_, terms)) => Box::new(terms.into_iter().flat_map(move |t| match t {
|
|
Term::Value(x) => self.leaves(x),
|
|
Term::Const(_) => Box::new(std::iter::empty()),
|
|
})),
|
|
None => Box::new(std::iter::once(v)),
|
|
}
|
|
}
|
|
|
|
fn smem_address(
|
|
&self,
|
|
inst: &Inst,
|
|
base: &Operand,
|
|
offset: i32,
|
|
soffset: &Operand,
|
|
) -> Result<Address, &'static str> {
|
|
let Operand::Scalar { code, .. } = *base else {
|
|
return Err("an address not in SGPRs");
|
|
};
|
|
let word = |k| Some(self.origin(inst.read(Reg::S(code + k))?));
|
|
let (Some(lo), Some(hi)) = (word(0), word(1)) else {
|
|
return Err("an address not in SGPRs");
|
|
};
|
|
let mut extra = self.sum(
|
|
self.term(inst, soffset)
|
|
.ok_or("an unknown offset operand")?,
|
|
);
|
|
if extra.base.is_some() {
|
|
return Err("an offset from a root parameter");
|
|
}
|
|
extra.offset = extra.offset.wrapping_add(offset as u32);
|
|
self.address(lo, hi, extra)
|
|
}
|
|
|
|
fn descriptor(&self, inst: &Inst, op: &Operand) -> Result<Source, &'static str> {
|
|
let Operand::Scalar { code, dwords } = *op else {
|
|
return Err("a descriptor not in SGPRs");
|
|
};
|
|
let words = (0..dwords)
|
|
.map(|k| inst.read(Reg::S(code + k)).map(|v| self.origin(v)))
|
|
.collect::<Option<Vec<_>>>()
|
|
.ok_or("a descriptor not in SGPRs")?;
|
|
self.classify(&words, &mut Vec::new())
|
|
}
|
|
|
|
/// The source of a descriptor's words; `joins` are the phi blocks
|
|
/// being resolved.
|
|
fn classify(&self, words: &[Term], joins: &mut Vec<usize>) -> Result<Source, &'static str> {
|
|
if let Some(source) = self.phi(words, joins)? {
|
|
return Ok(source);
|
|
}
|
|
if let Some(consts) = words
|
|
.iter()
|
|
.map(|w| match *w {
|
|
Term::Const(c) => Some(c),
|
|
Term::Value(_) => None,
|
|
})
|
|
.collect::<Option<Vec<_>>>()
|
|
{
|
|
return Ok(Source::Constant(consts));
|
|
}
|
|
if let Some(source) = self.loaded(words) {
|
|
return Ok(source);
|
|
}
|
|
if let [lo, hi, Term::Const(w2), Term::Const(w3)] = *words {
|
|
let (mut hi, mut bits) = (hi, 0);
|
|
while let Term::Value(v) = hi
|
|
&& let Some((x, more)) = self.set_bits(v)
|
|
{
|
|
(hi, bits) = (x, bits | more);
|
|
}
|
|
let address = self.address(
|
|
lo,
|
|
hi,
|
|
Sum {
|
|
base: None,
|
|
offset: 0,
|
|
dynamic: None,
|
|
},
|
|
)?;
|
|
return Ok(Source::Built {
|
|
address,
|
|
bits,
|
|
words: [w2, w3],
|
|
});
|
|
}
|
|
let loaded = |w: &Term| {
|
|
matches!(*w, Term::Value(v)
|
|
if self.def(v).is_some_and(|i| i.name().starts_with("s_load_")))
|
|
};
|
|
Err(if words.iter().any(loaded) {
|
|
"a descriptor changed after loading"
|
|
} else {
|
|
"a descriptor neither loaded nor built"
|
|
})
|
|
}
|
|
|
|
/// Words that phis of one block join, traced per predecessor (words
|
|
/// defined before the join hold on every edge).
|
|
fn phi(&self, words: &[Term], joins: &mut Vec<usize>) -> Result<Option<Source>, &'static str> {
|
|
let phi_of = |w: &Term| match *w {
|
|
Term::Value(v) => self.phis[v.0 as usize],
|
|
Term::Const(_) => None,
|
|
};
|
|
let Some((block, _)) = words.iter().find_map(phi_of) else {
|
|
return Ok(None);
|
|
};
|
|
if words.iter().filter_map(phi_of).any(|(b, _)| b != block) {
|
|
return Err("a descriptor joined in several blocks");
|
|
}
|
|
if joins.contains(&block) {
|
|
return Err("a descriptor that changes in a loop");
|
|
}
|
|
// Each level multiplies the sources by the predecessor count.
|
|
if joins.len() >= MAX_JOINS {
|
|
return Err("a descriptor joined too often");
|
|
}
|
|
joins.push(block);
|
|
let phis = &self.p.blocks[block].phis;
|
|
let args = (0..self.p.blocks[block].preds.len())
|
|
.map(|j| {
|
|
let edge: Vec<Term> = words
|
|
.iter()
|
|
.map(|w| match phi_of(w) {
|
|
Some((_, i)) => self.origin(phis[i].args[j]),
|
|
None => *w,
|
|
})
|
|
.collect();
|
|
self.classify(&edge, joins)
|
|
})
|
|
.collect::<Result<_, _>>();
|
|
joins.pop();
|
|
Ok(Some(Source::Phi { block, args: args? }))
|
|
}
|
|
|
|
/// `v` as some other dword with constant bits set.
|
|
fn set_bits(&self, v: Value) -> Option<(Term, u32)> {
|
|
if let Some((_, [x, Term::Const(bits)] | [Term::Const(bits), x])) =
|
|
self.sop2(v, &["s_or_b32"])
|
|
{
|
|
return Some((x, bits));
|
|
}
|
|
let inst = self.def(v)?;
|
|
let Fields::Sop {
|
|
dst: Some(Operand::Scalar { code, .. }),
|
|
src,
|
|
..
|
|
} = &inst.fields
|
|
else {
|
|
return None;
|
|
};
|
|
match (inst.name(), self.term(inst, src.first()?)?) {
|
|
("s_bitset1_b32", Term::Const(bit)) => {
|
|
Some((self.origin(inst.read(Reg::S(*code))?), 1 << (bit & 31)))
|
|
}
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Consecutive dwords of one scalar load.
|
|
fn loaded(&self, words: &[Term]) -> Option<Source> {
|
|
let Term::Value(v0) = words[0] else {
|
|
return None;
|
|
};
|
|
let load = self.defs[v0.0 as usize]?;
|
|
let inst = self.inst(load);
|
|
if !inst.name().starts_with("s_load_") {
|
|
return None;
|
|
}
|
|
let first = inst.writes.iter().position(|w| w.1 == v0)?;
|
|
let same = words.iter().enumerate().all(|(k, w)| {
|
|
inst.writes
|
|
.get(first + k)
|
|
.is_some_and(|x| *w == Term::Value(x.1))
|
|
});
|
|
same.then_some(Source::Loaded {
|
|
load,
|
|
first: first as u8,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
enum Moved {
|
|
Const(u32),
|
|
Value(Value),
|
|
}
|
|
|
|
impl Tracer<'_> {
|
|
/// For `v_readlane_b32` of a constant lane: the SGPR spilled to that
|
|
/// lane, found by following the VGPR's `v_writelane_b32` writes.
|
|
fn restored(&self, inst: &Inst) -> Option<Moved> {
|
|
let Fields::Valu(v) = &inst.fields else {
|
|
return None;
|
|
};
|
|
if inst.name() != "v_readlane_b32" {
|
|
return None;
|
|
}
|
|
let [Operand::Vector { reg, .. }, lane] = v.src[..] else {
|
|
return None;
|
|
};
|
|
self.spilled(inst.read(Reg::V(reg))?, lane_of(lane)?, &mut Vec::new())
|
|
.ok()
|
|
.flatten()
|
|
}
|
|
|
|
/// What lane `lane` of VGPR value `v` holds: `Ok(None)` for a phi in
|
|
/// `seen` (already being resolved), `Err` if not a spilled SGPR.
|
|
fn spilled(&self, v: Value, lane: u32, seen: &mut Vec<Value>) -> Result<Option<Moved>, ()> {
|
|
if let Some((b, i)) = self.phis[v.0 as usize] {
|
|
if seen.contains(&v) {
|
|
return Ok(None);
|
|
}
|
|
seen.push(v);
|
|
let mut found = None;
|
|
for &a in &self.p.blocks[b].phis[i].args {
|
|
if let Some(m) = self.spilled(a, lane, seen)? {
|
|
if found.is_some_and(|f| f != m) {
|
|
return Err(());
|
|
}
|
|
found = Some(m);
|
|
}
|
|
}
|
|
return Ok(found);
|
|
}
|
|
let inst = self.def(v).ok_or(())?;
|
|
let Fields::Valu(w) = &inst.fields else {
|
|
return Err(());
|
|
};
|
|
let (Some(Operand::Vector { reg, .. }), [value, at]) = (w.dst, &w.src[..]) else {
|
|
return Err(());
|
|
};
|
|
if inst.name() != "v_writelane_b32" {
|
|
return Err(());
|
|
}
|
|
if lane_of(*at).ok_or(())? != lane {
|
|
return self.spilled(inst.read(Reg::V(reg)).ok_or(())?, lane, seen);
|
|
}
|
|
match *value {
|
|
Operand::Scalar { code, .. } if code != NULL => {
|
|
inst.read(Reg::S(code)).map(Moved::Value)
|
|
}
|
|
Operand::Literal(x) => Some(Moved::Const(x)),
|
|
Operand::Const(c) => inline(c).map(Moved::Const),
|
|
_ => None,
|
|
}
|
|
.map(Some)
|
|
.ok_or(())
|
|
}
|
|
}
|
|
|
|
/// A lane select given as a constant.
|
|
fn lane_of(op: Operand) -> Option<u32> {
|
|
match op {
|
|
Operand::Const(c) => inline(c),
|
|
Operand::Literal(x) => Some(x),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// What dword `k` of a move's result copies.
|
|
fn copy_source(inst: &Inst, k: usize) -> Option<Moved> {
|
|
let Fields::Sop { src, imm, .. } = &inst.fields else {
|
|
return None;
|
|
};
|
|
if inst.name() == "s_movk_i32" {
|
|
return Some(Moved::Const(i32::from((*imm)? as i16) as u32));
|
|
}
|
|
let src = src.first()?;
|
|
match (inst.name(), *src) {
|
|
("s_mov_b32" | "s_mov_b64", Operand::Scalar { code, .. }) if code != NULL => {
|
|
inst.read(Reg::S(code + k as u8)).map(Moved::Value)
|
|
}
|
|
("s_mov_b32" | "s_mov_b64", Operand::Scalar { .. }) => Some(Moved::Const(0)),
|
|
("s_mov_b32", Operand::Literal(x)) => Some(Moved::Const(x)),
|
|
("s_mov_b32", Operand::Const(c)) => inline(c).map(Moved::Const),
|
|
// shortcut: only the one-operand ways the compiler makes a constant
|
|
// that has no inline form; fold more if descriptors need it.
|
|
("s_brev_b32", Operand::Const(c)) => inline(c).map(|x| Moved::Const(x.reverse_bits())),
|
|
("s_not_b32", Operand::Const(c)) => inline(c).map(|x| Moved::Const(!x)),
|
|
// 64-bit inline integers are sign-extended.
|
|
("s_mov_b64", Operand::Const(c @ 128..=208)) => {
|
|
let low = inline(c)?;
|
|
Some(Moved::Const(if k == 0 {
|
|
low
|
|
} else {
|
|
((low as i32) >> 31) as u32
|
|
}))
|
|
}
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// The 32-bit value of an inline constant operand.
|
|
pub(crate) fn inline(code: u8) -> Option<u32> {
|
|
let float = |f: f32| Some(f.to_bits());
|
|
match code {
|
|
128..=192 => Some(u32::from(code) - 128),
|
|
193..=208 => Some((192 - i32::from(code)) as u32),
|
|
240 => float(0.5),
|
|
241 => float(-0.5),
|
|
242 => float(1.0),
|
|
243 => float(-1.0),
|
|
244 => float(2.0),
|
|
245 => float(-2.0),
|
|
246 => float(4.0),
|
|
247 => float(-4.0),
|
|
248 => Some(0x3E22_F983),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::native::RootType::*;
|
|
|
|
const S_ENDPGM: u32 = 0xBF81_0000;
|
|
|
|
fn track_words(words: &[u32], layout: &[RootParam], stage: HwStage) -> Resources {
|
|
let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
|
|
track(&crate::ir::build(&code, false, 256).unwrap(), layout, stage).unwrap()
|
|
}
|
|
|
|
fn param(root: RootType, user_data: u8, sgprs: u32) -> RootParam {
|
|
RootParam {
|
|
root,
|
|
table_offset_dwords: 0,
|
|
sgprs,
|
|
base_register: 0,
|
|
user_data,
|
|
slot: 0,
|
|
}
|
|
}
|
|
|
|
fn at(param: usize, offset: u32) -> Address {
|
|
Address {
|
|
param,
|
|
offset,
|
|
dynamic: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn table_descriptors_are_loaded_from_their_range() {
|
|
let r = track_words(
|
|
&[
|
|
0xBE82_0309, // s_mov_b32 s2, s9
|
|
0xBE89_0380, // s_mov_b32 s9, 0
|
|
0xBE83_0380, // s_mov_b32 s3, 0
|
|
0xF408_0204,
|
|
0xFA00_0000, // s_load_dwordx4 s[8:11], s[8:9], null
|
|
0xF40C_0001,
|
|
0xFA00_0000, // s_load_dwordx8 s[0:7], s[2:3], null
|
|
0xF080_0F08,
|
|
0x0040_0002, // image_sample v[0:3], v[2:3], s[0:7], s[8:11]
|
|
S_ENDPGM,
|
|
],
|
|
&[param(Table, 8, 1), param(Table, 9, 1)],
|
|
HwStage::Ps,
|
|
);
|
|
assert_eq!(r.scalar, [((0, 3), at(0, 0)), ((0, 4), at(1, 0))]);
|
|
let roles: Vec<_> = r.descriptors.iter().map(|d| (d.role, &d.source)).collect();
|
|
assert_eq!(
|
|
roles,
|
|
[
|
|
(
|
|
Role::Resource,
|
|
&Source::Loaded {
|
|
load: (0, 4),
|
|
first: 0
|
|
}
|
|
),
|
|
(
|
|
Role::Sampler,
|
|
&Source::Loaded {
|
|
load: (0, 3),
|
|
first: 0
|
|
}
|
|
),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn root_descriptors_are_built_in_code() {
|
|
let r = track_words(
|
|
&[
|
|
0x8801_FF0B,
|
|
0x0010_0000, // s_or_b32 s1, s11, 0x100000
|
|
0xBE80_030A, // s_mov_b32 s0, s10
|
|
0xBE82_03C1, // s_mov_b32 s2, -1
|
|
0xBE83_03FF,
|
|
0x1101_4FAC, // s_mov_b32 s3, 0x11014fac
|
|
0xF430_0000,
|
|
0xFA00_0000, // s_buffer_load_dwordx16 s[0:15], s[0:3], null
|
|
S_ENDPGM,
|
|
],
|
|
&[param(Descriptor, 10, 2)],
|
|
HwStage::Vs,
|
|
);
|
|
assert_eq!(
|
|
r.descriptors[0].source,
|
|
Source::Built {
|
|
address: at(0, 0),
|
|
bits: 0x10_0000,
|
|
words: [u32::MAX, 0x1101_4FAC]
|
|
}
|
|
);
|
|
assert_eq!(
|
|
r.params.iter().map(|p| (p.1, p.2)).collect::<Vec<_>>(),
|
|
[(0, 0), (0, 1)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn heap_indices_from_root_constants_in_a_merged_stage() {
|
|
// User data starts at s8: the constant (index 6) is s14, the heap
|
|
// (index 10) s18.
|
|
let r = track_words(
|
|
&[
|
|
0x8F08_850E, // s_lshl_b32 s8, s14, 5
|
|
0xBE93_0380, // s_mov_b32 s19, 0
|
|
0xF408_0209,
|
|
0x1000_0000, // s_load_dwordx4 s[8:11], s[18:19], s8
|
|
0xE000_2000,
|
|
0x8002_0400, // buffer_load_format_x v4, v0, s[8:11], 0 idxen
|
|
S_ENDPGM,
|
|
],
|
|
&[param(Constants, 6, 1), param(ResourceHeap, 10, 1)],
|
|
HwStage::Ms,
|
|
);
|
|
let index = r.params.iter().find(|p| p.1 == 0).unwrap().0;
|
|
let shift = r.scalar[0].1.dynamic.unwrap();
|
|
assert_eq!(r.scalar[0].1.param, 1);
|
|
// The computed term is the shifted constant, not the constant itself.
|
|
assert_ne!(shift, index);
|
|
assert_eq!(
|
|
r.descriptors[0].source,
|
|
Source::Loaded {
|
|
load: (0, 2),
|
|
first: 0
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn spilled_sgprs_are_followed_through_vgpr_lanes() {
|
|
let r = track_words(
|
|
&[
|
|
0xD761_0000,
|
|
0x0001_0609, // v_writelane_b32 v0, s9, 3
|
|
0xD760_0002,
|
|
0x0001_0700, // v_readlane_b32 s2, v0, 3
|
|
0xBE83_0380, // s_mov_b32 s3, 0
|
|
0xF40C_0001,
|
|
0xFA00_0020, // s_load_dwordx8 s[0:7], s[2:3], 0x20
|
|
S_ENDPGM,
|
|
],
|
|
&[param(Table, 9, 1)],
|
|
HwStage::Ps,
|
|
);
|
|
assert_eq!(r.scalar, [((0, 3), at(0, 0x20))]);
|
|
}
|
|
|
|
#[test]
|
|
fn descriptors_joined_by_control_flow_are_traced_per_edge() {
|
|
let r = track_words(
|
|
&[
|
|
0xBE82_0309, // s_mov_b32 s2, s9
|
|
0xBE83_0380, // s_mov_b32 s3, 0
|
|
0xBF06_8001, // s_cmp_eq_u32 s1, 0
|
|
0xBF85_0003, // s_cbranch_scc1 -> 0x1c
|
|
0xF408_0001,
|
|
0xFA00_0000, // s_load_dwordx4 s[0:3], s[2:3], null
|
|
0xBF82_0002, // s_branch -> 0x24
|
|
0xF408_0001,
|
|
0xFA00_0010, // 0x1c: s_load_dwordx4 s[0:3], s[2:3], 0x10
|
|
0xF420_0100,
|
|
0xFA00_0000, // 0x24: s_buffer_load_dword s4, s[0:3], null
|
|
S_ENDPGM,
|
|
],
|
|
&[param(Table, 9, 1)],
|
|
HwStage::Ps,
|
|
);
|
|
let Source::Phi { args, .. } = &r.descriptors[0].source else {
|
|
panic!("{:?}", r.descriptors)
|
|
};
|
|
let offsets: Vec<_> = args
|
|
.iter()
|
|
.map(|a| match a {
|
|
Source::Loaded { load, .. } => {
|
|
r.scalar.iter().find(|s| s.0 == *load).unwrap().1.offset
|
|
}
|
|
a => panic!("{a:?}"),
|
|
})
|
|
.collect();
|
|
assert_eq!(offsets.len(), 2);
|
|
assert!(
|
|
offsets.contains(&0) && offsets.contains(&0x10),
|
|
"{offsets:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn untraceable_addresses_fail_with_a_reason() {
|
|
let code: Vec<u8> = [0xF408_0001, 0xFA00_0000, S_ENDPGM] // s_load_dwordx4 s[0:3], s[2:3]
|
|
.iter()
|
|
.flat_map(|w: &u32| w.to_le_bytes())
|
|
.collect();
|
|
let p = crate::ir::build(&code, false, 256).unwrap();
|
|
let err = track(&p, &[param(Table, 9, 1)], HwStage::Ps).unwrap_err();
|
|
assert_eq!(
|
|
err,
|
|
ShaderError::Unsupported {
|
|
offset: 0,
|
|
what: "an address in an SGPR that is no root parameter"
|
|
}
|
|
);
|
|
}
|
|
}
|