Path: blob/main/crates/cranelift/src/builder.rs
3069 views
//! Implementation of a "compiler builder" for cranelift1//!2//! This module contains the implementation of how Cranelift is configured, as3//! well as providing a function to return the default configuration to build.45use crate::isa_builder::IsaBuilder;6use cranelift_codegen::{7CodegenResult,8isa::{self, OwnedTargetIsa},9};10use std::fmt;11use std::path;12use std::sync::Arc;13use target_lexicon::Triple;14use wasmtime_environ::error::Result;15use wasmtime_environ::{CacheStore, CompilerBuilder, Setting, Tunables};1617struct Builder {18tunables: Option<Tunables>,19inner: IsaBuilder<CodegenResult<OwnedTargetIsa>>,20emit_debug_checks: bool,21linkopts: LinkOptions,22cache_store: Option<Arc<dyn CacheStore>>,23clif_dir: Option<path::PathBuf>,24wmemcheck: bool,25}2627#[derive(Clone, Default)]28pub struct LinkOptions {29/// A debug-only setting used to synthetically insert 0-byte padding between30/// compiled functions to simulate huge compiled artifacts and exercise31/// logic related to jump veneers.32pub padding_between_functions: usize,3334/// A debug-only setting used to force inter-function calls in a wasm module35/// to always go through "jump veneers" which are typically only generated36/// when functions are very far from each other.37pub force_jump_veneers: bool,38}3940pub fn builder(triple: Option<Triple>) -> Result<Box<dyn CompilerBuilder>> {41let mut builder = Builder {42tunables: None,43inner: IsaBuilder::new(triple, |triple| isa::lookup(triple).map_err(|e| e.into()))?,44linkopts: LinkOptions::default(),45cache_store: None,46clif_dir: None,47wmemcheck: false,48emit_debug_checks: false,49};5051builder.set("enable_verifier", "false").unwrap();52builder.set("opt_level", "speed").unwrap();5354// When running under MIRI try to optimize for compile time of Wasm code55// itself as much as possible. Disable optimizations by default and use the56// fastest regalloc available to us.57if cfg!(miri) {58builder.set("opt_level", "none").unwrap();59builder.set("regalloc_algorithm", "single_pass").unwrap();60}6162Ok(Box::new(builder))63}6465impl CompilerBuilder for Builder {66fn triple(&self) -> &target_lexicon::Triple {67self.inner.triple()68}6970fn clif_dir(&mut self, path: &path::Path) -> Result<()> {71self.clif_dir = Some(path.to_path_buf());72Ok(())73}7475fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {76self.inner.target(target)?;77Ok(())78}7980fn set(&mut self, name: &str, value: &str) -> Result<()> {81// Special wasmtime-cranelift-only settings first82match name {83"wasmtime_linkopt_padding_between_functions" => {84self.linkopts.padding_between_functions = value.parse()?;85}86"wasmtime_linkopt_force_jump_veneer" => {87self.linkopts.force_jump_veneers = value.parse()?;88}89"wasmtime_inlining_intra_module" => {90self.tunables.as_mut().unwrap().inlining_intra_module = value.parse()?;91}92"wasmtime_inlining_small_callee_size" => {93self.tunables.as_mut().unwrap().inlining_small_callee_size = value.parse()?;94}95"wasmtime_inlining_sum_size_threshold" => {96self.tunables.as_mut().unwrap().inlining_sum_size_threshold = value.parse()?;97}98"wasmtime_debug_checks" => {99self.emit_debug_checks = true;100}101_ => {102self.inner.set(name, value)?;103}104}105Ok(())106}107108fn enable(&mut self, name: &str) -> Result<()> {109self.inner.enable(name)110}111112fn set_tunables(&mut self, tunables: Tunables) -> Result<()> {113self.tunables = Some(tunables);114Ok(())115}116117fn tunables(&self) -> Option<&Tunables> {118self.tunables.as_ref()119}120121fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {122let isa = self.inner.build()?;123Ok(Box::new(crate::compiler::Compiler::new(124self.tunables125.as_ref()126.expect("set_tunables not called")127.clone(),128isa,129self.cache_store.clone(),130self.emit_debug_checks,131self.linkopts.clone(),132self.clif_dir.clone(),133self.wmemcheck,134)))135}136137fn settings(&self) -> Vec<Setting> {138self.inner.settings()139}140141fn enable_incremental_compilation(142&mut self,143cache_store: Arc<dyn wasmtime_environ::CacheStore>,144) -> Result<()> {145self.cache_store = Some(cache_store);146Ok(())147}148149fn wmemcheck(&mut self, enable: bool) {150self.wmemcheck = enable;151}152}153154impl fmt::Debug for Builder {155fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {156f.debug_struct("Builder")157.field("shared_flags", &self.inner.shared_flags().to_string())158.finish()159}160}161162163