Files
wasmtime/tests/debug/obj.rs
Alex Crichton d4fcd32cdc Optimize generated code via the CLI by default (#973)
* Optimize generated code via the CLI by default

This commit updates the behavior of the CLI and adds a new flag. It
first enables the `--optimize` flag by default, ensuring that usage of
the `wasmtime` CLI will enable cranelift optimizations by default. Next
it also adds a `--opt-level` flag which is similar to Rust's
`-Copt-level` where it takes a string argument of how to optimize. This
is updates to support 0/1/2/s, where 1 is currently the same as 2 but
added for consistency with other compilers. The default setting is
`--opt-level=2`.

When the `-O` flag is not passed the `--opt-level` flag is used,
otherwise `-O` takes precedent in the sense that it implies
`--opt-level=2` which is the highest optimization level. The thinking is
that these flags will in general select the highest optimization level
specified as the final optimization level.

* Add inline docs

* fix a test
2020-02-24 15:18:08 -06:00

35 lines
825 B
Rust

use anyhow::{Context as _, Result};
use std::fs::File;
use std::path::Path;
use target_lexicon::Triple;
use wasmtime::Strategy;
use wasmtime_cli::compile_to_obj;
use wasmtime_environ::CacheConfig;
pub fn compile_cranelift(
wasm: &[u8],
target: Option<Triple>,
output: impl AsRef<Path>,
) -> Result<()> {
let obj = compile_to_obj(
wasm,
target.as_ref(),
Strategy::Cranelift,
false,
wasmtime::OptLevel::None,
true,
output
.as_ref()
.file_name()
.unwrap()
.to_string_lossy()
.to_string(),
&CacheConfig::new_cache_disabled(),
)?;
let file = File::create(output).context("failed to create object file")?;
obj.write(file).context("failed to write object file")?;
Ok(())
}