Repository navigation
YJIT: Support Windows (mswin x64) - #19114
Conversation
8160e5f to
9406b31
Compare
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. |
|
Yeah let's carve out non-mswin parts as separate PRs. |
k0kubun
left a comment
There was a problem hiding this comment.
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.
38ad8d7 to
9e33366
Compare
|
|
||
| /// 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 { |
There was a problem hiding this comment.
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,
);There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Please consider having one more approval before moving forward; we started to have a soft requirement of getting two approvals for big JIT changes.
|
@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
left a comment
There was a problem hiding this comment.
The mswin specific stuff seem fairly isolated from the rest, so no objections from me for integrating.
| #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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Right, it only holds at the calls, which all happen inside the frame. I reworded the comment in 3284cf0.
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>
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>
9e33366 to
4f417f2
Compare
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>
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.
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.
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.
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.
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.
This makes YJIT build and pass
nmake checkon 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.x86_64-pc-windows-msvctoolchain that rustup provides, so mingw is out of scope, andconfigurerejects--enable-yjitthere explicitly.windows.ymlrunsnmake checkwith--yjit-call-threshold=1.I chose YJIT because it is the production JIT today, while ZJIT still assumes the SysV ABI. The
jit.cpart 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
jit.cmanages code memory withVirtualAllocand reserves it near the Ruby DLL, so calls to C functions fit in rel32.longjmpand C++ exceptions pass through JIT frames even with pushes and pops in the body.long, with one branch that replaces the 64-bit overflow check there. The guard sign-extends the low 32 bits withload_sext, soload_sextnow 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.
c_long::BITSand compile to nothing wherelongis 64-bit.cfg(windows).load_sexttypes its output as 64 bits everywhere, but only the LLP64 guard calls it with a 32-bit value.Two other differences are visible.
--yjit-statshas two new exit counters, which stay 0 outside Windows.--enable-yjit.The commits are meant to be reviewed one at a time.
Generated with Claude Code