wasmtime_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    isa::{self, OwnedTargetIsa},
10    CodegenResult,
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        if name == "wasmtime_linkopt_padding_between_functions" {
69            self.linkopts.padding_between_functions = value.parse()?;
70            return Ok(());
71        }
72        if name == "wasmtime_linkopt_force_jump_veneer" {
73            self.linkopts.force_jump_veneers = value.parse()?;
74            return Ok(());
75        }
76
77        self.inner.set(name, value)
78    }
79
80    fn enable(&mut self, name: &str) -> Result<()> {
81        self.inner.enable(name)
82    }
83
84    fn set_tunables(&mut self, tunables: Tunables) -> Result<()> {
85        self.tunables = Some(tunables);
86        Ok(())
87    }
88
89    fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {
90        let isa = self.inner.build()?;
91        Ok(Box::new(crate::compiler::Compiler::new(
92            self.tunables
93                .as_ref()
94                .expect("set_tunables not called")
95                .clone(),
96            isa,
97            self.cache_store.clone(),
98            self.linkopts.clone(),
99            self.clif_dir.clone(),
100            self.wmemcheck,
101        )))
102    }
103
104    fn settings(&self) -> Vec<Setting> {
105        self.inner.settings()
106    }
107
108    fn enable_incremental_compilation(
109        &mut self,
110        cache_store: Arc<dyn wasmtime_environ::CacheStore>,
111    ) -> Result<()> {
112        self.cache_store = Some(cache_store);
113        Ok(())
114    }
115
116    fn wmemcheck(&mut self, enable: bool) {
117        self.wmemcheck = enable;
118    }
119}
120
121impl fmt::Debug for Builder {
122    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123        f.debug_struct("Builder")
124            .field("shared_flags", &self.inner.shared_flags().to_string())
125            .finish()
126    }
127}