wardite 1.2.2 → 1.3.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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -11
  3. data/Rakefile +0 -177
  4. data/benchmark/README.md +10 -36
  5. data/benchmark/call_cost_worker.rb +3 -2
  6. data/benchmark/compare.rb +1 -2
  7. data/benchmark/compare_linear_memory_loads.py +144 -0
  8. data/benchmark/i32_memory.wat +5 -1
  9. data/benchmark/investigate_calls.rb +4 -4
  10. data/benchmark/linear_memory_loads.wat +52 -0
  11. data/benchmark/optimization_worker.rb +3 -2
  12. data/benchmark/patches/adr0017-spinel-signed64.patch +190 -0
  13. data/benchmark/patches/adr0017-wardite-split-load64.patch +70 -0
  14. data/benchmark/results/adr0019-startup.json +1687 -0
  15. data/benchmark/results/adr0020-i64.json +2034 -0
  16. data/benchmark/results/adr0021-optimizer.json +5798 -0
  17. data/benchmark/results/adr0022-lazy-mir.json +3057 -0
  18. data/benchmark/results/crc8-stream-repeat.csv +89 -0
  19. data/benchmark/results/crc8-stream.csv +89 -0
  20. data/benchmark/results/linear-memory-loads-a2aa05227.csv +793 -0
  21. data/benchmark/results/linear-memory-loads-stage-b.csv +353 -0
  22. data/benchmark/results/linear-memory-loads.csv +1057 -0
  23. data/benchmark/results/real-workloads-baseline-2026-11-18.json +1741 -0
  24. data/benchmark/results/stack-vm-removal.json +334 -0
  25. data/benchmark/validate_m4.rb +0 -2
  26. data/docs/adr/0017-efflorescence-linear-memory-loads.md +90 -0
  27. data/docs/adr/0018-efflorescence-real-workload-measurement.md +126 -0
  28. data/docs/adr/0019-efflorescence-startup-quick-wins.md +85 -0
  29. data/docs/adr/0020-efflorescence-inline-i64-bit-operations.md +89 -0
  30. data/docs/adr/0021-efflorescence-optimize-real-functions.md +133 -0
  31. data/docs/adr/0022-efflorescence-lazy-function-build.md +111 -0
  32. data/docs/adr/0023-efflorescence-byte-sequences-and-copy-coalescing.md +56 -0
  33. data/docs/efflorescence-ja.md +3 -5
  34. data/docs/efflorescence-linear-memory-loads.md +134 -0
  35. data/docs/efflorescence-m6.md +1 -1
  36. data/docs/efflorescence-mir2-cache.md +7 -0
  37. data/docs/efflorescence-real-workloads.md +312 -0
  38. data/docs/efflorescence-spinel-linear-memory.md +2 -2
  39. data/docs/efflorescence.md +3 -5
  40. data/docs/stack-vm-removal.md +49 -0
  41. data/lib/wardite/cli/cli.rb +24 -24
  42. data/lib/wardite/efflorescence/byte_sequences.rb +80 -0
  43. data/lib/wardite/efflorescence/function_preparer.rb +64 -0
  44. data/lib/wardite/efflorescence/i64_arithmetic.rb +31 -0
  45. data/lib/wardite/efflorescence/inspection.rb +44 -12
  46. data/lib/wardite/efflorescence/ir.rb +33 -10
  47. data/lib/wardite/efflorescence/mir_cache.rb +10 -1
  48. data/lib/wardite/efflorescence/optimizer.rb +151 -42
  49. data/lib/wardite/efflorescence/store_access.rb +17 -0
  50. data/lib/wardite/efflorescence/translation_source.rb +50 -0
  51. data/lib/wardite/efflorescence/translator.rb +43 -13
  52. data/lib/wardite/efflorescence/vm.rb +116 -48
  53. data/lib/wardite/efflorescence.rb +3 -0
  54. data/lib/wardite/load.rb +46 -62
  55. data/lib/wardite/version.rb +1 -1
  56. data/lib/wardite.rb +21 -757
  57. data/scripts/build_spinel_zig.rb +1 -1
  58. data/sig/generated/wardite/cli/cli.rbs +4 -2
  59. data/sig/generated/wardite/efflorescence/byte_sequences.rbs +33 -0
  60. data/sig/generated/wardite/efflorescence/function_preparer.rbs +25 -0
  61. data/sig/generated/wardite/efflorescence/i64_arithmetic.rbs +19 -0
  62. data/sig/generated/wardite/efflorescence/inspection.rbs +8 -0
  63. data/sig/generated/wardite/efflorescence/ir.rbs +18 -2
  64. data/sig/generated/wardite/efflorescence/optimizer.rbs +41 -3
  65. data/sig/generated/wardite/efflorescence/store_access.rbs +5 -0
  66. data/sig/generated/wardite/efflorescence/translation_source.rbs +34 -0
  67. data/sig/generated/wardite/efflorescence/translator.rbs +11 -1
  68. data/sig/generated/wardite/efflorescence/vm.rbs +19 -9
  69. data/sig/generated/wardite/load.rbs +18 -18
  70. data/sig/generated/wardite.rbs +4 -155
  71. metadata +32 -17
  72. data/benchmark/compare_engines.rb +0 -119
  73. data/benchmark/engine_phases.rb +0 -30
  74. data/lib/wardite/alu_f32.generated.rb +0 -261
  75. data/lib/wardite/alu_f64.generated.rb +0 -261
  76. data/lib/wardite/alu_i32.generated.rb +0 -510
  77. data/lib/wardite/alu_i64.generated.rb +0 -572
  78. data/lib/wardite/convert.generated.rb +0 -340
  79. data/scripts/gen_alu.rb +0 -864
  80. data/scripts/gen_conv.rb +0 -86
  81. data/scripts/templates/alu_module.rb.tmpl +0 -20
  82. data/scripts/templates/conv_module.rb.tmpl +0 -20
  83. data/sig/generated/wardite/alu_f32.generated.rbs +0 -12
  84. data/sig/generated/wardite/alu_f64.generated.rbs +0 -12
  85. data/sig/generated/wardite/alu_i32.generated.rbs +0 -12
  86. data/sig/generated/wardite/alu_i64.generated.rbs +0 -12
  87. data/sig/generated/wardite/convert.generated.rbs +0 -12
