victory-gta6: this fork's changes on top of VIctory main (ffe310e)
World rendering, lighting, frame-rate and memory work for Grand Theft Auto VI (Xbox Series X profile) on one Windows PC with an NVIDIA RTX 4070. The README describes the state, the changes and the credits. Based on VIctory by Marvisak and contributors (GPL-3.0-or-later); XCL by ivall and contributors was used as a reference. No game files are included. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
45c6e80601
commit
6fa814fba5
61 files changed
+32259
-1274
No files matched your search
@@ -101,6 +101,16 @@ const TERMINAL: u32 = u32::MAX;
|
||||
const MAX_NODES: usize = 1 << 21;
|
||||
/// Case splits [`Bools::meet`] makes before it gives up.
|
||||
const MAX_MEET: usize = 1 << 16;
|
||||
/// Conditions [`Meets`] keeps; past that, they are searched anew.
|
||||
const MAX_MEETS: usize = 1 << 18;
|
||||
|
||||
/// What [`Bools::meet`] found out about one [`Bools`], for its later calls:
|
||||
/// the conditions no lane satisfies, and the case splits it took to find
|
||||
/// that out of each.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Meets {
|
||||
none: Map<Box<[u32]>, usize>,
|
||||
}
|
||||
|
||||
impl Bools {
|
||||
fn new() -> Self {
|
||||
@@ -227,32 +237,99 @@ impl Bools {
|
||||
/// `b` where `holds`, its negation where not. Yes when the search grows
|
||||
/// too large to finish.
|
||||
pub fn meet(&self, all: &[(Bool, bool)]) -> bool {
|
||||
let mut budget = MAX_MEET;
|
||||
self.meet_in(all.to_vec(), &mut budget)
|
||||
self.meet_seen(all, &mut Meets::default())
|
||||
}
|
||||
|
||||
fn meet_in(&self, mut all: Vec<(Bool, bool)>, budget: &mut usize) -> bool {
|
||||
let fails = |&(b, holds): &(Bool, bool)| b == if holds { Bool::FALSE } else { Bool::TRUE };
|
||||
if all.iter().any(fails) {
|
||||
return false;
|
||||
/// [`Self::meet`], which looks up in `seen` what earlier calls on these
|
||||
/// `Bools` found out and adds what it finds. The answer is the one
|
||||
/// `meet` gives.
|
||||
pub fn meet_seen(&self, all: &[(Bool, bool)], seen: &mut Meets) -> bool {
|
||||
self.meet_within(all, MAX_MEET, seen)
|
||||
}
|
||||
|
||||
/// [`Self::meet_seen`] with `budget` case splits.
|
||||
fn meet_within(&self, all: &[(Bool, bool)], mut budget: usize, seen: &mut Meets) -> bool {
|
||||
let mut work = all
|
||||
.iter()
|
||||
.map(|&(b, holds)| b.0 << 1 | u32::from(holds))
|
||||
.collect();
|
||||
self.meet_in(0, &mut work, &mut budget, seen)
|
||||
}
|
||||
|
||||
/// The search of [`Self::meet`] for the conditions `work[at..]` (a node
|
||||
/// doubled, plus one where it must hold), which it takes off `work`.
|
||||
///
|
||||
/// The search is a case split on the top variable, and the cofactors of
|
||||
/// a few large functions come down to the same conditions over and over
|
||||
/// (a block's reach under a mask and under its negation share most of
|
||||
/// their nodes), so splitting each anew is exponential where the
|
||||
/// distinct conditions are few. `seen` keeps the conditions that no lane
|
||||
/// satisfies with the case splits it took to find that out: meeting
|
||||
/// them again takes those splits off the budget at once. A search
|
||||
/// therefore gives up exactly where one that split everything anew
|
||||
/// would (nothing below such conditions ends it early, so it either
|
||||
/// makes all those splits or runs out among them), and gives the same
|
||||
/// answer.
|
||||
fn meet_in(
|
||||
&self,
|
||||
at: usize,
|
||||
work: &mut Vec<u32>,
|
||||
budget: &mut usize,
|
||||
seen: &mut Meets,
|
||||
) -> bool {
|
||||
// Constants either fail or say nothing.
|
||||
let mut kept = at;
|
||||
for i in at..work.len() {
|
||||
let (b, holds) = (work[i] >> 1, work[i] & 1 != 0);
|
||||
if b > Bool::TRUE.0 {
|
||||
work[kept] = work[i];
|
||||
kept += 1;
|
||||
} else if (b == Bool::TRUE.0) != holds {
|
||||
work.truncate(at);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
all.retain(|&(b, _)| b != Bool::FALSE && b != Bool::TRUE);
|
||||
if all.is_empty() || *budget == 0 {
|
||||
work.truncate(kept);
|
||||
if kept == at || *budget == 0 {
|
||||
work.truncate(at);
|
||||
return true;
|
||||
}
|
||||
// As a set: neither order nor repeats change the search.
|
||||
work[at..].sort_unstable();
|
||||
let mut end = at + 1;
|
||||
for i in at + 1..kept {
|
||||
if work[i] != work[end - 1] {
|
||||
work[end] = work[i];
|
||||
end += 1;
|
||||
}
|
||||
}
|
||||
work.truncate(end);
|
||||
if let Some(&splits) = seen.none.get(&work[at..]) {
|
||||
let within = splits <= *budget;
|
||||
*budget = budget.saturating_sub(splits);
|
||||
work.truncate(at);
|
||||
return !within;
|
||||
}
|
||||
let before = *budget;
|
||||
*budget -= 1;
|
||||
let top = |b: Bool| self.nodes[b.0 as usize].0;
|
||||
let v = all.iter().map(|&(b, _)| top(b)).min().expect("not empty");
|
||||
[false, true].into_iter().any(|value| {
|
||||
let next = all
|
||||
.iter()
|
||||
.map(|&(b, holds)| {
|
||||
let (lo, hi) = self.split(b, v);
|
||||
(if value { hi } else { lo }, holds)
|
||||
})
|
||||
.collect();
|
||||
self.meet_in(next, budget)
|
||||
})
|
||||
let top = |code: u32| self.nodes[(code >> 1) as usize].0;
|
||||
let v = work[at..].iter().map(|&c| top(c)).min().expect("not empty");
|
||||
let mut found = false;
|
||||
for value in [false, true] {
|
||||
for i in at..end {
|
||||
let (lo, hi) = self.split(Bool(work[i] >> 1), v);
|
||||
work.push((if value { hi } else { lo }).0 << 1 | work[i] & 1);
|
||||
}
|
||||
if self.meet_in(end, work, budget, seen) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !found && seen.none.len() < MAX_MEETS {
|
||||
seen.none.insert(work[at..end].into(), before - *budget);
|
||||
}
|
||||
work.truncate(at);
|
||||
found
|
||||
}
|
||||
|
||||
fn implies(&mut self, a: Bool, b: Bool) -> bool {
|
||||
@@ -351,8 +428,12 @@ pub struct Structured {
|
||||
pub highs: HashMap<Value, Value>,
|
||||
/// Masks (low dwords) set only in lanes whose EXEC bit is set where
|
||||
/// they are written: every such lane reaches the writing block, so a
|
||||
/// ballot there gives the mask as data.
|
||||
/// ballot there gives the mask as data. No split low dword, as in
|
||||
/// `masks`.
|
||||
pub active: HashSet<Value>,
|
||||
/// Incoming lane boolean per predecessor edge (in block predecessor order)
|
||||
/// for each [`Atom::Phi`] id.
|
||||
pub phi_masks: HashMap<Value, Vec<Bool>>,
|
||||
}
|
||||
|
||||
impl Structured {
|
||||
@@ -371,11 +452,15 @@ pub fn structure(program: &Program) -> Result<Structured, ShaderError> {
|
||||
loop {
|
||||
let execs = l.masks_in_order();
|
||||
let cond = l.paths()?;
|
||||
let wrong = l.wrong_joins(&cond);
|
||||
if wrong.is_empty() {
|
||||
let (wrong, new_derived) = l.check_joins(&cond);
|
||||
if l.b.overflow {
|
||||
return Err(TOO_LARGE);
|
||||
}
|
||||
if wrong.is_empty() && new_derived.is_empty() {
|
||||
return l.finish(&execs, &cond);
|
||||
}
|
||||
l.unresolved.extend(wrong);
|
||||
l.derived_phis.extend(new_derived);
|
||||
l.reset();
|
||||
}
|
||||
}
|
||||
@@ -397,16 +482,26 @@ struct Lanes<'a> {
|
||||
phis: Map<Value, (usize, usize)>,
|
||||
/// Mask joins found wrong when resolved to a constant: atoms instead.
|
||||
unresolved: HashSet<Value>,
|
||||
/// Phi ids whose joins depend on EXEC or VALU results (including along
|
||||
/// back edges), carried across resets so their joins stay in `masks`.
|
||||
derived_phis: HashSet<Value>,
|
||||
b: Bools,
|
||||
/// Masks derived from EXEC and VALU results.
|
||||
masks: Map<Key, Bool>,
|
||||
/// Other values read as masks: constants, uniform selects, opaque ones.
|
||||
other: Map<Key, Bool>,
|
||||
/// Pairs read as masks that `mask` does not see: carry-ins, and pairs
|
||||
/// joins take over back edges.
|
||||
seen: Vec<Key>,
|
||||
/// For branches: SCC of a mask operation ("some lane is set") as the
|
||||
/// lane's own bit.
|
||||
scc_lane: Map<Value, Bool>,
|
||||
/// Joins resolved to a constant, to check once all edges are known.
|
||||
joins: Vec<Join>,
|
||||
/// [`Atom::Phi`] joins created in the current pass, resolved in
|
||||
/// `check_joins` and exported in `finish`.
|
||||
phi_joins: Vec<PhiJoin>,
|
||||
phi_join_ids: HashSet<Value>,
|
||||
/// Per block: what a lane knows on entry, over forward edges only.
|
||||
first: Vec<Bool>,
|
||||
/// Per block ending in a branch: when a lane takes it.
|
||||
@@ -422,6 +517,16 @@ struct Join {
|
||||
/// The phi value naming the join.
|
||||
id: Value,
|
||||
value: bool,
|
||||
derived: bool,
|
||||
args: Vec<Key>,
|
||||
}
|
||||
|
||||
/// A mask join represented as [`Atom::Phi`], whose incoming edge masks are
|
||||
/// resolved once every block (including loop back edges) has been visited.
|
||||
struct PhiJoin {
|
||||
block: usize,
|
||||
id: Value,
|
||||
derived: bool,
|
||||
args: Vec<Key>,
|
||||
}
|
||||
|
||||
@@ -468,11 +573,15 @@ impl<'a> Lanes<'a> {
|
||||
defs,
|
||||
phis,
|
||||
unresolved: HashSet::new(),
|
||||
derived_phis: HashSet::new(),
|
||||
b: Bools::new(),
|
||||
masks: Map::default(),
|
||||
other: Map::default(),
|
||||
seen: Vec::new(),
|
||||
scc_lane: Map::default(),
|
||||
joins: Vec::new(),
|
||||
phi_joins: Vec::new(),
|
||||
phi_join_ids: HashSet::new(),
|
||||
first: vec![Bool::FALSE; blocks.len()],
|
||||
when: vec![None; blocks.len()],
|
||||
active: HashSet::new(),
|
||||
@@ -483,8 +592,11 @@ impl<'a> Lanes<'a> {
|
||||
self.b = Bools::new();
|
||||
self.masks.clear();
|
||||
self.other.clear();
|
||||
self.seen.clear();
|
||||
self.scc_lane.clear();
|
||||
self.joins.clear();
|
||||
self.phi_joins.clear();
|
||||
self.phi_join_ids.clear();
|
||||
self.first.fill(Bool::FALSE);
|
||||
self.when.fill(None);
|
||||
self.active.clear();
|
||||
@@ -557,28 +669,61 @@ impl<'a> Lanes<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Joins whose constant fails on some edge, given what lanes know.
|
||||
fn wrong_joins(&mut self, cond: &[Bool]) -> Vec<Value> {
|
||||
/// Joins whose constant fails on some edge, and joins that need another
|
||||
/// pass because a later edge turned out to derive from EXEC or VALU.
|
||||
fn check_joins(&mut self, cond: &[Bool]) -> (Vec<Value>, Vec<Value>) {
|
||||
let mut wrong = Vec::new();
|
||||
let mut new_derived = Vec::new();
|
||||
let mut seen_derived = HashSet::new();
|
||||
let mut j = 0;
|
||||
let mut p = 0;
|
||||
// Checking an argument can resolve another join; it is checked too.
|
||||
while j < self.joins.len() {
|
||||
let (block, id, value) = (self.joins[j].block, self.joins[j].id, self.joins[j].value);
|
||||
for (n, a) in self.joins[j].args.clone().into_iter().enumerate() {
|
||||
let (q, taken) = self.incoming[block][n];
|
||||
let e = self.edge(cond[q], q, taken);
|
||||
let mut m = self.mask(a).0;
|
||||
if !value {
|
||||
m = self.b.not(m);
|
||||
}
|
||||
if !self.b.implies(e, m) {
|
||||
wrong.push(id);
|
||||
break;
|
||||
while j < self.joins.len() || p < self.phi_joins.len() {
|
||||
while j < self.joins.len() {
|
||||
let (block, id, value, was_derived) = (
|
||||
self.joins[j].block,
|
||||
self.joins[j].id,
|
||||
self.joins[j].value,
|
||||
self.joins[j].derived,
|
||||
);
|
||||
let mut failed = false;
|
||||
for (n, a) in self.joins[j].args.clone().into_iter().enumerate() {
|
||||
let (q, taken) = self.incoming[block][n];
|
||||
let e = self.edge(cond[q], q, taken);
|
||||
let (mut m, d) = self.mask(a);
|
||||
if !was_derived
|
||||
&& d
|
||||
&& !self.derived_phis.contains(&id)
|
||||
&& seen_derived.insert(id)
|
||||
{
|
||||
new_derived.push(id);
|
||||
}
|
||||
if !value {
|
||||
m = self.b.not(m);
|
||||
}
|
||||
if !failed && !self.b.implies(e, m) {
|
||||
wrong.push(id);
|
||||
failed = true;
|
||||
}
|
||||
}
|
||||
j += 1;
|
||||
}
|
||||
while p < self.phi_joins.len() {
|
||||
let (id, was_derived) = (self.phi_joins[p].id, self.phi_joins[p].derived);
|
||||
for a in self.phi_joins[p].args.clone() {
|
||||
let (_, d) = self.mask(a);
|
||||
if !was_derived
|
||||
&& d
|
||||
&& !self.derived_phis.contains(&id)
|
||||
&& seen_derived.insert(id)
|
||||
{
|
||||
new_derived.push(id);
|
||||
}
|
||||
}
|
||||
p += 1;
|
||||
}
|
||||
j += 1;
|
||||
}
|
||||
wrong
|
||||
(wrong, new_derived)
|
||||
}
|
||||
|
||||
fn finish(
|
||||
@@ -586,6 +731,22 @@ impl<'a> Lanes<'a> {
|
||||
execs: &[Vec<Option<Bool>>],
|
||||
cond: &[Bool],
|
||||
) -> Result<Structured, ShaderError> {
|
||||
let phi_joins = std::mem::take(&mut self.phi_joins);
|
||||
let mut phi_masks = HashMap::with_capacity(phi_joins.len());
|
||||
for pj in phi_joins {
|
||||
let incoming: Vec<Bool> = pj
|
||||
.args
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(n, a)| {
|
||||
let (q, taken) = self.incoming[pj.block][n];
|
||||
let e = self.edge(cond[q], q, taken);
|
||||
let m = self.mask(a).0;
|
||||
self.b.given(m, e)
|
||||
})
|
||||
.collect();
|
||||
phi_masks.insert(pj.id, incoming);
|
||||
}
|
||||
let blocks = &self.p.blocks;
|
||||
let b = &mut self.b;
|
||||
let guards = blocks
|
||||
@@ -624,6 +785,20 @@ impl<'a> Lanes<'a> {
|
||||
if self.b.overflow {
|
||||
return Err(TOO_LARGE);
|
||||
}
|
||||
// Exports go by low dword. A low dword read with two high dwords
|
||||
// (one rewritten alone) names no one mask: it is left out, so
|
||||
// lowering refuses what reads it instead of taking the other
|
||||
// pair's lanes 32-63. Not where the low dword holds a number: see
|
||||
// the shortcut in `mask`.
|
||||
let (mut high, mut split) = (Map::default(), HashSet::new());
|
||||
let keys = self.masks.keys().chain(self.other.keys());
|
||||
for &(lo, hi) in keys.chain(&self.seen) {
|
||||
if *high.entry(lo).or_insert(hi) != hi {
|
||||
split.insert(lo);
|
||||
}
|
||||
}
|
||||
self.masks.retain(|k, _| !split.contains(&k.0));
|
||||
self.active.retain(|lo| !split.contains(lo));
|
||||
Ok(Structured {
|
||||
masks: self.masks.iter().map(|(&(lo, _), &m)| (lo, m)).collect(),
|
||||
highs: self
|
||||
@@ -634,6 +809,7 @@ impl<'a> Lanes<'a> {
|
||||
.collect(),
|
||||
paired_masks: if self.p.wave64 { self.masks } else { Map::default() },
|
||||
active: self.active,
|
||||
phi_masks,
|
||||
bools: self.b,
|
||||
exits,
|
||||
guards,
|
||||
@@ -694,6 +870,12 @@ impl<'a> Lanes<'a> {
|
||||
})
|
||||
};
|
||||
let derived = exec_write(lo, k.0) || exec_write(hi, k.1);
|
||||
// shortcut: the atom names the pair by its low dword, and
|
||||
// lowering takes the high dword written beside it. For halves
|
||||
// written apart (two `s_mov_b32`) lanes 32-63 are wrong unless
|
||||
// lowering has no value for the low dword (EXEC, compares)
|
||||
// and refuses. Give the atom both dwords when such code
|
||||
// turns up.
|
||||
let m = m.unwrap_or_else(|| {
|
||||
let born = self.born(k.0);
|
||||
self.b.atom(Atom::Opaque(k.0), born)
|
||||
@@ -730,7 +912,7 @@ impl<'a> Lanes<'a> {
|
||||
.map(|n| (arg(lo, k.0, n), arg(hi, k.1, n)))
|
||||
.collect();
|
||||
let id = lo.map_or(k.1, |_| k.0);
|
||||
let mut derived = false;
|
||||
let mut derived = self.derived_phis.contains(&id);
|
||||
// (edge condition, mask) per forward edge; back edges come later.
|
||||
let mut forward = Vec::new();
|
||||
for (n, &a) in args.iter().enumerate() {
|
||||
@@ -740,6 +922,8 @@ impl<'a> Lanes<'a> {
|
||||
derived |= d;
|
||||
let e = self.edge(self.first[q], q, taken);
|
||||
forward.push((e, m));
|
||||
} else {
|
||||
self.seen.push(a);
|
||||
}
|
||||
}
|
||||
if forward.len() == args.len()
|
||||
@@ -760,6 +944,7 @@ impl<'a> Lanes<'a> {
|
||||
block,
|
||||
id,
|
||||
value,
|
||||
derived,
|
||||
args,
|
||||
});
|
||||
let m = if value { Bool::TRUE } else { Bool::FALSE };
|
||||
@@ -767,6 +952,14 @@ impl<'a> Lanes<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.phi_join_ids.insert(id) {
|
||||
self.phi_joins.push(PhiJoin {
|
||||
block,
|
||||
id,
|
||||
derived,
|
||||
args,
|
||||
});
|
||||
}
|
||||
let born = self.index[block];
|
||||
Some((self.b.atom(Atom::Phi(id), born), derived))
|
||||
}
|
||||
@@ -854,9 +1047,13 @@ impl<'a> Lanes<'a> {
|
||||
"cselect" => {
|
||||
let (a, c) = (arg(0, self)?, arg(1, self)?);
|
||||
let scc = inst.read(Reg::Scc)?;
|
||||
let born = self.born(scc);
|
||||
let u = self.b.atom(Atom::Uniform(scc), born);
|
||||
(self.b.ite(u, a, c), None)
|
||||
if self.clear_after_loop(scc) {
|
||||
(c, None)
|
||||
} else {
|
||||
let born = self.born(scc);
|
||||
let u = self.b.atom(Atom::Uniform(scc), born);
|
||||
(self.b.ite(u, a, c), None)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let (a, c) = (arg(0, self)?, arg(1, self)?);
|
||||
@@ -872,6 +1069,31 @@ impl<'a> Lanes<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether SCC value `scc` is clear wherever it is read: the instruction before an
|
||||
/// `s_cbranch_execnz` that closes a loop wrote it as "the EXEC it leaves is not
|
||||
/// empty" (`s_andn2_wrexec`, say), and the wave leaves `do { .. } while (EXEC)`
|
||||
/// with EXEC empty. A read inside the loop or behind another way out of it sees a
|
||||
/// join of this value, not the value itself.
|
||||
fn clear_after_loop(&self, scc: Value) -> bool {
|
||||
let Some(&(b, i)) = self.defs.get(&scc) else {
|
||||
return false;
|
||||
};
|
||||
let block = &self.p.blocks[b];
|
||||
let (Some(writer), Some(last)) = (block.insts.get(i), block.insts.last()) else {
|
||||
return false;
|
||||
};
|
||||
let exec = writer.write(Reg::S(EXEC));
|
||||
let back = match block.exit {
|
||||
Exit::Branch { taken, .. } => self.index[taken] <= self.index[b],
|
||||
_ => false,
|
||||
};
|
||||
back && i + 2 == block.insts.len()
|
||||
&& exec.is_some()
|
||||
&& last.name() == "s_cbranch_execnz"
|
||||
&& last.read(Reg::S(EXEC)) == exec
|
||||
&& (!self.p.wave64 || last.read(Reg::S(EXEC + 1)) == writer.write(Reg::S(EXEC + 1)))
|
||||
}
|
||||
|
||||
fn combine(&mut self, f: &str, a: Bool, c: Bool) -> Option<Bool> {
|
||||
let b = &mut self.b;
|
||||
Some(match f {
|
||||
@@ -963,6 +1185,23 @@ impl<'a> Lanes<'a> {
|
||||
};
|
||||
self.scc_lane.insert(scc, m);
|
||||
}
|
||||
// `s_bcnt1` of a mask every running lane sees alike (EXEC under a scalar
|
||||
// condition, or EXEC itself): its SCC, "some lane is set", is each lane's own
|
||||
// bit. A branch on it then reads no ballot.
|
||||
if takes_mask
|
||||
&& name.starts_with("s_bcnt1_i32_b")
|
||||
&& let Some(scc) = inst.write(Reg::Scc)
|
||||
&& let [a] = &src[..]
|
||||
&& let Some((m, true)) = self.operand(inst, a)
|
||||
{
|
||||
let e = exec.unwrap_or(Bool::TRUE);
|
||||
let own = self.b.given(m, e);
|
||||
if self.b.implies(m, e)
|
||||
&& self.b.support(own).iter().all(|a| matches!(a, Atom::Uniform(_)))
|
||||
{
|
||||
self.scc_lane.insert(scc, own);
|
||||
}
|
||||
}
|
||||
}
|
||||
Fields::Valu(v) => {
|
||||
let exec = self.exec(inst);
|
||||
@@ -974,11 +1213,22 @@ impl<'a> Lanes<'a> {
|
||||
l.active.insert(k.0);
|
||||
};
|
||||
// A select reads a mask nothing else may: a join of compare
|
||||
// results is resolved here, as for EXEC.
|
||||
if name == "v_cndmask_b32"
|
||||
// results is resolved here, as for EXEC. A carry-in is only
|
||||
// recorded for `finish`: resolving its join would turn a
|
||||
// refusal into a per-lane phi, wrong after a divergent branch.
|
||||
if (name == "v_cndmask_b32" || name.contains("_co_ci_"))
|
||||
&& let Some(&Operand::Scalar { code, .. }) = v.src.get(2)
|
||||
&& code != NULL
|
||||
&& let Some(k) = self.key(|r| inst.read(r), code)
|
||||
{
|
||||
if name == "v_cndmask_b32" {
|
||||
self.mask(k);
|
||||
} else {
|
||||
self.seen.push(k);
|
||||
}
|
||||
}
|
||||
if matches!(name, "v_div_fmas_f32" | "v_div_fmas_f64")
|
||||
&& let Some(k) = self.key(|r| inst.read(r), VCC)
|
||||
{
|
||||
self.mask(k);
|
||||
}
|
||||
@@ -1112,6 +1362,102 @@ mod tests {
|
||||
s.guards.iter().flatten().filter(|g| g.is_some()).count()
|
||||
}
|
||||
|
||||
/// The search of [`Bools::meet`] with every case split made anew, as it
|
||||
/// was before [`Meets`].
|
||||
fn meet_anew(b: &Bools, mut all: Vec<(Bool, bool)>, budget: &mut usize) -> bool {
|
||||
let fails = |&(b, holds): &(Bool, bool)| b == if holds { Bool::FALSE } else { Bool::TRUE };
|
||||
if all.iter().any(fails) {
|
||||
return false;
|
||||
}
|
||||
all.retain(|&(b, _)| b != Bool::FALSE && b != Bool::TRUE);
|
||||
if all.is_empty() || *budget == 0 {
|
||||
return true;
|
||||
}
|
||||
*budget -= 1;
|
||||
let top = |x: Bool| b.nodes[x.0 as usize].0;
|
||||
let v = all.iter().map(|&(x, _)| top(x)).min().expect("not empty");
|
||||
[false, true].into_iter().any(|value| {
|
||||
let next = all
|
||||
.iter()
|
||||
.map(|&(x, holds)| {
|
||||
let (lo, hi) = b.split(x, v);
|
||||
(if value { hi } else { lo }, holds)
|
||||
})
|
||||
.collect();
|
||||
meet_anew(b, next, budget)
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_search_that_remembers_answers_as_one_made_anew() {
|
||||
// Functions of 12 variables put together at random, and conditions
|
||||
// of two to five of them (repeats and constants among them), under
|
||||
// budgets that run out early, late and never: the answer, and where
|
||||
// no lane is found the budget left, are those of the search that
|
||||
// remembers nothing; with what one `Meets` gathered over all of
|
||||
// them, and with an empty one.
|
||||
let mut b = Bools::new();
|
||||
let mut state = 0x9E37_79B9_7F4A_7C15_u64;
|
||||
let mut random = move |n: usize| {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 7;
|
||||
state ^= state << 17;
|
||||
(state % n as u64) as usize
|
||||
};
|
||||
let mut f: Vec<Bool> = (0..12).map(|i| b.atom(Atom::Constant(i + 2), 0)).collect();
|
||||
f.extend([Bool::FALSE, Bool::TRUE]);
|
||||
for _ in 0..120 {
|
||||
let (x, y) = (f[random(f.len())], f[random(f.len())]);
|
||||
let made = match random(4) {
|
||||
0 => b.and(x, y),
|
||||
1 => b.or(x, y),
|
||||
2 => b.xor(x, y),
|
||||
_ => b.not(x),
|
||||
};
|
||||
f.push(made);
|
||||
}
|
||||
let mut seen = Meets::default();
|
||||
let (mut none, mut some, mut gave_up) = (0, 0, 0);
|
||||
for _ in 0..1500 {
|
||||
let all: Vec<(Bool, bool)> = (0..2 + random(4))
|
||||
.map(|_| (f[random(f.len())], random(2) == 0))
|
||||
.collect();
|
||||
for budget in [0, 1, 2, 3, 5, 9, 20, 60, 250, 1000, MAX_MEET] {
|
||||
let mut left = budget;
|
||||
let anew = meet_anew(&b, all.clone(), &mut left);
|
||||
for fresh in [false, true] {
|
||||
let mut empty = Meets::default();
|
||||
let seen = if fresh { &mut empty } else { &mut seen };
|
||||
let mut work = all
|
||||
.iter()
|
||||
.map(|&(x, holds)| x.0 << 1 | u32::from(holds))
|
||||
.collect();
|
||||
let mut mine = budget;
|
||||
let found = b.meet_in(0, &mut work, &mut mine, seen);
|
||||
assert_eq!(found, anew, "{all:?} within {budget}");
|
||||
assert!(work.is_empty());
|
||||
if !found {
|
||||
assert_eq!(mine, left, "{all:?} within {budget}");
|
||||
}
|
||||
}
|
||||
assert_eq!(b.meet_within(&all, budget, &mut seen), anew);
|
||||
if !anew {
|
||||
none += 1;
|
||||
} else if meet_anew(&b, all.clone(), &mut usize::MAX.clone()) {
|
||||
some += 1;
|
||||
} else {
|
||||
gave_up += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// All three outcomes came up, and searches were remembered.
|
||||
assert!(
|
||||
none > 100 && some > 100 && gave_up > 100,
|
||||
"{none} {some} {gave_up}"
|
||||
);
|
||||
assert!(seen.none.len() > 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn divergent_if_becomes_a_lane_branch() {
|
||||
let p = build(
|
||||
@@ -1193,6 +1539,39 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
/// `s_cselect` on the SCC a lane loop ended with (the blend-shape skinning
|
||||
/// programs): the loop is left when no lane remains, so the select takes
|
||||
/// its second operand; it was a uniform atom whose value no lane has.
|
||||
#[test]
|
||||
fn a_select_on_the_condition_a_lane_loop_ended_with_takes_its_second_operand() {
|
||||
let cnd = 256 | (256 << 9) | (3 << 18);
|
||||
let p = build(
|
||||
&[
|
||||
0xBE83_037E, // 0: s_mov_b32 s3, exec_lo
|
||||
0xBE9C_037E, // s_mov_b32 s28, exec_lo
|
||||
0x7E3A_0507, // 8: v_readfirstlane_b32 s29, v7
|
||||
0x7DA4_0E1D, // v_cmpx_eq_u32 exec_lo, s29, v7
|
||||
0xBE9C_471C, // s_andn2_wrexec_b32 s28, s28
|
||||
0xBF89_FFFC, // s_cbranch_execnz -> 8
|
||||
0xBEFE_0303, // s_mov_b32 exec_lo, s3
|
||||
0x8503_807E, // s_cselect_b32 s3, exec_lo, 0
|
||||
0xD501_0001,
|
||||
cnd, // v_cndmask_b32 v1, v0, v0, s3
|
||||
S_ENDPGM,
|
||||
],
|
||||
false,
|
||||
);
|
||||
let s = structure(&p).unwrap();
|
||||
let select = p
|
||||
.blocks
|
||||
.iter()
|
||||
.flat_map(|b| &b.insts)
|
||||
.find(|i| i.name() == "s_cselect_b32")
|
||||
.expect("the select");
|
||||
let mask = select.write(Reg::S(3)).unwrap();
|
||||
assert_eq!(s.masks.get(&mask), Some(&Bool::FALSE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_branches_stay_uniform() {
|
||||
let p = build(
|
||||
@@ -1212,6 +1591,57 @@ mod tests {
|
||||
assert_eq!(guarded(&s), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mask_whose_high_dword_is_rewritten_is_not_the_compare() {
|
||||
for reader in [
|
||||
0x0202_0480, // v_cndmask_b32 v1, 0, v2, vcc
|
||||
0x5002_0480, // v_add_co_ci_u32 v1, vcc, 0, v2, vcc
|
||||
] {
|
||||
let p = build(
|
||||
&[
|
||||
0x7D84_0080, // v_cmp_eq_u32 vcc, 0, v0
|
||||
0xBEEB_0380, // s_mov_b32 vcc_hi, 0
|
||||
reader,
|
||||
S_ENDPGM,
|
||||
],
|
||||
true,
|
||||
);
|
||||
let s = structure(&p).unwrap();
|
||||
let lo = p.blocks[0].insts[2].read(Reg::S(VCC)).unwrap();
|
||||
assert_eq!(p.blocks[0].insts[0].write(Reg::S(VCC)), Some(lo));
|
||||
// The reader sees (compare low dword, 0): lanes 32-63 are clear.
|
||||
// The mask exported for that low dword must not be the compare.
|
||||
assert_ne!(
|
||||
s.masks.get(&lo).and_then(|&m| s.bools.node(m)),
|
||||
Some((Atom::Lane(lo), Bool::FALSE, Bool::TRUE)),
|
||||
"{reader:#x}: vcc_hi was zeroed after the compare"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mask_joined_over_a_back_edge_with_a_rewritten_high_dword_is_not_the_compare() {
|
||||
let p = build(
|
||||
&[
|
||||
0x7D84_0080, // v_cmp_eq_u32 vcc, 0, v0
|
||||
0x0202_0480, // 4: v_cndmask_b32 v1, 0, v2, vcc
|
||||
0x7D84_0280, // v_cmp_eq_u32 vcc, 0, v1
|
||||
0xBEEB_0380, // s_mov_b32 vcc_hi, 0
|
||||
0xBF85_FFFC, // s_cbranch_scc1 -> 4
|
||||
S_ENDPGM,
|
||||
],
|
||||
true,
|
||||
);
|
||||
let s = structure(&p).unwrap();
|
||||
// The select reads the loop header's join; over the back edge it
|
||||
// takes (compare low dword, 0), which only the join sees.
|
||||
let lo = p.blocks[1].insts[1].write(Reg::S(VCC)).unwrap();
|
||||
assert_ne!(
|
||||
s.masks.get(&lo).and_then(|&m| s.bools.node(m)),
|
||||
Some((Atom::Lane(lo), Bool::FALSE, Bool::TRUE))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn irreducible_loops_are_split() {
|
||||
let p = build(
|
||||
@@ -1231,4 +1661,117 @@ mod tests {
|
||||
assert_eq!(p.blocks[1].preds, [2]);
|
||||
assert!(structure(&p).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mask_phi_joining_constant_and_compare_records_edge_bools() {
|
||||
let cnd = 258 | (259 << 9) | (2 << 18);
|
||||
let p = build(
|
||||
&[
|
||||
0xBF06_8000, // 0: s_cmp_eq_u32 s0, 0
|
||||
0xBF85_0002, // 4: s_cbranch_scc1 -> 16
|
||||
0xBE82_0380, // 8: s_mov_b32 s2, 0
|
||||
0xBF82_0002, // 12: s_branch -> 24
|
||||
0xD4C2_0002,
|
||||
0x0002_0080, // 16: v_cmp_eq_u32 s2, 0, v0
|
||||
0xD501_0001,
|
||||
cnd, // 24: v_cndmask_b32 v1, v2, v3, s2
|
||||
S_ENDPGM,
|
||||
],
|
||||
false,
|
||||
);
|
||||
let s = structure(&p).unwrap();
|
||||
let phi = p
|
||||
.blocks
|
||||
.iter()
|
||||
.flat_map(|b| &b.phis)
|
||||
.find(|phi| phi.reg == Reg::S(2))
|
||||
.expect("mask phi for s2");
|
||||
assert_eq!(
|
||||
s.masks.get(&phi.value).and_then(|&m| s.bools.node(m)),
|
||||
Some((Atom::Phi(phi.value), Bool::FALSE, Bool::TRUE))
|
||||
);
|
||||
let edges = &s.phi_masks[&phi.value];
|
||||
assert_eq!(edges.len(), 2);
|
||||
assert!(edges.contains(&Bool::FALSE), "{edges:?}");
|
||||
assert!(
|
||||
edges
|
||||
.iter()
|
||||
.any(|&e| matches!(s.bools.support(e)[..], [Atom::Lane(_)])),
|
||||
"{edges:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loop_mask_accumulator_from_zero_tracks_phi_and_edges() {
|
||||
let cnd = 258 | (259 << 9) | (2 << 18);
|
||||
let p = build(
|
||||
&[
|
||||
0xBE82_0480, // 0: s_mov_b64 s[2:3], 0
|
||||
0xBE84_0380, // 4: s_mov_b32 s4, 0
|
||||
0xD501_0001,
|
||||
cnd, // 8: v_cndmask_b32 v1, v2, v3, s[2:3]
|
||||
0x7D84_0080, // 16: v_cmp_eq_u32 vcc, 0, v0
|
||||
0x8882_026A, // 20: s_or_b64 s[2:3], vcc, s[2:3]
|
||||
0x8004_8104, // 24: s_add_u32 s4, s4, 1
|
||||
0xBF0A_8404, // 28: s_cmp_lt_u32 s4, 4
|
||||
0xBF85_FFF9, // 32: s_cbranch_scc1 -> 8
|
||||
0xD501_0004,
|
||||
cnd, // 36: v_cndmask_b32 v4, v2, v3, s[2:3]
|
||||
S_ENDPGM,
|
||||
],
|
||||
true,
|
||||
);
|
||||
let s = structure(&p).unwrap();
|
||||
let phi = p
|
||||
.blocks
|
||||
.iter()
|
||||
.flat_map(|b| &b.phis)
|
||||
.find(|phi| phi.reg == Reg::S(2))
|
||||
.expect("loop mask phi for s[2:3]");
|
||||
assert!(s.masks.contains_key(&phi.value));
|
||||
let edges = &s.phi_masks[&phi.value];
|
||||
assert_eq!(edges.len(), 2);
|
||||
assert!(edges.contains(&Bool::FALSE), "{edges:?}");
|
||||
assert!(edges.iter().any(|&e| {
|
||||
let sup = s.bools.support(e);
|
||||
sup.contains(&Atom::Phi(phi.value)) && sup.iter().any(|a| matches!(a, Atom::Lane(_)))
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loop_mask_phi_reset_to_constant_on_back_edge() {
|
||||
let cnd = 258 | (259 << 9) | (2 << 18);
|
||||
let p = build(
|
||||
&[
|
||||
0xD4C2_0002,
|
||||
0x0002_0080, // 0: v_cmp_eq_u32 s2, 0, v0
|
||||
0xBE84_0380, // 8: s_mov_b32 s4, 0
|
||||
0xD501_0001,
|
||||
cnd, // 12: v_cndmask_b32 v1, v2, v3, s2
|
||||
0xBE82_0380, // 20: s_mov_b32 s2, 0
|
||||
0x8004_8104, // 24: s_add_u32 s4, s4, 1
|
||||
0xBF0A_8404, // 28: s_cmp_lt_u32 s4, 4
|
||||
0xBF85_FFFA, // 32: s_cbranch_scc1 -> 12
|
||||
S_ENDPGM,
|
||||
],
|
||||
false,
|
||||
);
|
||||
let s = structure(&p).unwrap();
|
||||
let phi = p
|
||||
.blocks
|
||||
.iter()
|
||||
.flat_map(|b| &b.phis)
|
||||
.find(|phi| phi.reg == Reg::S(2))
|
||||
.expect("loop mask phi for s2");
|
||||
assert!(s.masks.contains_key(&phi.value));
|
||||
let edges = &s.phi_masks[&phi.value];
|
||||
assert_eq!(edges.len(), 2);
|
||||
assert!(edges.contains(&Bool::FALSE), "{edges:?}");
|
||||
assert!(
|
||||
edges
|
||||
.iter()
|
||||
.any(|&e| matches!(s.bools.support(e)[..], [Atom::Lane(_)])),
|
||||
"{edges:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user