Skip to content

x86: zero-extending byte loads and byte-field extraction (Tier 2 part of #6) #25

Closed

Description

@AananthV

Tier 2, trusted-base change (new x86 Asm instructions). This is the Tier 2 part of #6. Blocked on #1 (proposal + owner approval).

  1. Zero-extending byte loads into a 64-bit register. movzbl mem, %r32 producing a Vlong (the hardware zero-extends to 64 bits). This removes the remaining movl %r,%r after byte loads; siphash24 still has 5 per 8-byte word. Asm surgery: all of them gone gives 0.0824 vs 0.0837 s.
  2. Byte-field extraction. (x >> 8) & 0xff as a single movzbl %ah-style instruction instead of movq/shrl/andl/movl (vmach, part of its remaining gap).

Both need new constructors and semantics in x86/Asm.v and printer cases, in the style of Pshrlzx_ri (#10). Write a proposal as a comment, following #1 and the example on #5.

Activity

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

    @AananthV
    OwnerAuthor

    Trusted-base proposal for #25: zero-extending 8/16-bit loads and moves with a 64-bit result

    Problem

    (unsigned long) p[i] with p an unsigned char * (siphash's U8TO64_LE, and table lookups indexed by a loaded byte) compiles to movzbl mem, %r32; movl %r32, %r32. The first instruction already zero-extends to 64 bits in hardware. But CompCert's model of Pmovzb_rm produces a 32-bit Vint, so Olongofintu must still emit the redundant movl. siphash24 has 5 of these per 8-byte word. Removing all of them by hand gave 0.0837 → 0.0824 s; more is expected in byte-table code such as aes-like ciphers, parsers and compressors.

    Assumption: exact text (proposed additions to x86/Asm.v)

      | Pmovzbq_rm (rd: ireg) (a: addrmode)   (**r [movzbl mem, rd], 64-bit result *)
      | Pmovzwq_rm (rd: ireg) (a: addrmode)   (**r [movzwl mem, rd], 64-bit result *)
      | Pmovzbq_rr (rd: ireg) (r1: ireg)      (**r [movzbl r1, rd], 64-bit result *)
      | Pmovzwq_rr (rd: ireg) (r1: ireg)      (**r [movzwl r1, rd], 64-bit result *)
    
    Definition exec_load_zx (chunk: memory_chunk) (m: mem)
                            (a: addrmode) (rs: regset) (rd: preg) :=
      match Mem.loadv chunk m (eval_addrmode a rs) with
      | Some v => Next (nextinstr_nf (rs#rd <- (Val.longofintu v))) m
      | None => Stuck
      end.
    
      | Pmovzbq_rm rd a  => exec_load_zx Mint8unsigned m a rs rd
      | Pmovzwq_rm rd a  => exec_load_zx Mint16unsigned m a rs rd
      | Pmovzbq_rr rd r1 => Next (nextinstr (rs#rd <- (Val.longofintu (Val.zero_ext 8 rs#r1)))) m
      | Pmovzwq_rr rd r1 => Next (nextinstr (rs#rd <- (Val.longofintu (Val.zero_ext 16 rs#r1)))) m

    Printer (x86/TargetPrinter.ml): print them exactly as the existing Pmovzb_rm/Pmovzw_rm/Pmovzb_rr/Pmovzw_rr, i.e. movzbl <addr>, %r32, movzwl <addr>, %r32, movzbl %r8, %r32 and movzwl %r16, %r32. They are the same machine instructions, given a 64-bit view of the destination.

    Justification

    • Intel SDM Vol. 2, MOVZX: zero-extends the source into the 32-bit destination.
    • Intel SDM Vol. 1 §3.4.1.1: a 32-bit destination is zero-extended to 64 bits.

    The combination gives exactly Val.longofintu (zero_ext 8/16 x). This is the same rule that ledger entry 2 relies on, and asm_difftest already confirmed it for shifts and masks, including when the upper half held garbage.

    Faithfulness argument

    • The load reads memory exactly like the existing Pmovzb_rm/Pmovzw_rm (same Mem.loadv, same chunk); if the load is not possible in the model, the step is Stuck.
    • The register forms use the same Val.zero_ext as Pmovzb_rr/Pmovzw_rr.
    • The only difference from the existing instructions is that the destination is viewed as 64-bit (Val.longofintu), which is what the hardware does.
    • Flags: unchanged by MOVZX. The memory forms use nextinstr_nf, which is conservative and consistent with exec_load; the register forms use nextinstr, like Pmovzb_rr.

    Blast radius

    • Four new constructors in x86/Asm.v and their printer cases. No change to existing instructions, other targets, or axioms.

    • Verified side:

      • new operators or addressing (e.g. Ozextb64/Ozextw64, or a load chunk variant at the Mach level);
      • selection rules for longofintu(load Mint8unsigned …), longofintu(cast8unsigned x) and their 16-bit versions;
      • Asmgen.

      All fully proved, behind -fzext-load, off by default.

    Validation plan

    • Extend harness/asm_difftest/ with the four instructions:

      • all byte and halfword values;
      • garbage in the upper 56/48 bits of the source register and in the old destination;
      • random addresses for the memory forms.

      Compare the full 64-bit destination against the extracted semantics.

    • Run the regression suite with -fzext-load.

    Payoff

    • siphash24: about −1.5% measured by assembly surgery.
    • Byte-table code: expected small gains. This is a modest item; its main value is removing a whole class of redundant movls.

    Not included

    The movzbl %ah byte-field extraction from #6 is dropped from this proposal:

    • it only works with %rax..%rdx as source;
    • it needs register-allocation constraints;
    • its measured payoff (part of vmach's remaining ~9%) is unclear.

    Requesting owner approval: apply approved: trusted-base, or comment with changes.

  3. AananthV commented on Oct 22, 2026

    @AananthV
    OwnerAuthor

    Owner approval recorded (2026-10-22): the owner approved this proposal in chat ("LGTM for all the 4 proposals"). Scope: exactly the proposal above, behind its off-by-default flag. Any change to the trusted definitions beyond the proposal needs re-approval. The approved: trusted-base label is left to the owner per #1.

  4. AananthV commented on Oct 22, 2026

    @AananthV
    OwnerAuthor

    Implementation in progress on branch ``.

  5. added
    approved: trusted-base
    on Oct 22, 2026
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-compcertneeds-approvaltier-2: trusted-base

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions