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.
Files changed (184) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +191 -0
  3. data/Makefile +14 -0
  4. data/README.md +107 -2
  5. data/Rakefile +5 -0
  6. data/Steepfile +1 -0
  7. data/assets/wardite.svg +12 -0
  8. data/benchmark/README.md +500 -0
  9. data/benchmark/build_call_profile.rb +42 -0
  10. data/benchmark/call_cost_worker.rb +27 -0
  11. data/benchmark/compare.rb +93 -0
  12. data/benchmark/compare_call_types.rb +212 -0
  13. data/benchmark/compare_dispatch.rb +123 -0
  14. data/benchmark/compare_engines.rb +119 -0
  15. data/benchmark/compare_optimizations.rb +139 -0
  16. data/benchmark/engine_phases.rb +30 -0
  17. data/benchmark/figures/README.md +35 -0
  18. data/benchmark/figures/plot_spinel_comparison.py +137 -0
  19. data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
  20. data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
  21. data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
  22. data/benchmark/figures/spinel_new_vm_summary.json +82 -0
  23. data/benchmark/float_m5.wat +42 -0
  24. data/benchmark/i32_call_arities.wat +89 -0
  25. data/benchmark/i32_calls.wat +174 -0
  26. data/benchmark/i32_memory.wat +58 -0
  27. data/benchmark/i32_mix.wat +80 -0
  28. data/benchmark/i64_m4.wat +55 -0
  29. data/benchmark/investigate_calls.rb +231 -0
  30. data/benchmark/investigate_generated_guards.rb +218 -0
  31. data/benchmark/m3_call_worker.rb +38 -0
  32. data/benchmark/measure_m1.rb +51 -0
  33. data/benchmark/measure_m2.rb +55 -0
  34. data/benchmark/measure_m3_calls.rb +89 -0
  35. data/benchmark/measure_m4.rb +134 -0
  36. data/benchmark/measure_m5_boundary.rb +41 -0
  37. data/benchmark/measure_mir_cache_verify.rb +58 -0
  38. data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
  39. data/benchmark/measure_ruby_wasm_startup.rb +43 -0
  40. data/benchmark/measure_wasi_write.rb +75 -0
  41. data/benchmark/optimization_worker.rb +23 -0
  42. data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
  43. data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
  44. data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
  45. data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
  46. data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
  47. data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
  48. data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
  49. data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
  50. data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
  51. data/benchmark/results/efflorescence-m1-cruby.json +799 -0
  52. data/benchmark/results/efflorescence-m1-spinel.json +799 -0
  53. data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
  54. data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
  55. data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
  56. data/benchmark/results/efflorescence-m2-memory.json +408 -0
  57. data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
  58. data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
  59. data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
  60. data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
  61. data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
  62. data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
  63. data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
  64. data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
  65. data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
  66. data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
  67. data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
  68. data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
  69. data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
  70. data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
  71. data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
  72. data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
  73. data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
  74. data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
  75. data/benchmark/summarize_call_profile.py +54 -0
  76. data/benchmark/validate_m1_eh.rb +34 -0
  77. data/benchmark/validate_m2.rb +119 -0
  78. data/benchmark/validate_m3.rb +44 -0
  79. data/benchmark/validate_m4.rb +95 -0
  80. data/benchmark/validate_m5.rb +108 -0
  81. data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
  82. data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
  83. data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
  84. data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
  85. data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
  86. data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
  87. data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
  88. data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
  89. data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
  90. data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
  91. data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
  92. data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
  93. data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
  94. data/docs/efflorescence-call-arities.md +115 -0
  95. data/docs/efflorescence-call-investigation.md +155 -0
  96. data/docs/efflorescence-call-transfer.md +128 -0
  97. data/docs/efflorescence-call-types.md +102 -0
  98. data/docs/efflorescence-coverage-ja.md +351 -0
  99. data/docs/efflorescence-generated-c-guards.md +410 -0
  100. data/docs/efflorescence-inlining.md +165 -0
  101. data/docs/efflorescence-ja.md +155 -0
  102. data/docs/efflorescence-m1.md +160 -0
  103. data/docs/efflorescence-m2.md +233 -0
  104. data/docs/efflorescence-m3.md +131 -0
  105. data/docs/efflorescence-m4.md +88 -0
  106. data/docs/efflorescence-m5.md +82 -0
  107. data/docs/efflorescence-m6.md +95 -0
  108. data/docs/efflorescence-mir2-cache.md +123 -0
  109. data/docs/efflorescence-operand-decoding.md +105 -0
  110. data/docs/efflorescence-phase2-ja.md +225 -0
  111. data/docs/efflorescence-phase2.md +240 -0
  112. data/docs/efflorescence-register-initialization.md +75 -0
  113. data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
  114. data/docs/efflorescence-spinel-linear-memory.md +72 -0
  115. data/docs/efflorescence-startup-snapshot.md +60 -0
  116. data/docs/efflorescence-unboxed-globals.md +61 -0
  117. data/docs/efflorescence.md +144 -0
  118. data/docs/spinel-string-aset-inplace-proposal.md +200 -0
  119. data/lib/wardite/alu_f32.generated.rb +11 -2
  120. data/lib/wardite/alu_f64.generated.rb +11 -2
  121. data/lib/wardite/alu_i32.generated.rb +54 -17
  122. data/lib/wardite/alu_i64.generated.rb +73 -31
  123. data/lib/wardite/cli/cli.rb +112 -29
  124. data/lib/wardite/const.rb +1 -0
  125. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  126. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  127. data/lib/wardite/efflorescence/inspection.rb +165 -0
  128. data/lib/wardite/efflorescence/ir.rb +1164 -0
  129. data/lib/wardite/efflorescence/mir_cache.rb +657 -0
  130. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  131. data/lib/wardite/efflorescence/register_validation.rb +362 -0
  132. data/lib/wardite/efflorescence/snapshot.rb +229 -0
  133. data/lib/wardite/efflorescence/store_access.rb +366 -0
  134. data/lib/wardite/efflorescence/translator.rb +866 -0
  135. data/lib/wardite/efflorescence/vm.rb +1170 -0
  136. data/lib/wardite/efflorescence/wasi.rb +52 -0
  137. data/lib/wardite/efflorescence.rb +17 -0
  138. data/lib/wardite/errors.rb +20 -0
  139. data/lib/wardite/float_bits.rb +46 -0
  140. data/lib/wardite/instruction.rb +35 -4
  141. data/lib/wardite/leb128.rb +33 -12
  142. data/lib/wardite/load.rb +292 -143
  143. data/lib/wardite/value.rb +169 -175
  144. data/lib/wardite/version.rb +1 -1
  145. data/lib/wardite/wasi/consts.rb +1 -0
  146. data/lib/wardite/wasi/spinel.rb +41 -0
  147. data/lib/wardite/wasi.rb +483 -268
  148. data/lib/wardite/wasm_module.rb +6 -3
  149. data/lib/wardite.rb +360 -98
  150. data/rbs_collection.lock.yaml +36 -96
  151. data/scripts/gen_alu.rb +121 -21
  152. data/scripts/verify_ruby_wasm.rb +70 -0
  153. data/sig/generated/wardite/cli/cli.rbs +23 -6
  154. data/sig/generated/wardite/const.rbs +2 -0
  155. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  156. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  157. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  158. data/sig/generated/wardite/efflorescence/ir.rbs +888 -0
  159. data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
  160. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  161. data/sig/generated/wardite/efflorescence/register_validation.rbs +104 -0
  162. data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
  163. data/sig/generated/wardite/efflorescence/store_access.rbs +145 -0
  164. data/sig/generated/wardite/efflorescence/translator.rbs +218 -0
  165. data/sig/generated/wardite/efflorescence/vm.rbs +240 -0
  166. data/sig/generated/wardite/efflorescence/wasi.rbs +34 -0
  167. data/sig/generated/wardite/errors.rbs +12 -0
  168. data/sig/generated/wardite/float_bits.rbs +30 -0
  169. data/sig/generated/wardite/instruction.rbs +22 -1
  170. data/sig/generated/wardite/leb128.rbs +3 -1
  171. data/sig/generated/wardite/load.rbs +59 -7
  172. data/sig/generated/wardite/value.rbs +70 -11
  173. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  174. data/sig/generated/wardite/wasi/spinel.rbs +15 -0
  175. data/sig/generated/wardite/wasi.rbs +111 -4
  176. data/sig/generated/wardite.rbs +114 -13
  177. data/sig/mir_cache_stdlib.rbs +5 -0
  178. data/sig/vernier.rbs +2 -2
  179. data/spinel/README.md +30 -0
  180. data/spinel/bin/wardite.rb +6 -0
  181. data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
  182. data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
  183. data/spinel/spin.toml +7 -0
  184. metadata +154 -3
