Skip to content

Commit aaeaa4d

Browse files
committed
ZJIT: Test additional arg passing scenarios
1 parent 465a86c commit aaeaa4d

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

test/ruby/test_zjit.rb

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ def test_send_kwarg
553553
def test(a:, b:) = [a, b]
554554
def entry = test(b: 2, a: 1) # change order
555555
entry
556-
}
556+
entry
557+
}, call_threshold: 2
557558
end
558559

559560
def test_send_kwarg_optional
@@ -565,6 +566,15 @@ def entry = test
565566
}, call_threshold: 2
566567
end
567568

569+
def test_send_kwarg_optional_too_many
570+
assert_compiles '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]', %q{
571+
def test(a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10) = [a, b, c, d, e, f, g, h, i, j]
572+
def entry = test
573+
entry
574+
entry
575+
}, call_threshold: 2
576+
end
577+
568578
def test_send_kwarg_required_and_optional
569579
assert_compiles '[3, 2]', %q{
570580
def test(a:, b: 2) = [a, b]
@@ -583,6 +593,28 @@ def entry = test(a: 3)
583593
}, call_threshold: 2
584594
end
585595

596+
def test_send_kwarg_to_ccall
597+
assert_compiles '["a", "b", "c"]', %q{
598+
def test(s) = s.each_line(chomp: true).to_a
599+
def entry = test(%(a\nb\nc))
600+
entry
601+
entry
602+
}, call_threshold: 2
603+
end
604+
605+
def test_send_kwarg_and_block_to_ccall
606+
assert_compiles '["a", "b", "c"]', %q{
607+
def test(s)
608+
a = []
609+
s.each_line(chomp: true) { |l| a << l }
610+
a
611+
end
612+
def entry = test(%(a\nb\nc))
613+
entry
614+
entry
615+
}, call_threshold: 2
616+
end
617+
586618
def test_send_kwrest
587619
assert_compiles '{a: 3}', %q{
588620
def test(**kwargs) = kwargs
@@ -592,6 +624,47 @@ def entry = test(a: 3)
592624
}, call_threshold: 2
593625
end
594626

627+
def test_send_req_kwreq
628+
assert_compiles '[1, 3]', %q{
629+
def test(a, c:) = [a, c]
630+
def entry = test(1, c: 3)
631+
entry
632+
entry
633+
}, call_threshold: 2
634+
end
635+
636+
def test_send_req_opt_kwreq_kwopt
637+
assert_compiles '[[1, 2, 3, 4], [-1, -2, -3, -4]]', %q{
638+
def test(a, b = 2, c:, d: 4) = [a, b, c, d]
639+
def entry = [test(1, c: 3), test(-1, -2, d: -4, c: -3)] # specify all, change kw order
640+
entry
641+
entry
642+
}, call_threshold: 2
643+
end
644+
645+
def test_send_unexpected_keyword
646+
assert_compiles ':error', %q{
647+
def test(a: 1) = a*2
648+
def entry
649+
test(z: 2)
650+
rescue ArgumentError
651+
:error
652+
end
653+
654+
entry
655+
entry
656+
}, call_threshold: 2
657+
end
658+
659+
def test_send_all_arg_types
660+
assert_compiles '[:req, :opt, :post, :kwr, :kwo, true]', %q{
661+
def test(a, b = :opt, c, d:, e: :kwo) = [a, b, c, d, e, block_given?]
662+
def entry = test(:req, :post, d: :kwr) {}
663+
entry
664+
entry
665+
}, call_threshold: 2
666+
end
667+
595668
def test_send_ccall_variadic_with_different_receiver_classes
596669
assert_compiles '[true, true]', %q{
597670
def test(obj) = obj.start_with?("a")

zjit/src/hir/opt_tests.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,6 +2862,70 @@ mod hir_opt_tests {
28622862
");
28632863
}
28642864

2865+
#[test]
2866+
fn dont_optimize_ccall_with_kwarg() {
2867+
eval("
2868+
def test = sprintf('%s', a: 1)
2869+
test
2870+
test
2871+
");
2872+
assert_snapshot!(hir_string("test"), @r"
2873+
fn test@<compiled>:2:
2874+
bb0():
2875+
EntryPoint interpreter
2876+
v1:BasicObject = LoadSelf
2877+
Jump bb2(v1)
2878+
bb1(v4:BasicObject):
2879+
EntryPoint JIT(0)
2880+
Jump bb2(v4)
2881+
bb2(v6:BasicObject):
2882+
v11:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
2883+
v12:StringExact = StringCopy v11
2884+
v14:Fixnum[1] = Const Value(1)
2885+
IncrCounter complex_arg_pass_caller_kwarg
2886+
v16:BasicObject = SendWithoutBlock v6, :sprintf, v12, v14
2887+
CheckInterrupts
2888+
Return v16
2889+
");
2890+
}
2891+
2892+
#[test]
2893+
fn dont_optimize_ccall_with_block_and_kwarg() {
2894+
eval("
2895+
def test(s)
2896+
a = []
2897+
s.each_line(chomp: true) { |l| a << l }
2898+
a
2899+
end
2900+
test %(a\nb\nc)
2901+
test %()
2902+
");
2903+
assert_snapshot!(hir_string("test"), @r"
2904+
fn test@<compiled>:3:
2905+
bb0():
2906+
EntryPoint interpreter
2907+
v1:BasicObject = LoadSelf
2908+
v2:BasicObject = GetLocal l0, SP@5
2909+
v3:NilClass = Const Value(nil)
2910+
Jump bb2(v1, v2, v3)
2911+
bb1(v6:BasicObject, v7:BasicObject):
2912+
EntryPoint JIT(0)
2913+
v8:NilClass = Const Value(nil)
2914+
Jump bb2(v6, v7, v8)
2915+
bb2(v10:BasicObject, v11:BasicObject, v12:NilClass):
2916+
v16:ArrayExact = NewArray
2917+
SetLocal l0, EP@3, v16
2918+
v22:TrueClass = Const Value(true)
2919+
IncrCounter complex_arg_pass_caller_kwarg
2920+
v24:BasicObject = Send v11, 0x1000, :each_line, v22
2921+
v25:BasicObject = GetLocal l0, EP@4
2922+
v26:BasicObject = GetLocal l0, EP@3
2923+
v30:BasicObject = GetLocal l0, EP@3
2924+
CheckInterrupts
2925+
Return v30
2926+
");
2927+
}
2928+
28652929
#[test]
28662930
fn dont_replace_get_constant_path_with_empty_ic() {
28672931
eval("

0 commit comments

Comments
 (0)