Skip to content

Commit

Permalink
Test basic DWARF generation (#931)
Browse files Browse the repository at this point in the history
* Add obj generation with debug info
* Add simple transform check
  • Loading branch information
yurydelendik authored Feb 20, 2020
1 parent 4460e56 commit b96b53e
Show file tree
Hide file tree
Showing 17 changed files with 414 additions and 129 deletions.
3 changes: 3 additions & 0 deletions .github/actions/define-dwarfdump-env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# define-dwarfdump-env

Defines `DWARFDUMP` path executable.
6 changes: 6 additions & 0 deletions .github/actions/define-dwarfdump-env/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: 'Set up a DWARFDUMP env'
description: 'Set up a DWARFDUMP env (see tests/debug/dump.rs)'

runs:
using: node12
main: 'main.js'
11 changes: 11 additions & 0 deletions .github/actions/define-dwarfdump-env/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

// On OSX pointing to brew's LLVM location.
if (process.platform == 'darwin') {
console.log("::set-env name=DWARFDUMP::/usr/local/opt/llvm/bin/llvm-dwarfdump");
}

// On Linux pointing to specific version
if (process.platform == 'linux') {
console.log("::set-env name=DWARFDUMP::/usr/bin/llvm-dwarfdump-9");
}
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ jobs:
- uses: ./.github/actions/install-rust
with:
toolchain: ${{ matrix.rust }}
- uses: ./.github/actions/define-dwarfdump-env

- name: Install libclang
# Note: libclang is pre-installed on the macOS and linux images.
Expand Down Expand Up @@ -327,7 +328,7 @@ jobs:
- run: $CENTOS cargo build --release --manifest-path crates/c-api/Cargo.toml
shell: bash
# Test what we just built
- run: $CENTOS cargo test --features test_programs --release --all --exclude lightbeam --exclude wasmtime --exclude wasmtime-c-api --exclude wasmtime-fuzzing
- run: $CENTOS cargo test --features test_programs --release --all --exclude lightbeam --exclude wasmtime --exclude wasmtime-c-api --exclude wasmtime-fuzzing -- --skip test_debug_dwarf_
shell: bash
env:
RUST_BACKTRACE: 1
Expand Down
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ more-asserts = "0.2.1"
# `cargo test --features test-programs`.
test-programs = { path = "crates/test-programs" }
tempfile = "3.1.0"
filecheck = "0.4.0"

[build-dependencies]
anyhow = "1.0.19"
Expand Down
6 changes: 5 additions & 1 deletion crates/debug/src/transform/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ fn generate_vars(
) {
let vmctx_label = get_vmctx_value_label();

for label in frame_info.value_ranges.keys() {
// Normalize order of ValueLabelsRanges keys to have reproducable results.
let mut vars = frame_info.value_ranges.keys().collect::<Vec<_>>();
vars.sort_by(|a, b| a.index().cmp(&b.index()));

for label in vars {
if label.index() == vmctx_label.index() {
append_vmctx_info(
unit,
Expand Down
140 changes: 13 additions & 127 deletions src/commands/wasm2obj.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
//! The module that implements the `wasmtime wasm2obj` command.
use crate::obj::compile_to_obj;
use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions};
use anyhow::{anyhow, bail, Context as _, Result};
use faerie::Artifact;
use anyhow::{anyhow, Context as _, Result};
use std::{
fs::File,
path::{Path, PathBuf},
str::FromStr,
};
use structopt::{clap::AppSettings, StructOpt};
use target_lexicon::Triple;
use wasmtime::Strategy;
use wasmtime_debug::{emit_debugsections, read_debuginfo};
use wasmtime_environ::CacheConfig;
#[cfg(feature = "lightbeam")]
use wasmtime_environ::Lightbeam;
use wasmtime_environ::{
entity::EntityRef, settings, settings::Configurable, wasm::DefinedMemoryIndex,
wasm::MemoryIndex, CacheConfig, Compiler, Cranelift, ModuleEnvironment, ModuleMemoryOffset,
ModuleVmctxInfo, Tunables, VMOffsets,
};
use wasmtime_jit::native;
use wasmtime_obj::emit_module;

/// The after help text for the `wasm2obj` command.
pub const WASM2OBJ_AFTER_HELP: &str = "The translation is dependent on the environment chosen.\n\
Expand Down Expand Up @@ -78,122 +70,16 @@ impl WasmToObjCommand {

let data = wat::parse_file(&self.module).context("failed to parse module")?;

let isa_builder = match self.target.as_ref() {
Some(target) => native::lookup(target.clone())?,
None => native::builder(),
};
let mut flag_builder = settings::builder();

// There are two possible traps for division, and this way
// we get the proper one if code traps.
flag_builder.enable("avoid_div_traps").unwrap();

if self.common.enable_simd {
flag_builder.enable("enable_simd").unwrap();
}

if self.common.optimize {
flag_builder.set("opt_level", "speed").unwrap();
}

let isa = isa_builder.finish(settings::Flags::new(flag_builder));

let mut obj = Artifact::new(isa.triple().clone(), self.output.clone());

// TODO: Expose the tunables as command-line flags.
let tunables = Tunables::default();

let (
module,
module_translation,
lazy_function_body_inputs,
lazy_data_initializers,
target_config,
) = {
let environ = ModuleEnvironment::new(isa.frontend_config(), tunables);

let translation = environ
.translate(&data)
.context("failed to translate module")?;

(
translation.module,
translation.module_translation.unwrap(),
translation.function_body_inputs,
translation.data_initializers,
translation.target_config,
)
};

// TODO: use the traps information
let (compilation, relocations, address_transform, value_ranges, stack_slots, _traps) =
match strategy {
Strategy::Auto | Strategy::Cranelift => Cranelift::compile_module(
&module,
&module_translation,
lazy_function_body_inputs,
&*isa,
self.common.debug_info,
&cache_config,
),
#[cfg(feature = "lightbeam")]
Strategy::Lightbeam => Lightbeam::compile_module(
&module,
&module_translation,
lazy_function_body_inputs,
&*isa,
self.common.debug_info,
&cache_config,
),
#[cfg(not(feature = "lightbeam"))]
Strategy::Lightbeam => bail!("lightbeam support not enabled"),
other => bail!("unsupported compilation strategy {:?}", other),
}
.context("failed to compile module")?;

if compilation.is_empty() {
bail!("no functions were found/compiled");
}

let module_vmctx_info = {
let ofs = VMOffsets::new(target_config.pointer_bytes(), &module);
ModuleVmctxInfo {
memory_offset: if ofs.num_imported_memories > 0 {
ModuleMemoryOffset::Imported(ofs.vmctx_vmmemory_import(MemoryIndex::new(0)))
} else if ofs.num_defined_memories > 0 {
ModuleMemoryOffset::Defined(
ofs.vmctx_vmmemory_definition_base(DefinedMemoryIndex::new(0)),
)
} else {
ModuleMemoryOffset::None
},
stack_slots,
}
};

emit_module(
&mut obj,
&module,
&compilation,
&relocations,
&lazy_data_initializers,
&target_config,
)
.map_err(|e| anyhow!(e))
.context("failed to emit module")?;

if self.common.debug_info {
let debug_data = read_debuginfo(&data);
emit_debugsections(
&mut obj,
&module_vmctx_info,
target_config,
&debug_data,
&address_transform,
&value_ranges,
)
.context("failed to emit debug sections")?;
}
let obj = compile_to_obj(
&data,
self.target.as_ref(),
strategy,
self.common.enable_simd,
self.common.optimize,
self.common.debug_info,
self.output.clone(),
&cache_config,
)?;

// FIXME: Make the format a parameter.
let file = File::create(Path::new(&self.output)).context("failed to create object file")?;
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
)]

pub mod commands;
mod obj;

use anyhow::{bail, Result};
use std::path::PathBuf;
use structopt::StructOpt;
use wasmtime::{Config, 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,
Expand Down
Loading

0 comments on commit b96b53e

Please sign in to comment.