tweag / asterius

DEPRECATED in favor of ghc wasm backend, see https://www.tweag.io/blog/2022-11-22-wasm-backend-merged-in-ghc
1.97k stars 58 forks source link

"RuntimeError: unreachable" #722

Open lemmih opened 4 years ago

lemmih commented 4 years ago

Describe the bug Running this code fails with 'RuntimeError: unreachable':

{-# LANGUAGE PackageImports #-}
module Main where

import Graphics.SvgTree
import Graphics.SvgTree.Printer
import "lens" Control.Lens

main :: IO ()
main = putStrLn $ ppTree (CircleTree $ Circle mempty (Num 0, Num 0) (Num 2))
root@ac07adc0d6f9:/workspace# ahc-link --input-hs test.hs --run
[INFO] Compiling test.hs to WebAssembly
[INFO] Converting linked IR to binaryen IR
[INFO] Running binaryen optimization
[INFO] Validating binaryen IR
[INFO] Writing WebAssembly binary to "./test.wasm"
[INFO] Writing JavaScript runtime modules to "."
[INFO] Writing JavaScript loader module to "./test.wasm.mjs"
[INFO] Writing JavaScript req module to "./test.req.mjs"
[INFO] Writing JavaScript entry module to "./test.mjs"
[INFO] Running ./test.mjs
test: JSException "RuntimeError: unreachable\n    at <anonymous>:wasm-function[5314]:0x1320cb\n    at <anonymous>:wasm-function[12289]:0x2db0f9\n    at scheduleTSO (<anonymous>:wasm-function[12290]:0x2db126)\n    at Scheduler.tick (file:///workspace/rts.scheduler.mjs:346:22)\n    at Immediate.<anonymous> (file:///workspace/rts.scheduler.mjs:381:29)\n    at processImmediate (internal/timers.js:458:21)"

Running the same code but with a circle radius of '0' works fine:

main = putStrLn $ ppTree (CircleTree $ Circle mempty (Num 0, Num 0) (Num 0))
root@ac07adc0d6f9:/workspace# ahc-link --input-hs test.hs --run
[INFO] Compiling test.hs to WebAssembly
[INFO] Converting linked IR to binaryen IR
[INFO] Running binaryen optimization
[INFO] Validating binaryen IR
[INFO] Writing WebAssembly binary to "./test.wasm"
[INFO] Writing JavaScript runtime modules to "."
[INFO] Writing JavaScript loader module to "./test.wasm.mjs"
[INFO] Writing JavaScript req module to "./test.req.mjs"
[INFO] Writing JavaScript entry module to "./test.mjs"
[INFO] Running ./test.mjs
<circle />

Oh, running with --verbose-err gives this (reformatted):

test: JSException "RuntimeError: barf: _hs_ToFixedLength\n
    at ExceptionHelper.barf (file:///workspace/rts.exception.mjs:147:13)\n
    at barf (<anonymous>:wasm-function[817]:0x3f822)\n
    at doublezmconversionzm2zi0zi2zi0zmL7SF9hmoOH71AQFMRAfBSp_DataziDoubleziConversionziText_toFixedzulen_entry (<anonymous>:wasm-function[5319]:0x13237f)\n
    at scheduleTSO (<anonymous>:wasm-function[12294]:0x2db6b4)\n
    at scheduleTSO_wrapper (<anonymous>:wasm-function[12295]:0x2db6e1)\n
    at Scheduler.tick (file:///workspace/rts.scheduler.mjs:346:22)\n
    at Immediate.<anonymous> (file:///workspace/rts.scheduler.mjs:381:29)\n
    at processImmediate (internal/timers.js:458:21)"

To Reproduce Put the above code in test.hs and run it with: ahc-link --input-hs test.hs --run. It depends on 'reanimate-svg' which is already included in the docker image.

Expected behavior I expected it to print <circle r="2"/>.

Environment

lemmih commented 4 years ago

Ah, it must be because I'm using double-conversion which uses cbits. Guess you can close this issue.

lemmih commented 4 years ago

Can I manually compile the C code with emscripten and make it available somehow?

TerrorJack commented 4 years ago

compile the C code with emscripten and make it available somehow?

That code can only be used via foreign import javascript, which means one needs to patch the ccall logic for now and handle the emscripten packaging logic themselves. This is the best we can do for now, but we've about to start working on adding a C toolchain to handle C stuff, so the status quo will be improved; can't give an exact ETA yet though.

lemmih commented 4 years ago

Alright. Can I detect when my package is being compiled by asterius? Something like if impl(asterius) {} in the cabal file?

lemmih commented 4 years ago

725 solves my issue but no new docker image has been built. Are new docker images not built automatically when new patches are pushed?

TerrorJack commented 4 years ago

terrorjack/asterius:latest has now been updated to terrorjack/asterius:200805. The docker images are built with a different CI infra, and takes a lot of time to build.

lemmih commented 4 years ago

My library now works with Asterius. :) But it requires a lot of memory to compile, a little more than 6GB. Do you have any tips or tricks for reducing the memory requirements?