|
|
@@ -259,6 +259,9 @@ struct Lower<'a> {
|
|
|
/// they are globally coherent; a load in a loop that only waits would
|
|
|
/// be made once otherwise.
|
|
|
coherent: HashSet<(usize, u32)>,
|
|
|
+ /// In the instruction being lowered: the host copy its root view's bytes
|
|
|
+ /// are in ([`Lower::root_memory`]).
|
|
|
+ root_copy: Option<V>,
|
|
|
mask_phis: HashMap<Value, V>,
|
|
|
pending_masks: Vec<(V, usize, usize)>,
|
|
|
data_phis: Vec<(V, usize, usize)>,
|
|
|
@@ -1572,6 +1575,7 @@ impl<'a> Lower<'a> {
|
|
|
read_phis: HashSet::new(),
|
|
|
stale: HashSet::new(),
|
|
|
coherent: HashSet::new(),
|
|
|
+ root_copy: None,
|
|
|
ordered: if x.native.stage == HwStage::Ps {
|
|
|
ordered_section(p)
|
|
|
} else {
|
|
|
@@ -1735,6 +1739,7 @@ impl<'a> Lower<'a> {
|
|
|
let Fields::Flat { dst, .. } = &inst.fields else {
|
|
|
unreachable!()
|
|
|
};
|
|
|
+ self.root_copy = None;
|
|
|
let name = inst.name();
|
|
|
if self.scalar.get(&(b, i)).is_some_and(|a| a.param == MEMORY) {
|
|
|
return self.memory_flat(inst, b, i);
|
|
|
@@ -1778,8 +1783,15 @@ impl<'a> Lower<'a> {
|
|
|
if let Some(bias) = self.root_bias(a.param) {
|
|
|
off = self.s.m.bin(c, Bin::Add, off, bias);
|
|
|
}
|
|
|
- Some(self.handle((b, i), &src, false, (11, 0))?)
|
|
|
+ match self.root_memory(&src, off, guard, false) {
|
|
|
+ Some((h, byte)) => {
|
|
|
+ off = byte;
|
|
|
+ Some(h)
|
|
|
+ }
|
|
|
+ None => Some(self.handle((b, i), &src, false, (11, 0))?),
|
|
|
+ }
|
|
|
};
|
|
|
+ let c = self.cur;
|
|
|
let dword = |l: &mut Self, byte: V| -> Result<V, Why> {
|
|
|
match h {
|
|
|
None => l.code_dword(byte),
|
|
|
@@ -2999,9 +3011,12 @@ impl<'a> Lower<'a> {
|
|
|
/// `SLOT_MEMORY`): the handle of the raw view of the host copy holding
|
|
|
/// it and the byte in that view. A page without a copy reads zeros
|
|
|
/// (and drops writes) through the null view at the first slot, and its
|
|
|
- /// number is left for the host, for lanes that run (`guard`).
|
|
|
- fn memory(&mut self, lo: V, hi: V, guard: Option<V>) -> (V, V) {
|
|
|
- use super::shader::{MEMORY_FAULTS, MEMORY_PAGES, SLOT_FAULTS, SLOT_MEMORY, USER_MEMORY_BASE};
|
|
|
+ /// number is left for the host, for lanes that run (`guard`). Goes on in
|
|
|
+ /// another block.
|
|
|
+ /// With `store`, a page that is not to be written ([`super::shader::MEMORY_READ_ONLY`])
|
|
|
+ /// is the null view.
|
|
|
+ fn memory(&mut self, lo: V, hi: V, guard: Option<V>, store: bool) -> (V, V) {
|
|
|
+ use super::shader::{MEMORY_FAULTS, MEMORY_PAGES, MEMORY_READ_ONLY, SLOT_FAULTS, SLOT_MEMORY, USER_MEMORY_BASE};
|
|
|
let c = self.cur;
|
|
|
let (i32t, res) = (self.s.t.i32, self.s.t.res_ret);
|
|
|
let k = |l: &mut Self, v: u32| l.s.m.i32(v);
|
|
|
@@ -3030,22 +3045,26 @@ impl<'a> Lower<'a> {
|
|
|
&[directory, at, iu, both, align],
|
|
|
);
|
|
|
let (copy, start) = (self.s.m.extract(c, entry, 0), self.s.m.extract(c, entry, 1));
|
|
|
- let (copy, start) = (self.as_int(copy), self.as_int(start));
|
|
|
- // A page without a copy: its number, at a place by the number, or
|
|
|
- // at the sink for lanes that do not run and pages that have one.
|
|
|
+ let (number, start) = (self.as_int(copy), self.as_int(start));
|
|
|
+ let mask = k(self, !MEMORY_READ_ONLY);
|
|
|
+ let copy = self.s.m.bin(c, Bin::And, number, mask);
|
|
|
+ // A page without a copy: its number, at a place by the number. In a
|
|
|
+ // branch: the notes lie in the PC's memory, and a store there with
|
|
|
+ // every node of every ray would be most of what a trace does.
|
|
|
let zero = k(self, 0);
|
|
|
let mut missing = self.s.m.cmp(c, pred::EQ, copy, zero);
|
|
|
if let Some(g) = guard {
|
|
|
missing = self.s.m.bin(c, Bin::And, missing, g);
|
|
|
}
|
|
|
let places = k(self, MEMORY_FAULTS - 1);
|
|
|
+ let faults = self.s.root_uav_handle(SLOT_FAULTS);
|
|
|
+ let noting = self.begin_guarded(Some(missing));
|
|
|
+ let c = self.cur;
|
|
|
let place = self.s.m.bin(c, Bin::URem, page, places);
|
|
|
let one = k(self, 1);
|
|
|
let place = self.s.m.bin(c, Bin::Add, place, one);
|
|
|
let k2 = k(self, 2);
|
|
|
let place = self.s.m.bin(c, Bin::Shl, place, k2);
|
|
|
- let place = self.s.m.select(c, missing, place, zero);
|
|
|
- let faults = self.s.root_uav_handle(SLOT_FAULTS);
|
|
|
let void = self.s.t.void;
|
|
|
let mask = self.s.m.i8(1);
|
|
|
self.s.op(
|
|
|
@@ -3056,15 +3075,69 @@ impl<'a> Lower<'a> {
|
|
|
140,
|
|
|
&[faults, place, iu, page, iu, iu, iu, mask, align],
|
|
|
);
|
|
|
+ self.end_guarded(noting, &[]);
|
|
|
+ let c = self.cur;
|
|
|
+ let copy = if store {
|
|
|
+ let closed = self.s.m.cmp(c, pred::SLT, number, zero);
|
|
|
+ self.s.m.select(c, closed, zero, copy)
|
|
|
+ } else {
|
|
|
+ copy
|
|
|
+ };
|
|
|
let base = self.host_word(RootType::Other(USER_MEMORY_BASE), 0, 0);
|
|
|
let slot = self.s.m.bin(c, Bin::Add, base, copy);
|
|
|
- let h = self.s.heap_handle(c, slot, false, true, (11 | 0x1000, 0));
|
|
|
+ // The builder's threads read what others of the dispatch wrote (a
|
|
|
+ // refit climbs from children to parents): globally coherent.
|
|
|
+ let coherent = if crate::spill::is_spilled(self.x.native) { 0x4000 } else { 0 };
|
|
|
+ let h = self.s.heap_handle(c, slot, false, true, (11 | 0x1000 | coherent, 0));
|
|
|
let k64k = k(self, 0xFFFF);
|
|
|
let within = self.s.m.bin(c, Bin::And, lo, k64k);
|
|
|
let byte = self.s.m.bin(c, Bin::Add, start, within);
|
|
|
(h, byte)
|
|
|
}
|
|
|
|
|
|
+ /// The title address in the root view `src` is built around, in a program
|
|
|
+ /// with a spilled user-data table (`shader.rs` `USER_ROOT_ADDRESS`).
|
|
|
+ fn root_address(&mut self, src: &Source) -> Option<(V, V)> {
|
|
|
+ use super::shader::USER_ROOT_ADDRESS;
|
|
|
+ let Source::Built { address, .. } = src else {
|
|
|
+ return None;
|
|
|
+ };
|
|
|
+ let rp = *self.x.native.layout.get(address.param)?;
|
|
|
+ if rp.root != RootType::Descriptor || !crate::spill::is_spilled(self.x.native) {
|
|
|
+ return None;
|
|
|
+ }
|
|
|
+ let used = self.x.native.used.iter().find(|u| u.user_data == rp.user_data)?;
|
|
|
+ if !matches!(used.kind, ResourceType::Srv | ResourceType::Uav) {
|
|
|
+ return None;
|
|
|
+ }
|
|
|
+ let lo = self.host_word(RootType::Other(USER_ROOT_ADDRESS), rp.slot, 0);
|
|
|
+ let hi = self.host_word(RootType::Other(USER_ROOT_ADDRESS + 1), rp.slot, 0);
|
|
|
+ Some((lo, hi))
|
|
|
+ }
|
|
|
+
|
|
|
+ /// For an access through such a root view: the raw view of the host copy
|
|
|
+ /// and the byte in it of the view's byte `off` (signed: an offset below
|
|
|
+ /// the address stays below it). [`Lower::handle`] gives the same view for
|
|
|
+ /// the rest of the instruction. None for any other source. Goes on in
|
|
|
+ /// another block.
|
|
|
+ /// shortcut: an access of several dwords is taken not to cross the end
|
|
|
+ /// of a host copy (the builder's records lie on their size).
|
|
|
+ fn root_memory(&mut self, src: &Source, off: V, guard: Option<V>, store: bool) -> Option<(V, V)> {
|
|
|
+ let (lo, hi) = self.root_address(src)?;
|
|
|
+ let c = self.cur;
|
|
|
+ let i32t = self.s.t.i32;
|
|
|
+ let sum = self.s.m.bin(c, Bin::Add, lo, off);
|
|
|
+ let wrapped = self.s.m.cmp(c, pred::IULT, sum, lo);
|
|
|
+ let carry = self.s.m.cast(c, Cast::ZExt, wrapped, i32t);
|
|
|
+ let k31 = self.s.m.i32(31);
|
|
|
+ let sign = self.s.m.bin(c, Bin::AShr, off, k31);
|
|
|
+ let hi = self.s.m.bin(c, Bin::Add, hi, carry);
|
|
|
+ let hi = self.s.m.bin(c, Bin::Add, hi, sign);
|
|
|
+ let (h, byte) = self.memory(sum, hi, guard, store);
|
|
|
+ self.root_copy = Some(h);
|
|
|
+ Some((h, byte))
|
|
|
+ }
|
|
|
+
|
|
|
/// `global_load_*`, `global_store_*` and `global_atomic_*` on title
|
|
|
/// memory by address (`resource::MEMORY`): the address is the VGPR
|
|
|
/// pair, each lane its own.
|
|
|
@@ -3094,7 +3167,6 @@ impl<'a> Lower<'a> {
|
|
|
let (lo, hi) = (word(self, 0)?, word(self, 1)?);
|
|
|
let (lo, hi) = self.address_plus(lo, hi, i32::from(*offset));
|
|
|
let guard = self.guard(b, i)?;
|
|
|
- let c = self.cur;
|
|
|
let (i32t, res, void) = (self.s.t.i32, self.s.t.res_ret, self.s.t.void);
|
|
|
let iu = self.s.m.undef(i32t);
|
|
|
let align = self.s.m.i32(4);
|
|
|
@@ -3111,7 +3183,8 @@ impl<'a> Lower<'a> {
|
|
|
let Some(Operand::Vector { reg, .. }) = *dst else {
|
|
|
return Err("a global load result");
|
|
|
};
|
|
|
- let (h, byte) = self.memory(lo, hi, guard);
|
|
|
+ let (h, byte) = self.memory(lo, hi, guard, false);
|
|
|
+ let c = self.cur;
|
|
|
let mask = self.s.m.i8((1u8 << n) - 1);
|
|
|
let r = self.s.op(
|
|
|
c,
|
|
|
@@ -3132,7 +3205,7 @@ impl<'a> Lower<'a> {
|
|
|
let n = count(rest)?;
|
|
|
let d = data.as_ref().ok_or("global store data")?;
|
|
|
let vals = self.vgprs(inst, d, n)?;
|
|
|
- let (h, byte) = self.memory(lo, hi, guard);
|
|
|
+ let (h, byte) = self.memory(lo, hi, guard, true);
|
|
|
let mut args = vec![h, byte, iu];
|
|
|
args.extend(&vals);
|
|
|
args.resize(3 + 4, iu);
|
|
|
@@ -3159,7 +3232,8 @@ impl<'a> Lower<'a> {
|
|
|
Some(x) => self.i32_of(x)?,
|
|
|
None => self.s.m.i32(0),
|
|
|
};
|
|
|
- let (h, byte) = self.memory(lo, hi, guard);
|
|
|
+ let (h, byte) = self.memory(lo, hi, guard, true);
|
|
|
+ let c = self.cur;
|
|
|
// DXIL's atomic operations: add 0, and 1, or 2, xor 3, signed
|
|
|
// minimum 4 and maximum 5, unsigned 6 and 7, exchange 8.
|
|
|
let simple = match op {
|
|
|
@@ -3293,7 +3367,8 @@ impl<'a> Lower<'a> {
|
|
|
for first in (0..count).step_by(4) {
|
|
|
let n = (count - first).min(4);
|
|
|
let (lo, hi) = self.address_plus(lo, hi, offset + 4 * i32::from(first));
|
|
|
- let (h, byte) = self.memory(lo, hi, None);
|
|
|
+ let (h, byte) = self.memory(lo, hi, None, false);
|
|
|
+ let c = self.cur;
|
|
|
let mask = self.s.m.i8((1u8 << n) - 1);
|
|
|
let r = self.s.op(
|
|
|
c,
|
|
|
@@ -5830,6 +5905,7 @@ impl<'a> Lower<'a> {
|
|
|
else {
|
|
|
unreachable!()
|
|
|
};
|
|
|
+ self.root_copy = None;
|
|
|
let name = inst.name();
|
|
|
if let Some(a) = self.scalar.get(&site).copied().filter(|a| a.param == CODE) {
|
|
|
return self.code_smem(inst, a);
|
|
|
@@ -5938,7 +6014,14 @@ impl<'a> Lower<'a> {
|
|
|
return Ok(());
|
|
|
}
|
|
|
if matches!(kind, Some(u) if u.kind == ResourceType::Srv) {
|
|
|
- let h = self.handle(site, src, false, (11, 0))?;
|
|
|
+ let h = match self.root_memory(src, addr, None, false) {
|
|
|
+ Some((h, byte)) => {
|
|
|
+ addr = byte;
|
|
|
+ h
|
|
|
+ }
|
|
|
+ None => self.handle(site, src, false, (11, 0))?,
|
|
|
+ };
|
|
|
+ let c = self.cur;
|
|
|
for k in 0..count {
|
|
|
let off = self.s.m.i32(4 * k);
|
|
|
let a = self.s.m.bin(c, Bin::Add, addr, off);
|
|
|
@@ -6645,13 +6728,14 @@ impl<'a> Lower<'a> {
|
|
|
|
|
|
/// For a root view the host binds as a raw view that may begin before
|
|
|
/// the title's address (`shader.rs` `USER_VIEW_BIAS`): the bytes to add
|
|
|
- /// to offsets through it. Every root UAV, and the root SRVs of programs
|
|
|
- /// with a spilled user-data table (the others keep the views they
|
|
|
+ /// to offsets through it. Every root UAV (root SRVs keep the views they
|
|
|
/// have).
|
|
|
fn root_bias(&mut self, param: usize) -> Option<V> {
|
|
|
use super::shader::USER_VIEW_BIAS;
|
|
|
let rp = *self.x.native.layout.get(param)?;
|
|
|
- if rp.root != RootType::Descriptor {
|
|
|
+ // (Title memory by address where the user-data table is spilled:
|
|
|
+ // [`Lower::root_memory`].)
|
|
|
+ if rp.root != RootType::Descriptor || crate::spill::is_spilled(self.x.native) {
|
|
|
return None;
|
|
|
}
|
|
|
let kind = self
|
|
|
@@ -6661,12 +6745,7 @@ impl<'a> Lower<'a> {
|
|
|
.iter()
|
|
|
.find(|u| u.user_data == rp.user_data)
|
|
|
.map(|u| u.kind);
|
|
|
- let biased = match kind {
|
|
|
- Some(ResourceType::Uav) => true,
|
|
|
- Some(ResourceType::Srv) => crate::spill::is_spilled(self.x.native),
|
|
|
- _ => false,
|
|
|
- };
|
|
|
- biased.then(|| self.host_word(RootType::Other(USER_VIEW_BIAS), rp.slot, 0))
|
|
|
+ (kind == Some(ResourceType::Uav)).then(|| self.host_word(RootType::Other(USER_VIEW_BIAS), rp.slot, 0))
|
|
|
}
|
|
|
|
|
|
/// The used-resource entry a descriptor loaded from a table falls in.
|
|
|
@@ -6739,6 +6818,7 @@ impl<'a> Lower<'a> {
|
|
|
Ok(self.s.sampler_handle(self.cur, n))
|
|
|
}
|
|
|
Source::Constant(_) => Err("a constant resource descriptor"),
|
|
|
+ Source::Built { .. } if self.root_copy.is_some() => self.root_copy.ok_or("no copy"),
|
|
|
Source::Built { address, .. } => {
|
|
|
let rp = *self
|
|
|
.x
|
|
|
@@ -11045,6 +11125,7 @@ impl<'a> Lower<'a> {
|
|
|
}
|
|
|
|
|
|
fn buffer(&mut self, inst: &Inst, b: usize, i: usize) -> Result<(), Why> {
|
|
|
+ self.root_copy = None;
|
|
|
let Fields::Buffer {
|
|
|
data,
|
|
|
addr,
|
|
|
@@ -11207,6 +11288,20 @@ impl<'a> Lower<'a> {
|
|
|
}
|
|
|
return Ok(());
|
|
|
}
|
|
|
+ if self.root_address(src).is_some() {
|
|
|
+ // Title memory by address: the byte in the host copy from here on.
|
|
|
+ if let Some(element) = index {
|
|
|
+ if used.is_none_or(|u| u.stride != 1) {
|
|
|
+ return Err("an indexed access through a root view by address");
|
|
|
+ }
|
|
|
+ off = self.s.m.bin(c, Bin::Add, element, off);
|
|
|
+ index = None;
|
|
|
+ }
|
|
|
+ if let Some((_, byte)) = self.root_memory(src, off, guard, store || atomic.is_some()) {
|
|
|
+ off = byte;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let c = self.cur;
|
|
|
let uav = store || atomic.is_some() || used.is_some_and(|u| u.kind == ResourceType::Uav);
|
|
|
let mut u = if uav { 0x1000 } else { 0 };
|
|
|
if uav && !typed && self.entry(src).is_some_and(|e| self.coherent.contains(&e)) {
|