@@ -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個を超えるレジスタと、元の受け入れベンチマーク全体も実行している。
@@ -0,0 +1,90 @@
1
+ # Efflorescenceのruby.wasm対応フェーズ1
2
+
3
+ [ADR 0007](adr/0007-efflorescence-ruby-wasm-phase1.md) の実装記録。CRuby版とSpinel版で `--new-engine` を指定し、Ruby 4.0.9のWASI Preview 1コマンドを実行する。
4
+
5
+ - 共有WASIのimportはstart前に検証する。realtimeとmonotonicの時計、環境変数、`fdstat` のABIを修正した。ADRに列挙した10個の未実装APIは既知のシグネチャでリンクでき、呼び出すと `ENOSYS` を返す。
6
+ - MIRレジスタ検証は固定幅のビット集合と逆ポストオーダーを使う。基本ブロックの合流では定義集合を交差させ、ループと例外辺を含めて収束まで再検証する。
7
+ - CRubyの `memory.fill` は全範囲を検査してから、バイナリ文字列を最大64 KiBずつ書き込む。Spinelと、Store API経由で非バイナリ文字列が設定された場合はバイト単位で書く。
8
+ - `path_readlink` はホストのファイルシステムエラーをWASI errnoに変換する。`path_filestat_get` はlookup flagsを使い、第5引数の結果ポインタに `fd_filestat_get` と共通の形式で出力する。
9
+ - Spinel版も共有WASI実装を使う。POSIXのディスクリプタフラグとerrnoはFFIで取得・変換し、WASIの結果を既存の線形メモリへ書き戻す。WASIを無効にするには `--no-wasi` を指定する。
10
+
11
+ ## ビルドと実行
12
+
13
+ 検証に使ったRubyは `v4.0.9`(`3e93b23afcec3433f6aecc34b5d832799d754238`)。BASERUBY 4.0.2、WASI SDK 25.0、Binaryen 133、16 MiBのWasmスタックでビルドした。確認したWasmのSHA-256は `5d87d7e932b9b698892711f065d5e0313ad7aad96f88a9693d05aaae02541669`。生成物はビルド環境によって変わるため、比較には同じファイルを両ランタイムに渡す。
14
+
15
+ RubyのGitチェックアウトとWASI SDKの場所を `ruby_source`、`wasi_sdk_path` に指定する。ホストにRuby、autoconf、GNU makeとBinaryenを用意し、Warditeのルートから実行する。Warditeの実行と計測にはCRuby 4.0.9を使った。
16
+
17
+ ```sh
18
+ build_root=$(mktemp -d)
19
+ mkdir -p "$build_root/src" "$build_root/build"
20
+ git -C "$ruby_source" archive 3e93b23afc | tar -x -C "$build_root/src"
21
+ (
22
+ cd "$build_root/src"
23
+ ruby tool/downloader.rb -d tool -e gnu config.guess config.sub
24
+ ./autogen.sh
25
+ )
26
+ (
27
+ cd "$build_root/build"
28
+ WASI_SDK_PATH="$wasi_sdk_path" LDFLAGS="-Xlinker -zstack-size=16777216" \
29
+ "$build_root/src/configure" --host=wasm32-unknown-wasi --with-static-linked-ext --with-ext=
30
+ make -j8 ruby
31
+ )
32
+ ruby_wasm="$build_root/build/ruby"
33
+ wasm-tools validate "$ruby_wasm"
34
+ bundle exec ruby scripts/verify_ruby_wasm.rb "$ruby_wasm"
35
+ ```
36
+
37
+ 照合に使った参照エンジンはWasmtime 49.0.0(`17830bd3c`)。スクリプトは一時ディレクトリに `main.rb` と `answer.rb` を作り、各CLIを独立プロセスとして実行する。`bundle exec` で起動した場合はBundler適用前の環境を子プロセスへ渡す。Spinel版も照合に含めるには `WARDITE_SPINEL_BIN` を指定する。
38
+
39
+ ```sh
40
+ make spinel
41
+ build/wardite --new-engine -- "$ruby_wasm" --disable-gems -e 'puts RUBY_VERSION; puts 6*7'
42
+ WARDITE_SPINEL_BIN="$PWD/build/wardite" bundle exec ruby scripts/verify_ruby_wasm.rb "$ruby_wasm"
43
+ ```
44
+
45
+ Wasmtime、CRuby版Efflorescence、Spinel版Efflorescenceの3つで以下の結果が一致した。
46
+
47
+ | ケース | 標準出力 | 終了コード |
48
+ |---|---|---:|
49
+ | `--disable-gems -e 'puts RUBY_VERSION; puts 6*7'` | `4.0.9` と `42` | 0 |
50
+ | preopenの `/app/main.rb` から `require_relative` | `42` | 0 |
51
+ | `--disable-gems -e 'exit 7'` | 空 | 7 |
52
+
53
+ ## 回帰試験
54
+
55
+ ```sh
56
+ bundle exec rake test
57
+ bundle exec rake check
58
+ bundle exec rake parity
59
+ make spinel-test RUBY="bundle exec ruby"
60
+ ```
61
+
62
+ CRubyのsuiteは258件、26,511 assertionsで失敗0件。Spinel専用76件はnative suiteで別に実行し、2,547 assertions、失敗・省略0件。型チェックも通過した。`rake parity` はwasm-tools 1.218.0とWasmtime 49.0.0を使い、CRubyとSpinelで各436 fixtureを検証した。Spinelのローカル版は `6f1a2c259`(`-O2`、既定のoverflowチェック)。
63
+
64
+ ## キャッシュなしの起動時間
65
+
66
+ macOS 27.0.1 arm64、CRuby 4.0.9、YJIT有効で、同じWasmの `-e` を各構成2回、新規プロセスで実行した。比較元 `e9b922e` にはWASI起動時ABIとレジスタ検証の変更が入っている。計測対象の `e1a39ea` はSpinelホスト追加前のCRuby実装。この `-e` はファイル用のWASI APIを使わない。
67
+
68
+ ```sh
69
+ baseline_dir=$(mktemp -d)
70
+ git archive e9b922e lib | tar -x -C "$baseline_dir"
71
+ for lib_path in "$baseline_dir/lib" lib lib "$baseline_dir/lib"; do
72
+ /usr/bin/time -l bundle exec ruby --yjit -I"$lib_path" benchmark/measure_ruby_wasm_startup.rb "$ruby_wasm"
73
+ done
74
+ ```
75
+
76
+ 計測スクリプトは工程別時間を秒で出力する。ピークRSSはmacOSの `/usr/bin/time -l` が返すバイト数から換算した。起動全体はWardite CLIの実行開始から `_start` の完了までを指す。
77
+
78
+ | 工程 | 一括書き込み前の中央値(範囲) | フェーズ1の中央値(範囲) |
79
+ |---|---:|---:|
80
+ | Wasmデコード(秒) | 1.643(1.635–1.650) | 1.652(1.650–1.654) |
81
+ | Store初期化(秒) | 0.036(0.035–0.036) | 0.037(0.037–0.038) |
82
+ | MIR変換(秒) | 1.625(1.610–1.641) | 1.641(1.634–1.648) |
83
+ | MIR最適化(秒) | 4.527(4.469–4.584) | 4.490(4.484–4.496) |
84
+ | レジスタ検証(秒) | 7.721(7.700–7.743) | 7.732(7.726–7.739) |
85
+ | Lowering(秒) | 0.674(0.670–0.678) | 0.685(0.676–0.694) |
86
+ | ゲスト実行(秒) | 17.599(17.537–17.661) | 9.261(9.258–9.264) |
87
+ | 起動全体(秒) | 33.968(33.902–34.033) | 25.637(25.613–25.661) |
88
+ | ピークRSS(GiB) | 6.770(6.770–6.771) | 6.457(6.137–6.777) |
89
+
90
+ MIR2ビルドキャッシュと、ゲスト実行中の追加の性能改善は [フェーズ2](adr/0008-efflorescence-ruby-wasm-phase2.md) で扱う。
@@ -0,0 +1,72 @@
1
+ # Spinel版Warditeの線形メモリ
2
+
3
+ [ADR 0008の6a](adr/0008-efflorescence-ruby-wasm-phase2.md#6a-spinelに書き込み可能なstringビューを実装)では、Spinelに `IO::Buffer.for(string) { |buffer| ... }` を追加し、Efflorescenceの `memory.fill` で書き込み可能なビューを使った。ブロック中はStringの変更を拒否する。正常終了と例外でロックを解除し、渡したバッファを解放する。凍結Stringは読み取り専用。Spinelの回帰フィクスチャをCRuby 4.0.9と照合した。
4
+
5
+ Spinel側のコミットは `255955a06a0311df92f6b422c69d65ff2f39fe6e`。WarditeのCIでは固定したSpinel `6e7fc13556709bf7d7ba4af5bd1d12bebc00733e` に `spinel/patches/0001-io-buffer-string-view.patch` を適用する。
6
+
7
+ Apple M3 ProのSpinel `-O2` で、初回のfillでページを確保してから256 MiBを3回埋めた。バイトループは628.70–632.33 ms、Stringビューと `buffer.clear` は4.13–4.67 msだった。Ruby 4.0.9のWasmは `-e 'puts RUBY_VERSION; puts 6*7'` を実行し、標準出力の `4.0.9` と `42` を確認した。
8
+
9
+ ## 6bの所有バッファ
10
+
11
+ Spinelでは各 `Wardite::Memory` が `IO::Buffer` を所有する。Storeのアクティブデータ、Stack VM、Efflorescence、WASIは同じ領域を読み書きする。Efflorescenceの数値load/storeはlittle-endianの型付きアクセサを使う。bulk memoryは全範囲を確認してから `copy`、`clear`、`set_string` を呼ぶ。Spinel版のStack VMでも、範囲外のstoreとbulk memoryは書き込み前にtrapし、`memory.fill` の値は下位8ビットを使う。
12
+
13
+ Spinelのホストコールバックでは `store.memories[0].buffer` で現在の `IO::Buffer` を取得する。バッファのサイズ変更、解放、所有権の移動は `Memory` が行う。ゲスト呼び出しの後は、growに備えてバッファを取り直す。成功したgrowは旧領域の内容を引き継ぎ、新規ページをゼロで初期化してから古いバッファを解放する。上限超過、ロック中、割り当て失敗では旧バッファとページ数を維持する。
14
+
15
+ CRubyのホストAPIは `Memory#data` のStringを引き続き使える。両ホストで `Memory#read_bytes(offset, length)` と `Memory#write_bytes(offset, bytes)` を使える。WASIではパス、iovec、ファイルI/Oに必要な範囲を検査し、必要なバイト範囲だけをStringに取り出す。
16
+
17
+ ## ビルド
18
+
19
+ Spinel側では `a9a35b85d75ca273558f7e25a875cc240fc0f5a3` で、`IO::Buffer.new` の領域割り当て失敗を `Errno::ENOMEM` として伝える。WarditeのCIは固定したSpinelに両方のパッチを適用し、パッチのハッシュをツールチェーンキャッシュのキーに含める。同じ構成をローカルでビルドするには、Spinelのクリーンな作業ツリーを `spinel_src` に指定し、Warditeのルートから実行する。
20
+
21
+ ```sh
22
+ git -C "$spinel_src" switch --detach 6e7fc13556709bf7d7ba4af5bd1d12bebc00733e
23
+ git -C "$spinel_src" apply "$PWD"/spinel/patches/*.patch
24
+ make -C "$spinel_src" deps
25
+ make -C "$spinel_src" all
26
+ make spinel SPINEL="$spinel_src/bin/spinel"
27
+ ```
28
+
29
+ ## 計測
30
+
31
+ Apple M3 Pro、macOS arm64、Spinel `6f1a2c259` を基準に各段階のパッチを適用し、`-O2` と既定の整数overflowチェックで測定した。Ruby 4.0.9のゲストはフェーズ1と同じWasm(SHA-256 `5d87d7e932b9b698892711f065d5e0313ad7aad96f88a9693d05aaae02541669`)。`_start` の計測区間は、インスタンス構築とargvの設定後からゲストの戻りまで。MIR2キャッシュは使用していない。
32
+
33
+ | Spinelのメモリ実装 | 256 MiB fill(3回の範囲) | Rubyの `_start`(2回の範囲) |
34
+ |---|---:|---:|
35
+ | Stringのバイトループ | 628.70–632.33 ms | 2.72–3.15 s |
36
+ | 6aのStringビュー | 4.13–4.67 ms | 2.16–2.45 s |
37
+ | 6bの所有バッファ | 3.52–3.58 ms | 1.74–1.75 s |
38
+
39
+ 小さいWasmでの比較は、Efflorescenceのエクスポート実行だけを各3回計測した。i32の試験は同じ非整列アドレスへのstoreとloadを50万組行う。WASIの試験は4バイトの `fd_read`(`/dev/zero`)と `fd_write`(`/dev/null`)を5万組行う。両実装のチェックサムは一致した。
40
+
41
+ | 処理 | 移行前の中央値 | 所有バッファの中央値 |
42
+ |---|---:|---:|
43
+ | i32 load/store | 56.33 ms | 22.68 ms |
44
+ | WASI read/write | 133.06 ms | 107.31 ms |
45
+
46
+ ## 検証
47
+
48
+ 固定したSpinelに両パッチを適用してビルドし、17件の `IO::Buffer` フィクスチャを照合した。Warditeの型チェックと全272テスト(29,219 assertions、失敗・省略なし)が通った。CRubyとSpinelで各436件の命令パリティが通り、SpinelのStack VMで分類される差は47件から29件になった。
49
+
50
+ Ruby 4.0.9では、Wasmtime 49.0.0とCRuby・Spinelの `-e`、`require_relative`、`exit 7` の出力と終了コードが一致した。CRubyとSpinelでは通常実行に加え、各ホスト自身のMIR2キャッシュを別プロセスから読み戻した実行も照合した。
51
+
52
+ ## memory.growの余裕容量
53
+
54
+ Spinel版ではWasmの現在ページ数と内部の確保容量を分ける。再確保時は要求ページ数の8分の1(最低1ページ)を足し、Wasmの上限以内に収める。余裕容量の確保に失敗した場合は要求サイズで再試行する。余裕容量内のgrowでは、`IO::Buffer#transfer` で割り当てを移し、既存バイトのコピーを省く。
55
+
56
+ ゲストとホストには現在のWasmサイズに収まる `IO::Buffer` だけを公開する。未公開のページは割り当て時のゼロを保持する。成功時は旧バッファと派生ビューを無効にする。ロック中、上限超過、必要サイズの割り当て失敗では元の状態を保持する。CRubyはStringの実装を使う。
57
+
58
+ ### Ruby 4.0.9の起動
59
+
60
+ macOS arm64、固定Spinel `6e7fc135` と上記の両パッチ、`-O2` で比較した。変更前は `3d07b48`。同じWasmと同じ既存MIR2キャッシュを使い、`--disable-gems -e 'puts RUBY_VERSION'` の出力が両方とも `4.0.9` であることを確認した。OSのファイルキャッシュが温まった状態で、変更前後の実行順を入れ替えながら各5回計測した。
61
+
62
+ | 中央値 | 変更前 | 余裕容量あり |
63
+ |---|---:|---:|
64
+ | 起動全体 | 1.538 s | 1.225 s |
65
+ | ロード | 0.516 s | 0.518 s |
66
+ | ピークRSS | 5.114 GiB | 1.074 GiB |
67
+
68
+ 別の計測用ビルドで各3回、ゲストの `_start` とgrowのコピーを測った。ゲストは両方とも20回拡張し、最後に4,813ページ(約301 MiB)に達した。既存領域のコピーは20回から3回、合計4.14 GiBから64.125 MiBになった。コピー時間の中央値は312 msから4.51 ms、`_start` の中央値は1.004 sから0.706 sだった。
69
+
70
+ 既存の小さいWasmも、ウォームアップ後に各4回測定した。i32の `inline_mix`、i64の `alu`、`float_alu`、インライン化を無効にした `call_one`、Storeの `array_scan` と `copy_fill` は、出力が一致し、CLI全体の実時間の中央値の変化は−1.5%から+0.5%だった。`array_scan` の先頭で1ページ拡張してから走査した場合は117.9 msから118.3 msだった。
71
+
72
+ Spinelの回帰では、余裕容量内のgrowと再確保の両方でバイトの保持、ゼロ初期化、旧ビューの無効化を確認した。ゲストとホストの範囲外アクセス、ロック中のgrow、余裕容量の確保失敗からの再試行、必要サイズも確保できない場合の状態保持を検査した。GCストレス下でもこの回帰を実行した。CRubyとSpinelの回帰、型チェック、各436ケースの命令パリティ、およびRuby 4.0.9のWASI実行の照合が通った。
@@ -0,0 +1,60 @@
1
+ # Efflorescenceの起動スナップショット
2
+
3
+ [ADR 0012](adr/0012-efflorescence-startup-snapshot.md)を実装した。WASIコマンドを `--new-engine --mir-cache DIR --snapshot DIR` で実行すると、ゲストが許可リストにないWASI関数を初めて呼ぶ直前の状態を保存する。同じキーの次回以降の実行では、Wasmの命令を実行せずにその時点から再開する。
4
+
5
+ ```sh
6
+ WARDITE_TRACE=1 build/wardite --new-engine --mir-cache "$HOME/.cache/wardite" \
7
+ --snapshot "$HOME/.cache/wardite" -- "$ruby_wasm" --disable-gems -e 'puts RUBY_DESCRIPTION'
8
+ ```
9
+
10
+ `WARDITE_TRACE=1` で標準エラーに `snapshot saved`、`snapshot restored`、`snapshot miss`、`snapshot skipped (理由)` が出る。保存と復元には所要時間も付く。
11
+
12
+ チェックポイントより前にゲストが取得した時刻と乱数は、復元したすべての実行で同じ値になる。Rubyでは `Random` の既定のシードやハッシュのシードが固定される。信頼できない入力をハッシュのキーにする用途では使わない。
13
+
14
+ ## 実装
15
+
16
+ - [Snapshot](../lib/wardite/efflorescence/snapshot.rb) がキーの計算、チェックポイントの判定、保存、検査と復元を行う。ファイルの読み書きは [MIRCache](../lib/wardite/efflorescence/mir_cache.rb) の `Reader`、`Writer`、`read_payload`、`publish` を共用する。
17
+ - キーは、スナップショットの形式バージョン、各メモリの現在のページ数、MIR2キャッシュのキー(Wasm全体のSHA-256、形式と生成コードのバージョン、最適化の無効化設定、ホスト識別子)、WASIの `snapshot_identity`(引数、環境変数、開いている全fdの番号と種別、preopenのパス)のSHA-256。
18
+ - [VM](../lib/wardite/efflorescence/vm.rb) の `CALL_HOST` と `CALL_INDIRECT` は、ホスト関数を呼ぶ間 `frame.pc` に命令の位置を保持する。`execute_flat` は `frame.pc` から始まる。`execute_host_site` は記録中だけ許可リストを判定し、公開呼び出しが入れ子でなければ保存する。判定は1プロセスで1回だけ行う。
19
+ - 復元は、Storeを通常どおり作った後、ファイルの全体を検査してからメモリとglobalを書き換え、`VM#resume` でフレームを積み直して最上位フレームのホスト呼び出しから再実行する。
20
+ - [ADR 0011](adr/0011-efflorescence-unboxed-globals.md)の整数配列とは、保存の直前にStoreへ書き戻し、`VM#resume` の開始時にStoreから読み込むことで同期する。
21
+ - 許可リストは `args_*`、`environ_*`、`fd_prestat_*`、`fd_fdstat_get`、`clock_res_get`、`clock_time_get`、`random_get` と、fd 0〜2に対する `fd_filestat_get`。
22
+ - startセクションを持つモジュールでは保存も復元もしない。
23
+
24
+ ## 検証
25
+
26
+ - [efflorescence_snapshot_test.rb](../test/efflorescence_snapshot_test.rb) と [spinel_snapshot_test.rb](../test/e2e/spinel_snapshot_test.rb):入れ子のフレーム、例外ハンドラ、`memory.grow`、i32とi64のglobal、`random_get` の後に、直接呼び出しと `call_indirect` のそれぞれでチェックポイントに入るWASIコマンドで、保存した実行と復元した実行の出力と終了コードが一致する。引数ごとに別のファイルになること、ペイロードを壊したファイルで通常の起動に戻り保存し直すこと、`--mir-cache` なしの指定を拒否することも確かめる。
27
+ - [verify_ruby_wasm.rb](../scripts/verify_ruby_wasm.rb) に `WARDITE_VERIFY_SNAPSHOT=1` を追加した。Ruby 4.0.9のWasmの `-e`、`require_relative`、`exit 7` の3ケースで、CRuby版とSpinel版それぞれの保存する実行と復元する実行がWasmtimeと一致した。
28
+ - CRubyのtest(281件)・型チェック、Spinelの回帰試験(82件)が通った。Wasmtimeを参照する `rake parity` は、手元のWasmtime 28.0.0が `-W exceptions` に対応していないため実行できなかった。CRubyとSpinelの間のparityは、`rake test` と `make spinel-test` に含まれるparity試験で確認した。
29
+
30
+ ```sh
31
+ make spinel
32
+ WARDITE_VERIFY_SNAPSHOT=1 WARDITE_SPINEL_BIN="$PWD/build/wardite" \
33
+ bundle exec ruby scripts/verify_ruby_wasm.rb "$ruby_wasm"
34
+ ```
35
+
36
+ ## 計測結果
37
+
38
+ Apple M3 Pro、macOS 26.6.2、Spinel `6f1a2c259`(`-O2`)。ゲストはRuby 4.0.9のWasm(SHA-256 `b1ee00286fc1efd27d8d9fd4ebe45034e74f006e37d4e759d77ea2f27ffc5a7e`)。すべてMIR2キャッシュのヒット。変更前(`94a60e5`)、変更後のスナップショットなし、変更後の復元の3条件を順に入れ替えながら各5回、別プロセスで実行し、中央値を取った。保存は既存のスナップショットを消して3回実行した。OSのファイルキャッシュは消去していない。ピークRSSは `/usr/bin/time -l` の最大常駐セット。
39
+
40
+ `-e 'puts RUBY_DESCRIPTION'`(スナップショット5.1 MiB):
41
+
42
+ | 条件 | プロセス全体 | `load_from_buffer` | スナップショットの読み書き | ピークRSS |
43
+ |---|---:|---:|---:|---:|
44
+ | 変更前 | 1.51 s | 0.73 s | — | 1102 MiB |
45
+ | 変更後、スナップショットなし | 1.50 s | 0.71 s | — | 1024 MiB |
46
+ | 保存する実行 | 1.64 s | 0.71 s | 0.12 s | 1373 MiB |
47
+ | 復元する実行 | 0.81 s | 0.70 s | 0.08 s | 945 MiB |
48
+
49
+ `/app/main.rb`(`--mapdir`、スナップショット4.5 MiB):
50
+
51
+ | 条件 | プロセス全体 | `load_from_buffer` | スナップショットの読み書き | ピークRSS |
52
+ |---|---:|---:|---:|---:|
53
+ | 変更前 | 1.50 s | 0.72 s | — | 1102 MiB |
54
+ | 変更後、スナップショットなし | 1.49 s | 0.70 s | — | 1021 MiB |
55
+ | 保存する実行 | 1.60 s | 0.70 s | 0.09 s | 1323 MiB |
56
+ | 復元する実行 | 1.26 s | 0.70 s | 0.06 s | 960 MiB |
57
+
58
+ `-e` では、復元する実行のプロセス全体が変更前より46%短い。残りの大半はMIR2キャッシュの読み込み(`load_from_buffer`)で、[ADR 0009](adr/0009-efflorescence-mir2-cache-integrity-modes.md)と[ADR 0010](adr/0010-efflorescence-mir2-cache-function-restore.md)の対象になる。スクリプトファイルでは、チェックポイントがスクリプトを開く `path_open` の直前(実行命令の31%の位置)になるため、短縮は16%にとどまる。
59
+
60
+ CRuby + YJITでも同じ `-e` で、MIR2キャッシュのヒットと復元を合わせたプロセス全体は1.94秒だった(1回の計測)。
@@ -0,0 +1,61 @@
1
+ # Efflorescenceのglobal整数配列化
2
+
3
+ VMはglobalの値をレジスタと同じ整数表現で保持する。公開関数とstart関数の入口、ホスト呼び出しの前後、実行を抜けるときに `Store` と同期する。書き戻しではビット列が変わった値だけをbox化する。[ADR 0011](adr/0011-efflorescence-unboxed-globals.md)
4
+
5
+ globalを持たないモジュールでは、VMの初期化時の判定で同期を省く。
6
+
7
+ ## 計測環境
8
+
9
+ 2026-11-12にApple M3 Pro、macOS 27.0.1 arm64で計測した。変更前はWardite `94a60e566a7349092697c9a5efabe3e5afbeb062`。Spinelの両バイナリはCIと同じ `6e7fc1355` にリポジトリのIO::Bufferパッチ2件を当てたコンパイラで `-O2` ビルドし、既定の整数overflowチェックを使った。CRubyは4.0.9。
10
+
11
+ ## Ruby 4.0.9 Wasm
12
+
13
+ 同じWasmで `--disable-gems -e 'puts RUBY_DESCRIPTION'` を実行した。SpinelとCRubyのMIR2キャッシュをそれぞれ変更前のバイナリで作り、全構成のウォームアップを1回行った。構成ごとに新規プロセスで5回計測し、反復ごとに実行順を反転した。各回でキャッシュヒットを確認し、標準出力をWasmtimeと照合した。ゲスト実行時間はプロセスの経過時間から `load_from_buffer` を引いた値。OSのファイルキャッシュはウォーム。
14
+
15
+ | ランタイム | ゲスト実行の中央値と範囲(秒) | プロセス全体の中央値(秒) |
16
+ |---|---|---|
17
+ | Spinel | 0.710(0.695–0.716) → 0.540(0.534–0.558)、23.9%短縮 | 1.225 → 1.065 |
18
+ | CRuby | 10.954(10.839–11.251) → 10.448(10.356–10.531)、4.6%短縮 | 12.146 → 11.648 |
19
+ | CRuby + YJIT | 7.456(7.418–7.527) → 7.319(7.242–7.515)、1.8%短縮 | 8.258 → 8.122 |
20
+
21
+ ```sh
22
+ baseline_dir=$(mktemp -d)
23
+ git archive 94a60e5 | tar -x -C "$baseline_dir"
24
+ make -C "$baseline_dir" spinel
25
+ make spinel
26
+ RUNS=5 WARMUPS=1 ruby benchmark/measure_ruby_wasm_cache_hit.rb \
27
+ "$ruby_wasm" "$baseline_dir" "$baseline_dir/build/wardite" build/wardite
28
+ ```
29
+
30
+ ## 小規模ベンチマーク
31
+
32
+ `measure_m4.rb` で既存のi32・i64・float・callの入力を実行した。i32とcallは10万回、i64とfloatは2万回のループを使い、各構成でウォームアップ2回、計測11回。各サンプルの結果をWasmtimeまたは既知のchecksumと照合した。
33
+
34
+ Storeは `i32_memory.wat` と `call_cost_worker.rb` を使い、seed 123、3,000回のループ、インライン化なしで計測した。2回のウォームアップ後、CRubyとSpinelの前後を交互に各11回実行した。floatの公開APIは `measure_m5_boundary.rb` の30,000呼び出しを使い、新規プロセス4回の各5試行のうち最初の1試行を除いて集計した。これらのCRuby計測はYJITを無効にした。
35
+
36
+ `global_update` の中央値はSpinelで0.188 → 0.126 ms(33.0%短縮)、CRubyで2.359 → 1.930 ms(18.2%短縮)だった。
37
+ CRubyで中央値が増えたケースの最大は `copy_fill` の1.8%。Spinelでは `direct_host` が8.034 → 8.242 ms(2.6%増)、`indirect_host` が13.578 → 13.963 ms(2.8%増)だった。中央値が増えた各ケースで、変更前後のサンプル範囲は重なった。
38
+
39
+ [計測データ](../benchmark/results/efflorescence-unboxed-globals.json)
40
+
41
+ ## CPUプロファイル
42
+
43
+ 同じRuby 4.0.9のWasmをMIR2キャッシュヒットで実行し、InstrumentsのTime Profilerで前後各1回、1 ms間隔で採取した。表はスタックに `sp_VM_execute` または `sp_VM_execute_flat` があるサンプルのうち、各関数が最上位だった回数。回帰試験と並行して採取し、上の経過時間の計測とは別に実施した。
44
+
45
+ | 関数 | 変更前(1,168サンプル) | 変更後(566サンプル) |
46
+ |---|---:|---:|
47
+ | `sp_class_le_mod` / `sp_class_anc_walk` / `sp_class_anc_step` | 64 | 0 |
48
+ | `sp_poly_bitop` | 20 | 0 |
49
+ | `sp_pd_58a8a817` | 23 | 0 |
50
+ | `sp_poly_truthy` | 4 | 1 |
51
+ | `sp_VM_execute_flat` | 840 | 468 |
52
+ | `sp_FramePool_acquire` | 52 | 8 |
53
+ | `sp_VM_calculate` | 25 | 23 |
54
+
55
+ ## 検証
56
+
57
+ - CRubyとSpinelを含むテストは276件、29,278 assertions、失敗・エラー・省略0件。Steepの型チェックも通過した。
58
+ - parityはCRubyとSpinelで各436フィクスチャをWasmtime 49.0.0と照合した。
59
+ - `verify_ruby_wasm.rb` は `-e`、`require_relative`、`exit 7` の出力と終了コードが両ランタイムでWasmtimeと一致した。MIR2キャッシュのmissとhitも確認した。
60
+ - CRubyとSpinelの境界テストで、start関数、ホストの読み書き、再入、trap、未捕捉Wasm例外、ホスト例外、i64の符号と浮動小数点NaNのペイロードを検証した。Wasmバイナリのglobal importは現行ローダーで未対応のため、インスタンス間の可視性はRubyの `Store` APIで同じ可変 `Global` を共有して検証した。
61
+ - Spinelの生成Cで、全8種類のglobal命令が `sp_IntArray` を使い、`sp_poly_` と `sp_box_` を呼ばないことを回帰テストに追加した。[生成Cの検査](efflorescence-generated-c-guards.md)
@@ -0,0 +1,144 @@
1
+ # Efflorescence VM (Phase 1)
2
+
3
+ [日本語](efflorescence-ja.md)
4
+
5
+ Current coverage includes [M1 i32, structured control and new EH](efflorescence-m1.md),
6
+ [M2 memory and globals](efflorescence-m2.md), [M3 host and indirect calls](efflorescence-m3.md),
7
+ [M4 i64](efflorescence-m4.md), and [M5 floating point](efflorescence-m5.md).
8
+ [M6](efflorescence-m6.md) verifies the M0 coverage baseline and the additional EH opcodes.
9
+ The Phase 1 limitations below are historical.
10
+
11
+ This document records Phase 1 (`67fbfae`). Current Efflorescence enables the
12
+ [Phase 2 optimizations](efflorescence-phase2.md) by default; `--mir-disable all`
13
+ selects unoptimized MIR lowered to the same flat bytecode executor. Wardite's default is still the stack VM.
14
+
15
+ Efflorescence is Wardite's opt-in, pure Ruby register interpreter. The default
16
+ remains `Wardite::Runtime`, the stack interpreter. Both engines share the binary
17
+ loader, decoded Wasm instructions, store and exports; execution is independent.
18
+
19
+ ```sh
20
+ wasm-tools parse benchmark/i32_mix.wat -o build/i32_mix.wasm
21
+ ruby -Ilib exe/wardite --new-engine --no-wasi --invoke inline_mix build/i32_mix.wasm 100000 123
22
+ ruby -Ilib exe/wardite --new-engine --no-wasi --invoke call_mix build/i32_mix.wasm 100000 123
23
+ # Both: return value: I32(-424175775)
24
+ ```
25
+
26
+ The equivalent Ruby API is:
27
+
28
+ ```ruby
29
+ require "wardite"
30
+ instance = Wardite.new(path: "build/i32_mix.wasm", enable_wasi: false, engine: :efflorescence)
31
+ instance.runtime.call("call_mix", [100000, 123]) # Wardite::I32, same as Runtime
32
+ instance.runtime.call_unboxed("call_mix", [100000, 123]) # 3870791521 (Integer)
33
+ ```
34
+
35
+ Spinel uses the same sources (`make spinel`). Invoke exported functions through
36
+ `runtime.call` or the CLI: Spinel currently does not dispatch undefined methods
37
+ through Ruby's `method_missing` (including the existing VM's convenience API).
38
+ No Spinel-specific runtime API or global integer-overflow option is needed.
39
+
40
+ ## MIR and execution
41
+
42
+ All new implementation classes live under `Wardite::Efflorescence`:
43
+
44
+ | Class | Responsibility |
45
+ | --- | --- |
46
+ | `IR` | Module's translated functions, indexed by Wasm function index |
47
+ | `Function` | Index, parameter/result types, local count, register count, initial register values, instructions |
48
+ | `Instruction` / `Opcode` | Integer operands and integer opcode constants |
49
+ | `Translator` / `Translator::Control` | Abstract operand stack, structured labels, snapshots, result joins and PC fixups |
50
+ | `Frame` | Function, Integer register array, return destination and saved PC |
51
+ | `VM` | Export checks, Ruby boundary conversion, independent dispatch and explicit call frames |
52
+
53
+ `Instruction` wraps an `Array<Integer>`. The wrapper gives Spinel a typed operand
54
+ accessor even when it represents the outer instruction table as a generic array.
55
+ Instructions are allocated during translation, never during dispatch.
56
+
57
+ | MIR form | Meaning |
58
+ | --- | --- |
59
+ | `[COPY, dst, src]` | Copy register |
60
+ | `[I32_ADD/SUB/MUL/XOR/SHL/SHR_U, dst, lhs, rhs]` | Wasm binary operation |
61
+ | `[I32_EQZ, dst, src]` | Zero test |
62
+ | `[BR, pc]`, `[BR_IF, condition, pc]` | Absolute MIR target |
63
+ | `[CALL, dst, function_index, *argument_registers]` | Internal call (`dst = -1` for void) |
64
+ | `[RETURN, src]` | Return (`src = -1` for void) |
65
+ | `[TRAP]` | Wasm `unreachable` |
66
+
67
+ Locals occupy the first registers. `local.get` pushes a source in the translator,
68
+ without emitting a runtime instruction. Before overwriting a local that remains
69
+ on the abstract stack, the translator copies its old value to a temporary and
70
+ rewrites those sources. At block/loop entry, live local sources are snapshotted
71
+ before the branch target, preserving stack prefixes across edges and backedges.
72
+ `local.tee` uses the same mechanism. Constants are deduplicated into immutable
73
+ initial register slots; `i32.const` emits no runtime instruction.
74
+
75
+ Each arithmetic result has a temporary slot. Result-bearing blocks/functions
76
+ have join slots; incoming branches and normal completion copy into those slots.
77
+ Loop branches target the header and carry no arguments; block branches target
78
+ the end and carry their result. Unconditional transfers make the rest of that
79
+ control region unreachable. There are no runtime Wasm labels or operand stack.
80
+
81
+ Registers always contain unsigned `0..0xffffffff` Ruby Integers. Add/subtract
82
+ wrap, shift counts use their low five bits, and multiply uses 16-bit limbs to
83
+ avoid overflowing Spinel's signed native word before masking the result. Internal
84
+ calls copy Integers between independent frames. Only `VM#call` boxes the final
85
+ Ruby-facing result; `call_unboxed` does not. Calls use an explicit frame array,
86
+ so guest recursion does not consume Ruby's method call stack.
87
+
88
+ No optimization pass is required to execute this MIR. Future passes can operate
89
+ on each Function between translation and execution. Conservative COPYs and
90
+ temporary registers are intentionally retained for Phase 1.
91
+
92
+ ## Scope and validation
93
+
94
+ Supported: i32 parameters/results/locals/constants, get/set/tee, add/sub/mul/xor,
95
+ shl/shr_u/eqz, block/loop/br/br_if/end, call/return, nop/drop/unreachable and
96
+ function exports. Blocks and loops can have zero or one i32 result; loop
97
+ parameters and type-index block signatures are unsupported.
98
+
99
+ Imports/host functions, WASI calls, memory/global/table instructions, if/else,
100
+ other numeric operations, multi-value signatures and advanced Wasm proposals
101
+ raise `UnsupportedFeature`. Every defined function is translated eagerly, so an
102
+ unsupported instruction is rejected even in dead code or an unexported function.
103
+ There is no fallback to `Wardite::Runtime`. The shared loader's existing binary
104
+ decoding and validation limitations still apply.
105
+
106
+ ```sh
107
+ bundle exec rake test
108
+ bundle exec rake check
109
+ make spinel-test RUBY="bundle exec ruby"
110
+ ruby benchmark/compare_engines.rb 100000 123
111
+ ```
112
+
113
+ Tests compile the **entire unchanged** `benchmark/i32_mix.wat` using the existing
114
+ `wasm-tools parse` flow. No prebuilt fixture or reduced workload is substituted.
115
+ All eight specified input pairs run against both exports, both engines and an
116
+ independent modulo-based reference on CRuby and Spinel. Additional tests cover
117
+ snapshots, result branches, loop prefixes, recursion, void calls, normalization,
118
+ shift counts, unsigned products, unsupported features and CLI selection. A
119
+ TracePoint regression checks that 1,000 internal calls allocate just one I32 at
120
+ the Ruby return boundary and never execute a `Wardite::Runtime` method.
121
+
122
+ Verified on 2026-11-03 with CRuby 4.0.9 and Spinel `2026.10.25+1967`
123
+ (`6e7fc1355`, default integer overflow checks): **75 tests, 1,106 assertions,
124
+ zero failures/errors/omissions**, with `WARDITE_SPINEL_BIN` enabled for the full
125
+ suite. `bundle exec rake check` also passed. Both exports on both engines and
126
+ both runtimes produced these unsigned results:
127
+
128
+ | Rounds | Seed | Checksum |
129
+ | ---: | ---: | ---: |
130
+ | 0 | 0 | 0 |
131
+ | 0 | 123 | 123 |
132
+ | 1 | 0 | 186080479 |
133
+ | 1 | 123 | 434058398 |
134
+ | 10 | 123 | 483678737 |
135
+ | 1000 | 123 | 1949289519 |
136
+ | 1000 | 4294967295 | 1895198137 |
137
+ | 100000 | 123 | 3870791521 |
138
+
139
+ [Recorded performance comparison](../benchmark/README.md#phase-1-sample-2026-11-03).
140
+
141
+ Next steps: reduce COPYs and temporary count in independent passes, specialize
142
+ immediates, improve instruction-table representation for Spinel, reuse frames,
143
+ and extend supported Wasm operations/control constructs. Performance is not a
144
+ Phase 1 acceptance criterion.