Translator tests: the barriers our own passes insert keep Metal's flags in OpenCL mode
The pass that reads an OpenCL-mode air.wg.barrier's flags as OpenCL's
(canon::opencl::barriers) runs in specialization, after the last SCCP
and before the post-check (canon/mod.rs, specialize_linked). Our
passes that write a barrier:
- canon::async_copy writes air.simdgroup.barrier(mem_threadgroup)
(async_copy.rs, NAME at line 295). It runs before the pass, but the
pass takes air.wg.barrier alone, so this barrier keeps Metal's flags.
- lower::simd::lockstep::insert writes air.wg.barrier and
air.simdgroup.barrier with Metal's mask (lockstep.rs, at lines 1133
to 1149). It runs from pipeline::compile's order_specialized
(compile.rs, at line 2832), on the entry specialization returns, so
after the pass.
- The partial-threadgroups variant's vote (lower/simd/workgroup.rs,
at lines 549 and 552), the mesh and object barrier
(lower/mesh/mod.rs, at line 1253), tile staging
(lower/access/tile.rs, at line 1122) and cooperative imageblock
staging (lower/texture/emit.rs, at line 3289) emit OpControlBarrier
in lowering directly, with no air.wg.barrier call the pass could
see.
The new test runs where a pass of ours does add barriers: the
lockstep pass under the resident-table domains, on the 780M's profile.
These are the barriers that broke the refusal test's control at
cb77b3f60d: two air.wg.barrier calls with mem_device and
mem_threadgroup (UniformMemory | WorkgroupMemory). The kernel holds
OpenCL barriers 1, 2 and 3, then stores to its __local slot again
right before reading it. In a threadgroup narrower than 32, two
threads of a simdgroup can share that slot, and no barrier orders the
pair, so the pass inserts one. In the ordered entry the program's
barriers read as OpenCL's (threadgroup, device, both), and every
inserted barrier's decoded flags are Metal's reading of its own
operand, threadgroup memory among them. Without the fix the first
assertion fails, as the program's 1 and 2 read as mem_device and
mem_threadgroup. If the pass ran after lockstep, the inserted
barriers' mem_threadgroup (2) would read as CLK_GLOBAL_MEM_FENCE, and
the second assertion would fail.
This commit is contained in:
1 parent
578f8c9df5
commit
5af18cd7a7
1 file changed
+92
@@ -1038,3 +1038,95 @@ fn an_opencl_mode_simdgroup_barrier_keeps_metals_flags() {
|
||||
assert_eq!(storage, [0x100], "OpenCL mode {open_cl_mode}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The barriers our own passes write keep Metal's flags while the program's are read as OpenCL's.
|
||||
/// Specialization writes no `air.wg.barrier` before it reads the program's flags (an asynchronous
|
||||
/// copy's wait is a simdgroup barrier); the lockstep pass writes its own after specialization, in
|
||||
/// Metal's `mem_flags`, where it runs: under the resident-table domains, here on the 780M's
|
||||
/// profile. The kernel holds barriers of `CLK_LOCAL_MEM_FENCE`, `CLK_GLOBAL_MEM_FENCE` and both,
|
||||
/// then stores to its `__local` slot again right before it reads it: two threads of one simdgroup
|
||||
/// share a slot in a threadgroup narrower than 32, and no barrier orders that pair, so the pass
|
||||
/// inserts one. In the ordered entry each barrier the program wrote orders what OpenCL's flags name,
|
||||
/// and each the pass inserted what its operand names in Metal's `mem_flags`, threadgroup memory
|
||||
/// among it.
|
||||
#[test]
|
||||
fn an_opencl_mode_kernel_keeps_the_flags_of_the_barriers_we_insert() {
|
||||
use crate::air::intrinsics::{AirIntrinsic, MemFlags, Sync};
|
||||
use crate::air::ir::{Callee, Const, InstKind, Value};
|
||||
use crate::device::DevicePolicy;
|
||||
use crate::library::FunctionConstantValues;
|
||||
use crate::pipeline::compile::{specialized_bitcode_entry, EntryRef};
|
||||
use crate::pipeline::testing::{meter, profile};
|
||||
let calls = format!(
|
||||
"{} store float 2.000000e+00, ptr addrspace(3) %pt, align 4\n",
|
||||
wrapper_calls(&[1, 2, 3])
|
||||
);
|
||||
let text = barrier_kernel(&calls, BARRIER_TWO_OPERANDS);
|
||||
let bitcode = match metal2vulkan_devtools::llvm_assemble_text(&text) {
|
||||
Ok(b) => b,
|
||||
Err(AssembleError::Missing(why)) => {
|
||||
eprintln!("SKIPPED: {why}");
|
||||
return;
|
||||
}
|
||||
Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the module: {why}"),
|
||||
};
|
||||
let p = profile("amd-780m-radv", DevicePolicy::RESIDENT_TABLES).expect("the 780M's profile");
|
||||
let mut inputs = LowerInputs::new();
|
||||
inputs.open_cl_mode = true;
|
||||
let consts = FunctionConstantValues::defaults();
|
||||
let entry = specialized_bitcode_entry(
|
||||
&bitcode,
|
||||
EntryRef::Name("__entry_b"),
|
||||
(&consts, &[], &inputs),
|
||||
&p,
|
||||
&mut meter(),
|
||||
)
|
||||
.expect("the ordered entry");
|
||||
let body = (entry.module.func(entry.func))
|
||||
.and_then(|f| f.body.as_ref())
|
||||
.expect("the entry's body");
|
||||
let (mut program, mut inserted) = (Vec::new(), Vec::new());
|
||||
for (id, inst) in body.insts.iter() {
|
||||
let InstKind::Call(call) = &inst.kind else {
|
||||
continue;
|
||||
};
|
||||
let Callee::Air {
|
||||
op: AirIntrinsic::Sync(Sync::Barrier { flags, .. }),
|
||||
..
|
||||
} = call.callee
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let raw = match call.args.first() {
|
||||
Some(Value::Const(c)) => match entry.module.consts().get(*c).map(|k| &k.value) {
|
||||
Some(Const::Int(v)) => *v,
|
||||
other => panic!("flags {other:?}"),
|
||||
},
|
||||
other => panic!("flags {other:?}"),
|
||||
};
|
||||
if entry.lockstep.inserted(id) {
|
||||
inserted.push((raw, flags));
|
||||
} else {
|
||||
program.push((raw, flags));
|
||||
}
|
||||
}
|
||||
let local = MemFlags {
|
||||
threadgroup: true,
|
||||
..MemFlags::default()
|
||||
};
|
||||
let global = MemFlags {
|
||||
device: true,
|
||||
..MemFlags::default()
|
||||
};
|
||||
let both = MemFlags {
|
||||
device: true,
|
||||
threadgroup: true,
|
||||
..MemFlags::default()
|
||||
};
|
||||
assert_eq!(program, [(1, local), (2, global), (3, both)]);
|
||||
assert!(!inserted.is_empty(), "the lockstep pass inserted nothing");
|
||||
for (raw, flags) in inserted {
|
||||
assert_eq!(Some(flags), MemFlags::from_raw(raw), "an inserted barrier");
|
||||
assert!(flags.threadgroup, "an inserted barrier: {flags:?}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user