Tests: OpenCL C's mem_fence fences at acq_rel, the order Apple's compiler writes

Apple's OpenCL compiler (Metal's OpenCL mode) writes mem_fence(flags) as
air.atomic.fence(flags, 4, 1): order 4, acq_rel, after its trunc, llvm.bitreverse.i2 and zext
of the flags; read_mem_fence takes order 2 and write_mem_fence order 3 (the probe
opencl_operand_encodings, M3 Max, job m3-max-76799; the module text read again in job
m3-max-76855). The test modules that stand for mem_fence wrote order 3, a release fence, which
puts a fence weaker than Apple's on the side that reads: opencl_kernels's FENCE,
opencl_barriers's _Z9mem_fencej and unbound_immediates's fence. Each now writes order 4, and the
canon test asserts the fence it binds is acq_rel. None of the three stands for write_mem_fence,
so none keeps order 3.

A test correction only: no product code changes. The before and after runs of the two device
tests on one card per vendor follow this commit.
This commit is contained in:
Anees Iqbal committed 2026-11-27 05:27:14 -08:00
1 parent 39b435e4b5
commit d922da7b73
3 files changed
+13 -7

No files matched your search

@@ -134,7 +134,9 @@ fn binding_a_callers_flags_does_no_work_of_its_own() {
#[test]
fn a_fence_whose_flags_its_callers_flags_compute_binds_after_folding() {
// OpenCL C's `mem_fence(flags)` as Metal's compiler leaves it: CLK_GLOBAL_MEM_FENCE (2)
// becomes `mem_device` (1) by reversing the two flag bits, an `i2` operation.
// becomes `mem_device` (1) by reversing the two flag bits, an `i2` operation, and the fence's
// order is acq_rel (4), as Apple's OpenCL compiler writes `mem_fence` (the probe
// opencl_operand_encodings, job m3-max-76799).
let body = "define void @k(ptr addrspace(1) %x) #0 {\n\
\x20 store i32 1, ptr addrspace(1) %x, align 4\n\
\x20 tail call fastcc void @fence(i32 2)\n\
@@ -144,7 +146,7 @@ fn a_fence_whose_flags_its_callers_flags_compute_binds_after_folding() {
\x20 %t = trunc i32 %flags to i2\n\
\x20 %r = tail call i2 @llvm.bitreverse.i2(i2 %t)\n\
\x20 %m = zext i2 %r to i32\n\
\x20 tail call void @air.atomic.fence(i32 %m, i32 3, i32 1) #2\n\
\x20 tail call void @air.atomic.fence(i32 %m, i32 4, i32 1) #2\n\
\x20 ret void\n}\n\
declare void @air.atomic.fence(i32, i32, i32) #2\n\
declare i2 @llvm.bitreverse.i2(i2) #3\n\
@@ -161,7 +163,7 @@ fn a_fence_whose_flags_its_callers_flags_compute_binds_after_folding() {
let entry = entry.expect("specialized");
let fence = Sync::Fence {
flags: flags(1),
order: MemoryOrder::from_raw(3).expect("order"),
order: MemoryOrder::from_raw(4).expect("order"),
scope: ThreadScope::from_raw(1).expect("scope"),
};
assert_eq!(syncs(&entry), [fence]);
@@ -79,12 +79,14 @@ const BARRIER_THREE: &str = "define internal fastcc void @_Z7barrierj(i32 %0) #2
ret void\n}\n\
declare void @air.wg.barrier(i32, i32, i32) #4\n";
/// `mem_fence(flags)`: `air.atomic.fence` of the flags' two bits reversed, out of line.
/// `mem_fence(flags)`: `air.atomic.fence` of the flags' two bits reversed, out of line, its order
/// acq_rel (4), as Apple's OpenCL compiler writes `mem_fence` (the probe
/// opencl_operand_encodings, job m3-max-76799).
const FENCE: &str = "define internal fastcc void @_Z9mem_fencej(i32 %0) #2 {\n\
%2 = trunc i32 %0 to i2\n\
%3 = tail call i2 @llvm.bitreverse.i2(i2 %2)\n\
%4 = zext i2 %3 to i32\n\
tail call void @air.atomic.fence(i32 %4, i32 3, i32 1) #5\n\
tail call void @air.atomic.fence(i32 %4, i32 4, i32 1) #5\n\
ret void\n}\n\
declare void @air.atomic.fence(i32, i32, i32) #5\n\
declare i2 @llvm.bitreverse.i2(i2) #9\n";
@@ -111,7 +111,9 @@ attributes #3 = { convergent nounwind }\n\
!2 = !{!3}\n";
/// `x[i] = 1`, `@fence(2)` (its flags `bitreverse.i2` of the parameter's low bits, as OpenCL C's
/// `mem_fence(CLK_GLOBAL_MEM_FENCE)` becomes `mem_device`), `x[i] = 2`.
/// `mem_fence(CLK_GLOBAL_MEM_FENCE)` becomes `mem_device`, its order acq_rel (4), as Apple's
/// OpenCL compiler writes `mem_fence`: the probe opencl_operand_encodings, job m3-max-76799),
/// `x[i] = 2`.
const FENCE: &str = "define void @store2(ptr addrspace(1) nocapture %x) #3 {\n\
%id = tail call i32 @air.get_global_id.i32(i32 0)\n\
%i = zext i32 %id to i64\n\
@@ -124,7 +126,7 @@ define internal fastcc void @fence(i32 %flags) #2 {\n\
%t = trunc i32 %flags to i2\n\
%r = tail call i2 @llvm.bitreverse.i2(i2 %t)\n\
%m = zext i2 %r to i32\n\
tail call void @air.atomic.fence(i32 %m, i32 3, i32 1) #1\n\
tail call void @air.atomic.fence(i32 %m, i32 4, i32 1) #1\n\
ret void\n}\n\
declare void @air.atomic.fence(i32, i32, i32) #1\n\
declare i2 @llvm.bitreverse.i2(i2) #9\n\