binaryen-rs icon indicating copy to clipboard operation
binaryen-rs copied to clipboard

Optimizations are executed, even if chosen level is `0`

Open cmichi opened this issue 4 years ago • 0 comments

I discovered this when executing tests for a project which uses binaryen-rs as a dependency. The project sets the optimization level to 0 during some tests.

From wasm-opt --help:

   -O0                                          execute no optimization passes 

The wasm-opt behavior:

$ wasm-opt -O0 --shrink-level=0 -o output.wasm flipper.wasm
warning: no passes specified, not doing any work

But binaryen-rs still optimizes if the optimization level and the shrink level is both set to 0:

#[test]
fn must_not_optimize_for_level_zero() {
    let input: Vec<u8> = vec![
        0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
        0x03, 0x02, 0x01, 0x00, 0x07, 0x08, 0x01, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00,
        0x08, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
    ];

    let codegen_config = binaryen::CodegenConfig {
        optimization_level: 0,
        shrink_level: 0,
        debug_info: true,
    };
    let mut module = binaryen::Module::read(&input).unwrap();
    module.optimize(&codegen_config);

    // no optimization should have happened
    assert_eq!(module.write(), input);
}

This test fails for master.

I only quickly searched in the code and my hunch is that this could be a reason: https://github.com/pepyakin/binaryen-rs/blob/f6091c0e4e5ad414bda02c922a53735ec4e51082/binaryen-sys/Shim.cpp#L74

cmichi avatar Mar 23 '21 13:03 cmichi