Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 382 additions & 0 deletions mingw-w64-ruby-head/0017-Windows-Allow-YJIT-on-MINGW-toolset.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
From e0455344828a71364a9aa1d310ead4cb9b453b1a Mon Sep 17 00:00:00 2001
From: Lars Kanis <lars@greiz-reinsdorf.de>
Date: Fri, 9 Oct 2026 22:59:17 +0200
Subject: [PATCH] Windows: Allow YJIT on MINGW toolset

YJIT on Windows was introduced in https://github.com/ruby/ruby/pull/19114
for MSVC on x86_64 only. But is works with a few adjustments on MINGW as well.

Currently the libruby dll exports leaks symbols from rust on x64, so that the leak
check is bypassed for now.

I divided the SEH unwind tests into C++ standard based and MSVC custom syntax
extension.
---
.github/workflows/mingw.yml | 4 ++-
NEWS.md | 10 ++++---
configure.ac | 13 +++++----
defs/jit.mk | 4 +++
doc/distribution/windows.md | 8 +++---
doc/jit/yjit.md | 12 ++++----
ext/-test-/win32/cxx_catch/extconf.rb | 4 +--
missing/lgamma_r.c | 4 +++
test/-ext-/win32/test_cxx_catch.rb | 41 +++++++++++++++++++++++++++
test/-ext-/win32/test_seh_unwind.rb | 24 ----------------
test/ruby/test_yjit.rb | 6 ++--
tool/leaked-globals | 4 +++
yjit.rb | 4 +--
13 files changed, 88 insertions(+), 50 deletions(-)
create mode 100644 test/-ext-/win32/test_cxx_catch.rb

diff --git a/NEWS.md b/NEWS.md
index 256720720a2..b724adc9b8c 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -731,10 +731,12 @@ A lot of work has gone into making Ractors more stable, performant, and usable.

### YJIT

