baseline: Weight the computed goto with the opcode frequencies

Aperta
#1,792 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

Start with baseline_execution.cpp, dispatch_cgoto, and cgoto_table to understand how the computed goto maps targets to opcodes. The issue proposes adding opcode frequency weights to the indirectbr, then lists benchmarks, mainnet frequency counts, AArch64 and switch dispatch checks, and Clang build integration as open work. Done means benchmarked weights based on mainnet blocks and verified the affected dispatch paths and build integration.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

The register allocator decides which values of the Baseline dispatch loop stay in registers and which are reloaded from the stack. Without a profile, LLVM's block frequency estimate gives every target of the computed goto the same weight: in dispatch_cgoto (Clang 21) all 149 non-terminating handlers get 0.583 × the function entry, so ADD weighs as much as BLOBHASH. Changes that shift liveness in the loop then move reloads into hot handlers by chance, e.g. #1779 on AArch64 with Clang 21: +1 stack load in 52 handlers, including ADD, LT, EQ, JUMPI and DUP2.

Regular PGO does not fix this:

  • Front-end instrumentation (-fprofile-instr-use) leaves the indirectbr without weights.
  • IR instrumentation (-fprofile-use) weights it, but restructures the whole loop. With a profile from snailtracer, sha1_shifts and swap_math, these benchmarks got 5–8% slower: the checks materialize a status code and each handler reloads 4 values before the indirect jump (ADD 18 → 26 instructions).
  • The IR profile is keyed by the CFG hash of dispatch_cgoto, which contains all inlined handlers, so it goes stale with almost every change to the loop. GCC cannot reuse profiles across commits at all.

Idea

Give the compiler only the opcode frequencies, as branch_weights on the computed goto's indirectbr, and leave everything else as without a profile. The weights depend only on the opcode distribution, not on the code, so a checked-in table stays valid. The frequencies should come from mainnet blocks.

Prototype

Clang 21, x86-64: compile baseline_execution.cpp to -O3 IR, map each indirectbr target to its opcodes through cgoto_table, add the weights, then run only the code generation (-Xclang -disable-llvm-passes; without weights this gives the same object as the normal build). The weights are the opcode counts of the profile above.

Shortest success paths of the 149 handlers (instructions / stack loads):

weights all handlers POP, SWAP1, JUMPI, DUP1, DUP2, AND, ADD, JUMP, SUB SHL, SHR
none 3152 / 180 1 load each 23/1, 24/1
measured counts 3485 / 419 no loads, same length 25/1, 26/1
3 tiers 3316 / 252 no loads, same length 27/2, 28/2

The tiers are 1000 for opcodes with at least 1% of the executed instructions, 30 for at least 0.01%, and 1 for the rest. MLOAD and MSTORE also lose a load. The reloads move into the cold handlers.

TODO

  • Benchmark the variants.
  • Count the opcode frequencies on mainnet blocks.
  • Check AArch64 and the switch-based dispatch<false> loop.
  • Build integration (Clang only): an extra IR step for baseline_execution.cpp, or a pass plugin.
Lingua principale
C++
Stelle
979
Fork
340
Merge medio
12h 16m
PR unite (30g)
25

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ipsilon/evmone

Tutte le issue di ipsilon/evmone

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.