wardite 0.9.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +191 -0
- data/Makefile +14 -0
- data/README.md +107 -2
- data/Rakefile +5 -0
- data/Steepfile +1 -0
- data/assets/wardite.svg +12 -0
- data/benchmark/README.md +500 -0
- data/benchmark/build_call_profile.rb +42 -0
- data/benchmark/call_cost_worker.rb +27 -0
- data/benchmark/compare.rb +93 -0
- data/benchmark/compare_call_types.rb +212 -0
- data/benchmark/compare_dispatch.rb +123 -0
- data/benchmark/compare_engines.rb +119 -0
- data/benchmark/compare_optimizations.rb +139 -0
- data/benchmark/engine_phases.rb +30 -0
- data/benchmark/figures/README.md +35 -0
- data/benchmark/figures/plot_spinel_comparison.py +137 -0
- data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
- data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
- data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
- data/benchmark/figures/spinel_new_vm_summary.json +82 -0
- data/benchmark/float_m5.wat +42 -0
- data/benchmark/i32_call_arities.wat +89 -0
- data/benchmark/i32_calls.wat +174 -0
- data/benchmark/i32_memory.wat +58 -0
- data/benchmark/i32_mix.wat +80 -0
- data/benchmark/i64_m4.wat +55 -0
- data/benchmark/investigate_calls.rb +231 -0
- data/benchmark/investigate_generated_guards.rb +218 -0
- data/benchmark/m3_call_worker.rb +38 -0
- data/benchmark/measure_m1.rb +51 -0
- data/benchmark/measure_m2.rb +55 -0
- data/benchmark/measure_m3_calls.rb +89 -0
- data/benchmark/measure_m4.rb +134 -0
- data/benchmark/measure_m5_boundary.rb +41 -0
- data/benchmark/measure_mir_cache_verify.rb +58 -0
- data/benchmark/measure_ruby_wasm_cache_hit.rb +59 -0
- data/benchmark/measure_ruby_wasm_startup.rb +43 -0
- data/benchmark/measure_wasi_write.rb +75 -0
- data/benchmark/optimization_worker.rb +23 -0
- data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
- data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
- data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
- data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
- data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
- data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
- data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
- data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
- data/benchmark/results/efflorescence-m1-cruby.json +799 -0
- data/benchmark/results/efflorescence-m1-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
- data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
- data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
- data/benchmark/results/efflorescence-m2-memory.json +408 -0
- data/benchmark/results/efflorescence-mir2-cache-integrity.json +390 -0
- data/benchmark/results/efflorescence-unboxed-globals.json +3451 -0
- data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
- data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
- data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
- data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
- data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
- data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
- data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
- data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
- data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
- data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
- data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
- data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
- data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
- data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
- data/benchmark/summarize_call_profile.py +54 -0
- data/benchmark/validate_m1_eh.rb +34 -0
- data/benchmark/validate_m2.rb +119 -0
- data/benchmark/validate_m3.rb +44 -0
- data/benchmark/validate_m4.rb +95 -0
- data/benchmark/validate_m5.rb +108 -0
- data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
- data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
- data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
- data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
- data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
- data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
- data/docs/adr/0007-efflorescence-ruby-wasm-phase1.md +75 -0
- data/docs/adr/0008-efflorescence-ruby-wasm-phase2.md +75 -0
- data/docs/adr/0009-efflorescence-mir2-cache-integrity-modes.md +77 -0
- data/docs/adr/0010-efflorescence-mir2-cache-function-restore.md +80 -0
- data/docs/adr/0011-efflorescence-unboxed-globals.md +89 -0
- data/docs/adr/0012-efflorescence-startup-snapshot.md +173 -0
- data/docs/adr/0013-efflorescence-frame-acquisition.md +114 -0
- data/docs/efflorescence-call-arities.md +115 -0
- data/docs/efflorescence-call-investigation.md +155 -0
- data/docs/efflorescence-call-transfer.md +128 -0
- data/docs/efflorescence-call-types.md +102 -0
- data/docs/efflorescence-coverage-ja.md +351 -0
- data/docs/efflorescence-generated-c-guards.md +410 -0
- data/docs/efflorescence-inlining.md +165 -0
- data/docs/efflorescence-ja.md +155 -0
- data/docs/efflorescence-m1.md +160 -0
- data/docs/efflorescence-m2.md +233 -0
- data/docs/efflorescence-m3.md +131 -0
- data/docs/efflorescence-m4.md +88 -0
- data/docs/efflorescence-m5.md +82 -0
- data/docs/efflorescence-m6.md +95 -0
- data/docs/efflorescence-mir2-cache.md +123 -0
- data/docs/efflorescence-operand-decoding.md +105 -0
- data/docs/efflorescence-phase2-ja.md +225 -0
- data/docs/efflorescence-phase2.md +240 -0
- data/docs/efflorescence-register-initialization.md +75 -0
- data/docs/efflorescence-ruby-wasm-phase1.md +90 -0
- data/docs/efflorescence-spinel-linear-memory.md +72 -0
- data/docs/efflorescence-startup-snapshot.md +60 -0
- data/docs/efflorescence-unboxed-globals.md +61 -0
- data/docs/efflorescence.md +144 -0
- data/docs/spinel-string-aset-inplace-proposal.md +200 -0
- data/lib/wardite/alu_f32.generated.rb +11 -2
- data/lib/wardite/alu_f64.generated.rb +11 -2
- data/lib/wardite/alu_i32.generated.rb +54 -17
- data/lib/wardite/alu_i64.generated.rb +73 -31
- data/lib/wardite/cli/cli.rb +112 -29
- data/lib/wardite/const.rb +1 -0
- data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
- data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
- data/lib/wardite/efflorescence/inspection.rb +165 -0
- data/lib/wardite/efflorescence/ir.rb +1164 -0
- data/lib/wardite/efflorescence/mir_cache.rb +657 -0
- data/lib/wardite/efflorescence/optimizer.rb +580 -0
- data/lib/wardite/efflorescence/register_validation.rb +362 -0
- data/lib/wardite/efflorescence/snapshot.rb +229 -0
- data/lib/wardite/efflorescence/store_access.rb +366 -0
- data/lib/wardite/efflorescence/translator.rb +866 -0
- data/lib/wardite/efflorescence/vm.rb +1170 -0
- data/lib/wardite/efflorescence/wasi.rb +52 -0
- data/lib/wardite/efflorescence.rb +17 -0
- data/lib/wardite/errors.rb +20 -0
- data/lib/wardite/float_bits.rb +46 -0
- data/lib/wardite/instruction.rb +35 -4
- data/lib/wardite/leb128.rb +33 -12
- data/lib/wardite/load.rb +292 -143
- data/lib/wardite/value.rb +169 -175
- data/lib/wardite/version.rb +1 -1
- data/lib/wardite/wasi/consts.rb +1 -0
- data/lib/wardite/wasi/spinel.rb +41 -0
- data/lib/wardite/wasi.rb +483 -268
- data/lib/wardite/wasm_module.rb +6 -3
- data/lib/wardite.rb +360 -98
- data/rbs_collection.lock.yaml +36 -96
- data/scripts/gen_alu.rb +121 -21
- data/scripts/verify_ruby_wasm.rb +70 -0
- data/sig/generated/wardite/cli/cli.rbs +23 -6
- data/sig/generated/wardite/const.rbs +2 -0
- data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
- data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
- data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
- data/sig/generated/wardite/efflorescence/ir.rbs +888 -0
- data/sig/generated/wardite/efflorescence/mir_cache.rbs +236 -0
- data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
- data/sig/generated/wardite/efflorescence/register_validation.rbs +104 -0
- data/sig/generated/wardite/efflorescence/snapshot.rbs +83 -0
- data/sig/generated/wardite/efflorescence/store_access.rbs +145 -0
- data/sig/generated/wardite/efflorescence/translator.rbs +218 -0
- data/sig/generated/wardite/efflorescence/vm.rbs +240 -0
- data/sig/generated/wardite/efflorescence/wasi.rbs +34 -0
- data/sig/generated/wardite/errors.rbs +12 -0
- data/sig/generated/wardite/float_bits.rbs +30 -0
- data/sig/generated/wardite/instruction.rbs +22 -1
- data/sig/generated/wardite/leb128.rbs +3 -1
- data/sig/generated/wardite/load.rbs +59 -7
- data/sig/generated/wardite/value.rbs +70 -11
- data/sig/generated/wardite/wasi/consts.rbs +2 -0
- data/sig/generated/wardite/wasi/spinel.rbs +15 -0
- data/sig/generated/wardite/wasi.rbs +111 -4
- data/sig/generated/wardite.rbs +114 -13
- data/sig/mir_cache_stdlib.rbs +5 -0
- data/sig/vernier.rbs +2 -2
- data/spinel/README.md +30 -0
- data/spinel/bin/wardite.rb +6 -0
- data/spinel/patches/0001-io-buffer-string-view.patch +699 -0
- data/spinel/patches/0002-io-buffer-allocation-error.patch +47 -0
- data/spinel/spin.toml +7 -0
- metadata +154 -3
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
module Wardite
|
|
5
|
+
module Efflorescence
|
|
6
|
+
class Configuration
|
|
7
|
+
FEATURES = %i[copy immediate branch superinstruction inline frames].freeze
|
|
8
|
+
attr_reader :disabled #: Array[Symbol]
|
|
9
|
+
|
|
10
|
+
# @rbs disabled: Array[Symbol]
|
|
11
|
+
def initialize(disabled)
|
|
12
|
+
unknown = disabled - FEATURES - [:all]
|
|
13
|
+
raise Wardite::ArgumentError, "unknown MIR optimization: #{unknown.join(', ')}" unless unknown.empty?
|
|
14
|
+
@disabled = disabled.include?(:all) ? FEATURES.dup : disabled.dup
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @rbs feature: Symbol
|
|
18
|
+
# @rbs return: bool
|
|
19
|
+
def enabled?(feature)
|
|
20
|
+
!@disabled.include?(feature)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# All transformations use instruction ordinals. Only the final lowering
|
|
25
|
+
# changes PCs to integer-array offsets. Every rewrite repairs branch edges.
|
|
26
|
+
class Rewrite
|
|
27
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
28
|
+
def initialize(function)
|
|
29
|
+
@function = function
|
|
30
|
+
@mapping = Array.new(function.instructions.size + 1, 0)
|
|
31
|
+
@output = [] #: Array[Wardite::Efflorescence::Instruction]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @rbs pc: Integer
|
|
35
|
+
# @rbs return: void
|
|
36
|
+
def mark(pc)
|
|
37
|
+
@mapping[pc] = @output.size
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @rbs instruction: Wardite::Efflorescence::Instruction
|
|
41
|
+
# @rbs return: void
|
|
42
|
+
def emit(instruction)
|
|
43
|
+
@output << instruction
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @rbs return: void
|
|
47
|
+
def finish
|
|
48
|
+
@mapping[@function.instructions.size] = @output.size
|
|
49
|
+
@function.replace_instructions(@output)
|
|
50
|
+
@function.remap_targets!(@mapping)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# A shared CFG includes exceptional edges with their edge-local writes.
|
|
55
|
+
# Handler shapes are checked independently of definite assignment.
|
|
56
|
+
class FlowGraph
|
|
57
|
+
attr_reader :normal #: Array[Array[Integer]]
|
|
58
|
+
attr_reader :exceptional #: Array[Array[Wardite::Efflorescence::CatchTarget]]
|
|
59
|
+
|
|
60
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
61
|
+
def initialize(function)
|
|
62
|
+
count = function.instructions.size
|
|
63
|
+
@normal = Array.new(count) { [] }
|
|
64
|
+
@exceptional = Array.new(count) { [] }
|
|
65
|
+
@states = Array.new(count) { [] } #: Array[Array[Integer]]
|
|
66
|
+
@reached = Array.new(count, false)
|
|
67
|
+
@pending = [] #: Array[Integer]
|
|
68
|
+
function.instructions.each_with_index do |instruction, pc|
|
|
69
|
+
position = Opcode.target_position(instruction)
|
|
70
|
+
if position >= 0
|
|
71
|
+
target = instruction[position]
|
|
72
|
+
raise TranslationError, "invalid MIR branch target #{target}" unless target >= 0 && target < count
|
|
73
|
+
@normal.fetch(pc) << target
|
|
74
|
+
end
|
|
75
|
+
@normal.fetch(pc) << pc + 1 unless Opcode.terminal?(instruction[0]) || pc + 1 == count
|
|
76
|
+
end
|
|
77
|
+
function.handlers.each do |handler|
|
|
78
|
+
handler.clauses.each do |clause|
|
|
79
|
+
raise TranslationError, "invalid catch target PC" unless clause.target_pc >= 0 && clause.target_pc < count
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
merge(0, []) unless count == 0
|
|
83
|
+
until @pending.empty?
|
|
84
|
+
pc = @pending.pop || raise(TranslationError, "missing CFG node")
|
|
85
|
+
instruction = function.instructions.fetch(pc)
|
|
86
|
+
stack = @states.fetch(pc).dup
|
|
87
|
+
if Opcode.may_throw?(instruction[0])
|
|
88
|
+
stack.each_with_index do |id, depth|
|
|
89
|
+
function.handlers.fetch(id).clauses.each do |clause|
|
|
90
|
+
unless clause.handler_depth >= 0 && clause.handler_depth <= depth
|
|
91
|
+
raise TranslationError, "catch target retains a removed handler"
|
|
92
|
+
end
|
|
93
|
+
@exceptional.fetch(pc) << clause unless @exceptional.fetch(pc).include?(clause)
|
|
94
|
+
merge(clause.target_pc, stack.take(clause.handler_depth))
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
if instruction[0] == Opcode::EH_ENTER
|
|
99
|
+
id = instruction[1]
|
|
100
|
+
raise TranslationError, "invalid handler descriptor" unless id >= 0 && id < function.handlers.size
|
|
101
|
+
stack << id
|
|
102
|
+
elsif instruction[0] == Opcode::EH_TRIM
|
|
103
|
+
depth = instruction[1]
|
|
104
|
+
raise TranslationError, "invalid handler trim" unless depth >= 0 && depth <= stack.size
|
|
105
|
+
stack = stack.take(depth)
|
|
106
|
+
end
|
|
107
|
+
if @normal.fetch(pc).empty? && !Opcode.terminal?(instruction[0])
|
|
108
|
+
raise TranslationError, "MIR falls through without RETURN"
|
|
109
|
+
end
|
|
110
|
+
@normal.fetch(pc).each { |target| merge(target, stack) }
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
# @rbs pc: Integer
|
|
117
|
+
# @rbs stack: Array[Integer]
|
|
118
|
+
# @rbs return: void
|
|
119
|
+
def merge(pc, stack)
|
|
120
|
+
if @reached.fetch(pc)
|
|
121
|
+
raise TranslationError, "inconsistent handler stack at MIR PC #{pc}" unless @states.fetch(pc) == stack
|
|
122
|
+
else
|
|
123
|
+
@states[pc] = stack.dup
|
|
124
|
+
@reached[pc] = true
|
|
125
|
+
@pending << pc
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
class Analysis
|
|
131
|
+
# @rbs @flow: Wardite::Efflorescence::FlowGraph?
|
|
132
|
+
attr_reader :leaders #: Array[Integer]
|
|
133
|
+
attr_reader :reads #: Array[Integer]
|
|
134
|
+
attr_reader :writes #: Array[Integer]
|
|
135
|
+
|
|
136
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
137
|
+
def initialize(function)
|
|
138
|
+
@function = function
|
|
139
|
+
@flow = function.uses_eh || !function.handlers.empty? ? FlowGraph.new(function) : nil
|
|
140
|
+
@leaders = Array.new(function.instructions.size, 0)
|
|
141
|
+
@leaders[0] = 1 unless @leaders.empty?
|
|
142
|
+
@reads = Array.new(function.register_count, 0)
|
|
143
|
+
@writes = Array.new(function.register_count, 0)
|
|
144
|
+
function.instructions.each_with_index do |instruction, pc|
|
|
145
|
+
function.reads(instruction).each do |register|
|
|
146
|
+
@reads[register] += 1
|
|
147
|
+
end
|
|
148
|
+
destination = Opcode.destination(instruction)
|
|
149
|
+
@writes[destination] += 1 if destination >= 0
|
|
150
|
+
target = Opcode.target_position(instruction)
|
|
151
|
+
if target >= 0
|
|
152
|
+
destination_pc = instruction[target]
|
|
153
|
+
unless destination_pc >= 0 && destination_pc < function.instructions.size
|
|
154
|
+
raise TranslationError, "Efflorescence: invalid MIR branch target #{destination_pc}"
|
|
155
|
+
end
|
|
156
|
+
@leaders[destination_pc] = 1
|
|
157
|
+
end
|
|
158
|
+
exceptional = @flow ? @flow.exceptional.fetch(pc) : [] #: Array[Wardite::Efflorescence::CatchTarget]
|
|
159
|
+
exceptional.each { |clause| @leaders[clause.target_pc] = 1 }
|
|
160
|
+
if target >= 0 || Opcode.terminal?(instruction[0]) || !exceptional.empty?
|
|
161
|
+
@leaders[pc + 1] = 1 if pc + 1 < @leaders.size
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Ordinary optimization passes need only the summary above. Build the full
|
|
167
|
+
# normal CFG once for validation; EH summaries also need it for leaders.
|
|
168
|
+
# @rbs return: Wardite::Efflorescence::FlowGraph
|
|
169
|
+
def flow
|
|
170
|
+
@flow ||= FlowGraph.new(@function)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# @rbs register: Integer
|
|
174
|
+
# @rbs return: bool
|
|
175
|
+
def single_use?(register)
|
|
176
|
+
register >= 0 && @reads[register] == 1 && @writes[register] == 1
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
module Passes
|
|
181
|
+
# Expand only small, straight-line leaves from a snapshot of the IR.
|
|
182
|
+
# One round avoids transitive/exponential expansion and keeps recursion
|
|
183
|
+
# on the ordinary CALL path. Existing local passes clean up the copies.
|
|
184
|
+
class Inlining
|
|
185
|
+
MAX_INSTRUCTIONS = 8
|
|
186
|
+
MAX_REGISTERS = 32
|
|
187
|
+
MAX_ADDED_INSTRUCTIONS = 64
|
|
188
|
+
MAX_ADDED_REGISTERS = 256
|
|
189
|
+
|
|
190
|
+
# @rbs ir: Wardite::Efflorescence::IR
|
|
191
|
+
# @rbs return: Array[Integer]
|
|
192
|
+
def run(ir)
|
|
193
|
+
callees = ir.copy
|
|
194
|
+
changed = [] #: Array[Integer]
|
|
195
|
+
index = 0
|
|
196
|
+
while index < ir.functions.size
|
|
197
|
+
function = ir.defined_at(index)
|
|
198
|
+
changed << function.index if !function.uses_i64 && !function.uses_float && !function.uses_eh && !function.uses_store && !function.uses_dynamic_calls && expand_calls(function, callees)
|
|
199
|
+
index += 1
|
|
200
|
+
end
|
|
201
|
+
changed
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
private
|
|
205
|
+
|
|
206
|
+
# Explicit whitelist: new opcodes must opt into inlining. In particular,
|
|
207
|
+
# traps, profiling records, control flow and nested calls stay intact.
|
|
208
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
209
|
+
# @rbs return: bool
|
|
210
|
+
def eligible?(function)
|
|
211
|
+
return false if function.uses_i64 || function.uses_float || function.uses_eh || function.uses_store || function.uses_dynamic_calls
|
|
212
|
+
instructions = function.instructions
|
|
213
|
+
return false if instructions.empty? || instructions.size > MAX_INSTRUCTIONS || function.register_count > MAX_REGISTERS
|
|
214
|
+
return false unless instructions.last[0] == Opcode::RETURN
|
|
215
|
+
index = 0
|
|
216
|
+
while index < instructions.size - 1
|
|
217
|
+
opcode = instructions[index][0]
|
|
218
|
+
return false unless (opcode >= Opcode::COPY && opcode <= Opcode::I32_EQZ) ||
|
|
219
|
+
(opcode >= Opcode::I32_ADD_IMM && opcode <= Opcode::I32_SHR_U_IMM) ||
|
|
220
|
+
opcode == Opcode::I32_XOR_SHR_U_IMM || opcode == Opcode::I32_XOR_SHL_IMM
|
|
221
|
+
index += 1
|
|
222
|
+
end
|
|
223
|
+
true
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
227
|
+
# @rbs callees: Wardite::Efflorescence::IR
|
|
228
|
+
# @rbs return: bool
|
|
229
|
+
def expand_calls(function, callees)
|
|
230
|
+
rewrite = Rewrite.new(function)
|
|
231
|
+
added_instructions = 0
|
|
232
|
+
added_registers = 0
|
|
233
|
+
changed = false
|
|
234
|
+
function.instructions.each_with_index do |instruction, pc|
|
|
235
|
+
rewrite.mark(pc)
|
|
236
|
+
if instruction[0] == Opcode::CALL
|
|
237
|
+
callee = callees.function_at(instruction[2])
|
|
238
|
+
# Count the expansion before copy cleanup, conservatively.
|
|
239
|
+
growth = callee.parameter_count + callee.local_count + callee.instructions.size - 2
|
|
240
|
+
growth += 1 if instruction[1] >= 0
|
|
241
|
+
growth = 0 if growth < 0
|
|
242
|
+
registers = callee.register_count + (callee.local_count > 0 ? 1 : 0)
|
|
243
|
+
if eligible?(callee) && added_instructions + growth <= MAX_ADDED_INSTRUCTIONS &&
|
|
244
|
+
added_registers + registers <= MAX_ADDED_REGISTERS
|
|
245
|
+
expand(function, callee, instruction, rewrite)
|
|
246
|
+
added_instructions += growth
|
|
247
|
+
added_registers += registers
|
|
248
|
+
changed = true
|
|
249
|
+
next
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
rewrite.emit(instruction.copy)
|
|
253
|
+
end
|
|
254
|
+
rewrite.finish
|
|
255
|
+
changed
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# @rbs caller: Wardite::Efflorescence::Function
|
|
259
|
+
# @rbs callee: Wardite::Efflorescence::Function
|
|
260
|
+
# @rbs call: Wardite::Efflorescence::Instruction
|
|
261
|
+
# @rbs rewrite: Wardite::Efflorescence::Rewrite
|
|
262
|
+
# @rbs return: void
|
|
263
|
+
def expand(caller, callee, call, rewrite)
|
|
264
|
+
registers = [] #: Array[Integer]
|
|
265
|
+
index = 0
|
|
266
|
+
while index < callee.register_count
|
|
267
|
+
registers << if callee.constant_registers.include?(index)
|
|
268
|
+
caller.allocate_constant(callee.initial_value(index))
|
|
269
|
+
else
|
|
270
|
+
caller.allocate_register(0)
|
|
271
|
+
end
|
|
272
|
+
index += 1
|
|
273
|
+
end
|
|
274
|
+
# Fresh slots preserve argument snapshots even if the callee mutates
|
|
275
|
+
# a parameter, or the CALL destination aliases a caller argument.
|
|
276
|
+
index = 0
|
|
277
|
+
while index < callee.parameter_count
|
|
278
|
+
rewrite.emit(Instruction.new([Opcode::COPY, registers.fetch(index), call[3 + index]]))
|
|
279
|
+
index += 1
|
|
280
|
+
end
|
|
281
|
+
if callee.local_count > 0
|
|
282
|
+
zero = caller.allocate_constant(0)
|
|
283
|
+
while index < callee.parameter_count + callee.local_count
|
|
284
|
+
rewrite.emit(Instruction.new([Opcode::COPY, registers.fetch(index), zero]))
|
|
285
|
+
index += 1
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
callee.instructions.each do |original|
|
|
289
|
+
if original[0] == Opcode::RETURN
|
|
290
|
+
if call[1] >= 0
|
|
291
|
+
rewrite.emit(Instruction.new([Opcode::COPY, call[1], registers.fetch(original[1])]))
|
|
292
|
+
end
|
|
293
|
+
else
|
|
294
|
+
instruction = original.copy
|
|
295
|
+
Opcode.read_positions(original).each do |position|
|
|
296
|
+
instruction[position] = registers.fetch(original[position])
|
|
297
|
+
end
|
|
298
|
+
destination = Opcode.destination(original)
|
|
299
|
+
instruction[1] = registers.fetch(destination) if destination >= 0
|
|
300
|
+
rewrite.emit(instruction)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
class CopyPropagation
|
|
307
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
308
|
+
# @rbs return: void
|
|
309
|
+
def run(function)
|
|
310
|
+
coalesce_results(function)
|
|
311
|
+
propagate(function)
|
|
312
|
+
remove_overwritten_copies(function)
|
|
313
|
+
remove_dead_copies(function)
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
private
|
|
317
|
+
|
|
318
|
+
# Redirect an adjacent producer into the COPY destination. The producer
|
|
319
|
+
# temporary has one definition and one use in the entire function, and
|
|
320
|
+
# nobody can enter at the COPY. No value is moved across another op.
|
|
321
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
322
|
+
# @rbs return: void
|
|
323
|
+
def coalesce_results(function)
|
|
324
|
+
analysis = Analysis.new(function)
|
|
325
|
+
rewrite = Rewrite.new(function)
|
|
326
|
+
pc = 0
|
|
327
|
+
while pc < function.instructions.size
|
|
328
|
+
rewrite.mark(pc)
|
|
329
|
+
instruction = function.instructions[pc].copy
|
|
330
|
+
following = function.instructions[pc + 1]
|
|
331
|
+
destination = Opcode.destination(instruction)
|
|
332
|
+
if following && following[0] == Opcode::COPY && destination >= function.parameter_count + function.local_count &&
|
|
333
|
+
analysis.single_use?(destination) && following[2] == destination && analysis.leaders[pc + 1] == 0
|
|
334
|
+
instruction[1] = following[1]
|
|
335
|
+
rewrite.mark(pc + 1)
|
|
336
|
+
pc += 1
|
|
337
|
+
end
|
|
338
|
+
rewrite.emit(instruction)
|
|
339
|
+
pc += 1
|
|
340
|
+
end
|
|
341
|
+
rewrite.finish
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
345
|
+
# @rbs return: void
|
|
346
|
+
def propagate(function)
|
|
347
|
+
analysis = Analysis.new(function)
|
|
348
|
+
aliases = (0...function.register_count).to_a
|
|
349
|
+
rewrite = Rewrite.new(function)
|
|
350
|
+
function.instructions.each_with_index do |original, pc|
|
|
351
|
+
rewrite.mark(pc)
|
|
352
|
+
aliases = (0...function.register_count).to_a if analysis.leaders[pc] == 1
|
|
353
|
+
instruction = original.copy
|
|
354
|
+
Opcode.read_positions(instruction).each do |position|
|
|
355
|
+
instruction[position] = aliases.fetch(instruction[position])
|
|
356
|
+
end
|
|
357
|
+
destination = Opcode.destination(instruction)
|
|
358
|
+
if destination >= 0
|
|
359
|
+
aliases.each_with_index do |source, register|
|
|
360
|
+
aliases[register] = register if source == destination || register == destination
|
|
361
|
+
end
|
|
362
|
+
if instruction[0] == Opcode::COPY && instruction[2] != destination
|
|
363
|
+
aliases[destination] = instruction[2]
|
|
364
|
+
end
|
|
365
|
+
end
|
|
366
|
+
rewrite.emit(instruction)
|
|
367
|
+
end
|
|
368
|
+
rewrite.finish
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# Backward liveness inside each basic block removes copies overwritten
|
|
372
|
+
# before any read (e.g. an inlined parameter initializer). All registers
|
|
373
|
+
# are conservatively live across edges; no value moves across a branch.
|
|
374
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
375
|
+
# @rbs return: void
|
|
376
|
+
def remove_overwritten_copies(function)
|
|
377
|
+
analysis = Analysis.new(function)
|
|
378
|
+
live = Array.new(function.register_count, 1)
|
|
379
|
+
removed = Array.new(function.instructions.size, 0)
|
|
380
|
+
pc = function.instructions.size - 1
|
|
381
|
+
while pc >= 0
|
|
382
|
+
live.fill(1) if pc + 1 < analysis.leaders.size && analysis.leaders[pc + 1] == 1
|
|
383
|
+
instruction = function.instructions[pc]
|
|
384
|
+
destination = Opcode.destination(instruction)
|
|
385
|
+
if instruction[0] == Opcode::COPY && live[destination] == 0
|
|
386
|
+
removed[pc] = 1
|
|
387
|
+
else
|
|
388
|
+
live[destination] = 0 if destination >= 0
|
|
389
|
+
Opcode.read_positions(instruction).each { |position| live[instruction[position]] = 1 }
|
|
390
|
+
end
|
|
391
|
+
pc -= 1
|
|
392
|
+
end
|
|
393
|
+
rewrite = Rewrite.new(function)
|
|
394
|
+
function.instructions.each_with_index do |instruction, index|
|
|
395
|
+
rewrite.mark(index)
|
|
396
|
+
rewrite.emit(instruction.copy) if removed[index] == 0
|
|
397
|
+
end
|
|
398
|
+
rewrite.finish
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# Keep snapshot and join copies unless their destination has no reads
|
|
402
|
+
# anywhere. Iteration also removes copies feeding another dead copy.
|
|
403
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
404
|
+
# @rbs return: void
|
|
405
|
+
def remove_dead_copies(function)
|
|
406
|
+
loop do
|
|
407
|
+
analysis = Analysis.new(function)
|
|
408
|
+
rewrite = Rewrite.new(function)
|
|
409
|
+
changed = false
|
|
410
|
+
function.instructions.each_with_index do |instruction, pc|
|
|
411
|
+
rewrite.mark(pc)
|
|
412
|
+
if instruction[0] == Opcode::COPY && (instruction[1] == instruction[2] || analysis.reads[instruction[1]] == 0)
|
|
413
|
+
changed = true
|
|
414
|
+
else
|
|
415
|
+
rewrite.emit(instruction.copy)
|
|
416
|
+
end
|
|
417
|
+
end
|
|
418
|
+
rewrite.finish
|
|
419
|
+
break unless changed
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
class ImmediateSpecialization
|
|
425
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
426
|
+
# @rbs return: void
|
|
427
|
+
def run(function)
|
|
428
|
+
function.instructions.each do |instruction|
|
|
429
|
+
opcode = instruction[0]
|
|
430
|
+
next unless opcode >= Opcode::I32_ADD && opcode <= Opcode::I32_SHR_U
|
|
431
|
+
left = instruction[2]
|
|
432
|
+
right = instruction[3]
|
|
433
|
+
if !function.constant_registers.include?(right) && function.constant_registers.include?(left) &&
|
|
434
|
+
(opcode == Opcode::I32_ADD || opcode == Opcode::I32_MUL || opcode == Opcode::I32_XOR)
|
|
435
|
+
left, right = right, left
|
|
436
|
+
end
|
|
437
|
+
next unless function.constant_registers.include?(right)
|
|
438
|
+
instruction[0] = Opcode::I32_ADD_IMM + opcode - Opcode::I32_ADD
|
|
439
|
+
instruction[2] = left
|
|
440
|
+
instruction[3] = function.initial_value(right)
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
class BranchFusion
|
|
446
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
447
|
+
# @rbs return: void
|
|
448
|
+
def run(function)
|
|
449
|
+
analysis = Analysis.new(function)
|
|
450
|
+
rewrite = Rewrite.new(function)
|
|
451
|
+
pc = 0
|
|
452
|
+
while pc < function.instructions.size
|
|
453
|
+
rewrite.mark(pc)
|
|
454
|
+
instruction = function.instructions[pc]
|
|
455
|
+
following = function.instructions[pc + 1]
|
|
456
|
+
if instruction[0] == Opcode::I32_EQZ && following && following[0] == Opcode::BR_IF &&
|
|
457
|
+
following[1] == instruction[1] && analysis.single_use?(instruction[1]) &&
|
|
458
|
+
instruction[1] >= function.parameter_count + function.local_count && analysis.leaders[pc + 1] == 0
|
|
459
|
+
rewrite.mark(pc + 1)
|
|
460
|
+
rewrite.emit(Instruction.new([Opcode::BR_IF_ZERO, instruction[2], following[2]]))
|
|
461
|
+
pc += 2
|
|
462
|
+
else
|
|
463
|
+
rewrite.emit(instruction.copy)
|
|
464
|
+
pc += 1
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
rewrite.finish
|
|
468
|
+
end
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
class Superinstructions
|
|
472
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
473
|
+
# @rbs return: void
|
|
474
|
+
def run(function)
|
|
475
|
+
analysis = Analysis.new(function)
|
|
476
|
+
rewrite = Rewrite.new(function)
|
|
477
|
+
pc = 0
|
|
478
|
+
while pc < function.instructions.size
|
|
479
|
+
rewrite.mark(pc)
|
|
480
|
+
instruction = function.instructions[pc]
|
|
481
|
+
following = function.instructions[pc + 1]
|
|
482
|
+
shift = instruction[0] == Opcode::I32_SHR_U_IMM || instruction[0] == Opcode::I32_SHL_IMM
|
|
483
|
+
if shift && following && following[0] == Opcode::I32_XOR && analysis.leaders[pc + 1] == 0 &&
|
|
484
|
+
instruction[1] >= function.parameter_count + function.local_count && analysis.single_use?(instruction[1]) &&
|
|
485
|
+
((following[2] == instruction[2] && following[3] == instruction[1]) ||
|
|
486
|
+
(following[3] == instruction[2] && following[2] == instruction[1]))
|
|
487
|
+
opcode = instruction[0] == Opcode::I32_SHR_U_IMM ? Opcode::I32_XOR_SHR_U_IMM : Opcode::I32_XOR_SHL_IMM
|
|
488
|
+
rewrite.mark(pc + 1)
|
|
489
|
+
rewrite.emit(Instruction.new([opcode, following[1], instruction[2], instruction[3] & 31]))
|
|
490
|
+
pc += 2
|
|
491
|
+
else
|
|
492
|
+
rewrite.emit(instruction.copy)
|
|
493
|
+
pc += 1
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
rewrite.finish
|
|
497
|
+
end
|
|
498
|
+
end
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
class Optimizer
|
|
502
|
+
# @rbs configuration: Wardite::Efflorescence::Configuration
|
|
503
|
+
def initialize(configuration)
|
|
504
|
+
@configuration = configuration
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
# Never mutate the translator's semantic MIR; retain it for inspection
|
|
508
|
+
# and differential execution. Each pass can also be run independently.
|
|
509
|
+
# @rbs ir: Wardite::Efflorescence::IR
|
|
510
|
+
# @rbs return: Wardite::Efflorescence::IR
|
|
511
|
+
def optimize(ir)
|
|
512
|
+
output = ir.copy
|
|
513
|
+
output.functions.each do |function|
|
|
514
|
+
optimize_function(function)
|
|
515
|
+
end
|
|
516
|
+
if @configuration.enabled?(:inline)
|
|
517
|
+
Passes::Inlining.new.run(output).each do |index|
|
|
518
|
+
optimize_function(output.function_at(index))
|
|
519
|
+
end
|
|
520
|
+
end
|
|
521
|
+
output
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
private
|
|
525
|
+
|
|
526
|
+
# @rbs function: Wardite::Efflorescence::Function
|
|
527
|
+
# @rbs return: void
|
|
528
|
+
def optimize_function(function)
|
|
529
|
+
return if function.uses_i64 || function.uses_float || function.uses_eh || function.uses_store || function.uses_dynamic_calls
|
|
530
|
+
Passes::CopyPropagation.new.run(function) if @configuration.enabled?(:copy)
|
|
531
|
+
Passes::ImmediateSpecialization.new.run(function) if @configuration.enabled?(:immediate)
|
|
532
|
+
Passes::BranchFusion.new.run(function) if @configuration.enabled?(:branch)
|
|
533
|
+
Passes::Superinstructions.new.run(function) if @configuration.enabled?(:superinstruction)
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
class Lowerer
|
|
538
|
+
WIDTH = 4
|
|
539
|
+
|
|
540
|
+
# @rbs ir: Wardite::Efflorescence::IR
|
|
541
|
+
# @rbs return: void
|
|
542
|
+
def compile(ir)
|
|
543
|
+
function_index = 0
|
|
544
|
+
while function_index < ir.functions.size
|
|
545
|
+
function = ir.defined_at(function_index)
|
|
546
|
+
code = [] #: Array[Integer]
|
|
547
|
+
calls = [] #: Array[Wardite::Efflorescence::CallSite]
|
|
548
|
+
function.instructions.each do |instruction|
|
|
549
|
+
opcode = instruction[0]
|
|
550
|
+
a = instruction.size > 1 ? instruction[1] : 0
|
|
551
|
+
b = instruction.size > 2 ? instruction[2] : 0
|
|
552
|
+
c = instruction.size > 3 ? instruction[3] : 0
|
|
553
|
+
if opcode == Opcode::CALL
|
|
554
|
+
callee = ir.function_at(instruction[2])
|
|
555
|
+
arguments = [] #: Array[Integer]
|
|
556
|
+
index = 3
|
|
557
|
+
while index < instruction.size
|
|
558
|
+
arguments << instruction[index]
|
|
559
|
+
index += 1
|
|
560
|
+
end
|
|
561
|
+
b = calls.size
|
|
562
|
+
# The fourth slot is already decoded on every dispatch. A single
|
|
563
|
+
# argument can name its source here, bypassing the argument table
|
|
564
|
+
# and loop; -1 selects the generic zero/multiple-argument path.
|
|
565
|
+
c = arguments.size == 1 ? arguments.fetch(0) : -1
|
|
566
|
+
calls << CallSite.new(callee, arguments)
|
|
567
|
+
end
|
|
568
|
+
target_position = Opcode.target_position(instruction)
|
|
569
|
+
a *= WIDTH if target_position == 1
|
|
570
|
+
b *= WIDTH if target_position == 2
|
|
571
|
+
code << opcode << a << b << c
|
|
572
|
+
end
|
|
573
|
+
function.lower_handlers
|
|
574
|
+
function.install_code(code, calls)
|
|
575
|
+
function_index += 1
|
|
576
|
+
end
|
|
577
|
+
end
|
|
578
|
+
end
|
|
579
|
+
end
|
|
580
|
+
end
|