Repository navigation
IC: compilerprocs, type infos and consts are no liveness roots - #26776
Merged
Merged
Conversation
The merge stage's liveness walk only sees cnif-marked names, so every definition whose uses it could not see had to be a root: * compilerprocs (named at sem time, so unmarked) and `extern:` procs: their names are now marked like any other routine's. A `cgsym` whose caller writes the name verbatim records an invisible use (`cuse`) of the proc being generated, or a module root at the top level: the same demand-driven rule as whole-program codegen. * V1/V2 type infos and consts were `'d'` data roots, and V1 init procs ran from every DatInit. They are `'u'` units now with marked uses; a V1 init is called from a same-named DatInit unit, so it runs exactly when its type info is live. Discriminant checks and deepCopy get explicit edges to the V1 info whose init fills what they read. Release build of the compiler: IC minus classic goes from +616 KB to +416 KB (.text +417 KB -> +268 KB, .bss +53 KB -> +1.6 KB). A hello world: 116 KB -> 57 KB text (classic: 53 KB). cnif format 7 -> 8. `-d:icWhyLive` + `IC_WHY_LIVE=name,...` prints why the merge keeps a name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under --hotCodeReloading these are reached through hcrGetGlobal by string name or registered from the module init under a `_const` name, so no marked use refers to them: as 'u' units the merge dropped them (undefined `*_const` at link/dlopen; refc crashed in newSeq because the V1 init calls, wrapped in a unit named after a type info that is no cnif unit under HCR, never ran). Fixes tests/dll/nimhcr_ic.nim. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Araq
commented
Dec 5, 2026
| sfImportc notin s.flags and | ||
| (sfExportc notin s.flags or sfCompilerProc in s.flags) and | ||
| not hasCnifMarks(s.loc.snippet) and | ||
| allCharsInSet(s.loc.snippet, IdentChars): |
Araq
commented
Dec 5, 2026
| proc rttiRef(m: BModule; name: Snippet): Snippet = | ||
| ## A reference to the type info `name`. In the per-module backend it is | ||
| ## cnif-marked: the type info is a DCE'd unit, live only when used. | ||
| if m.config.cmd == cmdNifC and not m.hcrOn and name.startsWith("NTI") and |
Araq
commented
Dec 5, 2026
| markers: Builder | ||
| typeNodes: int | ||
| typeNodesName: Rope | ||
| procUses: seq[string] # the enclosing definition's `icProcUses` |
…ifUsesOf The allCharsInSet(.., IdentChars) checks in fillBackendName and rttiRef never fail (probed with a nim ic build of the compiler, tests/cpp member /constructor tests and tests/ic); rttiRef's callers only pass NTI names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
IC release builds were noticeably bigger than classic builds. The per-module backend's merge stage does Nimony-style mark & sweep, but over the C text: it only sees cnif-marked names. So every definition whose uses it couldn't see had to be a root. This PR makes those uses visible.
Routines
extern:procs and compilerprocs get their sem-time C names cnif-marked, like every other routine. Compilerprocs lose their'x'root flag: as in whole-program codegen, they are generated when used.cgsym(m, "name"), the use is recorded explicitly:(cuse …), written as\6name\3in the marked text) of that definition;Type infos and consts
'd'data roots, and every V1 type info's_initwas called from every demanding DatInit. A dead proc that merely generated a type info therefore kept the type info, its init, itsTNimNodegraph and the hooks it points to.'u'units whose uses are marked (rttiRef). Theirexterndeclarations at the artifact's top level are no longer marked, since a top-level name is a root.typeInfoV1;Results
Compiler release build:
.text.bssHello world (
-d:release): 116 KB.textbefore, 57 KB after; classic is 53 KB.What remains of the gap is mostly gcc inlining differently because instances are placed in the using modules (the Nimony model), plus identical-code hooks for different types. Name-based merging can't fold those.
Tests
tests/ic: 94/94.tests/ic/tdcelive(ORC) andtdceliverefc(refc + deepCopy). With the discriminant edge removed,tdcelivefails (an illegal branch change is no longer caught).nim icvsnim coutput A/B overtests/{objvariant,exception,objects,deepcopy,method}under ORC and refc: no new differences. The three that differ also differ on devel: two print a module name in stack traces,tnildispatcherprints a different trace.nim ic -b:cppsmoke test passes.cnif format 7 → 8. A debugging aid is included:
-d:icWhyLivewithIC_WHY_LIVE=name,…makes the merge print the root chain that keeps a name alive.🤖 Generated with Claude Code