Skip to content

inline, sink: small loops unroll, a read-once local forwards - #4821

Merged
borisbat merged 3 commits into
masterfrom
loop-unroll
Nov 28, 2026
Merged

borisbat merged 3 commits into
masterfrom
loop-unroll

Conversation

@borisbat

@borisbat borisbat commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Rebuild required: CodeOfPolicies changes layout - a host built on the old headers is refused by the ABI stamp (DAS_POLICIES_VERSION 7).

Why. In the interpreter a short loop pays its setup and a node a pass, and an inlined call leaves locals read once. A four-pass loop over an array literal, through an [inline] function, cost 51.4 ns and costs 10.9 ns.

What changes.

  • The inliner unrolls a for loop of known passes while passes times body statements fits loop_unroll_cost (8).
  • An array literal given to a parameter that is only a loop source becomes that loop's source.
  • A callee that ends in its one return splices with no result local.
  • The sink pass replaces a pure local read once in the next statement with its initializer; disable_forward keeps it.
  • Under the debugger an if over a fused compare stays its own node.

Observable behavior.

  • for (i in range(4)) { s += i } -> four statements, no loop.
  • for [unroll_disable] or options loop_unroll_cost = 0 -> the loop stays.
  • A debugger step over if (a < b) -> stops at the condition again.

Where to look. unrollLoop (ast_inline.cpp) and forwardLet (ast_sink.cpp): the risk is an evaluation moved past an effect.

Validation, claims, ledger

