wasmtime_internal_cranelift/
builder.rs

1//! Implementation of a "compiler builder" for cranelift
2//!
3//! This module contains the implementation of how Cranelift is configured, as
4//! well as providing a function to return the default configuration to build.
5
6use crate::isa_builder::IsaBuilder;
7use anyhow::Result;
8use cranelift_codegen::{
9    CodegenResult,
10    isa::{self, OwnedTargetIsa},
11};
12use std::fmt;
13use std::path;
14use std::sync::Arc;
15use target_lexicon::Triple;
16use wasmtime_environ::{CacheStore, CompilerBuilder, Setting, Tunables};
17
18struct Builder {
19    tunables: Option<Tunables>,
20    inner: IsaBuilder<CodegenResult<OwnedTargetIsa>>,
21    linkopts: LinkOptions,
22    cache_store: Option<Arc<dyn CacheStore>>,
23    clif_dir: Option<path::PathBuf>,
24    wmemcheck: bool,
25}
26
27#[derive(Clone, Default)]
28pub struct LinkOptions {
29    /// A debug-only setting used to synthetically insert 0-byte padding between
30    /// compiled functions to simulate huge compiled artifacts and exercise
31    /// logic related to jump veneers.
32    pub padding_between_functions: usize,
33
34    /// A debug-only setting used to force inter-function calls in a wasm module
35    /// to always go through "jump veneers" which are typically only generated
36    /// when functions are very far from each other.
37    pub force_jump_veneers: bool,
38}
39
40pub fn builder(triple: Option<Triple>) -> Result<Box<dyn CompilerBuilder>> {
41    Ok(Box::new(Builder {
42        tunables: None,
43        inner: IsaBuilder::new(triple, |triple| isa::lookup(triple).map_err(|e| e.into()))?,
44        linkopts: LinkOptions::default(),
45        cache_store: None,
46        clif_dir: None,
47        wmemcheck: false,
48    }))
49}
50
51impl CompilerBuilder for Builder {
52    fn triple(&self) -> &target_lexicon::Triple {
53        self.inner.triple()
54    }
55
56    fn clif_dir(&mut self, path: &path::Path) -> Result<()> {
57        self.clif_dir = Some(path.to_path_buf());
58        Ok(())
59    }
60
61    fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {
62        self.inner.target(target)?;
63        Ok(())
64    }
65
66    fn set(&mut self, name: &str, value: &str) -> Result<()> {
67        // Special wasmtime-cranelift-only settings first
68        match name {
69            "wasmtime_linkopt_padding_between_functions" => {
70                self.linkopts.padding_between_functions = value.parse()?;
71            }
72            "wasmtime_linkopt_force_jump_veneer" => {
73                self.linkopts.force_jump_veneers = value.parse()?;
74            }
75            "wasmtime_inlining_intra_module" => {
76                self.tunables.as_mut().unwrap().inlining_intra_module = value.parse()?;
77            }
78            "wasmtime_inlining_small_callee_size" => {
79                self.tunables.as_mut().unwrap().inlining_small_callee_size = value.parse()?;
80            }
81            "wasmtime_inlining_sum_size_threshold" => {
82                self.tunables.as_mut().unwrap().inlining_sum_size_threshold = value.parse()?;
83            }
84            _ => {
85                self.inner.set(name, value)?;
86            }
87        }
88        Ok(())
89    }
90
91    fn enable(&mut self, name: &str) -> Result<()> {
92        self.inner.enable(name)
93    }
94
95    fn set_tunables(&mut self, tunables: Tunables) -> Result<()> {
96        self.tunables = Some(tunables);
97        Ok(())
98    }
99
100    fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {
101        let isa = self.inner.build()?;
102        Ok(Box::new(crate::compiler::Compiler::new(
103            self.tunables
104                .as_ref()
105                .expect("set_tunables not called")
106                .clone(),
107            isa,
108            self.cache_store.clone(),
109            self.linkopts.clone(),
110            self.clif_dir.clone(),
111            self.wmemcheck,
112        )))
113    }
114
115    fn settings(&self) -> Vec<Setting> {
116        self.inner.settings()
117    }
118
119    fn enable_incremental_compilation(
120        &mut self,
121        cache_store: Arc<dyn wasmtime_environ::CacheStore>,
122    ) -> Result<()> {
123        self.cache_store = Some(cache_store);
124        Ok(())
125    }
126
127    fn wmemcheck(&mut self, enable: bool) {
128        self.wmemcheck = enable;
129    }
130}
131
132impl fmt::Debug for Builder {
133    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134        f.debug_struct("Builder")
135            .field("shared_flags", &self.inner.shared_flags().to_string())
136            .finish()
137    }
138}