Skip to content

Fix TCO miscompiling extern/@cEmbed shim tail calls into infinite loops (#517) - #523

Merged
godofecht merged 2 commits into
mainfrom
fix/tco-extern-shim-tail-call
Aug 16, 2026
Merged

godofecht merged 2 commits into
mainfrom
fix/tco-extern-shim-tail-call

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Summary

Fixes #517: the tail-call pass miscompiled return foo_get_c(i) into for(;;) { i = i; continue; } — an infinite loop that never calls the shim — whenever an extern/@cembed shim's declared name started with the wrapper function's name (e.g. shim foo_get_c inside wrapper foo_get).

The old self-call check was a loose prefix match:

st.value.name == self._current_tco_fn or st.value.name.startswith(self._current_tco_fn + "_")

(_current_tco_fn held the unmangled name, so foo_get_c.startswith(foo_get + _) matched.) At -O2 clang then saw the loop as noreturn, inlined it, and silently deleted everything after the call in the calling function.

Fix

Self-calls are now recognized by exact equality against the function's own names — the unmangled source name and its mangled C name (e.g. countdown / countdown_i32):

  • _tail_self_calls / _return_is_self_call take a frozenset of the function's own names; only calls to exactly one of them count.
  • The call-site rewrite check in the return codegen uses st.value.name in self._current_tco_fn.
  • The mangled name is read from the existing self._mangled_names[id(fn)] map, populated by the mangle pass before _gen_function runs.

Shim calls are never rewritten; genuine tail recursion still becomes a constant-stack for(;;) loop with parameter reassignment.

Regression tests (tests/unit/test_tco_shim.py)

  • Shim tail call emits a real call (no for(;;)) and actually runs (returns the shim's value).
  • Genuine tail recursion still TCOs into a loop.
  • 1M-deep recursion runs without stack overflow (constant stack).
  • A tail call referencing the function's mangled name is still recognized as a self-call.

Validation

  • python3 -m pytest tests/unit -q → 1395 passed (full unit suite).
  • flow-sqlite (the project that hit this) rebuilds and passes 840/840 self-checks with the patched transpiler.

…ps (#517)

The tail-call pass recognized self-recursion with a loose prefix match
(`fn_name + "_"`), so an extern or @cembed shim whose declared name
started with the wrapper's name (e.g. `foo_get_c` inside `foo_get`) was
mistaken for a self-call and its `return` was rewritten into `for(;;){}
continue;` — an infinite loop that never called the shim. At -O2 clang
then saw the loop as noreturn and silently deleted the rest of the
calling function.

Self-calls are now recognized by exact equality against the function's
own names (the unmangled source name and its mangled C name), so shim
calls are never rewritten while genuine tail recursion still becomes a
constant-stack loop.

Adds tests/unit/test_tco_shim.py: shim tail calls emit real calls and
run correctly, genuine tail recursion still TCOs, a 1M-deep recursion
stays constant-stack, and mangled self-calls in tail position still
become loops.
@cursor

cursor Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@godofecht
godofecht merged commit 7149334 into main Aug 16, 2026
16 checks passed
@godofecht
godofecht deleted the fix/tco-extern-shim-tail-call branch August 16, 2026 22:23
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.

TCO pass miscompiles return extern_shim(x) into an infinite loop when the shim name prefix-matches the wrapper

1 participant