Path: blob/main/crates/cranelift/src/builder.rs
1691 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 anyhow::Result;7use cranelift_codegen::{8CodegenResult,9isa::{self, OwnedTargetIsa},10};11use std::fmt;12use std::path;13use std::sync::Arc;14use target_lexicon::Triple;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>> {41Ok(Box::new(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}))50}5152impl CompilerBuilder for Builder {53fn triple(&self) -> &target_lexicon::Triple {54self.inner.triple()55}5657fn clif_dir(&mut self, path: &path::Path) -> Result<()> {58self.clif_dir = Some(path.to_path_buf());59Ok(())60}6162fn target(&mut self, target: target_lexicon::Triple) -> Result<()> {63self.inner.target(target)?;64Ok(())65}6667fn set(&mut self, name: &str, value: &str) -> Result<()> {68// Special wasmtime-cranelift-only settings first69match name {70"wasmtime_linkopt_padding_between_functions" => {71self.linkopts.padding_between_functions = value.parse()?;72}73"wasmtime_linkopt_force_jump_veneer" => {74self.linkopts.force_jump_veneers = value.parse()?;75}76"wasmtime_inlining_intra_module" => {77self.tunables.as_mut().unwrap().inlining_intra_module = value.parse()?;78}79"wasmtime_inlining_small_callee_size" => {80self.tunables.as_mut().unwrap().inlining_small_callee_size = value.parse()?;81}82"wasmtime_inlining_sum_size_threshold" => {83self.tunables.as_mut().unwrap().inlining_sum_size_threshold = value.parse()?;84}85"wasmtime_debug_checks" => {86self.emit_debug_checks = true;87}88_ => {89self.inner.set(name, value)?;90}91}92Ok(())93}9495fn enable(&mut self, name: &str) -> Result<()> {96self.inner.enable(name)97}9899fn set_tunables(&mut self, tunables: Tunables) -> Result<()> {100self.tunables = Some(tunables);101Ok(())102}103104fn build(&self) -> Result<Box<dyn wasmtime_environ::Compiler>> {105let isa = self.inner.build()?;106Ok(Box::new(crate::compiler::Compiler::new(107self.tunables108.as_ref()109.expect("set_tunables not called")110.clone(),111isa,112self.cache_store.clone(),113self.emit_debug_checks,114self.linkopts.clone(),115self.clif_dir.clone(),116self.wmemcheck,117)))118}119120fn settings(&self) -> Vec<Setting> {121self.inner.settings()122}123124fn enable_incremental_compilation(125&mut self,126cache_store: Arc<dyn wasmtime_environ::CacheStore>,127) -> Result<()> {128self.cache_store = Some(cache_store);129Ok(())130}131132fn wmemcheck(&mut self, enable: bool) {133self.wmemcheck = enable;134}135}136137impl fmt::Debug for Builder {138fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {139f.debug_struct("Builder")140.field("shared_flags", &self.inner.shared_flags().to_string())141.finish()142}143}144145146