wardite 0.8.2 → 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/examples/load_perf.rb +24 -0
- data/lib/wardite/alu_f32.generated.rb +13 -10
- data/lib/wardite/alu_f64.generated.rb +13 -10
- data/lib/wardite/alu_i32.generated.rb +38 -31
- data/lib/wardite/alu_i64.generated.rb +48 -48
- data/lib/wardite/cli/cli.rb +73 -22
- data/lib/wardite/const.rb +1 -0
- data/lib/wardite/convert.generated.rb +7 -5
- 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 +460 -69
- data/lib/wardite/leb128.rb +35 -17
- data/lib/wardite/load.rb +260 -159
- data/lib/wardite/revisitor.rb +12 -4
- 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 +220 -105
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +90 -34
- data/scripts/templates/alu_module.rb.tmpl +7 -5
- data/scripts/templates/conv_module.rb.tmpl +7 -5
- data/sig/generated/wardite/alu_f32.generated.rbs +4 -3
- data/sig/generated/wardite/alu_f64.generated.rbs +4 -3
- data/sig/generated/wardite/alu_i32.generated.rbs +4 -3
- data/sig/generated/wardite/alu_i64.generated.rbs +4 -3
- data/sig/generated/wardite/cli/cli.rbs +13 -6
- data/sig/generated/wardite/const.rbs +2 -0
- data/sig/generated/wardite/convert.generated.rbs +4 -3
- 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 +35 -10
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +38 -13
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite.rbs +55 -18
- data/sig/vernier.rbs +3 -0
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/spin.toml +7 -0
- metadata +128 -3
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "digest"
|
|
5
|
+
require "json"
|
|
6
|
+
require "open3"
|
|
7
|
+
require "rbconfig"
|
|
8
|
+
require "shellwords"
|
|
9
|
+
|
|
10
|
+
Dir.chdir(File.expand_path("..", __dir__))
|
|
11
|
+
$stdout.sync = true
|
|
12
|
+
root = Dir.pwd
|
|
13
|
+
output = File.expand_path("build/benchmark/call-cost")
|
|
14
|
+
FileUtils.mkdir_p(output)
|
|
15
|
+
rounds = Integer(ARGV.fetch(0, "1000000"), 10)
|
|
16
|
+
seed = Integer(ARGV.fetch(1, "123"), 10)
|
|
17
|
+
runs = Integer(ENV.fetch("RUNS", "7"))
|
|
18
|
+
warmups = Integer(ENV.fetch("WARMUPS", "2"))
|
|
19
|
+
abort "invalid inputs" unless (0..0x7fffffff).cover?(rounds) && (0..0xffffffff).cover?(seed) && runs > 1 && warmups >= 0
|
|
20
|
+
compiler = Shellwords.split(ENV.fetch("SPINEL", "spinel"))
|
|
21
|
+
flags = Shellwords.split(ENV.fetch("SPINELFLAGS", "-O2"))
|
|
22
|
+
exports = %w[empty_loop call_identity inline_add_one call_add_one inline_mix call_mix]
|
|
23
|
+
variants = %w[full_reset locals_only fresh registers_only metadata_only]
|
|
24
|
+
if ENV["EXPORTS"]
|
|
25
|
+
requested = ENV.fetch("EXPORTS").split(",").uniq
|
|
26
|
+
abort "invalid EXPORTS" if requested.empty? || !(requested - exports).empty?
|
|
27
|
+
exports = requested
|
|
28
|
+
end
|
|
29
|
+
if ENV["VARIANTS"]
|
|
30
|
+
requested = ENV.fetch("VARIANTS").split(",").uniq
|
|
31
|
+
abort "invalid VARIANTS" if requested.empty? || !(requested - variants).empty?
|
|
32
|
+
variants = requested
|
|
33
|
+
end
|
|
34
|
+
version, status = Open3.capture2e(*compiler, "--version")
|
|
35
|
+
abort version unless status.success?
|
|
36
|
+
|
|
37
|
+
def run!(command, log)
|
|
38
|
+
abort "Failed: #{command.inspect}; see #{log}" unless system(*command, out: log, err: [:child, :out])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def replace_once(source, before, after)
|
|
42
|
+
raise "Experimental patch no longer matches current VM" unless source.scan(before).size == 1
|
|
43
|
+
source.sub(before, after)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def expected(export, rounds, seed)
|
|
47
|
+
case export
|
|
48
|
+
when "empty_loop", "call_identity" then seed
|
|
49
|
+
when "inline_add_one", "call_add_one" then (seed + rounds) % (2**32)
|
|
50
|
+
else
|
|
51
|
+
x = seed
|
|
52
|
+
rounds.times do
|
|
53
|
+
x ^= x / 8192
|
|
54
|
+
x = (x * 1664525 + 1013904223) % (2**32)
|
|
55
|
+
x = (x ^ (x * 128)) % (2**32)
|
|
56
|
+
end
|
|
57
|
+
x
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def measure(command, checksum, env = {})
|
|
62
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
63
|
+
stdout, stderr, status = Open3.capture3(env, *command)
|
|
64
|
+
e2e = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
65
|
+
fields = stdout.lines.first.to_s.strip.split(",")
|
|
66
|
+
abort "Checksum failure: #{command.inspect}\n#{stdout}\n#{stderr}" unless status.success? && fields.size == 4 && Integer(fields[3]) == checksum
|
|
67
|
+
{e2e: e2e, load: Float(fields[0]), translate: Float(fields[1]), execute: Float(fields[2]),
|
|
68
|
+
metrics: stdout.lines[1]&.strip, stderr: stderr}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def stats(values)
|
|
72
|
+
mean = values.sum / values.size
|
|
73
|
+
variance = values.sum { |value| (value - mean)**2 } / (values.size - 1)
|
|
74
|
+
{mean: mean, sd: Math.sqrt(variance), min: values.min, max: values.max}
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Build an auditable fixture from the unchanged acceptance benchmark, replacing
|
|
78
|
+
# only the loop body for the differential controls. No calls are inlined by MIR.
|
|
79
|
+
mix = File.read("benchmark/i32_mix.wat")
|
|
80
|
+
loop_function = mix[/ \(func \(export "call_mix"\).*?(?=\n\)\s*\z)/m] || raise("fixture changed")
|
|
81
|
+
call = " local.get $x\n call $mix\n local.set $x"
|
|
82
|
+
identity = loop_function.sub('export "call_mix"', 'export "call_identity"').sub("call $mix", "call $identity")
|
|
83
|
+
add = loop_function.sub('export "call_mix"', 'export "call_add_one"').sub("call $mix", "call $add_one")
|
|
84
|
+
empty = replace_once(loop_function.sub('export "call_mix"', 'export "empty_loop"'), call, "")
|
|
85
|
+
inline_add = add.sub('export "call_add_one"', 'export "inline_add_one"').sub("call $add_one", "i32.const 1\n i32.add")
|
|
86
|
+
helpers = " (func $identity (param i32) (result i32) local.get 0)\n" \
|
|
87
|
+
" (func $add_one (param i32) (result i32) local.get 0 i32.const 1 i32.add)\n"
|
|
88
|
+
wat = "#{mix.rstrip[0...-1]}\n#{helpers}\n#{[empty, identity, inline_add, add].join("\n\n")}\n)\n"
|
|
89
|
+
File.write("#{output}/call_cost.wat", wat)
|
|
90
|
+
wasm = "#{output}/call_cost.wasm"
|
|
91
|
+
run!(["wasm-tools", "parse", "#{output}/call_cost.wat", "-o", wasm], "#{output}/wat-build.log")
|
|
92
|
+
|
|
93
|
+
ir_source = File.read("lib/wardite/efflorescence/ir.rb")
|
|
94
|
+
# Keep the current constructor, validator, MIR and entry point identical. The
|
|
95
|
+
# baseline differs only in the body of Frame#reset, reconstructed in a copy.
|
|
96
|
+
local_reset = "index = @local_start\n while index < @local_end\n @registers[index] = 0"
|
|
97
|
+
full_reset = "index = 0\n while index < @function.register_count\n @registers[index] = @function.initial_value(index)"
|
|
98
|
+
full_ir_source = replace_once(ir_source, local_reset, full_reset)
|
|
99
|
+
workers = {}
|
|
100
|
+
variants.each do |variant|
|
|
101
|
+
directory = "#{output}/#{variant}"
|
|
102
|
+
FileUtils.mkdir_p(directory)
|
|
103
|
+
library = "#{root}/lib"
|
|
104
|
+
unless variant == "locals_only"
|
|
105
|
+
FileUtils.rm_rf("#{directory}/lib")
|
|
106
|
+
FileUtils.cp_r("lib", "#{directory}/lib")
|
|
107
|
+
library = "#{directory}/lib"
|
|
108
|
+
source = variant == "metadata_only" ? ir_source.dup : full_ir_source.dup
|
|
109
|
+
if variant == "registers_only"
|
|
110
|
+
# Reuse the inactive frame's Integer array, but allocate a new Frame on
|
|
111
|
+
# every acquisition. Both this and pooled still reset every register.
|
|
112
|
+
source = replace_once(source, "def initialize(function, return_register)\n", "def initialize(function, return_register, registers = nil)\n")
|
|
113
|
+
source = replace_once(source, "@registers = function.initial_registers.dup", "@registers = registers || function.initial_registers.dup")
|
|
114
|
+
# A new Frame backed by an old array still needs the baseline reset.
|
|
115
|
+
constructor_end = "@local_end = @local_start + function.local_count\n @return_register = return_register\n @pc = 0"
|
|
116
|
+
source = replace_once(source, constructor_end, "#{constructor_end}\n reset(return_register) if registers")
|
|
117
|
+
source = replace_once(source, "frame.reset(destination)\n return frame", "@created += 1\n return Frame.new(@function, destination, frame.registers)")
|
|
118
|
+
elsif variant == "metadata_only"
|
|
119
|
+
# A deliberately restricted upper bound: the three leaf callees in this
|
|
120
|
+
# fixture have no non-parameter locals and read no temporary before writing
|
|
121
|
+
# it. Initial constants survive reuse; arguments are overwritten by CALL.
|
|
122
|
+
source = replace_once(source, "frame.reset(destination)\n return frame", "frame.reset_metadata(destination)\n return frame")
|
|
123
|
+
marker = " # An invocation owns its pools. Active recursive frames never enter the"
|
|
124
|
+
method = <<~RUBY
|
|
125
|
+
# @rbs return_register: Integer
|
|
126
|
+
# @rbs return: void
|
|
127
|
+
def reset_metadata(return_register)
|
|
128
|
+
raise \"benchmark requires zero non-parameter locals\" unless @function.local_count == 0
|
|
129
|
+
@return_register = return_register
|
|
130
|
+
@pc = 0
|
|
131
|
+
end
|
|
132
|
+
RUBY
|
|
133
|
+
# Insert into Frame, immediately before its closing end.
|
|
134
|
+
before = " end\n\n#{marker}"
|
|
135
|
+
source = replace_once(source, before, "#{method.lines.map { |line| " #{line}" }.join} end\n\n#{marker}")
|
|
136
|
+
end
|
|
137
|
+
File.write("#{library}/wardite/efflorescence/ir.rb", source)
|
|
138
|
+
end
|
|
139
|
+
worker = "#{directory}/worker"
|
|
140
|
+
run!([*compiler, "-I#{library}", "--strict-require", *flags, "benchmark/call_cost_worker.rb", "-o", worker], "#{directory}/build.log")
|
|
141
|
+
workers[variant] = {binary: worker, library: library, disabled: variant == "fresh" ? "inline,frames" : "inline"}
|
|
142
|
+
puts "Built #{variant}"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Confirm the current engine, old Stack VM and independent arithmetic agree on
|
|
146
|
+
# the controls and i32 boundaries before measuring any variant. Each native
|
|
147
|
+
# variant is also checked at zero/one/many iterations with negative-bit seeds.
|
|
148
|
+
validation = [[0, 0], [0, 123], [1, 0xffffffff], [10, 123], [1000, 0xffffffff]]
|
|
149
|
+
validation_worker = "#{output}/validate.rb"
|
|
150
|
+
File.write(validation_worker, <<~RUBY)
|
|
151
|
+
require "wardite"
|
|
152
|
+
wasm, rounds, seed = ARGV
|
|
153
|
+
instance = File.open(wasm, "rb") { |file| Wardite::BinaryLoader.load_from_buffer(file, enable_wasi: false) }
|
|
154
|
+
#{exports.inspect}.each { |name| puts "\#{name},\#{instance.runtime.call(name, [rounds.to_i, seed.to_i]).value}" }
|
|
155
|
+
RUBY
|
|
156
|
+
validation.each do |count, initial|
|
|
157
|
+
stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-Ilib", validation_worker, wasm, count.to_s, initial.to_s)
|
|
158
|
+
abort stderr unless status.success?
|
|
159
|
+
stdout.each_line do |line|
|
|
160
|
+
name, value = line.strip.split(",")
|
|
161
|
+
raise "Old VM mismatch" unless Integer(value) == expected(name, count, initial)
|
|
162
|
+
end
|
|
163
|
+
workers.each_value do |worker|
|
|
164
|
+
exports.each do |name|
|
|
165
|
+
command = [worker[:binary], wasm, name, count.to_s, initial.to_s, worker[:disabled]]
|
|
166
|
+
measure(command, expected(name, count, initial))
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
checksums = exports.to_h { |name| [name, expected(name, rounds, seed)] }
|
|
171
|
+
puts "All variants, old VM and reference agree on boundary/control cases."
|
|
172
|
+
|
|
173
|
+
rows = exports.flat_map do |name|
|
|
174
|
+
workers.map do |variant, worker|
|
|
175
|
+
command = [worker[:binary], wasm, name, rounds.to_s, seed.to_s, worker[:disabled]]
|
|
176
|
+
counters = measure([*command, "metrics"], checksums.fetch(name))
|
|
177
|
+
{export: name, variant: variant, command: command, metrics: counters[:metrics], samples: []}
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Round-robin all cases, rotating and reversing order. Process start, load and
|
|
182
|
+
# translation are recorded separately; execution is the primary comparison.
|
|
183
|
+
(warmups + runs).times do |iteration|
|
|
184
|
+
ordered = rows.rotate(iteration * 5)
|
|
185
|
+
ordered = ordered.reverse if iteration.odd?
|
|
186
|
+
ordered.each do |row|
|
|
187
|
+
sample = measure(row[:command], checksums.fetch(row[:export]))
|
|
188
|
+
row[:samples] << sample.reject { |key, _| %i[metrics stderr].include?(key) } if iteration >= warmups
|
|
189
|
+
end
|
|
190
|
+
puts "Completed #{iteration < warmups ? "warmup" : "sample"} #{iteration < warmups ? iteration + 1 : iteration - warmups + 1}"
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
rows.each do |row|
|
|
194
|
+
row[:statistics_ms] = %i[e2e load translate execute].to_h do |phase|
|
|
195
|
+
[phase, stats(row[:samples].map { |sample| sample.fetch(phase) * 1000 })]
|
|
196
|
+
end
|
|
197
|
+
stat = row[:statistics_ms][:execute]
|
|
198
|
+
puts "%s/%s: %.3f ± %.3f ms (%s)" % [row[:export], row[:variant], stat[:mean], stat[:sd], row[:metrics]]
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Allocation reports are deliberately outside timing. Compare equal binaries
|
|
202
|
+
# at zero and nonzero rounds to remove most loader/translation allocations.
|
|
203
|
+
allocations = []
|
|
204
|
+
workers.each do |variant, worker|
|
|
205
|
+
%w[call_identity call_mix].each do |name|
|
|
206
|
+
[0, 10000].each do |count|
|
|
207
|
+
report = "#{output}/#{variant}/#{name}-#{count}.alloc.folded"
|
|
208
|
+
command = [worker[:binary], wasm, name, count.to_s, seed.to_s, worker[:disabled]]
|
|
209
|
+
measure(command, expected(name, count, seed), {"SPINEL_ALLOC_REPORT" => report})
|
|
210
|
+
counts = File.readlines(report).filter_map do |line|
|
|
211
|
+
match = line.match(/^alloc;(.+) (\d+)$/)
|
|
212
|
+
[match[1], Integer(match[2])] if match
|
|
213
|
+
end.to_h
|
|
214
|
+
allocations << {variant: variant, export: name, rounds: count,
|
|
215
|
+
frames: counts.fetch("Wardite::Efflorescence::Frame"),
|
|
216
|
+
no_scan: counts.fetch("(no-scan)"), total: counts.values.sum}
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
commit, status = Open3.capture2("git", "rev-parse", "HEAD")
|
|
222
|
+
abort "git revision unavailable" unless status.success?
|
|
223
|
+
report = {rounds: rounds, seed: seed, checksums: checksums, runs: runs, warmups: warmups,
|
|
224
|
+
ruby: RUBY_DESCRIPTION, spinel: version.strip, spinel_flags: flags,
|
|
225
|
+
source_files_sha256: Dir["lib/**/*.rb"].sort.to_h { |path| [path, Digest::SHA256.file(path).hexdigest] },
|
|
226
|
+
binary_sha256: workers.to_h { |name, worker| [name, Digest::SHA256.file(worker[:binary]).hexdigest] },
|
|
227
|
+
source_commit: commit.strip, allocations: allocations,
|
|
228
|
+
note: "full_reset restores the old reset loop in a copy; locals_only is production; metadata_only is fixture-specific",
|
|
229
|
+
results: rows}
|
|
230
|
+
File.write("#{output}/results.json", JSON.pretty_generate(report) + "\n")
|
|
231
|
+
puts "Saved #{output}/results.json"
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# All experiments live in copied sources/generated C under build/. Production
|
|
4
|
+
# Ruby, the installed Spinel compiler and its collector are never patched.
|
|
5
|
+
require "digest"
|
|
6
|
+
require "fileutils"
|
|
7
|
+
require "json"
|
|
8
|
+
require "open3"
|
|
9
|
+
require "rbconfig"
|
|
10
|
+
require "shellwords"
|
|
11
|
+
|
|
12
|
+
Dir.chdir(File.expand_path("..", __dir__))
|
|
13
|
+
$stdout.sync = true
|
|
14
|
+
output = File.expand_path(ENV.fetch("GUARDS_DIR", "build/benchmark/generated-guards"))
|
|
15
|
+
abort "GUARDS_DIR must be inside build/" unless output.start_with?(File.expand_path("build") + "/")
|
|
16
|
+
FileUtils.mkdir_p(output)
|
|
17
|
+
rounds = Integer(ARGV.fetch(0, "1000000"))
|
|
18
|
+
seed = Integer(ARGV.fetch(1, "123"))
|
|
19
|
+
runs = Integer(ENV.fetch("RUNS", "3"))
|
|
20
|
+
warmups = Integer(ENV.fetch("WARMUPS", "1"))
|
|
21
|
+
abort "invalid inputs" unless (0..0x7fffffff).cover?(rounds) && (0..0xffffffff).cover?(seed) && runs > 1 && warmups >= 0
|
|
22
|
+
variants = %w[baseline guarded_index while_loop while_guarded c_root_elision while_c_root_elision]
|
|
23
|
+
if ENV["VARIANTS"]
|
|
24
|
+
requested = ENV.fetch("VARIANTS").split(",").uniq
|
|
25
|
+
abort "invalid VARIANTS (include baseline)" unless requested.include?("baseline") && (requested - variants).empty?
|
|
26
|
+
variants = requested
|
|
27
|
+
end
|
|
28
|
+
compiler = Shellwords.split(ENV.fetch("SPINEL", "spinel"))
|
|
29
|
+
cc = Shellwords.split(ENV.fetch("CC", "clang"))
|
|
30
|
+
native_flags = ["-O2"]
|
|
31
|
+
native_flags += %w[-g -fno-omit-frame-pointer] if ENV["PROFILE"] == "1"
|
|
32
|
+
with_cli = ENV["CLI"] == "1"
|
|
33
|
+
version, status = Open3.capture2e(*compiler, "--version")
|
|
34
|
+
abort version unless status.success?
|
|
35
|
+
cc_version, status = Open3.capture2e(*cc, "--version")
|
|
36
|
+
abort cc_version unless status.success?
|
|
37
|
+
commit, status = Open3.capture2e("git", "rev-parse", "HEAD")
|
|
38
|
+
abort commit unless status.success?
|
|
39
|
+
|
|
40
|
+
def run!(command, log)
|
|
41
|
+
abort "Failed: #{command.inspect}; see #{log}" unless system(*command, out: log, err: [:child, :out])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def vm_body(source)
|
|
45
|
+
source[/^static [^\n]+sp_(?:Wardite__Efflorescence__)?VM_execute_flat\([^;\n]*\) \{.*?(?=^static |\z)/m] || raise("missing execute_flat C body")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def c_metrics(source)
|
|
49
|
+
body = vm_body(source)
|
|
50
|
+
loop_prefix = body[/(?:for \(;;\)|while \(1\)) \{\n(.*?)^\s*lv_opcode\w* = \(\{/m, 1] || ""
|
|
51
|
+
{bytes: body.bytesize, temporary_roots: body.scan(/SP_GC_ROOT\(/).size,
|
|
52
|
+
instruction_roots: body.scan(/sp_IntArray \*(_t\d+) = lv_instructions; SP_GC_ROOT\(\1\);/).size,
|
|
53
|
+
rooted_instructions: !!body.match(/SP_GC_ENTRY_PTR\(lv_instructions\)/),
|
|
54
|
+
root_frames: body.scan(/SP_GC_ROOT_FRAME\(/).size, setjmps: body.scan(/\bsetjmp\(/).size,
|
|
55
|
+
volatile_lines: body.lines.count { |line| line.match?(/\bvolatile\b/) },
|
|
56
|
+
boxed_local_names: body.scan(/^\s*(?:volatile\s+)?sp_RbVal(?:\s+volatile)?\s+(lv_\w+)\s*=/).flatten,
|
|
57
|
+
iteration_resets: loop_prefix.lines.count { |line| line.match?(/^\s*lv_\w+ = (?:0|NULL|sp_box_nil\(\)|sp_oint_nil\(\));\s*$/) },
|
|
58
|
+
generic_array_ops: body.scan(/sp_PolyArray_(?:get|set|push|pop|new)/).size,
|
|
59
|
+
polymorphic_calls: body.scan(/sp_pd_[a-f0-9]+\(/).size}
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def reference(rounds, seed)
|
|
63
|
+
x = seed
|
|
64
|
+
rounds.times do
|
|
65
|
+
x ^= x / 8192
|
|
66
|
+
x = (x * 1664525 + 1013904223) % (2**32)
|
|
67
|
+
x = (x ^ (x * 128)) % (2**32)
|
|
68
|
+
end
|
|
69
|
+
x
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def sample(command, expected)
|
|
73
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
74
|
+
out, err, status = Open3.capture3(*command)
|
|
75
|
+
e2e = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
76
|
+
fields = out.lines.first.to_s.strip.split(",")
|
|
77
|
+
abort "Checksum mismatch: #{command.inspect}\n#{out}#{err}" unless status.success? && fields.size == 4 && Integer(fields[3]) == expected
|
|
78
|
+
{worker_e2e: e2e, load: Float(fields[0]), translate: Float(fields[1]), execute: Float(fields[2]), metrics: out.lines[1]&.strip}
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def cli_sample(command, expected)
|
|
82
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
83
|
+
out, err, status = Open3.capture3(*command)
|
|
84
|
+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
85
|
+
signed = expected >= 0x80000000 ? expected - 0x100000000 : expected
|
|
86
|
+
abort "CLI checksum mismatch: #{command.inspect}\n#{out}#{err}" unless status.success? && out.empty? && err == "return value: I32(#{signed})\n"
|
|
87
|
+
elapsed
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def statistics(values)
|
|
91
|
+
mean = values.sum / values.size
|
|
92
|
+
{mean: mean, sd: Math.sqrt(values.sum { |x| (x - mean)**2 } / (values.size - 1)), min: values.min, max: values.max}
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
wasm = "#{output}/i32_mix.wasm"
|
|
96
|
+
run!(["wasm-tools", "parse", "benchmark/i32_mix.wat", "-o", wasm], "#{output}/wat.log")
|
|
97
|
+
original = File.read("lib/wardite/efflorescence/vm.rb")
|
|
98
|
+
snapshots = {}
|
|
99
|
+
variants.each do |variant|
|
|
100
|
+
directory = "#{output}/#{variant}"
|
|
101
|
+
FileUtils.mkdir_p(directory)
|
|
102
|
+
%w[lib exe benchmark].each { |name| FileUtils.rm_rf("#{directory}/#{name}") }
|
|
103
|
+
FileUtils.cp_r(%w[lib exe], directory)
|
|
104
|
+
FileUtils.mkdir_p("#{directory}/benchmark")
|
|
105
|
+
FileUtils.cp("benchmark/call_cost_worker.rb", "#{directory}/benchmark")
|
|
106
|
+
prefix, flat = original.split(" def execute_flat(frame, pools)\n", 2)
|
|
107
|
+
raise "VM shape changed" unless flat && flat.scan(/^ (?:loop do|while true)\n/).size == 1
|
|
108
|
+
# Reconstruct both spellings even after production adopts the while loop.
|
|
109
|
+
use_while = %w[while_loop while_guarded while_c_root_elision].include?(variant)
|
|
110
|
+
loop_header = use_while ? "while true" : "loop do"
|
|
111
|
+
flat = flat.sub(/^ (?:loop do|while true)\n/, " #{loop_header}\n")
|
|
112
|
+
unless use_while
|
|
113
|
+
flat = flat.sub(" # Steep treats while as possibly nil; execution always returns or raises.\n" \
|
|
114
|
+
" raise Wardite::EvalError, \"Efflorescence: MIR execution ended without RETURN\"\n", "")
|
|
115
|
+
end
|
|
116
|
+
if %w[guarded_index while_guarded].include?(variant)
|
|
117
|
+
count = 0
|
|
118
|
+
flat = flat.gsub(/instructions\.fetch\((pc(?: \+ \d+)?)\)/) do
|
|
119
|
+
count += 1
|
|
120
|
+
"(instructions[#{$1}] || raise(::IndexError, \"Efflorescence: invalid MIR word\"))"
|
|
121
|
+
end
|
|
122
|
+
raise "missing instruction fetches" unless count > 20
|
|
123
|
+
end
|
|
124
|
+
File.write("#{directory}/lib/wardite/efflorescence/vm.rb", "#{prefix} def execute_flat(frame, pools)\n#{flat}")
|
|
125
|
+
artifacts = {}
|
|
126
|
+
entries = {"worker" => "benchmark/call_cost_worker.rb"}
|
|
127
|
+
entries["wardite"] = "exe/wardite" if with_cli
|
|
128
|
+
entries.each do |binary, entry|
|
|
129
|
+
generated = "#{directory}/#{binary}.c"
|
|
130
|
+
command = [*compiler, "-I#{directory}/lib", "--strict-require", "-O2", "--no-line-map", "-c",
|
|
131
|
+
"#{directory}/#{entry}", "-o", generated, "--print-build"]
|
|
132
|
+
out, err, status = Open3.capture3(*command)
|
|
133
|
+
File.write("#{directory}/#{binary}-spinel.log", err)
|
|
134
|
+
abort "Spinel failed: #{variant}/#{binary}" unless status.success?
|
|
135
|
+
File.write("#{directory}/#{binary}-ingredients.txt", out)
|
|
136
|
+
ingredients = out.lines.map { |line| line.strip.split(" ", 2) }
|
|
137
|
+
known = %w[cflag include define source link runtime lib]
|
|
138
|
+
raise "unexpected build ingredients" unless ingredients.all? { |kind, value| known.include?(kind) && value } && ingredients.include?(["source", generated])
|
|
139
|
+
c_source = File.read(generated)
|
|
140
|
+
before = c_metrics(c_source)
|
|
141
|
+
if %w[c_root_elision while_c_root_elision].include?(variant)
|
|
142
|
+
body = vm_body(c_source)
|
|
143
|
+
raise "instruction variable is not a function-scoped root" unless before[:rooted_instructions]
|
|
144
|
+
# The index must be the local PC or its checked integer addition. An
|
|
145
|
+
# arbitrary Ruby argument can allocate or overwrite the receiver's slot.
|
|
146
|
+
fetch_root = /(sp_IntArray \*(_t\d+) = lv_instructions; )SP_GC_ROOT\(\2\);( sp_int _t\d+ = (?:lv_pc|sp_int_add\(lv_pc, [123]LL\));)/
|
|
147
|
+
raise "unrecognized fetch argument" unless body.scan(fetch_root).size == before[:instruction_roots]
|
|
148
|
+
changed = body.gsub(fetch_root, '\1\3')
|
|
149
|
+
raise "no redundant fetch roots" unless before[:instruction_roots] > 20 && changed != body
|
|
150
|
+
c_source = c_source.sub(body, changed)
|
|
151
|
+
File.write(generated, c_source)
|
|
152
|
+
end
|
|
153
|
+
flags = ingredients.filter_map { |kind, value| kind == "include" ? "-I#{value}" : (%w[cflag define].include?(kind) ? value : nil) }
|
|
154
|
+
links = ingredients.filter_map { |kind, value| value if %w[link runtime lib].include?(kind) }
|
|
155
|
+
object = "#{directory}/#{binary}.o"
|
|
156
|
+
build = [*cc, *native_flags, *flags, "-c", generated, "-o", object]
|
|
157
|
+
link = [*cc, *(ENV["PROFILE"] == "1" ? ["-g"] : []), object, *links, "-o", "#{directory}/#{binary}"]
|
|
158
|
+
File.write("#{directory}/#{binary}-compile.json", JSON.pretty_generate({compile: build, link: link}) + "\n")
|
|
159
|
+
run!(build, "#{directory}/#{binary}-clang.log")
|
|
160
|
+
run!(link, "#{directory}/#{binary}-link.log")
|
|
161
|
+
if ENV["PROFILE"] == "1" && RUBY_PLATFORM.include?("darwin")
|
|
162
|
+
FileUtils.rm_rf("#{directory}/#{binary}.dSYM")
|
|
163
|
+
run!(["dsymutil", "#{directory}/#{binary}"], "#{directory}/#{binary}-dsym.log")
|
|
164
|
+
end
|
|
165
|
+
artifacts[binary] = {before: before, after: c_metrics(c_source), c_sha256: Digest::SHA256.file(generated).hexdigest,
|
|
166
|
+
binary_sha256: Digest::SHA256.file("#{directory}/#{binary}").hexdigest, compile_command: build, link_command: link}
|
|
167
|
+
end
|
|
168
|
+
sources = Dir["#{directory}/{lib,exe,benchmark}/**/*"].select { |path| File.file?(path) }.sort.to_h do |path|
|
|
169
|
+
[path.delete_prefix("#{directory}/"), Digest::SHA256.file(path).hexdigest]
|
|
170
|
+
end
|
|
171
|
+
snapshots[variant] = {directory: directory, source_sha256: sources, vm_source: File.read("#{directory}/lib/wardite/efflorescence/vm.rb"), artifacts: artifacts}
|
|
172
|
+
puts "Built #{variant}: #{artifacts.fetch('worker').fetch(:after)}"
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
cases = [["inline_mix", ""], ["call_mix", ""], ["call_mix", "inline"]]
|
|
176
|
+
checksum = reference(rounds, seed)
|
|
177
|
+
rows = cases.flat_map do |name, disabled|
|
|
178
|
+
variants.map do |variant|
|
|
179
|
+
directory = snapshots.fetch(variant).fetch(:directory)
|
|
180
|
+
worker = ["#{directory}/worker", wasm, name, rounds.to_s, seed.to_s, disabled]
|
|
181
|
+
cli = with_cli ? ["#{directory}/wardite", "--new-engine", "--no-wasi"] : nil
|
|
182
|
+
cli += ["--mir-disable", "inline"] if cli && !disabled.empty?
|
|
183
|
+
cli += ["--invoke", name, "--", wasm, rounds.to_s, seed.to_s] if cli
|
|
184
|
+
metrics = sample([*worker, "metrics"], checksum).fetch(:metrics)
|
|
185
|
+
[[0, 0], [0, 123], [1, 0], [1, 123], [10, 123], [1000, 123], [1000, 0xffffffff], [100000, 123]].each do |n, initial|
|
|
186
|
+
sample(["#{directory}/worker", wasm, name, n.to_s, initial.to_s, disabled], reference(n, initial))
|
|
187
|
+
end
|
|
188
|
+
{export: name, disabled: disabled, variant: variant, worker: worker, cli: cli, metrics: metrics, samples: []}
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
cases.each do |name, disabled|
|
|
192
|
+
counters = rows.select { |row| row[:export] == name && row[:disabled] == disabled }.map { |row| row[:metrics] }.uniq
|
|
193
|
+
abort "MIR metrics differ" unless counters.size == 1
|
|
194
|
+
end
|
|
195
|
+
puts "All complete acceptance inputs, measured checksums and MIR counters match."
|
|
196
|
+
(warmups + runs).times do |iteration|
|
|
197
|
+
order = rows.rotate(iteration * 3)
|
|
198
|
+
order.reverse! if iteration.odd?
|
|
199
|
+
order.each do |row|
|
|
200
|
+
measured = sample(row[:worker], checksum).reject { |key, _| key == :metrics }
|
|
201
|
+
measured[:cli_e2e] = cli_sample(row[:cli], checksum) if row[:cli]
|
|
202
|
+
row[:samples] << measured if iteration >= warmups
|
|
203
|
+
end
|
|
204
|
+
puts "Completed #{iteration < warmups ? 'warmup' : 'sample'} #{iteration < warmups ? iteration + 1 : iteration - warmups + 1}"
|
|
205
|
+
end
|
|
206
|
+
rows.each do |row|
|
|
207
|
+
row[:statistics_ms] = row[:samples].first.keys.to_h { |phase| [phase, statistics(row[:samples].map { |s| s.fetch(phase) * 1000 })] }
|
|
208
|
+
stats = row[:statistics_ms][:execute]
|
|
209
|
+
puts "%s/%s/%s: %.3f ± %.3f ms" % [row[:variant], row[:export], row[:disabled].empty? ? 'inline-on' : 'inline-off', stats[:mean], stats[:sd]]
|
|
210
|
+
row.delete(:worker)
|
|
211
|
+
row.delete(:cli)
|
|
212
|
+
end
|
|
213
|
+
snapshots.each_value { |snapshot| snapshot.delete(:directory) }
|
|
214
|
+
File.write("#{output}/results.json", JSON.pretty_generate({commit: commit.strip, spinel: version.strip,
|
|
215
|
+
cc: cc_version.lines.first.strip, native_flags: native_flags, rounds: rounds, seed: seed, checksum: checksum,
|
|
216
|
+
fixture: {wat_sha256: Digest::SHA256.file("benchmark/i32_mix.wat").hexdigest, wasm_sha256: Digest::SHA256.file(wasm).hexdigest},
|
|
217
|
+
runs: runs, warmups: warmups, cli: with_cli, snapshots: snapshots, results: rows}) + "\n")
|
|
218
|
+
puts "Saved #{output}/results.json"
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "wardite"
|
|
5
|
+
|
|
6
|
+
class IncrementHost
|
|
7
|
+
include Wardite::WasmModule
|
|
8
|
+
|
|
9
|
+
def increment(_store, arguments)
|
|
10
|
+
value = arguments[0]
|
|
11
|
+
raise "expected boxed i32" unless value.is_a?(Wardite::I32)
|
|
12
|
+
value.value + 1
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Keep the native receiver concrete at the public API boundary.
|
|
17
|
+
class M3CallProbe
|
|
18
|
+
# @rbs vm: Wardite::Efflorescence::VM
|
|
19
|
+
# @rbs export: String
|
|
20
|
+
# @rbs rounds: Integer
|
|
21
|
+
# @rbs return: Integer?
|
|
22
|
+
def self.run(vm, export, rounds)
|
|
23
|
+
vm.call_unboxed(export, [rounds, 123])
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
28
|
+
instance = File.open(ARGV.fetch(0), "rb") do |file|
|
|
29
|
+
Wardite::BinaryLoader.load_from_buffer(file, enable_wasi: false, engine: :efflorescence,
|
|
30
|
+
disabled_optimizations: [:inline], import_object: {env: IncrementHost.new})
|
|
31
|
+
end
|
|
32
|
+
loaded = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
33
|
+
vm = instance.runtime
|
|
34
|
+
raise "wrong engine" unless vm.is_a?(Wardite::Efflorescence::VM)
|
|
35
|
+
checksum = M3CallProbe.run(vm, ARGV.fetch(1), ARGV.fetch(2).to_i)
|
|
36
|
+
finished = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
37
|
+
raise "missing checksum" unless checksum
|
|
38
|
+
puts "#{loaded - started},#{finished - loaded},#{checksum}"
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Compare identical call_cost_worker executables built against two revisions.
|
|
4
|
+
# Usage: bundle exec ruby benchmark/measure_m1.rb BEFORE_WORKER AFTER_WORKER
|
|
5
|
+
# Workers: spinel -I lib --strict-require -O2 benchmark/call_cost_worker.rb -o PATH
|
|
6
|
+
require 'open3'
|
|
7
|
+
require 'json'
|
|
8
|
+
require 'tmpdir'
|
|
9
|
+
|
|
10
|
+
workers = ARGV
|
|
11
|
+
abort 'Pass before and after worker executable paths' unless workers.size == 2
|
|
12
|
+
runs = Integer(ENV.fetch('RUNS', '5'))
|
|
13
|
+
warmups = Integer(ENV.fetch('WARMUPS', '2'))
|
|
14
|
+
rounds = Integer(ENV.fetch('ROUNDS', '100000'))
|
|
15
|
+
mix = 123
|
|
16
|
+
rounds.times do
|
|
17
|
+
mix ^= mix >> 13
|
|
18
|
+
mix = (mix * 1664525 + 1013904223) & 0xffffffff
|
|
19
|
+
mix = (mix ^ (mix << 7)) & 0xffffffff
|
|
20
|
+
end
|
|
21
|
+
expected = {'inline_mix' => mix, 'call_mix' => mix, 'empty_loop' => 123,
|
|
22
|
+
'call_zero' => (123 + rounds * 7) & 0xffffffff, 'call_one' => 123,
|
|
23
|
+
'call_two' => (123 + rounds) & 0xffffffff,
|
|
24
|
+
'call_three' => (123 + rounds * 3) & 0xffffffff,
|
|
25
|
+
'call_four' => (123 + rounds * 6) & 0xffffffff}
|
|
26
|
+
rows = []
|
|
27
|
+
Dir.mktmpdir('wardite-m1-bench') do |dir|
|
|
28
|
+
{'i32_mix' => %w[inline_mix call_mix], 'i32_call_arities' => %w[empty_loop call_zero call_one call_two call_three call_four]}.each do |file, names|
|
|
29
|
+
wasm = File.join(dir, "#{file}.wasm")
|
|
30
|
+
abort 'parse failed' unless system('wasm-tools', 'parse', "benchmark/#{file}.wat", '-o', wasm)
|
|
31
|
+
names.each do |name|
|
|
32
|
+
(warmups + runs).times do |run|
|
|
33
|
+
# Alternate order to reduce systematic drift across the comparison.
|
|
34
|
+
order = run.even? ? [0, 1] : [1, 0]
|
|
35
|
+
values = []
|
|
36
|
+
order.each do |i|
|
|
37
|
+
output, error, status = Open3.capture3(workers[i], wasm, name, rounds.to_s, '123', 'inline')
|
|
38
|
+
abort error unless status.success?
|
|
39
|
+
loading, translation, execution, checksum = output.strip.split(',')
|
|
40
|
+
values[i] = Integer(checksum)
|
|
41
|
+
if run >= warmups
|
|
42
|
+
rows << {revision: i == 0 ? 'before' : 'after', export: name,
|
|
43
|
+
load: Float(loading) + Float(translation), execution: Float(execution), checksum: Integer(checksum)}
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
abort "checksum mismatch for #{name}" unless values[0] == values[1] && values[0] == expected.fetch(name)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
puts JSON.pretty_generate({workers: workers, runs: runs, warmups: warmups, rounds: rounds, seed: 123, disabled: ['inline'], samples: rows})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Usage: RUNS=7 WARMUPS=2 ROUNDS=1000 bundle exec ruby benchmark/measure_m2.rb
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'fileutils'
|
|
6
|
+
require 'open3'
|
|
7
|
+
require 'rbconfig'
|
|
8
|
+
require 'shellwords'
|
|
9
|
+
|
|
10
|
+
Dir.chdir(File.expand_path('..', __dir__))
|
|
11
|
+
runs = Integer(ENV.fetch('RUNS','7'))
|
|
12
|
+
warmups = Integer(ENV.fetch('WARMUPS','2'))
|
|
13
|
+
rounds = Integer(ENV.fetch('ROUNDS','1000'))
|
|
14
|
+
seed = Integer(ENV.fetch('SEED','123'))
|
|
15
|
+
abort 'invalid sample counts or i32 arguments' unless runs > 0 && warmups >= 0 && (0..0x7fffffff).cover?(rounds) && (0..0xffffffff).cover?(seed)
|
|
16
|
+
FileUtils.mkdir_p('build/benchmark')
|
|
17
|
+
binary = 'build/benchmark/i32_memory.wasm'
|
|
18
|
+
worker = ENV.fetch('M2_SPINEL_WORKER','build/benchmark/m2-worker')
|
|
19
|
+
compiler = Shellwords.split(ENV.fetch('SPINEL','spinel'))
|
|
20
|
+
flags = Shellwords.split(ENV.fetch('SPINELFLAGS','-O2'))
|
|
21
|
+
parse, status = Open3.capture2e('wasm-tools','parse','benchmark/i32_memory.wat','-o',binary)
|
|
22
|
+
abort parse unless status.success?
|
|
23
|
+
valid, status = Open3.capture2e('wasm-tools','validate',binary)
|
|
24
|
+
abort valid unless status.success?
|
|
25
|
+
unless ENV['M2_SPINEL_WORKER']
|
|
26
|
+
log, status = Open3.capture2e(*compiler,'-I','lib','--strict-require',*flags,'benchmark/call_cost_worker.rb','-o',worker)
|
|
27
|
+
File.write('build/benchmark/m2-worker-build.log',log)
|
|
28
|
+
abort log unless status.success?
|
|
29
|
+
end
|
|
30
|
+
expected = {
|
|
31
|
+
'array_scan' => (rounds * (256 * seed + 32640)) & 0xffffffff,
|
|
32
|
+
'copy_fill' => rounds.times.sum { |i| (seed + i) & 255 } & 0xffffffff,
|
|
33
|
+
'global_update' => (seed + rounds * 1664525) & 0xffffffff,
|
|
34
|
+
'segment_init' => (seed + rounds.times.sum { |i| '0123456789abcdef'.getbyte(i & 15) }) & 0xffffffff
|
|
35
|
+
}
|
|
36
|
+
runners = {'CRuby' => [RbConfig.ruby,'--disable-yjit','-Ilib','benchmark/call_cost_worker.rb'], 'Spinel' => [worker]}
|
|
37
|
+
rows = []
|
|
38
|
+
expected.each do |name, checksum|
|
|
39
|
+
(runs + warmups).times do |run|
|
|
40
|
+
order = run.even? ? runners.to_a : runners.to_a.reverse
|
|
41
|
+
order.each do |runtime, command|
|
|
42
|
+
output, error, status = Open3.capture3(*command,binary,name,rounds.to_s,seed.to_s,'inline')
|
|
43
|
+
abort "#{runtime}/#{name}: #{error}" unless status.success?
|
|
44
|
+
loading, translation, execution, actual = output.strip.split(',')
|
|
45
|
+
abort "checksum mismatch: #{runtime}/#{name}: #{actual} != #{checksum}" unless Integer(actual) == checksum
|
|
46
|
+
if run >= warmups
|
|
47
|
+
rows << {runtime: runtime, export: name, load: Float(loading) + Float(translation), execution: Float(execution), checksum: Integer(actual)}
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
spinel_version, status = Open3.capture2e(*compiler,'--version')
|
|
53
|
+
abort spinel_version unless status.success?
|
|
54
|
+
puts JSON.pretty_generate({ruby: RUBY_DESCRIPTION, spinel: spinel_version.strip, compiler_flags: flags,
|
|
55
|
+
runs: runs, warmups: warmups, rounds: rounds, seed: seed, disabled: ['inline'], samples: rows})
|