Skip to content

x86: load-op memory operands (addsd mem,%x) [needs trusted-base approval] #5

Closed

Description

@AananthV

Tier 1, no trusted-base change. The new Asm instructions need semantics, and those are reviewed like other Asm instructions.

Problem

CompCert always emits movsd mem,%t; addsd %t,%x. GCC emits addsd mem,%x. This costs extra instructions and registers, and therefore spills in floating-point-heavy loops.

Evidence (assembly surgery)

benchmark change effect
nbody fold 9 loads into memory operands in the inner loop 106 → 95 ms (−10%)
aes xorl mem,%r 85.2 → 82.5 ms (−3%)

Proposed approach

An RTL operator that reads memory would break the invariants Deadcode, CSE and ValueAnalysis rely on: they track memory only through Iload/Istore/builtins. Better options:

  • fuse load; op at the Linear/Mach → Asm boundary, in a verified peephole. The x86sel agent found that a straightforward Asmgen peephole conflicts with Asmgenproof0's transl_instr_tail / is_tail structure, so this needs a dedicated pass with its own simulation, e.g. on Mach or Linear;
  • or fuse in a new Linear-level pass that introduces Lop_mem instructions, with Stacking and Asmgen support.

Acceptance criteria

  • Complete proofs: no Admitted, admit or new Axiom. check_axioms.sh adds at most flag or validated-oracle parameters.
  • make proof succeeds on x86_64, x86_32, aarch64, arm, powerpc and rv64. Stubs on non-x86 targets are fine.
  • make -C test all test passes.
  • Benchmarks:
    • full test/c suite, plus the larger programs once that evaluation issue lands;
    • no regression beyond noise;
    • the change is also measured combined with all the passes already on master, using the -fno-* flags for ablation (see REPORT.md §2.4).
  • Compile-time and code-size impact reported.
  • New behaviour can be turned off with a -fno-* flag.