Validation

  • Box: Linux x86-64, clang 19, Ninja, Release, the ci/release_modules.txt module set, an AMD Radeon 760M under RADV. make-pr's own gates are green on the pushed tip. The one full run stopped at the fast tier on two gates: five added test lines over the length limit, and the JIT emitter pin, which follows the LLVM_JIT_CODEGEN_VERSION bump. Both were fixed; each of those gates and every lane the stop skipped then ran once, and the gates and the interpreter, JIT, AOT and C++ lanes ran again after disable_forward went in.
  • Green: lint (70 files, three rails), review-md, review-md-tests, ast-verify, cpp-syntax, compile-sweep (737 roots), tests-cpp (ctest -L small, which runs the new case in tests-cpp/small/test_policies_canary.cpp), tests-interp, tests-jit, tests-aot, watchdog-interp, watchdog-jit, utils-tests, docs/das2rst, docs/stubs.
  • tests-aot went red once, in the run where all lanes shared the box: tests/lint/test_parallel_equivalence.das took 64.7 s against the 60 s file budget. Run alone the lane is green, twice, and that file takes 13.8 s.
  • docs/sphinx-html is red on the box: 138 warnings, every one video: <name>.mp4 is not staged. The box has no GitHub login to fetch the tutorial recordings. No other warning.
  • docs/digest is red on the box with no change from this PR in it: the regenerated skills/daslang/references/everything.md differs from the shipped one in one line, the place of Jobserver in the jobque list.
  • dasLLAMA, each with the runner's own line: REPORT suite model-free: 84 file(s), 0 failed, 115 cell(s) skipped (a filtered arm, an absent device, or a missing model or fixture - each skips loudly) and REPORT suite gpu-kernels: 5 file(s), 0 failed, 71 cell(s) skipped (a filtered arm, an absent device, or a missing model or fixture - each skips loudly). The box has no models, so no --changed, stocked or category run was made.
  • Kernel sources: 30 dasLLAMA files that hold kernel code set loop_unroll_cost = 0 and disable_forward; the sink pass stays on in them, as on master. Emitted Vulkan kernels, master against the branch: the gpu-kernels suite under DASLLAMA_VK_SPV_DUMP on both builds, the dumps compared by harness/vk_spv_diff.das: 386 identical, 2 moved, 0 appeared, 0 vanished. The two are kq_batch_k6_mmq_cls_spv (74836 -> 74472 bytes) and kq_batch_k6_mmqs_cls_spv (75360 -> 74996 bytes): the inliner splices mmq_k6_s8 with no result local, so each has two variables, 16 stores and 8 loads fewer, and the same values. With disable_sink in place of disable_forward nine NV4 kernels moved too, because the sink shapes ue4m3_half_f32 on master. The CUDA and Metal kernel texts were not compared: the box has neither device.
  • Source-shape readers: the MCP introspection compiles set loop_unroll_cost = 0 and disable_forward beside their auto_inline_functions = false, since a go-to-definition on an unrolled loop variable or a forwarded local has nothing to land on (utils/mcp/test_tools_navigation.das). The JIT debug-info fixture sets disable_forward: its test names every local.
  • Negative controls, each a mutant compiler build and the named test going red: the finally refusal, the written loop variable, the foreign generic instance, the macro-owned gate of the forward, the disable_forward gate, the optimized-build gate, the urange source, the callee-write test of a literal element (the test returns 102 for 3 without it), the unsafe callee of the flat splice.
  • External review: one codex round at 35e4fbd8bb, no findings. The commits after it add tests, wrap lines, set the emitter pin and add disable_forward, one condition in the sink pass.
  • --jit-check-abi was not run for a cross target: no wasm or ARM build of this tree exists on the box. loop_unroll_cost and disable_forward sit after every other bound field of CodeOfPolicies, and dll_search_paths, the one standard-library member that sat in front of bound fields, is now the last member.
  • Versions: AST serializer 291, DAS_POLICIES_VERSION 7, LLVM_JIT_CODEGEN_VERSION 0xD3 with LLVM_JIT_EMITTER_HASH set again.
  • Citations: ARCHITECTURE_INLINE.md#loop-unroll (loopPassValues, InlineCollect::visit(ExprFor), unrollLoop), #inline-loop-source-argument (ParamReadScan, InlineBodyRewriter::visit(ExprFor), stackLiteralForLoopSource), #inline-trailing-return (trailingReturn), ARCHITECTURE_EFFECTS.md#forward-read-once-let (evaluatesAfterPureSiblings, forwardableLet, evaluatesAfterPureSiblingsFrom, forwardLet), #macro-owned-shape (hasMacroOwnedShape, callerAutoCfg) are new, each read against its section, all match. ARCHITECTURE_LOOP_SOURCE.md#stack-loop-literal changed; its citer stackLoopLiteral matches. src/simulate/ARCHITECTURE.md#if-over-compare changed; its citers SimNode_IfCmp, the evalBool redefinition in simulate_fusion_op2_bool.cpp and FusionPoint_IfCmp match.
  • Test files and options: tests/optimization/optimization_inline.das takes disable_forward whole. Its structural tests assert on the temps a splice binds or does not bind: with the forward on, a "no temp" check passes even where the inliner bound one, and the "binds a copy" check of target_var_from_let fails. The five local-function tests of tests/ast_match/test_comprehensions.das moved to test_local_function_post_inference.das, which carries the option; the rest of that file keeps its options. In tests/gc/test_stackwalk.das the invoked block reads its argument twice, so the argument temp the test walks is not forwarded away.
  • Doc claims and their tests: the budget, [unroll], [unroll_disable], loop_unroll_cost = 0 and the optimized-build limit are test_loop_unroll_fired and test_loop_unroll_keeps; disable_auto_inline is test_loop_stays_without_auto_inline; disable_forward is test_disable_forward_keeps_a_read_once_local; the two host-side policies are the C API case in test_policies_canary.cpp.