@@ -0,0 +1,190 @@
1
+ From 1d6433c70c0f8f8160726a8253d229f913bcbc3c Mon Sep 17 00:00:00 2001
2
+ From: Uchio Kondo <udzura@udzura.jp>
3
+ Date: Tue, 17 Nov 2026 16:55:05 +0900
4
+ Subject: [PATCH] Lower signed 64-bit buffer reads to integers
5
+
6
+ Use the typed accessor when the target can hold every signed value.
7
+ Keep the boxed path for unsigned 64-bit and on narrow targets.
8
+
9
+ Co-authored-by: Codex <codex@openai.com>
10
+
11
+ diff --git a/lib/sp_iobuffer.h b/lib/sp_iobuffer.h
12
+ index b52af70fd..a811278ac 100644
13
+ --- a/lib/sp_iobuffer.h
14
+ +++ b/lib/sp_iobuffer.h
15
+ @@ -79,8 +79,8 @@ SP_COLD double sp_IOBuffer_get_f(sp_IOBuffer *b, sp_int ty, sp_int off);
16
+ header when it uses IO::Buffer). `ty` is a literal at every call site, so
17
+ the width, byte order and sign fold away and a read is a bounds check and
18
+ a load. An in-range offset on an owned buffer or a valid slice takes that
19
+ - path. Invalid views and offsets take the checked read. The 64-bit
20
+ - integers read boxed through sp_IOBuffer_get_x.
21
+ + path. Invalid views and offsets take the checked read. Integer types
22
+ + that can exceed sp_int read boxed through sp_IOBuffer_get_x.
23
+ Width, order and sign follow iob_types in sp_iobuffer.c: the integers
24
+ after U8/S8 come in (u, s, U, S) groups, the floats are (f32, f64, F32,
25
+ F64), and an odd integer code is a signed type. */
26
+ diff --git a/src/analyze_infer_recv.c b/src/analyze_infer_recv.c
27
+ index cc838e9f7..89e8b9bc7 100644
28
+ --- a/src/analyze_infer_recv.c
29
+ +++ b/src/analyze_infer_recv.c
30
+ @@ -1338,8 +1338,8 @@ int infer_object_call(Compiler *c, int id, TyKind rt, TyKind *out) {
31
+ int cid = ncid;
32
+ /* IO::Buffer#get_value with a LITERAL type symbol lowers to a typed
33
+ accessor (the codegen fold); the site's type follows the symbol:
34
+ - floats are Float, u64 stays boxed (a value above 2^63-1 is a
35
+ - Bignum), every other integer type is a machine int. The offset may
36
+ + floats are Float, integer types that can exceed the target's
37
+ + machine integer stay boxed, and the rest are Integer. The offset may
38
+ be boxed (the fold unboxes it), which is what every int local is
39
+ under --int-overflow=promote. The conditions mirror the fold's
40
+ exactly (a literal the fold declines keeps the generic binding's
41
+ @@ -1360,7 +1360,7 @@ int infer_object_call(Compiler *c, int id, TyKind rt, TyKind *out) {
42
+ if (it >= 0 && ot == TY_UNKNOWN && g_infer_optimistic) { *out = TY_UNKNOWN; return 1; }
43
+ if (it >= 0 && (ot == TY_INT || ot == TY_POLY)) {
44
+ *out = comp_iob_ty_is_float(it) ? TY_FLOAT
45
+ - : comp_iob_ty_is_64(it) ? TY_POLY : TY_INT;
46
+ + : comp_iob_ty_boxes_read(it) ? TY_POLY : TY_INT;
47
+ return 1;
48
+ }
49
+ }
50
+ diff --git a/src/codegen_call_recv.c b/src/codegen_call_recv.c
51
+ index 1615be548..fa3605707 100644
52
+ --- a/src/codegen_call_recv.c
53
+ +++ b/src/codegen_call_recv.c
54
+ @@ -12713,7 +12713,7 @@ static int emit_native_binding_call(Compiler *c, int id, int cid, const char *na
55
+ (comp_ntype(c, argv[1]) == TY_INT || comp_ntype(c, argv[1]) == TY_POLY)) {
56
+ int it = comp_iob_sym_type(nt_str(nt, argv[0], "value"));
57
+ if (it >= 0 && sp_streq(name, "get_value") && argc == 2) {
58
+ - if (comp_iob_ty_is_64(it)) {
59
+ + if (comp_iob_ty_boxes_read(it)) {
60
+ buf_puts(b, "sp_IOBuffer_get_x(");
61
+ emit_expr(c, recv, b);
62
+ buf_printf(b, ", %d, ", it);
63
+ diff --git a/src/codegen_stmt.c b/src/codegen_stmt.c
64
+ index 4b7702833..7fbee382e 100644
65
+ --- a/src/codegen_stmt.c
66
+ +++ b/src/codegen_stmt.c
67
+ @@ -12537,8 +12537,8 @@ static int hc_iob_binding(Compiler *c, TyKind t, const char *nm, int argc) {
68
+ return comp_native_method_find(c, cid, nm, argc, 0) >= 0 && comp_method_in_chain(c, cid, nm, NULL) < 0;
69
+ }
70
+ /* `buf.get_value(:U8, i)`: a read emit_native_binding_call lowers inline --
71
+ - a literal type symbol of 32 bits or fewer, or a float (the 64-bit
72
+ - integers read boxed) -- at an Integer offset */
73
+ + a literal float or integer type whose values fit the typed accessor,
74
+ + at an Integer offset */
75
+ int hc_iob_read(Compiler *c, int id) {
76
+ const NodeTable *nt = c->nt;
77
+ const char *nm = nt_str(nt, id, "name");
78
+ @@ -12549,7 +12549,7 @@ int hc_iob_read(Compiler *c, int id) {
79
+ nt_kind(nt, av[0]) != NK_SymbolNode || !hc_iob_binding(c, comp_ntype(c, recv), nm, 2))
80
+ return 0;
81
+ int it = comp_iob_sym_type(nt_str(nt, av[0], "value"));
82
+ - return it >= 0 && !comp_iob_ty_is_64(it) && (comp_ntype(c, av[1]) == TY_INT || hc_promoted_int(c, av[1]));
83
+ + return it >= 0 && !comp_iob_ty_boxes_read(it) && (comp_ntype(c, av[1]) == TY_INT || hc_promoted_int(c, av[1]));
84
+ }
85
+ /* `buf.set_value(:U8, i, v)`: a write emit_native_binding_call lowers
86
+ inline -- a literal type symbol at an Integer offset, with an Integer
87
+ diff --git a/src/compiler.c b/src/compiler.c
88
+ index d6c5db252..0bc10cdc9 100644
89
+ --- a/src/compiler.c
90
+ +++ b/src/compiler.c
91
+ @@ -3620,14 +3620,15 @@ int comp_iob_ty_is_float(int t) { return t >= 14 && t <= 17; }
92
+ int comp_iob_ty_width(int t) { return t >= 14 ? (t == 14 || t == 16 ? 4 : 8) : t <= 1 ? 1 : t <= 5 ? 2 : t <= 9 ? 4 : 8; }
93
+ int comp_iob_ty_be(int t) { return t >= 14 ? t >= 16 : t >= 2 && ((t - 2) & 2); }
94
+ int comp_iob_ty_signed(int t) { return t < 14 && (t & 1); }
95
+ -/* the 64-bit integer types stay BOXED through the fold: u64 values above
96
+ - 2^63-1 are Bignums, and the signed ones take the same path */
97
+ -int comp_iob_ty_is_64(int t) {
98
+ +/* A signed 64-bit value fits a 64-bit sp_int. Unsigned 64-bit values
99
+ + can require a Bignum, as can either 64-bit type on a 32-bit target. */
100
+ +int comp_iob_ty_boxes_read(int t) {
101
+ extern int sp_target_int_bits;
102
+ /* on a 32-bit target the 32-bit types are in the same position: a U32
103
+ above 2**31-1 is a Bignum there (#4647) */
104
+ if (sp_target_int_bits == 32 && t >= 6 && t <= 9) return 1; /* u32/s32/U32/S32 */
105
+ - return t >= 10 && t <= 13; /* u64/s64/U64/S64 */
106
+ + return comp_iob_ty_width(t) == 8 && !comp_iob_ty_is_float(t) &&
107
+ + (sp_target_int_bits == 32 || !comp_iob_ty_signed(t));
108
+ }
109
+
110
+ /* Type-keyed variant: among same-name same-arity bindings, prefer one whose
111
+ diff --git a/src/compiler.h b/src/compiler.h
112
+ index 5e5765043..cd2dfa9af 100644
113
+ --- a/src/compiler.h
114
+ +++ b/src/compiler.h
115
+ @@ -2311,7 +2311,7 @@ int comp_iob_ty_is_float(int t);
116
+ int comp_iob_ty_width(int t);
117
+ int comp_iob_ty_be(int t);
118
+ int comp_iob_ty_signed(int t);
119
+ -int comp_iob_ty_is_64(int t);
120
+ +int comp_iob_ty_boxes_read(int t);
121
+
122
+ /* Global variables and top-level constants. *_intern finds or creates. */
123
+ LocalVar *comp_gvar(Compiler *c, const char *name);
124
+ diff --git a/test/io_buffer_signed64_inline.rb b/test/io_buffer_signed64_inline.rb
125
+ new file mode 100644
126
+ index 000000000..78d378dfc
127
+ --- /dev/null
128
+ +++ b/test/io_buffer_signed64_inline.rb
129
+ @@ -0,0 +1,45 @@
130
+ +# Literal signed 64-bit reads must keep all bits on both native and narrow targets.
131
+ +def signed_words(buffer)
132
+ + puts buffer.get_value(:s64, 3)
133
+ + puts buffer.get_value(:S64, 11)
134
+ + puts buffer.get_value(:s64, 19)
135
+ + puts buffer.get_value(:u64, 3)
136
+ +end
137
+ +
138
+ +def sum_signed_words(buffer)
139
+ + sum = 0
140
+ + i = 0
141
+ + while i < 3
142
+ + sum += buffer.get_value(:s64, i * 8)
143
+ + i += 1
144
+ + end
145
+ + sum
146
+ +end
147
+ +
148
+ +buffer = IO::Buffer.new(27)
149
+ +buffer.set_string("\x00\x00\x00\x00\x00\x00\x00\x80", 3)
150
+ +buffer.set_string("\x7f\xff\xff\xff\xff\xff\xff\xff", 11)
151
+ +buffer.set_string("\xff\xff\xff\xff\xff\xff\xff\xff", 19)
152
+ +signed_words(buffer)
153
+ +
154
+ +view = buffer.slice(1, 26)
155
+ +puts view.get_value(:s64, 2)
156
+ +puts view.get_value(:S64, 10)
157
+ +puts view.get_value(:s64, 18)
158
+ +# Min + 1 + -1 is representable after each addition.
159
+ +buffer.set_string("\x01\0\0\0\0\0\0\0", 11)
160
+ +puts sum_signed_words(buffer.slice(3, 24))
161
+ +
162
+ +begin
163
+ + view.get_value(:s64, 19)
164
+ + raise "accepted a read beyond the slice"
165
+ +rescue ArgumentError => error
166
+ + puts error.class
167
+ +end
168
+ +buffer.free
169
+ +begin
170
+ + view.get_value(:s64, 2)
171
+ + raise "read an invalidated slice"
172
+ +rescue IO::Buffer::InvalidatedError => error
173
+ + puts error.class
174
+ +end
175
+ diff --git a/test/io_buffer_signed64_inline.rb.expected b/test/io_buffer_signed64_inline.rb.expected
176
+ new file mode 100644
177
+ index 000000000..4be9c2de2
178
+ --- /dev/null
179
+ +++ b/test/io_buffer_signed64_inline.rb.expected
180
+ @@ -0,0 +1,10 @@
181
+ +-9223372036854775808
182
+ +9223372036854775807
183
+ +-1
184
+ +9223372036854775808
185
+ +-9223372036854775808
186
+ +9223372036854775807
187
+ +-1
188
+ +-9223372036854775808
189
+ +ArgumentError
190
+ +IO::Buffer::InvalidatedError
@@ -0,0 +1,70 @@
1
+ diff --git a/lib/wardite/efflorescence/store_access.rb b/lib/wardite/efflorescence/store_access.rb
2
+ --- a/lib/wardite/efflorescence/store_access.rb
3
+ +++ b/lib/wardite/efflorescence/store_access.rb
4
+ @@ -158,12 +158,12 @@
5
+ # @rbs signed: bool
6
+ # @rbs return: Integer
7
+ def load_i64(address, offset, width, signed)
8
+ + return load64(address, offset) if width == 8
9
+ +
10
+ start = address + offset
11
+ if RUBY_ENGINE == "spinel"
12
+ source = memory.buffer
13
+ StoreAccess.check_range(start, width, source.size)
14
+ - return Integer(source.get_value(:s64, start)) if width == 8
15
+ -
16
+ value = case width
17
+ when 1 then Integer(source.get_value(:U8, start))
18
+ when 2 then Integer(source.get_value(:u16, start))
19
+ @@ -173,12 +173,25 @@
20
+ end
21
+ data = memory.data
22
+ StoreAccess.check_range(start, width, data.bytesize)
23
+ - if width == 8
24
+ - I64Arithmetic.join(read_word(data, start, 4), read_word(data, start + 4, 4))
25
+ - else
26
+ - value = read_word(data, start, width)
27
+ - signed ? I64Arithmetic.extend(value, width * 8) : value
28
+ + value = read_word(data, start, width)
29
+ + signed ? I64Arithmetic.extend(value, width * 8) : value
30
+ + end
31
+ +
32
+ + # @rbs address: Integer
33
+ + # @rbs offset: Integer
34
+ + # @rbs return: Integer
35
+ + def load64(address, offset)
36
+ + start = address + offset
37
+ + value = if RUBY_ENGINE == "spinel"
38
+ + source = memory.buffer
39
+ + StoreAccess.check_range(start, 8, source.size)
40
+ + source.get_value(:s64, start)
41
+ + else
42
+ + data = memory.data
43
+ + StoreAccess.check_range(start, 8, data.bytesize)
44
+ + I64Arithmetic.join(read_word(data, start, 4), read_word(data, start + 4, 4))
45
+ end
46
+ + Integer(value)
47
+ end
48
+
49
+ # Check the whole access before the first write. Effective addresses are
50
+ diff --git a/lib/wardite/efflorescence/vm.rb b/lib/wardite/efflorescence/vm.rb
51
+ --- a/lib/wardite/efflorescence/vm.rb
52
+ +++ b/lib/wardite/efflorescence/vm.rb
53
+ @@ -629,7 +629,7 @@
54
+ registers[a] = @store_access.load(registers[b], offset, 4, false)
55
+ when Opcode::F64_LOAD
56
+ offset = instructions.fetch(pc + 3)
57
+ - registers[a] = @store_access.load_i64(registers[b], offset, 8, false)
58
+ + registers[a] = @store_access.load64(registers[b], offset)
59
+ when Opcode::F32_STORE
60
+ offset = instructions.fetch(pc + 3)
61
+ @store_access.store(registers[b], offset, 4, registers[a])
62
+ @@ -1095,7 +1095,7 @@
63
+ when 104 # Opcode::I64_LOAD
64
+ b = instructions.fetch(pc + 2)
65
+ c = instructions.fetch(pc + 3)
66
+ - registers[a] = @store_access.load_i64(registers[b], c, 8, false)
67
+ + registers[a] = @store_access.load64(registers[b], c)
68
+ when 105 # Opcode::I64_LOAD8_S
69
+ b = instructions.fetch(pc + 2)
70
+ c = instructions.fetch(pc + 3)