Shader forms: lives of a spill lane, one lane of what a whole-wave section leaves
Water transmission volumes and the rest of the upscaler translate. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
389db9efb2
commit
e5841a0553
4 files changed
+344
-77
No files matched your search
@@ -2762,6 +2762,18 @@ impl<'a> Lower<'a> {
|
|||||||
self.s.m.extract(entry, r, index % 4)
|
self.s.m.extract(entry, r, index % 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether some instruction reads `v` as a number (or may, through a
|
||||||
|
/// join), rather than in a descriptor operand.
|
||||||
|
fn read_as_number(&self, v: Value) -> bool {
|
||||||
|
self.x.p.blocks.iter().any(|b| {
|
||||||
|
b.phis.iter().any(|p| p.args.contains(&v))
|
||||||
|
|| b.insts.iter().any(|i| {
|
||||||
|
matches!(i.fields, Fields::Sop { .. } | Fields::Valu(_))
|
||||||
|
&& i.reads.iter().any(|r| r.1 == v)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Whether some instruction computes with `v` (scalar or vector
|
/// Whether some instruction computes with `v` (scalar or vector
|
||||||
/// arithmetic), rather than only naming it in a descriptor operand,
|
/// arithmetic), rather than only naming it in a descriptor operand,
|
||||||
/// moving it (at most `depth` moves are followed) or comparing it: a
|
/// moving it (at most `depth` moves are followed) or comparing it: a
|
||||||
@@ -5983,11 +5995,23 @@ impl<'a> Lower<'a> {
|
|||||||
};
|
};
|
||||||
((kind | u, 0x409), Some(kind))
|
((kind | u, 0x409), Some(kind))
|
||||||
}
|
}
|
||||||
Fields::Buffer { .. } => {
|
Fields::Buffer { idxen, format, .. } => {
|
||||||
|
let stride = used.map_or(0, |u| u.stride);
|
||||||
|
let counted = inst
|
||||||
|
.write(Reg::S(code + first + 2))
|
||||||
|
.is_some_and(|v| self.read_as_number(v));
|
||||||
let props = if un.starts_with("buffer_load_format")
|
let props = if un.starts_with("buffer_load_format")
|
||||||
|| un.starts_with("buffer_store_format")
|
|| un.starts_with("buffer_store_format")
|
||||||
{
|
{
|
||||||
(10 | u, 0x409)
|
(10 | u, 0x409)
|
||||||
|
} else if !counted
|
||||||
|
&& *idxen
|
||||||
|
&& format.is_none()
|
||||||
|
&& stride >= 4
|
||||||
|
&& stride.is_multiple_of(4)
|
||||||
|
{
|
||||||
|
// Nothing reads the count: the query is cleaned up.
|
||||||
|
(12 | u, stride)
|
||||||
} else {
|
} else {
|
||||||
// Structured ones too: the host's views of them are raw
|
// Structured ones too: the host's views of them are raw
|
||||||
// (see `buffer`), which count bytes, and a size asked
|
// (see `buffer`), which count bytes, and a size asked
|
||||||
@@ -6859,6 +6883,17 @@ impl<'a> Lower<'a> {
|
|||||||
self.lane(inst, Some(true));
|
self.lane(inst, Some(true));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
// One lane of a VGPR a whole-wave section left.
|
||||||
|
if name == "v_readlane_b32"
|
||||||
|
&& let Some(&(_, w)) = inst.writes.first()
|
||||||
|
&& let Some(sh) = self.sections.get(&b).and_then(|s| {
|
||||||
|
s.iter().filter(|s| s.1.end < i).find_map(|s| s.1.reads.get(&w).copied())
|
||||||
|
})
|
||||||
|
{
|
||||||
|
let x = self.shape(sh, false)?;
|
||||||
|
self.vals.insert(w, Val::I32(x));
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
if let Some(rest) = name.strip_prefix("v_cmp_").or(name.strip_prefix("v_cmpx_")) {
|
if let Some(rest) = name.strip_prefix("v_cmp_").or(name.strip_prefix("v_cmpx_")) {
|
||||||
let r = if v.dpp.is_some() {
|
let r = if v.dpp.is_some() {
|
||||||
Err("a compare with DPP")
|
Err("a compare with DPP")
|
||||||
|
|||||||
@@ -143,6 +143,10 @@ pub(crate) enum Shape {
|
|||||||
pub(crate) struct Section {
|
pub(crate) struct Section {
|
||||||
pub end: usize,
|
pub end: usize,
|
||||||
pub shapes: HashMap<Value, Shape>,
|
pub shapes: HashMap<Value, Shape>,
|
||||||
|
/// What a `v_readlane_b32` of one lane takes, later in the block, from a
|
||||||
|
/// VGPR the section leaves (by the scalar it writes): the last lanes of
|
||||||
|
/// a reduction hold it before the first do.
|
||||||
|
pub reads: HashMap<Value, Shape>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Every whole-wave section of `block`, by start index.
|
/// Every whole-wave section of `block`, by start index.
|
||||||
@@ -180,8 +184,25 @@ pub(crate) fn sections(p: &Program, block: usize) -> HashMap<usize, Section> {
|
|||||||
let Some(end) = end else {
|
let Some(end) = end else {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
let shapes = simulate(&insts[i + 1..end], wave, saved);
|
let (vec, uni) = simulate(&insts[i + 1..end], wave, saved);
|
||||||
out.insert(i, Section { end, shapes });
|
let mut reads = HashMap::new();
|
||||||
|
for later in insts[end + 1..].iter().filter(|i| i.name() == "v_readlane_b32") {
|
||||||
|
if let Fields::Valu(v) = &later.fields
|
||||||
|
&& let (Some(Operand::Scalar { code, .. }), [Operand::Vector { reg, .. }, lane]) =
|
||||||
|
(v.dst, &v.src[..])
|
||||||
|
&& let Some(left) = later.read(Reg::V(*reg)).and_then(|x| vec.get(&x))
|
||||||
|
&& let Some(w) = later.write(Reg::S(code))
|
||||||
|
&& let Some(l) = lane_number(lane)
|
||||||
|
&& let Some(&sym) = left.get(l as usize)
|
||||||
|
// (a lane's own value stays a lane read)
|
||||||
|
&& !matches!(sym, Sym::Own(_))
|
||||||
|
&& let Some(shape) = single(sym, wave)
|
||||||
|
{
|
||||||
|
reads.insert(w, shape);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let shapes = shapes(vec, uni, wave);
|
||||||
|
out.insert(i, Section { end, shapes, reads });
|
||||||
i = end + 1;
|
i = end + 1;
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
@@ -268,7 +289,19 @@ fn combine(op: Op, a: Sym, b: Sym) -> Sym {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simulate(insts: &[Inst], wave: u32, saved: Value) -> HashMap<Value, Shape> {
|
/// The lane a `v_readlane_b32` names.
|
||||||
|
fn lane_number(op: &Operand) -> Option<u32> {
|
||||||
|
match *op {
|
||||||
|
Operand::Const(c) => crate::resource::inline(c),
|
||||||
|
Operand::Literal(x) => Some(x),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The lanes' values of the VGPRs the section writes, and its scalars.
|
||||||
|
type Left = (HashMap<Value, Vec<Sym>>, HashMap<Value, Sym>);
|
||||||
|
|
||||||
|
fn simulate(insts: &[Inst], wave: u32, saved: Value) -> Left {
|
||||||
let all = if wave == 64 {
|
let all = if wave == 64 {
|
||||||
u64::MAX
|
u64::MAX
|
||||||
} else {
|
} else {
|
||||||
@@ -289,7 +322,7 @@ fn simulate(insts: &[Inst], wave: u32, saved: Value) -> HashMap<Value, Shape> {
|
|||||||
.then(|| constant(inst))
|
.then(|| constant(inst))
|
||||||
.flatten();
|
.flatten();
|
||||||
let Some(k) = lo else {
|
let Some(k) = lo else {
|
||||||
return shapes(vec, uni, wave);
|
return (vec, uni);
|
||||||
};
|
};
|
||||||
exec = (if name == "s_mov_b64" {
|
exec = (if name == "s_mov_b64" {
|
||||||
k
|
k
|
||||||
@@ -304,7 +337,7 @@ fn simulate(insts: &[Inst], wave: u32, saved: Value) -> HashMap<Value, Shape> {
|
|||||||
.then(|| constant(inst))
|
.then(|| constant(inst))
|
||||||
.flatten()
|
.flatten()
|
||||||
else {
|
else {
|
||||||
return shapes(vec, uni, wave);
|
return (vec, uni);
|
||||||
};
|
};
|
||||||
exec = ((exec & 0xFFFF_FFFF) | ((k & 0xFFFF_FFFF) << 32)) & all;
|
exec = ((exec & 0xFFFF_FFFF) | ((k & 0xFFFF_FFFF) << 32)) & all;
|
||||||
continue;
|
continue;
|
||||||
@@ -371,11 +404,7 @@ fn simulate(insts: &[Inst], wave: u32, saved: Value) -> HashMap<Value, Shape> {
|
|||||||
else {
|
else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let l = match *lane {
|
let l = lane_number(lane);
|
||||||
Operand::Const(c) => crate::resource::inline(c),
|
|
||||||
Operand::Literal(x) => Some(x),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
let x = lanes(src, &vec, &uni);
|
let x = lanes(src, &vec, &uni);
|
||||||
if let Some(w) = inst.write(Reg::S(code)) {
|
if let Some(w) = inst.write(Reg::S(code)) {
|
||||||
let s = l
|
let s = l
|
||||||
@@ -664,7 +693,7 @@ fn simulate(insts: &[Inst], wave: u32, saved: Value) -> HashMap<Value, Shape> {
|
|||||||
.collect();
|
.collect();
|
||||||
vec.insert(dst, merged);
|
vec.insert(dst, merged);
|
||||||
}
|
}
|
||||||
shapes(vec, uni, wave)
|
(vec, uni)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn constant(inst: &Inst) -> Option<u64> {
|
fn constant(inst: &Inst) -> Option<u64> {
|
||||||
@@ -688,19 +717,10 @@ fn constant(inst: &Inst) -> Option<u64> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The shape of each value, where every lane agrees on one.
|
/// The shape of a scalar.
|
||||||
fn shapes(
|
fn single(s: Sym, wave: u32) -> Option<Shape> {
|
||||||
vec: HashMap<Value, Vec<Sym>>,
|
let all = u64::MAX >> (64 - wave);
|
||||||
uni: HashMap<Value, Sym>,
|
match s {
|
||||||
wave: u32,
|
|
||||||
) -> HashMap<Value, Shape> {
|
|
||||||
let all = if wave == 64 {
|
|
||||||
u64::MAX
|
|
||||||
} else {
|
|
||||||
u64::from(u32::MAX)
|
|
||||||
};
|
|
||||||
let mut out = HashMap::new();
|
|
||||||
let single = |s: Sym| match s {
|
|
||||||
Sym::K(k) => Some(Shape::K(k)),
|
Sym::K(k) => Some(Shape::K(k)),
|
||||||
Sym::Own(x) | Sym::Uni(x) => Some(Shape::Value(x)),
|
Sym::Own(x) | Sym::Uni(x) => Some(Shape::Value(x)),
|
||||||
Sym::Red {
|
Sym::Red {
|
||||||
@@ -718,9 +738,19 @@ fn shapes(
|
|||||||
high,
|
high,
|
||||||
} if lanes == all => Some(Shape::ActiveF64(lo, hi, mask, high)),
|
} if lanes == all => Some(Shape::ActiveF64(lo, hi, mask, high)),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The shape of each value, where every lane agrees on one.
|
||||||
|
fn shapes(
|
||||||
|
vec: HashMap<Value, Vec<Sym>>,
|
||||||
|
uni: HashMap<Value, Sym>,
|
||||||
|
wave: u32,
|
||||||
|
) -> HashMap<Value, Shape> {
|
||||||
|
let all = u64::MAX >> (64 - wave);
|
||||||
|
let mut out = HashMap::new();
|
||||||
for (v, s) in uni {
|
for (v, s) in uni {
|
||||||
if let Some(sh) = single(s) {
|
if let Some(sh) = single(s, wave) {
|
||||||
out.insert(v, sh);
|
out.insert(v, sh);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1074,6 +1104,40 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A wave OR whose last step stays in the section's VGPR: the lower
|
||||||
|
/// lanes add the first half's to themselves, so only the upper ones
|
||||||
|
/// hold the whole, and the last lane is read after the section (an
|
||||||
|
/// upscaler's programs).
|
||||||
|
#[test]
|
||||||
|
fn one_lane_of_what_a_section_leaves_is_read_after_it() {
|
||||||
|
let words = |lane: u32| {
|
||||||
|
let mut w = WAVE_OR_V7_TO_S7[..14].to_vec(); // .. v_or_b32 v1, v2, v1
|
||||||
|
w.extend([
|
||||||
|
0xD760_0007,
|
||||||
|
0x0001_0101, // v_readlane_b32 s7, v1, 0
|
||||||
|
0x3802_0207, // v_or_b32 v1, s7, v1
|
||||||
|
0xBEFE_0404, // s_mov_b64 exec, s[4:5]
|
||||||
|
0xD760_0006,
|
||||||
|
0x0001_0101 | lane << 9, // v_readlane_b32 s6, v1, lane
|
||||||
|
S_ENDPGM,
|
||||||
|
]);
|
||||||
|
w
|
||||||
|
};
|
||||||
|
let read = |lane: u32| {
|
||||||
|
let p = build(&words(lane), true);
|
||||||
|
let insts = &p.blocks[0].insts;
|
||||||
|
let saved = insts[0].write(Reg::S(4)).unwrap();
|
||||||
|
let v7 = p.inputs.iter().find_map(|&(r, v)| (r == Reg::V(7)).then_some(v)).unwrap();
|
||||||
|
let got = insts[insts.len() - 2].write(Reg::S(6)).unwrap();
|
||||||
|
let sec = §ions(&p, 0)[&0];
|
||||||
|
(sec.reads.get(&got).copied(), Shape::Active(Op::Or, v7, saved, 0))
|
||||||
|
};
|
||||||
|
let (found, whole) = read(63);
|
||||||
|
assert_eq!(found, Some(whole));
|
||||||
|
// Lane 0 has the first half's alone: still a lane read.
|
||||||
|
assert_eq!(read(0).0, None);
|
||||||
|
}
|
||||||
|
|
||||||
/// `WaveActiveSum` of 16-bit floats, as compiled in
|
/// `WaveActiveSum` of 16-bit floats, as compiled in
|
||||||
/// `compute_graph_ops:CS_TransposeVecMatMulAlignedOperation` (0x19c on): the tree the
|
/// `compute_graph_ops:CS_TransposeVecMatMulAlignedOperation` (0x19c on): the tree the
|
||||||
/// lowering reproduces. A tree in another order is not followed.
|
/// lowering reproduces. A tree in another order is not followed.
|
||||||
|
|||||||
+166
-50
@@ -15,6 +15,8 @@
|
|||||||
//! operand's whole possible range: from the encoded register up to the
|
//! operand's whole possible range: from the encoded register up to the
|
||||||
//! program's VGPR count, all read (and all written, for a destination).
|
//! program's VGPR count, all read (and all written, for a destination).
|
||||||
|
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
use crate::ShaderError;
|
use crate::ShaderError;
|
||||||
use crate::gfx10::{self, Fields, Flow, Instruction, Operand};
|
use crate::gfx10::{self, Fields, Flow, Instruction, Operand};
|
||||||
|
|
||||||
@@ -149,7 +151,7 @@ pub fn build(code: &[u8], wave64: bool, vgprs: u16) -> Result<Program, ShaderErr
|
|||||||
}
|
}
|
||||||
leaders.sort_unstable();
|
leaders.sort_unstable();
|
||||||
leaders.dedup();
|
leaders.dedup();
|
||||||
let parked = unspill(&mut list, &leaders, wave64, lanes, vgprs);
|
let parked = unspill(&mut list, &flows, &leaders, wave64, lanes, vgprs);
|
||||||
let block_at = |offset: usize| {
|
let block_at = |offset: usize| {
|
||||||
leaders
|
leaders
|
||||||
.binary_search(&offset)
|
.binary_search(&offset)
|
||||||
@@ -249,8 +251,14 @@ pub fn build(code: &[u8], wave64: bool, vgprs: u16) -> Result<Program, ShaderErr
|
|||||||
/// address) moves as a pair (`s_mov_b64`) when every save and every restore
|
/// address) moves as a pair (`s_mov_b64`) when every save and every restore
|
||||||
/// of the two lanes come together, so a parked lane mask comes back as the
|
/// of the two lanes come together, so a parked lane mask comes back as the
|
||||||
/// mask it is; otherwise its halves come back as two scalars.
|
/// mask it is; otherwise its halves come back as two scalars.
|
||||||
|
///
|
||||||
|
/// The compiler reuses a lane once what it held is dead, for a pair first and
|
||||||
|
/// a scalar later, say. Where that keeps a pair from being one, each life of a
|
||||||
|
/// lane (the saves that reach a restore in common, and those restores) is a
|
||||||
|
/// store of its own.
|
||||||
fn unspill(
|
fn unspill(
|
||||||
list: &mut [gfx10::Instruction],
|
list: &mut [gfx10::Instruction],
|
||||||
|
flows: &[Flow],
|
||||||
leaders: &[usize],
|
leaders: &[usize],
|
||||||
wave64: bool,
|
wave64: bool,
|
||||||
lanes: u8,
|
lanes: u8,
|
||||||
@@ -362,9 +370,6 @@ fn unspill(
|
|||||||
// compiler puts loads and constants between the two: five instructions later
|
// compiler puts loads and constants between the two: five instructions later
|
||||||
// in the ped mesh programs). Only wave64 programs have pairs: a wave32 mask
|
// in the ped mesh programs). Only wave64 programs have pairs: a wave32 mask
|
||||||
// is one dword, and a pair move of it with its neighbour would hide it.
|
// is one dword, and a pair move of it with its neighbour would hide it.
|
||||||
let mut slots: Vec<(u8, u32)> = parks.iter().flatten().map(|p| (p.1, p.3)).collect();
|
|
||||||
slots.sort_unstable();
|
|
||||||
slots.dedup();
|
|
||||||
// instruction -> the instruction that completes its pair
|
// instruction -> the instruction that completes its pair
|
||||||
let partner = |i: usize| -> Option<usize> {
|
let partner = |i: usize| -> Option<usize> {
|
||||||
let (save, reg, code, lane) = parks[i]?;
|
let (save, reg, code, lane) = parks[i]?;
|
||||||
@@ -405,52 +410,163 @@ fn unspill(
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut pairs: Vec<(u8, u32)> = Vec::new();
|
// The store each parking instruction uses, and what the stores are lanes
|
||||||
for &(reg, lane) in &slots {
|
// of: one per (VGPR, lane), or (`split`) one per life of it.
|
||||||
if !wave64 || pairs.iter().any(|&(r, l)| r == reg && l + 1 == lane) || !slots.contains(&(reg, lane + 1)) {
|
let stores_of = |split: bool| -> (Vec<usize>, Vec<(u8, u32)>) {
|
||||||
continue;
|
let slot = |i: usize| parks[i].map(|p| (p.1, p.3));
|
||||||
|
// Instructions of one store, by the first of them.
|
||||||
|
let mut first: Vec<usize> = (0..list.len()).collect();
|
||||||
|
if split {
|
||||||
|
fn root(first: &mut [usize], mut i: usize) -> usize {
|
||||||
|
while first[i] != i {
|
||||||
|
first[i] = first[first[i]];
|
||||||
|
i = first[i];
|
||||||
|
}
|
||||||
|
i
|
||||||
|
}
|
||||||
|
let at = |offset: usize| list.partition_point(|i| i.offset < offset);
|
||||||
|
for save in (0..list.len()).filter(|&i| parks[i].is_some_and(|p| p.0)) {
|
||||||
|
let mut seen = HashSet::new();
|
||||||
|
let mut work = vec![save + 1];
|
||||||
|
while let Some(from) = work.pop() {
|
||||||
|
for i in from..list.len() {
|
||||||
|
if slot(i).is_some() && slot(i) == slot(save) {
|
||||||
|
if parks[i].is_some_and(|p| p.0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let (a, b) = (root(&mut first, save), root(&mut first, i));
|
||||||
|
first[a.max(b)] = a.min(b);
|
||||||
|
}
|
||||||
|
let mut go = |to: usize| {
|
||||||
|
if seen.insert(to) {
|
||||||
|
work.push(to);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match flows[i] {
|
||||||
|
Flow::Next if i + 1 < list.len() && leader(i + 1) => go(i + 1),
|
||||||
|
Flow::Next => continue,
|
||||||
|
Flow::Stop => {}
|
||||||
|
Flow::Jump(t) => go(at(t)),
|
||||||
|
Flow::Branch(t) => {
|
||||||
|
go(at(t));
|
||||||
|
go(i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i in 0..list.len() {
|
||||||
|
first[i] = root(&mut first, i);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let mut of: HashMap<(u8, u32), usize> = HashMap::new();
|
||||||
|
for i in 0..list.len() {
|
||||||
|
if let Some(s) = slot(i) {
|
||||||
|
first[i] = *of.entry(s).or_insert(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let lower: Vec<usize> = (0..list.len()).filter(|&i| parks[i].is_some_and(|p| (p.1, p.3) == (reg, lane))).collect();
|
let mut stores: Vec<((u8, u32), usize)> = (0..list.len())
|
||||||
let upper = parks.iter().flatten().filter(|p| (p.1, p.3) == (reg, lane + 1)).count();
|
.filter(|&i| first[i] == i)
|
||||||
let partners: Vec<Option<usize>> = lower.iter().map(|&i| partner(i)).collect();
|
.filter_map(|i| Some((slot(i)?, i)))
|
||||||
let mut distinct: Vec<usize> = partners.iter().flatten().copied().collect();
|
.collect();
|
||||||
distinct.sort_unstable();
|
stores.sort_unstable();
|
||||||
distinct.dedup();
|
let number = |i: usize| stores.iter().position(|s| s.1 == first[i]);
|
||||||
if partners.iter().all(Option::is_some) && distinct.len() == lower.len() && upper == lower.len() {
|
let of = (0..list.len())
|
||||||
pairs.push((reg, lane));
|
.map(|i| parks[i].and_then(|_| number(i)).unwrap_or(usize::MAX))
|
||||||
|
.collect();
|
||||||
|
(of, stores.into_iter().map(|s| s.0).collect())
|
||||||
|
};
|
||||||
|
// The stores that move as pairs (lower, upper) and every store's code;
|
||||||
|
// none if the spare codes do not suffice.
|
||||||
|
let plan = |split: bool| -> Option<(Vec<usize>, Vec<(usize, usize)>, Vec<u8>)> {
|
||||||
|
let (mut of, stores) = stores_of(split);
|
||||||
|
let mut pairs: Vec<(usize, usize)> = Vec::new();
|
||||||
|
for (n, &(reg, lane)) in stores.iter().enumerate() {
|
||||||
|
if !wave64 || pairs.iter().any(|&(_, upper)| upper == n) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let lower: Vec<usize> = (0..list.len()).filter(|&i| of[i] == n).collect();
|
||||||
|
let partners: Vec<Option<usize>> = lower.iter().map(|&i| partner(i)).collect();
|
||||||
|
let mut distinct: Vec<usize> = partners.iter().flatten().copied().collect();
|
||||||
|
distinct.sort_unstable();
|
||||||
|
distinct.dedup();
|
||||||
|
let Some(upper) = distinct.first().map(|&j| of[j]) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if stores[upper] == (reg, lane + 1)
|
||||||
|
&& partners.iter().all(Option::is_some)
|
||||||
|
&& distinct.len() == lower.len()
|
||||||
|
&& distinct.iter().all(|&j| of[j] == upper)
|
||||||
|
&& of.iter().filter(|&&o| o == upper).count() == lower.len()
|
||||||
|
{
|
||||||
|
pairs.push((n, upper));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
// Lives of one lane that move alike share a code, as they share the lane.
|
||||||
// Codes for the parked scalars: the trap temporaries (108..=123), then
|
let role = |n: usize| {
|
||||||
// user SGPRs the program never names, from the top, then the codes past
|
let (lower, upper) = (pairs.iter().any(|p| p.0 == n), pairs.iter().any(|p| p.1 == n));
|
||||||
// the machine's (`PARKED`..=254; 255 stays unused so that the code after a
|
(stores[n], u8::from(lower) + 2 * u8::from(upper))
|
||||||
// parked one always exists). A material resolve program parks up to 63
|
|
||||||
// scalars and leaves about 20 machine codes unused.
|
|
||||||
let past = usize::from(PARKED);
|
|
||||||
let mut free = [false; 256];
|
|
||||||
for c in (0..105).chain(108..=123) {
|
|
||||||
free[c] = !used[c];
|
|
||||||
}
|
|
||||||
free[past..255].fill(true);
|
|
||||||
let mut codes: Vec<((u8, u32), u8)> = Vec::new();
|
|
||||||
for &(reg, lane) in &pairs {
|
|
||||||
let at = (108..123).chain((0..104).rev()).chain(past..254).find(|&c| free[c] && free[c + 1]);
|
|
||||||
// Not enough spare codes: leave the program as it is.
|
|
||||||
let Some(c) = at else { return Vec::new() };
|
|
||||||
(free[c], free[c + 1]) = (false, false);
|
|
||||||
codes.push(((reg, lane), c as u8));
|
|
||||||
codes.push(((reg, lane + 1), c as u8 + 1));
|
|
||||||
}
|
|
||||||
for &slot in &slots {
|
|
||||||
if codes.iter().any(|c| c.0 == slot) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let Some(c) = (108..=123).chain((0..105).rev()).chain(past..255).find(|&c| free[c]) else {
|
|
||||||
return Vec::new();
|
|
||||||
};
|
};
|
||||||
free[c] = false;
|
let mut shared: Vec<_> = (0..stores.len()).map(role).collect();
|
||||||
codes.push((slot, c as u8));
|
let place: Vec<usize> = {
|
||||||
|
let all = shared.clone();
|
||||||
|
shared.sort_unstable();
|
||||||
|
shared.dedup();
|
||||||
|
all.iter().filter_map(|r| shared.binary_search(r).ok()).collect()
|
||||||
|
};
|
||||||
|
for o in of.iter_mut().filter(|o| **o != usize::MAX) {
|
||||||
|
*o = place[*o];
|
||||||
|
}
|
||||||
|
for p in &mut pairs {
|
||||||
|
*p = (place[p.0], place[p.1]);
|
||||||
|
}
|
||||||
|
pairs.sort_unstable();
|
||||||
|
pairs.dedup();
|
||||||
|
// Codes for the parked scalars: the trap temporaries (108..=123), then
|
||||||
|
// user SGPRs the program never names, from the top, then the codes past
|
||||||
|
// the machine's (`PARKED`..=254; 255 stays unused so that the code after a
|
||||||
|
// parked one always exists). A material resolve program parks up to 63
|
||||||
|
// scalars and leaves about 20 machine codes unused.
|
||||||
|
let past = usize::from(PARKED);
|
||||||
|
let mut free = [false; 256];
|
||||||
|
for c in (0..105).chain(108..=123) {
|
||||||
|
free[c] = !used[c];
|
||||||
|
}
|
||||||
|
free[past..255].fill(true);
|
||||||
|
let mut codes: Vec<Option<u8>> = vec![None; shared.len()];
|
||||||
|
for &(lower, upper) in &pairs {
|
||||||
|
let at = (108..123).chain((0..104).rev()).chain(past..254).find(|&c| free[c] && free[c + 1]);
|
||||||
|
// Not enough spare codes: leave the program as it is.
|
||||||
|
let c = at?;
|
||||||
|
(free[c], free[c + 1]) = (false, false);
|
||||||
|
codes[lower] = Some(c as u8);
|
||||||
|
codes[upper] = Some(c as u8 + 1);
|
||||||
|
}
|
||||||
|
for code in codes.iter_mut().filter(|c| c.is_none()) {
|
||||||
|
let c = (108..=123).chain((0..105).rev()).chain(past..255).find(|&c| free[c])?;
|
||||||
|
free[c] = false;
|
||||||
|
*code = Some(c as u8);
|
||||||
|
}
|
||||||
|
Some((of, pairs, codes.into_iter().flatten().collect()))
|
||||||
|
};
|
||||||
|
let Some(mut chosen) = plan(false) else { return Vec::new() };
|
||||||
|
let lonely = |(of, pairs, _): &(Vec<usize>, Vec<(usize, usize)>, Vec<u8>)| {
|
||||||
|
(0..list.len()).any(|i| {
|
||||||
|
parks[i].is_some_and(|p| p.0)
|
||||||
|
&& partner(i).is_some()
|
||||||
|
&& !pairs.iter().any(|&(lower, upper)| of[i] == lower || of[i] == upper)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
if wave64
|
||||||
|
&& lonely(&chosen)
|
||||||
|
&& let Some(split) = plan(true)
|
||||||
|
&& split.1.len() > chosen.1.len()
|
||||||
|
{
|
||||||
|
chosen = split;
|
||||||
}
|
}
|
||||||
let code_of = |reg: u8, lane: u32| codes.iter().find(|c| c.0 == (reg, lane)).map(|c| c.1);
|
let (store, pairs, codes) = chosen;
|
||||||
let put = |list: &mut [gfx10::Instruction], i: usize, encoding, opcode: u16, word: u32| {
|
let put = |list: &mut [gfx10::Instruction], i: usize, encoding, opcode: u16, word: u32| {
|
||||||
list[i] = gfx10::Instruction {
|
list[i] = gfx10::Instruction {
|
||||||
offset: list[i].offset,
|
offset: list[i].offset,
|
||||||
@@ -466,11 +582,11 @@ fn unspill(
|
|||||||
const NOP: u32 = 0xBF80_0000;
|
const NOP: u32 = 0xBF80_0000;
|
||||||
let mut edits: Vec<(usize, Option<(bool, u8, u8)>)> = Vec::new();
|
let mut edits: Vec<(usize, Option<(bool, u8, u8)>)> = Vec::new();
|
||||||
for i in 0..list.len() {
|
for i in 0..list.len() {
|
||||||
let Some((save, reg, code, lane)) = parks[i] else {
|
let Some((save, _, code, _)) = parks[i] else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let Some(slot) = code_of(reg, lane) else { return Vec::new() };
|
let slot = codes[store[i]];
|
||||||
if pairs.contains(&(reg, lane)) {
|
if pairs.iter().any(|p| p.0 == store[i]) {
|
||||||
let Some(j) = partner(i) else { return Vec::new() };
|
let Some(j) = partner(i) else { return Vec::new() };
|
||||||
// A save moves the pair where its second half is saved; a
|
// A save moves the pair where its second half is saved; a
|
||||||
// restore where its first half is restored.
|
// restore where its first half is restored.
|
||||||
@@ -478,7 +594,7 @@ fn unspill(
|
|||||||
let (dst, src) = if save { (slot, code) } else { (code, slot) };
|
let (dst, src) = if save { (slot, code) } else { (code, slot) };
|
||||||
edits.push((at, Some((true, dst, src))));
|
edits.push((at, Some((true, dst, src))));
|
||||||
edits.push((gone, None));
|
edits.push((gone, None));
|
||||||
} else if lane > 0 && pairs.contains(&(reg, lane - 1)) {
|
} else if pairs.iter().any(|p| p.1 == store[i]) {
|
||||||
// the upper half of a pair: moved with the lower
|
// the upper half of a pair: moved with the lower
|
||||||
} else {
|
} else {
|
||||||
let (dst, src) = if save { (slot, code) } else { (code, slot) };
|
let (dst, src) = if save { (slot, code) } else { (code, slot) };
|
||||||
|
|||||||
@@ -2647,6 +2647,58 @@ pub(crate) mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Two lanes hold a pair (a lane mask, say) and later one of them a
|
||||||
|
/// scalar, as in a water volume program that parks more than the lanes
|
||||||
|
/// of one VGPR hold: the pair still moves as one.
|
||||||
|
#[test]
|
||||||
|
fn a_lane_spilled_to_again_later_does_not_split_an_earlier_pair() {
|
||||||
|
let pair = [
|
||||||
|
0xD761_0000u32,
|
||||||
|
0x0001_0004, // v_writelane_b32 v0, s4, 0
|
||||||
|
0xD761_0000,
|
||||||
|
0x0001_0205, // v_writelane_b32 v0, s5, 1
|
||||||
|
0xBE84_0380, // s_mov_b32 s4, 0
|
||||||
|
0xBE85_0380, // s_mov_b32 s5, 0
|
||||||
|
0xD760_0004,
|
||||||
|
0x0001_0100, // v_readlane_b32 s4, v0, 0
|
||||||
|
0xD760_0005,
|
||||||
|
0x0001_0300, // v_readlane_b32 s5, v0, 1
|
||||||
|
];
|
||||||
|
let scalar = [
|
||||||
|
0xD761_0000u32,
|
||||||
|
0x0001_0006, // v_writelane_b32 v0, s6, 0
|
||||||
|
0xBE86_0380, // s_mov_b32 s6, 0
|
||||||
|
0xD760_0006,
|
||||||
|
0x0001_0100, // v_readlane_b32 s6, v0, 0
|
||||||
|
S_ENDPGM,
|
||||||
|
];
|
||||||
|
let moves = |words: &[u32]| {
|
||||||
|
let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
|
||||||
|
let p = crate::ir::build(&code, true, 8).unwrap();
|
||||||
|
let names: Vec<_> = p.blocks[0].insts.iter().map(|i| i.name()).collect();
|
||||||
|
assert!(!names.iter().any(|n| n.contains("lane")));
|
||||||
|
names.iter().filter(|n| **n == "s_mov_b64").count()
|
||||||
|
};
|
||||||
|
assert_eq!(moves(&[&pair[..], &scalar].concat()), 2);
|
||||||
|
// A restore both saves reach keeps the lane one scalar.
|
||||||
|
let joined = [
|
||||||
|
&pair[..4],
|
||||||
|
&[0xBF84_0002], // s_cbranch_scc0 past the second save
|
||||||
|
&scalar[..2],
|
||||||
|
&pair[4..],
|
||||||
|
&scalar[2..],
|
||||||
|
]
|
||||||
|
.concat();
|
||||||
|
assert_eq!(moves_anywhere(&joined), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn moves_anywhere(words: &[u32]) -> usize {
|
||||||
|
let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
|
||||||
|
let p = crate::ir::build(&code, true, 8).unwrap();
|
||||||
|
let insts = p.blocks.iter().flat_map(|b| &b.insts);
|
||||||
|
insts.filter(|i| i.name() == "s_mov_b64").count()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn descriptors_joined_by_control_flow_are_traced_per_edge() {
|
fn descriptors_joined_by_control_flow_are_traced_per_edge() {
|
||||||
let r = track_words(
|
let r = track_words(
|
||||||
|
|||||||
Reference in new issue
Block a user