A read-only device buffer may be a constant-space pointer
Apple's compiler declares a by-value kernel argument it passes as a constant pointer as an `air.buffer` with `air.read` and `air.address_space 1` (a one-byte `char`): a Core Image kernel of macOS 13.7's frameworks (AIR 2.5) declares its parameter block so, and the ABI check refused the library with RoleTypeMismatch (the buffer's declared space against its pointer's). Metal takes the shape: on the M3 Max (macOS 27.0.1, job m3-max-14624) our own kernel over `constant Params *p`, its node rewritten to `device` read (with and without the `char` description) and reassembled with metal-as, made a compute pipeline and wrote the same 32 floats as the kernel as Metal's compiler wrote it. abi::check's role_type now also accepts an `air.buffer` declared read-only in `device` space on a `constant` pointer (read_through_constant). The buffer keeps its declared space and access, so the planner binds it as a read-only device buffer; the entry's loads are through its pointer. A writable buffer, or a `constant` buffer on a `device` pointer, is still a mismatch. Tests: the ABI unit test of both the accepted and the still-refused forms; an engine test that compiles our kernel with either node on lavapipe, moltenvk-m3, the 780M's RADV and the Vulkan 1.2 floor and passes L3; and a runtime device test that runs both forms and compares them with the M3 Max's 32 floats. SEMANTICS_VERSION 2025: a refused input now translates.
This commit is contained in:
1 parent
e9d402a179
commit
521f778bf6
8 files changed
+314
-6
No files matched your search
@@ -5,8 +5,8 @@ use std::collections::BTreeSet;
|
||||
|
||||
use super::md::{abi_error, Site};
|
||||
use super::{
|
||||
AirAbi, CoreImageKernel, CoreImageParam, CoreImageRole, EntryPoint, ParamBinding, ParamRole,
|
||||
PatchInput, Predicate, Stage, StructRef, VisibleFunction,
|
||||
Access, AirAbi, CoreImageKernel, CoreImageParam, CoreImageRole, EntryPoint, ParamBinding,
|
||||
ParamRole, PatchInput, Predicate, Stage, StructRef, VisibleFunction,
|
||||
};
|
||||
use crate::bitcode::PointeeTable;
|
||||
use crate::budget::Meter;
|
||||
@@ -387,12 +387,15 @@ fn predicate(m: &Module, p: Option<&Predicate>, site: &Site) -> Result<(), Error
|
||||
|
||||
/// A parameter's role against its IR type: resource and buffer roles are pointers in the space
|
||||
/// the role implies, value roles are not pointers. A buffer, texture, or sampler table passed
|
||||
/// as an `array_ref` is a thread-space pointer to its elements.
|
||||
/// as an `array_ref` is a thread-space pointer to its elements. A read-only `device` buffer may
|
||||
/// also be a `constant`-space pointer ([`read_through_constant`]).
|
||||
fn role_type(m: &Module, p: &ParamBinding, ty: TypeId, site: &Site) -> Result<(), Error> {
|
||||
let space = m.types().ptr_space(ty);
|
||||
let ok = match &p.role {
|
||||
ParamRole::Buffer(b) | ParamRole::ArgumentBuffer(b) | ParamRole::Threadgroup(b) => {
|
||||
space == Some(b.space) || space == Some(AddrSpace::Thread)
|
||||
space == Some(b.space)
|
||||
|| space == Some(AddrSpace::Thread)
|
||||
|| read_through_constant(&p.role, space)
|
||||
}
|
||||
ParamRole::Texture(_) => {
|
||||
space == Some(AddrSpace::Device) || space == Some(AddrSpace::Thread)
|
||||
@@ -432,6 +435,18 @@ fn role_type(m: &Module, p: &ParamBinding, ty: TypeId, site: &Site) -> Result<()
|
||||
}
|
||||
}
|
||||
|
||||
/// A buffer parameter (`air.buffer`) declared `air.read` in `device` space whose IR type is a
|
||||
/// `constant`-space pointer. Apple's compiler writes such a node for a by-value argument it
|
||||
/// passes as a constant pointer (a Core Image kernel in macOS 13.7's frameworks, AIR 2.5, declares
|
||||
/// one as a one-byte `char`), and Metal takes it: on an Apple M3 Max (macOS 27.0.1), a kernel whose
|
||||
/// `constant` buffer's node was rewritten so, with and without its type described as that `char`,
|
||||
/// made a compute pipeline and read the buffer as the unrewritten kernel did. The buffer keeps its
|
||||
/// declared space; the entry's loads are through its pointer. A writable one stays a mismatch.
|
||||
fn read_through_constant(role: &ParamRole, space: Option<AddrSpace>) -> bool {
|
||||
matches!(role, ParamRole::Buffer(b) if b.space == AddrSpace::Device && b.access == Access::Read)
|
||||
&& space == Some(AddrSpace::Constant)
|
||||
}
|
||||
|
||||
/// `air.arg_type_size`, `air.arg_type_align_size`, `air.buffer_size`, and `air.struct_type_info` of
|
||||
/// a pointer parameter against the DataLayout of its pointee.
|
||||
pub(super) fn pointee_check(
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
//!
|
||||
//! Decoding also cross-checks the metadata against the IR: every entry parameter has exactly one
|
||||
//! binding, each role agrees with its parameter's type and address space (a buffer, texture, or
|
||||
//! sampler table passed as an `array_ref` is a thread-space pointer), and the outputs agree with
|
||||
//! sampler table passed as an `array_ref` is a thread-space pointer, and a read-only `device`
|
||||
//! buffer may be a constant-space pointer), and the outputs agree with
|
||||
//! the return type; likewise each `!air.visible` function binds every parameter once and has an
|
||||
//! output exactly when it returns a value, and each `!air.ci` Core Image kernel also binds every
|
||||
//! parameter once, with a role that agrees with the parameter's type, and has a scalar or vector
|
||||
|
||||
@@ -541,6 +541,57 @@ fn role_must_match_parameter_type() {
|
||||
expect_abi(k.decode(), AbiError::RoleTypeMismatch);
|
||||
}
|
||||
|
||||
/// A `device` buffer declared `air.read` on a `constant`-space pointer, as Apple's compiler
|
||||
/// declares a by-value argument (a one-byte `char`), is the buffer it declares; written or read and
|
||||
/// written, it is still a mismatch, as is a `constant` buffer on a `device` pointer.
|
||||
#[test]
|
||||
fn a_read_only_device_buffer_may_be_a_constant_pointer() {
|
||||
// Parameter 2, the standard kernel's constant pointer, as `[[buffer(1)]]`.
|
||||
let declared = |access: &'static str| -> Vec<O<'static>> {
|
||||
vec![
|
||||
I(2),
|
||||
S("air.buffer"),
|
||||
S("air.location_index"),
|
||||
I(1),
|
||||
I(1),
|
||||
S(access),
|
||||
S("air.address_space"),
|
||||
I(1),
|
||||
S("air.arg_type_size"),
|
||||
I(1),
|
||||
S("air.arg_type_align_size"),
|
||||
I(1),
|
||||
S("air.arg_type_name"),
|
||||
S("char"),
|
||||
S("air.arg_name"),
|
||||
S("transform"),
|
||||
]
|
||||
};
|
||||
let with = |buffer: Vec<O<'static>>, third: Vec<O<'static>>| {
|
||||
Kernel::with(&[buffer, texture_arg(1), third, gid_arg(3)]).decode()
|
||||
};
|
||||
let abi = with(buffer_arg(0), declared("air.read"))
|
||||
.expect("a read-only device buffer on a constant pointer decodes");
|
||||
let p = &abi.entries[0].params[2];
|
||||
let ParamRole::Buffer(b) = &p.role else {
|
||||
panic!("{:?}", p.role);
|
||||
};
|
||||
assert_eq!((b.space, b.access), (AddrSpace::Device, Access::Read));
|
||||
for access in ["air.write", "air.read_write"] {
|
||||
expect_abi(
|
||||
with(buffer_arg(0), declared(access)),
|
||||
AbiError::RoleTypeMismatch,
|
||||
);
|
||||
}
|
||||
let mut constant_on_device = buffer_arg(0);
|
||||
constant_on_device[5] = S("air.read");
|
||||
constant_on_device[7] = I(2);
|
||||
expect_abi(
|
||||
with(constant_on_device, declared("air.read")),
|
||||
AbiError::RoleTypeMismatch,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_parameter_needs_exactly_one_binding() {
|
||||
let k = Kernel::with(&[buffer_arg(0), texture_arg(1), sampler_arg(2)]);
|
||||
|
||||
@@ -393,6 +393,8 @@ mod queue_share_device;
|
||||
mod queue_sharing;
|
||||
mod range;
|
||||
mod rate_map;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod read_through_constant;
|
||||
mod reader_sizes;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod rebind_behind_a_wait;
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
//! A kernel buffer declared read-only in `device` space whose parameter is a `constant`-space
|
||||
//! pointer, as Apple's compiler declares a by-value argument (`air.address_space 1`, a one-byte
|
||||
//! `char`), runs as the same kernel declaring the `constant` buffer it reads, and computes what the
|
||||
//! M3 Max (macOS 27.0.1) computes for it.
|
||||
//!
|
||||
//! The kernel is ours: `out[i] = p->rect + p->m[i & 3] * p->a + float4(p->n[i % 3], p->e)` over a
|
||||
//! 160-byte `constant Params *p` (`float4 rect; float4x4 m; float3x3 n; float a, b, c, d, e;`)
|
||||
//! set as bytes at index 0, `out` at index 1, over 8 threads, the 40 floats of `p` `k / 2 + 1`.
|
||||
//! Metal's compiler wrote the kernel's `constant` node; the M3 Max made a pipeline of the kernel
|
||||
//! with that node rewritten to the `device` `char` one and answered the same 32 floats as with the
|
||||
//! node it wrote (`metal-as` of its typed text, both nodes).
|
||||
//!
|
||||
//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`,
|
||||
//! `m2v_cpu_icd`), on the kind of device the cfg selects. The modules are LLVM text written here
|
||||
//! (`llvm-as`).
|
||||
|
||||
use super::blit_holds::{device, Scratch, WAIT};
|
||||
use crate::{ComputePipelineState, Device, PipelineOptions, ResourceOptions, Size};
|
||||
use metal2vulkan::pipeline::{ComputePipelineDescriptor, StageSource};
|
||||
|
||||
/// The node of the `Params` buffer as Metal's compiler writes it for `constant Params *p`.
|
||||
const AS_CONSTANT: &str = "!\"air.read\", !\"air.address_space\", i32 2, !\"air.arg_type_size\", i32 160, !\"air.arg_type_align_size\", i32 16, !\"air.arg_type_name\", !\"Params\"";
|
||||
/// The node as Apple's compiler writes it for a by-value argument: `device`, one `char`.
|
||||
const AS_DEVICE_CHAR: &str = "!\"air.read\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 1, !\"air.arg_type_align_size\", i32 1, !\"air.arg_type_name\", !\"char\"";
|
||||
|
||||
/// What the M3 Max answers, thread by thread.
|
||||
const METAL: [f32; 32] = [
|
||||
63.0, 72.5, 82.0, 98.0, 99.0, 108.5, 118.0, 132.0, 135.0, 144.5, 154.0, 166.0, 165.0, 174.5,
|
||||
184.0, 200.0, 65.0, 74.5, 84.0, 98.0, 101.0, 110.5, 120.0, 132.0, 131.0, 140.5, 150.0, 166.0,
|
||||
167.0, 176.5, 186.0, 200.0,
|
||||
];
|
||||
|
||||
/// The kernel `k`, its `Params` buffer's node holding `node`.
|
||||
fn kernel(node: &str) -> String {
|
||||
format!(
|
||||
"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 @k(ptr addrspace(2) noundef readonly %0, ptr addrspace(1) noundef %1, i32 noundef %2) {{\n\
|
||||
entry:\n\
|
||||
%rect = load <4 x float>, ptr addrspace(2) %0, align 16\n\
|
||||
%j = and i32 %2, 3\n\
|
||||
%j64 = zext i32 %j to i64\n\
|
||||
%jo = shl i64 %j64, 4\n\
|
||||
%mo = add i64 %jo, 16\n\
|
||||
%mp = getelementptr inbounds i8, ptr addrspace(2) %0, i64 %mo\n\
|
||||
%row = load <4 x float>, ptr addrspace(2) %mp, align 16\n\
|
||||
%ap = getelementptr inbounds i8, ptr addrspace(2) %0, i64 128\n\
|
||||
%a = load float, ptr addrspace(2) %ap, align 16\n\
|
||||
%r = urem i32 %2, 3\n\
|
||||
%r64 = zext i32 %r to i64\n\
|
||||
%ro = shl i64 %r64, 4\n\
|
||||
%no = add i64 %ro, 80\n\
|
||||
%np = getelementptr inbounds i8, ptr addrspace(2) %0, i64 %no\n\
|
||||
%n = load <3 x float>, ptr addrspace(2) %np, align 16\n\
|
||||
%ep = getelementptr inbounds i8, ptr addrspace(2) %0, i64 144\n\
|
||||
%e = load float, ptr addrspace(2) %ep, align 16\n\
|
||||
%a0 = insertelement <4 x float> poison, float %a, i64 0\n\
|
||||
%as = shufflevector <4 x float> %a0, <4 x float> poison, <4 x i32> zeroinitializer\n\
|
||||
%scaled = fmul <4 x float> %row, %as\n\
|
||||
%sum = fadd <4 x float> %rect, %scaled\n\
|
||||
%n4 = shufflevector <3 x float> %n, <3 x float> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 poison>\n\
|
||||
%ne = insertelement <4 x float> %n4, float %e, i64 3\n\
|
||||
%v = fadd <4 x float> %sum, %ne\n\
|
||||
%i64 = zext i32 %2 to i64\n\
|
||||
%op = getelementptr inbounds <4 x float>, ptr addrspace(1) %1, i64 %i64\n\
|
||||
store <4 x float> %v, ptr addrspace(1) %op, align 16\n\
|
||||
ret void\n\
|
||||
}}\n\n\
|
||||
!llvm.module.flags = !{{!0}}\n!air.kernel = !{{!1}}\n!air.version = !{{!7}}\n!air.language_version = !{{!8}}\n\
|
||||
!0 = !{{i32 7, !\"air.max_device_buffers\", i32 31}}\n\
|
||||
!1 = !{{ptr @k, !2, !3}}\n!2 = !{{}}\n!3 = !{{!4, !5, !6}}\n\
|
||||
!4 = !{{i32 0, !\"air.buffer\", !\"air.location_index\", i32 0, i32 1, {node}, !\"air.arg_name\", !\"p\"}}\n\
|
||||
!5 = !{{i32 1, !\"air.buffer\", !\"air.location_index\", i32 1, i32 1, !\"air.read_write\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 16, !\"air.arg_type_align_size\", i32 16, !\"air.arg_type_name\", !\"float4\", !\"air.arg_name\", !\"out\"}}\n\
|
||||
!6 = !{{i32 2, !\"air.thread_position_in_grid\", !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"i\"}}\n\
|
||||
!7 = !{{i32 2, i32 8, i32 0}}\n!8 = !{{!\"Metal\", i32 4, i32 0, i32 0}}\n"
|
||||
)
|
||||
}
|
||||
|
||||
/// The state of `k` with its `Params` node `node`.
|
||||
fn state(device: &Device, node: &str) -> ComputePipelineState {
|
||||
let air = metal2vulkan_devtools::llvm_assemble_text(&kernel(node)).unwrap();
|
||||
let library = metal2vulkan::library::Library::from_air(&air).unwrap();
|
||||
let function = device
|
||||
.new_library_with_compile_library(library)
|
||||
.new_function_with_name("k")
|
||||
.unwrap();
|
||||
let descriptor =
|
||||
ComputePipelineDescriptor::new(StageSource::Function(function.function_descriptor()));
|
||||
device
|
||||
.new_compute_pipeline_state(&descriptor, &PipelineOptions::default())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// The 32 floats `state` writes over 8 threads.
|
||||
fn run(device: &Device, state: &ComputePipelineState) -> Vec<f32> {
|
||||
let params: Vec<u8> = (0..40)
|
||||
.flat_map(|k| (k as f32 / 2.0 + 1.0).to_le_bytes())
|
||||
.collect();
|
||||
let out = device
|
||||
.new_buffer_with_bytes(&[0; 128], ResourceOptions::shared())
|
||||
.unwrap();
|
||||
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_bytes(¶ms, 0).unwrap();
|
||||
encoder.set_buffer(Some(&out), 0, 1).unwrap();
|
||||
encoder
|
||||
.dispatch_threads(Size::new(8, 1, 1), Size::new(8, 1, 1))
|
||||
.unwrap();
|
||||
encoder.end_encoding().unwrap();
|
||||
command_buffer.commit().unwrap();
|
||||
command_buffer.wait_until_completed_for(WAIT).unwrap();
|
||||
assert_eq!(command_buffer.error(), None);
|
||||
let contents = out.contents().unwrap();
|
||||
// SAFETY: the buffer's bytes, which no command uses now.
|
||||
let bytes = unsafe { std::slice::from_raw_parts(contents.as_ptr(), out.length() as usize) };
|
||||
bytes
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|w| f32::from_le_bytes(*w))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_read_only_device_buffer_read_through_a_constant_pointer_computes_what_metal_computes() {
|
||||
let scratch = Scratch::new("read-through-constant");
|
||||
let device = device(&scratch.0);
|
||||
for (form, node) in [("constant", AS_CONSTANT), ("device char", AS_DEVICE_CHAR)] {
|
||||
assert_eq!(run(&device, &state(&device, node)), METAL, "{form}");
|
||||
}
|
||||
}
|
||||
Vendored
+1
-1
@@ -60,7 +60,7 @@ mod tests;
|
||||
/// unchanged version, and its bless records the version. Independent of the crate's version and of
|
||||
/// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no
|
||||
/// history.
|
||||
pub const SEMANTICS_VERSION: u32 = 2024;
|
||||
pub const SEMANTICS_VERSION: u32 = 2025;
|
||||
|
||||
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
|
||||
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
//! A kernel buffer declared read-only in `device` space whose parameter is a `constant`-space
|
||||
//! pointer, as Apple's compiler declares a by-value argument (`air.address_space 1`, a one-byte
|
||||
//! `char`), compiles on every profile as the same kernel declaring the `constant` buffer it reads.
|
||||
//!
|
||||
//! The kernel is ours, `out[i] = p->rect + p->m[i & 3] * p->a + float4(p->n[i % 3], p->e)` over a
|
||||
//! 160-byte `constant Params *p` (`float4 rect; float4x4 m; float3x3 n; float a, b, c, d, e;`),
|
||||
//! written here as LLVM text with the buffer's node in either form. MSL has no spelling for the
|
||||
//! rewritten node; Metal takes it (`abi::check`'s `read_through_constant`), and the runtime's
|
||||
//! `read_through_constant` device test runs both forms against the M3 Max's answers.
|
||||
//!
|
||||
//! `llvm-as` assembles the modules; without it the test skips visibly.
|
||||
|
||||
use crate::air::abi::{Access, ParamRole};
|
||||
use crate::air::ir::AddrSpace;
|
||||
use crate::budget::{Budget, Meter};
|
||||
use crate::device::DevicePolicy;
|
||||
use crate::pipeline::testing::{
|
||||
compile, link_and_validate, llvm_assemble_text, profile, AssembleError,
|
||||
};
|
||||
|
||||
/// The node of the `Params` buffer as Metal's compiler writes it for `constant Params *p`.
|
||||
const AS_CONSTANT: &str = "!\"air.read\", !\"air.address_space\", i32 2, !\"air.arg_type_size\", i32 160, !\"air.arg_type_align_size\", i32 16, !\"air.arg_type_name\", !\"Params\"";
|
||||
/// The node as Apple's compiler writes it for a by-value argument: `device`, one `char`.
|
||||
const AS_DEVICE_CHAR: &str = "!\"air.read\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 1, !\"air.arg_type_align_size\", i32 1, !\"air.arg_type_name\", !\"char\"";
|
||||
|
||||
/// The kernel `k`, its `Params` buffer's node holding `node`.
|
||||
fn kernel(node: &str) -> String {
|
||||
format!(
|
||||
"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 @k(ptr addrspace(2) noundef readonly %0, ptr addrspace(1) noundef %1, i32 noundef %2) {{\n\
|
||||
entry:\n\
|
||||
%rect = load <4 x float>, ptr addrspace(2) %0, align 16\n\
|
||||
%j = and i32 %2, 3\n\
|
||||
%j64 = zext i32 %j to i64\n\
|
||||
%jo = shl i64 %j64, 4\n\
|
||||
%mo = add i64 %jo, 16\n\
|
||||
%mp = getelementptr inbounds i8, ptr addrspace(2) %0, i64 %mo\n\
|
||||
%row = load <4 x float>, ptr addrspace(2) %mp, align 16\n\
|
||||
%ap = getelementptr inbounds i8, ptr addrspace(2) %0, i64 128\n\
|
||||
%a = load float, ptr addrspace(2) %ap, align 16\n\
|
||||
%r = urem i32 %2, 3\n\
|
||||
%r64 = zext i32 %r to i64\n\
|
||||
%ro = shl i64 %r64, 4\n\
|
||||
%no = add i64 %ro, 80\n\
|
||||
%np = getelementptr inbounds i8, ptr addrspace(2) %0, i64 %no\n\
|
||||
%n = load <3 x float>, ptr addrspace(2) %np, align 16\n\
|
||||
%ep = getelementptr inbounds i8, ptr addrspace(2) %0, i64 144\n\
|
||||
%e = load float, ptr addrspace(2) %ep, align 16\n\
|
||||
%a0 = insertelement <4 x float> poison, float %a, i64 0\n\
|
||||
%as = shufflevector <4 x float> %a0, <4 x float> poison, <4 x i32> zeroinitializer\n\
|
||||
%scaled = fmul <4 x float> %row, %as\n\
|
||||
%sum = fadd <4 x float> %rect, %scaled\n\
|
||||
%n4 = shufflevector <3 x float> %n, <3 x float> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 poison>\n\
|
||||
%ne = insertelement <4 x float> %n4, float %e, i64 3\n\
|
||||
%v = fadd <4 x float> %sum, %ne\n\
|
||||
%i64 = zext i32 %2 to i64\n\
|
||||
%op = getelementptr inbounds <4 x float>, ptr addrspace(1) %1, i64 %i64\n\
|
||||
store <4 x float> %v, ptr addrspace(1) %op, align 16\n\
|
||||
ret void\n\
|
||||
}}\n\n\
|
||||
!llvm.module.flags = !{{!0}}\n!air.kernel = !{{!1}}\n!air.version = !{{!7}}\n!air.language_version = !{{!8}}\n\
|
||||
!0 = !{{i32 7, !\"air.max_device_buffers\", i32 31}}\n\
|
||||
!1 = !{{ptr @k, !2, !3}}\n!2 = !{{}}\n!3 = !{{!4, !5, !6}}\n\
|
||||
!4 = !{{i32 0, !\"air.buffer\", !\"air.location_index\", i32 0, i32 1, {node}, !\"air.arg_name\", !\"p\"}}\n\
|
||||
!5 = !{{i32 1, !\"air.buffer\", !\"air.location_index\", i32 1, i32 1, !\"air.read_write\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 16, !\"air.arg_type_align_size\", i32 16, !\"air.arg_type_name\", !\"float4\", !\"air.arg_name\", !\"out\"}}\n\
|
||||
!6 = !{{i32 2, !\"air.thread_position_in_grid\", !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"i\"}}\n\
|
||||
!7 = !{{i32 2, i32 8, i32 0}}\n!8 = !{{!\"Metal\", i32 4, i32 0, i32 0}}\n"
|
||||
)
|
||||
}
|
||||
|
||||
/// Both forms of the kernel compile and pass L3 on every profile here, the rewritten one keeping
|
||||
/// its buffer's declared `device` space and read access.
|
||||
#[test]
|
||||
fn a_read_only_device_buffer_read_through_a_constant_pointer_compiles() {
|
||||
let assemble = |node| match llvm_assemble_text(&kernel(node)) {
|
||||
Ok(b) => Some(b),
|
||||
Err(AssembleError::Missing(why)) => {
|
||||
eprintln!("SKIPPED: {why}");
|
||||
None
|
||||
}
|
||||
Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the module: {why}"),
|
||||
};
|
||||
let (Some(constant), Some(device)) = (assemble(AS_CONSTANT), assemble(AS_DEVICE_CHAR)) else {
|
||||
return;
|
||||
};
|
||||
let mut compiled = 0;
|
||||
for stem in ["lavapipe", "moltenvk-m3", "amd-780m-radv", "vk12-floor"] {
|
||||
let p = profile(stem, DevicePolicy::default()).expect("profile");
|
||||
for (form, bc) in [("constant", &constant), ("device char", &device)] {
|
||||
let what = format!("{form} on {stem}");
|
||||
let a = compile(bc, "k", &p).unwrap_or_else(|e| panic!("{what}: {e}"));
|
||||
link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{what}: L3: {e}"));
|
||||
compiled += 1;
|
||||
}
|
||||
}
|
||||
let abi = crate::air::bitcode::decode(&device, &mut Meter::new(&Budget::default()))
|
||||
.expect("the module decodes")
|
||||
.abi;
|
||||
let ParamRole::Buffer(b) = &abi.entries[0].params[0].role else {
|
||||
panic!("{:?}", abi.entries[0].params[0].role);
|
||||
};
|
||||
assert_eq!((b.space, b.access), (AddrSpace::Device, Access::Read));
|
||||
eprintln!("compiled {compiled} modules of the two forms");
|
||||
}
|
||||
@@ -121,6 +121,7 @@ mod engine_quad_ops;
|
||||
mod engine_qualified_signature;
|
||||
mod engine_rate_map;
|
||||
mod engine_ray_tracing;
|
||||
mod engine_read_through_constant;
|
||||
mod engine_recursion;
|
||||
mod engine_refused_families;
|
||||
mod engine_render_target_slice;
|
||||
|
||||
Reference in new issue
Block a user