Skip to content

YJIT: Support Windows (mswin x64) - #19114

Merged
hsbt merged 23 commits into
ruby:masterfrom
hsbt:claude/ruby-mswin-yjit-1c220e
Oct 6, 2026
Merged

hsbt merged 23 commits into
ruby:masterfrom
hsbt:claude/ruby-mswin-yjit-1c220e

Conversation

@hsbt

@hsbt hsbt commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

This makes YJIT build and pass nmake check on mswin x64, which Feature #18439 has asked for. I will look after the Windows side, so the JIT team does not need a Windows machine to keep it working.

  • Only x64 and YJIT's release mode are supported.
  • YJIT targets only the x86_64-pc-windows-msvc toolchain that rustup provides, so mingw is out of scope, and configure rejects --enable-yjit there explicitly.
  • A new job in windows.yml runs nmake check with --yjit-call-threshold=1.

I chose YJIT because it is the production JIT today, while ZJIT still assumes the SysV ABI. The jit.c part is shared with ZJIT, and the unwind approach below should also fit ZJIT since it already keeps a frame pointer.

Fixes split out

The bugs I found in code that Linux and macOS also use were fixed separately in #19123, #19124 and #19125.

Windows-only changes

  • The x86_64 backend uses the Win64 calling convention. Since RSI and RDI are callee-saved on Win64, Windows has 3 temp registers instead of 5.
  • jit.c manages code memory with VirtualAlloc and reserves it near the Ruby DLL, so calls to C functions fit in rel32.
  • One RBP-based unwind entry covers the whole JIT region, the shape Alan suggested for DWARF in Feature #19541. With the frame pointer always set up, longjmp and C++ exceptions pass through JIT frames even with pushes and pops in the body.
  • Fixnum results are guarded to fit in a 32-bit long, with one branch that replaces the 64-bit overflow check there. The guard sign-extends the low 32 bits with load_sext, so load_sext now types its output as 64 bits.

YJIT is 2.0x faster than the interpreter on ruby-bench's headline benchmarks on mswin (1.95x and 2.11x in two runs). I ran them with a minimal harness because ruby-bench's own harness does not work on Windows.

Changes outside Windows

Apart from the fixes split out, the code YJIT generates on Linux and macOS does not change.

  • The LLP64 checks depend on c_long::BITS and compile to nothing where long is 64-bit.
  • The Win64 code sits under cfg(windows).
  • load_sext types its output as 64 bits everywhere, but only the LLP64 guard calls it with a 32-bit value.

Two other differences are visible.

  • --yjit-stats has two new exit counters, which stay 0 outside Windows.
  • The warning for builds without YJIT mentions --enable-yjit.

The commits are meant to be reviewed one at a time.

Generated with Claude Code

@matzbot
matzbot requested a review from a team September 29, 2026 07:30
@github-actions github-actions Bot added the jit YJIT, ZJIT, ... label Sep 29, 2026
@hsbt

hsbt commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

@k0kubun @XrXr Could you look this PR?

The mswin support sits on top of fixes for five existing bugs in three groups, listed under "Bugs in shared code" in the description. Extracting them into three PRs first might make this easier to review, so let me know if you'd like me to do that.

@hsbt
hsbt force-pushed the claude/ruby-mswin-yjit-1c220e branch from 8160e5f to 9406b31 Compare September 29, 2026 11:30
@XrXr

XrXr commented Sep 29, 2026

Copy link
Copy Markdown
Member

Extracting them into three PRs first might make this easier to review, so let me know if you'd like me to do that.

That'd be nice, please do.

I'm pretty busy this week and next week so can't look at the mswin specific parts of this PR, but the shared bug fixes seem reasonable and simple enough to integrate.

@k0kubun

k0kubun commented Sep 29, 2026

Copy link
Copy Markdown
Member

Yeah let's carve out non-mswin parts as separate PRs.

@hsbt

hsbt commented Sep 30, 2026

Copy link
Copy Markdown
Member Author

I extracted non Windows issues to #19123 , #19124 and #19125

Comment thread yjit/src/codegen.rs Resolved
Comment thread yjit/src/codegen.rs Resolved
Comment thread yjit/src/codegen.rs Resolved

@k0kubun k0kubun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primarily reviewed the codegen part that I care most about; it adds more complexity for the Windows support but doesn't look unreasonably bad for it.

Since you stepped up for maintaining the Windows side yourself, I think we can move forward with this support.

@hsbt
hsbt force-pushed the claude/ruby-mswin-yjit-1c220e branch 2 times, most recently from 38ad8d7 to 9e33366 Compare September 30, 2026 09:47
Comment thread yjit/src/codegen.rs Outdated

