Skip to content

Commit dc1b55a

Browse files
authored
ZJIT: Add new ZJIT types for Set (#13743)
1 parent 44e4b02 commit dc1b55a

File tree

7 files changed

+66
-13
lines changed

7 files changed

+66
-13
lines changed

zjit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include <errno.h>
3434

35+
RUBY_EXTERN VALUE rb_cSet; // defined in set.c and it's not exposed yet
36+
3537
uint32_t
3638
rb_zjit_get_page_size(void)
3739
{

zjit/bindgen/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ fn main() {
186186
.allowlist_var("rb_cThread")
187187
.allowlist_var("rb_cArray")
188188
.allowlist_var("rb_cHash")
189+
.allowlist_var("rb_cSet")
189190
.allowlist_var("rb_cClass")
190191
.allowlist_var("rb_cISeq")
191192

zjit/src/cruby_bindings.inc.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zjit/src/hir.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6235,4 +6235,24 @@ mod opt_tests {
62356235
Return v11
62366236
"#]]);
62376237
}
6238+
6239+
#[test]
6240+
fn test_set_type_from_constant() {
6241+
eval("
6242+
MY_SET = Set.new
6243+
6244+
def test = MY_SET
6245+
6246+
test
6247+
test
6248+
");
6249+
assert_optimized_method_hir("test", expect![[r#"
6250+
fn test:
6251+
bb0(v0:BasicObject):
6252+
PatchPoint SingleRactorMode
6253+
PatchPoint StableConstantNames(0x1000, MY_SET)
6254+
v7:SetExact[VALUE(0x1008)] = Const Value(VALUE(0x1008))
6255+
Return v7
6256+
"#]]);
6257+
}
62386258
}

zjit/src/hir_type/gen_hir_type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def base_type name
7272
base_type "Array"
7373
base_type "Hash"
7474
base_type "Range"
75+
base_type "Set"
7576

7677
(integer, integer_exact) = base_type "Integer"
7778
# CRuby partitions Integer into immediate and non-immediate variants.

zjit/src/hir_type/hir_type.inc.rs

Lines changed: 21 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zjit/src/hir_type/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(non_upper_case_globals)]
22
use crate::cruby::{Qfalse, Qnil, Qtrue, VALUE, RUBY_T_ARRAY, RUBY_T_STRING, RUBY_T_HASH};
3-
use crate::cruby::{rb_cInteger, rb_cFloat, rb_cArray, rb_cHash, rb_cString, rb_cSymbol, rb_cObject, rb_cTrueClass, rb_cFalseClass, rb_cNilClass, rb_cRange};
3+
use crate::cruby::{rb_cInteger, rb_cFloat, rb_cArray, rb_cHash, rb_cString, rb_cSymbol, rb_cObject, rb_cTrueClass, rb_cFalseClass, rb_cNilClass, rb_cRange, rb_cSet};
44
use crate::cruby::ClassRelationship;
55
use crate::cruby::get_class_name;
66
use crate::cruby::rb_mRubyVMFrozenCore;
@@ -195,6 +195,9 @@ impl Type {
195195
else if is_string_exact(val) {
196196
Type { bits: bits::StringExact, spec: Specialization::Object(val) }
197197
}
198+
else if val.class_of() == unsafe { rb_cSet } {
199+
Type { bits: bits::SetExact, spec: Specialization::Object(val) }
200+
}
198201
else if val.class_of() == unsafe { rb_cObject } {
199202
Type { bits: bits::ObjectExact, spec: Specialization::Object(val) }
200203
}
@@ -394,6 +397,7 @@ impl Type {
394397
if self.is_subtype(types::NilClassExact) { return Some(unsafe { rb_cNilClass }); }
395398
if self.is_subtype(types::ObjectExact) { return Some(unsafe { rb_cObject }); }
396399
if self.is_subtype(types::RangeExact) { return Some(unsafe { rb_cRange }); }
400+
if self.is_subtype(types::SetExact) { return Some(unsafe { rb_cSet }); }
397401
if self.is_subtype(types::StringExact) { return Some(unsafe { rb_cString }); }
398402
if self.is_subtype(types::SymbolExact) { return Some(unsafe { rb_cSymbol }); }
399403
if self.is_subtype(types::TrueClassExact) { return Some(unsafe { rb_cTrueClass }); }
@@ -585,6 +589,21 @@ mod tests {
585589
assert_eq!(types::Integer.inexact_ruby_class(), None);
586590
}
587591

592+
#[test]
593+
fn set() {
594+
assert_subtype(types::SetExact, types::Set);
595+
assert_subtype(types::SetSubclass, types::Set);
596+
}
597+
598+
#[test]
599+
fn set_has_ruby_class() {
600+
crate::cruby::with_rubyvm(|| {
601+
assert_eq!(types::SetExact.runtime_exact_ruby_class(), Some(unsafe { rb_cSet }));
602+
assert_eq!(types::Set.runtime_exact_ruby_class(), None);
603+
assert_eq!(types::SetSubclass.runtime_exact_ruby_class(), None);
604+
});
605+
}
606+
588607
#[test]
589608
fn display_exact_bits_match() {
590609
assert_eq!(format!("{}", Type::fixnum(4)), "Fixnum[4]");

0 commit comments

Comments
 (0)