You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Model x86 shift-count masking in Asm semantics #11
Tier 2, trusted-base change (x86 Asm semantics). Blocked on #1. Probably the smallest Tier 2 item.
Problem
CompCert emits andq $31,%rcx before sall %cl. Val.shl is undefined for counts ≥ 32, and Psall_rcl in x86/Asm.v doesn't model the hardware masking of the count to 5 bits (6 for 64-bit).
Evidence
Removing the masking in nsievebits by assembly surgery: 97 → 93.5 ms (−4%). Remaining gap: 1.10×.
Trusted-base question
Refine Psall_rcl/Pshrl_rcl/Psarl_rcl (and the 64-bit variants) to mask the count: Val.shl v (Val.and rs#RCX (Vint 31)).
This makes the model more faithful: Intel SDM, SAL/SAR/SHL/SHR, "the count is masked to 5 bits (or 6 bits with REX.W)".
Then a fully proved selection rule can drop an explicit & 31 when the shift is emitted with %cl. Measure first how often the C source has & 31 versus CompCert inserting the mask itself.
Follow the trusted-base protocol (#1): write the proposal as a comment on this issue first. No implementation until the owner applies approved: trusted-base.
Trusted-base proposal for #11 (model x86 shift-count masking)
Problem
In nsievebits, a[i / NBITS] & (1 << i % NBITS) (with NBITS = 32 and unsigned i) compiles to:
movl$1, %eaxandq$31, %rcx # i % 32sall %cl, %eax
The hardware already masks the count of sall %cl to 5 bits (6 for 64-bit). CompCert must keep the andq, because its Asm model gives Psall_rcl the semantics Val.shl rs#rd rs#RCX, which is undefined (Vundef) for counts ≥ 32. The model is more conservative than the hardware. Removing the mask by hand gave nsievebits 97 → 93.5 ms (−4%) in the round-1 diagnosis.
Assumption: exact text (proposed changes to x86/Asm.v)
Refine the six variable-count shifts to mask the count, as the hardware does:
| Psall_rcl rd => Next (nextinstr_nf (rs#rd <- (Val.shl rs#rd (Val.and rs#RCX (Vint (Int.repr 31)))))) m
| Pshrl_rcl rd => Next (nextinstr_nf (rs#rd <- (Val.shru rs#rd (Val.and rs#RCX (Vint (Int.repr 31)))))) m
| Psarl_rcl rd => Next (nextinstr_nf (rs#rd <- (Val.shr rs#rd (Val.and rs#RCX (Vint (Int.repr 31)))))) m
| Psalq_rcl rd => Next (nextinstr_nf (rs#rd <- (Val.shll rs#rd (Val.and rs#RCX (Vint (Int.repr 63)))))) m
| Pshrq_rcl rd => Next (nextinstr_nf (rs#rd <- (Val.shrlu rs#rd (Val.and rs#RCX (Vint (Int.repr 63)))))) m
| Psarq_rcl rd => Next (nextinstr_nf (rs#rd <- (Val.shrl rs#rd (Val.and rs#RCX (Vint (Int.repr 63)))))) m
The 64-bit shifts take their count as a 32-bit Vint in CompCert, as today. The printer does not change, because the instructions are the same.
Justification
Intel SDM Vol. 2, SAL/SAR/SHL/SHR: "The count is masked to 5 bits (or 6 bits if in 64-bit mode and REX.W is used). The count range is limited to 0 to 31 (or 63 if 64-bit mode and REX.W is used)." For the flags, counts of 0 leave them unchanged and other counts set them. The model already makes all flags undefined (nextinstr_nf), which over-approximates both cases.
Faithfulness argument
Old model: for counts in [0, 31] the new semantics equals the old one, and for counts ≥ 32 the old result was Vundef.
New model: for counts ≥ 32 it returns exactly what the hardware computes, the masked shift. So the new model is a refinement: everywhere the old model was defined, the two agree.
Every existing proof that uses these instructions for Oshl/Oshru/Oshr keeps working via the lemma Val.shl x y = Val.shl x (Val.and y 31) whenever the left side is defined. The verified compiler's guarantee is unaffected, because it already never relies on Vundef results.
This makes the model more faithful to the hardware, not less.
Blast radius
Six exec_instr cases in x86/Asm.v.
x86/Asmgenproof1.v / Asmgenproof.v need the refinement lemma.
No new axioms; no other target is affected.
Optimization side, fully verified:
a selection rule that compiles x << (y & 31) (and >>u, >>s, and the 64-bit forms with & 63) to a new masked shift operator, Oshlmask etc. in x86/Op.v, whose semantics is the masked shift;
Asmgen emits the plain %cl shift for it, dropping the and.
Opt-in story
The semantic refinement itself is unconditional. The model change cannot be put behind a flag cleanly, and it is a strict refinement. The optimization that drops the and is behind -fshift-mask, off by default, per #1.
Validation plan
Extend harness/asm_difftest/ (added in x86: load-op memory operands (addsd mem,%x) [needs trusted-base approval] #5) with the six instructions, for all counts 0..255 and random values, comparing the extracted semantics with the CPU bit for bit. Flags are excluded because they are undefined in the model.
Run the regression suite with -fshift-mask.
Payoff
benchmark
effect
source
nsievebits
−4%
asm surgery
other bit-manipulation code (e.g. 1 << (k & 31) in bitsets, hash functions)
small
expected
Requesting owner approval: apply approved: trusted-base to proceed, or comment with changes.
Hardware evidence for this proposal (PR #30, asm_difftest group shift): for the six %cl shifts, all counts 0..255 with garbage in bits 8..63 of %rcx, about 20k tests each:
Current model: 0 mismatches where it is defined, but it is defined in only ~6–12% of the tests (counts below 32/64 with clean upper bits). The rest are Vundef.
Proposed model (Val.shl x (Val.and rcx 31) etc.): 0 mismatches, defined in 100%.
Owner approval recorded (2026-10-22): the owner approved this proposal in chat and applied approved: trusted-base. The approval covers exactly the six refined exec_instr cases above; the optimization goes behind -fshift-mask, off by default. Implementation is starting on branch issue-11-shift-mask.
Activity
AananthV commentedon Oct 22, 2026
Trusted-base proposal for #11 (model x86 shift-count masking)
Problem
In nsievebits,
a[i / NBITS] & (1 << i % NBITS)(withNBITS = 32and unsignedi) compiles to:The hardware already masks the count of
sall %clto 5 bits (6 for 64-bit). CompCert must keep theandq, because its Asm model givesPsall_rclthe semanticsVal.shl rs#rd rs#RCX, which is undefined (Vundef) for counts ≥ 32. The model is more conservative than the hardware. Removing the mask by hand gave nsievebits 97 → 93.5 ms (−4%) in the round-1 diagnosis.Assumption: exact text (proposed changes to
x86/Asm.v)Refine the six variable-count shifts to mask the count, as the hardware does:
The 64-bit shifts take their count as a 32-bit
Vintin CompCert, as today. The printer does not change, because the instructions are the same.Justification
Intel SDM Vol. 2, SAL/SAR/SHL/SHR: "The count is masked to 5 bits (or 6 bits if in 64-bit mode and REX.W is used). The count range is limited to 0 to 31 (or 63 if 64-bit mode and REX.W is used)." For the flags, counts of 0 leave them unchanged and other counts set them. The model already makes all flags undefined (
nextinstr_nf), which over-approximates both cases.Faithfulness argument
Vundef.Oshl/Oshru/Oshrkeeps working via the lemmaVal.shl x y = Val.shl x (Val.and y 31)whenever the left side is defined. The verified compiler's guarantee is unaffected, because it already never relies onVundefresults.Blast radius
exec_instrcases inx86/Asm.v.x86/Asmgenproof1.v/Asmgenproof.vneed the refinement lemma.x << (y & 31)(and>>u,>>s, and the 64-bit forms with& 63) to a new masked shift operator,Oshlmasketc. inx86/Op.v, whose semantics is the masked shift;%clshift for it, dropping theand.Opt-in story
The semantic refinement itself is unconditional. The model change cannot be put behind a flag cleanly, and it is a strict refinement. The optimization that drops the
andis behind-fshift-mask, off by default, per #1.Validation plan
harness/asm_difftest/(added in x86: load-op memory operands (addsd mem,%x) [needs trusted-base approval] #5) with the six instructions, for all counts 0..255 and random values, comparing the extracted semantics with the CPU bit for bit. Flags are excluded because they are undefined in the model.-fshift-mask.Payoff
1 << (k & 31)in bitsets, hash functions)Requesting owner approval: apply
approved: trusted-baseto proceed, or comment with changes.asm_difftest: test round-1 Asm instructions, printer xor-zeroing, and…
AananthV commentedon Oct 22, 2026
Hardware evidence for this proposal (PR #30,
asm_difftestgroupshift): for the six%clshifts, all counts 0..255 with garbage in bits 8..63 of%rcx, about 20k tests each:Vundef.Val.shl x (Val.and rcx 31)etc.): 0 mismatches, defined in 100%.AananthV commentedon Oct 22, 2026
Owner approval recorded (2026-10-22): the owner approved this proposal in chat and applied
approved: trusted-base. The approval covers exactly the six refinedexec_instrcases above; the optimization goes behind-fshift-mask, off by default. Implementation is starting on branchissue-11-shift-mask.x86: model shift-count masking; optional masked-shift selection
faster-compcert: ledger entry 4 and report on shift-count masking (RE…
Enable -fshift-mask, -fzext-load and -fmerge-bytes by default