/// Make a C long length field usable as a 64-bit operand. long is 32-bit on LLP64 (Windows),
/// where loading it into a register zero-extends it, and lengths are never negative.
fn c_long_len(&mut self, len_opnd: Opnd) -> Opnd {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing seems specific to "len" at the moment. How about changing the API to be c_long_mem instead?

As in, instead of:

    let array_len_opnd = Opnd::mem(
        std::os::raw::c_long::BITS as u8,
        array_reg,
        RUBY_OFFSET_RARRAY_AS_HEAP_LEN,
    );
    let array_len_opnd = asm.c_long_len(array_len_opnd);

something that can be used like:

    let array_len_opnd = asm.c_long_mem(
        array_reg,
        RUBY_OFFSET_RARRAY_AS_HEAP_LEN,
    );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agreed with your point, so I changed it to c_long_mem(base, disp) in 46f25e5. It now takes only what it needs and builds the operand itself.

@k0kubun k0kubun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider having one more approval before moving forward; we started to have a soft requirement of getting two approvals for big JIT changes.

@hsbt

hsbt commented Oct 2, 2026 •

Copy link
Copy Markdown
Member Author

@ruby/jit Could someone else please review this PR? I will wait until next Thursday (October 8) for one more approval. If no one is able to review by then, I will merge it with the current approval.

@XrXr XrXr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mswin specific stuff seem fairly isolated from the rest, so no objections from me for integrating.

Comment thread yjit.c Outdated
#ifdef _WIN32
// RtlUnwindEx(), which longjmp() and C++ exceptions use, needs unwind info for
// every frame. All JIT code runs in the frame gen_entry_prologue() sets up with
// frame_pointer, so a single UNWIND_INFO based on RBP covers the code region:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is true, strictly speaking. During the few instructions that does the frame setup you'd need different unwind code. I'm not sure if Windows can async unwind during that window, though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it only holds at the calls, which all happen inside the frame. I reworded the comment in 3284cf0.

hsbt and others added 10 commits October 6, 2026 12:40
LoadSExt exists to widen a narrower operand, but load_sext typed its output with the operand width, so the result could not be mixed with 64-bit operands and the arm64 emitter passed a 32-bit destination to sxtw. The register allocator still sizes the out register by the operand, so both emitters widen it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
std::os::unix::io::RawFd is not available on Windows. Leaking the opened File works everywhere and needs no fd round trip on each write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
spawn cannot pass file descriptors other than 0 to 2 on Windows. A child that dies without writing the file, or leaves it empty, now surfaces with its exit status and stderr instead of an error from loading the stats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The bindings are generated on LP64, where ID and st_data_t are unsigned long and long is 64-bit. On Windows long is 32-bit and both are unsigned long long, so pin them to u64, cast long arguments with the platform width, and widen the RString and RArray length fields before mixing them with 64-bit operands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A Fixnum holds a C long, which is 32-bit on Windows, so an arithmetic result or a length that did not overflow 64 bits could still be out of the Fixnum range and make an unnormalized Fixnum. Side-exit there as each instruction does on a 64-bit overflow, with opt_mult chaining to send, and treat String#length as a possible Bignum, whose allocation needs the PC and SP saved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Stats and counters can exceed the Fixnum range, which is 31 bits on Windows, and failed the assertion in VALUE::fixnum_from_usize.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Generated code called C functions and Rust callbacks with the SysV convention. On Windows, pass the first four arguments in RCX, RDX, R8 and R9 and the rest on the stack above the 32-byte shadow space, and keep RSI and RDI out of the temp registers since they are callee-saved there, which limits --yjit-temp-regs to 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reserve the region with VirtualAlloc and commit pages when they become writable, in place of mmap and mprotect. Freed pages stay committed with their contents discarded, since rb_jit_mark_executable() covers the whole mapped region and VirtualProtect() fails on decommitted or reserved-only pages, and YJIT frees only the mapped part of the last code page for the same reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
VirtualAlloc(NULL) placed the region more than 2 GiB away from the DLL, so every C call from YJIT code became an indirect call through a register. Probe below rb_jit_reserve_addr_space like the MAP_FIXED_NOREPLACE loop on Linux so that the calls fit in rel32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RtlUnwindEx(), which longjmp() and C++ exceptions use, needs unwind info for every frame, so raising through JIT frames skipped __finally blocks and C++ destructors. Exception dispatch also could not walk through JIT frames to reach the unhandled exception filter that the compaction read barrier relies on. Describe the entry frame, which now always has a frame pointer on Windows, for the whole region.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hsbt and others added 11 commits October 6, 2026 12:41
UCRT's isatty returns 0x40 for a character device, so the comparison with 1 never held and --yjit-dump-disasm was never colored. Checking for non-zero instead would also color output sent to NUL, which is a character device too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rustc builds yjit.lib, which is linked into miniruby and the DLL directly. Archiving it into the static library would make mkexports.rb export every Rust symbol under a mangled name that does not exist, so a YJIT build does not install the static library.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jit.c now picks its Windows code paths by _WIN32 alone, so an explicit --enable-yjit on mingw no longer stops at the #error there and would build a combination nobody tests. YJIT on Windows is supported only through the mswin build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
/tmp/perf-{pid}.map resolves to a usually missing directory on the current drive, so the first compilation panicked, and Linux perf could not read the map there anyway. --yjit-perf=fp is unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The disasm options and RubyVM::YJIT.disasm need a dev build, which win32/configure.bat cannot make. The warning for builds without YJIT also mentions --enable-yjit, since installing rustc alone does not enable it on mswin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Win64 code paths and LLP64 guards in YJIT are compiled only for mswin, so no other YJIT job notices a change that breaks them. --yjit-call-threshold=1 makes the btest cases for those guards run compiled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bindgen now finds rb_jit_get_page_size and rb_jit_reserve_addr_space in internal/jit.h, which moves them in both generated files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They emit instructions, which a call like asm.cmp_fixnum_fits_long(val) makes clearer than a free function taking asm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixnum operands fit in 32 bits where long is 32-bit, so the 64-bit overflow check never fires there and the range check alone decides. Code generated where long is 64-bit is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hsbt
hsbt force-pushed the claude/ruby-mswin-yjit-1c220e branch from 9e33366 to 4f417f2 Compare October 6, 2026 03:41
hsbt and others added 2 commits October 6, 2026 18:30
Nothing in it was specific to lengths, and building the C long memory operand inside saves each caller from spelling out its width.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The single entry is right at every call, but not within the instructions that set up and tear down the frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hsbt

hsbt commented Oct 6, 2026

Copy link
Copy Markdown
Member Author

@k0kubun @XrXr Thank you both for taking the time to review this while you were busy, it means a lot to finally bring YJIT to Windows users. I'm going to merge it tomorrow.

@hsbt
hsbt merged commit e8964f3 into ruby:master Oct 6, 2026
156 of 158 checks passed
@hsbt
hsbt deleted the claude/ruby-mswin-yjit-1c220e branch October 6, 2026 21:11
@hsbt hsbt mentioned this pull request Oct 6, 2026
larskanis added a commit to larskanis/ruby that referenced this pull request Oct 16, 2026
YJIT on Windows was introduced in ruby#19114
for MSVC on x86_64 only. But is works with a few adjustments on MINGW as well.

Currently the libruby dll exports leaked symbols from rust, so that the leak
check is bypassed for now.
larskanis added a commit to larskanis/ruby that referenced this pull request Oct 16, 2026
YJIT on Windows was introduced in ruby#19114
for MSVC on x86_64 only. But is works with a few adjustments on MINGW as well.

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

I divided the SEH unwind tests into C++ standard based and MSVC custom syntax
extension.
larskanis added a commit to larskanis/ruby that referenced this pull request Oct 16, 2026
YJIT on Windows was introduced in ruby#19114
for MSVC on x86_64 only. But is works with a few adjustments on MINGW as well.

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

I divided the SEH unwind tests into C++ standard based and MSVC custom syntax
extension.
larskanis added a commit to larskanis/ruby that referenced this pull request Oct 16, 2026
YJIT on Windows was introduced in ruby#19114
for MSVC on x86_64 only. But is works with a few adjustments on MINGW as well.

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

I divided the SEH unwind tests into C++ standard based and MSVC custom syntax
extension.
larskanis added a commit to oneclick/rubyinstaller2-packages that referenced this pull request Nov 15, 2026
larskanis added a commit to oneclick/rubyinstaller2-packages that referenced this pull request Nov 15, 2026
larskanis added a commit to oneclick/rubyinstaller2-packages that referenced this pull request Nov 15, 2026
larskanis added a commit to larskanis/ruby that referenced this pull request Nov 15, 2026
YJIT on Windows was introduced in ruby#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. On arm64 the dll exports are as intended.

I divided the SEH unwind tests into C++ standard based and MSVC custom syntax
extension.
larskanis added a commit to oneclick/rubyinstaller2-packages that referenced this pull request Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit YJIT, ZJIT, ...

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants