tests: a translated kernel's load and store through a device pointer

On the UHD 630 (Windows, 101.2145) the indirect dispatch on the flat path, from
Shared, Private and unimported memory alike, and the GPU replay of copied
commands run nothing, while the host step with no device addresses
(shaderInt64 withheld) runs. This separates the translated kernel's own reads
and writes by device address (direct dispatches) from the indirect machinery.

Verified: m3-max-32562 (UHD 630) ran the earlier five: 4 failed, the host step passed.
This commit is contained in:
Anees Iqbal committed 2026-11-15 08:31:57 -08:00
1 parent 8e8af60ade
commit d2a9880b4e
1 file changed
+130 -2
@@ -16,6 +16,11 @@
//! into another buffer that the same command buffer executes next, writes `i` at each of its 64
//! threads' words (`scale_and_offset` of zero input and scale).
//!
//! - **Device pointers.** A direct dispatch of a kernel that loads a buffer's address from
//! another buffer reads 256 words through it, and one that stores through it writes them
//! (`useResource` makes the pointee resident): the translated kernel's own reads and writes by
//! device address, with no indirect command.
//!
//! Each test prints what the device announced, how many host steps it recorded and where its
//! buffers' memory came from (`HOST-STEP-READS` lines).
//!
@@ -31,13 +36,49 @@ use super::device_recovery_live_table::{
use super::inert_dispatch::state;
use super::opencl_mode::{words, words_buffer};
use crate::{
Buffer, CommandBufferStatus, Degradation, Device, MessageSink, ResourceOptions, ResourceRef,
Size, WorkCounts,
Buffer, CommandBufferStatus, ComputePipelineState, Degradation, Device, MessageSink,
PipelineOptions, ResourceOptions, ResourceRef, Size, WorkCounts,
};
use metal2vulkan::device::{VkExtension, VkFeature};
use metal2vulkan::metal::ResourceUsage;
use metal2vulkan::pipeline::{ComputePipelineDescriptor, StageSource};
use std::sync::{Arc, Mutex};
/// Two kernels of buffers `a` (index 0, a device pointer) and `b` (index 1) and the thread's grid
/// position: `deref_load` stores `p[gid] + 1` at `b[gid]`, `p` the pointer `a` holds;
/// `deref_store` stores `gid + 1` at `p[gid]`.
const POINTER_KERNELS: &str = "target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32\"\n\
target triple = \"air64_v28-apple-macosx26.0.0\"\n\n\
define void @deref_load(ptr addrspace(1) noundef %a, ptr addrspace(1) noundef %b, i32 noundef %gid) {\n\
entry:\n\
%p = load ptr addrspace(1), ptr addrspace(1) %a, align 8\n\
%gw = zext i32 %gid to i64\n\
%q = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %gw\n\
%x = load i32, ptr addrspace(1) %q, align 4\n\
%v = add i32 %x, 1\n\
%r = getelementptr inbounds i32, ptr addrspace(1) %b, i64 %gw\n\
store i32 %v, ptr addrspace(1) %r, align 4\n\
ret void\n\
}\n\n\
define void @deref_store(ptr addrspace(1) noundef %a, ptr addrspace(1) noundef %b, i32 noundef %gid) {\n\
entry:\n\
%p = load ptr addrspace(1), ptr addrspace(1) %a, align 8\n\
%gw = zext i32 %gid to i64\n\
%q = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %gw\n\
%v = add i32 %gid, 1\n\
store i32 %v, ptr addrspace(1) %q, align 4\n\
ret void\n\
}\n\n\
!llvm.module.flags = !{!0, !1}\n!air.kernel = !{!20, !21}\n!air.version = !{!8}\n!air.language_version = !{!9}\n\
!0 = !{i32 7, !\"air.max_device_buffers\", i32 31}\n\
!1 = !{i32 7, !\"air.max_threadgroup_buffers\", i32 31}\n\
!3 = !{}\n!4 = !{!5, !6, !7}\n\
!5 = !{i32 0, !\"air.buffer\", !\"air.location_index\", i32 0, i32 1, !\"air.read\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 8, !\"air.arg_type_align_size\", i32 8, !\"air.arg_type_name\", !\"ulong\", !\"air.arg_name\", !\"a\"}\n\
!6 = !{i32 1, !\"air.buffer\", !\"air.location_index\", i32 1, i32 1, !\"air.read_write\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 4, !\"air.arg_type_align_size\", i32 4, !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"b\"}\n\
!7 = !{i32 2, !\"air.thread_position_in_grid\", !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"gid\"}\n\
!8 = !{i32 2, i32 8, i32 0}\n!9 = !{!\"Metal\", i32 4, i32 0, i32 0}\n\
!20 = !{ptr @deref_load, !3, !4}\n!21 = !{ptr @deref_store, !3, !4}\n";
/// The words the dispatch tests read: past the 256 the dispatch writes.
const N: usize = 1024;
@@ -235,3 +276,90 @@ fn an_execution_of_copied_commands_runs_them() {
let floats: Vec<f32> = words(&output).into_iter().map(f32::from_bits).collect();
assert_eq!(floats, dispatched());
}
/// The state of kernel `name` of [`POINTER_KERNELS`].
fn pointer_state(device: &Device, name: &str) -> ComputePipelineState {
let air = metal2vulkan_devtools::llvm_assemble_text(POINTER_KERNELS).unwrap();
let library = metal2vulkan::library::Library::from_air(&air).unwrap();
let function = device
.new_library_with_compile_library(library)
.new_function_with_name(name)
.unwrap();
let descriptor =
ComputePipelineDescriptor::new(StageSource::Function(function.function_descriptor()));
device
.new_compute_pipeline_state(&descriptor, &PipelineOptions::default())
.unwrap()
}
/// A direct dispatch of `name` over 256 threads, `a` holding `pointee`'s address and `b` bound,
/// `pointee` resident with `usage`.
fn through_pointer(
device: &Device,
name: &str,
pointee: &Buffer,
b: &Buffer,
usage: ResourceUsage,
) {
let state = pointer_state(device, name);
let address = pointee.gpu_address().0;
let a = words_buffer(device, &[address as u32, (address >> 32) as u32]);
let queue = device.new_command_queue().unwrap();
let command_buffer = queue.command_buffer().unwrap();
let mut encoder = command_buffer.compute_command_encoder().unwrap();
encoder.set_compute_pipeline_state(&state).unwrap();
encoder.set_buffer(Some(&a), 0, 0).unwrap();
encoder.set_buffer(Some(b), 0, 1).unwrap();
encoder
.use_resource(ResourceRef::Buffer(pointee), usage)
.unwrap();
encoder
.dispatch_threads(Size::new(256, 1, 1), Size::new(64, 1, 1))
.unwrap();
encoder.end_encoding().unwrap();
command_buffer.commit().unwrap();
command_buffer.wait_until_completed_for(WAIT).unwrap();
assert_eq!(
(command_buffer.status(), command_buffer.error()),
(CommandBufferStatus::Completed, None)
);
}
/// A translated kernel's own load and store through a device pointer, in direct dispatches.
#[test]
fn a_kernel_reads_and_writes_through_a_device_pointer() {
let scratch = Scratch::new("gpu-reads-pointer");
let (device, heard) = device(&scratch, &[], &[]);
let before = device.work_counts();
let source: Vec<u32> = (0..N as u32).map(|i| 5 * i).collect();
let pointee = words_buffer(&device, &source);
let loaded = words_buffer(&device, &[0; N]);
through_pointer(
&device,
"deref_load",
&pointee,
&loaded,
ResourceUsage::READ,
);
let stored = words_buffer(&device, &[0; N]);
let unused = words_buffer(&device, &[0]);
through_pointer(
&device,
"deref_store",
&stored,
&unused,
ResourceUsage::WRITE,
);
report("device pointer", &device, &heard, &before);
let (loaded, stored) = (words(&loaded), words(&stored));
eprintln!(
"HOST-STEP-READS device pointer: loaded {:?}, stored {:?}",
&loaded[..4],
&stored[..4]
);
let expected_load: Vec<u32> = (0..N as u32)
.map(|i| if i < 256 { 5 * i + 1 } else { 0 })
.collect();
assert_eq!(loaded, expected_load, "the load through the pointer");
assert_eq!(stored, filled(), "the store through the pointer");
}