[前][次][番号順一覧][スレッド一覧]

ruby-changes:72075

From: Noah <ko1@a...>
Date: Tue, 7 Jun 2022 02:47:44 +0900 (JST)
Subject: [ruby-changes:72075] 653e517eef (master): Use bindgen to import Ruby constants wherever possible. (#5943)

https://git.ruby-lang.org/ruby.git/commit/?id=653e517eef

From 653e517eefaa0c4f2710b30b4dff9a9dad7b9d6a Mon Sep 17 00:00:00 2001
From: Noah Gibbs <noah.gibbs@s...>
Date: Mon, 6 Jun 2022 18:47:24 +0100
Subject: Use bindgen to import Ruby constants wherever possible. (#5943)

Constants that can't be imported via bindgen should have
a comment saying why not.
---
 yjit/bindgen/src/main.rs       | 19 ++++++++++++
 yjit/src/codegen.rs            | 12 ++++----
 yjit/src/cruby.rs              | 65 ++++++++----------------------------------
 yjit/src/cruby_bindings.inc.rs | 55 +++++++++++++++++++++++++++++++++++
 yjit/src/invariants.rs         |  2 +-
 5 files changed, 93 insertions(+), 60 deletions(-)

diff --git a/yjit/bindgen/src/main.rs b/yjit/bindgen/src/main.rs
index 26717d018c..6ccdbc0cc5 100644
--- a/yjit/bindgen/src/main.rs
+++ b/yjit/bindgen/src/main.rs
@@ -125,6 +125,21 @@ fn main() { https://github.com/ruby/ruby/blob/trunk/yjit/bindgen/src/main.rs#L125
         .allowlist_var("rb_cArray")
         .allowlist_var("rb_cHash")
 
+        // From include/ruby/internal/fl_type.h
+        .allowlist_type("ruby_fl_type")
+        .allowlist_type("ruby_fl_ushift")
+
+        // From include/ruby/internal/core/robject.h
+        .allowlist_type("ruby_robject_flags")
+        .allowlist_type("ruby_robject_consts")
+
+        // From include/ruby/internal/core/rarray.h
+        .allowlist_type("ruby_rarray_flags")
+        .allowlist_type("ruby_rarray_consts")
+
+        // From include/ruby/internal/core/rclass.h
+        .allowlist_type("ruby_rmodule_flags")
+
         // From ruby/internal/globals.h
         .allowlist_var("rb_mKernel")
 
@@ -214,6 +229,10 @@ fn main() { https://github.com/ruby/ruby/blob/trunk/yjit/bindgen/src/main.rs#L229
         .blocklist_type("rb_control_frame_struct")
         .opaque_type("rb_control_frame_struct")
         .allowlist_function("rb_vm_bh_to_procval")
+        .allowlist_type("vm_special_object_type")
+        .allowlist_var("VM_ENV_DATA_INDEX_SPECVAL")
+        .allowlist_var("VM_ENV_DATA_INDEX_FLAGS")
+        .allowlist_var("VM_ENV_DATA_SIZE")
 
         // From yjit.c
         .allowlist_function("rb_iseq_(get|set)_yjit_payload")
diff --git a/yjit/src/codegen.rs b/yjit/src/codegen.rs
index 87639b9d88..b15d87cac6 100644
--- a/yjit/src/codegen.rs
+++ b/yjit/src/codegen.rs
@@ -1038,7 +1038,7 @@ fn gen_putspecialobject( https://github.com/ruby/ruby/blob/trunk/yjit/src/codegen.rs#L1038
 ) -> CodegenStatus {
     let object_type = jit_get_arg(jit, 0);
 
-    if object_type == VALUE(VM_SPECIAL_OBJECT_VMCORE) {
+    if object_type == VALUE(VM_SPECIAL_OBJECT_VMCORE.as_usize()) {
         let stack_top: X86Opnd = ctx.stack_push(Type::UnknownHeap);
         jit_mov_gc_ptr(jit, cb, REG0, unsafe { rb_mRubyVMFrozenCore });
         mov(cb, stack_top, REG0);
@@ -1956,8 +1956,8 @@ fn gen_get_ivar( https://github.com/ruby/ruby/blob/trunk/yjit/src/codegen.rs#L1956
     }
 
     // Compile time self is embedded and the ivar index lands within the object
-    let test_result = unsafe { FL_TEST_RAW(comptime_receiver, VALUE(ROBJECT_EMBED)) != VALUE(0) };
-    if test_result && ivar_index < ROBJECT_EMBED_LEN_MAX {
+    let test_result = unsafe { FL_TEST_RAW(comptime_receiver, VALUE(ROBJECT_EMBED.as_usize())) != VALUE(0) };
+    if test_result && ivar_index < (ROBJECT_EMBED_LEN_MAX.as_usize()) {
         // See ROBJECT_IVPTR() from include/ruby/internal/core/robject.h
 
         // Guard that self is embedded
@@ -2009,7 +2009,7 @@ fn gen_get_ivar( https://github.com/ruby/ruby/blob/trunk/yjit/src/codegen.rs#L2009
         );
 
         // Check that the extended table is big enough
-        if ivar_index > ROBJECT_EMBED_LEN_MAX {
+        if ivar_index > (ROBJECT_EMBED_LEN_MAX.as_usize()) {
             // Check that the slot is inside the extended table (num_slots > index)
             let num_slots = mem_opnd(32, REG0, RUBY_OFFSET_ROBJECT_AS_HEAP_NUMIV);
 
@@ -3422,7 +3422,7 @@ fn jit_guard_known_klass( https://github.com/ruby/ruby/blob/trunk/yjit/src/codegen.rs#L3422
             ctx.upgrade_opnd_type(insn_opnd, Type::Flonum);
         }
     } else if unsafe {
-        FL_TEST(known_klass, VALUE(RUBY_FL_SINGLETON)) != VALUE(0)
+        FL_TEST(known_klass, VALUE(RUBY_FL_SINGLETON as usize)) != VALUE(0)
             && sample_instance == rb_attr_get(known_klass, id__attached__ as ID)
     } {
         // Singleton classes are attached to one specific object, so we can
@@ -5012,7 +5012,7 @@ fn gen_invokesuper( https://github.com/ruby/ruby/blob/trunk/yjit/src/codegen.rs#L5012
     // vm_search_normal_superclass
     let rbasic_ptr: *const RBasic = current_defined_class.as_ptr();
     if current_defined_class.builtin_type() == RUBY_T_ICLASS
-        && unsafe { RB_TYPE_P((*rbasic_ptr).klass, RUBY_T_MODULE) && FL_TEST_RAW((*rbasic_ptr).klass, VALUE(RMODULE_IS_REFINEMENT)) != VALUE(0) }
+        && unsafe { RB_TYPE_P((*rbasic_ptr).klass, RUBY_T_MODULE) && FL_TEST_RAW((*rbasic_ptr).klass, VALUE(RMODULE_IS_REFINEMENT.as_usize())) != VALUE(0) }
     {
         return CantCompile;
     }
diff --git a/yjit/src/cruby.rs b/yjit/src/cruby.rs
index e86e451643..3a172391f6 100644
--- a/yjit/src/cruby.rs
+++ b/yjit/src/cruby.rs
@@ -701,7 +701,8 @@ pub const Qundef: VALUE = VALUE(52); https://github.com/ruby/ruby/blob/trunk/yjit/src/cruby.rs#L701
 mod manual_defs {
     use super::*;
 
-    pub const RUBY_SYMBOL_FLAG: usize = 0x0c;
+    pub const SIZEOF_VALUE: usize = 8;
+    pub const SIZEOF_VALUE_I32: i32 = SIZEOF_VALUE as i32;
 
     pub const RUBY_LONG_MIN: isize = std::os::raw::c_long::MIN as isize;
     pub const RUBY_LONG_MAX: isize = std::os::raw::c_long::MAX as isize;
@@ -710,20 +711,16 @@ mod manual_defs { https://github.com/ruby/ruby/blob/trunk/yjit/src/cruby.rs#L711
     pub const RUBY_FIXNUM_MAX: isize = RUBY_LONG_MAX / 2;
     pub const RUBY_FIXNUM_FLAG: usize = 0x1;
 
+    // All these are defined in include/ruby/internal/special_consts.h,
+    // in the same enum as RUBY_Qfalse, etc.
+    // Do we want to switch to using Ruby's definition of Qnil, Qfalse, etc?
+    pub const RUBY_SYMBOL_FLAG: usize = 0x0c;
     pub const RUBY_FLONUM_FLAG: usize = 0x2;
     pub const RUBY_FLONUM_MASK: usize = 0x3;
-
-    pub const RUBY_IMMEDIATE_MASK: usize = 0x7;
-
     pub const RUBY_SPECIAL_SHIFT: usize = 8;
+    pub const RUBY_IMMEDIATE_MASK: usize = 0x7;
 
-    // Constants from vm_core.h
-    pub const VM_SPECIAL_OBJECT_VMCORE: usize = 0x1;
-    pub const VM_ENV_DATA_INDEX_SPECVAL: isize = -1;
-    pub const VM_ENV_DATA_INDEX_FLAGS: isize = 0;
-    pub const VM_ENV_DATA_SIZE: usize = 3;
-
-    // From vm_callinfo.h
+    // From vm_callinfo.h - uses calculation that seems to confuse bindgen
     pub const VM_CALL_ARGS_SPLAT: u32 = 1 << VM_CALL_ARGS_SPLAT_bit;
     pub const VM_CALL_ARGS_BLOCKARG: u32 = 1 << VM_CALL_ARGS_BLOCKARG_bit;
     pub const VM_CALL_FCALL: u32 = 1 << VM_CALL_FCALL_bit;
@@ -731,49 +728,11 @@ mod manual_defs { https://github.com/ruby/ruby/blob/trunk/yjit/src/cruby.rs#L728
     pub const VM_CALL_KW_SPLAT: u32 = 1 << VM_CALL_KW_SPLAT_bit;
     pub const VM_CALL_TAILCALL: u32 = 1 << VM_CALL_TAILCALL_bit;
 
-    pub const SIZEOF_VALUE: usize = 8;
-    pub const SIZEOF_VALUE_I32: i32 = SIZEOF_VALUE as i32;
+    // From internal/struct.h - in anonymous enum, so we can't easily import it
+    pub const RSTRUCT_EMBED_LEN_MASK: usize = (RUBY_FL_USER2 | RUBY_FL_USER1) as usize;
 
-    pub const RUBY_FL_SINGLETON: usize = RUBY_FL_USER_0;
-
-    pub const ROBJECT_EMBED: usize = RUBY_FL_USER_1;
-    pub const ROBJECT_EMBED_LEN_MAX: usize = 3; // This is a complex calculation in ruby/internal/core/robject.h
-
-    pub const RMODULE_IS_REFINEMENT: usize = RUBY_FL_USER_3;
-
-    // Constants from include/ruby/internal/fl_type.h
-    pub const RUBY_FL_USHIFT: usize = 12;
-    pub const RUBY_FL_USER_0: usize = 1 << (RUBY_FL_USHIFT + 0);
-    pub const RUBY_FL_USER_1: usize = 1 << (RUBY_FL_USHIFT + 1);
-    pub const RUBY_FL_USER_2: usize = 1 << (RUBY_FL_USHIFT + 2);
-    pub const RUBY_FL_USER_3: usize = 1 << (RUBY_FL_USHIFT + 3);
-    pub const RUBY_FL_USER_4: usize = 1 << (RUBY_FL_USHIFT + 4);
-    pub const RUBY_FL_USER_5: usize = 1 << (RUBY_FL_USHIFT + 5);
-    pub const RUBY_FL_USER_6: usize = 1 << (RUBY_FL_USHIFT + 6);
-    pub const RUBY_FL_USER_7: usize = 1 << (RUBY_FL_USHIFT + 7);
-    pub const RUBY_FL_USER_8: usize = 1 << (RUBY_FL_USHIFT + 8);
-    pub const RUBY_FL_USER_9: usize = 1 << (RUBY_FL_USHIFT + 9);
-    pub const RUBY_FL_USER_10: usize = 1 << (RUBY_FL_USHIFT + 10);
-    pub const RUBY_FL_USER_11: usize = 1 << (RUBY_FL_USHIFT + 11);
-    pub const RUBY_FL_USER_12: usize = 1 << (RUBY_FL_USHIFT + 12);
-    pub const RUBY_FL_USER_13: usize = 1 << (RUBY_FL_USHIFT + 13);
-    pub const RUBY_FL_USER_14: usize = 1 << (RUBY_FL_USHIFT + 14);
-    pub const RUBY_FL_USER_15: usize = 1 << (RUBY_FL_USHIFT + 15);
-    pub const RUBY_FL_USER_16: usize = 1 << (RUBY_FL_USHIFT + 16);
-    pub const RUBY_FL_USER_17: usize = 1 << (RUBY_FL_USHIFT + 17);
-    pub const RUBY_FL_USER_18: usize = 1 << (RUBY_FL_USHIFT + 18);
-    pub const RUBY_FL_USER_19: usize = 1 << (RUBY_FL_USHIFT + 19);
-
-    // Constants from include/ruby/internal/core/rarray.h
-    pub const RARRAY_EMBED_FLAG: usize = RUBY_FL_USER_1;
-    pub const RARRAY_EMBED_LEN_SHIFT: usize = RUBY_FL_USHIFT + 3;
-    pub const RARRAY_EMBED_LEN_MASK: usize = RUBY_FL_USER_3 | RUBY_FL_USER_4;
-
-    // From internal/struct.h
-    pub const RSTRUCT_EMBED_LEN_MASK: usize = RUBY_FL_USER_2 | RUBY_FL_USER_1;
-
-    // From iseq.h
-    pub const ISEQ_TRANSLATED: usize = RUBY_FL_USER_7;
+    // From iseq.h - via a different constant, which seems to confuse bindgen
+    pub const ISEQ_TRANSLATED: usize = RUBY_FL_USER7 as usize;
 
     // We'll need to encode a lot of Ruby struct/field offsets as constants unless we want to
     // redeclare all the Ruby C structs and write our own offsetof macro. For now, we use constants.
diff --git a/yjit/src/cruby_bindings.inc.rs b/yjit/src/cruby_bindings.inc.rs
index d686 (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]