1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
# frozen_string_literal: true
#
# This set of tests can be run with:
# make test-all TESTS=test/ruby/test_zjit.rb
require 'test/unit'
require 'envutil'
require_relative '../lib/jit_support'
return unless JITSupport.zjit_supported?
class TestZJIT < Test::Unit::TestCase
def test_nil
assert_compiles 'nil', %q{
def test = nil
test
}
end
def test_putobject
assert_compiles '1', %q{
def test = 1
test
}
end
def test_leave_param
assert_compiles '5', %q{
def test(n) = n
test(5)
}
end
def test_setlocal
assert_compiles '3', %q{
def test(n)
m = n
m
end
test(3)
}
end
def test_send_without_block
assert_compiles '[1, 2, 3]', %q{
def foo = 1
def bar(a) = a - 1
def baz(a, b) = a - b
def test1 = foo
def test2 = bar(3)
def test3 = baz(4, 1)
[test1, test2, test3]
}
end
def test_opt_plus_const
assert_compiles '3', %q{
def test = 1 + 2
test # profile opt_plus
test
}, call_threshold: 2
end
def test_opt_plus_fixnum
assert_compiles '3', %q{
def test(a, b) = a + b
test(0, 1) # profile opt_plus
test(1, 2)
}, call_threshold: 2
end
def test_opt_plus_chain
assert_compiles '6', %q{
def test(a, b, c) = a + b + c
test(0, 1, 2) # profile opt_plus
test(1, 2, 3)
}, call_threshold: 2
end
# Test argument ordering
def test_opt_minus
assert_compiles '2', %q{
def test(a, b) = a - b
test(2, 1) # profile opt_minus
test(6, 4)
}, call_threshold: 2
end
def test_opt_mult
assert_compiles '6', %q{
def test(a, b) = a * b
test(1, 2) # profile opt_mult
test(2, 3)
}, call_threshold: 2
end
def test_opt_mult_overflow
omit 'side exits are not implemented yet'
assert_compiles '[6, -6, 9671406556917033397649408, -9671406556917033397649408, 21267647932558653966460912964485513216]', %q{
def test(a, b)
a * b
end
test(1, 1) # profile opt_mult
r1 = test(2, 3)
r2 = test(2, -3)
r3 = test(2 << 40, 2 << 41)
r4 = test(2 << 40, -2 << 41)
r5 = test(1 << 62, 1 << 62)
[r1, r2, r3, r4, r5]
}, call_threshold: 2
end
def test_opt_eq
assert_compiles '[true, false]', %q{
def test(a, b) = a == b
test(0, 2) # profile opt_eq
[test(1, 1), test(0, 1)]
}, call_threshold: 2
end
def test_opt_neq
assert_compiles '[false, true]', %q{
def test(a, b) = a != b
test(0, 2) # profile opt_neq
[test(1, 1), test(0, 1)]
}, call_threshold: 2
end
def test_opt_lt
assert_compiles '[true, false, false]', %q{
def test(a, b) = a < b
test(2, 3) # profile opt_lt
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
end
def test_opt_le
assert_compiles '[true, true, false]', %q{
def test(a, b) = a <= b
test(2, 3) # profile opt_le
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
end
def test_opt_gt
assert_compiles '[false, false, true]', %q{
def test(a, b) = a > b
test(2, 3) # profile opt_gt
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
end
def test_opt_ge
assert_compiles '[false, true, true]', %q{
def test(a, b) = a >= b
test(2, 3) # profile opt_ge
[test(0, 1), test(0, 0), test(1, 0)]
}, call_threshold: 2
end
def test_if
assert_compiles '[0, nil]', %q{
def test(n)
if n < 5
0
end
end
[test(3), test(7)]
}
end
def test_if_else
assert_compiles '[0, 1]', %q{
def test(n)
if n < 5
0
else
1
end
end
[test(3), test(7)]
}
end
def test_if_else_params
assert_compiles '[1, 20]', %q{
def test(n, a, b)
if n < 5
a
else
b
end
end
[test(3, 1, 2), test(7, 10, 20)]
}
end
def test_if_else_nested
assert_compiles '[3, 8, 9, 14]', %q{
def test(a, b, c, d, e)
if 2 < a
if a < 4
b
else
c
end
else
if a < 0
d
else
e
end
end
end
[
test(-1, 1, 2, 3, 4),
test( 0, 5, 6, 7, 8),
test( 3, 9, 10, 11, 12),
test( 5, 13, 14, 15, 16),
]
}
end
def test_if_else_chained
assert_compiles '[12, 11, 21]', %q{
def test(a)
(if 2 < a then 1 else 2 end) + (if a < 4 then 10 else 20 end)
end
[test(0), test(3), test(5)]
}
end
def test_if_elsif_else
assert_compiles '[0, 2, 1]', %q{
def test(n)
if n < 5
0
elsif 8 < n
1
else
2
end
end
[test(3), test(7), test(9)]
}
end
def test_ternary_operator
assert_compiles '[1, 20]', %q{
def test(n, a, b)
n < 5 ? a : b
end
[test(3, 1, 2), test(7, 10, 20)]
}
end
def test_ternary_operator_nested
assert_compiles '[2, 21]', %q{
def test(n, a, b)
(n < 5 ? a : b) + 1
end
[test(3, 1, 2), test(7, 10, 20)]
}
end
def test_while_loop
assert_compiles '10', %q{
def test(n)
i = 0
while i < n
i = i + 1
end
i
end
test(10)
}
end
def test_while_loop_chain
assert_compiles '[135, 270]', %q{
def test(n)
i = 0
while i < n
i = i + 1
end
while i < n * 10
i = i * 3
end
i
end
[test(5), test(10)]
}
end
def test_while_loop_nested
assert_compiles '[0, 4, 12]', %q{
def test(n, m)
i = 0
while i < n
j = 0
while j < m
j += 2
end
i += j
end
i
end
[test(0, 0), test(1, 3), test(10, 5)]
}
end
def test_while_loop_if_else
assert_compiles '[9, -1]', %q{
def test(n)
i = 0
while i < n
if n >= 10
return -1
else
i = i + 1
end
end
i
end
[test(9), test(10)]
}
end
def test_if_while_loop
assert_compiles '[9, 12]', %q{
def test(n)
i = 0
if n < 10
while i < n
i += 1
end
else
while i < n
i += 3
end
end
i
end
[test(9), test(10)]
}
end
private
# Assert that every method call in `test_script` can be compiled by ZJIT
# at a given call_threshold
def assert_compiles(expected, test_script, call_threshold: 1)
pipe_fd = 3
script = <<~RUBY
_test_proc = -> {
RubyVM::ZJIT.assert_compiles
#{test_script}
}
result = _test_proc.call
IO.open(#{pipe_fd}).write(result.inspect)
RUBY
status, out, err, actual = eval_with_jit(script, call_threshold:, pipe_fd:)
message = "exited with status #{status.to_i}"
message << "\nstdout:\n```\n#{out}```\n" unless out.empty?
message << "\nstderr:\n```\n#{err}```\n" unless err.empty?
assert status.success?, message
assert_equal expected, actual
end
# Run a Ruby process with ZJIT options and a pipe for writing test results
def eval_with_jit(script, call_threshold: 1, timeout: 1000, pipe_fd:, debug: true)
args = [
"--disable-gems",
"--zjit-call-threshold=#{call_threshold}",
]
args << "--zjit-debug" if debug
args << "-e" << script_shell_encode(script)
pipe_r, pipe_w = IO.pipe
# Separate thread so we don't deadlock when
# the child ruby blocks writing the output to pipe_fd
pipe_out = nil
pipe_reader = Thread.new do
pipe_out = pipe_r.read
pipe_r.close
end
out, err, status = EnvUtil.invoke_ruby(args, '', true, true, rubybin: RbConfig.ruby, timeout: timeout, ios: { pipe_fd => pipe_w })
pipe_w.close
pipe_reader.join(timeout)
[status, out, err, pipe_out]
ensure
pipe_reader&.kill
pipe_reader&.join(timeout)
pipe_r&.close
pipe_w&.close
end
def script_shell_encode(s)
# We can't pass utf-8-encoded characters directly in a shell arg. But we can use Ruby \u constants.
s.chars.map { |c| c.ascii_only? ? c : "\\u%x" % c.codepoints[0] }.join
end
end
|