Skip to content

compiler: faster inference and optimization on big programs, smaller AST nodes - #4912

Draft
aleksisch wants to merge 8 commits into
GaijinEntertainment:masterfrom
aleksisch:aleksisch/infer-name-lookup
Draft

aleksisch wants to merge 8 commits into
GaijinEntertainment:masterfrom
aleksisch:aleksisch/infer-name-lookup

Conversation

@aleksisch

@aleksisch aleksisch commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

ABI / rebuild: AST node layout changes (gc_node 48 -> 40 bytes, Expression loses printFlags, Program gains a flag), serializer version 312, ExprPrintFlags / Expression.printFlags / ast_print_flags leave the das API. External modules rebuild. #nightly

Why

Compiling examples/dasLLAMA/run.das (17.8k functions, ~100 modules) spends its time in work that grows with the size of the library rather than with the code being compiled: name and alias lookups that walk every module or every global, and analysis passes that recompute whole-module results after changes that did not touch them. Its peak memory is mostly AST nodes, each carrying 16 bytes nothing needs.

What changes

Inference:

  • An unqualified function lookup visits only the modules that define the name (per-pass index by name hash); the "does this name exist anywhere" probe asks the same per-pass list.
  • An alias lookup scans only the globals whose type can name an alias, not every global of the module.

Optimizer and inliner:

  • Side-effect flags (checkSideEffects) are recomputed only after the AST changed, like the access flags.
  • buildAccessFlags refreshes only the module's use-edges (collectUseLists) instead of re-running library-wide symbol-use marking on every rebuild; nothing between inference and the final symbol-use pass reads the use flags.
  • The inliner's heavy-caller check (body cost + call-cycle search) runs only for a body that has an auto-inline site.

Memory:

  • gc_node: gc_id becomes 32-bit beside gc_magic; every AST node is 8 bytes smaller (the global counter stays 64-bit).
  • Expression.printFlags is gone; the bracket decision lives in a side table, ExprBracketLevels in ast_boost, built by the AOT emitters and ast_print before they emit.

Results

Whole set, last four commits (5-8), user CPU time on an idle machine, average of two runs, -compile-only -no-module-cache:

program before after change peak RSS
examples/dasLLAMA/run.das (17.8k functions) 46.4 s 38.4 s -17% unchanged
dasllama_vulkan_classes 13.1 s 12.0 s -8.5% unchanged
dasllama_math_gen 5.55 s 5.05 s -9% unchanged

Per commit:

commit what it removes measured on result
1-2 name index a walk of every module per unqualified lookup run.das, one P-core inferTypes instructions 141.7G -> 119.2G (-16%); infer 103.7 s -> 82.5 s
3 gc_node packing 8 bytes per AST node run.das together with 4: peak RSS about -80 MB
4 printFlags 8 bytes per Expression run.das (see 3)
5 alias-global list a walk of every global per alias lookup dasllama_vulkan_classes 248k lookups: globals walk 17.6 s -> 0.56 s (only 2 lookups ever end at a global)
6 side-effect cache two whole-module visitors before every fold / CSE / DSE pass dasllama_gguf -8% instructions
7 lazy heavy-caller check a body-cost walk and a call-cycle search per function per inline round dasllama_gguf caller checks 0.93 s -> 0.24 s; cycle-search bodies 93k -> 25k; -1% instructions
8 use-edge refresh library-wide symbol-use marking on every access-flag rebuild (1691 rebuilds) run.das symbol use in the rebuilds 5.1 s -> 1.1 s; compile 42.8 s -> 39.1 s (-8.7%)

Rows 1, 5 and 6 were measured on a loaded machine (absolute seconds there run several times higher than idle); the ratios hold.

Observable behavior

  • ast_print no longer brackets every operator: return x + 1, ++y, b += f(...) (golden text in tests/daslib/test_ast_print.das). AOT C++ output is unchanged.
  • Module cache records from older builds are rejected (version 312).

Where to look

  • src/ast/ast_infer_type.h, ast_infer_type_function.cpp, ast_infer_type_helper.cpp - the per-pass name index, the alias-global list.
  • src/ast/ast_const_folding.cpp (checkSideEffects), src/ast/ast_unused.cpp + ast_export.cpp (collectUseLists), src/ast/ast_inline.cpp (callerTooHeavy).
  • include/daScript/misc/gc_node.h, src/misc/gc_node.cpp - the packed id and its size static_assert.
  • daslib/ast_boost.das (ExprBracketLevels, new_bracket_levels / delete_bracket_levels / no_bracket), daslib/aot_cpp.das, daslib/aot_standalone.das, daslib/ast_print.das.
