baseline: Decide which instruction implementations to inline into the dispatch loop

Abierto
#1,813 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
12/100
Tipo de issue
Refactorización
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
cpp

Línea de trabajo

The work is in the Baseline dispatch loops in baseline_execution.cpp and in intx's addmod(), sdivrem() and udivrem(). Start by building with GCC and Clang and checking the inlining decisions the issue describes. Done means a per-instruction inlining choice that is measured on random operands, at several code alignments, with branch-miss counters. This is a performance research task, not a first contribution.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Which instruction implementations are inlined into the Baseline dispatch loops (dispatch_cgoto, dispatch<false>, dispatch<true>) is left to the compilers' inlining heuristics. The same goes for which intx functions are inlined into those implementations. The heuristics weigh code size, not how hot or how expensive an instruction is, so unrelated size changes in evmone or intx move handlers in or out of the loops.

  • GCC inlines into baseline_execution.cpp until the file has grown by --param inline-unit-growth (40%). It also lets a large function grow only by large-function-growth (100%). The dispatch loops sit at this limit:
    • SAR, LOG1–4, CALLDATACOPY, CODECOPY and EXTCODECOPY are left out.
    • LOG0, MCOPY and RETURNDATACOPY move in and out with small changes in intx. For example, a smaller SLT in chfast/intx#453 pulls LOG0 in. The larger exp() of chfast/intx#455 turns the EXP handler into a call and pulls LOG0 and MCOPY in.
  • Clang decides per call site and keeps larger intx functions behind calls. The 256-bit results then take extra copies through memory, see ADDMOD below.
  • Relayout: every such change also relayouts the loops, and the dispatch branch prediction depends on the layout. With GCC the chained-MUL benchmark runs in one of two states: 0.46M or 6.4M branch misses per 3 executions, with identical instructions and 1.5–2.4× the cycles.

Options

  • Decide per instruction in evmone: [[gnu::always_inline]], [[gnu::noinline]] or [[gnu::flatten]] on the implementations, possibly different for GCC and Clang.
  • Raise GCC's budget for baseline_execution.cpp only:
    • Use --param=inline-unit-growth=100 and --param=large-function-growth=200.

    • In a prototype at x86-64-v3, this pins the current decisions and inlines the left-out handlers:

      change
      SAR −2…−4%
      CODECOPY −3…−9%
      CALLDATACOPY −1…−5%
      RETURNDATACOPY −1…−4%
      evm-benchmarks ±2%
      snailtracer −3.8%
      .text +6.8 KB (v2), +8.7 KB (v3)
  • Attributes in intx instead: they also affect intx's other users.

Items to check

Measure each item with per-instruction contracts on random (worst-case) operands, at 3 code alignments, and with branch-miss counters.

  • ADDMOD with Clang.
    • What happens: intx::addmod() (127 instructions) stays out of line. It copies the remainder out of the division result with a 32-byte load right after udivrem()'s 8-byte stores. The load fails store forwarding, which costs about 11% of the cycles of an ADDMOD loop. The handler then copies the returned value again.

    • [[clang::always_inline]] on intx::addmod() (x86-64-v3, 3 code alignments, ADDMOD contracts):

      modulus change
      1–4 words −5…−8%
      BN254 −16…−18%

      GCC's code is identical.

    • The same with [[gnu::always_inline]]: GCC gets slower. GCC already inlines the fast path of addmod() into the loop, and with the attribute the whole handler leaves the loop. BN254 +20%; chains of reduced ADDMODs +16% (v3) and +44% (v2).

    • Side cost: with a zero modulus, the cheapest case, ADDMOD pays for the handler's frame: +24 cycles.

  • SDIV/SMOD with Clang. intx::sdivrem() stays out of line and returns its 64-byte result through memory. The copy of the result fails store forwarding. [[gnu::always_inline]] on it: Clang −10…−18%, GCC at x86-64-v3 up to +4.6% (its handlers then sit behind a call).
  • EXP with GCC: a call instead of inlined after chfast/intx#455.
  • SAR, LOG1–4, CALLDATACOPY, CODECOPY, EXTCODECOPY with GCC: left out by the budget.
Lenguaje dominante
C++
Estrellas
979
Forks
340
Merge medio
12 h 16 min
PR fusionados (30 d)
25

Preparar el entorno

  • Incluye un Dockerfile o un archivo de Docker Compose
  • Sin plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ipsilon/evmone

Todos los issues de ipsilon/evmone

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.