Runtime: a kernel past the stage bound holds no VkShaderModule; each make of its pipelines makes its own and destroys it
A heavy kernel (past the stage bound, made only from its helper's cache data) kept the shader module its words made for the content's whole life, though only its VkPipelines' creates name it. A driver keeps a copy of the module's code for that life: on an RTX 3090 (NVIDIA 620.70.06, job m3-max-67862) destroying the module of kernel 16d856fc (7,966,928 bytes of words), its pipeline kept, gave back 7.6 MiB of heap, per heavy content, while the app runs. The kernel now holds one of two things (`Code`): a light kernel's shader module, made with it as before, or a heavy kernel's `Heavy`, its words. `stage::made` makes a shader module of those words only once the pipeline's entry holds data, creates the pipeline through the entry's cache and that module, and destroys both once the create returns (Vulkan: "A shader module can be destroyed while pipelines created using its shaders are still in use"). A make refused unfilled makes no module. Two makes of one kernel's variants at once each make their own, since make_pending lets go of the variants' lock before it creates; a module-less create needs maintenance5 or graphicsPipelineLibrary, neither on the floor. `release` frees a module only for a light kernel. The words stay held (dropping them is a later step), and nothing else changes: the same stage keys, entries, requests and wire. Docs: the runtime README's "Kernels past the stage bound" make bullet and the map paragraph's "its shader modules", and the module docs of stage.rs and pipeline.rs.
This commit is contained in:
1 parent
6cc7e45145
commit
6ec733421c
3 files changed
+129
-68
No files matched your search
@@ -2515,8 +2515,9 @@ while the driver's exit handler waited on a lock, with every runtime thread alre
|
||||
constant values, linked functions and dynamic libraries, the descriptor's fields that change
|
||||
the compile (the translator's `Device::compute_pipeline_key`), and its
|
||||
`requiredThreadsPerThreadgroup`. Every state of one content shares one compiled kernel: its
|
||||
translation, its shader modules and their `VkPipeline`s, its emulated-SIMD-group variant and
|
||||
its precision notes. The device's map holds the last 1,024 contents used, so a state of a
|
||||
translation, its shader modules (a module past the stage bound as its words, with no shader
|
||||
module: Kernels past the stage bound) and their `VkPipeline`s, its emulated-SIMD-group variant
|
||||
and its precision notes. The device's map holds the last 1,024 contents used, so a state of a
|
||||
content it holds costs a lookup and its samplers' numbering, and compiles nothing. Past them the
|
||||
map forgets the least recently used content, which every state that took it keeps; a compiled
|
||||
kernel goes once its last state and the map have let it go. Each state numbers its own
|
||||
@@ -2899,10 +2900,16 @@ heaviest such stage's weight.
|
||||
|
||||
- The make of such a `VkPipeline` (at the state's creation, a `prepare_dispatch` call or a
|
||||
dispatch) reads the pipeline's entry under `stages/` in the cache directory. With data, it
|
||||
makes a `VkPipelineCache` of that entry alone, creates the pipeline through it with
|
||||
`VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT`, and destroys the cache: the
|
||||
device's own pipeline cache and its blob never hold it. `Metrics::pipelines_from_stage_fills`
|
||||
counts these creates, which `pipelines_created` counts too.
|
||||
makes a `VkPipelineCache` of that entry alone and a shader module of the kernel's words,
|
||||
creates the pipeline through them with
|
||||
`VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT`, and destroys both: the device's own
|
||||
pipeline cache and its blob never hold the pipeline, and the kernel holds no shader module,
|
||||
only its words, of which each make, two at once included, makes its own. Vulkan lets a module
|
||||
go while pipelines made of it are in use. A driver may keep a copy of a module's code for the
|
||||
module's life: destroying the module of a kernel of 7,966,928 bytes of words, its pipeline
|
||||
kept, gave back 7.6 MiB of heap on an RTX 3090 (NVIDIA 620.70.06; job m3-max-67862).
|
||||
`Metrics::pipelines_from_stage_fills` counts these creates, which `pipelines_created` counts
|
||||
too.
|
||||
- With no entry, the make is refused with `Error::StageUnfilled`, carrying a `StageRequest`. Two
|
||||
requests of one pipeline compare equal and hash alike (by the pipeline's stage key, the name of
|
||||
its entry), so a consumer joins the makes, the `prepare_dispatch` calls and the dispatches that
|
||||
|
||||
@@ -22,7 +22,9 @@
|
||||
//! What a state compiles is its content ([`KernelKey`]), never the state: the translator's key
|
||||
//! of its descriptor and its `requiredThreadsPerThreadgroup`. Every state of one content shares
|
||||
//! one [`Compiled`]: its translation, its shader modules and their `VkPipeline`s, its emulated
|
||||
//! variant and its notes. The device's map of kernels holds each content's (`kernel_map`), so a
|
||||
//! variant and its notes. A module past the stage bound is kept as its words and no shader
|
||||
//! module: each make of its `VkPipeline`s makes one of its own and destroys it once the create
|
||||
//! returns (`crate::stage`). The device's map of kernels holds each content's (`kernel_map`), so a
|
||||
//! state of a content the map holds costs a lookup; past its cap the map forgets the least
|
||||
//! recently used, which a state that took it keeps. What stays a state's own is what the consumer
|
||||
//! gives it: the indices of its numbered samplers and the constexpr samplers' elements it holds
|
||||
@@ -36,10 +38,11 @@
|
||||
//! with the state too.
|
||||
//!
|
||||
//! What a state's make is given is made at the make, as Metal compiles a state when it is made:
|
||||
//! every module ([`Lowered`] holds each variant's), and each `VkPipeline` whose key the descriptor
|
||||
//! holds (a tiling kernel's tiles, an image-only kernel's square workgroups, and an observing
|
||||
//! kernel's at the descriptor's `requiredThreadsPerThreadgroup`, or, with none, at `[N, 1, 1]`
|
||||
//! for the bound `N` of threads its state declares). Nothing the runtime records
|
||||
//! every module ([`Lowered`] holds each variant's: a shader module, or a module past the stage
|
||||
//! bound's words), and each `VkPipeline` whose key the descriptor holds (a tiling kernel's
|
||||
//! tiles, an image-only kernel's square workgroups, and an observing kernel's at the
|
||||
//! descriptor's `requiredThreadsPerThreadgroup`, or, with none, at `[N, 1, 1]` for the bound `N`
|
||||
//! of threads its state declares). Nothing the runtime records
|
||||
//! depends on when a compile finishes: an encoding call whose `VkPipeline` is not made yet makes
|
||||
//! it and waits for it, then records, so there is one recorded form. Each such wait is a
|
||||
//! first-use wait, counted with its time
|
||||
@@ -408,17 +411,25 @@ pub(crate) enum Partial {
|
||||
|
||||
/// One translated module of a state, its interface, and the `VkPipeline`s made of it so far.
|
||||
pub(crate) struct Kernel {
|
||||
module: vk::ShaderModule,
|
||||
code: Code,
|
||||
/// The module's entry point.
|
||||
entry: CString,
|
||||
pub(crate) interface: Interface,
|
||||
/// The module, where it is past the stage bound: each `VkPipeline` is made from its helper's
|
||||
/// cache data alone (`crate::stage`).
|
||||
heavy: Option<Heavy>,
|
||||
/// The `VkPipeline`s made so far, by their specialization.
|
||||
variants: Variants,
|
||||
}
|
||||
|
||||
/// What a kernel's `VkPipeline`s are created from.
|
||||
enum Code {
|
||||
/// A module at or under the stage bound: its shader module, made with the kernel and named
|
||||
/// by each of its `VkPipeline`s' creates.
|
||||
Module(vk::ShaderModule),
|
||||
/// A module past the stage bound: each `VkPipeline` is made from its helper's cache data
|
||||
/// alone, through a shader module of that make's own, made from the module's words and
|
||||
/// destroyed once the create returns (`crate::stage`). The kernel holds no shader module.
|
||||
Heavy(Heavy),
|
||||
}
|
||||
|
||||
/// The variants of a kernel that each have a cell of their own.
|
||||
const VARIANT_CELLS: usize = 8;
|
||||
|
||||
@@ -1963,10 +1974,9 @@ impl ComputePipelineState {
|
||||
let module = unsafe { Kernel::module(&core, words) }
|
||||
.map_err(Error::vulkan("vkCreateShaderModule"))?;
|
||||
let whole = Kernel {
|
||||
module,
|
||||
code: Code::Module(module),
|
||||
entry: c"main".to_owned(),
|
||||
interface,
|
||||
heavy: None,
|
||||
variants: Variants::default(),
|
||||
};
|
||||
let form = Form::Sized {
|
||||
@@ -2103,7 +2113,8 @@ impl Lowered {
|
||||
}
|
||||
|
||||
impl Kernel {
|
||||
/// The translator's `pipeline` as a module of the device, with its interface.
|
||||
/// The translator's `pipeline` as a module of the device, with its interface: its shader
|
||||
/// module made now, or, past the stage bound, its words kept for each make's own.
|
||||
fn new(
|
||||
core: &Core,
|
||||
pipeline: ComputePipeline,
|
||||
@@ -2115,13 +2126,19 @@ impl Kernel {
|
||||
// The translator's entry points hold no NUL; one that did would name no entry point.
|
||||
let entry = CString::new(stage.entry_point)
|
||||
.map_err(|_| Unmade::Unsupplied(KernelInputKind::EntryPoint))?;
|
||||
// SAFETY: the words are the translator's SPIR-V, for the features device creation enabled.
|
||||
let module = unsafe { Kernel::module(core, &stage.spirv) }.map_err(Unmade::Failed)?;
|
||||
let code = match heavy {
|
||||
Some(heavy) => Code::Heavy(heavy),
|
||||
None => {
|
||||
// SAFETY: the words are the translator's SPIR-V, for the features device creation
|
||||
// enabled.
|
||||
let module = unsafe { Kernel::module(core, &stage.spirv) };
|
||||
Code::Module(module.map_err(Unmade::Failed)?)
|
||||
}
|
||||
};
|
||||
Ok(Kernel {
|
||||
module,
|
||||
code,
|
||||
entry,
|
||||
interface,
|
||||
heavy,
|
||||
variants: Variants::default(),
|
||||
})
|
||||
}
|
||||
@@ -2140,7 +2157,7 @@ impl Kernel {
|
||||
/// For the device tests: whether the kernel holds a shader module of its own.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) fn holds_module(&self) -> bool {
|
||||
self.module != vk::ShaderModule::null()
|
||||
matches!(self.code, Code::Module(_))
|
||||
}
|
||||
|
||||
/// The `VkPipeline` of `variant`, made now where it is not yet: the calling thread waits for
|
||||
@@ -2296,46 +2313,58 @@ impl Kernel {
|
||||
vk::PipelineShaderStageRequiredSubgroupSizeCreateInfo::default()
|
||||
.required_subgroup_size(size)
|
||||
});
|
||||
let mut stage = vk::PipelineShaderStageCreateInfo::default()
|
||||
.flags(self.interface.stage_flags)
|
||||
.stage(vk::ShaderStageFlags::COMPUTE)
|
||||
.module(self.module)
|
||||
.name(&self.entry)
|
||||
.specialization_info(&specialization);
|
||||
if let Some(required) = &mut required {
|
||||
stage = stage.push_next(required);
|
||||
}
|
||||
let info = vk::ComputePipelineCreateInfo::default()
|
||||
.stage(stage)
|
||||
.layout(core.layout);
|
||||
if let Some(heavy) = &self.heavy {
|
||||
let ids: Vec<u32> = constants.iter().map(|&(id, _)| id).collect();
|
||||
let create = Create {
|
||||
entry: &self.entry,
|
||||
flags: self.interface.stage_flags,
|
||||
subgroup: self.interface.required_subgroup_size,
|
||||
ids: &ids,
|
||||
data: &values,
|
||||
};
|
||||
let info = info.flags(vk::PipelineCreateFlags::FAIL_ON_PIPELINE_COMPILE_REQUIRED);
|
||||
return crate::stage::made(core, heavy, &create, |cache| {
|
||||
// SAFETY: as below; the cache is made of the pipeline's entry alone.
|
||||
// The pipeline's one create, of `module` through `cache` with `flags`.
|
||||
let mut create =
|
||||
|module: vk::ShaderModule, cache: vk::PipelineCache, flags: vk::PipelineCreateFlags| {
|
||||
let mut stage = vk::PipelineShaderStageCreateInfo::default()
|
||||
.flags(self.interface.stage_flags)
|
||||
.stage(vk::ShaderStageFlags::COMPUTE)
|
||||
.module(module)
|
||||
.name(&self.entry)
|
||||
.specialization_info(&specialization);
|
||||
if let Some(required) = &mut required {
|
||||
stage = stage.push_next(required);
|
||||
}
|
||||
let info = vk::ComputePipelineCreateInfo::default()
|
||||
.flags(flags)
|
||||
.stage(stage)
|
||||
.layout(core.layout);
|
||||
// SAFETY: the module, the layout and the cache are the device's, and the
|
||||
// specialization gives each of the kernel's constants a 4-byte value.
|
||||
unsafe { core.device.create_compute_pipelines(cache, &[info], None) }
|
||||
});
|
||||
};
|
||||
match &self.code {
|
||||
Code::Module(module) => made_through_cache(core, "vkCreateComputePipelines", |cache| {
|
||||
create(*module, cache, vk::PipelineCreateFlags::empty())
|
||||
}),
|
||||
Code::Heavy(heavy) => {
|
||||
let ids: Vec<u32> = constants.iter().map(|&(id, _)| id).collect();
|
||||
let stage = Create {
|
||||
entry: &self.entry,
|
||||
flags: self.interface.stage_flags,
|
||||
subgroup: self.interface.required_subgroup_size,
|
||||
ids: &ids,
|
||||
data: &values,
|
||||
};
|
||||
// The cache is made of the pipeline's entry alone, and the module of the
|
||||
// kernel's words for this create alone.
|
||||
crate::stage::made(core, heavy, &stage, |cache, module| {
|
||||
let flags = vk::PipelineCreateFlags::FAIL_ON_PIPELINE_COMPILE_REQUIRED;
|
||||
create(module, cache, flags)
|
||||
})
|
||||
}
|
||||
}
|
||||
made_through_cache(core, "vkCreateComputePipelines", |cache| {
|
||||
// SAFETY: the module, the layout and the cache are the device's, and the
|
||||
// specialization gives each of the kernel's constants a 4-byte value.
|
||||
unsafe { core.device.create_compute_pipelines(cache, &[info], None) }
|
||||
})
|
||||
}
|
||||
|
||||
/// Gives the module and its `VkPipeline`s to the reaper.
|
||||
/// Gives its `VkPipeline`s, and a kernel's at or under the stage bound its module, to the
|
||||
/// reaper.
|
||||
fn release(&mut self, core: &Core) {
|
||||
for pipeline in self.variants.drain() {
|
||||
core.reaper.give(Job::Free(Free::Pipeline(pipeline)));
|
||||
}
|
||||
core.reaper.give(Job::Free(Free::ShaderModule(self.module)));
|
||||
if let Code::Module(module) = self.code {
|
||||
core.reaper.give(Job::Free(Free::ShaderModule(module)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,11 @@
|
||||
//! that runs the program never compiles it. Each of its `VkPipeline`s is made from a
|
||||
//! `VkPipelineCache` made of that pipeline's own entry in the cache directory alone, with
|
||||
//! `VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT` ([`made`]), and the cache destroyed
|
||||
//! once the create returns: the device's own pipeline cache and its blob never hold it. Where the
|
||||
//! directory holds no entry for the pipeline, the make (or the dispatch, or
|
||||
//! once the create returns: the device's own pipeline cache and its blob never hold it. The kernel
|
||||
//! keeps its module's words and no shader module: each such create is of a shader module made of
|
||||
//! the words for it alone, destroyed with the cache, as Vulkan lets a module go while pipelines
|
||||
//! made of it are in use, and two makes of one kernel's pipelines at once each make their own.
|
||||
//! Where the directory holds no entry for the pipeline, the make (or the dispatch, or
|
||||
//! [`ComputePipelineState::prepare_dispatch`](crate::ComputePipelineState::prepare_dispatch)) is
|
||||
//! refused with [`Error::StageUnfilled`], carrying a [`StageRequest`]: the consumer runs its helper
|
||||
//! process, hands its pipes to [`Device::fill_stage`], which keeps the helper's cache data as the
|
||||
@@ -171,8 +174,9 @@ impl fmt::Display for StageFill {
|
||||
}
|
||||
}
|
||||
|
||||
/// A kernel's module past the stage bound, kept with its kernel for the requests of its
|
||||
/// `VkPipeline`s.
|
||||
/// A kernel's module past the stage bound, kept with its kernel in place of a shader module: the
|
||||
/// words each make of its `VkPipeline`s makes a shader module of its own from ([`made`]), and
|
||||
/// that a request carries.
|
||||
pub(crate) struct Heavy {
|
||||
/// The module's entry weight.
|
||||
weight: u64,
|
||||
@@ -334,20 +338,24 @@ fn gap(weight: u64, cause: StageGap) -> Error {
|
||||
Error::Unsupported(Gap::StageSize { weight, cause })
|
||||
}
|
||||
|
||||
/// A compute `VkPipeline` of `heavy` that `make` creates through the cache it is given, with
|
||||
/// `VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT` in its create info: through a cache
|
||||
/// made of the pipeline's entry alone, destroyed once `make` returns (the module docs). Counted as
|
||||
/// made, and its time (`Metrics::pipeline_creation_nanos`).
|
||||
/// A compute `VkPipeline` of `heavy` that `make` creates through the cache and of the shader
|
||||
/// module it is given, with `VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT` in its
|
||||
/// create info: through a cache made of the pipeline's entry alone, and of a module made of
|
||||
/// `heavy`'s words for this create alone, both destroyed once `make` returns (the module docs).
|
||||
/// Counted as made, and its time (`Metrics::pipeline_creation_nanos`).
|
||||
///
|
||||
/// Err: [`Error::StageUnfilled`] where the directory holds no entry for it; [`Gap::StageSize`]
|
||||
/// where it holds a breach, where the driver answers compile-required for its data (kept as a
|
||||
/// breach), on a device with no cache directory or whose driver writes no pipeline cache header;
|
||||
/// [`Error::Vulkan`] where the create fails otherwise.
|
||||
/// [`Error::Vulkan`] where the cache's, the module's or the pipeline's create fails otherwise.
|
||||
pub(crate) fn made(
|
||||
core: &Core,
|
||||
heavy: &Heavy,
|
||||
create: &Create<'_>,
|
||||
make: impl FnOnce(vk::PipelineCache) -> Result<Vec<vk::Pipeline>, (Vec<vk::Pipeline>, vk::Result)>,
|
||||
make: impl FnOnce(
|
||||
vk::PipelineCache,
|
||||
vk::ShaderModule,
|
||||
) -> Result<Vec<vk::Pipeline>, (Vec<vk::Pipeline>, vk::Result)>,
|
||||
) -> Result<vk::Pipeline, Error> {
|
||||
let weight = heavy.weight;
|
||||
let dir = (core.caches.dir()).ok_or_else(|| gap(weight, StageGap::NoCacheDirectory))?;
|
||||
@@ -385,12 +393,29 @@ pub(crate) fn made(
|
||||
result,
|
||||
}
|
||||
})?;
|
||||
let info = vk::ShaderModuleCreateInfo::default().code(&heavy.words);
|
||||
// SAFETY: the words are the translator's SPIR-V, for the features device creation enabled.
|
||||
let module = match unsafe { core.device.create_shader_module(&info, None) } {
|
||||
Ok(module) => module,
|
||||
Err(result) => {
|
||||
// SAFETY: the cache is the device's, and nothing uses it.
|
||||
unsafe { core.device.destroy_pipeline_cache(cache, None) };
|
||||
return Err(Error::Vulkan {
|
||||
call: "vkCreateShaderModule",
|
||||
result,
|
||||
});
|
||||
}
|
||||
};
|
||||
let began = host_time();
|
||||
let created = make(cache);
|
||||
let created = make(cache, module);
|
||||
core.counters
|
||||
.pipeline_creation(host_time().saturating_sub(began));
|
||||
// SAFETY: the cache is the device's, and the create that used it has returned.
|
||||
unsafe { core.device.destroy_pipeline_cache(cache, None) };
|
||||
// SAFETY: the cache and the module are the device's, and the create that used them has
|
||||
// returned; a module may be destroyed while pipelines made of it are in use.
|
||||
unsafe {
|
||||
core.device.destroy_pipeline_cache(cache, None);
|
||||
core.device.destroy_shader_module(module, None);
|
||||
}
|
||||
match created {
|
||||
Ok(pipelines) => {
|
||||
core.counters.count(Metric::PipelinesCreated);
|
||||
|
||||
Reference in new issue
Block a user