Skip to content

Conversation

@pguyot
Copy link
Collaborator

@pguyot pguyot commented Oct 2, 2025

The computed goto is a GCC extension that isn't implemented by clang when targeting wasm. In some cases the compiler failed to optimize it.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, but CI fails with:


[100%] Built target test_suite_psa_crypto.pbkdf2
[100%] Built target test_suite_psa_crypto
In file included from /__w/AtomVM/AtomVM/src/libAtomVM/context.c:50:
/__w/AtomVM/AtomVM/src/libAtomVM/opcodesswitch.h:1875:14: error: WebAssembly hasn't implemented computed gotos
 1875 | HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
      |              ^
/__w/AtomVM/AtomVM/src/libAtomVM/opcodesswitch.h:1875:14: error: WebAssembly hasn't implemented computed gotos
fatal error: error in backend: Cannot select: 0x5571b808bc40: i32 = BlockAddress<@scheduler_entry_point, %6247> 0
In function: scheduler_entry_point
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:

Are we missing anything?

@bettio
Copy link
Collaborator

bettio commented Oct 2, 2025

It might be this one:

2849                     if (needs_to_wait) {
2850 #pragma GCC diagnostic push
2851 #pragma GCC diagnostic ignored "-Wpedantic"
2852                         ctx->restore_trap_handler = &&wait_timeout_trap_handler;
2853 #pragma GCC diagnostic pop
2854                         SCHEDULE_WAIT(mod, saved_pc);
2855                     } else {
2856                         JUMP_TO_ADDRESS(mod->labels[label]);
2857                     }
2858                 #endif

@pguyot pguyot force-pushed the w40/remove-unecessary-computed-goto branch from 66a8a25 to c6080b0 Compare October 2, 2025 16:33
@pguyot
Copy link
Collaborator Author

pguyot commented Oct 2, 2025

I think recent version of emscripten no longer tolerates this pattern. This PR now completely gets rid of it.

Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, I'll merge it as soon as CI is green again.

By the way, I'm not understanding why this problem appeared only here and not on main branch.

@pguyot
Copy link
Collaborator Author

pguyot commented Oct 2, 2025

I think it's an emscripten image update thing and we didn't run CI on emscripten recently.

@pguyot pguyot force-pushed the w40/remove-unecessary-computed-goto branch from c6080b0 to c2508b4 Compare October 2, 2025 19:33
Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing since valgrind is detecting an uninitialised value.

[...]
add:
==16441== Conditional jump or move depends on uninitialised value(s)
==16441==    at 0x11D114: scheduler_entry_point (opcodesswitch.h:1925)
==16441==    by 0x11D015: context_execute_loop (opcodesswitch.h:1867)
==16441==    by 0x118AE4: test_atom (test.c:632)
==16441==    by 0x118D54: test_module_execution (test.c:682)
==16441==    by 0x118F12: test_modules_execution (test.c:738)
==16441==    by 0x1192E7: main (test.c:827)
==16441== 
add:OK
[...]
state_test2:
==16441== Thread 2:
==16441== Conditional jump or move depends on uninitialised value(s)
==16441==    at 0x11D114: scheduler_entry_point (opcodesswitch.h:1925)
==16441==    by 0x23C8BF: scheduler_thread_entry_point (smp.c:75)
==16441==    by 0x4A5B608: start_thread (pthread_create.c:477)
==16441==    by 0x4B95352: clone (clone.S:95)
==16441== 
state_test2:OK
[...]

I think the issue is waiting_with_timeout being not initialized.

The computed goto is a GCC extension that isn't implemented by clang when
targeting wasm. Recent versions of emscripten failed to optimize it.

In fact, the emulator didn't need it and it can be replaced with a simple
boolean when waiting with timeout.

Signed-off-by: Paul Guyot <[email protected]>
@pguyot pguyot force-pushed the w40/remove-unecessary-computed-goto branch from c2508b4 to 8b5e393 Compare October 3, 2025 19:26
Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I agree with this change: there is no reason to use computed gotos. Still what was happening doesn't make sense:

CI in main is still successful with the same emsdk container that was failing on this branch:

main:
Screenshot_20251005_092139

When it was failing here:
Screenshot_20251005_091808
Screenshot_20251005_091844

@bettio bettio merged commit e824db0 into atomvm:main Oct 5, 2025
180 of 183 checks passed
@pguyot pguyot deleted the w40/remove-unecessary-computed-goto branch October 5, 2025 09:05
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.

2 participants