Claims - stated, not tested

  • Under the debugger the if keeps its step. No test observes single steps. Checked by a node dump of if (a < 5) under options debugger: IfLessThenElseArgConst before, IfThenElse over LessArgConst after, and the fused node again with the guard removed. A break shows as a stepping session that passes over the if.
  • The forward refuses an initializer that is a reference. A control with that guard removed stays green: the value-read test refuses the same local first.
  • test_loop_stays_without_auto_inline passes with the optimized-build gate removed: under disable_auto_inline the patch pass does not run at all. The unoptimized-module test is the one that proves the gate.
  • No test covers these refusals: a forward under a parent kind other than return, a two-operand operator, a let and an if condition; the trailing return with a move, a label or a site that stores in place; a renamed local that would shadow an argument in an unrolled body.
  • The loop variable takes the literal's constness. Only AOT sees it: without it the generated C++ does not compile.

Not done

  • The flat splice still runs in dasLLAMA kernel files and reaches kernel code at three call sites (mmq_k6_s8 twice, fp4_strip_code). Master inlined the same calls with a result local in a block; the values are the same. The Vulkan compare above shows the first two.
  • evaluatesAfterPureSiblings keeps its own list of parent kinds and does not reuse childrenInEvalOrder. That walk answers a different question, lists more kinds and marks the left of && eager, so folding the two widens what forwards and needs its own pass over the tree.
  • Small copies left as they are: the decode of a constant range (also in ast_const_folding.cpp), the read-count lookup and the local-rename setup in ast_inline.cpp. exprBody and flatBody stay two flags.
  • Shader code outside dasLLAMA does not opt out: a helper function a dasVulkan tutorial shader calls gets its read-once locals forwarded.
  • Under --jit-debug a forwarded local and an unrolled loop variable have no debug record, as an inlined call has no frame.
  • No rule or gate yet says that a file with kernel code carries the two options.
  • The forward stops at an initializer that may raise and at a read past the next statement.

@borisbat
borisbat force-pushed the loop-unroll branch 2 times, most recently from 794363e to 96c0044 Compare November 28, 2026 02:36
borisbat and others added 3 commits November 27, 2026 19:34
The debugger's if node reports a step at the condition and at the arm
it takes. The compare twin reports neither, and the fusion replaced the
debugger's node with it, so a stepping session passed over the if. The
fusion point now makes no twin for a context that has the debugger on.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
In the interpreter a short loop pays its setup and a node a pass, and a
spliced callee leaves locals that are read once. Four rewrites remove
that cost.

The inliner unrolls a for loop whose passes are known at compile time
(a constant range, an array literal of constants, a fixed array) while
passes times body statements fits loop_unroll_cost, 8 by default. The
loops are planned on the walk that plans the call sites. for [unroll]
lifts the limit and for [unroll_disable] keeps one loop.

An array literal given to a parameter the body reads once, as a loop
source, becomes that loop's source: a fixed array on the stack and no
heap array. A callee body that ends in its one return splices with no
result local and no scope block.

The sink pass replaces a pure local read once in the next statement
with its initializer; disable_forward keeps such locals. Nothing with
an effect may sit between the two.

A function that carries an annotation, and a generated one, keep their
loops and locals: a macro may read that body after the optimizer. The
shader fixtures set loop_unroll_cost = 0.

CodeOfPolicies gains loop_unroll_cost and disable_forward after its
last bound field, and dll_search_paths moves behind them. Serializer
291, DAS_POLICIES_VERSION 7, LLVM_JIT_CODEGEN_VERSION 0xCB.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A kernel body is lowered by a shader emitter and never runs in the
interpreter, so unrolling its loops and forwarding its locals buys no
time and only changes the code the emitter writes. The annotation gate
does not reach a helper method or a free function a kernel calls, so
each file that holds kernel code sets loop_unroll_cost = 0 and
disable_forward: the 26 files with kernel entry points, the shared
kernel bodies, the GPU math helpers and the Metal subgroup helpers.
The sink pass stays on in them, as it is on master.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@borisbat
borisbat merged commit ffaf9da into master Nov 28, 2026
66 checks passed
@borisbat
borisbat deleted the loop-unroll branch November 28, 2026 05:27
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