Skip to content

IC: compilerprocs, type infos and consts are no liveness roots - #26776

Merged
Araq merged 4 commits into
develfrom
araq-ic-dce-roots
Dec 5, 2026
Merged

Araq merged 4 commits into
develfrom
araq-ic-dce-roots

Conversation

@Araq

@Araq Araq commented Nov 20, 2026

Copy link
Copy Markdown
Member

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.
  • Where codegen writes a compilerproc's name verbatim after cgsym(m, "name"), the use is recorded explicitly:
    • inside a proc or RTTI init it becomes an invisible use ((cuse …), written as \6name\3 in the marked text) of that definition;
    • at the top level it becomes a module root.

Type infos and consts

  • V1/V2 type infos and consts were 'd' data roots, and every V1 type info's _init was called from every demanding DatInit. A dead proc that merely generated a type info therefore kept the type info, its init, its TNimNode graph and the hooks it points to.
  • They are now 'u' units whose uses are marked (rttiRef). Their extern declarations at the artifact's top level are no longer marked, since a top-level name is a root.
  • A V1 init is called from a DatInit unit with the type info's own name, so it runs exactly when the type info is live, in its owner. All DatInits run before any module init.
  • Explicit edges where the runtime reads data that the V1 init fills:
    • discriminant checks → the discriminator table;
    • deepCopy → typeInfoV1;
    • nested type infos → their own init call.

Results

Compiler release build:

classic IC before IC after
file size 13.68 MB 14.30 MB 14.10 MB
.text 11.28 MB 11.69 MB 11.54 MB
.bss 12.5 KB 66 KB 14 KB

Hello world (-d:release): 116 KB .text before, 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.
  • New tests/ic/tdcelive (ORC) and tdceliverefc (refc + deepCopy). With the discriminant edge removed, tdcelive fails (an illegal branch change is no longer caught).
  • nim ic vs nim c output A/B over tests/{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, tnildispatcher prints a different trace.
  • The IC-built compiler builds itself to a fixpoint.
  • nim ic -b:cpp smoke test passes.

cnif format 7 → 8. A debugging aid is included: -d:icWhyLive with IC_WHY_LIVE=name,… makes the merge print the root chain that keeps a name alive.

🤖 Generated with Claude Code

Araq and others added 3 commits November 20, 2026 20:52
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>
Comment thread compiler/ccgtypes.nim Outdated
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):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

hack.

Comment thread compiler/ccgtypes.nim Outdated
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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hack.

Comment thread compiler/ccgtypes.nim
markers: Builder
typeNodes: int
typeNodesName: Rope
procUses: seq[string] # the enclosing definition's `icProcUses`

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

why?

…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>
@Araq
Araq merged commit bd86013 into devel Dec 5, 2026
22 checks passed
@Araq
Araq deleted the araq-ic-dce-roots branch December 5, 2026 16:24
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.

1 participant