Remove the wasmtime wasm2obj command (#3301)
* Remove the `wasmtime wasm2obj` command This commit removes the `wasm2obj` subcommand of the `wasmtime` CLI. This subcommand has a very long history and dates back quite far. While it's existed, however, it's never been documented in terms of the output it's produced. AFAIK it's only ever been used for debugging to see the machine code output of Wasmtime on some modules. With recent changes to the module serialization output the output of `wasmtime compile`, the `*.cwasm` file, is now a native ELF file which can be fed to standard tools like `objdump`. Consequently I dont think there's any remaining need to keep `wasm2obj` around itself, so this commit removes the subcommand. * More code to delete * Try to fix debuginfo tests
This commit is contained in:
@@ -71,16 +71,6 @@ $ wasmtime config new
|
||||
|
||||
And that'll print out the path to the file you can edit.
|
||||
|
||||
## `wasm2obj`
|
||||
|
||||
This is an experimental subcommand to compile a WebAssembly module to native
|
||||
code. Work for this is still heavily under development, but you can execute this
|
||||
with:
|
||||
|
||||
```sh
|
||||
$ wasmtime wasm2obj foo.wasm foo.o
|
||||
```
|
||||
|
||||
## `compile`
|
||||
|
||||
This subcommand is used to Ahead-Of-Time (AOT) compile a WebAssembly module to produce
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
use anyhow::Result;
|
||||
use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt};
|
||||
use wasmtime_cli::commands::{
|
||||
CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WasmToObjCommand, WastCommand,
|
||||
CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WastCommand,
|
||||
};
|
||||
|
||||
/// Wasmtime WebAssembly Runtime
|
||||
@@ -44,9 +44,6 @@ enum WasmtimeApp {
|
||||
Run(RunCommand),
|
||||
/// Displays available Cranelift settings for a target.
|
||||
Settings(SettingsCommand),
|
||||
/// Translates a WebAssembly module to native object file
|
||||
#[structopt(name = "wasm2obj")]
|
||||
WasmToObj(WasmToObjCommand),
|
||||
/// Runs a WebAssembly test script file
|
||||
Wast(WastCommand),
|
||||
}
|
||||
@@ -59,7 +56,6 @@ impl WasmtimeApp {
|
||||
Self::Compile(c) => c.execute(),
|
||||
Self::Run(c) => c.execute(),
|
||||
Self::Settings(c) => c.execute(),
|
||||
Self::WasmToObj(c) => c.execute(),
|
||||
Self::Wast(c) => c.execute(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ mod compile;
|
||||
mod config;
|
||||
mod run;
|
||||
mod settings;
|
||||
mod wasm2obj;
|
||||
mod wast;
|
||||
|
||||
pub use self::{compile::*, config::*, run::*, settings::*, wasm2obj::*, wast::*};
|
||||
pub use self::{compile::*, config::*, run::*, settings::*, wast::*};
|
||||
|
||||
@@ -22,8 +22,9 @@ use wasmtime_wasi_crypto::WasiCryptoCtx;
|
||||
fn parse_module(s: &OsStr) -> Result<PathBuf, OsString> {
|
||||
// Do not accept wasmtime subcommand names as the module name
|
||||
match s.to_str() {
|
||||
Some("help") | Some("config") | Some("run") | Some("wasm2obj") | Some("wast")
|
||||
| Some("compile") => Err("module name cannot be the same as a subcommand".into()),
|
||||
Some("help") | Some("config") | Some("run") | Some("wast") | Some("compile") => {
|
||||
Err("module name cannot be the same as a subcommand".into())
|
||||
}
|
||||
_ => Ok(s.into()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
//! The module that implements the `wasmtime wasm2obj` command.
|
||||
|
||||
use crate::obj::compile_to_obj;
|
||||
use crate::{parse_target, pick_compilation_strategy, CommonOptions};
|
||||
use anyhow::{Context as _, Result};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use structopt::{clap::AppSettings, StructOpt};
|
||||
use target_lexicon::Triple;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref AFTER_HELP: String = {
|
||||
format!(
|
||||
"The translation is dependent on the environment chosen.\n\
|
||||
The default is a dummy environment that produces placeholder values.\n\
|
||||
\n\
|
||||
{}",
|
||||
crate::FLAG_EXPLANATIONS.as_str()
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
/// Translates a WebAssembly module to native object file
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(
|
||||
name = "wasm2obj",
|
||||
version = env!("CARGO_PKG_VERSION"),
|
||||
setting = AppSettings::ColoredHelp,
|
||||
after_help = AFTER_HELP.as_str(),
|
||||
)]
|
||||
pub struct WasmToObjCommand {
|
||||
#[structopt(flatten)]
|
||||
common: CommonOptions,
|
||||
|
||||
/// The path of the WebAssembly module to translate
|
||||
#[structopt(index = 1, value_name = "MODULE_PATH", parse(from_os_str))]
|
||||
module: PathBuf,
|
||||
|
||||
/// The path of the output object file
|
||||
#[structopt(index = 2, value_name = "OUTPUT_PATH")]
|
||||
output: String,
|
||||
|
||||
/// The target triple; default is the host triple
|
||||
#[structopt(long, value_name = "TARGET", parse(try_from_str = parse_target))]
|
||||
target: Option<Triple>,
|
||||
}
|
||||
|
||||
impl WasmToObjCommand {
|
||||
/// Executes the command.
|
||||
pub fn execute(self) -> Result<()> {
|
||||
self.common.init_logging();
|
||||
|
||||
let strategy = pick_compilation_strategy(self.common.cranelift, self.common.lightbeam)?;
|
||||
|
||||
let data = wat::parse_file(&self.module).context("failed to parse module")?;
|
||||
|
||||
let obj = compile_to_obj(
|
||||
&data,
|
||||
self.target.as_ref(),
|
||||
strategy,
|
||||
self.common.enable_simd,
|
||||
self.common.opt_level(),
|
||||
self.common.debug_info,
|
||||
)?;
|
||||
|
||||
let mut file =
|
||||
File::create(Path::new(&self.output)).context("failed to create object file")?;
|
||||
file.write_all(&obj)
|
||||
.context("failed to write object file")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -94,17 +94,13 @@ lazy_static::lazy_static! {
|
||||
}
|
||||
|
||||
pub mod commands;
|
||||
mod obj;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
use target_lexicon::Triple;
|
||||
use wasmtime::{Config, ProfilingStrategy, Strategy};
|
||||
|
||||
pub use obj::compile_to_obj;
|
||||
|
||||
fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result<Strategy> {
|
||||
Ok(match (lightbeam, cranelift) {
|
||||
(true, false) => Strategy::Lightbeam,
|
||||
@@ -528,11 +524,6 @@ fn parse_cranelift_flag(name_and_value: &str) -> Result<(String, String)> {
|
||||
Ok((name, value))
|
||||
}
|
||||
|
||||
fn parse_target(s: &str) -> Result<Triple> {
|
||||
use std::str::FromStr;
|
||||
Triple::from_str(&s).map_err(|e| anyhow::anyhow!(e))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
67
src/obj.rs
67
src/obj.rs
@@ -1,67 +0,0 @@
|
||||
use anyhow::{bail, Context as _, Result};
|
||||
use std::mem;
|
||||
use target_lexicon::Triple;
|
||||
use wasmparser::WasmFeatures;
|
||||
use wasmtime::Strategy;
|
||||
use wasmtime_environ::{ModuleEnvironment, PrimaryMap, Tunables};
|
||||
|
||||
/// Creates object file from binary wasm data.
|
||||
pub fn compile_to_obj(
|
||||
wasm: &[u8],
|
||||
target: Option<&Triple>,
|
||||
strategy: Strategy,
|
||||
enable_simd: bool,
|
||||
opt_level: wasmtime::OptLevel,
|
||||
debug_info: bool,
|
||||
) -> Result<Vec<u8>> {
|
||||
match strategy {
|
||||
Strategy::Cranelift | Strategy::Auto => {}
|
||||
other => panic!("unsupported strategy {:?}", other),
|
||||
}
|
||||
let mut builder = wasmtime_cranelift::builder();
|
||||
if let Some(target) = target {
|
||||
builder.target(target.clone())?;
|
||||
}
|
||||
let mut features = WasmFeatures::default();
|
||||
|
||||
if enable_simd {
|
||||
builder.enable("enable_simd").unwrap();
|
||||
features.simd = true;
|
||||
}
|
||||
|
||||
match opt_level {
|
||||
wasmtime::OptLevel::None => {}
|
||||
wasmtime::OptLevel::Speed => {
|
||||
builder.set("opt_level", "speed").unwrap();
|
||||
}
|
||||
wasmtime::OptLevel::SpeedAndSize => {
|
||||
builder.set("opt_level", "speed_and_size").unwrap();
|
||||
}
|
||||
other => bail!("unknown optimization level {:?}", other),
|
||||
}
|
||||
|
||||
// TODO: Expose the tunables as command-line flags.
|
||||
let mut tunables = Tunables::default();
|
||||
tunables.generate_native_debuginfo = debug_info;
|
||||
tunables.parse_wasm_debuginfo = debug_info;
|
||||
|
||||
let compiler = builder.build();
|
||||
let environ = ModuleEnvironment::new(&tunables, &features);
|
||||
let (_main_module, mut translation, types) = environ
|
||||
.translate(wasm)
|
||||
.context("failed to translate module")?;
|
||||
assert_eq!(translation.len(), 1);
|
||||
let mut funcs = PrimaryMap::default();
|
||||
for (index, func) in mem::take(&mut translation[0].function_body_inputs) {
|
||||
funcs.push(compiler.compile_function(&translation[0], index, func, &tunables, &types)?);
|
||||
}
|
||||
let mut obj = compiler.object()?;
|
||||
compiler.emit_obj(
|
||||
&translation[0],
|
||||
&types,
|
||||
funcs,
|
||||
tunables.generate_native_debuginfo,
|
||||
&mut obj,
|
||||
)?;
|
||||
Ok(obj.write()?)
|
||||
}
|
||||
@@ -3,25 +3,24 @@ use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use target_lexicon::Triple;
|
||||
use wasmtime::Strategy;
|
||||
use wasmtime_cli::compile_to_obj;
|
||||
use wasmtime::{Config, Engine, Module};
|
||||
|
||||
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,
|
||||
)?;
|
||||
let mut config = Config::new();
|
||||
config.debug_info(true);
|
||||
if let Some(target) = target {
|
||||
config.target(&target.to_string())?;
|
||||
}
|
||||
let engine = Engine::new(&config)?;
|
||||
let module = Module::new(&engine, wasm)?;
|
||||
let bytes = module.serialize()?;
|
||||
|
||||
let mut file = File::create(output).context("failed to create object file")?;
|
||||
file.write_all(&obj)
|
||||
file.write_all(&bytes)
|
||||
.context("failed to write object file")?;
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user