Optimizing code for AMD Zen 5
The sizes, widths and rules of AMD’s Zen 5 core that matter when tuning code for it, from AMD’s optimization guide for the core, checked against Agner Fog’s measurements and LLVM’s model, with the places where they disagree.
- Microarchitecture
- AMD Zen 5
- Claims
- 54, 11 with a source that differs
The core in numbers
- Processors
- Family 1Ah, models 00h to 4Fh and 60h to 6Fh. [AMD 58455, Chapter 1, page 8]
- Dispatch
- At most 8 macro-ops a cycle, dispatched from the front end into the schedulers. [AMD 58455, Section 2.9.7, page 39]
- In flight
- 448 macro-ops in the retire queue, or 224 per thread when two threads share the core; 240 integer physical registers and 192 for flags. [AMD 58455, Section 2.10.3, page 42]
- Execution
- 6 integer ALUs, 4 address generation units, and 4 vector and floating-point pipes of 512 bits with 2 more pipes for stores and moves to integer registers. [AMD 58455, Section 2.4, page 18; AMD 58455, Section 2.11, page 43]
- Op cache
- 64 sets of 16 ways, at most 6K instructions, and up to 12 instructions per cycle. [AMD 58455, Section 2.9.1, page 34]
- L1 instruction cache
- 32 KB per core, 8-way, with 64-byte lines. [AMD 58455, Section 2.6.1, page 19]
- L1 data cache
- 48 KB per core, 12-way; a load hit takes 4 or 5 cycles to an integer register and 7 or 8 to a vector register. [AMD 58455, Section 2.6.2, page 20]
- L2 cache
- 1 MB per core, 16-way, inclusive of the L1 caches; a hit takes at least 14 cycles. One run on an EPYC 9645 in a KVM guest measured 14 cycles for chains of loads over 64 KB to 256 KB. [AMD 58455, Section 2.6.3, page 23; Measured, EPYC 9645, result load-64k, median 14.02 cycles; Measured, EPYC 9645, result load-256k, median 14.02 cycles]
- L3 cache
- Up to 96 MB, 16-way, shared by the cores of a complex and filled with what the L2 caches evict; a hit takes 46 cycles on average. [AMD 58455, Section 2.6.4, page 23]
- Data TLBs
- 96 entries in the L1 TLB for pages of every size; in the L2 TLB, 4,096 entries for 4 KB, 16 KB and 2 MB pages and another 1,024 for 1 GB pages. [AMD 58455, Sections 2.7.1 and 2.7.2, page 24]
- Branch misprediction
- 12 to 18 cycles, by the kind of branch and by whether the op cache feeds the pipeline; 15 in the common case. [AMD 58455, Section 2.8, page 26]
Front end
Two decode pipes each deliver as many as four instructions a cycle from a 32-byte window. One thread alone decodes at most four instructions per cycle; with two threads, each has a pipe of its own. [AMD 58455, Section 2.9, page 33]
Of the four decode slots, only the first can take an instruction longer than 10 bytes, so code that runs from the decoders should not have more than one such instruction in any four; long NOPs used as padding are the usual case. [AMD 58455, Sections 2.8.3.1 and 2.9, pages 32 and 33]
Branch targets aligned to 16 bytes give the decoders their full width. [AMD 58455, Section 2.9, page 33]
An op cache entry holds up to six instructions, from at most two adjacent 64-byte lines, with six slots for 32-bit immediates and displacements between them. A 64-bit immediate takes two slots, so instructions with large constants fill entries sooner. [AMD 58455, Section 2.9.1, page 34]
The front end switches from the decoders to the op cache only at a taken branch, and back when the op cache misses. Hot code that fits in the op cache avoids the switches: with two threads on the core, each should keep to half of it, which limits how far loops are worth unrolling. [AMD 58455, Section 2.9.1, page 34]
For padding, fewer and longer NOPs are better than many short ones. Zen 5 decodes a NOP of up to 15 bytes, made by adding 66h prefixes to the 8-byte form, without a penalty; earlier AMD processors such as Family 15h slow down with more than three 66h prefixes, so code for all of them stops at 11 bytes. [AMD 58455, Section 2.8.3.1, Tables 6 and 7, pages 31 and 32]
Branches
With its 16K entries, the L1 BTB predicts direct jumps, direct calls and indirect branches with a single target with no bubble; returns and indirect branches with several targets cost two cycles. A prediction from the 8K-entry L2 BTB costs eight. [AMD 58455, Section 2.8.1.2, page 27]
A BTB entry holds two branches, so code entering a 64-byte line best meets at most two branches in it before leaving: a third takes another entry and more cycles to predict, unless an earlier branch is unconditional or nearly always taken. [AMD 58455, Section 2.8.3, page 30]
Loop alignment rarely matters. For a hot loop, AMD suggests starting it at a 64-byte boundary or, slightly better, ending it on the last byte of a 64-byte line. [AMD 58455, Section 2.8.3, page 30]
A conditional branch that has never been taken is not in the BTB and is predicted not taken, and branches that never go the other way stay out of the global history. Branches that are mostly not taken are the cheapest. [AMD 58455, Section 2.8.1.5, page 29]
The return address stack has 52 entries per thread. A call to the next instruction is treated specially and does not unbalance it, but in 64-bit code lea rax, [rip] is the simpler way to read RIP. The longer return that AMD once advised for Family 10h is not needed: a one-byte ret costs nothing more. [AMD 58455, Section 2.8.1.3, page 28]
Indirect branches with one target are predicted from the BTB; those with several use a 3,072-entry table indexed with global history, so fewer targets per branch predict better. [AMD 58455, Section 2.8.1.4, pages 28 and 29]
Integer execution
A cmp, test, add, sub, inc, dec, and, or or xor followed by a conditional branch becomes one macro-op, also with a memory operand, within limits on the sizes of its displacement and immediate. [AMD 58455, Section 2.9.3, page 36]
New in Zen 5, a register-to-register mov fuses with a following ALU instruction that writes the same register and reads only registers or an immediate, as in mov eax, edi then add eax, esi. [AMD 58455, Section 2.9.4, pages 36 and 37]
Register-to-register moves of 32-bit and 64-bit general registers and of whole vector registers take no execution cycle, nor do the zeroing idioms such as xor eax, eax and vpxor, which also break the dependency on the old value. [AMD 58455, Sections 2.9.2 and 2.9.5, pages 35 and 37]
PUSH, POP, CALL and RET update RSP without a dependency, and loads, stores, lea and add or sub of a small immediate through RSP can use the tracked value. Other instructions that read or write RSP cost an extra macro-op and reset the tracking. [AMD 58455, Section 2.9.6, page 38]
PDEP and PEXT run natively, three per cycle with a latency of three cycles; code with a separate path for processors where they are slow should take the fast path. [AMD 58455, Section 2.10.2, page 41]
Three ALUs multiply, 64 by 64 bits in three cycles; a multiplication that writes two registers delivers the second one a cycle later. [AMD 58455, Section 2.10.2, page 41]
Division takes about 8 cycles plus 1 for every 9 bits of the quotient, and a typical 64-bit division can start every 8 cycles. [AMD 58455, Section 2.10.2, page 41]
An lea with a base, an index and a displacement is two macro-ops, where one with two of them is one. [AMD 58455, Section 2.10.2, page 41]
Some versions of Zen 5 make TZCNT, BLSI, BLSR and BLSMSK wait for the old value of their destination. Clearing the destination with xor first removes the wait. LLVM 23.1.2 tunes code for every Zen 5 as if these instructions had the false dependency. One run on an EPYC 9645 (family 1Ah, model 11h, stepping 0) in a KVM guest shows none for BLSI, BLSR and BLSMSK: each, followed by an IMUL of its result, took 0.5 cycles a pair, with or without the destination cleared, while BSF, which keeps its destination when the source is zero, took 4.0. The run’s readings for TZCNT spread too widely to cite. [AMD 58455, Section 2.9.9, page 39; LLVM 23.1.2, X86.td, line 1747; LLVM 23.1.2, X86.td, line 605; Measured, EPYC 9645, result dep-blsi-64, median 0.50 cycles; Measured, EPYC 9645, result dep-blsr-64, median 0.50 cycles; Measured, EPYC 9645, result dep-blsmsk-64, median 0.50 cycles; Measured, EPYC 9645, result dep-blsr-32-xor, median 0.49 cycles; Measured, EPYC 9645, result dep-bsf-64, median 4.01 cycles]
Vectors and floating point
The vector units are 512 bits wide, so most 512-bit operations are one macro-op like their 256-bit forms, and AMD recommends 512-bit vectors where the data allows. Zen 4 issues them over two cycles on 256-bit paths. [AMD 58455, Sections 2.11 and 2.11.2, pages 43 and 46; AMD 58455, Section 2.3, Table 5, page 17; AMD 57647, Section 2.11, page 34]
Of the four vector pipes, 0 and 1 multiply and run FMA, 2 and 3 add and convert, 1 divides and takes square roots, and all four run moves and logic. Shuffles and shifts run on 1 and 2. [AMD 58455, Section 2.11.1, page 45]
An FMA, or an AVX-512 operation with merge masking, needs a third source and competes for the source bus of pipe 4 or 5, which may stall it. [AMD 58455, Section 2.11, page 44]
AMD’s latency spreadsheet gives VPADDD a latency of one cycle and a throughput of four per cycle, on any of the four vector pipes. [AMD 58455, latency spreadsheet, row 179 (VPADDD)]
Legacy SSE instructions are slower while the upper bits of the vector registers are not zero, since each then has to merge them; vzeroupper after AVX code avoids it. [AMD 58455, Section 2.11.6, page 49; Agner Fog, Section 25.11, page 257]
Multiplication, division and square root may run again with a longer latency when an input or result is denormal; setting FTZ and DAZ in MXCSR avoids it, at the cost of IEEE 754 results. x87 code has no such switch. [AMD 58455, Sections 2.11.2 and 2.11.4, pages 46 and 47]
An FMA shortens a chain that runs through a multiplication into an addition, but lengthens one that runs through the addend: there a separate addition, or unrolling with several accumulators, is faster. [AMD 58455, Section 2.11.7, page 49]
A gather is slower than vector loads and shuffles when the indices are known in advance. [AMD 58455, Section 2.11.2, page 46]
Loads, stores and caches
The load-store unit runs four memory operations each cycle. Loads may fill all four slots, vector loads two of them; stores may fill two, and a 512-bit store one. [AMD 58455, Section 2.12, page 50]
A load with a base and a displacement, or a base and an index without scale, reaches an integer register in 4 cycles. A scaled index, a base with an index and a displacement, or a segment base that is not zero adds one cycle. One run on an EPYC 9645 in a KVM guest measured the same in chains of loads within the L1: 4 cycles for a base plus a displacement, 5 with an index scaled by 8 or with a base, an index and a displacement. [AMD 58455, Section 2.12, pages 51 and 52; Measured, EPYC 9645, result load-16k-disp, median 4.02 cycles; Measured, EPYC 9645, result load-16k-scaled, median 5.02 cycles; Measured, EPYC 9645, result load-16k-index-disp, median 5.02 cycles]
An access that crosses a 64-byte line takes two cycles of the cache instead of one, and one that crosses a 4 KB page can cost much more. Aligned data is as fast with the unaligned instructions, such as movups, as with the aligned ones. [AMD 58455, Section 2.6.2, page 20]
A load gets its value from an earlier store still in the store queue when that one store holds every byte of the load, wherever the load starts within it. One run on an EPYC 9645 in a KVM guest measured 7 cycles from the store’s data to the load’s result for every load within a store of 1 to 8 bytes from a general-purpose register, at every offset, and 9 for loads within a store of 16 or 32 bytes from a vector register. A load that only partly overlapped the store took 14 to 16. [AMD 58455, Section 2.12, page 50; Measured, EPYC 9645, result stlf-8-4-4, median 7.02 cycles; Measured, EPYC 9645, result stlf-1-1-0, median 7.02 cycles; Measured, EPYC 9645, result vstlf-32-16-8, median 9.02 cycles; Measured, EPYC 9645, result stlf-8-8-1, median 14.01 cycles; Measured, EPYC 9645, result vstlf-16-16-m8, median 16.26 cycles]
One run on an EPYC 9645 (family 1Ah, model 11h, stepping 0) in a KVM guest found an exception for 64-byte stores from a ZMM register: a load within the store got its value in 11 cycles, but a load that crossed the store’s middle, its 32nd byte, took 15, as long as a load that only partly overlaps a store. AMD’s guide does not describe this. [Measured, EPYC 9645, result vstlf-64-16-0, median 11.01 cycles; Measured, EPYC 9645, result vstlf-64-8-28, median 15.01 cycles]
The same run found that a load can skip forwarding altogether. A 4-byte store and a 4-byte load from the same address through the same base register, each load’s result the next store’s data, took 0.53 cycles a pair instead of the 7 of forwarding: the load did not wait for the store at all. AMD’s guide does not describe this. [Measured, EPYC 9645, result stlf-4-4-0-same, median 0.53 cycles; Measured, EPYC 9645, result stlf-8-4-4, median 7.02 cycles]
Store forwarding matches on bits 11:0 of the address, so several stores in flight to addresses that differ by multiples of 4 KB slow down a load that needs one of them. One run on an EPYC 9645 in a KVM guest measured a chain of loads at 7.5 cycles a load instead of 4 when each load followed a store to an address 4 KB, 8 KB or 16 KB below it, and no slowdown for 1 KB, 2 KB or 3 KB. [AMD 58455, Section 2.12, page 50; Measured, EPYC 9645, result alias-load-4096, median 7.54 cycles; Measured, EPYC 9645, result alias-load-1024, median 4.04 cycles]
The load queue tracks 64 loads that have not completed, the store queue holds 104 stores, and 124 cache misses can be outstanding at once. [AMD 58455, Section 2.12, pages 50 and 51]
PREFETCHT0 fills every data cache level, PREFETCHT1 all but the L1, and PREFETCHT2 all but the L1 and L2. PREFETCHNTA marks the line to leave the L2 early and never enter the L3. [AMD 58455, Section 2.13.1, Table 8, page 54; AMD 58455, Section 2.6.4, page 23]
rep movs and rep stos of at least the size of the L3 store with streaming stores that bypass the caches, and on some systems smaller ones skip reading the lines they overwrite whole. Either way, other cores may see the stores of one such instruction in a different order than they were made. [AMD 58455, Section 2.13.6, page 58]
Locked instructions are fastest when their memory operand stays within one 64-byte line, where Zen 4 wants it within 16 bytes. [AMD 58455, Section 2.15, page 60; AMD 57647, Section 2.15, page 46]
Two threads on a core
With two threads, the store queue, the retire queue and the op queue are split in half; the schedulers, register files and load queue are shared up to a limit per thread; the caches, TLBs and the op cache are shared freely. [AMD 58455, Section 2.14, Table 10, page 60]
Two threads that run the same code share BTB and op cache entries only if that code sits at identical linear and physical addresses; threads that run different code collide less in the BTB when it is in different linear pages. [AMD 58455, Section 2.14, page 60]
Sources
- AMD: Software Optimization Guide for the AMD Zen5 Microarchitecture, 58455, revision 1.00, August 2024. The Wayback Machine’s copy of AMD’s ZIP, which holds the guide as a PDF and a spreadsheet of instruction latencies. Its printed page numbers are those of the PDF.
SHA-256
a9a9e04d910bccec; all rights reserved; facts only. - The LLVM Project: LLVM: the X86 target's processor definitions and scheduling models, commit 85ac56026243 of 20 September 2026 (github.com/llvm/llvm-project,
llvm/lib/Target/X86). The release pinned in docker/Dockerfile, whose Clang the instruction reference uses. X86.td names the scheduling model of each -march, and X86Sched*.td hold the models. Apache-2.0 WITH LLVM-exception. - Agner Fog: The microarchitecture of Intel, AMD, and VIA CPUs: An optimization guide for assembly programmers and compiler makers, 23 May 2026. Agner Fog’s measurements and descriptions, cited one fact at a time. Its URL always serves the latest version, so citations give the section and the page of the version of the date above as text instead of linking to the page.
SHA-256
75bcd395680df474; no redistribution; facts only. - x86-64.net: Measurements of an AMD EPYC 9645 (Zen 5) in a KVM guest, run of 19 October 2026 (
tools/optimize/measure/fixtures/zen5-epyc-9645.json). One run of the site’s measuring probe (make measure) on one virtual CPU of a KVM guest whose host has an EPYC 9645 (CPUID family 1Ah, model 11h, stepping 0), with 101 samples of each result; other jobs kept the guest’s load average between 20 and 24. The measuring chapter describes the method. SHA-2567c9da999b80313eb; our own measurement. - AMD: Software Optimization Guide for the AMD Zen4 Microarchitecture, 57647, revision 1.00, January 2023. The Wayback Machine’s copy of AMD’s ZIP, which holds the guide as a PDF and a spreadsheet of instruction latencies; amd.com no longer serves it. Its printed page numbers are those of the PDF.
SHA-256
ddea915eec7a91e3; all rights reserved; facts only.