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:
Alex Crichton
2021-09-08 10:40:58 -05:00
committed by GitHub
parent 164835ecf5
commit 8ebaaf928d
8 changed files with 15 additions and 182 deletions

View File

@@ -71,16 +71,6 @@ $ wasmtime config new
And that'll print out the path to the file you can edit. 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` ## `compile`
This subcommand is used to Ahead-Of-Time (AOT) compile a WebAssembly module to produce This subcommand is used to Ahead-Of-Time (AOT) compile a WebAssembly module to produce

View File

@@ -6,7 +6,7 @@
use anyhow::Result; use anyhow::Result;
use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt}; use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt};
use wasmtime_cli::commands::{ use wasmtime_cli::commands::{
CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WasmToObjCommand, WastCommand, CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WastCommand,
}; };
/// Wasmtime WebAssembly Runtime /// Wasmtime WebAssembly Runtime
@@ -44,9 +44,6 @@ enum WasmtimeApp {
Run(RunCommand), Run(RunCommand),
/// Displays available Cranelift settings for a target. /// Displays available Cranelift settings for a target.
Settings(SettingsCommand), Settings(SettingsCommand),
/// Translates a WebAssembly module to native object file
#[structopt(name = "wasm2obj")]
WasmToObj(WasmToObjCommand),
/// Runs a WebAssembly test script file /// Runs a WebAssembly test script file
Wast(WastCommand), Wast(WastCommand),
} }
@@ -59,7 +56,6 @@ impl WasmtimeApp {
Self::Compile(c) => c.execute(), Self::Compile(c) => c.execute(),
Self::Run(c) => c.execute(), Self::Run(c) => c.execute(),
Self::Settings(c) => c.execute(), Self::Settings(c) => c.execute(),
Self::WasmToObj(c) => c.execute(),
Self::Wast(c) => c.execute(), Self::Wast(c) => c.execute(),
} }
} }

View File

@@ -4,7 +4,6 @@ mod compile;
mod config; mod config;
mod run; mod run;
mod settings; mod settings;
mod wasm2obj;
mod wast; mod wast;
pub use self::{compile::*, config::*, run::*, settings::*, wasm2obj::*, wast::*}; pub use self::{compile::*, config::*, run::*, settings::*, wast::*};

View File

@@ -22,8 +22,9 @@ use wasmtime_wasi_crypto::WasiCryptoCtx;
fn parse_module(s: &OsStr) -> Result<PathBuf, OsString> { fn parse_module(s: &OsStr) -> Result<PathBuf, OsString> {
// Do not accept wasmtime subcommand names as the module name // Do not accept wasmtime subcommand names as the module name
match s.to_str() { match s.to_str() {
Some("help") | Some("config") | Some("run") | Some("wasm2obj") | Some("wast") Some("help") | Some("config") | Some("run") | Some("wast") | Some("compile") => {
| Some("compile") => Err("module name cannot be the same as a subcommand".into()), Err("module name cannot be the same as a subcommand".into())
}
_ => Ok(s.into()), _ => Ok(s.into()),
} }
} }

View File

@@ -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(())
}
}

View File

@@ -94,17 +94,13 @@ lazy_static::lazy_static! {
} }
pub mod commands; pub mod commands;
mod obj;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use target_lexicon::Triple;
use wasmtime::{Config, ProfilingStrategy, Strategy}; use wasmtime::{Config, ProfilingStrategy, Strategy};
pub use obj::compile_to_obj;
fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result<Strategy> { fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result<Strategy> {
Ok(match (lightbeam, cranelift) { Ok(match (lightbeam, cranelift) {
(true, false) => Strategy::Lightbeam, (true, false) => Strategy::Lightbeam,
@@ -528,11 +524,6 @@ fn parse_cranelift_flag(name_and_value: &str) -> Result<(String, String)> {
Ok((name, value)) 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)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@@ -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()?)
}

View File

@@ -3,25 +3,24 @@ use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use target_lexicon::Triple; use target_lexicon::Triple;
use wasmtime::Strategy; use wasmtime::{Config, Engine, Module};
use wasmtime_cli::compile_to_obj;
pub fn compile_cranelift( pub fn compile_cranelift(
wasm: &[u8], wasm: &[u8],
target: Option<Triple>, target: Option<Triple>,
output: impl AsRef<Path>, output: impl AsRef<Path>,
) -> Result<()> { ) -> Result<()> {
let obj = compile_to_obj( let mut config = Config::new();
wasm, config.debug_info(true);
target.as_ref(), if let Some(target) = target {
Strategy::Cranelift, config.target(&target.to_string())?;
false, }
wasmtime::OptLevel::None, let engine = Engine::new(&config)?;
true, let module = Module::new(&engine, wasm)?;
)?; let bytes = module.serialize()?;
let mut file = File::create(output).context("failed to create object file")?; 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")?; .context("failed to write object file")?;
Ok(()) Ok(())