summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Wu <[email protected]>2025-05-07 00:19:36 +0900
committerAlan Wu <[email protected]>2025-05-15 00:39:03 +0900
commit92b218fbc379fe85792eb060b71520e271971335 ()
treed5fbb4b7dd49d3388d9cff5fbdccc5effa7e20d3
parentb5575a80bc3849651c395d0ae470eb41dc3aa897 (diff)
YJIT: ZJIT: Allow both JITs in the same build
This commit allows building YJIT and ZJIT simultaneously, a "combo build". Previously, `./configure --enable-yjit --enable-zjit` failed. At runtime, though, only one of the two can be enabled at a time. Add a root Cargo workspace that contains both the yjit and zjit crate. The common Rust build integration mechanisms are factored out into defs/jit.mk. Combo YJIT+ZJIT s are supported; if either JIT uses `--enable-*=dev`, both of them are built in dev mode. The combo build requires Cargo, but building one JIT at a time with only rustc in release build remains supported.
Notes: Merged: https://.com/ruby/ruby/pull/13262
-rw-r--r--./auto_request_review.yml1
-rw-r--r--.gitignore3
-rw-r--r--Cargo.lock89
-rw-r--r--Cargo.toml51
-rw-r--r--common.mk13
-rw-r--r--configure.ac97
-rw-r--r--defs/gmake.mk1
-rw-r--r--defs/jit.mk53
-rw-r--r--gc/mmtk/Cargo.toml2
-rw-r--r--jit.rs4
-rw-r--r--template/Makefile.in9
-rw-r--r--vm.c4
-rw-r--r--yjit/Cargo.toml27
-rw-r--r--yjit/bindgen/Cargo.toml2
-rw-r--r--yjit/yjit.mk57
-rw-r--r--zjit.c1
-rw-r--r--zjit/Cargo.toml17
-rw-r--r--zjit/bindgen/Cargo.toml2
-rw-r--r--zjit/build.rs3
-rw-r--r--zjit/zjit.mk58
20 files changed, 297 insertions, 197 deletions
@@ -10,6 +10,7 @@ files:
'zjit/src/cruby_bindings.inc.rs': []
'doc/zjit*': [team:jit]
'test/ruby/test_zjit*': [team:jit]
options:
ignore_draft: true
# This currently doesn't work as intended. We want to skip reviews when only
@@ -246,6 +246,9 @@ lcov*.info
/yjit-bench
/yjit_exit_locations.dump
# /wasm/
/wasm/tests/*.wasm
@@ -0,0 +1,89 @@
@@ -0,0 +1,51 @@
@@ -187,10 +187,9 @@ COMMONOBJS = array.$(OBJEXT) \
weakmap.$(OBJEXT) \
$(PRISM_FILES) \
$(YJIT_OBJ) \
- $(YJIT_LIBOBJ) \
$(ZJIT_OBJ) \
- $(ZJIT_LIBOBJ) \
$(JIT_OBJ) \
$(COROUTINE_OBJ) \
$(DTRACE_OBJ) \
$(BUILTIN_ENCOBJS) \
@@ -346,7 +345,7 @@ YJIT_RUSTC_ARGS = --crate-name=yjit \
-C opt-level=3 \
-C overflow-checks=on \
'--out-dir=$(CARGO_TARGET_DIR)/release/' \
- $(top_srcdir)/yjit/src/lib.rs
ZJIT_RUSTC_ARGS = --crate-name=zjit \
--crate-type=staticlib \
@@ -355,8 +354,8 @@ ZJIT_RUSTC_ARGS = --crate-name=zjit \
-C lto=thin \
-C opt-level=3 \
-C overflow-checks=on \
- '--out-dir=$(ZJIT_CARGO_TARGET_DIR)/release/' \
- $(top_srcdir)/zjit/src/lib.rs
all: $(SHOWFLAGS) main
@@ -736,8 +735,8 @@ clean-local:: clean-runnable
$(Q)$(RM) probes.h probes.$(OBJEXT) probes.stamp ruby-glommed.$(OBJEXT) ruby.imp ChangeLog $(STATIC_RUBY)$(EXEEXT)
$(Q)$(RM) GNUmakefile.old Makefile.old $(arch)-fake.rb bisect.sh $(ENC_TRANS_D) builtin_binary.inc
$(Q)$(RM) $(PRISM_BUILD_DIR)/.time $(PRISM_BUILD_DIR)/*/.time yjit_exit_locations.dump
- -$(Q)$(RMALL) yjit/target
- -$(Q) $(RMDIR) enc/jis enc/trans enc $(COROUTINE_H:/Context.h=) coroutine yjit \
$(PRISM_BUILD_DIR)/*/ $(PRISM_BUILD_DIR) tmp \
2> $(NULL) || $(NULLCMD)
@@ -3924,46 +3924,33 @@ AC_ARG_ENABLE(yjit,
CARGO=
CARGO_BUILD_ARGS=
YJIT_LIBS=
AS_CASE(["${YJIT_SUPPORT}"],
[yes|dev|stats|dev_nodebug], [
AS_IF([test x"$RUSTC" = "xno"],
AC_MSG_ERROR([rustc is required. Installation instructions available at https://www.rust-lang.org/tools/install])
)
- AS_IF([test x"$ZJIT_SUPPORT" != "xno"],
- AC_MSG_ERROR([YJIT cannot be enabled when ZJIT is enabled])
- )
AS_CASE(["${YJIT_SUPPORT}"],
[yes], [
- rb_rust_target_subdir=release
],
[dev], [
- rb_rust_target_subdir=debug
- CARGO_BUILD_ARGS='--features disasm,runtime_checks'
AC_DEFINE(RUBY_DEBUG, 1)
],
[dev_nodebug], [
- rb_rust_target_subdir=dev_nodebug
- CARGO_BUILD_ARGS='--profile dev_nodebug --features disasm'
AC_DEFINE(YJIT_STATS, 1)
],
[stats], [
- rb_rust_target_subdir=stats
- CARGO_BUILD_ARGS='--profile stats'
AC_DEFINE(YJIT_STATS, 1)
])
- AS_IF([test -n "${CARGO_BUILD_ARGS}"], [
- AC_CHECK_TOOL(CARGO, [cargo], [no])
- AS_IF([test x"$CARGO" = "xno"],
- AC_MSG_ERROR([cargo is required. Installation instructions available at https://www.rust-lang.org/tools/install])
- ]))
-
- YJIT_LIBS="yjit/target/${rb_rust_target_subdir}/libyjit.a"
- AS_CASE(["$target_os"],[openbsd*],[
- # Link libc++abi (which requires libpthread) for _Unwind_* functions needed by yjit
- LDFLAGS="$LDFLAGS -lpthread -lc++abi"
- ])
YJIT_OBJ='yjit.$(OBJEXT)'
JIT_OBJ='jit.$(OBJEXT)'
AS_IF([test x"$YJIT_SUPPORT" != "xyes" ], [
@@ -3974,38 +3961,23 @@ AS_CASE(["${YJIT_SUPPORT}"],
AC_DEFINE(USE_YJIT, 0)
])
-ZJIT_CARGO_BUILD_ARGS=
ZJIT_LIBS=
AS_CASE(["${ZJIT_SUPPORT}"],
[yes|dev], [
AS_IF([test x"$RUSTC" = "xno"],
AC_MSG_ERROR([rustc is required. Installation instructions available at https://www.rust-lang.org/tools/install])
)
- AS_IF([test x"$YJIT_SUPPORT" != "xno"],
- AC_MSG_ERROR([ZJIT cannot be enabled when YJIT is enabled])
- )
AS_CASE(["${ZJIT_SUPPORT}"],
[yes], [
- rb_rust_target_subdir=release
],
[dev], [
- rb_rust_target_subdir=debug
- ZJIT_CARGO_BUILD_ARGS='--profile dev --features disasm'
AC_DEFINE(RUBY_DEBUG, 1)
])
- AS_IF([test -n "${ZJIT_CARGO_BUILD_ARGS}"], [
- AC_CHECK_TOOL(CARGO, [cargo], [no])
- AS_IF([test x"$CARGO" = "xno"],
- AC_MSG_ERROR([cargo is required. Installation instructions available at https://www.rust-lang.org/tools/install])
- ]))
-
- ZJIT_LIBS="zjit/target/${rb_rust_target_subdir}/libzjit.a"
- AS_CASE(["$target_os"],[openbsd*],[
- # Link libc++abi (which requires libpthread) for _Unwind_* functions needed by yjit
- LDFLAGS="$LDFLAGS -lpthread -lc++abi"
- ])
ZJIT_OBJ='zjit.$(OBJEXT)'
JIT_OBJ='jit.$(OBJEXT)'
AS_IF([test x"$ZJIT_SUPPORT" != "xyes" ], [
@@ -4016,18 +3988,57 @@ AS_CASE(["${ZJIT_SUPPORT}"],
AC_DEFINE(USE_ZJIT, 0)
])
dnl These variables end up in ::RbConfig::CONFIG
-AC_SUBST(YJIT_SUPPORT)dnl what flavor of YJIT the Ruby build includes
AC_SUBST(RUSTC)dnl Rust compiler command
AC_SUBST(CARGO)dnl Cargo command for Rust builds
AC_SUBST(CARGO_BUILD_ARGS)dnl for selecting Rust build profiles
-AC_SUBST(ZJIT_CARGO_BUILD_ARGS)dnl for selecting Rust build profiles
-AC_SUBST(YJIT_LIBS)dnl for optionally building the Rust parts of YJIT
AC_SUBST(YJIT_OBJ)dnl for optionally building the C parts of YJIT
AC_SUBST(ZJIT_SUPPORT)dnl what flavor of ZJIT the Ruby build includes
-AC_SUBST(ZJIT_LIBS)dnl for optionally building the Rust parts of ZJIT
AC_SUBST(ZJIT_OBJ)dnl for optionally building the C parts of ZJIT
AC_SUBST(JIT_OBJ)dnl for optionally building C glue code for Rust FFI
}
[begin]_group "build section" && {
@@ -443,6 +443,7 @@ endif
include $(top_srcdir)/yjit/yjit.mk
include $(top_srcdir)/zjit/zjit.mk
# Query on the generated rdoc
#
@@ -0,0 +1,53 @@
@@ -35,3 +35,5 @@ default = []
# When moving an object, clear its original copy.
clear_old_copy = []
@@ -0,0 +1,4 @@
@@ -107,15 +107,14 @@ JIT_OBJ=@JIT_OBJ@
YJIT_SUPPORT=@YJIT_SUPPORT@
YJIT_LIBS=@YJIT_LIBS@
YJIT_OBJ=@YJIT_OBJ@
-YJIT_LIBOBJ = $(YJIT_LIBS:.a=.@OBJEXT@)
ZJIT_SUPPORT=@ZJIT_SUPPORT@
ZJIT_LIBS=@ZJIT_LIBS@
ZJIT_OBJ=@ZJIT_OBJ@
-ZJIT_LIBOBJ = $(ZJIT_LIBS:.a=.@OBJEXT@)
-CARGO_TARGET_DIR=@abs_top_builddir@/yjit/target
CARGO_BUILD_ARGS=@CARGO_BUILD_ARGS@
-ZJIT_CARGO_BUILD_ARGS=@ZJIT_CARGO_BUILD_ARGS@
-ZJIT_CARGO_TARGET_DIR=@abs_top_builddir@/zjit/target
LDFLAGS = @STATIC@ $(CFLAGS) @LDFLAGS@
EXE_LDFLAGS = $(LDFLAGS)
EXTLDFLAGS = @EXTLDFLAGS@
@@ -452,7 +452,9 @@ jit_compile(rb_execution_context_t *ec)
rb_zjit_compile_iseq(iseq, ec, false);
}
}
-#elif USE_YJIT
// Increment the ISEQ's call counter and trigger JIT compilation if not compiled
if (body->jit_entry == NULL && rb_yjit_enabled_p) {
body->jit_entry_calls++;
@@ -9,9 +9,6 @@ edition = "2021" # Rust 2021 edition to compile with
rust-version = "1.58.0" # Minimally supported rust version
publish = false # Don't publish to crates.io
-[lib]
-crate-type = ["staticlib"]
-
[dependencies]
# No required dependencies to simplify build process. TODO: Link to yet to be
# written rationale. Optional For development and testing purposes
@@ -27,27 +24,3 @@ disasm = ["capstone"]
# from cfg!(debug_assertions) so that we can see disasm of the code
# that would run in the release mode.
runtime_checks = []
-
-[profile.dev]
-opt-level = 0
-debug = true
-debug-assertions = true
-overflow-checks = true
-
-[profile.dev_nodebug]
-inherits = "dev"
-
-[profile.stats]
-inherits = "release"
-
-[profile.release]
-# NOTE: --enable-yjit builds use `rustc` without going through Cargo. You
-# might want to update the `rustc` invocation if you change this profile.
-opt-level = 3
-# The extra robustness that comes from checking for arithmetic overflow is
-# worth the performance cost for the compiler.
-overflow-checks = true
-# Generate debug info
-debug = true
-# Use ThinLTO. Much smaller output for a small amount of build time increase.
-lto = "thin"
@@ -8,3 +8,5 @@ edition = "2021"
[dependencies]
bindgen = "0.70.1"
env_logger = "0.11.5"
@@ -19,60 +19,21 @@ YJIT_SRC_FILES = $(wildcard \
# rebuild at the next build.
YJIT_LIB_TOUCH = touch $@
# YJIT_SUPPORT=yes when `configure` gets `--enable-yjit`
ifeq ($(YJIT_SUPPORT),yes)
-$(YJIT_LIBS): $(YJIT_SRC_FILES)
$(ECHO) 'building Rust YJIT (release mode)'
+$(Q) $(RUSTC) $(YJIT_RUSTC_ARGS)
$(YJIT_LIB_TOUCH)
-else ifeq ($(YJIT_SUPPORT),no)
-$(YJIT_LIBS):
- $(ECHO) 'Error: Tried to build YJIT without configuring it first. Check `make showconfig`?'
- @false
-else ifeq ($(YJIT_SUPPORT),$(filter dev dev_nodebug stats,$(YJIT_SUPPORT)))
-# NOTE: MACOSX_DEPLOYMENT_TARGET to match `rustc --print deployment-target` to avoid the warning below.
-# ld: warning: object file (yjit/target/debug/libyjit.a(<libcapstone object>)) was built for
-# newer macOS version (15.2) than being linked (15.0)
-# We don't use newer macOS feature as of yet.
-$(YJIT_LIBS): $(YJIT_SRC_FILES)
- $(ECHO) 'building Rust YJIT ($(YJIT_SUPPORT) mode)'
- +$(Q)$(CHDIR) $(top_srcdir)/yjit && \
- CARGO_TARGET_DIR='$(CARGO_TARGET_DIR)' \
- CARGO_TERM_PROGRESS_WHEN='never' \
- MACOSX_DEPLOYMENT_TARGET=11.0 \
- $(CARGO) $(CARGO_VERBOSE) build $(CARGO_BUILD_ARGS)
- $(YJIT_LIB_TOUCH)
-else
-endif
-
-yjit-libobj: $(YJIT_LIBOBJ)
-
-YJIT_LIB_SYMBOLS = $(YJIT_LIBS:.a=).symbols
-$(YJIT_LIBOBJ): $(YJIT_LIBS)
- $(ECHO) 'partial linking $(YJIT_LIBS) into $@'
-ifneq ($(findstring darwin,$(target_os)),)
- $(Q) $(CC) -nodefaultlibs -r -o $@ -exported_symbols_list $(YJIT_LIB_SYMBOLS) $(YJIT_LIBS)
-else
- $(Q) $(LD) -r -o $@ --whole-archive $(YJIT_LIBS)
- -$(Q) $(OBJCOPY) --wildcard --keep-global-symbol='$(SYMBOL_PREFIX)rb_*' $(@)
endif
-# For Darwin only: a list of symbols that we want the glommed Rust static lib to export.
-# Unfortunately, using wildcard like '_rb_*' with -exported-symbol does not work, at least
-# not on version 820.1. Assume llvm-nm, so XCode 8.0 (from 2016) or newer.
-#
-# The -exported_symbols_list pulls out the right archive members. Symbols not listed
-# in the list are made private extern, which are in turn made local as we're using `ld -r`.
-# Note, section about -keep_private_externs in ld's man page hints at this behavior on which
-# we rely.
-ifneq ($(findstring darwin,$(target_os)),)
-$(YJIT_LIB_SYMBOLS): $(YJIT_LIBS)
- $(Q) $(tooldir)/darwin-ar $(NM) --defined-only --extern-only $(YJIT_LIBS) | \
- sed -n -e 's/.* //' -e '/^$(SYMBOL_PREFIX)rb_/p' \
- -e '/^$(SYMBOL_PREFIX)rust_eh_personality/p' \
- > $@
-
-$(YJIT_LIBOBJ): $(YJIT_LIB_SYMBOLS)
endif
# By using YJIT_BENCH_OPTS instead of RUN_OPTS, you can skip passing the options to `make install`
@@ -94,7 +55,7 @@ RUST_VERSION = +1.58.0
.PHONY: yjit-smoke-test
yjit-smoke-test:
ifneq ($(strip $(CARGO)),)
- $(CARGO) $(RUST_VERSION) test --all-features -q --manifest-path='$(top_srcdir)/yjit/Cargo.toml'
endif
$(MAKE) btest RUN_OPTS='--yjit-call-threshold=1' BTESTS=-j
$(MAKE) test-all TESTS='$(top_srcdir)/test/ruby/test_yjit.rb'
@@ -9,6 +9,7 @@
#include "internal/numeric.h"
#include "internal/gc.h"
#include "internal/vm.h"
#include "vm_core.h"
#include "vm_callinfo.h"
#include "builtin.h"
@@ -1,25 +1,10 @@
[package]
name = "zjit"
version = "0.0.1"
-edition = "2024" # Rust 2021 edition to compile with
rust-version = "1.85.0" # Minimally supported rust version
publish = false # Don't publish to crates.io
-[lib]
-crate-type = ["staticlib"]
-
-[profile.release]
-# NOTE: --enable-zjit builds use `rustc` without going through Cargo. You
-# might want to update the `rustc` invocation if you change this profile.
-opt-level = 3
-# The extra robustness that comes from checking for arithmetic overflow is
-# worth the performance cost for the compiler.
-overflow-checks = true
-# Generate debug info
-debug = true
-# Use ThinLTO. Much smaller output for a small amount of build time increase.
-lto = "thin"
-
[dependencies]
# No required dependencies to simplify build process. TODO: Link to yet to be
# written rationale. Optional For development and testing purposes
@@ -8,3 +8,5 @@ edition = "2024"
[dependencies]
bindgen = "0.71.1"
env_logger = "0.11.5"
@@ -1,7 +1,8 @@
fn main() {
use std::env;
- if let Ok(ruby_build_dir) = env::var("RUBY_BUILD_DIR") {
// Link against libminiruby
println!("cargo:rustc-link-search=native={ruby_build_dir}");
println!("cargo:rustc-link-lib=static:-bundle=miniruby");
@@ -19,60 +19,20 @@ ZJIT_SRC_FILES = $(wildcard \
# rebuild at the next build.
ZJIT_LIB_TOUCH = touch $@
# ZJIT_SUPPORT=yes when `configure` gets `--enable-zjit`
ifeq ($(ZJIT_SUPPORT),yes)
-$(ZJIT_LIBS): $(ZJIT_SRC_FILES)
$(ECHO) 'building Rust ZJIT (release mode)'
+$(Q) $(RUSTC) $(ZJIT_RUSTC_ARGS)
$(ZJIT_LIB_TOUCH)
-else ifeq ($(ZJIT_SUPPORT),no)
-$(ZJIT_LIBS):
- $(ECHO) 'Error: Tried to build ZJIT without configuring it first. Check `make showconfig`?'
- @false
-else ifeq ($(ZJIT_SUPPORT),$(filter dev dev_nodebug stats,$(ZJIT_SUPPORT)))
-# NOTE: MACOSX_DEPLOYMENT_TARGET to match `rustc --print deployment-target` to avoid the warning below.
-# ld: warning: object file (zjit/target/debug/libzjit.a(<libcapstone object>)) was built for
-# newer macOS version (15.2) than being linked (15.0)
-# We don't use newer macOS feature as of yet.
-$(ZJIT_LIBS): $(ZJIT_SRC_FILES)
- $(ECHO) 'building Rust ZJIT ($(ZJIT_SUPPORT) mode)'
- +$(Q)$(CHDIR) $(top_srcdir)/zjit && \
- CARGO_TARGET_DIR='$(ZJIT_CARGO_TARGET_DIR)' \
- CARGO_TERM_PROGRESS_WHEN='never' \
- MACOSX_DEPLOYMENT_TARGET=11.0 \
- $(CARGO) $(CARGO_VERBOSE) build $(ZJIT_CARGO_BUILD_ARGS)
- $(ZJIT_LIB_TOUCH)
-else
-endif
-
-zjit-libobj: $(ZJIT_LIBOBJ)
-
-ZJIT_LIB_SYMBOLS = $(ZJIT_LIBS:.a=).symbols
-$(ZJIT_LIBOBJ): $(ZJIT_LIBS)
- $(ECHO) 'partial linking $(ZJIT_LIBS) into $@'
-ifneq ($(findstring darwin,$(target_os)),)
- $(Q) $(CC) -nodefaultlibs -r -o $@ -exported_symbols_list $(ZJIT_LIB_SYMBOLS) $(ZJIT_LIBS)
-else
- $(Q) $(LD) -r -o $@ --whole-archive $(ZJIT_LIBS)
- -$(Q) $(OBJCOPY) --wildcard --keep-global-symbol='$(SYMBOL_PREFIX)rb_*' $(@)
endif
-# For Darwin only: a list of symbols that we want the glommed Rust static lib to export.
-# Unfortunately, using wildcard like '_rb_*' with -exported-symbol does not work, at least
-# not on version 820.1. Assume llvm-nm, so XCode 8.0 (from 2016) or newer.
-#
-# The -exported_symbols_list pulls out the right archive members. Symbols not listed
-# in the list are made private extern, which are in turn made local as we're using `ld -r`.
-# Note, section about -keep_private_externs in ld's man page hints at this behavior on which
-# we rely.
-ifneq ($(findstring darwin,$(target_os)),)
-$(ZJIT_LIB_SYMBOLS): $(ZJIT_LIBS)
- $(Q) $(tooldir)/darwin-ar $(NM) --defined-only --extern-only $(ZJIT_LIBS) | \
- sed -n -e 's/.* //' -e '/^$(SYMBOL_PREFIX)rb_/p' \
- -e '/^$(SYMBOL_PREFIX)rust_eh_personality/p' \
- > $@
-
-$(ZJIT_LIBOBJ): $(ZJIT_LIB_SYMBOLS)
endif
# By using ZJIT_BENCH_OPTS instead of RUN_OPTS, you can skip passing the options to `make install`
@@ -113,7 +73,7 @@ zjit-bindgen: zjit.$(OBJEXT)
zjit-test: libminiruby.a
RUBY_BUILD_DIR='$(TOP_BUILD_DIR)' \
RUBY_LD_FLAGS='$(LDFLAGS) $(XLDFLAGS) $(MAINLIBS)' \
- CARGO_TARGET_DIR='$(ZJIT_CARGO_TARGET_DIR)' \
$(CARGO) nextest run --manifest-path '$(top_srcdir)/zjit/Cargo.toml' $(ZJIT_TESTS)
# Run a ZJIT test written with Rust #[test] under LLDB
@@ -126,7 +86,7 @@ zjit-test-lldb: libminiruby.a
fi; \
exe_path=`RUBY_BUILD_DIR='$(TOP_BUILD_DIR)' \
RUBY_LD_FLAGS='$(LDFLAGS) $(XLDFLAGS) $(MAINLIBS)' \
- CARGO_TARGET_DIR='$(ZJIT_CARGO_TARGET_DIR)' \
$(CARGO) nextest list --manifest-path '$(top_srcdir)/zjit/Cargo.toml' --message-format json --list-type=binaries-only | \
$(BASERUBY) -rjson -e 'puts JSON.load(STDIN.read).dig("rust-binaries", "zjit", "binary-path")'`; \
exec lldb $$exe_path -- --test-threads=1 $(ZJIT_TESTS)