Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions tools/compiler-internals/msvc-backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tools/compiler-internals/slot_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading