wardite 0.9.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +191 -0
- data/Makefile +14 -0
- data/README.md +107 -2
- data/Rakefile +5 -0
- data/Steepfile +1 -0
- data/assets/wardite.svg +12 -0
- data/benchmark/README.md +500 -0
- data/benchmark/build_call_profile.rb +42 -0
- data/benchmark/call_cost_worker.rb +27 -0
- data/benchmark/compare.rb +93 -0
- data/benchmark/compare_call_types.rb +212 -0
- data/benchmark/compare_dispatch.rb +123 -0
- data/benchmark/compare_engines.rb +119 -0
- data/benchmark/compare_optimizations.rb +139 -0
- data/benchmark/engine_phases.rb +30 -0
- data/benchmark/figures/README.md +35 -0
- data/benchmark/figures/plot_spinel_comparison.py +137 -0
- data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
- data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
- data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
- data/benchmark/figures/spinel_new_vm_summary.json +82 -0
- data/benchmark/float_m5.wat +42 -0
- data/benchmark/i32_call_arities.wat +89 -0
- data/benchmark/i32_calls.wat +174 -0
- data/benchmark/i32_memory.wat +58 -0
- data/benchmark/i32_mix.wat +80 -0
- data/benchmark/i64_m4.wat +55 -0
- data/benchmark/investigate_calls.rb +231 -0
- data/benchmark/investigate_generated_guards.rb +218 -0
- data/benchmark/m3_call_worker.rb +38 -0
- data/benchmark/measure_m1.rb +51 -0
- data/benchmark/measure_m2.rb +55 -0
- data/benchmark/measure_m3_calls.rb +89 -0
- data/benchmark/measure_m4.rb +134 -0
- data/benchmark/measure_m5_boundary.rb +41 -0
- data/benchmark/measure_mir_cache_verify.rb +58 -0
- data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
- data/benchmark/measure_ruby_wasm_startup.rb +43 -0
- data/benchmark/measure_wasi_write.rb +75 -0
- data/benchmark/optimization_worker.rb +23 -0
- data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
- data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
- data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
- data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
- data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
- data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
- data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
- data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
- data/benchmark/results/efflorescence-m1-cruby.json +799 -0
- data/benchmark/results/efflorescence-m1-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
- data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
- data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-memory.json +408 -0
- data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
- data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
- data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
- data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
- data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
- data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
- data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
- data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
- data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
- data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
- data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
- data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
- data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
- data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
- data/benchmark/summarize_call_profile.py +54 -0
- data/benchmark/validate_m1_eh.rb +34 -0
- data/benchmark/validate_m2.rb +119 -0
- data/benchmark/validate_m3.rb +44 -0
- data/benchmark/validate_m4.rb +95 -0
- data/benchmark/validate_m5.rb +108 -0
- data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
- data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
- data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
- data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
- data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
- data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
- data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
- data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
- data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
- data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
- data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
- data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
- data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
- data/docs/efflorescence-call-arities.md +115 -0
- data/docs/efflorescence-call-investigation.md +155 -0
- data/docs/efflorescence-call-transfer.md +128 -0
- data/docs/efflorescence-call-types.md +102 -0
- data/docs/efflorescence-coverage-ja.md +351 -0
- data/docs/efflorescence-generated-c-guards.md +410 -0
- data/docs/efflorescence-inlining.md +165 -0
- data/docs/efflorescence-ja.md +155 -0
- data/docs/efflorescence-m1.md +160 -0
- data/docs/efflorescence-m2.md +233 -0
- data/docs/efflorescence-m3.md +131 -0
- data/docs/efflorescence-m4.md +88 -0
- data/docs/efflorescence-m5.md +82 -0
- data/docs/efflorescence-m6.md +95 -0
- data/docs/efflorescence-mir2-cache.md +123 -0
- data/docs/efflorescence-operand-decoding.md +105 -0
- data/docs/efflorescence-phase2-ja.md +225 -0
- data/docs/efflorescence-phase2.md +240 -0
- data/docs/efflorescence-register-initialization.md +75 -0
- data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
- data/docs/efflorescence-spinel-linear-memory.md +72 -0
- data/docs/efflorescence-startup-snapshot.md +60 -0
- data/docs/efflorescence-unboxed-globals.md +61 -0
- data/docs/efflorescence.md +144 -0
- data/docs/spinel-string-aset-inplace-proposal.md +200 -0
- data/lib/wardite/alu_f32.generated.rb +11 -2
- data/lib/wardite/alu_f64.generated.rb +11 -2
- data/lib/wardite/alu_i32.generated.rb +54 -17
- data/lib/wardite/alu_i64.generated.rb +73 -31
- data/lib/wardite/cli/cli.rb +112 -29
- data/lib/wardite/const.rb +1 -0
- data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
- data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
- data/lib/wardite/efflorescence/inspection.rb +165 -0
- data/lib/wardite/efflorescence/ir.rb +1164 -0
- data/lib/wardite/efflorescence/mir_cache.rb +657 -0
- data/lib/wardite/efflorescence/optimizer.rb +580 -0
- data/lib/wardite/efflorescence/register_validation.rb +362 -0
- data/lib/wardite/efflorescence/snapshot.rb +229 -0
- data/lib/wardite/efflorescence/store_access.rb +366 -0
- data/lib/wardite/efflorescence/translator.rb +866 -0
- data/lib/wardite/efflorescence/vm.rb +1170 -0
- data/lib/wardite/efflorescence/wasi.rb +52 -0
- data/lib/wardite/efflorescence.rb +17 -0
- data/lib/wardite/errors.rb +20 -0
- data/lib/wardite/float_bits.rb +46 -0
- data/lib/wardite/instruction.rb +35 -4
- data/lib/wardite/leb128.rb +33 -12
- data/lib/wardite/load.rb +292 -143
- data/lib/wardite/value.rb +169 -175
- data/lib/wardite/version.rb +1 -1
- data/lib/wardite/wasi/consts.rb +1 -0
- data/lib/wardite/wasi/spinel.rb +41 -0
- data/lib/wardite/wasi.rb +483 -268
- data/lib/wardite/wasm_module.rb +6 -3
- data/lib/wardite.rb +360 -98
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +121 -21
- data/scripts/verify_ruby_wasm.rb +70 -0
- data/sig/generated/wardite/cli/cli.rbs +23 -6
- data/sig/generated/wardite/const.rbs +2 -0
- data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
- data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
- data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
- data/sig/generated/wardite/efflorescence/ir.rbs +888 -0
- data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
- data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +104 -0
- data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +145 -0
- data/sig/generated/wardite/efflorescence/translator.rbs +218 -0
- data/sig/generated/wardite/efflorescence/vm.rbs +240 -0
- data/sig/generated/wardite/efflorescence/wasi.rbs +34 -0
- data/sig/generated/wardite/errors.rbs +12 -0
- data/sig/generated/wardite/float_bits.rbs +30 -0
- data/sig/generated/wardite/instruction.rbs +22 -1
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +59 -7
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite/wasi/spinel.rbs +15 -0
- data/sig/generated/wardite/wasi.rbs +111 -4
- data/sig/generated/wardite.rbs +114 -13
- data/sig/mir_cache_stdlib.rbs +5 -0
- data/sig/vernier.rbs +2 -2
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
- data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
- data/spinel/spin.toml +7 -0
- metadata +154 -3
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Efflorescence VM: 未使用オペランドを読み出さない
|
|
2
|
+
|
|
3
|
+
2026-11-04、`-O2` 固定で、flat実行器のオペランド読み出しを比較した。
|
|
4
|
+
対照は直前の `5924537`。`call_mix` の実行時間は4.4%、通常CLI全体では6.4%短縮した。
|
|
5
|
+
|
|
6
|
+
## 変更点
|
|
7
|
+
|
|
8
|
+
以前は、どの命令でもopcode・a・b・cの4ワードを読み出してから分岐していた。
|
|
9
|
+
現在は共通部分でopcodeとaを読み、b/cは必要な命令だけが取得する。
|
|
10
|
+
|
|
11
|
+
| 命令 | 以前の読み出し数 | 現在の読み出し数 |
|
|
12
|
+
| --- | ---: | ---: |
|
|
13
|
+
| RETURN / BR | 4 | 2 |
|
|
14
|
+
| COPY / I32_EQZ / BR_IF / BR_IF_ZERO | 4 | 3 |
|
|
15
|
+
| 算術 / CALL / PROFILE | 4 | 4 |
|
|
16
|
+
| TRAP | 4 | 2 |
|
|
17
|
+
|
|
18
|
+
TRAPでも共通ヘッダーのaは取得するが、使わないb/cは読まない。
|
|
19
|
+
読み出す値には従来どおり `Array#fetch` を使い、境界チェックを維持する。
|
|
20
|
+
命令幅は4ワードのままで、分岐先・レジスタ番号・CALLの引数ソース表現を変更しない。
|
|
21
|
+
|
|
22
|
+
PCは通常命令の処理後に4進める。分岐を取る場合、CALL、RETURNはPCを設定して次の反復へ移るため、共通の加算を通らない。
|
|
23
|
+
CALLは呼び出し元の復帰先を `pc + 4` として保存する。semantic実行器は変更していない。
|
|
24
|
+
|
|
25
|
+
## 先に比較した実装
|
|
26
|
+
|
|
27
|
+
最初は、共通部分でPCを先に4進め、各命令が `pc - 2` / `pc - 1` からb/cを取得する形にした。
|
|
28
|
+
100万回・ウォームアップ1回後に3回の探索的測定では、call_mixが93.843 → 93.206 msで差が小さく、inline対照は56.755 → 57.954 msと遅くなった。
|
|
29
|
+
|
|
30
|
+
読み出してからPCを進める形では、同条件の別の対照測定でcall_mixが93.322 → 87.422 msだった。
|
|
31
|
+
この版を以下の長い測定に使った。行や実験をまたぐ絶対時間の比較は避ける。
|
|
32
|
+
[両実験の全サンプルと、5924537に対する各パッチ](../benchmark/results/operand_decode_exploratory_2026-11-04.json)を保存した。
|
|
33
|
+
|
|
34
|
+
## Spinelの比較
|
|
35
|
+
|
|
36
|
+
macOS arm64、Spinel `2026.10.25+1967 (6e7fc1355)`。
|
|
37
|
+
両版とも明示的に `SPINELFLAGS=-O2` とし、通常の整数overflowチェックを使う。
|
|
38
|
+
5,000,000 rounds、seed 123、ウォームアップ2回後に7回測定した。単位ms、平均 ± 標本標準偏差。
|
|
39
|
+
|
|
40
|
+
| export | 改修前 | 改修後 | 実行時間の短縮 |
|
|
41
|
+
| --- | ---: | ---: | ---: |
|
|
42
|
+
| `empty_loop` | 104.594 ± 2.270 | 94.384 ± 2.716 | 9.8% |
|
|
43
|
+
| `call_identity` | 280.698 ± 4.836 | **257.311 ± 4.999** | **8.3%** |
|
|
44
|
+
| `call_add_one` | 320.028 ± 7.529 | **302.792 ± 4.635** | **5.4%** |
|
|
45
|
+
| `inline_mix` | 290.618 ± 7.831 | 287.863 ± 15.656 | 0.9% |
|
|
46
|
+
| `call_mix` | 482.231 ± 12.362 | **460.842 ± 5.859** | **4.4%** |
|
|
47
|
+
|
|
48
|
+
inlineの差は測定のばらつきの範囲であり、改善を主張しない。
|
|
49
|
+
通常CLI全体の平均は、inlineが300.856 → 293.664 ms、callが499.032 → 467.310 msだった。
|
|
50
|
+
CLIとworkerは別エントリポイントであり、Spinelのコード生成も異なるため、差をそのまま起動・ロード時間とは解釈しない。
|
|
51
|
+
|
|
52
|
+
全サンプルのchecksumは独立した参照計算と一致した。mixは `1647235310`。
|
|
53
|
+
MIR命令数・COPY数・仮想レジスタの読み書き数・フレーム生成数は両版で一致し、詳細カウンタは別のプロセスで取得する。
|
|
54
|
+
[全サンプル、コンパイラ設定、ソース・バイナリのSHA256](../benchmark/results/operand_decode_spinel_2026-11-04.json)を保存している。
|
|
55
|
+
|
|
56
|
+
## 読み出し削減の確認
|
|
57
|
+
|
|
58
|
+
Ruby上では、整数だけを保持するArrayの派生クラスで `fetch` を記録し、実際のワード読み出し位置をテストした。
|
|
59
|
+
RETURN / BRでb/c、COPY / EQZ / 条件分岐でcを読まないこと、CALLから復帰して次の命令を実行すること、PROFILEが3オペランドすべてを読むことを確認する。
|
|
60
|
+
|
|
61
|
+
mix全体も0・1・10回で記録し、読み出し数がinlineで `25 * rounds + 8`、callで `31 * rounds + 8` となることを確認した。
|
|
62
|
+
以前はすべての命令で4ワード取得していたため、それぞれ `28 * rounds + 12`、`36 * rounds + 12` だった。
|
|
63
|
+
5,000,000回の同じ制御フローでは、inlineが140,000,012 → 125,000,008、callが180,000,012 → 155,000,008ワードとなる。
|
|
64
|
+
大きな回数の値は式から求めたものであり、時間測定中にカウンタを入れたものではない。仮想レジスタへのアクセス数とも区別する。
|
|
65
|
+
|
|
66
|
+
生成Cでも共通部分の命令バッファ参照が4回から2回になったことを確認した。
|
|
67
|
+
Frame・スタック・pool・CallSite表の型情報と、flat実行ループ内の多相呼び出し0箇所も維持している。
|
|
68
|
+
|
|
69
|
+
## CRuby / YJITと検証
|
|
70
|
+
|
|
71
|
+
CRuby 4.0.9、100,000 rounds、seed 123、ウォームアップ1回後に3回の探索的な比較をした。単位ms。
|
|
72
|
+
|
|
73
|
+
| 対象 | 改修前 | 改修後 |
|
|
74
|
+
| --- | ---: | ---: |
|
|
75
|
+
| CRuby / inline_mix | 83.483 ± 0.267 | 75.610 ± 0.415 |
|
|
76
|
+
| CRuby / call_mix | 137.440 ± 0.712 | 125.597 ± 0.675 |
|
|
77
|
+
| YJIT / inline_mix | 38.775 ± 0.128 | 35.742 ± 0.118 |
|
|
78
|
+
| YJIT / call_mix | 59.639 ± 0.177 | 54.484 ± 0.562 |
|
|
79
|
+
|
|
80
|
+
サンプル数が少ないため、一般的な改善率は断定しない。[全サンプル](../benchmark/results/operand_decode_ruby_2026-11-04.json)を保存した。
|
|
81
|
+
|
|
82
|
+
ネイティブSpinelを含む全体で **105 tests / 5,741 assertions、失敗・エラー・省略0**。
|
|
83
|
+
型チェックと `-O2` のSpinelビルドも成功した。
|
|
84
|
+
新しい読み出しテストに加え、元の受け入れベンチマーク全入力、各最適化の個別無効化、分岐の両経路、0・複数引数、再帰・相互再帰・再入・trap後の再実行も検証する。
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
BASELINE_REF=5924537 SPINELFLAGS=-O2 RUNS=7 WARMUPS=2 \
|
|
88
|
+
ruby benchmark/compare_call_types.rb 5000000 123
|
|
89
|
+
|
|
90
|
+
RUNTIMES=CRuby,YJIT BASELINE_REF=5924537 SPINELFLAGS=-O2 RUNS=3 WARMUPS=1 \
|
|
91
|
+
ruby benchmark/compare_call_types.rb 100000 123
|
|
92
|
+
|
|
93
|
+
SPINELFLAGS=-O2 make spinel
|
|
94
|
+
WARDITE_SPINEL_BIN="$PWD/build/wardite" bundle exec rake test
|
|
95
|
+
bundle exec rake check
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
対照のコミットはローカルに必要。通常の `ruby benchmark/compare.rb` にも改修は反映される。
|
|
99
|
+
|
|
100
|
+
## インライン化について
|
|
101
|
+
|
|
102
|
+
今回の変更は関数のインライン化を行わない。
|
|
103
|
+
Wasmtimeには[関数インライン化の設定](https://github.com/bytecodealliance/wasmtime/blob/v49.0.0/crates/wasmtime/src/config.rs)があり、手元の49.0.0でも `wasmtime -C help` に `inlining` の指定があることを確認した。
|
|
104
|
+
ただし、このベンチマークでmixへのインライン化が実際に適用されているかは未確認。
|
|
105
|
+
Efflorescenceに導入する場合は、関数呼び出し自体の改善と区別できるよう、独立パスとして有効・無効を比較する。
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# Efflorescence VM — Phase 2
|
|
2
|
+
|
|
3
|
+
[English](efflorescence-phase2.md) · [Phase 1](efflorescence-ja.md)
|
|
4
|
+
|
|
5
|
+
Phase 2では、MIRを実行するときのオーバーヘッドを減らします。
|
|
6
|
+
Warditeのデフォルトは引き続き既存のスタックVMです。
|
|
7
|
+
`--new-engine` でEfflorescenceを選択すると、以下の最適化が有効になります。
|
|
8
|
+
現在の命令対応範囲は[M2の実装記録](efflorescence-m2.md)を参照してください。到達不能なコードやエクスポートされていない関数も含め、
|
|
9
|
+
未対応命令は引き続き明示的な例外になります。
|
|
10
|
+
|
|
11
|
+
## パイプラインと切り替え
|
|
12
|
+
|
|
13
|
+
```text
|
|
14
|
+
Wasm → Translator → 意味論を表すMIR(保持)
|
|
15
|
+
→ CopyPropagation
|
|
16
|
+
→ ImmediateSpecialization
|
|
17
|
+
→ BranchFusion
|
|
18
|
+
→ Superinstructions
|
|
19
|
+
→ Inlining → 変更した関数にローカルなパスを再適用
|
|
20
|
+
→ 最適化済みMIR(保持)
|
|
21
|
+
→ 必要に応じて計測命令を挿入
|
|
22
|
+
→ Lowerer → 実行用の整数配列 → VM
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
EH・参照・Storeアクセスのある関数は、ローカルなパスと双方向のinliningから保守的に除外します。
|
|
26
|
+
検証、flat変換、profiling、Frame再利用はその関数にも適用します。
|
|
27
|
+
|
|
28
|
+
ローカルなパスは関数単位で動作し、Inliningはモジュールのスナップショットを参照します。`Optimizer` は元のMIRをコピーしてから変更するため、
|
|
29
|
+
変換直後のMIRはそのまま残ります。
|
|
30
|
+
`Rewrite` は変更前後の命令位置の対応表を作り、命令列を書き換えるたびに分岐先を補正します。
|
|
31
|
+
`Analysis` はBasic Blockの入口を求め、関数全体でレジスタの定義回数と使用回数を数えます。
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
# デフォルトの最適化済み実行
|
|
35
|
+
ruby -Ilib exe/wardite --new-engine --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 100000 123
|
|
36
|
+
|
|
37
|
+
# 最適化を個別に無効化。複数指定する場合はオプションを繰り返す
|
|
38
|
+
ruby -Ilib exe/wardite --new-engine --mir-disable copy --mir-disable frames --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 100000 123
|
|
39
|
+
|
|
40
|
+
# 6つの最適化をすべて無効化したMIRをflatへ変換して実行
|
|
41
|
+
ruby -Ilib exe/wardite --new-engine --mir-disable all --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 100000 123
|
|
42
|
+
|
|
43
|
+
# 最適化前後のMIR表示と詳細計測。性能計測とは別に実行する
|
|
44
|
+
ruby -Ilib exe/wardite --new-engine --mir-dump --mir-profile --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 10 123
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`copy`、`immediate`、`branch`、`superinstruction`、`inline`、`frames` を個別に無効化できます。
|
|
48
|
+
不明な名前を指定するとエラーになります。
|
|
49
|
+
MIR用のCLIオプションには `--new-engine` が必要です。
|
|
50
|
+
表示・計測オプションを明示的に指定しなければ、checksumの出力形式は変わりません。
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
instance = Wardite.new(path: "build/benchmark/i32_mix.wasm",
|
|
54
|
+
enable_wasi: false, engine: :efflorescence,
|
|
55
|
+
disabled_optimizations: [:superinstruction], mir_profile: true)
|
|
56
|
+
vm = instance.runtime
|
|
57
|
+
vm.call("call_mix", [1000, 123])
|
|
58
|
+
puts vm.dump_mir
|
|
59
|
+
puts vm.metrics.report
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`BinaryLoader.load_from_buffer` でも同じキーワード引数を使えます。
|
|
63
|
+
`VM.new` を直接呼ぶ場合は、`disabled_optimizations:` と `profile:` を指定します。
|
|
64
|
+
|
|
65
|
+
## 最適化の内容と正しさを保つ条件
|
|
66
|
+
|
|
67
|
+
| 最適化 | 実装内容 | 正しさを保つ条件 |
|
|
68
|
+
| --- | --- | --- |
|
|
69
|
+
| COPY削減 | 直後のCOPYの格納先へ演算結果を直接書き込む。同じBasic Block内でコピー元を伝播し、自己コピー・読み出されないコピー・読み出す前に上書きされるコピーを削除する | 演算結果の一時レジスタは、関数全体で定義・使用がそれぞれ1回であること。吸収するCOPYへ途中から分岐できないこと。値ソースの対応表はBasic Blockの境界でリセットし、参照元を上書きしたら対応を無効化する。COPYの生存解析では、分岐の先ですべてのレジスタが使われると保守的に扱う |
|
|
70
|
+
| 即値化 | ADD/SUB/MUL/XOR/SHL/SHR_Uのオペランドに、書き換えない定数レジスタの値を直接埋め込む | 左辺の定数を右辺へ移してよいのはADD/MUL/XORだけ。シフト量のマスクと、16bitごとに分割する乗算処理は維持する |
|
|
71
|
+
| 分岐融合 | `I32_EQZ tmp, src; BR_IF tmp, pc` を `BR_IF_ZERO src, pc` にする | 一時レジスタがローカル変数ではなく、定義・使用が各1回であること。2命令目へ途中から分岐できないこと |
|
|
72
|
+
| Superinstruction | 即値による論理右シフト/左シフトと、元の値とのXORを融合する | 中間値の定義・使用が各1回で、XORがBasic Blockの入口ではないこと。ベンチマークの関数全体を置き換える専用命令は使わない |
|
|
73
|
+
| Inlining | 短い直線状のleaf関数を展開し、有効なローカルパスを再適用する | 呼び出し先は8命令・32スロット以内。レジスタを新しく割り当て、引数のスナップショットを保ち、各呼び出し位置でローカルを0初期化する。分岐・trap・内部CALLは対象外。1回のスナップショットに限定し、呼び出し元ごとの増加を整理前で64命令・256スロット以内に抑える。`--mir-disable inline` で無効化できる |
|
|
74
|
+
| フラット化(常時実行) | opcodeとオペランド3個を、固定幅4の整数配列に並べる | 分岐先の命令番号は4倍して配列上のオフセットに変換する。CALLの引数は別の `CallSite` に保持し、解決済みの呼び出し先と引数レジスタ配列を参照するため、引数の数は固定幅に制限されない |
|
|
75
|
+
| フレーム再利用 | 1回の公開API呼び出しが所有する、関数ごとの空きフレーム一覧を使う | 実行中のフレームは再利用しない。初回はレジスタのテンプレートを複製し、再利用時は非引数ローカルだけを0に戻してPC・戻り値の格納先を初期化する。引数はCALLで設定し、定数は保持する。一時値はロード時の定義解析で使用前の代入を保証する。再帰や重なり合う公開API呼び出しでも、実行中のフレームは独立させる |
|
|
76
|
+
|
|
77
|
+
実行器はflatに統一し、`--mir-disable all` の場合もLowererによる変換を必ず行います。
|
|
78
|
+
`flat`の最適化スイッチとsemantic実行器は削除しました。
|
|
79
|
+
意味MIRと最適化済みMIRは、検証・書き換え・表示用に保持します。
|
|
80
|
+
`--mir-disable all` は6つの任意の機能を無効化してからflatへ変換するモードであり、
|
|
81
|
+
Phase 1のバイナリそのものではありません。以下の過去の計測値は当時の設定による記録です。
|
|
82
|
+
比較スクリプトでは、コミット `67fbfae` を独立してビルドしたものも基準として使えます。
|
|
83
|
+
|
|
84
|
+
flat実行器には `while true` を使い、Spinelによる `Kernel#loop` のStopIteration捕捉と、
|
|
85
|
+
それに伴うvolatile変数の生成を避けます。StopIterationを含む命令実行中の例外はRubyの呼び出し元へ伝播します。
|
|
86
|
+
命令フェッチの `Array#fetch` と境界検査、整数overflowチェックは維持します。
|
|
87
|
+
CIで使う未改造のSpinelで動作します。fetchの重複GCルートを省くSpinelパッチは、独立したコンパイラ側の実験です。
|
|
88
|
+
[生成Cの調査と測定結果](efflorescence-generated-c-guards.md)に詳細を記録しています。
|
|
89
|
+
|
|
90
|
+
## MIRの表示と計測
|
|
91
|
+
|
|
92
|
+
`VM#semantic_ir` は変換直後のMIR、`optimized_ir` は最適化パスを適用したMIRです。
|
|
93
|
+
`ir` は実際に実行するMIRで、詳細計測を指定した場合はPROFILE命令も含みます。
|
|
94
|
+
フラットな命令列は `Function#bytecode` に保持します。
|
|
95
|
+
通常実行では、MIR命令ごとに計測フラグを判定したり、カウンタを更新したりしません。
|
|
96
|
+
|
|
97
|
+
詳細計測では、各MIR命令の前にPROFILE命令を挿入し、分岐先もその計測命令に合わせて補正します。
|
|
98
|
+
ディスパッチ回数には、計測用命令自身の実行は含めません。
|
|
99
|
+
レジスタの読み書き回数はMIRのオペランドアクセスを数えたもので、内部呼び出しの引数コピーと
|
|
100
|
+
戻り値の格納は含みますが、フレーム初期化・Rubyとの境界での準備・命令の読み出しは含みません。
|
|
101
|
+
ハードウェア上のメモリアクセス回数ではありません。
|
|
102
|
+
`metrics` は直近の公開API呼び出しの結果を保持します。
|
|
103
|
+
性能を測るプロセスでは、詳細計測を有効にしません。
|
|
104
|
+
|
|
105
|
+
以下はインライン化を追加する前のPhase 2初期のカウンタです。
|
|
106
|
+
`rounds=100000, seed=123` では、両方のエクスポート関数が符号なし整数 `3870791521` を返します。
|
|
107
|
+
|
|
108
|
+
| 指標 | inline 最適化なし | inline 最適化あり | call 最適化なし | call 最適化あり |
|
|
109
|
+
| --- | ---: | ---: | ---: | ---: |
|
|
110
|
+
| ディスパッチ回数 | 1,300,005 | 700,003 | 1,600,005 | 900,003 |
|
|
111
|
+
| COPY実行回数 | 300,002 | 1 | 400,002 | 1 |
|
|
112
|
+
| レジスタ読み出し | 1,900,005 | 600,003 | 2,200,005 | 800,003 |
|
|
113
|
+
| レジスタ書き込み | 1,100,003 | 500,001 | 1,400,003 | 700,001 |
|
|
114
|
+
| Frame生成数 | 1 | 1 | 100,001 | 2 |
|
|
115
|
+
|
|
116
|
+
3関数全体の静的なMIR命令数は35→20、COPY命令数は11→2になります。
|
|
117
|
+
レジスタ数は37のままです。この段階では、使わなくなった一時値や定数のスロットは詰め直しません。
|
|
118
|
+
現在はインライン化により、静的MIRが23命令・49スロットになり、`call_mix` のディスパッチは
|
|
119
|
+
`inline_mix` と同じ700,003回、Frame生成と関数への入場は各1回です。
|
|
120
|
+
[インライン化の計測と制約](efflorescence-inlining.md)を参照してください。
|
|
121
|
+
|
|
122
|
+
Spinelの生成Cコードも、以下の手順で確認しています。
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
spinel -I lib --strict-require -O2 -c exe/wardite -o build/benchmark/phase2-wardite.c
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
フラットな命令列とフレームのレジスタは、どちらも `sp_IntArray*` になっています。
|
|
129
|
+
オペランドの読み出しには `sp_IntArray_get` を使い、ディスパッチ時には `Instruction#[]` を呼び出しません。
|
|
130
|
+
オブジェクトのメタデータや、Frame/CallSiteの一覧には汎用配列が残る場合があります。
|
|
131
|
+
|
|
132
|
+
### 整数リテラルによるディスパッチ
|
|
133
|
+
|
|
134
|
+
実行ループでは、意図的に `when Opcode::COPY` ではなく
|
|
135
|
+
`when 0 # Opcode::COPY` と書いています。
|
|
136
|
+
検証したSpinelの[整数caseの高速化処理(`emit_case`)](https://github.com/matz/spinel/blob/master/src/codegen_stmt.c#L11032-L11101)は、
|
|
137
|
+
AST上の整数リテラルを条件にしています。値が整数であっても、定数の参照はこの条件を満たしません。
|
|
138
|
+
整数リテラルにすると、定数との比較を並べた処理から、opcode 0〜22を扱うCの `switch` に変わります。
|
|
139
|
+
|
|
140
|
+
MIRの生成と最適化パスでは、引き続きOpcode定数を使います。
|
|
141
|
+
ディスパッチの各ラベルにはOpcode名をコメントで残し、数値と定数の一致、全opcodeへの対応を回帰テストで確認します。
|
|
142
|
+
ネイティブテストではCLIをCに変換し、flat実行器で `switch` が生成されることも確認します。
|
|
143
|
+
Phase 2の追加変更時点(当時は両実行器が存在)の検証では、**88テスト、4,466 assertions、失敗・エラー・省略はいずれも0件**でした。
|
|
144
|
+
型チェックも成功しています。
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
ruby benchmark/compare_dispatch.rb 100000 123
|
|
148
|
+
# Spinelだけを対象に、必要なら処理量を増やして計測
|
|
149
|
+
RUNTIMES=Spinel ruby benchmark/compare_dispatch.rb 1000000 123
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
この比較では、現在のソースの一時コピーから定数ラベルのディスパッチを再現し、独立してビルドします。
|
|
153
|
+
ほかの最適化と公開APIの呼び出し境界は同じに保ち、定数版と整数リテラル版を交互の順序で計測します。
|
|
154
|
+
計測前と、各サンプルの実行時にchecksumを照合します。
|
|
155
|
+
結果は `build/benchmark/dispatch.json` に保存し、一時的な比較用ソースは計測後に削除します。
|
|
156
|
+
[ディスパッチの比較結果](../benchmark/README.md#integer-literal-dispatch)も記録しています。
|
|
157
|
+
Spinelで処理量を増やした計測(`rounds=1000000, seed=123`)では、実行部分が
|
|
158
|
+
inlineで100.43→90.68 ms、callで229.76→209.93 msとなり、それぞれ9.7%、8.6%短縮しました。
|
|
159
|
+
ウォームアップ2回の後に、5回計測した平均値です。
|
|
160
|
+
|
|
161
|
+
## 検証とベンチマーク
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
bundle exec rake check
|
|
165
|
+
make spinel
|
|
166
|
+
WARDITE_SPINEL_BIN="$PWD/build/wardite" bundle exec rake test
|
|
167
|
+
ruby benchmark/compare_optimizations.rb 100000 123
|
|
168
|
+
ABLATIONS=1 RUNS=3 WARMUPS=1 ruby benchmark/compare_optimizations.rb 100000 123
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
受け入れ用WAT全体を変更せずに使い、元の8組すべての入力を、デフォルト設定、各最適化の個別無効化、
|
|
172
|
+
全最適化の無効化で実行します。CRubyとSpinelの両方を対象にしています。
|
|
173
|
+
追加のテストでは、上書き・分岐・呼び出し・ループをまたぐスナップショット、融合候補の途中への分岐、
|
|
174
|
+
後で使われる中間値、符号なし整数の乗算、シフト量のマスク、オペランド順序、再利用時のローカル変数初期化、
|
|
175
|
+
再帰、公開API呼び出しの重なり、カウンタの正確な値を確認します。
|
|
176
|
+
既存のスタックVMのテストも、引き続き実行します。
|
|
177
|
+
|
|
178
|
+
その後の[フレーム初期化の軽量化](efflorescence-register-initialization.md)では、合流点・バックエッジを含む
|
|
179
|
+
レジスタの定義解析も追加しました。この改修後の全体検証は、Spinelネイティブ版を含めて95テスト・5,009 assertionsが成功しています。
|
|
180
|
+
続く[CALL/RETURNの型情報を保つ改修](efflorescence-call-types.md)では、生成CでFrame・pool・CallSite表の型を具体化しています。
|
|
181
|
+
MIR命令は変更せず、96テスト・5,021 assertionsが成功しました。
|
|
182
|
+
続く[引数搬送の軽量化](efflorescence-call-transfer.md)では、flat CALLの第4スロットを1引数のソース番号に使い、
|
|
183
|
+
一般の引数コピーを実行器の外へ分離し、poolのインデックスを保持します。99テスト・5,703 assertionsが成功しました。
|
|
184
|
+
続く[オペランド読み出しの軽量化](efflorescence-operand-decoding.md)では、4ワードのPC表現を保ちながら使わないb/cを読み出さなくしました。
|
|
185
|
+
`-O2` 固定で比較し、105テスト・5,741 assertionsが成功しています。
|
|
186
|
+
続く[小さな関数のインライン化](efflorescence-inlining.md)では、独立パスと有効・無効の比較を追加し、
|
|
187
|
+
112テスト・9,028 assertionsが成功しています。
|
|
188
|
+
|
|
189
|
+
2026年11月4日に、CRuby 4.0.9でSpinelのネイティブテストも有効にして全体を検証し、
|
|
190
|
+
**88テスト、4,466 assertions、失敗・エラー・省略はいずれも0件**でした。
|
|
191
|
+
`bundle exec rake check` も成功しています。
|
|
192
|
+
Spinelは `2026.10.25+1967`(`6e7fc1355`)を使い、`-O2` とデフォルトの整数オーバーフローチェックでビルドしました。
|
|
193
|
+
[Phase 1との性能比較と、各最適化を無効化した場合の計測結果](../benchmark/README.md#phase-2-sample-2026-11-04)を記録しています。
|
|
194
|
+
デフォルト設定の採用判断ではSpinelでの効果を重視しています。
|
|
195
|
+
CRubyやYJITでは、個々の最適化が同じ程度に効くとは限りません。
|
|
196
|
+
|
|
197
|
+
比較スクリプトは、計測対象のすべてのCLIと実行時間計測用プログラムについて、
|
|
198
|
+
剰余演算に基づく独立参照実装との一致を確認してから時間を測ります。
|
|
199
|
+
CLIプロセス全体の時間に加え、ロード・MIR変換・実行の各時間と、CRubyでの実行中のallocation数を記録します。
|
|
200
|
+
時間計測用プログラムはCLIと同じ `call` APIを使い、Rubyへ返す際のboxingも含めます。
|
|
201
|
+
Spinelのallocation数はCRubyから推定せず、取得不能として扱います。
|
|
202
|
+
生のサンプル、平均値、opcodeごとのカウンタ、静的なMIRの概要は
|
|
203
|
+
`build/benchmark/optimizations.json` に保存します。
|
|
204
|
+
`ABLATIONS=1` で、各最適化を1つずつ無効化した構成も比較できます。
|
|
205
|
+
|
|
206
|
+
各処理の時間は、CLIとは別にコンパイルしたプログラムで計測するため、足し合わせてもCLI全体の時間とは一致しません。
|
|
207
|
+
Spinelの型推論結果も、入口のプログラムが異なることで変わる場合があります。
|
|
208
|
+
ウォームアップは別プロセスで行うため、YJITのコンパイルもサンプルごとに発生します。
|
|
209
|
+
デフォルトでは、ウォームアップ2回の後に5回計測します。
|
|
210
|
+
|
|
211
|
+
Phase 1そのものと比較する場合は、コミット `67fbfae` のソースを別ディレクトリに用意してビルドし、
|
|
212
|
+
`PHASE1_ROOT`、`PHASE1_BIN`、`PHASE1_WORKER` を指定してください。
|
|
213
|
+
手順は[ベンチマークの説明](../benchmark/README.md)にあります。
|
|
214
|
+
|
|
215
|
+
## 今後の実験
|
|
216
|
+
|
|
217
|
+
アキュムレータ、Basic Block単位のディスパッチ、定数畳み込み、レジスタの詰め直し、共有レジスタスタック、
|
|
218
|
+
Ruby/ネイティブコード生成は実装していません。今回作った独立したパスの仕組みを使い、今後試せます。
|
|
219
|
+
対応するWasm命令の拡張は別の作業です。新しい命令を最適化の対象にする前に、
|
|
220
|
+
その命令のレジスタの読み書きと制御フローへの影響を定義する必要があります。
|
|
221
|
+
|
|
222
|
+
設計では、[WasmKitのレジスタ型インタプリタ](https://github.com/swiftwasm/WasmKit/blob/main/Documentation/RegisterMachine.md)と、
|
|
223
|
+
[Wasmi 2.0の最適化解説](https://wasmi-labs.github.io/blog/posts/wasmi-v2.0/)を参照しました。
|
|
224
|
+
これらのネイティブなディスパッチ方式やアキュムレータがRubyでも同じコストになるとは仮定せず、
|
|
225
|
+
今回実装した方式をCRubyとSpinelで検証しています。
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# Efflorescence VM — Phase 2
|
|
2
|
+
|
|
3
|
+
[日本語](efflorescence-phase2-ja.md) · [Phase 1](efflorescence.md)
|
|
4
|
+
|
|
5
|
+
Phase 2 reduces MIR execution overhead. The stack VM remains Wardite's default;
|
|
6
|
+
`--new-engine` selects Efflorescence with the optimizations below enabled.
|
|
7
|
+
For the current Wasm subset, see [M2](efflorescence-m2.md). Unsupported instructions still fail
|
|
8
|
+
explicitly, including in unreachable or unexported functions.
|
|
9
|
+
|
|
10
|
+
## Pipeline and controls
|
|
11
|
+
|
|
12
|
+
```text
|
|
13
|
+
Wasm → Translator → semantic MIR (retained)
|
|
14
|
+
→ CopyPropagation
|
|
15
|
+
→ ImmediateSpecialization
|
|
16
|
+
→ BranchFusion
|
|
17
|
+
→ Superinstructions
|
|
18
|
+
→ Inlining → local passes again on changed functions
|
|
19
|
+
→ optimized MIR (retained)
|
|
20
|
+
→ optional profiling instrumentation
|
|
21
|
+
→ Lowerer → executable integer array → VM
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
EH/reference/Store-access functions conservatively bypass local passes and both
|
|
25
|
+
directions of inlining. Validation, flat lowering, profiling and frame reuse
|
|
26
|
+
still apply.
|
|
27
|
+
|
|
28
|
+
Local passes operate on a function; inlining reads a module snapshot. `Optimizer` copies the semantic MIR before
|
|
29
|
+
changing it. `Rewrite` maps old instruction ordinals to new ones and repairs all
|
|
30
|
+
branch targets after each rewrite. `Analysis` identifies basic-block entries
|
|
31
|
+
and counts register definitions and uses across the whole function.
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
# Default optimized execution
|
|
35
|
+
ruby -Ilib exe/wardite --new-engine --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 100000 123
|
|
36
|
+
|
|
37
|
+
# Disable one feature, or several by repeating the option
|
|
38
|
+
ruby -Ilib exe/wardite --new-engine --mir-disable copy --mir-disable frames --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 100000 123
|
|
39
|
+
|
|
40
|
+
# Unoptimized MIR lowered to flat bytecode with all six features disabled
|
|
41
|
+
ruby -Ilib exe/wardite --new-engine --mir-disable all --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 100000 123
|
|
42
|
+
|
|
43
|
+
# Inspect before/after MIR, or collect counters separately from timing
|
|
44
|
+
ruby -Ilib exe/wardite --new-engine --mir-dump --mir-profile --no-wasi --invoke call_mix build/benchmark/i32_mix.wasm 10 123
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`copy`, `immediate`, `branch`, `superinstruction`, `inline` and `frames` can be
|
|
48
|
+
disabled independently. Unknown names raise an error. MIR CLI options require
|
|
49
|
+
`--new-engine`. The normal checksum output is unchanged unless inspection or
|
|
50
|
+
profiling is explicitly requested.
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
instance = Wardite.new(path: "build/benchmark/i32_mix.wasm",
|
|
54
|
+
enable_wasi: false, engine: :efflorescence,
|
|
55
|
+
disabled_optimizations: [:superinstruction], mir_profile: true)
|
|
56
|
+
vm = instance.runtime
|
|
57
|
+
vm.call("call_mix", [1000, 123])
|
|
58
|
+
puts vm.dump_mir
|
|
59
|
+
puts vm.metrics.report
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The same keywords are accepted by `BinaryLoader.load_from_buffer`.
|
|
63
|
+
Direct `VM.new` accepts `disabled_optimizations:` and `profile:`.
|
|
64
|
+
|
|
65
|
+
## Optimizations and safety
|
|
66
|
+
|
|
67
|
+
| Feature | Implementation | Correctness boundary |
|
|
68
|
+
| --- | --- | --- |
|
|
69
|
+
| COPY reduction | Redirect an adjacent producer into its COPY destination; propagate copies locally; remove self-copies, globally unread copies and copies overwritten before a read within a basic block | Producer temporaries must have one definition and one use. No incoming edge may target an absorbed COPY. Aliases reset at basic-block boundaries and are invalidated whenever their source is overwritten. Backward copy liveness conservatively treats all registers as live across edges. |
|
|
70
|
+
| Immediates | Embed immutable constant-register values in ADD/SUB/MUL/XOR/SHL/SHR_U operands | Only ADD/MUL/XOR may swap a constant left operand. Shift counts remain masked; multiplication still uses bounded 16-bit limbs. |
|
|
71
|
+
| Branch fusion | `I32_EQZ tmp, src; BR_IF tmp, pc` → `BR_IF_ZERO src, pc` | The temporary is not a local, has one definition/use, and the second instruction has no incoming edge. |
|
|
72
|
+
| Superinstructions | Fuse immediate logical-right/left shift followed by XOR with the original source | The intermediate has one definition/use and the XOR is not a basic-block entry. No benchmark-specific whole-function instruction is used. |
|
|
73
|
+
| Inlining | Expand small straight-line leaf functions, then clean up with the enabled local passes | At most 8 callee instructions and 32 slots. Fresh renamed registers preserve arguments; explicit local zeroing runs at each call site. No branches, traps or nested calls. One snapshot round, at most 64 added instructions and 256 added slots per caller before cleanup. Disable with `--mir-disable inline`. |
|
|
74
|
+
| Flat execution (always enabled) | Lower to four integers per instruction: opcode and three operands | Branch ordinals become offsets multiplied by four. CALL uses an out-of-line `CallSite` with the resolved callee and argument-register array, allowing any supported arity. |
|
|
75
|
+
| Frame reuse | Per-function free lists owned by one public invocation | Active frames are never pooled. New frames copy the register template; reused frames zero only non-parameter locals and reset PC/return destination. CALL supplies parameters, constants remain immutable, and load-time definite-assignment validation ensures temporaries are written before use. Recursion and overlapping invocations have independent active frames. |
|
|
76
|
+
|
|
77
|
+
Execution always uses the flat interpreter. Lowering is a required pipeline step,
|
|
78
|
+
including with `--mir-disable all`; `flat` is no longer an optimization switch.
|
|
79
|
+
The semantic executor has been removed. Semantic and optimized MIR are retained
|
|
80
|
+
for validation, rewriting and inspection. `--mir-disable all` disables the six
|
|
81
|
+
optional features before lowering; it does not reproduce the Phase 1 binary.
|
|
82
|
+
Historical benchmark results below used the configurations available at the time.
|
|
83
|
+
The benchmark can still run commit `67fbfae` as an independently built baseline.
|
|
84
|
+
|
|
85
|
+
The flat interpreter uses `while true` so Spinel does not generate
|
|
86
|
+
`Kernel#loop`'s StopIteration handler or the associated volatile locals.
|
|
87
|
+
Instruction exceptions, including StopIteration, propagate to the Ruby caller;
|
|
88
|
+
`Array#fetch` bounds checks and integer overflow checks remain enabled.
|
|
89
|
+
This works with the unmodified Spinel revision used by CI. Additional fetch-root
|
|
90
|
+
elision is a separate compiler experiment, not a Wardite dependency.
|
|
91
|
+
[Generated C investigation and measurements (日本語)](efflorescence-generated-c-guards.md).
|
|
92
|
+
|
|
93
|
+
## Inspection and metrics
|
|
94
|
+
|
|
95
|
+
`VM#semantic_ir` is the original translation. `optimized_ir` is the result of
|
|
96
|
+
the passes; `ir` is the executed IR, which additionally contains PROFILE
|
|
97
|
+
instructions when profiling is requested. Flat code is in `Function#bytecode`.
|
|
98
|
+
Normal execution does not test a profiling flag or update counters per MIR op.
|
|
99
|
+
|
|
100
|
+
Profiling inserts a PROFILE record before each MIR instruction and retargets
|
|
101
|
+
branches to those records. Counters exclude the instrumentation's own dispatches.
|
|
102
|
+
Register access counts model MIR operand reads/writes, including internal-call
|
|
103
|
+
argument copies and return destinations; they exclude frame initialization,
|
|
104
|
+
Ruby boundary setup and instruction fetches. They are not hardware memory-access
|
|
105
|
+
counters. `metrics` reports the most recent public invocation. Counter-enabled
|
|
106
|
+
execution is deliberately excluded from all timing measurements.
|
|
107
|
+
|
|
108
|
+
The following original Phase 2 counters predate inlining. At 100,000 rounds and seed 123, both exports return unsigned `3870791521`:
|
|
109
|
+
|
|
110
|
+
| Metric | Inline unoptimized | Inline optimized | Call unoptimized | Call optimized |
|
|
111
|
+
| --- | ---: | ---: | ---: | ---: |
|
|
112
|
+
| Dispatches | 1,300,005 | 700,003 | 1,600,005 | 900,003 |
|
|
113
|
+
| COPY executions | 300,002 | 1 | 400,002 | 1 |
|
|
114
|
+
| Register reads | 1,900,005 | 600,003 | 2,200,005 | 800,003 |
|
|
115
|
+
| Register writes | 1,100,003 | 500,001 | 1,400,003 | 700,001 |
|
|
116
|
+
| Frame allocations | 1 | 1 | 100,001 | 2 |
|
|
117
|
+
|
|
118
|
+
Across the three functions, static MIR falls from 35 to 20 instructions and
|
|
119
|
+
from 11 to 2 COPYs. Register count remains 37: unused temporary and constant
|
|
120
|
+
slots are not compacted in this phase.
|
|
121
|
+
With inlining, static MIR is now 23 instructions / 49 slots, and `call_mix`
|
|
122
|
+
has the same 700,003 dynamic dispatches as `inline_mix`, with one frame and
|
|
123
|
+
one function entry. See [inlining measurements and limits (日本語)](efflorescence-inlining.md).
|
|
124
|
+
|
|
125
|
+
Spinel's generated C was inspected with:
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
spinel -I lib --strict-require -O2 -c exe/wardite -o build/benchmark/phase2-wardite.c
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Both the flat code and frame registers are `sp_IntArray*`; operand fetches use
|
|
132
|
+
`sp_IntArray_get`, and dispatch performs no `Instruction#[]` calls. Object
|
|
133
|
+
metadata and frame/call-site collections may still use generic arrays.
|
|
134
|
+
|
|
135
|
+
### Integer-literal dispatch
|
|
136
|
+
|
|
137
|
+
The execution loop deliberately uses `when 0 # Opcode::COPY` rather than
|
|
138
|
+
`when Opcode::COPY`. In the tested Spinel version, the
|
|
139
|
+
[integer-case fast path in `emit_case`](https://github.com/matz/spinel/blob/master/src/codegen_stmt.c#L11032-L11101)
|
|
140
|
+
requires integer literal AST nodes. Constant references do not meet that
|
|
141
|
+
condition, even when their values are integers. The literal form produces a C
|
|
142
|
+
`switch` covering opcodes 0–22 instead of the constant-comparison chain.
|
|
143
|
+
|
|
144
|
+
Opcode constants remain the source of truth for MIR construction and optimizer
|
|
145
|
+
passes. Each dispatch label retains its Opcode name in a comment; a regression
|
|
146
|
+
test checks every literal against that constant and checks complete coverage.
|
|
147
|
+
The native suite additionally compiles the CLI to C and verifies the switches
|
|
148
|
+
in the flat executor. At the time of the Phase 2 follow-up (which still had
|
|
149
|
+
both executors), the full
|
|
150
|
+
suite passed **88 tests and 4,466 assertions**, with no failures/errors/omissions;
|
|
151
|
+
type checks also passed.
|
|
152
|
+
|
|
153
|
+
```sh
|
|
154
|
+
ruby benchmark/compare_dispatch.rb 100000 123
|
|
155
|
+
# Focus on Spinel, optionally increasing the workload:
|
|
156
|
+
RUNTIMES=Spinel ruby benchmark/compare_dispatch.rb 1000000 123
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The comparison reconstructs constant-label dispatch in a temporary copy of the
|
|
160
|
+
current source, builds it independently, and keeps all other optimizations and
|
|
161
|
+
the public call boundary identical. Paired constant/literal samples alternate
|
|
162
|
+
order. Checksums are verified before timing and for every measured call.
|
|
163
|
+
Results are saved to `build/benchmark/dispatch.json`; the temporary baseline is
|
|
164
|
+
removed after the run. See the [recorded dispatch comparison](../benchmark/README.md#integer-literal-dispatch).
|
|
165
|
+
In the longer Spinel run (1,000,000 rounds, seed 123), execution time fell from
|
|
166
|
+
100.43 to 90.68 ms for inline and from 229.76 to 209.93 ms for call: 9.7% and
|
|
167
|
+
8.6% reductions, respectively, over five samples after two warmups.
|
|
168
|
+
|
|
169
|
+
## Validation and benchmarking
|
|
170
|
+
|
|
171
|
+
```sh
|
|
172
|
+
bundle exec rake check
|
|
173
|
+
make spinel
|
|
174
|
+
WARDITE_SPINEL_BIN="$PWD/build/wardite" bundle exec rake test
|
|
175
|
+
ruby benchmark/compare_optimizations.rb 100000 123
|
|
176
|
+
ABLATIONS=1 RUNS=3 WARMUPS=1 ruby benchmark/compare_optimizations.rb 100000 123
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
The full unchanged acceptance WAT and all eight original inputs run with the
|
|
180
|
+
default optimizer, every feature independently disabled and all features
|
|
181
|
+
disabled, on CRuby and Spinel. Additional tests exercise snapshots across
|
|
182
|
+
overwrites/edges/calls/loops, incoming edges into candidate peepholes, live
|
|
183
|
+
intermediates, unsigned products, shift masks, operand order, frame-local
|
|
184
|
+
reinitialization, recursion, overlapping public calls and exact counter values.
|
|
185
|
+
The existing stack VM tests remain part of the suite.
|
|
186
|
+
|
|
187
|
+
The subsequent [selective frame initialization follow-up](efflorescence-register-initialization.md)
|
|
188
|
+
also validates register definitions across joins/backedges before execution.
|
|
189
|
+
Its full CRuby/native Spinel suite passed with 95 tests and 5,009 assertions.
|
|
190
|
+
The [CALL/RETURN type-preservation follow-up](efflorescence-call-types.md) keeps
|
|
191
|
+
frames, pools and resolved call sites concrete in Spinel's generated C. Its
|
|
192
|
+
suite passed with 96 tests and 5,021 assertions; the MIR opcode set is unchanged.
|
|
193
|
+
The [argument-transfer follow-up](efflorescence-call-transfer.md) uses the fourth
|
|
194
|
+
flat CALL word for a single source register (or -1 for the general path), keeps
|
|
195
|
+
generic copies outside the interpreter's live locals, and caches pool indices.
|
|
196
|
+
Its suite passed with 99 tests and 5,703 assertions.
|
|
197
|
+
The [operand-decoding follow-up](efflorescence-operand-decoding.md) skips unused
|
|
198
|
+
b/c padding while retaining four-word PCs and checked reads. At a fixed -O2,
|
|
199
|
+
its full CRuby/native Spinel suite passed with 105 tests and 5,741 assertions.
|
|
200
|
+
The [small-function inlining follow-up (日本語)](efflorescence-inlining.md) adds
|
|
201
|
+
an independent pass and same-binary off/on measurements; its full suite passed
|
|
202
|
+
with 112 tests and 9,028 assertions.
|
|
203
|
+
|
|
204
|
+
On 2026-11-04 the full suite passed on CRuby 4.0.9 with the Spinel native tests
|
|
205
|
+
enabled: **88 tests, 4,466 assertions, no failures/errors/omissions**.
|
|
206
|
+
`bundle exec rake check` also passed. Spinel was `2026.10.25+1967`
|
|
207
|
+
(`6e7fc1355`), built with `-O2` and default overflow checks.
|
|
208
|
+
[Recorded Phase 1/2 timings and feature-off measurements](../benchmark/README.md#phase-2-sample-2026-11-04).
|
|
209
|
+
Defaults prioritize the measured Spinel benefit; an individual feature need not
|
|
210
|
+
help CRuby/YJIT to the same extent.
|
|
211
|
+
|
|
212
|
+
The comparison script verifies every measured CLI and worker against an
|
|
213
|
+
independent modulo-based reference before timing. It measures CLI process time,
|
|
214
|
+
load/translation/execution phases, and CRuby's execution allocation count.
|
|
215
|
+
Workers use the same Ruby-facing `call` boundary as the CLI. Spinel allocation
|
|
216
|
+
counts are reported as unavailable, rather than estimated from CRuby.
|
|
217
|
+
Raw samples, means, per-opcode counters and static MIR summaries are saved to
|
|
218
|
+
`build/benchmark/optimizations.json`. `ABLATIONS=1` adds each-feature-off variants.
|
|
219
|
+
|
|
220
|
+
Phase timing uses a separately compiled worker, so phase values need not sum to
|
|
221
|
+
CLI time and Spinel type inference can differ between those entry points.
|
|
222
|
+
Warmups start separate processes; YJIT compilation occurs anew in every sample.
|
|
223
|
+
Default sample counts are five measured processes after two warmups.
|
|
224
|
+
|
|
225
|
+
For an independent Phase 1 comparison, first preserve/build sources from
|
|
226
|
+
`67fbfae` in a separate directory, then set `PHASE1_ROOT`, `PHASE1_BIN` and
|
|
227
|
+
`PHASE1_WORKER`; see [benchmark instructions](../benchmark/README.md).
|
|
228
|
+
|
|
229
|
+
## Remaining experiments
|
|
230
|
+
|
|
231
|
+
Accumulator execution, basic-block dispatch, constant folding, register
|
|
232
|
+
compaction, shared register stacks and Ruby/native code generation are not
|
|
233
|
+
implemented. The existing pass boundary is the place to experiment with them.
|
|
234
|
+
Wasm opcode coverage remains a separate task; a future opcode must first define
|
|
235
|
+
its operand reads/writes/control effects before an optimizer can handle it.
|
|
236
|
+
|
|
237
|
+
The design references were [WasmKit's register interpreter](https://github.com/swiftwasm/WasmKit/blob/main/Documentation/RegisterMachine.md)
|
|
238
|
+
and [Wasmi 2.0's optimization discussion](https://wasmi-labs.github.io/blog/posts/wasmi-v2.0/).
|
|
239
|
+
Their native dispatch/accumulator techniques are not assumed to have the same
|
|
240
|
+
costs in Ruby; the implemented variants are evaluated on CRuby and Spinel.
|