Details
  • Eight commits, each one change: two inference name-lookup commits, the gc_node packing, the printFlags removal, then the alias-global list, the side-effect cache, the lazy heavy-caller check and the use-edge refresh.
  • ExprBracketLevels is created and freed only through the ast_boost helpers, so the module's own AOT stub carries its constructor, methods and finalizer; a new / delete in the caller module left them out of the stub and failed the AOT link (50101).
  • The side-effect cache relies on the invariant the access-flag cache already uses: every AST change goes through astChanged().
  • Under log_optimization the inliner keeps its heavy-caller check ahead of the site collection, so its log lines do not change.
  • Local preflight: format, lint, hash-refs, ast-verify, docs, tests-interp, tests-aot. The only failures are tests/cuda/test_cuda_emit.das / test_cuda_oracle.das: NVRTC on this card cannot resolve __dp4a, the same on master. docs/digest reports drift already on master; this PR adds only its own digest lines.

…name

findMatchingFunctions and findMatchingFunctionsAndGenerics walked every
module of the library for each unqualified name and probed its function
and generic tables, though a name lives in a few modules. With many
modules required that walk is most of the lookup.

InferTypes keeps, per name hash, the modules that define a function or a
generic of that name, in library order, built on the first lookup of the
name in the pass. The module being compiled is always in the list: new
instances land there during the pass. Qualified names keep the walk.

examples/dasLLAMA/run.das (17436 functions), instructions inside
inferTypes on one P-core: 141.7G before, 119.2G after (-16%); infer
103.7 s -> 82.5 s, whole compile 240 s -> 215 s.
… list

ModuleLibrary::hasFunctionOrGenericNamed summed every module's
nameGeneration on each call to check its cache, and the cache dropped
whenever any module gained a name - which the module being compiled does
all through infer. The four infer callers now ask the module being
compiled directly and the per-name module list for the rest, and the
library cache, its three fields and Module::nameGeneration go.

examples/dasLLAMA/run.das, instructions inside inferTypes on one P-core:
119.2G before, 118.0G after.
gc_id only names a node in GC diagnostics (error reports,
DAS_GC_BREAK_ON_ID), so 32 bits are enough; the global id counter stays
64-bit. Every AST node is 8 bytes smaller. A static_assert pins the
64-bit size.
Expression drops printFlags, 8 bytes per node. ExprBracketLevels in
ast_boost collects the expressions an infix emitter prints without
brackets; the AOT emitters and ast_print read it, so ast_print stops
bracketing every operator. ast_print_flags and the unused
Program::setPrintFlags go away; the serializer version moves to 312.
…alias

findAlias and findFuncAlias asked every global of the module for the
alias before falling back to the type name, once per lookup. A module
with many globals pays that walk on every call whose name is not yet
resolved, though almost no lookup ends at a global.

InferTypes keeps, per pass, the globals whose type holds an alias or is
still auto or an alias, rebuilt when the module's global count moves.

dasllama_vulkan_classes: 248k lookups, the globals walk 17.6 s -> 0.56 s.
checkSideEffects runs two whole-module visitors and is called before
every constant-folding, CSE and dead-store pass of every optimization
round, though most calls follow passes that changed nothing.

The flags stay valid until astChanged(), the same signal the access
flags already key on.

dasllama_gguf compile: -8% instructions.
…ne site

processFunction asked, for every function in every patchInline round,
whether the caller is too heavy to receive auto-inlined bodies: a full
body-cost walk plus a call-graph cycle search. Most bodies hold no
auto-inline site, so the answer was never used.

The site collection now runs first; the check runs only when it found
an auto-call site, and a heavy caller collects again without them.
Under log_optimization the check stays ahead of the collection, so the
over-budget log lines do not change.

dasllama_gguf: caller checks 0.93 s -> 0.24 s, cycle-search bodies
walked 93k -> 25k.
buildAccessFlags ran the full symbol-use pass on every rebuild: clear
every use flag of the program, then propagate use from every exported
and init function of every module in the library. It rebuilds after
each optimization or inline round that changed the AST, so a big
program walks its whole library hundreds of times per module.

The side-effect pass reads only the module's use-edges, and no pass
between inference and the module's final symbol-use pass reads the use
flags; collectUseLists refreshes the edges alone.

examples/dasLLAMA/run.das: symbol use inside the access rebuilds
5.1 s -> 1.1 s, compile 42.8 s -> 39.1 s user.
@aleksisch
aleksisch marked this pull request as draft December 5, 2026 07:37

This branch has not been deployed

No deployments
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