Expand description
Structural reachability over an emitted C translation unit — split the runtime into top-level definition UNITS, then keep only those reachable (transitively) from a set of root symbols.
This is the shared tree-shaking pass for the C-derived tiers. The CUDA device-lift uses it so
nvcc only compiles the slice a program actually references (a float one-liner pulls in ~3
functions, not the whole ~1900-line runtime); the same pass can later shrink the C / Tenstorrent
output. It is an OVER-APPROXIMATION (a body’s every identifier is treated as a dependency), so it
never drops a needed unit; and it is verified downstream — a genuinely missed edge surfaces as a
compiler “undefined symbol”, never a silent miscompile.
The parser is a small byte scanner that understands C string/char/line/block-comment lexemes (so a
brace inside "{" does not throw off nesting) and the three top-level shapes the runtime uses:
typedef … ;, static … (…) { … } / … (…) ; functions, and file-scope globals.
Structs§
- CUnit
- One top-level C construct: a typedef, a
staticfunction definition or forward declaration, or a file-scope global.definedis the symbol(s) it introduces;referencesis every identifier it mentions (an over-approximation of its dependencies);textis the exact source span (with any leading comment/blank lines attached, so units travel with their doc comment).
Enums§
Functions§
- c_
identifiers - Collect every C identifier in
s, skipping string/char literals and comments. - macro_
references - A
#define’s macro name and the names its replacement mentions, its own parameters left out: a macro is expanded where it is named, so what it names is reached through it.Nonefor any other directive. A preprocessor unit is its one directive behind whatever commentsparse_c_unitsattached to it. - parse_
c_ units - Split an emitted C translation unit into top-level
CUnits. Leading comments/blank lines attach to the unit that follows.#…preprocessor directives, backslash-continued lines included, are their ownUnitKind::Preprocunits. - reachable_
units - Indices (in original order) of the units transitively reachable from
roots. A unit is reached when any symbol it defines is in the frontier; reaching it adds every identifier it references.