Skip to content

Model x86 shift-count masking in Asm semantics #11

Closed

Description

@AananthV

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.

Activity

  1. added
    faster-compcert
    tier-2: trusted-base
    needs-approval
    on Oct 21, 2026
  2. AananthV commented on Oct 22, 2026

    @AananthV
    OwnerAuthor

    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, %eax
    andq  $31, %rcx     # i % 32
    sall  %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

    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.

  3. AananthV commented on Oct 22, 2026

    @AananthV
    OwnerAuthor

    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%.
  4. added
    approved: trusted-base
    and removed
    needs-approval
    on Oct 22, 2026
  5. AananthV commented on Oct 22, 2026

    @AananthV
    OwnerAuthor

    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.

  6. added 2 commits that reference this issue on Oct 22, 2026
    eff645e
    6ab9c25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    approved: trusted-basefaster-compcerttier-2: trusted-base

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions