Repository navigation
asm-census: refuse overwritten loop counters and spilled flags - #1066
Conversation
There was a problem hiding this comment.
Verdict: MUST_FIX: A memory bit instruction can still produce a green derivation with the wrong loop count, and a removed test case lacks its required dated reason.
Must fix
1. Refuse memory bit instructions before checking stack overlap
tools/ref/asm-census.py:307 bounds btrq %rdx, 0x10(%rsp) to eight bytes. With %rdx=0x80, that instruction clears bit 0 at 0x20(%rsp). The spilled-flag fixture exits on its first test, but the reader reports two trips. Setting %edx=0x80 on entry and using this instruction as the fixture's head produces (82, 10), both rows ok, and red=0.
One refusal now prevents a later stale-table approval. Replace the guard at line 293:
if kind == "implicit" or (re.fullmatch(r"bt[rcs]?[wlq]?", mn) and any("(" in o for o in ops)):
return TrueAdd this case to tile_refusals() so restoring the bug makes the self-test fail:
("a bit reset aliases the spilled flag",
flagged("spill", inner="btrq\t%rdx, 0x10(%rsp)", entry=("movl\t$0x80, %edx",)),
"may touch the flag"),The replacement passes the existing self-test and rejects this case.
2. Date the removal of the induction-refusal test
tools/ref/asm-census.py:1017 replaces the refusal table without its former movl $0x2, %eax induction-count case. The body explains why the flag alone proves the trips, but the gate-removal contract also requires a dated reason.
Record the reason while the deleted case is visible in this diff. Add this line to the PR body:
PIN(2026-11-01): Removed the induction-value refusal and its test because the flag's entry value, dominating test, and latch clear prove two trips without an induction register.
Checked
Duplicates: no duplication finding submitted. No dryer pairs were supplied.
Complexity: no Rust functions changed; crapper supplied no function scores.
Silent failure: the missed write in item 1 reaches two green rows with red=0.
Tests: the existing self-test passes. The proposed regression fails on head and passes with the suggested fix. The inline replacement is valid Python on an added line.
Gates: tolerance unchanged; the removed induction-refusal case needs the dated reason in item 2.
Escape catalog: no patterns matched. This change touches tool code, with no inference hot path.
Notes
dryer and crapper did not run because no Rust file changed.
🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)
| %rsp is a register operand, an operand at %rsp may overlap the slot's SLOT_BYTES (its width the widest register | ||
| operand or integer suffix, unknown: overlapping), or a "D"/"DF" instruction stores through any other address.""" | ||
| mn, ops, kind = form(ins) | ||
| if kind == "implicit": |
There was a problem hiding this comment.
A register bit index lets memory bt/bts/btr/btc reach another stack slot. Refuse these forms before applying the fixed-width overlap check.
| if kind == "implicit": | |
| if kind == "implicit" or (re.fullmatch(r"bt[rcs]?[wlq]?", mn) and any("(" in o for o in ops)): |
… touch them The derivation proved loop trip counts by reading what each instruction writes, and every gap in that reading let a wrong loop cost come out as a green row: an overlapping store or a call over a spilled flag, a byte alias such as %dil, the implicit writes of cmpxchg, lahf and cltq, xadd and mulx with matching operands, and a latch that cleared its flag and then set it again. The counter and the flag now carry no write semantics. In a counted loop only the latch's `add $s, %r` and `cmp $n, %r` may name the counter (any alias), and the jne must return to the loop's head. In a flag loop only the `test $1, FLAG` and one clear in the latch (`xorl` of the register's 32-bit name, or `mov $0, SLOT`) may touch the flag, and the test must dominate the latch. Any other instruction that names the location outside an address, reads or stores an overlapping stack slot, stores through another address or moves %rsp refuses the loop, and so does every instruction with implicit effects (call, push, pop, xchg, xadd, cmpxchg, mulx, one-operand mul, div and imul, lahf, cltq, cqto). Between the flag test and its je only instructions that write no flags may stand. The entry value is read from the first instruction on the entry path that touches the location, which must write it a constant (or, for a slot, store a register that does). An instruction or operand form the table does not list is still refused by name. The flag alone proves two trips, so the induction register is no longer read. The self-test feeds one table of refusals, these shapes and the earlier ones, and checks each reason. The table report on the current qdot listing is identical to main: 14 rows within tolerance and IQ2_XXS refused on its own row.
56cceb0 to
b72726f
Compare
There was a problem hiding this comment.
Since the last review:
- Must-fix 1 fixed: tools/ref/asm-census.py:294 refuses memory bit instructions before checking stack overlap.
- Must-fix 2 fixed: tools/ref/asm-census.py:1077 and the PR body give the dated reason for removing the induction-refusal case.
Verdict: CLEAN: Both earlier findings are resolved, and the interdiff has no remaining finding.
Checked
Duplicates: 0 new functions, types or constants; the added cases reuse the existing fixture. No dryer pairs supplied.
Complexity: no crapper scores; the edit adds one guard predicate and a parameterized refusal matrix.
Silent failure: 3 callers of the changed guard followed through counted-loop checks, flag-loop checks and entry inference. Memory bit operations produce named refusals.
Tests: self-test passes. All 48 new cases assert the refusal reason. Removing the memory-bit clause at line 294 accepts all 48 and makes the self-test fail. Indexed, segment-based, immediate-index and negative-offset probes also refuse; unrelated register-only bit operations remain allowed.
Gates: no tolerance changed. The removed induction-refusal case now has its dated PIN and reason.
Escape catalog: no patterns matched. Only tool code changed; no inference hot path.
Notes
dryer and crapper did not run because no Rust file changed.
🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)
|
The review is done and the contribution check passed, so I'm merging this. |
타일 비용 유도기가 덮어쓴 loop counter와 spill된 flag를 잘못 읽어 틀린 명령 수를 받아들이던 문제를 막았습니다.
Supersedes #1063.
What and why
tools/ref/asm-census.pyderives the host tile cost rows from the kernels' disassembly. To count a loop's trips it proves that only the latch changes the loop counter, or that a flag loop's flag is set to one on entry and cleared once in the latch. Main proved this by reading what each instruction writes, and every gap in that reading let a wrong cost come out as a green row: a store or acallover a spilled flag, a stack slot at a negative offset, a byte alias such as%dil, the implicit writes ofcmpxchg,lahfandcltq,xchg,xaddandmulxwriting both outputs or a matching operand, a latch that cleared its flag and then set it again, and a memorybt,bts,btrorbtcwhose bit index reaches a slot far from its written address.The reader no longer models write semantics for the counter or the flag. It allows a fixed set of instructions on them and refuses the loop on anything else:
add $s, %randcmp $n, %rmay name the counter in any alias, thejnemust return to the loop's head, andnmust be a positive multiple ofs.test $1, FLAGand one clear in the latch (xorlof the register's 32-bit name, ormov $0, SLOT) may touch the flag. The test's block must dominate the latch, and only instructions that write no flags may stand between the test and itsje.%rsp. Every instruction with implicit effects (call,push,pop,xchg,xadd,cmpxchg,mulx, one-operandmul,divandimul,lahf,cltq,cqto) and a memorybt,bts,btrorbtcrefuses the loop wherever it stands in it.The flag alone proves the two trips, so the induction register is no longer read.
PIN(2026-11-01): Removed the induction-value refusal and its test because the flag's entry value, dominating test, and latch clear prove two trips without an induction register. An instruction or operand form the reader does not list is still refused by name. The cost table and
crates/model/src/ops.rsare unchanged.Release notes: Fixed: the tile cost derivation refuses a loop whose counter or flag it cannot follow instead of reporting a wrong cost.
Code shape
The review reads these first. A box left unticked needs one line of why.
copying it (
AGENTS.md"Common code first",docs/rust-quality.mdR14). Where I found an existing copy, Inamed it below.
(tensor shapes, the attention kind, the quant). A common owner gained no per-model branch, flag or trait method.
tools/check-comments.sh).PIN(YYYY-MM-DD):and its reason.
cargo fmtandcargo clippyare clean on what I touched; the lint count did not go up.Duplication found or removed (
path:line): none.What I ran
The self-test feeds one table of 115 refusal cases to the reader and checks the reason each one names. It covers the shapes above and the earlier counter and flag cases. Fed to the reader in main, 50 of the first 67 are accepted: 47 as an all-green report, two as a trip count from
Cfg.trips(a counted latch whosejneleaves the loop, read as 8 trips, and a flag test the latch can bypass, read as 2), and one as a stale row. For example:The 48 memory bit cases (
bt,bts,btr,btcwith no suffix,w,landq, in the head, the entry and the latch of a spilled flag loop) were all accepted before the guard, and the self-test failed:After the fix:
Derivation over the current qdot listing:
The same listing derives the same report with the reader in main (
diffempty). The other 14 rows are within tolerance of the table, IQ1_S's flag loops included, and IQ2_XXS is refused on its own row as it is on main.AI help
AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.