1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Implementation of a "compiler builder" for cranelift
//!
//! This module contains the implementation of how Cranelift is configured, as
//! well as providing a function to return the default configuration to build.

use anyhow::Result;
use cranelift_codegen::{
    isa::{self, OwnedTargetIsa},
    CodegenResult,
};
use std::fmt;
use std::path;
use std::sync::Arc;
use target_lexicon::Triple;
use wasmtime_cranelift_shared::isa_builder::IsaBuilder;
use wasmtime_environ::{CacheStore, CompilerBuilder, Setting, Tunables};

struct Builder {
    tunables: Option<Tunables>,
    inner: IsaBuilder<CodegenResult<OwnedTargetIsa>>,
    linkopts: LinkOptions,
    cache_store: Option<Arc<dyn CacheStore>>,
    clif_dir: Option<path::PathBuf>,
    wmemcheck: bool,
}

#[derive(Clone, Default)]
pub struct LinkOptions {
    /// A debug-only setting used to synthetically insert 0-byte padding between
    /// compiled functions to simulate huge compiled artifacts and exercise
    /// logic related to jump veneers.
    pub padding_between_functions: usize,

    /// A debug-only setting used to force inter-function calls in a wasm module
    /// to always go through "jump veneers" which are typically only generated
    /// when functions are very far from each other.
    pub force_jump_veneers: bool,
}

pub fn builder(triple: Option<Triple>) -> Result<Box<dyn CompilerBuilder>> {
    Ok(Box::new(Builder {
        tunables: None,
        inner: IsaBuilder::new(triple, |triple| isa::lookup(triple).map_err(|e| e.into()))?,
        linkopts: LinkOptions::default(),
        cache_store: None,
        clif_dir: None,
        wmemcheck: false,
    }))
}

impl CompilerBuilder for Builder {
    fn triple(&self) -> &target_lexicon::Triple {
        self.inner.triple()
    }

    fn clif_dir(&mut self, path: &path::Path) -> Result<()> {
        self.clif_dir = Some(path.to_path_buf());
        Ok(())
    }

    fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {
        self.inner.target(target)?;
        Ok(())
    }

    fn set(&mut self, name: &str, value: &str) -> Result<()> {
        // Special wasmtime-cranelift-only settings first
        if name == "wasmtime_linkopt_padding_between_functions" {
            self.linkopts.padding_between_functions = value.parse()?;
            return Ok(());
        }
        if name == "wasmtime_linkopt_force_jump_veneer" {
            self.linkopts.force_jump_veneers = value.parse()?;
            return Ok(());
        }

        self.inner.set(name, value)
    }

    fn enable(&mut self, name: &str) -> Result<()> {
        self.inner.enable(name)
    }

    fn set_tunables(&mut self, tunables: Tunables) -> Result<()> {
        self.tunables = Some(tunables);
        Ok(())
    }

    fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {
        let isa = self.inner.build()?;
        Ok(Box::new(crate::compiler::Compiler::new(
            self.tunables
                .as_ref()
                .expect("set_tunables not called")
                .clone(),
            isa,
            self.cache_store.clone(),
            self.linkopts.clone(),
            self.clif_dir.clone(),
            self.wmemcheck,
        )))
    }

    fn settings(&self) -> Vec<Setting> {
        self.inner.settings()
    }

    fn enable_incremental_compilation(
        &mut self,
        cache_store: Arc<dyn wasmtime_environ::CacheStore>,
    ) -> Result<()> {
        self.cache_store = Some(cache_store);
        Ok(())
    }

    fn wmemcheck(&mut self, enable: bool) {
        self.wmemcheck = enable;
    }
}

impl fmt::Debug for Builder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Builder")
            .field("shared_flags", &self.inner.shared_flags().to_string())
            .finish()
    }
}