wardite 0.9.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +191 -0
  3. data/Makefile +14 -0
  4. data/README.md +80 -1
  5. data/Rakefile +5 -0
  6. data/benchmark/README.md +500 -0
  7. data/benchmark/build_call_profile.rb +42 -0
  8. data/benchmark/call_cost_worker.rb +27 -0
  9. data/benchmark/compare.rb +93 -0
  10. data/benchmark/compare_call_types.rb +212 -0
  11. data/benchmark/compare_dispatch.rb +123 -0
  12. data/benchmark/compare_engines.rb +119 -0
  13. data/benchmark/compare_optimizations.rb +139 -0
  14. data/benchmark/engine_phases.rb +30 -0
  15. data/benchmark/figures/README.md +35 -0
  16. data/benchmark/figures/plot_spinel_comparison.py +137 -0
  17. data/benchmark/figures/spinel_new_vm_candlestick.png +0 -0
  18. data/benchmark/figures/spinel_new_vm_candlestick.svg +7615 -0
  19. data/benchmark/figures/spinel_new_vm_summary.csv +9 -0
  20. data/benchmark/figures/spinel_new_vm_summary.json +82 -0
  21. data/benchmark/float_m5.wat +42 -0
  22. data/benchmark/i32_call_arities.wat +89 -0
  23. data/benchmark/i32_calls.wat +174 -0
  24. data/benchmark/i32_memory.wat +58 -0
  25. data/benchmark/i32_mix.wat +80 -0
  26. data/benchmark/i64_m4.wat +55 -0
  27. data/benchmark/investigate_calls.rb +231 -0
  28. data/benchmark/investigate_generated_guards.rb +218 -0
  29. data/benchmark/m3_call_worker.rb +38 -0
  30. data/benchmark/measure_m1.rb +51 -0
  31. data/benchmark/measure_m2.rb +55 -0
  32. data/benchmark/measure_m3_calls.rb +89 -0
  33. data/benchmark/measure_m4.rb +134 -0
  34. data/benchmark/measure_m5_boundary.rb +41 -0
  35. data/benchmark/measure_wasi_write.rb +75 -0
  36. data/benchmark/optimization_worker.rb +23 -0
  37. data/benchmark/results/call_arities_exploratory_2026-11-04.json +4320 -0
  38. data/benchmark/results/call_arities_spinel_2026-11-04.json +1200 -0
  39. data/benchmark/results/call_cost_spinel_2026-11-04.json +2392 -0
  40. data/benchmark/results/call_protocol_exploratory_2026-11-04.json +3947 -0
  41. data/benchmark/results/call_protocol_profile_2026-11-04.json +383 -0
  42. data/benchmark/results/call_protocol_ruby_2026-11-04.json +1213 -0
  43. data/benchmark/results/call_protocol_spinel_2026-11-04.json +913 -0
  44. data/benchmark/results/call_types_ruby_2026-11-04.json +1212 -0
  45. data/benchmark/results/call_types_spinel_2026-11-04.json +923 -0
  46. data/benchmark/results/efflorescence-m1-cruby.json +799 -0
  47. data/benchmark/results/efflorescence-m1-spinel.json +799 -0
  48. data/benchmark/results/efflorescence-m2-bulk-loop.json +820 -0
  49. data/benchmark/results/efflorescence-m2-i32-cruby.json +799 -0
  50. data/benchmark/results/efflorescence-m2-i32-spinel.json +799 -0
  51. data/benchmark/results/efflorescence-m2-memory.json +408 -0
  52. data/benchmark/results/generated_guards_exploratory_2026-11-04.json +1225 -0
  53. data/benchmark/results/generated_guards_profile_2026-11-04.json +592 -0
  54. data/benchmark/results/generated_guards_spinel_2026-11-04.json +1650 -0
  55. data/benchmark/results/generated_guards_validation_2026-11-04.json +256 -0
  56. data/benchmark/results/inlining_engines_2026-11-04.json +241 -0
  57. data/benchmark/results/inlining_ruby_2026-11-04.json +1213 -0
  58. data/benchmark/results/inlining_spinel_2026-11-04.json +893 -0
  59. data/benchmark/results/operand_decode_exploratory_2026-11-04.json +1319 -0
  60. data/benchmark/results/operand_decode_ruby_2026-11-04.json +1213 -0
  61. data/benchmark/results/operand_decode_spinel_2026-11-04.json +924 -0
  62. data/benchmark/results/selective_reset_spinel_2026-11-04.json +454 -0
  63. data/benchmark/results/selective_reset_spinel_2026-11-04_exploratory.json +2679 -0
  64. data/benchmark/results/spinel_fetch_root_patch_2026-11-04.json +4523 -0
  65. data/benchmark/results/while_final_compiler_comparison_2026-11-04.json +324 -0
  66. data/benchmark/results/while_patched_external_2026-11-04.json +220 -0
  67. data/benchmark/results/while_user_unpatched_2026-11-04.json +230 -0
  68. data/benchmark/summarize_call_profile.py +54 -0
  69. data/benchmark/validate_m1_eh.rb +34 -0
  70. data/benchmark/validate_m2.rb +119 -0
  71. data/benchmark/validate_m3.rb +44 -0
  72. data/benchmark/validate_m4.rb +95 -0
  73. data/benchmark/validate_m5.rb +108 -0
  74. data/docs/adr/0001-efflorescence-m1-control-and-eh.md +319 -0
  75. data/docs/adr/0002-efflorescence-m2-memory-and-globals.md +185 -0
  76. data/docs/adr/0003-efflorescence-m3-host-and-indirect-calls.md +228 -0
  77. data/docs/adr/0004-efflorescence-m4-i64.md +202 -0
  78. data/docs/adr/0005-efflorescence-m5-floating-point.md +127 -0
  79. data/docs/adr/0006-efflorescence-m6-coverage-parity.md +101 -0
  80. data/docs/efflorescence-call-arities.md +115 -0
  81. data/docs/efflorescence-call-investigation.md +155 -0
  82. data/docs/efflorescence-call-transfer.md +128 -0
  83. data/docs/efflorescence-call-types.md +102 -0
  84. data/docs/efflorescence-coverage-ja.md +351 -0
  85. data/docs/efflorescence-generated-c-guards.md +405 -0
  86. data/docs/efflorescence-inlining.md +165 -0
  87. data/docs/efflorescence-ja.md +155 -0
  88. data/docs/efflorescence-m1.md +160 -0
  89. data/docs/efflorescence-m2.md +233 -0
  90. data/docs/efflorescence-m3.md +131 -0
  91. data/docs/efflorescence-m4.md +88 -0
  92. data/docs/efflorescence-m5.md +82 -0
  93. data/docs/efflorescence-m6.md +95 -0
  94. data/docs/efflorescence-operand-decoding.md +105 -0
  95. data/docs/efflorescence-phase2-ja.md +225 -0
  96. data/docs/efflorescence-phase2.md +240 -0
  97. data/docs/efflorescence-register-initialization.md +75 -0
  98. data/docs/efflorescence.md +144 -0
  99. data/docs/spinel-string-aset-inplace-proposal.md +200 -0
  100. data/lib/wardite/alu_f32.generated.rb +1 -0
  101. data/lib/wardite/alu_f64.generated.rb +1 -0
  102. data/lib/wardite/alu_i32.generated.rb +14 -9
  103. data/lib/wardite/alu_i64.generated.rb +18 -20
  104. data/lib/wardite/cli/cli.rb +61 -28
  105. data/lib/wardite/const.rb +1 -0
  106. data/lib/wardite/efflorescence/float_arithmetic.rb +216 -0
  107. data/lib/wardite/efflorescence/i64_arithmetic.rb +257 -0
  108. data/lib/wardite/efflorescence/inspection.rb +165 -0
  109. data/lib/wardite/efflorescence/ir.rb +1151 -0
  110. data/lib/wardite/efflorescence/optimizer.rb +580 -0
  111. data/lib/wardite/efflorescence/register_validation.rb +290 -0
  112. data/lib/wardite/efflorescence/store_access.rb +271 -0
  113. data/lib/wardite/efflorescence/translator.rb +859 -0
  114. data/lib/wardite/efflorescence/vm.rb +1006 -0
  115. data/lib/wardite/efflorescence/wasi.rb +108 -0
  116. data/lib/wardite/efflorescence.rb +11 -0
  117. data/lib/wardite/errors.rb +20 -0
  118. data/lib/wardite/float_bits.rb +46 -0
  119. data/lib/wardite/instruction.rb +35 -4
  120. data/lib/wardite/leb128.rb +33 -12
  121. data/lib/wardite/load.rb +203 -138
  122. data/lib/wardite/value.rb +169 -175
  123. data/lib/wardite/version.rb +1 -1
  124. data/lib/wardite/wasi/consts.rb +1 -0
  125. data/lib/wardite/wasm_module.rb +6 -3
  126. data/lib/wardite.rb +184 -70
  127. data/rbs_collection.lock.yaml +36 -96
  128. data/scripts/gen_alu.rb +66 -10
  129. data/sig/generated/wardite/cli/cli.rbs +8 -6
  130. data/sig/generated/wardite/const.rbs +2 -0
  131. data/sig/generated/wardite/efflorescence/float_arithmetic.rbs +65 -0
  132. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +99 -0
  133. data/sig/generated/wardite/efflorescence/inspection.rbs +64 -0
  134. data/sig/generated/wardite/efflorescence/ir.rbs +881 -0
  135. data/sig/generated/wardite/efflorescence/optimizer.rbs +189 -0
  136. data/sig/generated/wardite/efflorescence/register_validation.rbs +76 -0
  137. data/sig/generated/wardite/efflorescence/store_access.rbs +134 -0
  138. data/sig/generated/wardite/efflorescence/translator.rbs +214 -0
  139. data/sig/generated/wardite/efflorescence/vm.rbs +179 -0
  140. data/sig/generated/wardite/efflorescence/wasi.rbs +47 -0
  141. data/sig/generated/wardite/errors.rbs +12 -0
  142. data/sig/generated/wardite/float_bits.rbs +30 -0
  143. data/sig/generated/wardite/instruction.rbs +22 -1
  144. data/sig/generated/wardite/leb128.rbs +3 -1
  145. data/sig/generated/wardite/load.rbs +29 -4
  146. data/sig/generated/wardite/value.rbs +70 -11
  147. data/sig/generated/wardite/wasi/consts.rbs +2 -0
  148. data/sig/generated/wardite.rbs +49 -12
  149. data/sig/vernier.rbs +2 -2
  150. data/spinel/README.md +30 -0
  151. data/spinel/bin/wardite.rb +6 -0
  152. data/spinel/spin.toml +7 -0
  153. metadata +126 -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