From f75e1cb362f57aa9e18c42026adec39b86d4e5c6 Mon Sep 17 00:00:00 2001 From: Aiden Fox Ivey Date: Thu, 11 Sep 2025 23:32:06 -0400 Subject: [PATCH] ZJIT: Move jit.rs to ruby.rs and create a shared crate `jit` * ruby.rs should hold the main entrypoint to YJIT and ZJIT * The crate jit will hold code shared between them --- Cargo.lock | 16 +++++++++++----- Cargo.toml | 6 +++--- configure.ac | 4 ++-- defs/jit.mk | 2 +- jit/Cargo.toml | 6 ++++++ jit/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ jit.rs => ruby.rs | 0 yjit/Cargo.toml | 1 + zjit/Cargo.toml | 1 + 9 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 jit/Cargo.toml create mode 100644 jit/src/lib.rs rename jit.rs => ruby.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 6312cb46a9..9a4b2ebbba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,11 +62,7 @@ dependencies = [ [[package]] name = "jit" -version = "0.0.0" -dependencies = [ - "yjit", - "zjit", -] +version = "0.1.0" [[package]] name = "lazy_static" @@ -86,6 +82,14 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "ruby" +version = "0.0.0" +dependencies = [ + "yjit", + "zjit", +] + [[package]] name = "shlex" version = "1.3.0" @@ -176,6 +180,7 @@ name = "yjit" version = "0.1.0" dependencies = [ "capstone", + "jit", ] [[package]] @@ -184,4 +189,5 @@ version = "0.0.1" dependencies = [ "capstone", "insta", + "jit", ] diff --git a/Cargo.toml b/Cargo.toml index 3f373fdace..ec2ce880ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,10 +3,10 @@ # TODO(alan) notes about rust version requirements. Undecided yet. [workspace] -members = ["zjit", "yjit"] +members = ["zjit", "yjit", "jit"] [package] -name = "jit" +name = "ruby" version = "0.0.0" edition = "2024" rust-version = "1.85.0" @@ -18,7 +18,7 @@ zjit = { path = "zjit", optional = true } [lib] crate-type = ["staticlib"] -path = "jit.rs" +path = "ruby.rs" [features] disasm = ["yjit?/disasm", "zjit?/disasm"] diff --git a/configure.ac b/configure.ac index 47f8b79b03..8cb30429dc 100644 --- a/configure.ac +++ b/configure.ac @@ -4019,9 +4019,9 @@ AS_IF([test x"$JIT_CARGO_SUPPORT" != "xno" -o \( x"$YJIT_SUPPORT" != "xno" -a x" ]) CARGO_BUILD_ARGS="--profile ${JIT_CARGO_SUPPORT} --features ${rb_cargo_features}" AS_IF([test "${JIT_CARGO_SUPPORT}" = "dev"], [ - RUST_LIB="target/debug/libjit.a" + RUST_LIB="target/debug/libruby.a" ], [ - RUST_LIB="target/${JIT_CARGO_SUPPORT}/libjit.a" + RUST_LIB="target/${JIT_CARGO_SUPPORT}/libruby.a" ]) ]) diff --git a/defs/jit.mk b/defs/jit.mk index 28d8f2da3a..a537d80300 100644 --- a/defs/jit.mk +++ b/defs/jit.mk @@ -17,7 +17,7 @@ CARGO_VERBOSE = $(CARGO_VERBOSE_$(V)) # ld: warning: object file (target/debug/libjit.a()) was built for # newer macOS version (15.2) than being linked (15.0) # This limits us to an older set of macOS API in the rust code, but we don't use any. -$(RUST_LIB): $(srcdir)/jit.rs +$(RUST_LIB): $(srcdir)/ruby.rs $(Q)if [ '$(ZJIT_SUPPORT)' != no -a '$(YJIT_SUPPORT)' != no ]; then \ echo 'building YJIT and ZJIT ($(JIT_CARGO_SUPPORT:yes=release) mode)'; \ elif [ '$(ZJIT_SUPPORT)' != no ]; then \ diff --git a/jit/Cargo.toml b/jit/Cargo.toml new file mode 100644 index 0000000000..530fe3674b --- /dev/null +++ b/jit/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "jit" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/jit/src/lib.rs b/jit/src/lib.rs new file mode 100644 index 0000000000..6079d00f2f --- /dev/null +++ b/jit/src/lib.rs @@ -0,0 +1,37 @@ +//! Shared code between YJIT and ZJIT. + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::alloc::{GlobalAlloc, Layout, System}; + +#[global_allocator] +pub static GLOBAL_ALLOCATOR: StatsAlloc = StatsAlloc { alloc_size: AtomicUsize::new(0) }; + +pub struct StatsAlloc { + pub alloc_size: AtomicUsize, +} + +unsafe impl GlobalAlloc for StatsAlloc { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + self.alloc_size.fetch_add(layout.size(), Ordering::SeqCst); + unsafe { System.alloc(layout) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + self.alloc_size.fetch_sub(layout.size(), Ordering::SeqCst); + unsafe { System.dealloc(ptr, layout) } + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + self.alloc_size.fetch_add(layout.size(), Ordering::SeqCst); + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + if new_size > layout.size() { + self.alloc_size.fetch_add(new_size - layout.size(), Ordering::SeqCst); + } else if new_size < layout.size() { + self.alloc_size.fetch_sub(layout.size() - new_size, Ordering::SeqCst); + } + unsafe { System.realloc(ptr, layout, new_size) } + } +} diff --git a/jit.rs b/ruby.rs similarity index 100% rename from jit.rs rename to ruby.rs diff --git a/yjit/Cargo.toml b/yjit/Cargo.toml index ad7dd35ecf..af9c18c0dc 100644 --- a/yjit/Cargo.toml +++ b/yjit/Cargo.toml @@ -13,6 +13,7 @@ publish = false # Don't publish to crates.io # No required dependencies to simplify build process. TODO: Link to yet to be # written rationale. Optional For development and testing purposes capstone = { version = "0.13.0", optional = true } +jit = { version = "0.1.0", path = "../jit" } # NOTE: Development builds select a set of these via configure.ac # For debugging, `make V=1` shows exact cargo invocation. diff --git a/zjit/Cargo.toml b/zjit/Cargo.toml index 7334d465c2..617cd11916 100644 --- a/zjit/Cargo.toml +++ b/zjit/Cargo.toml @@ -9,6 +9,7 @@ publish = false # Don't publish to crates.io # No required dependencies to simplify build process. TODO: Link to yet to be # written rationale. Optional For development and testing purposes capstone = { version = "0.13.0", optional = true } +jit = { version = "0.1.0", path = "../jit" } [dev-dependencies] insta = "1.43.1"