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,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "rbconfig"
5
+ require "shellwords"
6
+ require "tmpdir"
7
+
8
+ # All four loops have the same control flow and increment by one. Table calls
9
+ # add a selector, and host calls use the same one-argument ABI in both paths.
10
+ calls = {
11
+ "direct_defined" => "call $defined",
12
+ "direct_host" => "call $host",
13
+ "indirect_defined" => "i32.const 0 call_indirect (type $increment)",
14
+ "indirect_host" => "i32.const 1 call_indirect (type $increment)"
15
+ }
16
+ loops = calls.map do |name, call|
17
+ <<~WAT
18
+ (func (export "#{name}") (param $n i32) (param $sum i32) (result i32)
19
+ block $done
20
+ loop $repeat
21
+ local.get $n i32.eqz br_if $done
22
+ local.get $sum #{call} local.set $sum
23
+ local.get $n i32.const 1 i32.sub local.set $n
24
+ br $repeat
25
+ end
26
+ end
27
+ local.get $sum)
28
+ WAT
29
+ end
30
+ wat = <<~WAT
31
+ (module
32
+ (type $increment (func (param i32) (result i32)))
33
+ (import "env" "increment" (func $host (type $increment)))
34
+ (func $defined (type $increment) local.get 0 i32.const 1 i32.add)
35
+ (table 2 funcref)
36
+ (elem (i32.const 0) func $defined $host)
37
+ #{loops.join})
38
+ WAT
39
+
40
+ root = File.expand_path("..", __dir__)
41
+ worker = File.join(__dir__, "m3_call_worker.rb")
42
+ rounds = Integer(ENV.fetch("ROUNDS", "100000"))
43
+ runs = Integer(ENV.fetch("RUNS", "7"))
44
+ warmups = Integer(ENV.fetch("WARMUPS", "2"))
45
+ abort "invalid rounds, runs or warmups" unless rounds.between?(0, 0x7fffffff) && runs > 0 && warmups >= 0
46
+ checksum = (rounds + 123) & 0xffffffff
47
+
48
+ def capture!(*command)
49
+ out, err, status = Open3.capture3(*command)
50
+ abort "#{command.inspect}\n#{out}#{err}" unless status.success?
51
+ out
52
+ end
53
+
54
+ def stats(samples)
55
+ sorted = samples.sort
56
+ median = (sorted.fetch((sorted.size - 1) / 2) + sorted.fetch(sorted.size / 2)) / 2
57
+ [median, sorted.first, sorted.last].map { |seconds| format("%.3f", seconds * 1000) }
58
+ end
59
+
60
+ Dir.mktmpdir("wardite-m3-bench") do |dir|
61
+ source = File.join(dir, "calls.wat")
62
+ binary = File.join(dir, "calls.wasm")
63
+ native = File.join(dir, "m3-worker")
64
+ File.write(source, wat)
65
+ capture!("wasm-tools", "parse", source, "-o", binary)
66
+ compiler = Shellwords.split(ENV.fetch("SPINEL", "spinel"))
67
+ flags = Shellwords.split(ENV.fetch("SPINELFLAGS", "-O2"))
68
+ capture!(*compiler, "-I#{root}/lib", "--strict-require", *flags, worker, "-o", native)
69
+ runners = {
70
+ "CRuby" => [RbConfig.ruby, "--disable-yjit", "-I#{root}/lib", worker],
71
+ "Spinel" => [native]
72
+ }
73
+
74
+ puts "runtime,case,rounds,checksum,load_median_ms,load_min_ms,load_max_ms,execute_median_ms,execute_min_ms,execute_max_ms"
75
+ runners.each do |runtime, command|
76
+ samples = calls.keys.to_h { |name| [name, []] }
77
+ (runs + warmups).times do |iteration|
78
+ calls.keys.rotate(iteration).each do |name|
79
+ load, execute, actual = capture!(*command, binary, name, rounds.to_s).strip.split(",")
80
+ abort "checksum mismatch for #{runtime}/#{name}: #{actual}" unless Integer(actual) == checksum
81
+ samples.fetch(name) << [Float(load), Float(execute)] if iteration >= warmups
82
+ end
83
+ end
84
+ samples.each do |name, observations|
85
+ load, execute = observations.transpose
86
+ puts [runtime, name, rounds, checksum, *stats(load), *stats(execute)].join(",")
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Usage: bundle exec ruby -Ilib benchmark/measure_m4.rb /path/to/baseline [M3|M4|M5] [current-label]
4
+ require "wardite"
5
+ require "open3"
6
+ require "rbconfig"
7
+ require "shellwords"
8
+ require "tmpdir"
9
+
10
+ root = File.expand_path("..", __dir__)
11
+ baseline = File.expand_path(ARGV.fetch(0))
12
+ previous = ARGV.fetch(1, "M3")
13
+ current = ARGV.fetch(2) { { "M3" => "M4", "M4" => "M5", "M5" => "M6" }.fetch(previous) }
14
+ worker = File.join(__dir__, "m3_call_worker.rb")
15
+ rounds = Integer(ENV.fetch("ROUNDS", "100000"))
16
+ i64_rounds = Integer(ENV.fetch("I64_ROUNDS", "20000"))
17
+ runs = Integer(ENV.fetch("RUNS", "7"))
18
+ warmups = Integer(ENV.fetch("WARMUPS", "2"))
19
+ abort "invalid rounds or runs" unless [rounds, i64_rounds].all? { |n| n.between?(1, 0x7fffffff) } && runs > 0 && warmups >= 0
20
+
21
+ calls = {
22
+ "direct_defined" => "call $defined",
23
+ "direct_host" => "call $host",
24
+ "indirect_defined" => "i32.const 0 call_indirect (type $increment)",
25
+ "indirect_host" => "i32.const 1 call_indirect (type $increment)"
26
+ }
27
+ call_wat = <<~WAT
28
+ (module
29
+ (type $increment (func (param i32) (result i32)))
30
+ (import "env" "increment" (func $host (type $increment)))
31
+ (func $defined (type $increment) local.get 0 i32.const 1 i32.add)
32
+ (table 2 funcref)
33
+ (elem (i32.const 0) func $defined $host)
34
+ #{calls.map do |name, call|
35
+ <<~FUNC
36
+ (func (export "#{name}") (param $n i32) (param $sum i32) (result i32)
37
+ block $done
38
+ loop $repeat
39
+ local.get $n i32.eqz br_if $done
40
+ local.get $sum #{call} local.set $sum
41
+ local.get $n i32.const 1 i32.sub local.set $n
42
+ br $repeat
43
+ end
44
+ end
45
+ local.get $sum)
46
+ FUNC
47
+ end.join})
48
+ WAT
49
+
50
+ def capture!(*command)
51
+ output, error, status = Open3.capture3(*command)
52
+ abort "#{command.inspect}\n#{output}#{error}" unless status.success?
53
+ output.strip
54
+ end
55
+
56
+ def stats(samples)
57
+ sorted = samples.sort
58
+ median = (sorted.fetch((sorted.size - 1) / 2) + sorted.fetch(sorted.size / 2)) / 2
59
+ [median, sorted.first, sorted.last].map { |seconds| format("%.3f", seconds * 1000) }
60
+ end
61
+
62
+ def allocation_probe(binary, export, rounds, expected)
63
+ vm = Wardite.new(path: binary, engine: :efflorescence, enable_wasi: false).runtime
64
+ args = [rounds, 123]
65
+ vm.call_unboxed(export, args)
66
+ start = GC.stat(:total_allocated_objects)
67
+ actual = vm.call_unboxed(export, args)
68
+ allocated = GC.stat(:total_allocated_objects) - start
69
+ abort "allocation probe checksum mismatch" unless actual == expected
70
+
71
+ boxes = 0
72
+ trace = TracePoint.new(:call) { |event| boxes += 1 if event.defined_class == Wardite::I64 && event.method_id == :initialize }
73
+ trace.enable { vm.call_unboxed(export, args) }
74
+ unboxed = boxes
75
+ trace.enable { vm.call(export, args) }
76
+ boxed = boxes - unboxed
77
+ abort "unexpected I64 allocations for #{export}: unboxed=#{unboxed}, boxed=#{boxed}" unless unboxed == 0 && boxed == 1
78
+
79
+ vm = Wardite.new(path: binary, engine: :efflorescence, enable_wasi: false, mir_profile: true).runtime
80
+ abort "profile checksum mismatch" unless vm.call_unboxed(export, args) == expected
81
+ warn "#{export}: allocations=#{allocated}, I64 boxes=#{unboxed} unboxed/#{boxed} boxed, #{vm.metrics.report}"
82
+ end
83
+
84
+ Dir.mktmpdir("wardite-m4-bench") do |dir|
85
+ runners = {}
86
+ compiler = Shellwords.split(ENV.fetch("SPINEL", "spinel"))
87
+ flags = Shellwords.split(ENV.fetch("SPINELFLAGS", "-O2"))
88
+ { previous => baseline, current => root }.each do |version, path|
89
+ native = File.join(dir, "#{version}-worker")
90
+ capture!(*compiler, "-I#{path}/lib", "--strict-require", *flags, worker, "-o", native)
91
+ runners[["CRuby", version]] = [RbConfig.ruby, "--disable-yjit", "-I#{path}/lib", worker]
92
+ runners[["Spinel", version]] = [native]
93
+ end
94
+
95
+ call_source = File.join(dir, "calls.wat")
96
+ File.write(call_source, call_wat)
97
+ suites = {
98
+ File.join(__dir__, "i32_mix.wat") => [%w[inline_mix call_mix], rounds, false],
99
+ File.join(__dir__, "i32_call_arities.wat") => [%w[empty_loop call_zero call_one call_two call_three call_four], rounds, false],
100
+ call_source => [calls.keys, rounds, false],
101
+ File.join(__dir__, "i64_m4.wat") => [%w[alu division memory mixed_call], i64_rounds, true, previous == "M3"],
102
+ File.join(__dir__, "float_m5.wat") => [%w[float_alu float_convert float_mixed_call], i64_rounds, true, previous == "M4"]
103
+ }
104
+
105
+ suites.delete(File.join(__dir__, "float_m5.wat")) if previous == "M3"
106
+
107
+ puts "runtime,version,case,rounds,checksum,load_median_ms,load_min_ms,load_max_ms,execute_median_ms,execute_min_ms,execute_max_ms"
108
+ suites.each do |source, (exports, count, i64, current_only)|
109
+ binary = File.join(dir, "input.wasm")
110
+ capture!("wasm-tools", "parse", source, "-o", binary)
111
+ commands = current_only ? runners.select { |(_, version), _| version == current } : runners
112
+ exports.each do |export|
113
+ checksum = if source == call_source
114
+ (count + 123) % 2**32
115
+ else
116
+ result = Integer(capture!("wasmtime", "run", "--invoke", export, binary, count.to_s, "123"))
117
+ i64 ? result : result % 2**32
118
+ end
119
+ samples = commands.keys.to_h { |key| [key, []] }
120
+ (runs + warmups).times do |i|
121
+ commands.keys.rotate(i).each do |key|
122
+ load, execute, actual = capture!(*commands.fetch(key), binary, export, count.to_s).split(",")
123
+ abort "#{key}/#{export}: #{checksum} expected, #{actual} actual" unless Integer(actual) == checksum
124
+ samples.fetch(key) << [Float(load), Float(execute)] if i >= warmups
125
+ end
126
+ end
127
+ samples.each do |(runtime, version), observations|
128
+ load, execute = observations.transpose
129
+ puts [runtime, version, export, count, checksum, *stats(load), *stats(execute)].join(",")
130
+ end
131
+ allocation_probe(binary, export, count, checksum) if source.end_with?("/i64_m4.wat")
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # Run with a compiled benchmark/float_m5.wat.
5
+ require "wardite"
6
+
7
+ class FloatBoundaryBenchmark
8
+ # @rbs vm: Wardite::Efflorescence::VM
9
+ # @rbs rounds: Integer
10
+ # @rbs boxed: bool
11
+ # @rbs return: Float
12
+ def self.measure(vm, rounds, boxed)
13
+ arguments = [1.25]
14
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
15
+ index = 0
16
+ if boxed
17
+ while index < rounds
18
+ result = vm.call("float_identity", arguments)
19
+ raise "boxed result" unless result.is_a?(Wardite::F32) && result.value == 1.25
20
+ index += 1
21
+ end
22
+ else
23
+ while index < rounds
24
+ raise "unboxed result" unless vm.call_unboxed("float_identity", arguments) == 1.25
25
+ index += 1
26
+ end
27
+ end
28
+ (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000
29
+ end
30
+ end
31
+
32
+ vm = Wardite.new(path: ARGV.fetch(0), enable_wasi: false, engine: :efflorescence).runtime
33
+ raise "wrong engine" unless vm.is_a?(Wardite::Efflorescence::VM)
34
+ rounds = ARGV.fetch(1, "30000").to_i
35
+ 5.times do |trial|
36
+ boxed_first = trial.odd?
37
+ first = FloatBoundaryBenchmark.measure(vm, rounds, boxed_first)
38
+ second = FloatBoundaryBenchmark.measure(vm, rounds, !boxed_first)
39
+ unboxed, boxed = boxed_first ? [second, first] : [first, second]
40
+ puts "#{unboxed},#{boxed}"
41
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Usage: ruby benchmark/measure_mir_cache_verify.rb RUBY_WASM BASELINE_BIN CURRENT_BIN
4
+ require "digest"
5
+ require "json"
6
+ require "open3"
7
+ require "tmpdir"
8
+
9
+ abort "Usage: ruby #{$PROGRAM_NAME} RUBY_WASM BASELINE_BIN CURRENT_BIN" unless ARGV.size == 3
10
+ wasm, baseline, current = ARGV.map { |path| File.expand_path(path) }
11
+ runs = Integer(ENV.fetch("RUNS", "9"))
12
+ warmups = Integer(ENV.fetch("WARMUPS", "2"))
13
+ abort "Invalid run or warmup count" unless runs >= 3 && warmups >= 0
14
+
15
+ guest = ["--disable-gems", "-e", "puts RUBY_VERSION"]
16
+ expected, status = Open3.capture2("wasmtime", "run", wasm, *guest)
17
+ abort "Wasmtime did not run Ruby 4.0.9" unless status.success? && expected == "4.0.9\n"
18
+
19
+ def invoke(command, expected, *traces)
20
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
21
+ stdout, stderr, status = Open3.capture3({"WARDITE_TRACE" => "1"}, *command, unsetenv_others: true)
22
+ total = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
23
+ load_time = stderr[/^load_from_buffer: (\S+)$/, 1]
24
+ unless status.success? && stdout == expected && load_time && traces.all? { |trace| stderr.include?(trace) }
25
+ abort "#{command.inspect}\n#{stdout}#{stderr}"
26
+ end
27
+ {load_seconds: Float(load_time), process_seconds: total}
28
+ end
29
+
30
+ samples = Dir.mktmpdir("wardite-mir-cache-verify") do |directory|
31
+ baseline_cache = [baseline, "--new-engine", "--mir-cache", File.join(directory, "before")]
32
+ current_cache = [current, "--new-engine", "--mir-cache", File.join(directory, "after")]
33
+ inputs = ["--", wasm, *guest]
34
+ snapshot = ->(version) { ["--snapshot", File.join(directory, "#{version}-snapshot")] }
35
+ invoke([*baseline_cache, *snapshot.call("before"), *inputs], expected, "MIR2 cache miss:", "snapshot saved:")
36
+ invoke([*current_cache, *snapshot.call("after"), *inputs], expected, "MIR2 cache miss:", "snapshot saved:")
37
+
38
+ [false, true].flat_map do |with_snapshot|
39
+ commands = {
40
+ "before" => baseline_cache,
41
+ "sha256" => [*current_cache, "--mir-cache-verify", "sha256"],
42
+ "checksum" => [*current_cache, "--mir-cache-verify", "checksum"],
43
+ }
44
+ traces = ["MIR2 cache hit:"]
45
+ traces << "snapshot restored:" if with_snapshot
46
+ (warmups + runs).times.flat_map do |iteration|
47
+ commands.keys.rotate(iteration).filter_map do |mode|
48
+ command = commands.fetch(mode)
49
+ command = [*command, *snapshot.call(mode == "before" ? "before" : "after")] if with_snapshot
50
+ sample = invoke([*command, *inputs], expected, *traces)
51
+ sample.merge(mode: mode, snapshot: with_snapshot, run: iteration - warmups + 1) if iteration >= warmups
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ puts JSON.pretty_generate(wasm_sha256: Digest::SHA256.file(wasm).hexdigest, guest_arguments: guest,
58
+ runs: runs, warmups: warmups, samples: samples)
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Usage: ruby benchmark/measure_ruby_wasm_cache_hit.rb RUBY_WASM BASELINE BASELINE_BIN CURRENT_BIN
4
+ require "digest"
5
+ require "json"
6
+ require "open3"
7
+ require "rbconfig"
8
+ require "tmpdir"
9
+
10
+ wasm, baseline, baseline_bin, current_bin = ARGV.map { |path| File.expand_path(path) }
11
+ abort "Usage: ruby #{$PROGRAM_NAME} RUBY_WASM BASELINE BASELINE_BIN CURRENT_BIN" unless ARGV.size == 4
12
+ current = File.expand_path("..", __dir__)
13
+ runs = Integer(ENV.fetch("RUNS", "7"))
14
+ warmups = Integer(ENV.fetch("WARMUPS", "2"))
15
+ abort "Invalid run or warmup count" unless runs >= 3 && warmups >= 0
16
+
17
+ guest = ["--disable-gems", "-e", "puts RUBY_DESCRIPTION"]
18
+ expected, status = Open3.capture2("wasmtime", "run", wasm, *guest)
19
+ abort "Wasmtime did not run Ruby 4.0.9" unless status.success? && expected.start_with?("ruby 4.0.9 ")
20
+
21
+ commands = [
22
+ ["Spinel", "before", [baseline_bin]],
23
+ ["Spinel", "after", [current_bin]],
24
+ ]
25
+ { "CRuby" => "--disable-yjit", "CRuby YJIT" => "--yjit" }.each do |runtime, option|
26
+ { "before" => baseline, "after" => current }.each do |version, root|
27
+ commands << [runtime, version, [RbConfig.ruby, option, "-I#{root}/lib", "#{root}/exe/wardite"]]
28
+ end
29
+ end
30
+
31
+ samples = Dir.mktmpdir("wardite-cache-hit") do |cache|
32
+ args = ["--new-engine", "--mir-cache", cache, "--", wasm, *guest]
33
+ run = lambda do |command, cache_status|
34
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
35
+ stdout, stderr, status = Open3.capture3({ "WARDITE_TRACE" => "1" }, *command, *args)
36
+ total = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
37
+ load = stderr[/^load_from_buffer: (\S+)$/, 1]
38
+ unless status.success? && stdout == expected && stderr.include?("MIR2 cache #{cache_status}:") && load
39
+ abort "#{command.inspect}\n#{stdout}#{stderr}"
40
+ end
41
+ load = Float(load)
42
+ { process_seconds: total, load_seconds: load, guest_seconds: total - load }
43
+ end
44
+
45
+ # MIR2 keys include the runtime, so prime both native and CRuby entries.
46
+ commands.select { |runtime, version, _| version == "before" && runtime != "CRuby YJIT" }.each do |_, _, command|
47
+ run.call(command, "miss")
48
+ end
49
+ (warmups + runs).times.flat_map do |iteration|
50
+ order = iteration.even? ? commands : commands.reverse
51
+ order.filter_map do |runtime, version, command|
52
+ result = run.call(command, "hit")
53
+ result.merge(runtime: runtime, version: version, run: iteration - warmups + 1) if iteration >= warmups
54
+ end
55
+ end
56
+ end
57
+
58
+ puts JSON.pretty_generate(wasm_sha256: Digest::SHA256.file(wasm).hexdigest, guest: expected.strip,
59
+ guest_arguments: guest, host_ruby: RUBY_DESCRIPTION, warmups: warmups, runs: runs, samples: samples)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "wardite"
5
+ require "wardite/cli/cli"
6
+
7
+ module StartupMeasurement
8
+ SECONDS = Hash.new(0.0)
9
+
10
+ def self.measure(klass, method_name, phase)
11
+ wrapper = Module.new do
12
+ define_method(method_name) do |*args, **kwargs, &block|
13
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
14
+ super(*args, **kwargs, &block)
15
+ ensure
16
+ SECONDS[phase] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
17
+ end
18
+ private method_name if klass.private_method_defined?(method_name)
19
+ end
20
+ klass.prepend(wrapper)
21
+ end
22
+ end
23
+
24
+ wasm = ARGV.shift || abort("Usage: ruby -Ilib #{$PROGRAM_NAME} RUBY.wasm [--mir-cache DIR]")
25
+ efflorescence = Wardite::Efflorescence
26
+ StartupMeasurement.measure(Wardite::BinaryLoader.singleton_class, :sections, :decode)
27
+ StartupMeasurement.measure(Wardite::Store, :initialize, :store)
28
+ StartupMeasurement.measure(efflorescence::Translator, :translate, :translate)
29
+ StartupMeasurement.measure(efflorescence::Optimizer, :optimize, :optimize)
30
+ StartupMeasurement.measure(efflorescence::RegisterValidation, :validate, :validate)
31
+ StartupMeasurement.measure(efflorescence::Lowerer, :compile, :lower)
32
+ StartupMeasurement.measure(efflorescence::VM, :call, :execute)
33
+ StartupMeasurement.measure(Wardite::Cli::Cli, :new_instance, :load)
34
+ if ARGV.include?("--mir-cache")
35
+ require "wardite/efflorescence/mir_cache"
36
+ StartupMeasurement.measure(efflorescence::MIRCache, :read, :cache_read)
37
+ StartupMeasurement.measure(efflorescence::MIRCache, :write, :cache_write)
38
+ end
39
+
40
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
41
+ Wardite::Cli::Cli.new(["--new-engine", *ARGV, "--", wasm, "--disable-gems", "-e", "puts RUBY_VERSION; puts 6*7"]).run
42
+ StartupMeasurement::SECONDS[:total] = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
43
+ warn JSON.generate(ruby: RUBY_DESCRIPTION, yjit: RubyVM::YJIT.enabled?, seconds: StartupMeasurement::SECONDS)
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # CRuby. Compare the boxed and unboxed WASI paths in the same Efflorescence VM.
4
+ require "wardite"
5
+ require "wardite/wasi"
6
+ require "open3"
7
+ require "tmpdir"
8
+
9
+ wat = <<~WAT
10
+ (module
11
+ (import "wasi_snapshot_preview1" "fd_write" (func $write (param i32 i32 i32 i32) (result i32)))
12
+ (memory 1)
13
+ (data (i32.const 0) "\\20\\00\\00\\00\\04\\00\\00\\00")
14
+ (data (i32.const 32) "data")
15
+ (func (export "run") (param $rounds i32) (result i32) (local $bytes i32)
16
+ block $done
17
+ loop $repeat
18
+ local.get $rounds i32.eqz br_if $done
19
+ i32.const 1 i32.const 0 i32.const 1 i32.const 8 call $write
20
+ if unreachable end
21
+ local.get $bytes i32.const 8 i32.load i32.add local.set $bytes
22
+ local.get $rounds i32.const 1 i32.sub local.set $rounds
23
+ br $repeat
24
+ end
25
+ end
26
+ local.get $bytes))
27
+ WAT
28
+
29
+ rounds = Integer(ENV.fetch("ROUNDS", "100000"))
30
+ runs = Integer(ENV.fetch("RUNS", "5"))
31
+ abort "invalid rounds or runs" unless rounds.between?(1, 0x3fffffff) && runs.positive?
32
+ Dir.mktmpdir("wardite-wasi-bench") do |dir|
33
+ wat_path = File.join(dir, "write.wat")
34
+ wasm_path = File.join(dir, "write.wasm")
35
+ File.write(wat_path, wat)
36
+ output, status = Open3.capture2e("wasm-tools", "parse", wat_path, "-o", wasm_path)
37
+ abort output unless status.success?
38
+
39
+ measurements = {"boxed" => [], "unboxed" => []}
40
+ File.open(IO::NULL, "wb") do |sink|
41
+ sink.sync = true
42
+ (runs + 2).times do |index|
43
+ measurements.keys.rotate(index).each do |mode|
44
+ if mode == "boxed"
45
+ wasi = Wardite::WasiSnapshotPreview1.new
46
+ instance = Wardite.new(path: wasm_path, engine: :efflorescence, enable_wasi: false,
47
+ import_object: {wasi_snapshot_preview1: wasi})
48
+ else
49
+ instance = Wardite.new(path: wasm_path, engine: :efflorescence)
50
+ wasi = instance.wasi
51
+ end
52
+ wasi.fd_table[1] = sink
53
+ vm = instance.runtime
54
+ native = vm.ir.hosts.fetch(0).unboxed_wasi
55
+ abort "unexpected ABI for #{mode}" unless !!native == (mode == "unboxed")
56
+
57
+ objects = GC.stat(:total_allocated_objects)
58
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
59
+ checksum = vm.call_unboxed(:run, [rounds])
60
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
61
+ allocations = GC.stat(:total_allocated_objects) - objects
62
+ abort "expected #{rounds * 4} bytes, got #{checksum}" unless checksum == rounds * 4
63
+ measurements.fetch(mode) << [elapsed * 1000, allocations] if index >= 2
64
+ end
65
+ end
66
+ end
67
+ puts "abi,rounds,bytes,median_ms,min_ms,max_ms,median_allocations"
68
+ measurements.each do |mode, observations|
69
+ times, objects = observations.transpose.map(&:sort)
70
+ low, high = (runs - 1) / 2, runs / 2
71
+ median_ms = (times[low] + times[high]) / 2
72
+ median_objects = (objects[low] + objects[high]) / 2
73
+ puts "%s,%d,%d,%.3f,%.3f,%.3f,%d" % [mode, rounds, rounds * 4, median_ms, times.first, times.last, median_objects]
74
+ end
75
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "wardite"
5
+
6
+ wasm = ARGV.fetch(0)
7
+ export = ARGV.fetch(1)
8
+ rounds = ARGV.fetch(2).to_i
9
+ seed = ARGV.fetch(3).to_i
10
+ disabled = ARGV.fetch(4, "").split(",").map { |name| name.to_sym }
11
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
12
+ instance = File.open(wasm, "rb") do |file|
13
+ Wardite::BinaryLoader.load_from_buffer(file, enable_wasi: false)
14
+ end
15
+ loaded = Process.clock_gettime(Process::CLOCK_MONOTONIC)
16
+ vm = Wardite::Efflorescence::VM.new(instance, disabled_optimizations: disabled)
17
+ translated = Process.clock_gettime(Process::CLOCK_MONOTONIC)
18
+ before = RUBY_ENGINE == "spinel" ? 0 : GC.stat(:total_allocated_objects)
19
+ execution_started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
20
+ result = vm.call(export, [rounds, seed]) || raise("missing checksum")
21
+ finished = Process.clock_gettime(Process::CLOCK_MONOTONIC)
22
+ after = RUBY_ENGINE == "spinel" ? 0 : GC.stat(:total_allocated_objects)
23
+ puts "#{loaded - started},#{translated - loaded},#{finished - execution_started},#{result.value},#{after - before}"