Repository navigation
Fix TCO miscompiling extern/@cEmbed shim tail calls into infinite loops (#517) - #523
Merged
Merged
Conversation
…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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #517: the tail-call pass miscompiled
return foo_get_c(i)intofor(;;) { 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. shimfoo_get_cinside wrapperfoo_get).The old self-call check was a loose prefix match:
(
_current_tco_fnheld the unmangled name, sofoo_get_c.startswith(foo_get+_) matched.) At-O2clang then saw the loop asnoreturn, 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_calltake afrozensetof the function's own names; only calls to exactly one of them count.st.value.name in self._current_tco_fn.self._mangled_names[id(fn)]map, populated by the mangle pass before_gen_functionruns.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)for(;;)) and actually runs (returns the shim's value).Validation
python3 -m pytest tests/unit -q→ 1395 passed (full unit suite).