spirv_tools/
opt.rs

1#[cfg(feature = "use-compiled-tools")]
2pub mod compiled;
3#[cfg(feature = "use-installed-tools")]
4pub mod tool;
5
6pub use spirv_tools_sys::opt::Passes;
7
8/// Options for specifying the behavior of the optimizer
9#[derive(Default, Clone)]
10pub struct Options {
11    /// Records the validator options that should be passed to the validator,
12    /// the validator will run with the options before optimizer.
13    pub validator_options: Option<crate::val::ValidatorOptions>,
14    /// Records the maximum possible value for the id bound.
15    pub max_id_bound: Option<u32>,
16    /// Records whether all bindings within the module should be preserved.
17    pub preserve_bindings: bool,
18    /// Records whether all specialization constants within the module
19    /// should be preserved.
20    pub preserve_spec_constants: bool,
21}
22
23pub trait Optimizer {
24    fn with_env(target_env: crate::TargetEnv) -> Self;
25
26    fn optimize<MC: crate::error::MessageCallback>(
27        &self,
28        input: impl AsRef<[u32]>,
29        msg_callback: &mut MC,
30        options: Option<Options>,
31    ) -> Result<crate::binary::Binary, crate::Error>;
32
33    /// Register a single pass with the the optimizer.
34    fn register_pass(&mut self, pass: Passes) -> &mut Self;
35    /// Registers passes that attempt to improve performance of generated code.
36    /// This sequence of passes is subject to constant review and will change
37    /// from time to time.
38    fn register_performance_passes(&mut self) -> &mut Self;
39    /// Registers passes that attempt to improve the size of generated code.
40    /// This sequence of passes is subject to constant review and will change
41    /// from time to time.
42    fn register_size_passes(&mut self) -> &mut Self;
43    /// Registers passes that attempt to legalize the generated code.
44    ///
45    /// Note: this recipe is specially designed for legalizing SPIR-V. It should be
46    /// used by compilers after translating HLSL source code literally. It should
47    /// *not* be used by general workloads for performance or size improvement.
48    ///
49    /// This sequence of passes is subject to constant review and will change
50    /// from time to time.
51    fn register_hlsl_legalization_passes(&mut self) -> &mut Self;
52}
53
54pub fn create(te: Option<crate::TargetEnv>) -> impl Optimizer {
55    let target_env = te.unwrap_or_default();
56
57    #[cfg(feature = "use-compiled-tools")]
58    {
59        compiled::CompiledOptimizer::with_env(target_env)
60    }
61
62    #[cfg(all(feature = "use-installed-tools", not(feature = "use-compiled-tools")))]
63    {
64        tool::ToolOptimizer::with_env(target_env)
65    }
66}