Skip to content

Commit ce6c859

Browse files
committed
ZJIT: Use nil? optimization to test guard generation against different types
1 parent 2a4f47b commit ce6c859

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

zjit/src/hir.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6661,4 +6661,124 @@ mod opt_tests {
66616661
Return v8
66626662
"#]]);
66636663
}
6664+
6665+
#[test]
6666+
fn test_guard_false_for_nil_opt() {
6667+
eval("
6668+
def test(val)
6669+
val.nil?
6670+
end
6671+
6672+
test(false)
6673+
test(false)
6674+
");
6675+
assert_optimized_method_hir("test", expect![[r#"
6676+
fn test:
6677+
bb0(v0:BasicObject, v1:BasicObject):
6678+
PatchPoint MethodRedefined(FalseClass@0x1000, nil?@0x1008)
6679+
v7:FalseClassExact = GuardType v1, FalseClassExact
6680+
v8:FalseClassExact = CCall nil?@0x1010, v7
6681+
Return v8
6682+
"#]]);
6683+
}
6684+
6685+
#[test]
6686+
fn test_guard_true_for_nil_opt() {
6687+
eval("
6688+
def test(val)
6689+
val.nil?
6690+
end
6691+
6692+
test(true)
6693+
test(true)
6694+
");
6695+
assert_optimized_method_hir("test", expect![[r#"
6696+
fn test:
6697+
bb0(v0:BasicObject, v1:BasicObject):
6698+
PatchPoint MethodRedefined(TrueClass@0x1000, nil?@0x1008)
6699+
v7:TrueClassExact = GuardType v1, TrueClassExact
6700+
v8:FalseClassExact = CCall nil?@0x1010, v7
6701+
Return v8
6702+
"#]]);
6703+
}
6704+
6705+
#[test]
6706+
fn test_guard_symbol_for_nil_opt() {
6707+
eval("
6708+
def test(val)
6709+
val.nil?
6710+
end
6711+
6712+
test(:foo)
6713+
test(:bar)
6714+
");
6715+
assert_optimized_method_hir("test", expect![[r#"
6716+
fn test:
6717+
bb0(v0:BasicObject, v1:BasicObject):
6718+
PatchPoint MethodRedefined(Symbol@0x1000, nil?@0x1008)
6719+
v7:StaticSymbol = GuardType v1, StaticSymbol
6720+
v8:FalseClassExact = CCall nil?@0x1010, v7
6721+
Return v8
6722+
"#]]);
6723+
}
6724+
6725+
#[test]
6726+
fn test_guard_fixnum_for_nil_opt() {
6727+
eval("
6728+
def test(val)
6729+
val.nil?
6730+
end
6731+
6732+
test(1)
6733+
test(2)
6734+
");
6735+
assert_optimized_method_hir("test", expect![[r#"
6736+
fn test:
6737+
bb0(v0:BasicObject, v1:BasicObject):
6738+
PatchPoint MethodRedefined(Integer@0x1000, nil?@0x1008)
6739+
v7:Fixnum = GuardType v1, Fixnum
6740+
v8:FalseClassExact = CCall nil?@0x1010, v7
6741+
Return v8
6742+
"#]]);
6743+
}
6744+
6745+
#[test]
6746+
fn test_guard_float_for_nil_opt() {
6747+
eval("
6748+
def test(val)
6749+
val.nil?
6750+
end
6751+
6752+
test(1.0)
6753+
test(2.0)
6754+
");
6755+
assert_optimized_method_hir("test", expect![[r#"
6756+
fn test:
6757+
bb0(v0:BasicObject, v1:BasicObject):
6758+
PatchPoint MethodRedefined(Float@0x1000, nil?@0x1008)
6759+
v7:Flonum = GuardType v1, Flonum
6760+
v8:FalseClassExact = CCall nil?@0x1010, v7
6761+
Return v8
6762+
"#]]);
6763+
}
6764+
6765+
#[test]
6766+
fn test_guard_string_for_nil_opt() {
6767+
eval("
6768+
def test(val)
6769+
val.nil?
6770+
end
6771+
6772+
test('foo')
6773+
test('bar')
6774+
");
6775+
assert_optimized_method_hir("test", expect![[r#"
6776+
fn test:
6777+
bb0(v0:BasicObject, v1:BasicObject):
6778+
PatchPoint MethodRedefined(String@0x1000, nil?@0x1008)
6779+
v7:StringExact = GuardType v1, StringExact
6780+
v8:FalseClassExact = CCall nil?@0x1010, v7
6781+
Return v8
6782+
"#]]);
6783+
}
66646784
}

0 commit comments

Comments
 (0)