Activity

  1. added
    faster-compcert
    tier-1: verified
    on Oct 21, 2026
  2. AananthV commented on Oct 22, 2026

    @AananthV
    OwnerAuthor

    Trusted-base proposal for #5 (load-op memory operands)

    This item needs new x86 Asm instructions with memory operands. CompCert's x86 Asm model has memory forms only for moves (Pmovsd_fm, Pmovl_rm, …). x86/Asm.v is on the trusted-path list in #1, so this is a Tier 2 change and needs owner approval before implementation.

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

    New constructors. A first, minimal set covers what was measured (nbody, aes):

      | Paddd_fm (rd: freg) (a: addrmode)   (**r [addsd mem, rd] *)
      | Psubd_fm (rd: freg) (a: addrmode)   (**r [subsd mem, rd] *)
      | Pmuld_fm (rd: freg) (a: addrmode)   (**r [mulsd mem, rd] *)
      | Pdivd_fm (rd: freg) (a: addrmode)   (**r [divsd mem, rd] *)
      | Paddl_rm (rd: ireg) (a: addrmode)   (**r [addl mem, rd] *)
      | Pxorl_rm (rd: ireg) (a: addrmode)   (**r [xorl mem, rd] *)
      | Pandl_rm (rd: ireg) (a: addrmode)   (**r [andl mem, rd] *)
      | Porl_rm  (rd: ireg) (a: addrmode)   (**r [orl mem, rd] *)

    Semantics, using a helper modelled on the existing exec_load:

    Definition exec_loadop (chunk: memory_chunk) (f: val -> val -> val) (setflags: bool)
                           (m: mem) (a: addrmode) (rs: regset) (rd: preg) :=
      match Mem.loadv chunk m (eval_addrmode a rs) with
      | Some v => Next ((if setflags then nextinstr_nf else nextinstr) (rs#rd <- (f rs#rd v))) m
      | None => Stuck
      end.
    
      | Paddd_fm rd a => exec_loadop Mfloat64 Val.addf false m a rs rd
      | Psubd_fm rd a => exec_loadop Mfloat64 Val.subf false m a rs rd
      | Pmuld_fm rd a => exec_loadop Mfloat64 Val.mulf false m a rs rd
      | Pdivd_fm rd a => exec_loadop Mfloat64 Val.divf false m a rs rd
      | Paddl_rm rd a => exec_loadop Mint32 Val.add true m a rs rd
      | Pxorl_rm rd a => exec_loadop Mint32 Val.xor true m a rs rd
      | Pandl_rm rd a => exec_loadop Mint32 Val.and true m a rs rd
      | Porl_rm  rd a => exec_loadop Mint32 Val.or  true m a rs rd

    Printer (x86/TargetPrinter.ml, also trusted): addsd %a, %freg, addl %a, %ireg32, etc., with the existing addressing printer.

    Justification

    Intel SDM Vol. 2:

    • ADDSD/SUBSD/MULSD/DIVSD: xmm1, xmm2/m64.
    • ADD/XOR/AND/OR: r32, r/m32 (opcodes 03 /r, 33 /r, 23 /r, 0B /r).

    Each instruction reads the operand from memory and combines it with the register exactly as the register-register form does. The register-register forms are already modelled: Paddd_ff → Val.addf, Pxorl_rr → Val.xor with nextinstr_nf.

    Faithfulness argument

    • The memory read is modelled exactly like the existing Pmovsd_fm/Pmovl_rm loads: the same Mem.loadv with the same chunk. If the model can't perform the load (invalid or misaligned per CompCert's chunk alignment), the step is Stuck, so no verified execution relies on it.
    • The SSE scalar forms have no alignment requirement in hardware. That only matters for packed instructions such as movaps, which are not used here. So the model is stricter than the hardware, which is the safe direction.
    • Flags:
      • The integer forms set the arithmetic flags, so they are modelled as undefined (nextinstr_nf), like Pxorl_rr.
      • The SSE forms don't touch EFLAGS (nextinstr), like Paddd_ff.
    • The result is identical to the existing two-instruction sequence movsd mem, tmp; addsd tmp, rd, except that no temporary register is clobbered.

    Blast radius

    • New constructors in x86/Asm.v and printer cases. No change to existing instructions, the memory model, or other targets.
    • Print Assumptions: no new axioms.
    • Proof side, all fully verified: a fusion step that turns load chunk a -> t; op rd rd t (with t dead afterwards) into the load-op form.
      • Candidate places: a new pass on Linear/Mach, or in Asmgen with a lookahead. Earlier work found the straightforward Asmgen peephole conflicts with Asmgenproof0's transl_instr_tail/is_tail, so this is likely a small Mach-level pass with its own simulation.
      • Only the Asm semantics above are trusted; the fusion itself is proved.

    Opt-in story

    Validation plan

    Payoff (measured by asm surgery, REPORT.md §1.3)

    benchmark change effect
    nbody 9 loads folded in the inner loop 106 → 95 ms (−10%)
    aes xorl mem −3%

    PolyBench inner loops (e.g. gemm: mulsd (%r9,%rdx), addsd (%rdi,%rdx) in gcc -O1) would also benefit.

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

  3. added
    tier-2: trusted-base
    needs-approval
    and removed
    tier-1: verified
    on Oct 22, 2026
  4. changed the title [-]x86: load-op memory operands (addsd mem,%x)[/-] [+]x86: load-op memory operands (addsd mem,%x) [needs trusted-base 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 ("LGTM for memory-operand arithmetic (#5)"). The approval covers exactly the proposal above:

    • the 8 instructions Paddd_fm/Psubd_fm/Pmuld_fm/Pdivd_fm/Paddl_rm/Pxorl_rm/Pandl_rm/Porl_rm;
    • their exec_loadop semantics;
    • the printer cases;
    • -fload-op off by default.
      Per Protocol: approval process for trusted-base (Tier 2) changes #1, the approved: trusted-base label is left for the owner to apply. Any change to the trusted definitions beyond this needs re-approval. Implementation is starting on branch issue-5-load-op.
  6. 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