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

compiler, runtime: implement recoverable divide-by-zero panic #4758

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Feb 25, 2025

This gets the math/bits package tests to pass.

Unfortunately, this also has a binary size impact of around 150-200 bytes in many cases. I'm a bit on the edge on whether this is worth it, since it's mostly used for getting package tests to work. But at the same time, having working package tests is very valuable.

Copy link

github-actions bot commented Feb 25, 2025

Size difference with the dev branch:

Binary size difference
 flash                          ram
 before   after   diff          before   after   diff
  70692   70588   -104  -0.15%    3656    3664      8   0.22% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go
   2841    2841      0   0.00%     558     558      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino ./examples/servo
   1581    1581      0   0.00%     598     598      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino   ./examples/ws2812
   1056    1056      0   0.00%     180     180      0   0.00% tinygo build -size short -o ./build/test.hex -target=digispark ./examples/ws2812
  16656   16808    152   0.91%    4180    4188      8   0.19% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/adafruit4650
  10528   10680    152   1.44%    3336    3344      8   0.24% tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/seesaw
  26044   26200    156   0.60%   16420   16428      8   0.05% tinygo build -size short -o ./build/test.hex -target=pico ./examples/waveshare-epd/epd2in66b/main.go
  11848   12008    160   1.35%    6580    6588      8   0.12% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/apds9960/proximity/main.go
  14748   14908    160   1.08%    6580    6588      8   0.12% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/hts221/main.go
  14112   14272    160   1.13%    6580    6588      8   0.12% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/lps22hb/main.go
   9940   10100    160   1.61%    4780    4788      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017/main.go
  10380   10540    160   1.54%    4788    4796      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017-multiple/main.go
  27152   27312    160   0.59%    3640    3648      8   0.22% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go
  27016   27176    160   0.59%    5620    5628      8   0.14% tinygo build -size short -o ./build/test.hex -target=microbit-v2 ./examples/microbitmatrix/main.go
  75472   75632    160   0.21%    7448    7456      8   0.11% tinygo build -size short -o ./build/test.hex -target=p1am-100 ./examples/p1am/main.go
  10712   10872    160   1.49%    4540    4548      8   0.18% tinygo build -size short -o ./build/test.hex -target=circuitplay-bluefruit ./examples/tone
  24708   24872    164   0.66%   13720   13728      8   0.06% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840-sense ./examples/waveshare-epd/epd1in54/main.go
  63988   64156    168   0.26%    6196    6204      8   0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
  64048   64216    168   0.26%    6228    6236      8   0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
   8364    8532    168   2.01%    3352    3360      8   0.24% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/sram/main.go
  57480   57648    168   0.29%    3688    3696      8   0.22% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
  57444   57612    168   0.29%    3688    3696      8   0.22% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
  62744   62912    168   0.27%    5948    5956      8   0.13% tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/is31fl3731/main.go
  61472   61648    176   0.29%    6188    6196      8   0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
  69720   69896    176   0.25%    6376    6384      8   0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/main.go
  67668   67844    176   0.26%    6360    6368      8   0.13% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/i2c/main.go
  68220   68396    176   0.26%    6504    6512      8   0.12% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go
  57448   57624    176   0.31%    3696    3704      8   0.22% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go
  22300   22500    200   0.90%    3556    3572     16   0.45% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/time/main.go
  69980   70180    200   0.29%    6980    6996     16   0.23% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/flash/console/spi
  10276   10484    208   2.02%    6924    6932      8   0.12% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic
  10300   10508    208   2.02%    6916    6924      8   0.12% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll
 263492  263700    208   0.08%   46752   46768     16   0.03% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/slideshow
   8212    8424    212   2.58%    6788    6796      8   0.12% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go
   6704    6916    212   3.16%    2288    2296      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go
   8824    9040    216   2.45%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
   9920   10136    216   2.18%    4760    4768      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
   8164    8380    216   2.65%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bh1750/main.go
   7468    7684    216   2.89%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/blinkm/main.go
  66724   66940    216   0.32%    9020    9036     16   0.18% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/flash/console/qspi
   8012    8228    216   2.70%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go
  10568   10784    216   2.04%    4748    4756      8   0.17% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/hd44780i2c/main.go
  10872   11088    216   1.99%    4868    4876      8   0.16% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll
   9924   10140    216   2.18%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go
  10208   10424    216   2.12%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/tm1637/main.go
  13852   14068    216   1.56%    4748    4756      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl6180x/main.go
   6988    7204    216   3.09%    4780    4788      8   0.17% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/ws2812
   9604    9824    220   2.29%    4748    4756      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
  10784   11004    220   2.04%    4876    4884      8   0.16% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic
  10760   10980    220   2.04%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mag3110/main.go
   8276    8496    220   2.66%    4748    4756      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mma8653/main.go
   8184    8404    220   2.69%    4740    4748      8   0.17% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mpu6050/main.go
   6236    6464    228   3.66%    3288    3296      8   0.24% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setbuffer/main.go
   5264    5492    228   4.33%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go
   6488    6716    228   3.51%    2320    2328      8   0.34% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13/main.go
   6400    6628    228   3.56%    2320    2328      8   0.34% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd4in2/main.go
   4628    4860    232   5.01%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/easystepper/main.go
   7232    7464    232   3.21%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/gc9a01/main.go
   5840    6072    232   3.97%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go
   5792    6024    232   4.01%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go
  11772   12004    232   1.97%    4780    4788      8   0.17% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis3dh/main.go
   6152    6384    232   3.77%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go
   5844    6076    232   3.97%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go
   6152    6384    232   3.77%    2312    2320      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/waveshare-epd/epd2in13x/main.go
  12484   12720    236   1.89%    4788    4796      8   0.17% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/lsm6ds3/main.go
  27804   28044    240   0.86%    4780    4796     16   0.33% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp180/main.go
  17544   17784    240   1.37%    4740    4756     16   0.34% tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/thermistor/main.go
  15372   15616    244   1.59%    4748    4764     16   0.34% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/vl53l1x/main.go
  12248   12500    252   2.06%    4812    4820      8   0.17% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bmp388/main.go
  26500   26752    252   0.95%    2328    2336      8   0.34% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/lsm303agr/main.go
  16160   16416    256   1.58%    2360    2376     16   0.68% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hub75/main.go
  68600   68872    272   0.40%    6196    6204      8   0.13% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp2515/main.go
  32256   32528    272   0.84%    4780    4788      8   0.17% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bme280/main.go
   8376    8660    284   3.39%    2320    2336     16   0.69% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/at24cx/main.go
  13752   14088    336   2.44%    3408    3416      8   0.23% tinygo build -size short -o ./build/test.hex -target=pico     ./examples/sgp30
  12236   12596    360   2.94%    3360    3368      8   0.24% tinygo build -size short -o ./build/test.hex -target=pico ./examples/pca9685/main.go
   5768    6148    380   6.59%    9522    9682    160   1.68% '-xesppie' is not a recognized feature for this target (ignoring feature)
  29644   30032    388   1.31%   38084   38092      8   0.02% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing
   9648   10052    404   4.19%    6788    6804     16   0.24% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/fourwire/main.go
  13584   14032    448   3.30%    6788    6796      8   0.12% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx
  10820   11292    472   4.36%    3348    3356      8   0.24% tinygo build -size short -o ./build/test.hex -target=pico ./examples/touch/capacitive
  12548   13044    496   3.95%    6984    7000     16   0.23% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/touch/resistive/pyportal_touchpaint/main.go
   6764    7356    592   8.75%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go
   6692    7284    592   8.85%    2280    2288      8   0.35% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go
2041078 2059850  18772   0.02%  472522  473410    888   0.00%

We previously used our own //go:extern but //go:linkname is probably the
better choice since it's used in the math/bits package.
@aykevl aykevl force-pushed the runtime-panic-recover-divbyzero branch from 5a5a6a0 to 7fe1e63 Compare February 26, 2025 10:46
This gets the math/bits and go/constant package tests to pass.

Unfortunately, this also has a binary size impact of around 150-200
bytes in many cases. I'm a bit on the edge on whether this is worth it,
since it's mostly used for getting package tests to work. But at the
same time, having working package tests is very valuable.
@aykevl aykevl force-pushed the runtime-panic-recover-divbyzero branch from 7fe1e63 to 98d55ab Compare February 26, 2025 11:58
@aykevl
Copy link
Member Author

aykevl commented Feb 26, 2025

Not happy with the implementation just yet. Currently it removes runtime panic locations, which are very useful for debugging.

@aykevl aykevl marked this pull request as draft February 26, 2025 12:03
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.

1 participant