Stream: general

Topic: Build .wasm without dead code elimination?


view this post on Zulip Alex Vidal (Nov 18 2020 at 01:58):

I'm trying to ensure I've exhaustively covered all possible imports in a wasm program (the idea being that I build the program and use wasm-nm to list all imports) in a wrapper (using wasmtime-go) that I'm building. Does anyone know how I can disable dead code elimination or some other mechanism to get these imports? Such as querying a crate to find all extern "C" decls, or functions with a wasm link attr?

view this post on Zulip Alex Vidal (Nov 18 2020 at 02:00):

I suppose the list of imports doesn't change often, so it's not really a big deal to hand-write the list, but now I feel nerd baited

view this post on Zulip bjorn3 (Nov 19 2020 at 11:40):

@Alex Vidal -Clink-dead-code=yes should do.

view this post on Zulip Alex Vidal (Nov 19 2020 at 16:38):

Doesn't seem to, but I haven't tried a minimal repro yet.

view this post on Zulip Alex Vidal (Nov 19 2020 at 18:16):

Ok. Just did a minimal repro that defines three wasm imports. Compiling with rustc --target=wasm32-wasi -g ./src/main.rs -C link-dead-code=yes then running the result through wasm-nm, the result is that there's no import defined for functions that are not used in my main function

view this post on Zulip Alex Vidal (Nov 19 2020 at 18:24):

// Compile with:
// rustc --target=wasm32-unknown-unknown -g main.rs -C link-dead-code=yes
// Check with:
// wasm-nm -i main.wasm

#[link(wasm_import_module="math")]
extern "C" {
    #[link_name = "add"]
    pub fn add(a: i32, b: i32) -> i32;

    #[link_name = "mul"]
    pub fn mul(a: i32, b: i32) -> i32;
}

#[no_mangle]
extern "C" {
    pub fn top();
}

fn main() {
    println!("Hello, world!");

    // Comment this out and there's no `top` import in the wasm binary
    unsafe { top() };

    // Comment this out and there's no `add` import in the wasm binary
    call_add();
}

fn call_add() {
    unsafe { add(1, 2) };
}

// mul is never in the wasm binary because `call_mul` is never called
fn call_mul() {
    unsafe { mul(1, 2) };
}

view this post on Zulip Alex Vidal (Nov 19 2020 at 18:27):

note, i also tried with -C opt-level=0 -C lto=no

view this post on Zulip bjorn3 (Nov 20 2020 at 20:58):

Executables only export the main function by default. The linker removes all functions that are not directly or indirectly used through an export. Didn't notice which topic this is.

view this post on Zulip bjorn3 (Nov 20 2020 at 20:59):

(deleted)

view this post on Zulip bjorn3 (Nov 20 2020 at 21:00):

I don't really know what will fix it.


Last updated: Nov 22 2024 at 16:03 UTC