Libraries of shaders report what their driver found, so a found one's record is not written again

What: where the device links its render pipelines from graphics pipeline libraries, the
pre-rasterization and fragment shader libraries were made through the shader cache with no
VK_EXT_pipeline_creation_feedback, so every such making read the driver's data back and compared
its SHA-256 with what the cache was loaded with. They now chain the feedback as a whole
pipeline's making does (VariantObjects::library_of_shaders). A library that the driver reports
found in the cache its record loaded reads nothing back and writes nothing (driver_data_found).
The reports count in driver_pipeline_hits and misses, as the pipeline_records and cache lanes
already count a linked device's makings.

Why: tests::warm::a_pass_over_its_records_own_directory_counts_only_what_it_writes failed in 1 of
3 full runtime suites on PHOENIX. The cause is the device, not another test or the load. I probed
the driver's data before and after each making on gem10's Radeon 780M (RADV PHOENIX, Mesa
26.2.6, release, --cfg m2v_gpu): the test failed in 2 of 20 runs alone. In both, the state's two
libraries were found in their loaded records, and each left data of the record's length with
one byte different (of 1352 and 1092 bytes, at offsets 708 and 796). keep() took the new
SHA-256 for new data and wrote both records again, while counting no growth: "2 records' data
written, pipeline_bytes 0". Whole pipelines report found and read nothing back, so the same drift
on them wrote nothing. The data's format is the driver's own, so no comparison here reads its
entries; the driver's report decides.

The negative: tests::driver_pipeline_compile::a_library_of_shaders_reports_what_its_driver_found.
A second device, over the directory where a first device's creation of a state wrote, must count
one report per library of shaders (2), with found == hits, and where both are found it writes no
record. Without the fix it reads 0 hits and 0 misses and fails.

Verified on gem10's 780M (RADV PHOENIX via MESA_VK_DEVICE_SELECT=1002:15bf!, Mesa 26.2.6,
release, --cfg m2v_gpu), in one job over the test alone and then the fix:
- The test alone (the negative) fails: 0 reports, 2 records loaded. The warm test x100 without
  the fix: 99 passed and 1 failed (2 records' data written, 2 found, 0 unchanged).
- With the fix:
  - The new test passed x21: 2 hits, 0 misses, 2 found, 0 written.
  - The warm test passed x101: 0 bytes, 0 written, 4 found.
  - tests::warm, shader_cache and driver_pipeline_compile: 31 passed.
  - The runtime suite (the GPU-hazard filter, the GPU-time tests left out) passed in 3 runs,
    1964 tests each.
  - runtime_lane::pipeline_records and runtime_lane::cache: 10 passed. Their second device
    read loads 2, saves 0, found 2, hits 2.
- cargo clippy -p metal2vulkan-runtime --all-targets --all-features -D warnings is clean, plain
  and with --cfg m2v_gpu.

(cherry picked from commit 037983e7a04af9346bcad1e4d4b61af864627fe0)
This commit is contained in:
Anees Iqbal committed 2026-11-09 02:51:59 -08:00
1 parent afe01b6093
commit 5cd5f93c9e
5 files changed
+150 -16

No files matched your search

@@ -1347,24 +1347,26 @@ impl VariantObjects {
let mut flag =
vk::GraphicsPipelineLibraryCreateInfoEXT::default().flags(part.flag());
let info = infos.part(part, &mut flag);
let create = |cache| {
// SAFETY: every pointer in `info` outlives the call; the pipeline cache is
// the making's own, destroyed once the call returned, or none.
unsafe { shared.vk().create_graphics_pipelines(cache, &[info], None) }
};
// A part with shaders is made through the device's shader cache, under a record
// of its own; one without holds nothing a record would keep.
let made = match part.holds_shaders() {
true => {
let _ = creation_log::take_record();
let made =
shared.make_pipelines(Making::new(part.kind()).digest(name), create);
self.library_of_shaders(Making::new(part.kind()).digest(name), info);
made_here = Some(creation_log::take_record());
made
}
false => {
made_here = Some(None);
create(vk::PipelineCache::null())
// SAFETY: every pointer in `info` outlives the call.
unsafe {
shared.vk().create_graphics_pipelines(
vk::PipelineCache::null(),
&[info],
None,
)
}
}
};
let made = made.map_err(|(_, error)| vulkan(error))?;
@@ -1405,6 +1407,50 @@ impl VariantObjects {
.copied()
.ok_or_else(|| internal("the driver linked no pipeline"))
}
/// The library of `info`, a part that holds shaders, made through the device's shader cache
/// under the record `making` names, with what its driver reports of it as a whole pipeline's
/// making has (`shader_cache::found_in_cache`, counted in `driver_pipeline_hits` and
/// `misses`): one found in a cache loaded from its record reads no data back and writes
/// nothing (`shader_cache/driver.rs`), whatever bytes the driver's data holds after it.
fn library_of_shaders(
&self,
making: Making,
info: vk::GraphicsPipelineCreateInfo<'_>,
) -> Result<Vec<vk::Pipeline>, (Vec<vk::Pipeline>, vk::Result)> {
let shared = self.device.shared();
let driver_reports = shared.shader_cache.driver_reports();
// Its stages' feedback where it has at most `REPORTED_STAGES`, none where more (a count
// of 0 is valid, one equal to `stageCount` else:
// VUID-VkGraphicsPipelineCreateInfo-pipelineStageCreationFeedbackCount-06594).
let mut whole = vk::PipelineCreationFeedback::default();
let mut stages = [vk::PipelineCreationFeedback::default(); REPORTED_STAGES];
let count = info.stage_count as usize;
let reported_stages = if count <= REPORTED_STAGES { count } else { 0 };
let made = shared.make_pipelines_reported(making, |cache| {
let reported = &mut stages[..reported_stages];
let mut feedback = vk::PipelineCreationFeedbackCreateInfo::default()
.pipeline_creation_feedback(&mut whole)
.pipeline_stage_creation_feedbacks(reported);
let mut info = info;
if driver_reports {
info = info.push_next(&mut feedback);
}
// SAFETY: every pointer in `info` outlives the call; the pipeline cache is the
// device's for the record, or none.
let made = unsafe { shared.vk().create_graphics_pipelines(cache, &[info], None) };
let found = driver_reports
.then(|| found_in_cache(&whole, &stages[..reported_stages]))
.flatten();
(made, found)
});
if driver_reports && made.is_ok() {
shared
.shader_cache
.driver_reported(&whole, &stages[..reported_stages]);
}
made
}
}
#[cfg(test)]
@@ -30,9 +30,14 @@
//! library map iterates its modules' keys opposite to their insertion), so the byte comparison
//! alone wrote every record of two modules again at each warm making. The data's format is
//! MoltenVK's own, undocumented beyond Vulkan's header, so no comparison here reads its
//! entries. A making whose driver reports nothing, or reports a compile, or whose cache was
//! made empty, reads the data and compares its SHA-256 with what the cache was loaded with or
//! last wrote.
//! entries. A library of shaders, where the device links its render pipelines from graphics
//! pipeline libraries, is made with its driver's report as a whole pipeline is: RADV's data
//! after a library's making from its record is now and then not the record's byte for byte,
//! one byte of over a thousand differing at the same length (Mesa 26.2.6 on a Radeon 780M:
//! both libraries of a state, in 2 runs of 20), which the byte comparison alone took for data
//! the making added, and wrote the record again. A making whose driver reports nothing, or
//! reports a compile, or whose cache was made empty, reads the data and compares its SHA-256
//! with what the cache was loaded with or last wrote.
//! - **Named by what a stage's conversion reads** ([`Making`]). A record is named by the SHA-256
//! of [`VERSION`], the making's description, the driver's identity (vendor, device, driver
//! version, `pipelineCacheUUID`) and the digest of the layer settings the instance was created
@@ -250,7 +250,9 @@ pub struct ShaderCacheStatistics {
/// `APPLICATION_PIPELINE_CACHE_HIT`, or every stage's feedback is valid and says it. Drivers
/// differ in where they say it: most for the whole pipeline; MoltenVK 1.4.1 for each stage
/// alone, whose Metal function it found converted in the cache (its whole pipeline's
/// feedback is only ever valid).
/// feedback is only ever valid). Where the device links render pipelines from graphics
/// pipeline libraries, each library of shaders is such a making (its pre-rasterization or
/// fragment shader part), and the link itself is none.
pub driver_pipeline_hits: u64,
/// Such pipelines whose valid feedback says no hit, for the whole pipeline or for every
/// stage: compiled, or a stage converted again.
@@ -52,6 +52,11 @@
//! library of shaders, so nothing of its stages is converted or compiled again; and no making
//! asks for a cache, so none is shared.
//!
//! **A library of shaders reports what its driver found**, as a whole pipeline's making does: a
//! later device over the directory an earlier one wrote counts one report per library of shaders
//! it makes, and reads no data back for one found in its record
//! (`a_library_of_shaders_reports_what_its_driver_found`).
//!
//! The device takes a shader cache directory of its own, empty, so no run reads a record an
//! earlier run stored.
//!
@@ -167,3 +172,78 @@ fn every_sibling_render_creation_takes_the_first_s_driver_cache() {
{RUN} found there, their stages being the same: {hits} hits and {misses} misses"
);
}
/// Where the device links its render pipelines from libraries and its makings report, the making
/// of each library of shaders reports what its driver found, as a whole pipeline's making does: a
/// later device over the directory an earlier one's creation of the same state wrote counts one
/// report for each of the state's pre-rasterization and fragment shader libraries, and each one
/// the driver reports found in the cache its record loaded reads no data back and writes nothing
/// (`driver_data_found`). Unreported, a library's making read its data back and compared the
/// bytes, and RADV's data after a library found in its record is now and then not the record's
/// byte for byte (`shader_cache/driver.rs`), so such a device wrote the record again.
#[test]
fn a_library_of_shaders_reports_what_its_driver_found() {
let scratch = Scratch::new("driver-library-reports");
let options = PipelineOptions::default();
{
let device = device(&scratch.0);
let shared = device.shared();
if shared.libraries.is_none() || !shared.shader_cache.driver_reports() {
println!(
"DRIVER-LIBRARY-REPORTS: not covered (links from libraries: {}, makings report: \
{})",
shared.libraries.is_some(),
shared.shader_cache.driver_reports()
);
return;
}
drop(
device
.new_render_pipeline_state(&descriptor(&device), &options)
.unwrap(),
);
}
// The first device is gone, so its cache thread wrote what its makings handed it.
let device = device(&scratch.0);
let base = descriptor(&device);
let before = device.shader_cache_statistics();
let counts_before = device.work_counts();
let _state = device.new_render_pipeline_state(&base, &options).unwrap();
let after = device.shader_cache_statistics();
let counts = device.work_counts();
let made = |of: fn(&crate::WorkCounts) -> u64| of(&counts) - of(&counts_before);
let since = |of: fn(&crate::ShaderCacheStatistics) -> u64| of(&after) - of(&before);
let (hits, misses) = (
since(|s| s.driver_pipeline_hits),
since(|s| s.driver_pipeline_misses),
);
let (found, saves) = (since(|s| s.driver_data_found), since(|s| s.driver_saves));
println!(
"DRIVER-LIBRARY-REPORTS: {hits} hits and {misses} misses reported, {found} found and read \
nothing back, {saves} records' data written, {} loaded",
since(|s| s.driver_loads)
);
// The fresh device makes each of the state's four libraries once and links them.
assert_eq!(
(
made(|c| c.pipelines_linked),
made(|c| c.pipeline_libraries_made),
made(|c| c.fragment_shader_libraries_made)
),
(1, 4, 1),
"the state links one pipeline from four libraries made for it"
);
assert_eq!(
hits + misses,
2,
"each of the state's two libraries of shaders reports what its driver found: {hits} hits \
and {misses} misses"
);
assert_eq!(
found, hits,
"each library its driver reports found in the cache its record loaded reads no data back"
);
if hits == 2 {
assert_eq!(saves, 0, "libraries found in their records write nothing");
}
}
@@ -13,11 +13,12 @@
//! draw's target byte for byte the first device's. A making handed a cache loaded from its
//! record that the driver reports found there reads no data back and writes nothing
//! (`driver_data_found`: MoltenVK's data after such a making is the record's entries in the
//! other order, which a byte comparison took for a change at every making). One the driver does
//! not report found writes it again exactly where the driver's data after the making is no
//! longer what the cache was loaded with or last wrote (RADV's after its first hit from a
//! compile's record is not, and settles), and counts every other read unchanged
//! (`driver_data_unchanged`).
//! other order, which a byte comparison took for a change at every making, and RADV's now and
//! then differs from the record's in a byte at the same length). Where the device links its
//! pipelines from libraries, each library of shaders is such a making and reports as a whole
//! pipeline does. One the driver does not report found writes it again exactly where the
//! driver's data after the making is no longer what the cache was loaded with or last wrote,
//! and counts every other read unchanged (`driver_data_unchanged`).
//! - **Lines from the triangles' cache.** A state's line pipeline, made at its first line draw,
//! names the record its triangle sibling named, which the state's creation made the cache of:
//! the topology reaches the driver's conversion of a stage through the point class alone