From fbf7fc74a32767608c4aa257e1768c5f6b36ff70 Mon Sep 17 00:00:00 2001 From: kotijekriv Date: Mon, 23 Nov 2026 08:12:08 +0100 Subject: [PATCH] docs: asm def-sets hold every asm-written local of the enclosing blocks msvc-backend.md's slot-tiebreak section said an __asm block's def-set is the locals that block writes. slot_probe.py shows more: every asm block puts into its def-set each local that any asm block of the function writes, if the local is declared in a block that contains it, wherever in that block. So a local written by only some blocks weighs more than its own references, a declaration moved within the function's block does not change it, and an inner block holding only the asm that uses it does. Point 3 is corrected (with its +1 / +2 / +3 counts), a paragraph gives the lever with the probe's numbers (renderUnitAnimationWithBlendingUnk, the one function traced for it so far: 10 at the top, 6 in the else block, which gives the original's slots), and slot_probe.py's docstring says the same. Co-Authored-By: Claude Opus 5.5 --- tools/compiler-internals/msvc-backend.md | 31 ++++++++++++++++++------ tools/compiler-internals/slot_probe.py | 5 ++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/tools/compiler-internals/msvc-backend.md b/tools/compiler-internals/msvc-backend.md index c38bc6e8a5..c1426425fb 100644 --- a/tools/compiler-internals/msvc-backend.md +++ b/tools/compiler-internals/msvc-backend.md @@ -2523,13 +2523,17 @@ first with weight >= the new one. Net effect: size ascending, weight descending, weights **the element that reached its final weight first comes first**, i.e. gets the higher address. Declaration order and initialisation order are nowhere in it (both re-tested: no change). -**3. An `__asm` block is one tuple whose operand sets supply the references, so each symbol -counts once per set, not once per mention.** The asm tuple (opcode 0x2ce) carries a points-at list -that `CollectStackElements` flattens as: the *def-set* (every local the block writes) as a nested -set, then the *use-list* (every local the block reads, in reverse order of first mention), then the -def-set again. For the walk in `tracePathPlan...` that is 4 + 17 + 4 = 25 references for 18 -symbols. The written locals therefore get +3 from the block, read-only ones +1, write-only ones +2; -a local's C-side references add 1 each before and after. +**3. An `__asm` block is one tuple whose operand sets supply the references, so each symbol counts +once per set, not once per mention.** The asm tuple (opcode 0x2ce) carries a points-at list that +`CollectStackElements` flattens as: the *def-set* as a nested set, then the *use-list* (every local +the block reads, in reverse order of first mention), then the def-set again. The def-set is not only +what the block itself writes: it holds every local that *any* `__asm` block of the function writes, +as long as that local is declared in a block that contains this one, wherever in that block the +declaration stands (see "A block scope moves an asm-written local" below). For the walk in +`tracePathPlan...` that is 4 + 17 + 4 = 25 references for 18 symbols. A local in the def-set +therefore gets +3 from the block if the block also reads it and +2 if not, whether the block writes +it or only another block does; a local the block only reads, and no asm block writes, gets +1. A +local's C-side references add 1 each before and after. **4. The def-set is iterated in hash order of the symbol name.** The second def-set pass is the *last* reference of every asm-written local that the C code does not touch afterwards, so their @@ -2577,6 +2581,19 @@ followed declaration order, which only happens when two names hash to the same b `name_rank.cpp` ranking puts them adjacent for that reason, and the probe on the real function is what settled each one. +**A block scope moves an asm-written local.** A local that any asm block writes goes into the +def-set of every asm block inside the block that declares it, so a local written by only some of +the blocks weighs more than its own references suggest, and moving its declaration within the same +block changes nothing. Declaring it in an inner block that holds only the asm that uses it does. In +`TextureRenderCore::renderUnitAnimationWithBlendingUnk` (`0x00451E00`: four asm blocks, a 0x1c +frame, so no sort) only the last two blocks write `columnOffset`. Declared at the top it weighs 10 +(+2 from each of the four def-sets, +1 from each of its two use-lists) and takes `-4`, pushing the +three locals the original has above it (weights 7, 7 and 6) down one slot each. Declared at +function level after the `if` that holds the first two blocks, or just before the third, it still +weighs 10. Declared inside the `else` that holds the last two blocks it weighs 6, lands behind the +other three, and every local sits at the original's offset. This function is the only one the rule +has been traced on so far. One `slot_probe.py` run shows the weight; a build confirms the layout. + ### The rule, as code: `simulate()`, `regime()`, and the regression tests `slot_probe.simulate(adds)` replays `AddStackElement` over a recorded reference sequence and diff --git a/tools/compiler-internals/slot_probe.py b/tools/compiler-internals/slot_probe.py index cf45aa795b..9d130882e1 100644 --- a/tools/compiler-internals/slot_probe.py +++ b/tools/compiler-internals/slot_probe.py @@ -13,8 +13,9 @@ later one adds 1 to its weight and moves it ahead of same-size elements with strictly lower weight. So the list ends up size ascending, weight descending, and among equal weights in the order the elements reached their final weight. An __asm block is a single tuple whose operand - sets are: its def-set (locals it writes, iterated in name-hash order), its use-list (locals it - reads, reverse order of first mention), and the def-set again - each symbol counts once per set. + sets are: its def-set (every local that any __asm block of the function writes, if declared in a + block that contains this one; iterated in name-hash order), its use-list (locals it reads, in + reverse order of first mention), and the def-set again - each symbol counts once per set. * the `symStackElements` list at `ColorStackElements` (0x10beaef0) entry, head to tail. Without the sort (below) this IS the slot order: head at the highest address. * whether `QSortColors` ran. It runs only for frames over 0x80 bytes or functions with a /GS