Skip to content

x86_64 architecture model and Intel Atom (Bonnell) microarchitecture model - #474

Open
magistras42 wants to merge 3 commits into
slothy-optimizer:mainfrom
h2parson:x86_pr1
Open

magistras42 wants to merge 3 commits into
slothy-optimizer:mainfrom
h2parson:x86_pr1

Conversation

@magistras42

Copy link
Copy Markdown

This is the first of three stacked PRs adding x86_64 support to SLOTHY:

  1. x86_64 architecture model + Intel Atom model
  2. Intel/AT&T syntax translator
  3. Keccak-f[1600] on Atom: utility scripts, round example and permutation sweep

The series doesn't change SLOTHY's core (core.py, dataflow.py, heuristics.py are untouched).

Each PR has three commits: code, tests, generated code. We'd like to merge them without squashing; please let us know if that doesn't work for you, and whether force-pushes to update an open PR are OK.

Part 1a: x86_64 architecture model (slothy/targets/x86_64/)

  • AT&T syntax is the model's native syntax. Intel syntax comes with the translator in PR 2.
  • Instruction classes (x86_64_base_instructions.py, x86_64_sse_instructions.py, x86_64_avx_instructions.py) on a pattern-based core (x86_64_instruction_core.py, instruction_core.py). This covers the forms needed for Keccak: GPR ALU ops, moves, rotates/shifts, test, xchg, push/pop, jz/jnz, memory forms with a base register and optional immediate offset, and SSE movdqu/pxor.
  • Registers are matched in two steps: an ID part (a, di, 8, ...) and a size check against the size tag in the pattern (HW/SW/DW/QW for 8/16/32/64-bit GPRs, XMM, YMM). Symbolic registers use the same tags, e.g. %QW<state>.
  • The base register of a memory operand is an instruction input and is recorded as addr, following SLOTHY's convention. push/pop also read and write rsp, so they stay ordered against stack accesses.
  • Flags: each flag bit (SF, ZF, PF) is its own register. Flags are added only as instructions need them (see Known limitations).
  • Memory hint tags (reads=/writes=) work as on AArch64.
  • xor with a memory operand has a fusion callback that splits it into a load and a register xor.
  • SP/BP registers are reserved by default.

Part 1b: Intel Atom / Bonnell model (slothy/targets/x86_64/intel_atom.py)

  • Dual-issue (issue_rate = 2), units ALU0, ALU1, MUL, DIV, MEM, FP0, FP1.
  • Latencies and inverse throughputs start from Agner Fog's instruction tables and were checked with nanoBench on a Bonnell machine (see Bonnell measurements below); the test suite embeds those measurements (test_atom_model.py).
  • Memory-source xor uses ALU0 + MEM (measured; Agner Fog lists either ALU).
  • Memory-source pxor uses FP0 + MEM. It needs the MEM unit and either FP unit, but a choice between unit groups ([[FP0, MEM], [FP1, MEM]]) isn't supported by SLOTHY's unit model, so the model fixes FP0.
  • push, pop and xchg (microcoded, measured inverse throughput 6) occupy every execution unit.
  • llvm_mca_target = "atom".
  • Opt-in constraint for an observed Bonnell memory quirk: intel_atom.memory_offset_constraint = True keeps accesses with the same immediate offset but different base registers at least 8 cycles apart. It is off by default because it is a simplification of behaviour we don't fully understand yet.

Bonnell measurements

The Atom model was checked against these nanoBench measurements, taken one instruction form at a time on a Bonnell machine with thrpt_lat.py (added in PR 3). Every value is in cycles, and "–" means the latency wasn't measured. test_atom_model.py embeds the values and lists the known deviations from the model.

Instruction (64-bit) Inverse throughput Latency
add %rbx, %rax 0.5 1
add $1, %rax 0.5 1
mov %rbx, %rax 0.5 1
mov $1, %rax 0.875 (modelled 0.5) 1
mov (%rdi), %rax 1 –
mov %rax, (%rdi) 1 –
movq $1, (%rdi) 1 –
mov 8(%rdi), %rax 1 –
mov %rax, 8(%rdi) 1 –
movq $1, 8(%rdi) 1 –
not %rax 0.5 1
or %rbx, %rax 0.5 1
pop %rax 1 1
push %rax 1 1
rol $1, %rax 1 1
rol %rax 1 1
shr $1, %rax 1 1
sub $1, %rax 0.5 1
xchg %rax, %rbx 6 6
xor $1, %rax 0.5 1
xor %rbx, %rax 0.5 1
xor 8(%rdi), %rax 1 –
xor %rax, 8(%rdi) 1 –
xorq $1, 8(%rdi) 1 –

