Skip to content

Invalid local.set generation after store in to_wasm_bytes. #14

@SiebenCorgie

Description

@SiebenCorgie

Hi, I'm currently trying to generate WASM for a function-call with pass-by-reference arguments.
However, somehow the generated WASM does not match the Waffle-IR.

My function looks like this in pseudo Rust (the source language is a custom DSL):

fn my_function(a: &[f32; 3], result: &mut f32){
   //something interesting that writes to `result`
}

//the function that is compiled:
fn myfield(a: [f32; 3]) -> f32{
   let mut value;
   my_function(&a, &mut value);
   value - 1.0
} 

The following waffle print-out for the compiled function:

 function(f32, f32, f32) -> f32 {
   block0(v0: f32, v1: f32, v2: f32): #
     # preds:
     # succs:
     v3 = f32const<1065353216>  #
     v4 = i32const<1114112>  #
     v5 = f32store<memory0, align=0, offset=0> v4, v0 # i32, f32
     v6 = f32store<memory0, align=0, offset=4> v4, v1 # i32, f32
     v7 = f32store<memory0, align=0, offset=8> v4, v2 # i32, f32
     v8 = i32const<1114124>  #
     v9 = call<func156> v4, v8 # i32, i32
     v10 = f32load<memory0, align=0, offset=0> v8 # i32
     v11 = f32sub v10, v3 # f32, f32
     return v11
 }

However, after calling to_wasm_bytes I get the following invalid function via wasm-tools print:

(;@20451 ;)  (func $myfield (;334;) (type 5) (param f32 f32 f32) (result f32)
(;@20452 ;)    (local f32 f32 i32 i32)
(;@2045a ;)    i32.const 1114112
(;@2045f ;)    local.get 0
(;@20461 ;)    f32.store align=1
(;@20464 ;)    local.set 6
(;@20466 ;)    local.set 4
(;@20468 ;)    i32.const 1114112
(;@2046d ;)    local.get 1
(;@2046f ;)    f32.store offset=4 align=1
(;@20472 ;)    local.set 6
(;@20474 ;)    local.set 4
(;@20476 ;)    i32.const 1114112
(;@2047b ;)    local.get 2
(;@2047d ;)    f32.store offset=8 align=1
(;@20480 ;)    local.set 6
(;@20482 ;)    local.set 4
(;@20484 ;)    i32.const 1114112
(;@20489 ;)    i32.const 1114124
(;@2048e ;)    call $my_function
(;@20491 ;)    local.set 5
(;@20493 ;)    local.set 6
(;@20495 ;)    i32.const 1114124
(;@2049a ;)    f32.load align=1
(;@2049d ;)    f32.const 0x1p+0 (;=1;)
(;@204a2 ;)    f32.sub
(;@204a3 ;)    local.set 3
(;@204a5 ;)    local.set 4
(;@204a7 ;)    local.get 3
(;@204a9 ;)    return
             )

According to wasm-tools validate at 0x20464 the local.set6 is invalid, which is indeed true, since the first f32.store at 20461 consumed the static address, and the f32-value, and there is nothing on the stack anymore.

I'm aware that the mistake is probably on my site in the way I'm using waffle, but I don't see anything wrong with the waffle-IR snipped. As far as I'm aware f32.store does not push a value onto the stack, so there shouldn't be any stack-handling after that. Even if it would do so, the following local.set 4 would be wrong too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions