Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm-unknown: add math and memory builtins that LLVM needs #4176

Merged
merged 2 commits into from
Mar 26, 2024

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Mar 4, 2024

WIP, not working yet due to existing bugs. But this is the general idea.

Copy link

github-actions bot commented Mar 4, 2024

Size difference with the dev branch:

Binary size difference
not the same command!
    tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650
    go: downloading tinygo.org/x/tinyfont v0.3.0
not the same command!
    tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/
    go: downloading golang.org/x/net v0.7.0
not the same command!
    tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/
    go: downloading github.com/soypat/natiu-mqtt v0.5.1
not the same command!
    tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/
    go: downloading github.com/eclipse/paho.mqtt.golang v1.2.0
 flash                          ram
 before   after   diff          before   after   diff
  16744   16744      0   0.00%    4308    4308      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650
  60912   60912      0   0.00%    6176    6176      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
   9652    9652      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
  13476   13476      0   0.00%    6780    6780      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx
   8600    8600      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
  11584   11584      0   0.00%    6556    6556      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/apds9960/proximity/main.go
   9660    9660      0   0.00%    4752    4752      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
   8180    8180      0   0.00%    2304    2304      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/at24cx/main.go
   8220    8220      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bh1750/main.go
   7508    7508      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/blinkm/main.go
  70396   70396      0   0.00%    3692    3692      0   0.00% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go
  63332   63332      0   0.00%    6176    6176      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
  27660   27660      0   0.00%    4772    4772      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp180/main.go
  63440   63440      0   0.00%    6208    6208      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
  12212   12212      0   0.00%    4804    4804      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bmp388/main.go
   8128    8128      0   0.00%    3332    3332      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/sram/main.go
  22088   22088      0   0.00%    3528    3528      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/time/main.go
  69220   69220      0   0.00%    6348    6348      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/main.go
   4704    4704      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/easystepper/main.go
  68796   68796      0   0.00%    6956    6956      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/flash/console/spi
  64828   64828      0   0.00%    8996    8996      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/flash/console/qspi
   7040    7040      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/gc9a01/main.go
  67812   67812      0   0.00%    6352    6352      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/i2c/main.go
  68240   68240      0   0.00%    6488    6488      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go
   8260    8260      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go
   5612    5612      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go
   5656    5656      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go
  10440   10440      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/hd44780i2c/main.go
  14496   14496      0   0.00%    6556    6556      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/hts221/main.go
  16868   16868      0   0.00%    2344    2344      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hub75/main.go
  10056   10056      0   0.00%    6900    6900      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic
  10696   10696      0   0.00%    4852    4852      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic
  29404   29404      0   0.00%   38060   38060      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing
  10080   10080      0   0.00%    6908    6908      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll
  10784   10784      0   0.00%    4860    4860      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll
 263248  263248      0   0.00%   46736   46736      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/slideshow
  11784   11784      0   0.00%    4772    4772      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis3dh/main.go
  13840   13840      0   0.00%    6556    6556      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/lps22hb/main.go
  26076   26076      0   0.00%    2312    2312      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/lsm303agr/main.go
  12360   12360      0   0.00%    4780    4780      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/lsm6ds3/main.go
  10800   10800      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mag3110/main.go
  10004   10004      0   0.00%    4764    4764      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017/main.go
  10424   10424      0   0.00%    4772    4772      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017-multiple/main.go
   9692    9692      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go
  66928   66928      0   0.00%    6176    6176      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp2515/main.go
  26704   26704      0   0.00%    3056    3056      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go
  26608   26608      0   0.00%    5100    5100      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit-v2 ./examples/microbitmatrix/main.go
   8320    8320      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mma8653/main.go
   8232    8232      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mpu6050/main.go
  75108   75108      0   0.00%    7460    7460      0   0.00% tinygo build -size short -o ./build/test.hex -target=p1am-100 ./examples/p1am/main.go
  12124   12124      0   0.00%    3336    3336      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/pca9685/main.go
   6068    6068      0   0.00%    3272    3272      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setbuffer/main.go
   5104    5104      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go
  10392   10392      0   0.00%    3312    3312      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/seesaw
   2697    2697      0   0.00%     556     556      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino ./examples/servo
  13652   13652      0   0.00%    3384    3384      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico     ./examples/sgp30
   7940    7940      0   0.00%    6780    6780      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go
  56576   56576      0   0.00%    3668    3668      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
  56632   56632      0   0.00%    3676    3676      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go
  56548   56548      0   0.00%    3668    3668      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
   6460    6460      0   0.00%    2272    2272      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go
   5996    5996      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go
   5676    5676      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go
   6612    6612      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go
   6512    6512      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go
  17044   17044      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/thermistor/main.go
  10292   10292      0   0.00%    4516    4516      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-bluefruit ./examples/tone
   9972    9972      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/tm1637/main.go
   9396    9396      0   0.00%    6772    6772      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/fourwire/main.go
  12468   12468      0   0.00%    6984    6984      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/pyportal_touchpaint/main.go
  15708   15708      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl53l1x/main.go
  13736   13736      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl6180x/main.go
   6380    6380      0   0.00%    2304    2304      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13/main.go
   6004    6004      0   0.00%    2296    2296      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13x/main.go
   6260    6260      0   0.00%    2304    2304      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd4in2/main.go
   6800    6800      0   0.00%    4772    4772      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/ws2812
   5506    5506      0   0.00%    9502    9502      0   0.00% tinygo build -size short -o ./build/test.bin -target=m5stamp-c3          ./examples/ws2812
  61824   61824      0   0.00%    5928    5928      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/is31fl3731/main.go
   1709    1709      0   0.00%     596     596      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino   ./examples/ws2812
   1040    1040      0   0.00%     178     178      0   0.00% tinygo build -size short -o ./build/test.hex -target=digispark ./examples/ws2812
  32072   32072      0   0.00%    4772    4772      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bme280/main.go
  16500   16500      0   0.00%    4716    4716      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/microphone/main.go
  11128   11128      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/buzzer/main.go
  12784   12784      0   0.00%    4772    4772      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/veml6070/main.go
   6676    6676      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/simple/main.go
   8596    8596      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l293x/speed/main.go
   6640    6640      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/simple/main.go
   9212    9212      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/l9110x/speed/main.go
   7288    7288      0   0.00%    3304    3304      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-f103rb ./examples/shiftregister/main.go
   6916    6916      0   0.00%    2252    2252      0   0.00% tinygo build -size short -o ./build/test.hex -target=hifive1b ./examples/ssd1351/main.go
  13060   13060      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis2mdl/main.go
   8304    8304      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/max72xx/main.go
  76664   76664      0   0.00%    6316    6316      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/dht/main.go
  36356   36356      0   0.00%    3972    3972      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8523/
  70752   70752      0   0.00%    6324    6324      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/alarm/
   7276    7276      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/clkout/
  70272   70272      0   0.00%    6328    6328      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/time/
  70664   70664      0   0.00%    6324    6324      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/timer/
  12052   12052      0   0.00%    3288    3288      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/qmi8658c/main.go
   8840    8840      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/ina260/main.go
   9272    9272      0   0.00%    5236    5236      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-l432kc ./examples/aht20/main.go
  72136   72136      0   0.00%   10732   10732      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/sdcard/console/
  60756   60756      0   0.00%    8216    8216      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m4 ./examples/i2csoft/adt7410/
  10144   10144      0   0.00%    6780    6780      0   0.00% tinygo build -size short -o ./build/test.elf -target=wioterminal ./examples/axp192/m5stack-core2-blinky/
   8892    8892      0   0.00%    3260    3260      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/xpt2046/main.go
  14532   14532      0   0.00%    4916    4916      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/sx126x/lora_rxtx/
  29948   29948      0   0.00%    4020    4020      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/ssd1289/main.go
  11156   11156      0   0.00%    4236    4236      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/irremote/main.go
  11180   11180      0   0.00%    3300    3300      0   0.00% tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/uc8151/main.go
  10284   10284      0   0.00%    3340    3340      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/scd4x/main.go
   8568    8568      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go
   9468    9468      0   0.00%    4748    4748      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ds18b20/main.go
  81916   81916      0   0.00%    6588    6588      0   0.00% tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/lora/lorawan/atcmd/
  15708   15708      0   0.00%    4860    4860      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/as560x/main.go
   9776    9776      0   0.00%    3280    3280      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu6886/main.go
   7716    7716      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ttp229/main.go
  66096   66096      0   0.00%    4808    4808      0   0.00% tinygo build -size short -o ./build/test.hex -target=pico ./examples/ndir/main_ndir.go
  61440   61440      0   0.00%    3776    3776      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ndir/main_ndir.go
  64568   64568      0   0.00%    6248    6248      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/ndir/main_ndir.go
   9192    9192      0   0.00%    3272    3272      0   0.00% tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/mpu9150/main.go
  12236   12236      0   0.00%    3308    3308      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/sh1106/macropad_spi
   8384    8384      0   0.00%    3744    3744      0   0.00% tinygo build -size short -o ./build/test.hex -target=macropad-rp2040 ./examples/encoders/quadrature-interrupt
  88152   88152      0   0.00%    5820    5820      0   0.00% tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/
 233460  233460      0   0.00%    9968    9968      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal -stack-size 8kb ./examples/net/http-get/
 116028  116028      0   0.00%    7480    7480      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 -stack-size 8kb ./examples/net/tcpclient/
 237908  237908      0   0.00%    8912    8912      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/websocket/dial/
 100560  100560      0   0.00%    9472    9472      0   0.00% tinygo build -size short -o ./build/test.hex -target=metro-m4-airlift -stack-size 8kb ./examples/net/socket/
 321348  321348      0   0.00%   15268   15268      0   0.00% tinygo build -size short -o ./build/test.hex -target=matrixportal-m4 -stack-size 8kb ./examples/net/webstatic/
 108264  108264      0   0.00%    7244    7244      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-mkrwifi1010 -stack-size 8kb ./examples/net/tlsclient/
 151456  151456      0   0.00%    6176    6176      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-rp2040 -stack-size 8kb ./examples/net/mqttclient/natiu/
 112764  112764      0   0.00%   12596   12596      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webclient/
 277244  277244      0   0.00%   17736   17736      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/webserver/
 278108  278108      0   0.00%   16628   16628      0   0.00% tinygo build -size short -o ./build/test.hex -target=wioterminal -stack-size 8kb ./examples/net/mqttclient/paho/
5038752 5038752      0   0.00%  752652  752652      0   0.00%

@aykevl
Copy link
Member Author

aykevl commented Mar 4, 2024

For those who would like to test: run tinygo clean, then tinygo build to cache the files (it'll result in a linker error), then run llvm-ranlib ~/.cache/tinygo/compiler-rt-wasm32-unknown-wasi-generic/lib.a and llvm-ranlib ~/.cache/tinygo/wasmbuiltins-wasm32-unknown-wasi-generic/lib.a to fix the libraries, and then try tinygo build again. (The paths may be different on your system).

The main thing I need to fix is to parse the "linker" section and use that instead of the import section while generating the archives in TinyGo (builder/ar.go).

builder/wasmbuiltins.go Outdated Show resolved Hide resolved
aykevl added a commit that referenced this pull request Mar 12, 2024
The symbol table was generated incorrectly. The correct way is to use
the custom linking WebAssembly section, which I implemented in go-wasm
for this purpose.

This fixes #4114 and is a
prerequisite for #4176.
@aykevl
Copy link
Member Author

aykevl commented Mar 12, 2024

Does anybody have a good testcase for this? CC @dgryski @deadprogram
I believe this PR together with #4188 should add the needed math functions, but I don't know how to verify this to be sure.

aykevl added a commit that referenced this pull request Mar 13, 2024
The symbol table was generated incorrectly. The correct way is to use
the custom linking WebAssembly section, which I implemented in go-wasm
for this purpose.

This fixes #4114 and is a
prerequisite for #4176.
@aykevl
Copy link
Member Author

aykevl commented Mar 13, 2024

Found an easy way to test this: simply by patching testdata/wasi.json to use wasmbuiltins instead of wasi-libc and then running tinygo clean and tinygo run -target=wasi ./testdata/math.go.
Still, it would be useful to have a confirmation that it indeed works correctly with -target=wasm-unknown (CC @ydnar).

@ydnar
Copy link
Contributor

ydnar commented Mar 13, 2024

To run it under wasm-unknown, make the following changes to wasm-unknown.json:

  • Remove "-mno-bulk-memory" from cflags
  • Remove "--import-memory" from ldflags
  • Change emulator to wasmtime --dir={tmpDir}::/tmp --invoke main {}

Then add //export main above func main() in testdata/math.go.

It should compile and run, with no output.

@aykevl
Copy link
Member Author

aykevl commented Mar 14, 2024

It should compile and run, with no output.

Yeah, that's the problem. I can't verify it actually works correctly.

@aykevl aykevl changed the title wasm-unknown: add math builtins that LLVM needs wasm-unknown: add math and memory builtins that LLVM needs Mar 14, 2024
@aykevl
Copy link
Member Author

aykevl commented Mar 14, 2024

I've now also added memory builtins: memset, memcpy, and memmove. They are generated by LLVM when bulk memory operations aren't used (as is currently the case for wasm-unknown).

@ydnar
Copy link
Contributor

ydnar commented Mar 14, 2024

It should compile and run, with no output.

Yeah, that's the problem. I can't verify it actually works correctly.

testdata/math.go needs only one import from the host: wasi_snapshot_preview1 fd_write, used by putchar. I think it’s probably fine to add these lines to the top of runtime_tinygowasm_unknown.go:

// Implements __wasi_iovec_t.
type __wasi_iovec_t struct {
	buf    unsafe.Pointer
	bufLen uint
}

//go:wasmimport wasi_snapshot_preview1 fd_write
func fd_write(id uint32, iovs *__wasi_iovec_t, iovs_len uint, nwritten *uint) (errno uint)

const (
	putcharBufferSize = 120
	stdout            = 1
)

// Using global variables to avoid heap allocation.
var (
	putcharBuffer        = [putcharBufferSize]byte{}
	putcharPosition uint = 0
	putcharIOVec         = __wasi_iovec_t{
		buf: unsafe.Pointer(&putcharBuffer[0]),
	}
	putcharNWritten uint
)

func putchar(c byte) {
	putcharBuffer[putcharPosition] = c
	putcharPosition++

	if c == '\n' || putcharPosition >= putcharBufferSize {
		putcharIOVec.bufLen = putcharPosition
		fd_write(stdout, &putcharIOVec, 1, &putcharNWritten)
		putcharPosition = 0
	}
}

Then tests can run comparing the output to the golden file.

@aykevl aykevl marked this pull request as ready for review March 16, 2024 13:30
@aykevl
Copy link
Member Author

aykevl commented Mar 16, 2024

@ydnar not sure about that, it seems to me that many people want to avoid all WASI calls, including stdout. (See Slack channel discussion).

I finally tested it by modifying the WASI target a little. It's now ready for review.

@ydnar
Copy link
Contributor

ydnar commented Mar 16, 2024

LGTM, can't test yet. Thanks for making this!

deadprogram pushed a commit that referenced this pull request Mar 18, 2024
The symbol table was generated incorrectly. The correct way is to use
the custom linking WebAssembly section, which I implemented in go-wasm
for this purpose.

This fixes #4114 and is a
prerequisite for #4176.
deadprogram pushed a commit that referenced this pull request Mar 19, 2024
The symbol table was generated incorrectly. The correct way is to use
the custom linking WebAssembly section, which I implemented in go-wasm
for this purpose.

This fixes #4114 and is a
prerequisite for #4176.
@deadprogram
Copy link
Member

@aykevl can you please rebase this PR against the latest dev?

@deadprogram
Copy link
Member

@orsinium any opinion on this PR?

@deadprogram
Copy link
Member

Tested with a few modules from Mechanoid examples. Looks like this PR reduced the size of the modules.

Before:

$ ls -l modules/*.wasm                     
-rwxrwxr-x 1 ron ron 1026 mar 15 18:59 modules/hithere.wasm
-rwxrwxr-x 1 ron ron  670 mar 18 22:44 modules/mynameis.wasm
-rwxrwxr-x 1 ron ron  654 mar 15 18:59 modules/mythingis.wasm
-rwxrwxr-x 1 ron ron  596 mar 15 18:59 modules/thisbadge.wasm
-rwxrwxr-x 1 ron ron  522 mar 18 22:44 modules/youarehere.wasm

After:

$ ls -l modules/*.wasm
-rwxrwxr-x 1 ron ron 922 mar 21 21:34 modules/hithere.wasm
-rwxrwxr-x 1 ron ron 560 mar 21 21:34 modules/mynameis.wasm
-rwxrwxr-x 1 ron ron 544 mar 21 21:34 modules/mythingis.wasm
-rwxrwxr-x 1 ron ron 486 mar 21 21:34 modules/thisbadge.wasm
-rwxrwxr-x 1 ron ron 412 mar 21 21:34 modules/youarehere.wasm

Tested and there was no change in the behavior of the built modules on my hardware device.

Before:

$ wasm-objdump modules/hithere.wasm -x

hithere.wasm:   file format wasm 0x1

Section Details:

Type[3]:
 - type[0] () -> nil
 - type[1] (i32, i32) -> nil
 - type[2] (i32) -> i32
Import[3]:
 - func[0] sig=1 <main.badge_set_text3> <- badge.set_text3
 - func[1] sig=1 <main.badge_set_text1> <- badge.set_text1
 - memory[0] pages: initial=1 max=1 <- env.memory
Function[5]:
 - func[2] sig=0 <__wasm_init_memory>
 - func[3] sig=0 <_initialize>
 - func[4] sig=2 <runtime.alloc>
 - func[5] sig=0 <start>
 - func[6] sig=0 <update>
Export[3]:
 - func[3] <_initialize> -> "_initialize"
 - func[5] <start> -> "start"
 - func[6] <update> -> "update"
Start:
 - start function: 2
DataCount:
 - data count: 2
Code[5]:
 - func[2] size=13 <__wasm_init_memory>
 - func[3] size=24 <_initialize>
 - func[4] size=128 <runtime.alloc>
 - func[5] size=45 <start>
 - func[6] size=243 <update>
Data[2]:
 - segment[0] memory=0 size=27 - init i32=4096
  - 0001000: 3048 656c 6c6f 2c20 5765 6241 7373 656d  0Hello, WebAssem
  - 0001010: 626c 7921 436f 756e 743a 20              bly!Count: 
 - segment[1] memory=0 size=4 - init i32=4124
  - 000101c: 8010 0000                                ....
Custom:
 - name: "name"
 - func[0] <main.badge_set_text3>
 - func[1] <main.badge_set_text1>
 - func[2] <__wasm_init_memory>
 - func[3] <_initialize>
 - func[4] <runtime.alloc>
 - func[5] <start>
 - func[6] <update>
Custom:
 - name: "producers"
Custom:
 - name: "target_features"

After:

$ wasm-objdump modules/hithere.wasm -x
                                                                                      
hithere.wasm:   file format wasm 0x1                                                                     
                                                                                      
Section Details:                         
                                                                                      
Type[3]:                                 
 - type[0] () -> nil                                                                  
 - type[1] (i32, i32) -> nil                                                          
 - type[2] (i32) -> i32  
Import[3]:                               
 - func[0] sig=1 <main.badge_set_text3> <- badge.set_text3                                                             
 - func[1] sig=1 <main.badge_set_text1> <- badge.set_text1                                                             
 - memory[0] pages: initial=1 max=1 <- env.memory                                                                      
Function[5]:                                                                          
 - func[2] sig=0 <__wasm_init_memory>    
 - func[3] sig=0 <_initialize>                                                        
 - func[4] sig=2 <runtime.alloc>                                                      
 - func[5] sig=0 <start>                                                              
 - func[6] sig=0 <update>                                                             
Export[3]:                                                                            
 - func[3] <_initialize> -> "_initialize"                                             
 - func[5] <start> -> "start"                                                         
 - func[6] <update> -> "update"
Start:         
 - start function: 2                                                                                     
DataCount:                                                                                               
 - data count: 2                                                                                         
Code[5]:                                                                                                 
 - func[2] size=13 <__wasm_init_memory>                                                                  
 - func[3] size=24 <_initialize>                    
 - func[4] size=128 <runtime.alloc>                 
 - func[5] size=45 <start>                          
 - func[6] size=243 <update>                        
Data[2]:                                            
 - segment[0] memory=0 size=27 - init i32=4096                                                                         
  - 0001000: 3048 656c 6c6f 2c20 5765 6241 7373 656d  0Hello, WebAssem                                                 
  - 0001010: 626c 7921 436f 756e 743a 20              bly!Count:                                                       
 - segment[1] memory=0 size=4 - init i32=4124                                                                          
  - 000101c: 8010 0000                                ....                                                             
Custom:                                             
 - name: "name"                                            
 - func[0] <main.badge_set_text3>                          
 - func[1] <main.badge_set_text1>                          
 - func[2] <__wasm_init_memory>                            
 - func[3] <_initialize>                                   
 - func[4] <runtime.alloc>                                 
 - func[5] <start>                                         
 - func[6] <update>                                        
Custom:                                                    
 - name: "producers"                                       
Custom:                                                    
 - name: "target_features"

aykevl and others added 2 commits March 26, 2024 10:39
This new library is needed for wasm targets that aren't WASI and don't
need/want a libc, but still need some intrinsics that are generated by
LLVM.
@deadprogram
Copy link
Member

Now merging, thanks @aykevl for working on this and to @ydnar and @b0ch3nski for feedback and review.

@deadprogram deadprogram merged commit a5ceb79 into dev Mar 26, 2024
25 checks passed
@deadprogram deadprogram deleted the wasm-nolibc branch March 26, 2024 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants