wardite 0.9.0 → 1.0.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 +80 -1
- data/Rakefile +5 -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_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/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/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 +405 -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-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.md +144 -0
- data/docs/spinel-string-aset-inplace-proposal.md +200 -0
- data/lib/wardite/alu_f32.generated.rb +1 -0
- data/lib/wardite/alu_f64.generated.rb +1 -0
- data/lib/wardite/alu_i32.generated.rb +14 -9
- data/lib/wardite/alu_i64.generated.rb +18 -20
- data/lib/wardite/cli/cli.rb +61 -28
- 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 +1151 -0
- data/lib/wardite/efflorescence/optimizer.rb +580 -0
- data/lib/wardite/efflorescence/register_validation.rb +290 -0
- data/lib/wardite/efflorescence/store_access.rb +271 -0
- data/lib/wardite/efflorescence/translator.rb +859 -0
- data/lib/wardite/efflorescence/vm.rb +1006 -0
- data/lib/wardite/efflorescence/wasi.rb +108 -0
- data/lib/wardite/efflorescence.rb +11 -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 +203 -138
- 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/wasm_module.rb +6 -3
- data/lib/wardite.rb +184 -70
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +66 -10
- data/sig/generated/wardite/cli/cli.rbs +8 -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 +881 -0
- data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
- data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
- data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
- data/sig/generated/wardite/efflorescence/wasi.rbs +47 -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 +29 -4
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite.rbs +49 -12
- data/sig/vernier.rbs +2 -2
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/spin.toml +7 -0
- metadata +126 -3
|
@@ -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.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Efflorescence VM: フレーム再利用時の初期化を軽量化する
|
|
2
|
+
|
|
3
|
+
2026-11-04、[callコストの調査](efflorescence-call-investigation.md)に基づき、フレーム再利用時の初期化範囲を絞った。
|
|
4
|
+
Spinelでの `call_mix` は、全レジスタ初期化の対照に比べて約17%短縮した。
|
|
5
|
+
|
|
6
|
+
## 初期化する範囲
|
|
7
|
+
|
|
8
|
+
| 領域 | フレーム生成時 | 再利用時 |
|
|
9
|
+
| --- | --- | --- |
|
|
10
|
+
| 引数 | テンプレートを複製した後、呼び出し元が設定する | 呼び出し元がすべて上書きする |
|
|
11
|
+
| 非引数ローカル | テンプレートから0を設定する | この範囲だけ0に戻す |
|
|
12
|
+
| 定数 | テンプレートから設定する | 保持する |
|
|
13
|
+
| 一時レジスタ | テンプレートを複製する | 使用前に命令が書き込む |
|
|
14
|
+
| PC・戻り値の格納先 | 呼び出しの情報を設定する | 呼び出しの情報を設定する |
|
|
15
|
+
|
|
16
|
+
`Frame.new` は `initial_registers.dup` で整数配列を生成する。以前の「全スロットを0で確保してから、Rubyのループで全初期値を上書きする」二重の処理も省いた。
|
|
17
|
+
|
|
18
|
+
`Frame#reset` は、Frame生成時に保存したローカル範囲だけを走査する。非引数ローカルがないmixなどのcalleeでは、レジスタへの書き込みは発生しない。レジスタ番号の詰め直しやMIR命令の追加・融合は行っていない。
|
|
19
|
+
|
|
20
|
+
## 一時レジスタを残しても安全であることの確認
|
|
21
|
+
|
|
22
|
+
新しい `RegisterValidation` が、最適化後のMIRをロード時に検証する。タイミングはPROFILE命令の挿入・フラット化より前であり、通常実行・カウンタ計測・各最適化を無効にした場合のすべてに適用する。
|
|
23
|
+
|
|
24
|
+
1. 関数の入口では、引数・ローカル・定数だけを定義済みとする。
|
|
25
|
+
2. Basic Block内を順番に読み、ソースは定義済みであることを確認し、命令の書き込み先を定義済みにする。
|
|
26
|
+
3. 分岐先と継続先へ定義情報を伝える。合流点では、到達した全経路の情報の共通部分を取る。
|
|
27
|
+
4. 合流点の情報が変わった場合は、そのブロックを再検証する。ループのバックエッジによって、初回入口には存在しない定義を認めることはない。
|
|
28
|
+
5. 定義前の読み出しや、定数スロットへの書き込みは `TranslationError` にする。
|
|
29
|
+
|
|
30
|
+
状態は命令ごとではなくBasic Blockごとに持つ。フラグは整数配列で表現し、ネイティブ整数幅を超えるビットマスクに依存しない。実行ループ内での検証・条件追加・allocationはない。
|
|
31
|
+
|
|
32
|
+
フレームpoolは従来どおり公開APIの1回の呼び出しごとに独立させる。実行中の再帰フレームは空き一覧に戻さず、同じVMへの再入・重なり合う呼び出しでもレジスタを共有しない。trapで中断した呼び出しのpoolを次の公開呼び出しへ持ち越すこともない。
|
|
33
|
+
|
|
34
|
+
## 性能測定
|
|
35
|
+
|
|
36
|
+
CRuby 4.0.9、Spinel `2026.10.25+1967 (6e7fc1355)`、macOS arm64。
|
|
37
|
+
ネイティブバイナリは `-O2` と通常の整数overflowチェックを使う。
|
|
38
|
+
seed 123、5,000,000 rounds、2回のウォームアップ後に7回測定した。表は平均 ± 標本標準偏差、単位はms。
|
|
39
|
+
|
|
40
|
+
| export | 全レジスタ初期化 | ローカルのみ初期化 |
|
|
41
|
+
| --- | ---: | ---: |
|
|
42
|
+
| `inline_mix` | 325.463 ± 2.453 | 327.028 ± 2.406 |
|
|
43
|
+
| `call_mix` | 727.817 ± 15.768 | **603.672 ± 13.384** |
|
|
44
|
+
|
|
45
|
+
`call_mix` は実行時間が **17.1%減少**した。`inline_mix` では測定のばらつきと同程度の差だった。
|
|
46
|
+
全測定で両exportのchecksumは `1647235310` と一致した。MIRの動的命令数も両版で一致し、inlineは35,000,003、callは45,000,003だった。
|
|
47
|
+
|
|
48
|
+
対照の `full_reset` は、現在のライブラリをコピーして `Frame#reset` のループだけを全スロット初期化へ戻したもの。
|
|
49
|
+
新規Frameのテンプレート複製、ロード時の定義解析、MIR、エントリポイント、コンパイラ設定は両版で同じにする。
|
|
50
|
+
したがって、今回の比較は再利用時の初期化範囲の効果であり、新規Frame生成の改善分を含まない。
|
|
51
|
+
|
|
52
|
+
測定は1サンプルにつき1プロセス。ロード・MIR生成と実行時間を分け、実行には `call_unboxed` の準備・フレーム設定を含める。
|
|
53
|
+
詳細カウンタとallocationは別プロセスで取得し、時間測定には含めない。実行順序は回転・反転する。
|
|
54
|
+
|
|
55
|
+
最初に100万回・全30構成で探索的に測定したが、call_mixに大きなばらつきがあったため、上記の長い比較で確認した。
|
|
56
|
+
[主測定の全サンプル・ソースとバイナリのSHA256](../benchmark/results/selective_reset_spinel_2026-11-04.json)と、
|
|
57
|
+
[探索的な30構成のデータ](../benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json)を保存している。
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
# 主測定と同じ条件
|
|
61
|
+
VARIANTS=full_reset,locals_only EXPORTS=inline_mix,call_mix \
|
|
62
|
+
RUNS=7 WARMUPS=2 ruby benchmark/investigate_calls.rb 5000000 123
|
|
63
|
+
|
|
64
|
+
# ループ・identity・add_one・mix、allocation方式も含めた全30構成
|
|
65
|
+
RUNS=7 WARMUPS=2 ruby benchmark/investigate_calls.rb 1000000 123
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 検証
|
|
69
|
+
|
|
70
|
+
CRuby・Spinelのネイティブテストを含む全体で、**95 tests / 5,009 assertions、失敗・エラー・省略0**。
|
|
71
|
+
`bundle exec rake check` とSpinelビルドも成功した。
|
|
72
|
+
|
|
73
|
+
追加テストでは、片方の分岐にしか代入がないMIR、後から到達した経路によって定義が失われる合流点、初回未定義のループ、到達不能コード、定数への書き込みを検証する。
|
|
74
|
+
Wasm実行では、2個の非引数ローカルの再初期化、分岐結果、一時値、即値化を無効にした定数参照、再帰中の生きたローカル、同期的な再入、trap後の再実行を確認する。
|
|
75
|
+
64個を超えるレジスタと、元の受け入れベンチマーク全体も実行している。
|