Changes to shared code

  • slothy/helper.py: .equ values may contain % (.equ rState, %rdi).
  • slothy/targets/query.py, common/OptimizationRunner.py, test.py, example.py: register x86_64 / Intel Atom.
  • .github/workflows/test_basic.yaml: slothy.targets.x86_64.intel_atom added to both target matrices.
  • README.md, docs/source/general.md: x86_64 / Intel Atom added to the list of supported targets; docs/source/faq.md: "Does SLOTHY support x86?" updated.

Known limitation: loops

Loop optimization on x86_64 (optimize_loop, BranchLoop) is currently unsafe and is being fixed. Flag writes are only partially modelled: test writes SF/ZF/PF and jz/jnz read ZF, but add/sub/and/or/xor/shr don't declare their flag writes. SLOTHY can therefore schedule a flag-writing instruction between test and the branch, which overwrites ZF on hardware. The selftest doesn't compare flags, so it doesn't catch this. Straight-line code that doesn't read flags, including all Keccak examples in this series, is not affected. The code carries the same warning (BranchLoop docstring). The fix (declaring the flag writes, non-renameable flags, the branch flag as a loop-body output, selftest support for flag bits) will follow in a separate PR.

Other limitations

  • addr holds the full register name, so (%rax) and (%eax) count as different addresses (fine when one register size is used, as in Keccak).
  • al/ax/eax/rax are separate registers; overlap is not modelled.
  • movdqu/pxor/xor from memory need an explicit offset (0(%rsi)); offset-free forms give a parse error.
  • AVX (vpunpcklqdq) is in the architecture model but not in the Atom model (Bonnell has no AVX).
  • Code that reads a symbolic register it never writes (e.g. a symbolic address register) can't be assembled for the selftest; optimize it with config.selftest = False.

Tests (commit 2)

  • tests/naive/x86_64/test_instruction_model.py: parsing and write-back of every instruction form, operand roles, size checks, symbolic registers, memory operands, push/pop and rsp, memory hints, optimizing symbolic-input code with the selftest off, register-ID round trip, the xor fusion callback, branches, and importing the architecture without a target.
  • tests/naive/x86_64/test_atom_model.py: unit assignments (memory-source forms, instructions that use every unit), model against nanoBench measurements, scheduling on the shared MEM unit, the opt-in memory constraint, the llvm-mca CPU name.
  • tests/naive/x86_64/_test.py: instructions.s (every non-branch form, with selftest), instructions_subregisters.s, x86_64_equ.s, plus the two unit-test modules; registered in test.py.
  • python3 -W error test.py passes on top of main, and example.py --dry-run --only-target=slothy.targets.x86_64.intel_atom passes.

Generated code (commit 3)

  • examples/naive/x86_64/simple/x86_64_simple.s → examples/opt/x86_64/simple/x86_64_simple_opt_atom.s, covering this PR's changes:
    • every non-branch instruction form on concrete registers, with .equ register aliases (%), memory forms, push/pop (implicit rsp), xchg (every unit) and memory-source pxor (FP0 + MEM); the selftest runs
    • the 8/16/32-bit register forms (individually since partial-register overlap isn't modelled)
    • symbolic registers and reads=/writes= memory hints (the selftest is skipped, following other architectures' examples)
    • the xor-with-memory fusion callback, then the opt-in memory constraint (memory_offset_constraint)
  • Not covered by the example: branches (loop optimization is unsafe, see above) and AVX (not available on Atom).

Commits

  1. x86_64: add x86_64 architectural model and Intel Atom (Bonnell) microarchitectural model
  2. x86_64: add simple tests for the architecture and Atom models
  3. x86_64: add Atom example covering the model and generated code

Co-authored by Hayden Parsons (@h2parson) and Maggie Simmons.

magistras42 and others added 3 commits November 2, 2026 23:50
Co-authored-by: h2parson <haydenfparsons@icloud.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: h2parson <haydenfparsons@icloud.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: h2parson <haydenfparsons@icloud.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant