Repository navigation
fix(coro): clobber aliased vector registers in the context switch - #557
Merged
Merged
Conversation
Contributor
📝 WalkthroughWalkthroughExpands ChangesSIMD clobber updates
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The context switch's inline-asm clobber list declared its vector registers as zmm0-31 only. On a target without AVX-512 (e.g. -mcpu baseline) the zmm register names don't exist, so LLVM silently drops those clobbers and never marks the aliased xmm0-15 as clobbered (confirmed in X86ISelLowering: a named clobber for a register requiring an absent subtarget feature resolves to nothing). The compiler is then free to keep a live value in an xmm register across the switch; on Windows x64 it parks values in the callee-saved xmm6-15, which the switch destroys — e.g. a '.none' union constant that gets splatted into Executor.pending_cleanup and later crashes the scheduler. Linux/SysV never keeps values live in xmm across a call, so the bug only surfaced on Windows/IOCP. Add xmm0-15 (covers the baseline SSE2 build) and ymm0-15 (AVX); zmm0-31 stay for AVX-512. Proven at codegen: an empty asm clobbering .zmm0 leaves the value in xmm0 (dropped); clobbering .xmm0 preserves it. Self-hosted backend honors both. (A comptime 'clobber everything' helper was tried but over-clobbered GP sub-registers/flags and miscompiled x86_64 release under LLVM; the explicit xmm/ymm additions are the minimal, targeted fix.)
lalinsky
force-pushed
the
fix/coro-vector-clobbers
branch
from
July 9, 2026 08:24
4b0b2de to
6e6a80f
Compare
The aarch64 switch listed only the SVE z0-31/p0-15 vector clobbers. Unlike x86's zmm (which LLVM drops on non-AVX-512 targets), LLVM does honor the z* clobbers as the aliased NEON v*/d* even without SVE, so this wasn't miscompiling — but relying on that aliasing is fragile. Clobber the NEON v0-v31 explicitly so preservation of the callee-saved v8-v15 across the switch is guaranteed regardless of how the wide SVE names are treated (same class as the x86_64 xmm fix, #460). Vector regs only — GP/flags are unchanged.
Mirror the x86_64 xmm/ymm fix (#460) on the remaining vector-capable arches. The context-switch inline asm must clobber every register the compiler could keep a live value in across the switch, including vector registers. - riscv64/riscv32: clobber RVV v0-v31 - loongarch64: clobber LSX vr0-vr31 and LASX xr0-xr31 These are defensive: on a baseline build the feature is absent and LLVM drops the named clobber (as it does for x86_64 zmm on baseline), so the codegen is unchanged. On a V/LSX/LASX-enabled build the clobber is honored and a vector value held across the switch is preserved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Intermittent crash on Windows/IOCP (ReleaseSafe): the scheduler dereferences a garbage task in
getNextTask, becauseExecutor.pending_cleanupgets stomped with theundefined(0xAA) fill pattern. It never reproduced on any other backend/OS, and had nothing to do with thepending_cleanupprotocol.Root cause
The x86_64 coroutine context switch's inline-asm clobber list declared its vector registers as
zmm0-31only. On a target without AVX-512 (e.g.-mcpu baseline, which is what we build) thosezmmregister names don't exist, so LLVM silently drops the clobbers and never marks the aliasedxmm0-15as clobbered (confirmed inX86ISelLowering: a named clobber for a register that requires an absent subtarget feature resolves to nothing).The compiler is then free to keep a live value in an
xmmregister across the switch. On Windows x64,xmm6-15are callee-saved, so it readily parks loop-invariants there — e.g. a.noneunion constant it will store intopending_cleanup. The switch destroysxmm6, and the garbage lands inpending_cleanup, later read as a bogus.park/.rescheduleand crashing the scheduler. On Linux/SysVxmm6-15are caller-saved, so the compiler never keeps a value live there across a call — which is why this was Windows/IOCP-only and looked like random field corruption.Diagnosed with WinDbg Time-Travel Debugging: a hardware data breakpoint on
pending_cleanup.tagreverse-executed toself.pending_cleanup = .nonecompiled asmovups [r13], xmm6, withxmm6 = 0xAAAA…(undefined fill), and no reload ofxmm6after the switch (whiler12/r13/r15were reloaded — their clobbers were honored).Fix
Add the aliased vector registers explicitly to each arch's context-switch clobber list, so the compiler can never keep a live value in a register the switch destroys:
xmm0-15andymm0-15(alongside the existingzmm0-31). This is the actual bug fix.v0-31(alongside the existing SVEz*). Defensive — LLVM already honors thez*clobbers as the aliasedv*/d*even without SVE, so this is belt-and-suspenders.v0-31. Defensive.vr0-31and LASXxr0-31. Defensive.On a baseline build the wider/feature-gated names are absent and LLVM drops the extra clobbers (exactly as it did for
zmmon baseline), so codegen is unchanged there. On a feature-enabled build (AVX/SVE/RVV/LSX/LASX) the clobber is honored and a vector value held across the switch is preserved.Verification
.zmm0on baseline leaves the value inxmm0(clobber dropped); clobbering.xmm0emits a preservingmovapsfirst. The self-hosted (Debug) backend honors both — so the bug was LLVM/release-only, and the fix is safe on both backends.riscv64+v,loongarch64+lsx+lasx) — all build.main) — no regression.Audit of other arches
Checked all switch clobber lists for the same aliasing gap:
d0-31), powerpc64 (f+v+vs), sparc64 (q0-15) — all use register names present on their baselines, and no wider aliasing feature drops them. Not affected; left unchanged.