Skip to content

codegen(win64): closure call type matches by-value callee return (#806) - #819

Merged
ropoctl merged 2 commits into
mainfrom
windows-806-closure-callabi
Aug 18, 2026
Merged

ropoctl merged 2 commits into
mainfrom
windows-806-closure-callabi

Conversation

@ropoctl

@ropoctl ropoctl commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #818 (windows-806-closure-sret).

Codegen fix

mir_build_closure_fn_type built the closure call type with manual
win64 sret-lowering: when internal_abi_needs_sret(ret) was true it
pushed an sret pointer parameter and set the return to void, ordering
the params env-first, sret-second.

But the closure definition is always emitted with a by-value direct
return (define %str @__closure(ptr %env)) — gen_closure forces
current_function_name_sym = 0, so a closure never inherits the
enclosing function's sret verdict. LLVM then applies its own win64 sret
to the callee: sret pointer in RCX, env shifted to RDX. The caller,
however, passed env in RCX and the sret pointer in RDX. Swapped
registers: the str result was written through the env pointer
(garbage) and the real destination was never written, so a
str-returning closure (e.g. .with_context(() => "lazy")) yielded a
corrupt message.

Removing the manual sret-lowering makes the call type match the
always-by-value callee. Contrast mir_build_raw_fn_type, whose sret
lowering is correct because a named fn-pointer target is itself
sret-lowered by declare_function; only the closure path diverged.
Gated behind internal_abi_needs_sret (false off win64), so linux/mac
IR is byte-identical.

Test un-skips

Three spec tests failed only through the corrupted str-closure path and
now match their linux baseline (cross-built, run under wine, exit 0):

  • spec_ss10_6_error_context — str-returning with_context closure
  • spec_ss10_5_sequence_traverse_transpose — fat-fn/closure str values
  • spec_ss14_3_1_no_suspend_blocks — async closure value return

The remaining four #806 tests (option_result_combinators,
result_combinators_complete, error_trait, collect_targets) still
fail on win64 through a distinct data-flow path and stay skipped.

@ropoctl
ropoctl force-pushed the windows-806-closure-callabi branch from 58865ae to 0eff756 Compare August 17, 2026 21:36
@ropoctl
ropoctl force-pushed the windows-806-closure-callabi branch from 0eff756 to 11fa778 Compare August 17, 2026 23:03
Base automatically changed from windows-806-closure-sret to main August 17, 2026 23:19
@ropoctl
ropoctl force-pushed the windows-806-closure-callabi branch 4 times, most recently from d0636d1 to b683c92 Compare August 18, 2026 02:24
…806)

mir_build_closure_fn_type built the closure CALL type with manual win64
sret-lowering: when internal_abi_needs_sret(ret) it pushed an sret ptr
param and set the return to void, ordering params env-first, sret-second.

But the closure DEFINITION is always emitted with a by-value direct
return (define %str @__closure(ptr %env)) — gen_closure forces
current_function_name_sym = 0 so a closure never inherits the enclosing
fn's sret verdict. LLVM then applies its own win64 sret to the callee:
sret ptr in RCX, env shifted to RDX. The caller, however, passed env in
RCX and the sret ptr in RDX. Swapped registers: the str result was
written through the env pointer (garbage) and the real destination was
never written, so a str-returning closure (e.g. .with_context(() =>
"lazy")) yielded a corrupt message.

Remove the manual sret-lowering so the call type matches the always
by-value callee. Contrast mir_build_raw_fn_type, whose sret lowering is
correct because a named fn-pointer target is itself sret-lowered by
declare_function; only the closure path diverged. Gated by
internal_abi_needs_sret (false off win64) so linux/mac IR is unchanged.
The by-value closure call-type fix makes str-returning closures produce
correct results on win64. Three spec tests that failed only through that
path now match their linux baseline under cross-build + wine:

- spec_ss10_6_error_context (str-returning with_context closure)
- spec_ss10_5_sequence_traverse_transpose (fat-fn/closure str values)
- spec_ss14_3_1_no_suspend_blocks (async closure value return)

The remaining four #806 tests (option_result_combinators,
result_combinators_complete, error_trait, collect_targets) still fail on
win64 through a distinct data-flow path and stay skipped.
@ropoctl
ropoctl force-pushed the windows-806-closure-callabi branch from b683c92 to f553011 Compare August 18, 2026 02:58
@ropoctl
ropoctl merged commit f852fe7 into main Aug 18, 2026
3 checks passed
@ehartford
ehartford deleted the windows-806-closure-callabi branch September 24, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant