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

未关闭
#1,813 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
12/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
C++
星标
979
派生
340
平均合并
12 小时 16 分钟
30 天内合并 PR
25

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ipsilon/evmone 的其他 Issue

查看 ipsilon/evmone 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。