🐛 Make a guest's stage functions with their constant values
Geekbench 7's Particle Physics workload failed: its kernel "particle" was made with constant values that set none of the constants it declares, so the guest sent it with no constants, the host made it by name, and the Vulkan runtime refused its pipeline (UnspecializedFunction), where Metal makes it with those constants undefined. A function made by descriptor without values met the same refusal. The guest's bundle already refuses a pipeline of a function made by name that declares constants, in Metal's words, so every stage function it names was made with constant values or by a descriptor, or declares none. The host now makes each stage function (compute, vertex, fragment, object, mesh) with its constant values, none among them too. Metal makes both kinds with values that set nothing, at the by-name cost for a function that declares none (measured on macOS 26.7.1, AMD Radeon Pro 5300M and Intel UHD 630). For the guest: a pipeline of such a function is made, and reads its unset constants as undefined, as on Metal. The decoder tests run a kernel of an optional constant (optional_constant.metal) sent with none and with one, on Metal and on the Vulkan platform.
This commit is contained in:
1 parent
61d08db578
commit
e6d1572dd5
6 files changed
+220
-10
No files matched your search
@@ -33,7 +33,9 @@
|
||||
//! `$OUT_DIR/tile_edges.metallib`, the tests' Core Image kernel, `src/gpu/ci_kernel.metal`,
|
||||
//! compiled as Core Image compiles one (`-fcikernel`, then `metallib -cikernel`) into
|
||||
//! `$OUT_DIR/ci_kernel.metallib`, the tests' mesh render pipelines' functions,
|
||||
//! `src/gpu/mesh.metal`, into `$OUT_DIR/mesh.metallib`, and those that import a symbol from a
|
||||
//! `src/gpu/mesh.metal`, into `$OUT_DIR/mesh.metallib`, the tests' kernel of a function constant
|
||||
//! it reads only where defined, `src/gpu/optional_constant.metal`, into
|
||||
//! `$OUT_DIR/optional_constant.metallib`, and those that import a symbol from a
|
||||
//! dynamic library, `src/gpu/refused_import.metal`, linked into three metallibs ([`imports`]), and
|
||||
//! the shell tests' stitchable functions, `guest/bundle/tests/stitchable.metal`, into
|
||||
//! `$OUT_DIR/stitchable.metallib`, with the library Metal's stitcher makes of them into
|
||||
@@ -93,7 +95,7 @@ const STITCH_TOOL: [&str; 6] = [
|
||||
];
|
||||
|
||||
/// The tests' metallibs of one source each, `src/gpu/<name>.metal`.
|
||||
const TEST_LIBRARIES: [&str; 23] = [
|
||||
const TEST_LIBRARIES: [&str; 24] = [
|
||||
"predication",
|
||||
"no_draw",
|
||||
"icb_encode",
|
||||
@@ -117,6 +119,7 @@ const TEST_LIBRARIES: [&str; 23] = [
|
||||
"linked_caller",
|
||||
"linked_callee",
|
||||
"mesh",
|
||||
"optional_constant",
|
||||
];
|
||||
|
||||
/// The files [`imports`] makes.
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
"linked_caller.metallib" = "5a9f899c17f97bd6f2f043ae011f76ebe44a98f6aa2eacdf059543a629b0b1ac"
|
||||
"linked_callee.metallib" = "12e554dcb8248e43489597dd8e013b07dcfc61719c2ff567c44088676df109a3"
|
||||
"mesh.metallib" = "a4f408406cf4201e2eb244bb0d2e90ce6bf543a454866ec31aee2ad888e059d6"
|
||||
"optional_constant.metallib" = "7d4cb5e3a6fd887205add821821245423e460038a0c870994fab9bec715c2e39"
|
||||
"rounding_safe.metallib" = "9b8720555eefdfa339a8033f57c8dde9bca4f7d5ca2167d02b3c21067a6d350d"
|
||||
"scaler.metallib" = "4250e79e58ba2a1a74cf007d7e0904fbae669fc11543b8d5f5bcf87ccd277e9e"
|
||||
"ci_kernel.metallib" = "d50192b91cff2f1163026c3dfcaf3e2d0a06216da5261480815baf39e60a819e"
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,13 @@
|
||||
// The tests' kernel of a function constant it reads only where the constant is defined
|
||||
// (`reims-vgpu`'s decoder tests): made with constant values that set none, as a guest makes a
|
||||
// function whose values set none of its constants, it writes 7; made with the constant set, the
|
||||
// constant's value.
|
||||
#include <metal_stdlib>
|
||||
using namespace metal;
|
||||
|
||||
constant int optional_value [[function_constant(0)]];
|
||||
constant bool has_optional_value = is_function_constant_defined(optional_value);
|
||||
|
||||
kernel void optional_constant(device int *out [[buffer(0)]]) {
|
||||
out[0] = has_optional_value ? optional_value : 7;
|
||||
}
|
||||
@@ -4229,7 +4229,8 @@ pub(crate) fn compile_pipeline<P: Platform>(
|
||||
let Some(vertex_library) = vertex_library.resolve() else {
|
||||
return Ok(refused(no_library("vertex")));
|
||||
};
|
||||
let Some(vertex) = specialised::<P>(&vertex_library, &vertex_name, &vertex_constants)?
|
||||
let Some(vertex) =
|
||||
stage_function::<P>(&vertex_library, &vertex_name, &vertex_constants)?
|
||||
else {
|
||||
return Ok(refused(no_function(&vertex_name)));
|
||||
};
|
||||
@@ -4239,7 +4240,7 @@ pub(crate) fn compile_pipeline<P: Platform>(
|
||||
let Some(library) = library.resolve() else {
|
||||
return Ok(refused(no_library("fragment")));
|
||||
};
|
||||
let Some(function) = specialised::<P>(&library, &name, &constants)? else {
|
||||
let Some(function) = stage_function::<P>(&library, &name, &constants)? else {
|
||||
return Ok(refused(no_function(&name)));
|
||||
};
|
||||
Some(function)
|
||||
@@ -4312,7 +4313,7 @@ pub(crate) fn compile_pipeline<P: Platform>(
|
||||
let Some(library) = library.resolve() else {
|
||||
return Ok(Err(no_library(stage)));
|
||||
};
|
||||
Ok(specialised::<P>(&library, &name, &constants)?
|
||||
Ok(stage_function::<P>(&library, &name, &constants)?
|
||||
.ok_or_else(|| no_function(&name)))
|
||||
};
|
||||
let object = match object.map(|f| function("object", f)).transpose()? {
|
||||
@@ -4401,7 +4402,7 @@ pub(crate) fn compile_pipeline<P: Platform>(
|
||||
let Some(library) = library.resolve() else {
|
||||
return Ok(refused(no_library("kernel")));
|
||||
};
|
||||
let Some(function) = specialised::<P>(&library, &name, &constants)? else {
|
||||
let Some(function) = stage_function::<P>(&library, &name, &constants)? else {
|
||||
return Ok(refused(no_function(&name)));
|
||||
};
|
||||
let linked = match linked_functions::<P>(linked)? {
|
||||
@@ -4502,6 +4503,24 @@ fn constants_tail(message: &[u8], offset: u32, count: u32) -> Option<Vec<Functio
|
||||
)
|
||||
}
|
||||
|
||||
/// A pipeline stage's function `name` of `library`, made with its constant values `constants`,
|
||||
/// none among them too, as the guest made it: the guest's bundle refuses a pipeline of a function
|
||||
/// made by name that declares constants, in Metal's words (`RgfxFunction`'s
|
||||
/// `rgfxUnspecialisedError`), so each stage function it names was made with constant values or by
|
||||
/// a descriptor, or declares none. Made with none, a function that declares constants reads them
|
||||
/// as undefined, and one that declares none is the function its name makes, at the same cost:
|
||||
/// Metal's `newFunctionWithName:constantValues:error:` makes both with values that set nothing,
|
||||
/// and a pipeline of each (measured: macOS 26.7.1, AMD Radeon Pro 5300M and Intel UHD Graphics
|
||||
/// 630). Made by name instead, a function that declares constants would have its pipeline refused
|
||||
/// on the Vulkan platform, as Metal ends its process there.
|
||||
fn stage_function<P: Platform>(
|
||||
library: &P::Library,
|
||||
name: &str,
|
||||
constants: &[FunctionConstant],
|
||||
) -> RaiseResult<Option<P::Function>> {
|
||||
library.new_function_with_constants(OnFault::Return, name, constants)
|
||||
}
|
||||
|
||||
/// The function `name` of `library`, specialised by `constants` when there are any.
|
||||
fn specialised<P: Platform>(
|
||||
library: &P::Library,
|
||||
@@ -7870,7 +7889,12 @@ lacks {part}"
|
||||
.unwrap(),
|
||||
Err(DecodeError::Refused)
|
||||
);
|
||||
faults.at("MTLLibrary", "newFunctionWithName:", 0, Fault::Crash);
|
||||
faults.at(
|
||||
"MTLLibrary",
|
||||
"newFunctionWithName:constantValues:error:",
|
||||
0,
|
||||
Fault::Crash,
|
||||
);
|
||||
assert_eq!(
|
||||
dec.decode(&device, &whole_render_create(101, false))
|
||||
.unwrap(),
|
||||
@@ -8142,7 +8166,8 @@ lacks {part}"
|
||||
let mark = p.call_log().mark();
|
||||
assert_eq!(dec.decode(&device, &bytes).unwrap(), Ok(()));
|
||||
assert_eq!(replies.lock()[0].status, RGFX_PIPELINE_OK);
|
||||
// Each function made in the wire's order, the binary one with its options.
|
||||
// Each function made in the wire's order, the kernel with its constant values (none), the
|
||||
// binary one with its options.
|
||||
let made: Vec<_> = p
|
||||
.call_log()
|
||||
.since(mark)
|
||||
@@ -8154,7 +8179,7 @@ lacks {part}"
|
||||
assert_eq!(
|
||||
sels,
|
||||
[
|
||||
"newFunctionWithName:",
|
||||
"newFunctionWithName:constantValues:error:",
|
||||
"newFunctionWithName:",
|
||||
"newFunctionWithDescriptor:error:",
|
||||
"newFunctionWithName:",
|
||||
@@ -8812,6 +8837,169 @@ lacks {part}"
|
||||
);
|
||||
}
|
||||
|
||||
/// The Vulkan crate's metallib of a kernel that reads a function constant only where it is
|
||||
/// defined (its `optional_constant.metal`), which a host without Metal takes as a Mac does.
|
||||
#[cfg(any(target_os = "macos", feature = "vulkan"))]
|
||||
const OPTIONAL_CONSTANT: &[u8] =
|
||||
include_bytes!("../../../../reims-vgpu-vulkan/metallibs/optional_constant.metallib");
|
||||
|
||||
/// What [`OPTIONAL_CONSTANT`]'s `optional_constant` writes, made through the decoder on `P`
|
||||
/// from a compute create of each of `cases`' constants: `None` where the create is refused.
|
||||
#[cfg(any(target_os = "macos", feature = "vulkan"))]
|
||||
fn optional_constant_through_the_decoder<P: crate::platform::ApiPlatform>(
|
||||
cases: &[&[RgfxFunctionConstant]],
|
||||
) -> Vec<Option<i32>> {
|
||||
use reims_vgpu_core::platform::command::{CommandBuffer as _, CommandQueue as _};
|
||||
use reims_vgpu_core::platform::compute::{ComputeCmd, ComputeEncoder as _};
|
||||
use reims_vgpu_core::platform::device::GpuDevice as _;
|
||||
use reims_vgpu_core::platform::resource::Buffer as _;
|
||||
use reims_vgpu_core::platform::types::{MtlSize, NsRange};
|
||||
use reims_vgpu_core::platform::values::ComputePassDesc;
|
||||
use reims_vgpu_core::raise::OnFault;
|
||||
let platform = P::process();
|
||||
let device = platform.metal_device(None).unwrap();
|
||||
let mut dec = GraphicsDecoder::<P>::new(
|
||||
Arc::clone(&platform),
|
||||
Arc::new(reims_graphics::region::Region::new(0x10000).unwrap()),
|
||||
);
|
||||
let replies = Arc::new(reims_vgpu_core::sync::Mutex::new(Vec::new()));
|
||||
let sink_replies = Arc::clone(&replies);
|
||||
dec.set_reflection_sink(Arc::new(move |r: PipelineReply| {
|
||||
sink_replies
|
||||
.lock()
|
||||
.push((r.reflection.obj_id, r.reflection.status));
|
||||
}));
|
||||
let mut m = RgfxLibraryCreate {
|
||||
header: RgfxMsgHeader {
|
||||
r#type: RGFX_CMD_LIBRARY_CREATE,
|
||||
size: (RGFX_LIBRARY_BYTES_OFFSET as usize + OPTIONAL_CONSTANT.len())
|
||||
.next_multiple_of(8) as u32,
|
||||
seqno: 1,
|
||||
},
|
||||
obj_id: 1,
|
||||
byte_length: OPTIONAL_CONSTANT.len() as u64,
|
||||
..RgfxLibraryCreate::default()
|
||||
}
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
m.extend_from_slice(OPTIONAL_CONSTANT);
|
||||
m.resize(m.len().next_multiple_of(8), 0);
|
||||
assert_eq!(dec.decode(&device, &m).unwrap(), Ok(()));
|
||||
let mut made = Vec::new();
|
||||
for (n, constants) in cases.iter().enumerate() {
|
||||
let obj_id = 100 + n as u64;
|
||||
let mut bytes = compute_create(obj_id);
|
||||
let name = b"optional_constant";
|
||||
let at = bytes.len() as u32;
|
||||
bytes.extend_from_slice(name);
|
||||
bytes.resize(bytes.len().next_multiple_of(8), 0);
|
||||
let constants_at = bytes.len() as u32;
|
||||
bytes.extend_from_slice(constants.as_bytes());
|
||||
let size = bytes.len() as u32;
|
||||
edit_head(&mut bytes, |m: &mut RgfxComputePipelineCreate| {
|
||||
m.header.size = size;
|
||||
(m.name_offset, m.name_len) = (at, name.len() as u32);
|
||||
(m.constants_offset, m.constant_count) = (constants_at, constants.len() as u32);
|
||||
});
|
||||
let decoded = dec.decode(&device, &bytes).unwrap();
|
||||
assert!(
|
||||
decoded == Ok(()) || decoded == Err(DecodeError::Refused),
|
||||
"{constants:?}: {decoded:?}"
|
||||
);
|
||||
let status = replies
|
||||
.lock()
|
||||
.pop()
|
||||
.map(|(id, status)| (id == obj_id, status));
|
||||
let Some(ObjHandle::ComputePipeline(pipeline)) = dec.object(obj_id) else {
|
||||
assert_eq!(status, Some((true, RGFX_PIPELINE_REFUSED)), "{constants:?}");
|
||||
made.push(None);
|
||||
continue;
|
||||
};
|
||||
assert_eq!(status, Some((true, RGFX_PIPELINE_OK)), "{constants:?}");
|
||||
let output = device
|
||||
.new_buffer_of_length(OnFault::Return, 4, 0)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let queue = device.new_command_queue().unwrap();
|
||||
let cb = queue.command_buffer(OnFault::Return).unwrap().unwrap();
|
||||
let enc = cb
|
||||
.compute_encoder_for_pass(&ComputePassDesc::default())
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let one = MtlSize::new(1, 1, 1);
|
||||
for cmd in [
|
||||
ComputeCmd::SetComputePipelineState {
|
||||
state: Some(pipeline),
|
||||
},
|
||||
ComputeCmd::SetBuffers {
|
||||
buffers: &[Some(&output)],
|
||||
offsets: &[0],
|
||||
range: NsRange {
|
||||
location: 0,
|
||||
length: 1,
|
||||
},
|
||||
},
|
||||
ComputeCmd::DispatchThreadgroups {
|
||||
threadgroups_per_grid: one,
|
||||
threads_per_threadgroup: one,
|
||||
},
|
||||
ComputeCmd::EndEncoding,
|
||||
] {
|
||||
enc.encode(OnFault::Return, cmd).unwrap();
|
||||
}
|
||||
cb.commit(OnFault::Return).unwrap();
|
||||
cb.wait_until_completed(OnFault::Return).unwrap();
|
||||
// SAFETY: the GPU wrote the buffer's 4 bytes, and is done.
|
||||
made.push(Some(unsafe { output.contents().as_ptr::<i32>().read() }));
|
||||
}
|
||||
made
|
||||
}
|
||||
|
||||
/// The constants [`optional_constant_through_the_decoder`]'s creates send: none, as the guest
|
||||
/// sends a function made with constant values that set none of its constants, or by a
|
||||
/// descriptor without them; then its constant, an `int` (`MTLDataTypeInt`) of 5.
|
||||
#[cfg(any(target_os = "macos", feature = "vulkan"))]
|
||||
fn optional_constant_cases() -> [Vec<RgfxFunctionConstant>; 2] {
|
||||
let mut value = [0; reims_graphics::wire::RGFX_FUNCTION_CONSTANT_VALUE_BYTES];
|
||||
value[..4].copy_from_slice(&5i32.to_le_bytes());
|
||||
[
|
||||
Vec::new(),
|
||||
vec![RgfxFunctionConstant {
|
||||
index: 0,
|
||||
data_type: 29,
|
||||
value,
|
||||
}],
|
||||
]
|
||||
}
|
||||
|
||||
/// On Metal, from the wire: a kernel that declares a function constant, sent with none, as
|
||||
/// the guest sends a function made with constant values that set none of its constants, is
|
||||
/// made, and reads it as undefined (7), as Metal makes one of such values; sent with it, it
|
||||
/// reads its value (5)
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn on_metal_a_stage_function_sent_with_no_constants_is_made_with_them_undefined() {
|
||||
let cases = optional_constant_cases();
|
||||
let cases: Vec<&[RgfxFunctionConstant]> = cases.iter().map(Vec::as_slice).collect();
|
||||
assert_eq!(
|
||||
optional_constant_through_the_decoder::<reims_vgpu_metal::MetalPlatform>(&cases),
|
||||
[Some(7), Some(5)]
|
||||
);
|
||||
}
|
||||
|
||||
/// [`on_metal_a_stage_function_sent_with_no_constants_is_made_with_them_undefined`] on the
|
||||
/// Vulkan platform, whose answers are Metal's
|
||||
#[cfg(feature = "vulkan")]
|
||||
#[test]
|
||||
fn on_vulkan_a_stage_function_sent_with_no_constants_is_made_with_them_undefined() {
|
||||
let cases = optional_constant_cases();
|
||||
let cases: Vec<&[RgfxFunctionConstant]> = cases.iter().map(Vec::as_slice).collect();
|
||||
assert_eq!(
|
||||
optional_constant_through_the_decoder::<reims_vgpu_vulkan::VulkanPlatform>(&cases),
|
||||
[Some(7), Some(5)]
|
||||
);
|
||||
}
|
||||
|
||||
/// The Vulkan crate's metallib of the readers of a texture (its `ycbcr_probe.metal`), which a
|
||||
/// host without Metal takes as a Mac does.
|
||||
#[cfg(any(target_os = "macos", feature = "vulkan"))]
|
||||
|
||||
@@ -6172,7 +6172,12 @@ fn a_compile_the_platform_refuses_is_a_refused_reflection() {
|
||||
);
|
||||
guest.send(&device, compute_pipeline_create(obj(1, 12), obj(1, 10)));
|
||||
expect_reflection(&mut guest, obj(1, 12), RGFX_PIPELINE_REFUSED);
|
||||
faults.at("MTLLibrary", "newFunctionWithName:", 0, Fault::Crash);
|
||||
faults.at(
|
||||
"MTLLibrary",
|
||||
"newFunctionWithName:constantValues:error:",
|
||||
0,
|
||||
Fault::Crash,
|
||||
);
|
||||
guest.send(&device, compute_pipeline_create(obj(1, 13), obj(1, 10)));
|
||||
expect_reflection(&mut guest, obj(1, 13), RGFX_PIPELINE_REFUSED);
|
||||
assert_eq!(device.decoder_object_count(), 1, "only the library");
|
||||
|
||||
Reference in new issue
Block a user