perf: global dead code elimination (Issue #4 — Tasks A + B)#7
Merged
Conversation
…dr, and __ hooks Root cause: count_top_fn_callers and mark_live_top_fns checked for TOK_FN (=0) but `fn` is lexed as TOK_IDENT — so no function bodies were ever scanned and both reach/caller_cnt tables stayed all-zero, eliminating ~400 of 816 functions during self-compilation. Fixes applied: - Use streq "fn" fallback in DCE scans (mirrors main compilation loop pattern) - Add should_skip_compile_fn helper; protect expansion products (impl/generic/ trait methods have names appended to source buffer at offset >= g_orig_src_len) - Protect __ runtime hooks (e.g. __thread_done used via spawn return address) - Detect fn_addr(name) references in scan_top_fn_calls so fn_addr targets are marked live even without a direct call - Remove debug print statements committed with initial DCE implementation Result: 394/394 conformance tests pass, self-host converges at 1,990,912 bytes
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
Closes #4 — implements global dead code elimination for top-level functions.
Task A: Call graph + unreachable function elimination
IDENT LPAREN),fn_addr(name)references, and DOT-method callsmainthrough the call graphmainwith no callers are skipped at compile timeTask B: Cross-block use counts via reaching-definitions framework
g_fn_caller_cnt_tbltracks raw call frequency across all function bodies (reaching-definitions style: counts how many call sites reference each function)g_fn_reach_tbl(transitive reachability frommain): both must be zero for a function to be eliminated — this is equivalent to "zero reaching definitions reach a live use"Bug fixes over initial implementation
count_top_fn_callers/mark_live_top_fnsusedTOK_FN == 0check, butfnlexes asTOK_IDENT— neither function ever ran. Fixed withstreqfallback matching the main compilation loop__runtime hooks (e.g.__thread_doneused as spawn return address) are exempt from DCEfn_addr(name)references now count as live uses of the referenced functionTest plan
dead_fn_unreachable_undeffail test: compile errors correctly on undefined dead function🤖 Generated with Claude Code