-* YJIT now supports Windows (mswin) on x64 and arm64 CPUs. Configure with
- `win32\configure.bat --enable-yjit`, which requires `rustc` with the
- `x86_64-pc-windows-msvc` or `aarch64-pc-windows-msvc` target. Only the
- release build is supported, and MinGW is not. [[Feature #18439]]
+* YJIT now supports Windows (mswin and mingw) on x64 and arm64 CPUs.
+ Configure with option `--enable-yjit`, which requires `rustc` with the mswin
+ target `x86_64-pc-windows-msvc` or `aarch64-pc-windows-msvc` target.
+ On mingw the necessary rust targets are `x86_64-pc-windows-gnu` or
+ `aarch64-pc-windows-gnu`.
+ Only the release build is supported. [[Feature #18439]]
* Add `RubyVM::YJIT.max_compile_time_ns` and `RubyVM::YJIT.max_compile_time_ns=`
to give YJIT a compilation time budget. When `RubyVM::YJIT.total_compile_time_ns`
exceeds it, YJIT pauses compilation until the budget is raised, which
diff --git a/configure.ac b/configure.ac
index 054ea8659e4..f942a0c0887 100644
--- a/configure.ac
+++ b/configure.ac
@@ -309,7 +309,7 @@ AS_CASE(["${build_os}"],
AC_PATH_TOOL([NM], [nm], [/usr/ccs/bin/nm], [/usr/ccs/bin:$PATH])
])
AS_CASE(["${target_os}"],
-[cygwin*|msys*|mingw*|darwin*], [
+[cygwin*|msys*|darwin*], [
ac_ct_OBJCOPY=":"
ac_cv_prog_OBJCOPY=":"
ac_cv_prog_ac_ct_OBJCOPY=":"
@@ -3888,7 +3888,7 @@ AS_IF([test "$RUSTC" != "no"],
AC_MSG_CHECKING([whether ${RUSTC} works for YJIT])
AS_CASE(["$target_cpu"],
[arm64|aarch64], [JIT_TARGET_ARCH=aarch64],
- [x86_64], [JIT_TARGET_ARCH=x86_64],
+ [x86_64|x64], [JIT_TARGET_ARCH=x86_64],
)
dnl Fails in case rustc target doesn't match ruby target.
dnl Can happen on Rosetta, for example.
@@ -3961,9 +3961,6 @@ YJIT_LIBS=
JIT_CARGO_SUPPORT=no
AS_CASE(["${YJIT_SUPPORT}"],
[yes|dev|stats|dev_nodebug], [
- AS_CASE(["$target_os"], [mingw*], [
- AC_MSG_ERROR([YJIT is not supported on mingw. Use win32\configure.bat --enable-yjit on mswin instead])
- ])
AS_IF([test x"$RUSTC" = "xno"],
AC_MSG_ERROR([rustc is required. Installation instructions available at https://www.rust-lang.org/tools/install])
)
@@ -4094,6 +4091,12 @@ AS_IF([test -n "$RUST_LIB"], [
RUST_LIB='$(TOP_BUILD_DIR)'/"${RUST_LIB}"
])

+AS_CASE(["$target_os"],
+[mingw*], [ AS_IF([test x"$YJIT_SUPPORT" = "xyes"], [
+ LIBS="$LIBS -lntdll -luserenv -ldbghelp -lsynchronization"
+ ])
+])
+
dnl These variables end up in ::RbConfig::CONFIG
AC_SUBST(RUSTC)dnl Rust compiler command
AC_SUBST(JIT_RUST_FLAGS)dnl the common rustc flags for JIT crates such as zjit
diff --git a/defs/jit.mk b/defs/jit.mk
index 9487c9c037a..80b413e2c82 100644
--- a/defs/jit.mk
+++ b/defs/jit.mk
@@ -69,6 +69,10 @@ $(RUST_LIBOBJ): $(RUST_LIB)
$(ECHO) 'partial linking $(RUST_LIB) into $@'
ifneq ($(findstring darwin,$(target_os)),)
$(Q) $(CC) -nodefaultlibs -r -o $@ -exported_symbols_list $(RUST_LIB_SYMBOLS) $(RUST_LIB)
+else ifneq ($(findstring x64-mingw,$(arch)),)
+ $(Q) $(OBJCOPY) --weaken-symbol=lgamma_r $(RUST_LIB) $@
+else ifneq ($(findstring aarch64-mingw,$(arch)),)
+ $(Q) lld-link //lib //out:`cygpath -w $@` $(YJIT_LIBS)
else
$(Q) $(LD) -r -o $@ --whole-archive $(RUST_LIB)
-$(Q) $(OBJCOPY) --wildcard --keep-global-symbol='$(SYMBOL_PREFIX)rb_*' $(@)
diff --git a/doc/distribution/windows.md b/doc/distribution/windows.md
index e8d6dfd48e2..7c34bcd0bf3 100644
--- a/doc/distribution/windows.md
+++ b/doc/distribution/windows.md
@@ -20,7 +20,7 @@ Ruby core development can be done either in Windows `cmd` like:
ridk install
ridk enable ucrt64

-pacman -S --needed %MINGW_PACKAGE_PREFIX%-openssl %MINGW_PACKAGE_PREFIX%-libyaml %MINGW_PACKAGE_PREFIX%-libffi
+pacman -S --needed %MINGW_PACKAGE_PREFIX%-openssl %MINGW_PACKAGE_PREFIX%-libyaml %MINGW_PACKAGE_PREFIX%-libffi %MINGW_PACKAGE_PREFIX%-rust

mkdir c:\work\ruby
cd /d c:\work\ruby
@@ -31,7 +31,7 @@ sh ./src/autogen.sh

mkdir build
cd build
-sh ../src/configure -C --disable-install-doc
+sh ../src/configure -C --disable-install-doc --enable-yjit
make
```

@@ -42,7 +42,7 @@ ridk install
ridk enable ucrt64
bash

-pacman -S --needed $MINGW_PACKAGE_PREFIX-openssl $MINGW_PACKAGE_PREFIX-libyaml $MINGW_PACKAGE_PREFIX-libffi
+pacman -S --needed $MINGW_PACKAGE_PREFIX-openssl $MINGW_PACKAGE_PREFIX-libyaml $MINGW_PACKAGE_PREFIX-libffi $MINGW_PACKAGE_PREFIX-rust

mkdir /c/work/ruby
cd /c/work/ruby
@@ -51,7 +51,7 @@ git clone https://github.com/ruby/ruby src

./src/autogen.sh
cd build
-../src/configure -C --disable-install-doc
+../src/configure -C --disable-install-doc --enable-yjit
make
```

diff --git a/doc/jit/yjit.md b/doc/jit/yjit.md
index 6db434b5523..cff49245c76 100644
--- a/doc/jit/yjit.md
+++ b/doc/jit/yjit.md
@@ -9,7 +9,7 @@ YJIT - Yet Another Ruby JIT

YJIT is a lightweight, minimalistic Ruby JIT built inside CRuby.
It lazily compiles code using a Basic Block Versioning (BBV) architecture.
-YJIT is currently supported for macOS, Linux and BSD on x86-64 and arm64/aarch64 CPUs, and for Windows (mswin) on x64 and arm64 CPUs.
+YJIT is currently supported for macOS, Linux and BSD on x86-64 and arm64/aarch64 CPUs, and for Windows (mswin and mingw) on x64 and arm64 CPUs.
This project is open source and falls under the same license as CRuby.

<p align="center"><b>
@@ -57,7 +57,7 @@ series = {MPLR 2023}

## Current Limitations

-YJIT may not be suitable for certain applications. It currently only supports macOS, Linux and BSD on x86-64 and arm64/aarch64 CPUs, and Windows (mswin) on x64 and arm64 CPUs. YJIT will use more memory than the Ruby interpreter because the JIT compiler needs to generate machine code in memory and maintain additional state information.
+YJIT may not be suitable for certain applications. It currently only supports macOS, Linux and BSD on x86-64 and arm64/aarch64 CPUs, and Windows (mswin and mingw) on x64 and arm64 CPUs. YJIT will use more memory than the Ruby interpreter because the JIT compiler needs to generate machine code in memory and maintain additional state information.
You can change how much executable memory is allocated using [YJIT's command-line options](#command-line-options).

## Installation
@@ -133,7 +133,9 @@ brew install openssl libyaml
make -j && make install
```

-On Windows (mswin), YJIT can be built only in release mode, with a `rustc` that has the `x86_64-pc-windows-msvc` target on x64, or the `aarch64-pc-windows-msvc` target on arm64.
+On Windows (mswin and mingw), YJIT can be built only in release mode.
+Mswin requires `rustc` that has the `x86_64-pc-windows-msvc` target on x64, or the `aarch64-pc-windows-msvc` target on arm64.
+Mingw requires `rustc` with the targets `x86_64-pc-windows-gnu` target on x64, or the `aarch64-pc-windows-gnu` target on arm64.
See [Windows](../distribution/windows.md) for the rest of the build steps.

```batch
@@ -187,7 +189,7 @@ You can see what YJIT has compiled by running YJIT with the `--yjit-log` command
```

The machine code generated for a given method can be printed by adding `puts RubyVM::YJIT.disasm(method(:method_name))` to a Ruby script. Note that no code will be generated if the method is not compiled.
-This needs Ruby configured with `--enable-yjit=dev` or `--enable-yjit=dev_nodebug`, so it is not available on Windows (mswin), where YJIT is built only in release mode.
+This needs Ruby configured with `--enable-yjit=dev` or `--enable-yjit=dev_nodebug`, so it is not available on Windows, where YJIT is built only in release mode.

<h3 id="command-line-options">Command-Line Options</h3>

@@ -256,7 +258,7 @@ can be optimized by YJIT, at the cost of more memory usage.
If you start Ruby with `--yjit-stats`, e.g. using an environment variable `RUBYOPT=--yjit-stats`,
`RubyVM::YJIT.runtime_stats[:ratio_in_yjit]` shows the percentage of total YARV instructions
executed by YJIT as opposed to the CRuby interpreter.
-This stat is available only when Ruby is configured with `--enable-yjit=stats`, `--enable-yjit=dev` or `--enable-yjit=dev_nodebug`, so not on Windows (mswin), where YJIT is built only in release mode.
+This stat is available only when Ruby is configured with `--enable-yjit=stats`, `--enable-yjit=dev` or `--enable-yjit=dev_nodebug`, so not on Windows, where YJIT is built only in release mode.
Ideally, `ratio_in_yjit` should be as large as 99%, and increasing `--yjit-mem-size` often
helps improving `ratio_in_yjit`.

diff --git a/ext/-test-/win32/cxx_catch/extconf.rb b/ext/-test-/win32/cxx_catch/extconf.rb
index dfc547250f4..2dec3e0ed3e 100644
--- a/ext/-test-/win32/cxx_catch/extconf.rb
+++ b/ext/-test-/win32/cxx_catch/extconf.rb
@@ -1,5 +1,5 @@
# frozen_string_literal: false
-if $mswin
- $CXXFLAGS << " -EHsc"
+if $mswin || $mingw
+ $CXXFLAGS << " -EHsc" if $mswin
create_makefile("-test-/win32/cxx_catch")
end
diff --git a/missing/lgamma_r.c b/missing/lgamma_r.c
index 01066d29300..35b9ce3bcab 100644
--- a/missing/lgamma_r.c
+++ b/missing/lgamma_r.c
@@ -59,6 +59,10 @@ loggamma(double x) /* the natural logarithm of the Gamma function. */
#endif

/* the natural logarithm of the absolute value of the Gamma function */
+#if defined(__MINGW64__) && RBIMPL_HAS_ATTRIBUTE(weak)
+/* Avoid error on MINGW, libyjit: multiple definition of `lgamma_r'; */
+__attribute__((weak))
+#endif
double
lgamma_r(double x, int *signp)
{
diff --git a/test/-ext-/win32/test_cxx_catch.rb b/test/-ext-/win32/test_cxx_catch.rb
new file mode 100644
index 00000000000..fd12d07b299
--- /dev/null
+++ b/test/-ext-/win32/test_cxx_catch.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: false
+require 'test/unit'
+require_relative '../../lib/jit_support'
+
+module Bug
+ module Win32
+ class TestCxxCatch < Test::Unit::TestCase
+ def test_raise_in_cxx_catch
+ assert_raise_in_cxx_catch(%w[--disable=yjit])
+ end
+
+ def test_raise_in_cxx_catch_through_yjit
+ omit "YJIT is not supported" unless JITSupport.yjit_supported?
+ assert_raise_in_cxx_catch(%w[--yjit --yjit-call-threshold=1])
+ end
+
+ private
+
+ def assert_raise_in_cxx_catch(args)
+ exp = /mswin/ =~ RUBY_PLATFORM ?
+ %w[true 0 RuntimeError 0] :
+ %w[true 0 RuntimeError]
+ assert_in_out_err(args, <<-INPUT, exp, [])
+ require '-test-/win32/cxx_catch'
+ def raise_in_catch
+ Bug::Win32.raise_in_catch
+ rescue RuntimeError => e
+ e.class
+ end
+ def throw_and_catch = Bug::Win32.throw_and_catch
+ p throw_and_catch
+ p Bug::Win32.live_exceptions
+ p raise_in_catch
+ # longjmp in C++ catch block is undefined behavior according to C++ standard.
+ # Destructor of counted_exception is called in MSVC but not in GCC.
+ p Bug::Win32.live_exceptions if /mswin/ =~ RUBY_PLATFORM
+ INPUT
+ end
+ end
+ end
+end if /mswin|mingw/ =~ RUBY_PLATFORM
diff --git a/test/-ext-/win32/test_seh_unwind.rb b/test/-ext-/win32/test_seh_unwind.rb
index 5bfff4723f6..4362fad0e3d 100644
--- a/test/-ext-/win32/test_seh_unwind.rb
+++ b/test/-ext-/win32/test_seh_unwind.rb
@@ -14,15 +14,6 @@ def test_finally_runs_on_raise_through_yjit
assert_finally_ran(%w[--yjit --yjit-call-threshold=1])
end

- def test_raise_in_cxx_catch
- assert_raise_in_cxx_catch(%w[--disable=yjit])
- end
-
- def test_raise_in_cxx_catch_through_yjit
- omit "YJIT is not supported" unless JITSupport.yjit_supported?
- assert_raise_in_cxx_catch(%w[--yjit --yjit-call-threshold=1])
- end
-
private

def assert_finally_ran(args)
@@ -37,21 +28,6 @@ def raise_in_try
p raise_in_try
INPUT
end
-
- def assert_raise_in_cxx_catch(args)
- assert_in_out_err(args, <<-INPUT, %w[0 true 0], [])
- require '-test-/win32/cxx_catch'
- def raise_in_catch
- Bug::Win32.raise_in_catch
- rescue RuntimeError
- end
- def throw_and_catch = Bug::Win32.throw_and_catch
- raise_in_catch
- p Bug::Win32.live_exceptions
- p throw_and_catch
- p Bug::Win32.live_exceptions
- INPUT
- end
end
end
end if /mswin/ =~ RUBY_PLATFORM
diff --git a/test/ruby/test_yjit.rb b/test/ruby/test_yjit.rb
index 42f88979dc9..eeae89cc252 100644
--- a/test/ruby/test_yjit.rb
+++ b/test/ruby/test_yjit.rb
@@ -49,7 +49,7 @@ def test_yjit_in_version
def test_command_line_switches
assert_in_out_err('--yjit-', '', [], /invalid option --yjit-/)
assert_in_out_err('--yjithello', '', [], /invalid option --yjithello/)
- limit = /x64-mswin/ =~ RUBY_PLATFORM ? 3 : 5
+ limit = /x64-mswin|x64-mingw/ =~ RUBY_PLATFORM ? 3 : 5
assert_in_out_err("--yjit-temp-regs=#{limit + 1}", '', [], /--yjit-temp-regs must be <= #{limit}\n.*invalid YJIT option 'temp-regs=#{limit + 1}'/m)
#assert_in_out_err('--yjit-call-threshold', '', [], /--yjit-call-threshold needs an argument/)
#assert_in_out_err('--yjit-call-threshold=', '', [], /--yjit-call-threshold needs an argument/)
@@ -65,7 +65,7 @@ def test_perf_map_on_windows
warning = ['WARNING: --yjit-perf does not write a perf map on Windows']
assert_in_out_err(%w[--yjit-perf --yjit-call-threshold=1], 'def foo = 1; foo; p :ok', [':ok'], warning, success: true)
assert_in_out_err(%w[--yjit-perf=fp --yjit-call-threshold=1], 'def foo = 1; foo; p :ok', [':ok'], [], success: true)
- end if /mswin/ =~ RUBY_PLATFORM
+ end if /mswin|mingw/ =~ RUBY_PLATFORM

def test_rel32_calls_on_windows
stats = assert_compiles(<<~'RUBY', exits: :any)
@@ -76,7 +76,7 @@ def foo = [1, 2].sum
omit "the JIT region was reserved out of rel32 range of the C functions"
end
assert_operator stats[:num_send_x86_rel32], :>, 0
- end if /x64-mswin/ =~ RUBY_PLATFORM
+ end if /x64-mswin|x64-mingw/ =~ RUBY_PLATFORM

def test_yjit_enable
args = []
diff --git a/tool/leaked-globals b/tool/leaked-globals
index c1a3fb42b88..6a0f682f239 100755
--- a/tool/leaked-globals
+++ b/tool/leaked-globals
@@ -77,6 +77,10 @@
puts "Skip #{col.skip("#{bitcode} LLVM bitcode object(s)")}" if bitcode > 0
end

+# TODO: if YJIT is enabled, the Rust functions are exported on GCC based MINGW currently
+# Hiding symbols leads to linker errors or segfaults in the resulting binary.
+exit if RUBY_PLATFORM.include?("x64-mingw")
+
print "Checking leaked global symbols..."
STDOUT.flush
if soext
diff --git a/yjit.rb b/yjit.rb
index f3c41c36d01..c47d4aa4d09 100644
--- a/yjit.rb
+++ b/yjit.rb
@@ -250,8 +250,8 @@ def self.disasm(iseq) # :nodoc:
disasm_str = Primitive.rb_yjit_disasm_iseq(iseq)

if !disasm_str
- hint = if RUBY_PLATFORM.include?("mswin")
- ",\nbut YJIT on mswin supports release builds only"
+ hint = if RUBY_PLATFORM =~ /mswin|mingw/
+ ",\nbut YJIT on Windows supports release builds only"
else
", i.e.\n./configure --enable-yjit=dev"
end
--
2.52.0.windows.1

Loading
Loading