tshort / WebAssemblyCompiler.jl

Create WebAssembly with Julia
https://tshort.github.io/WebAssemblyCompiler.jl/
MIT License
67 stars 4 forks source link

Issue with code that calls log #18

Open samakins opened 6 days ago

samakins commented 6 days ago

Hey Tshort,

I think I'm using this as intended but seemingly have an issue with argument count every time i call log(x::Float64).

I see its overwritten in quirks.jl and I've tried playing around with overlay myself but with no luck. Wondering if this was a bug or something dumb on my part.

Here is a very basic example where the first compile attempt has no problem but the second one doesn't work.

I tested this on a linux machine julia 1.10.4 as well as a couple versions of 1.8.*

using WebAssemblyCompiler

function expensivecalc1(a::Float64, b::Float64)
    a + b
end

function expensivecalc2(a::Float64, b::Float64)
    log(a + b)
end

  compile((expensivecalc1, Float64, Float64);
filepath = "bin/expensivecalc1.wasm",
 validate = false,
  optimize = false)

  compile((expensivecalc2, Float64, Float64);
  filepath = "bin/expensivecalc2.wasm",
   validate = false,
    optimize = false)

here is a corresponding manifest

[[deps.WebAssemblyCompiler]]
deps = ["Binaryen_jll", "CEnum", "CodeInfoTools", "CompTime", "GPUCompiler", "NodeCall", "Reexport", "Unrolled", "libjlnode_jll", "libnode_jll"]
git-tree-sha1 = "b9166ab12916a96d60bb65e4827edcee240c9844"
uuid = "9bc180e8-272c-4d08-a460-9682f1716cbe"
version = "0.1.15"

The error output looks something like this

ERROR: KeyError: key 3 not found
Stacktrace:
  [1] getindex
    @ ./dict.jl:498 [inlined]
  [2] _compile(ctx::WebAssemblyCompiler.CompilerContext, x::Core.SSAValue; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ WebAssemblyCompiler ~/.julia/packages/WebAssemblyCompiler/MqjfR/src/_compile.jl:21
  [3] _compile(ctx::WebAssemblyCompiler.CompilerContext, x::Core.SSAValue)
    @ WebAssemblyCompiler ~/.julia/packages/WebAssemblyCompiler/MqjfR/src/_compile.jl:16
  [4] compile_block(ctx::WebAssemblyCompiler.CompilerContext, cfg::Core.Compiler.CFG, phis::Dict{Int64, Any}, idx::Int64)
    @ WebAssemblyCompiler ~/.julia/packages/WebAssemblyCompiler/MqjfR/src/compile_block.jl:86
  [5] (::WebAssemblyCompiler.var"#283#284"{WebAssemblyCompiler.CompilerContext, Dict{Int64, Any}, Ptr{WebAssemblyCompiler.LibBinaryen.Relooper}, Core.Compiler.CFG})(idx::Int64)
    @ WebAssemblyCompiler ./none:0
  [6] iterate(::Base.Generator{Base.OneTo{Int64}, WebAssemblyCompiler.var"#283#284"{WebAssemblyCompiler.CompilerContext, Dict{Int64, Any}, Ptr{WebAssemblyCompiler.LibBinaryen.Relooper}, Core.Compiler.CFG}})
    @ Base ./generator.jl:47
  [7] collect(itr::Base.Generator{Base.OneTo{Int64}, WebAssemblyCompiler.var"#283#284"{WebAssemblyCompiler.CompilerContext, Dict{Int64, Any}, Ptr{WebAssemblyCompiler.LibBinaryen.Relooper}, Core.Compiler.CFG}})
    @ Base ./array.jl:787
  [8] compile_method_body(ctx::WebAssemblyCompiler.CompilerContext)
    @ WebAssemblyCompiler ~/.julia/packages/WebAssemblyCompiler/MqjfR/src/compiler.jl:145
  [9] compile_method(ctx::WebAssemblyCompiler.CompilerContext, funname::String; sig::Type, exported::Bool)
    @ WebAssemblyCompiler ~/.julia/packages/WebAssemblyCompiler/MqjfR/src/compiler.jl:101
 [10] compile(funs::Tuple{typeof(expensivecalc2), DataType, DataType}; filepath::String, jspath::String, validate::Bool, optimize::Bool, experimental::Bool, names::Nothing)
    @ WebAssemblyCompiler ~/.julia/packages/WebAssemblyCompiler/MqjfR/src/compiler.jl:58
 [11] top-level scope
    @ ~/Projects/Test/WasmTest.jl/expensivecalc.jl:19

Appreciate any thoughts on this, very cool Pkg otherwise!

tshort commented 5 days ago

Hmm. expensivecalc2 works locally for me. I wonder what's different about our systems. Here's the WebAssembly text output for that module. It looks reasonable.

(module
 (type $0 (func (param f64) (result f64)))
 (type $1 (func (param f64 f64) (result f64)))
 (import "js" "(x) => Math.log(x)" (func $_x_____Math_log_x_Float64Float64 (type $0) (param f64) (result f64)))
 (export "expensivecalc2" (func $expensivecalc2))
 (func $expensivecalc2 (type $1) (param $0 f64) (param $1 f64) (result f64)
  (local $2 f64)
  (local $3 f64)
  (block
   (local.set $2
    (f64.add
     (local.get $0)
     (local.get $1)
    )
   )
   (local.set $3
    (call $_x_____Math_log_x_Float64Float64
     (local.get $2)
    )
   )
   (return
    (local.get $3)
   )
  )
 )
)
tshort commented 5 days ago

I used Julia v1.9.3.