Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,27 @@ fn translate_entry(code: &[u8], o: KernelOptions, entry: Entry, compact: Option<
}

/// The memory windows of `mem_windows` and whether they come from the descriptor heap
/// (`recomp::HEAP_WINDOWS` in the count, `XCL_HEAP_WINDOWS=1`; Some(globallycoherent), and
/// `mems` then only counts them). Compute work across groups relies on coherent memory
/// (as the HLSL compute mode's `globallycoherent` windows).
fn mem_windows(s: &mut Shader, mem_windows: u32, coherent: bool) -> (Vec<RawUav>, Option<bool>) {
/// (`recomp::HEAP_WINDOWS` in the count, `XCL_HEAP_WINDOWS=1`). For root-UAV compute shaders
/// in `CoherenceMode::PerInstruction`, `mems` are plain windows `u2..` and `mems_coh` are
/// `globallycoherent` aliases `u6..` bound to the same GPU VAs.
fn mem_windows(s: &mut Shader, mem_windows: u32, coherence: crate::gpu::recomp::CoherenceMode) -> (Vec<RawUav>, Vec<RawUav>, bool) {
use crate::gpu::recomp::CoherenceMode;
let heap = mem_windows & crate::gpu::recomp::HEAP_WINDOWS != 0;
let max = if heap { crate::gpu::uma::MAX_HEAP_WINDOWS as u32 } else { crate::gpu::uma::MAX_WINDOWS as u32 };
let n = (mem_windows & !crate::gpu::recomp::HEAP_WINDOWS).clamp(1, max);
if heap && n > 1 {
let z = s.m.i32c(0);
return (vec![RawUav { handle: z, binding: None }; n as usize], Some(coherent));
return (vec![RawUav { handle: z, binding: None }; n as usize], Vec::new(), true);
}
match coherence {
CoherenceMode::None => ((0..n).map(|k| s.raw_uav(2 + k, 0)).collect(), Vec::new(), false),
CoherenceMode::AllCoherent => ((0..n).map(|k| s.raw_uav_coherent(2 + k, 0)).collect(), Vec::new(), false),
CoherenceMode::PerInstruction => (
(0..n).map(|k| s.raw_uav(2 + k, 0)).collect(),
(0..n).map(|k| s.raw_uav_coherent(6 + k, 0)).collect(),
false,
),
}
((0..n).map(|k| if coherent { s.raw_uav_coherent(2 + k, 0) } else { s.raw_uav(2 + k, 0) }).collect(), None)
}

fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry, compact: Option<bool>) -> Result<Kernel, String> {
Expand Down Expand Up @@ -447,7 +456,12 @@ impl Lw {
if stage == Stage::Pixel && uses_pops(insts) {
s.enable_rov();
}
let (mems, heap_mem) = mem_windows(&mut s, o.mem_windows, cs.is_some());
let coherence = if cs.is_some() {
crate::gpu::recomp::coherence_mode(insts)
} else {
crate::gpu::recomp::CoherenceMode::None
};
let (mems, mems_coh, heap_mem) = mem_windows(&mut s, o.mem_windows, coherence);
let i32 = s.m.i32();
let z = s.m.i32c(0);
let mut lw = Lw {
Expand All @@ -461,7 +475,10 @@ impl Lw {
upper: z,
half: z,
mems,
mems_coh,
heap_mem,
coherence,
inst_coherent_load: coherence == crate::gpu::recomp::CoherenceMode::AllCoherent,
res,
fmt_table: None,
fmt_desc_cache: HashMap::new(),
Expand Down
19 changes: 16 additions & 3 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ pub struct Lw {
pub half: Value,
/// the memory windows `mem`, `mem1`.. (u2..)
pub mems: Vec<RawUav>,
/// the windows come from the descriptor heap (`lower_window`; `mems` only counts them):
/// Some(globallycoherent)
pub heap_mem: Option<bool>,
/// coherent memory-window aliases `u6..` for root-UAV `CoherenceMode::PerInstruction`
pub mems_coh: Vec<RawUav>,
/// true when the memory windows come from `ResourceDescriptorHeap` (`XCL_HEAP_WINDOWS=1`)
pub heap_mem: bool,
/// flat-memory coherence policy for this shader
pub(crate) coherence: crate::gpu::recomp::CoherenceMode,
/// whether loads emitted while lowering the current instruction use the coherent view
pub inst_coherent_load: bool,
pub res: Option<RawUav>,
/// the buffer format table (`FMT_INFO`), created on first use
pub fmt_table: Option<Value>,
Expand Down Expand Up @@ -937,6 +942,14 @@ impl Lw {
let name = d.inst.name.to_ascii_lowercase();
self.pc = d.pc;
self.dpp_enable = None;
self.inst_coherent_load = match self.coherence {
crate::gpu::recomp::CoherenceMode::None => false,
crate::gpu::recomp::CoherenceMode::AllCoherent => true,
crate::gpu::recomp::CoherenceMode::PerInstruction => {
crate::gpu::recomp::inst_has_mem_coherence_bits(d)
|| crate::gpu::recomp::inst_is_flat_atomic(d)
}
};
if name == "s_sendmsg" && self.mesh.is_some() {
return self.mesh_sendmsg(d);
}
Expand Down
22 changes: 14 additions & 8 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl Lw {
&mut self,
mut guard: Option<Value>,
out_tys: &[super::module::Ty],
coherent: bool,
f: &mut dyn FnMut(&mut Lw, RawUav) -> Vec<Value>,
) -> Vec<Value> {
if let Some(c) = guard {
Expand All @@ -172,7 +173,7 @@ impl Lw {
}
// Descriptor-heap windows (`#1693`, `XCL_HEAP_WINDOWS=1`): single guarded heap lookup
// combined with `guard`, stacking cleanly with vector loads/stores & FormatLoad opt.
if let Some(coherent) = self.heap_mem {
if self.heap_mem {
let windows = self.mems.len() as u32;
let w = self.rd(super::lower::VAR_WIN);
let inside = self.cmpc(Pred::ULt, w, windows);
Expand Down Expand Up @@ -218,14 +219,15 @@ impl Lw {
}
return out;
}
let use_coh = coherent && !self.mems_coh.is_empty();
// Single window, or window known at translation time: at most one branch for `guard`.
let fixed_mem = if self.mems.len() <= 1 {
Some(self.mems[0])
Some(if use_coh { self.mems_coh[0] } else { self.mems[0] })
} else {
let w = self.rd(super::lower::VAR_WIN);
if let Some(k) = self.s.m.const_u64(w) {
if (k as usize) < self.mems.len() {
Some(self.mems[k as usize])
Some(if use_coh { self.mems_coh[k as usize] } else { self.mems[k as usize] })
} else {
return out_tys.iter().map(|&t| self.s.m.const_int(t, 0)).collect();
}
Expand Down Expand Up @@ -268,7 +270,7 @@ impl Lw {
// Multi-window dispatch via a single LLVM `switch` (default -> `join` for out-of-range
// or inactive lane, matching `#1693` window semantics).
let w = self.rd(super::lower::VAR_WIN);
let mems = self.mems.clone();
let mems = if use_coh { self.mems_coh.clone() } else { self.mems.clone() };
let n = mems.len();
let from = self.cur();
let arms: Vec<super::module::Block> = (0..n).map(|_| self.block(None)).collect();
Expand Down Expand Up @@ -322,26 +324,30 @@ impl Lw {
) -> Vec<Value> {
let i32 = self.s.m.i32();
let tys = vec![i32; n];
self.with_mem_many_tys(guard, &tys, f)
let coherent = self.inst_coherent_load;
self.with_mem_many_tys(guard, &tys, coherent, f)
}

pub(crate) fn with_mem_void(
&mut self,
guard: Option<Value>,
f: &mut dyn FnMut(&mut Lw, RawUav),
) {
let _ = self.with_mem_many_tys(guard, &[], &mut |lw, m| {
let coherent = self.coherence != crate::gpu::recomp::CoherenceMode::None;
let _ = self.with_mem_many_tys(guard, &[], coherent, &mut |lw, m| {
f(lw, m);
Vec::new()
});
}
fn mraw_atomic(&mut self, op: AtomicOp, a: Value, v: Value) -> Value {
let ty = self.s.m.type_of(v);
self.with_mem_many_tys(None, &[ty], &mut |lw, m| vec![lw.s.raw_atomic(m, op, a, v)])[0]
let coherent = self.coherence != crate::gpu::recomp::CoherenceMode::None;
self.with_mem_many_tys(None, &[ty], coherent, &mut |lw, m| vec![lw.s.raw_atomic(m, op, a, v)])[0]
}
fn mraw_cmpxchg(&mut self, a: Value, c: Value, n: Value) -> Value {
let ty = self.s.m.type_of(c);
self.with_mem_many_tys(None, &[ty], &mut |lw, m| vec![lw.s.raw_cmpxchg(m, a, c, n)])[0]
let coherent = self.coherence != crate::gpu::recomp::CoherenceMode::None;
self.with_mem_many_tys(None, &[ty], coherent, &mut |lw, m| vec![lw.s.raw_cmpxchg(m, a, c, n)])[0]
}
/// `mem.Load(a & ~3)`
pub fn mem_ld(&mut self, a: Value) -> Value {
Expand Down
Loading
Loading