Skip to content

codegen(win64): sret-lower dyn-trait wrappers for sync aggregate returns (#806) - #820

Merged
ropoctl merged 2 commits into
windows-806-closure-callabifrom
windows-806-error-trait
Aug 18, 2026
Merged

ropoctl merged 2 commits into
windows-806-closure-callabifrom
windows-806-error-trait

Conversation

@ropoctl

@ropoctl ropoctl commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

#806 — win64 dyn-trait sret ABI for sync aggregate returns

&dyn Error.source() returns Option[&dyn Error] — a 24-byte fat aggregate
{ i32, { ptr, ptr } }. On win64 an aggregate larger than 8 bytes is returned
via an implicit sret pointer, so the call site emits
call void %slot(ptr %sret, ptr %self). But the vtable wrapper
__dynwrap_ParseError_source was built from a second, independent ABI
derivation that returned the aggregate by value ({i32,{ptr,ptr}} @wrap(ptr %self)).
Producer and consumer disagreed: the wrapper read the caller's sret pointer as
self → stack-frame corruption → the program produced no output and exited 0.

This is the same D6 class as the dispatch-vs-vtable divergence: one function
signature must have exactly one ABI descriptor.

Fix

All three vtable-build sites in CodegenTraits.w now read the same
dyn_trait_method_fn_type descriptor that the call site
(mir_emit_dyn_trait_call) reads, so producer and consumer agree by
construction. create_dyn_wrapper detects sret structurally
(ret_ty == void && dyn_param_count == orig_param_count + 1) instead of gating
on the async flag, so a sync fat-aggregate return is sret-lowered too.

A report_errors=false variant of the descriptor lets a non-dispatchable slot
(one whose signature mentions Self, e.g. Ord.cmp) fall back to the real
method's by-value type without failing compilation — matching the prior
tolerant behavior for those slots.

Verification

Cross-built each affected test to win64 and ran under wine:

Instruction-level: the wrapper went from
define {i32,{ptr,ptr}} @__dynwrap_ParseError_source(ptr %0) to
define void @__dynwrap_ParseError_source(ptr %0, ptr %1), matching the caller
call void %6(ptr %1, ptr %3).

Un-skips spec_ss10_8_error_trait.

…rns (#806)

A vtable wrapper for a trait method whose return is a >8B aggregate was
built to return by value unless the method was async, while the dyn call
site (`mir_emit_dyn_trait_call`) always reconstructs the slot type with
`dyn_trait_method_fn_type`, which applies the win64 native ABI and sret-
lowers such a return (void ret + leading sret pointer). So a sync method
like `Error.source() -> Option[&dyn Error]` (a 24-byte `{i32,{ptr,ptr}}`)
got a by-value wrapper `{i32,{ptr,ptr}} @__dynwrap_..._source(ptr self)`
against a caller emitting `call void %slot(ptr sret, ptr self)`: the
wrapper reads the sret pointer as self and corrupts the frame — the exact
caller/callee ABI divergence D6 forbids.

Fix: the three vtable-build sites now read the same `dyn_trait_method_fn_type`
descriptor the call site reads, so producer and consumer agree, and
`create_dyn_wrapper` detects the sret slot structurally (void return with
one extra leading param vs the real method) so it fires for sync aggregate
returns, not only async Task. A method whose signature mentions Self (e.g.
`Ord.cmp`, never dynamically dispatchable) can't be lowered by
`dyn_trait_method_fn_type`; a new `report_errors=false` variant lets those
slots fall back to the real method's by-value type as a placeholder without
failing the whole compilation.
The win64 dyn-trait sret-lowering fix in the previous commit makes
&dyn Error.source() (returning the 24-byte fat Option[&dyn Error])
produce correct output on Windows. Remove the skip-windows gate.
@ropoctl
ropoctl force-pushed the windows-806-error-trait branch from b726643 to 21b0a91 Compare August 18, 2026 02:58
@ropoctl
ropoctl merged commit cbeb413 into main Aug 18, 2026
3 of 4 checks passed
@ehartford
ehartford deleted the windows-806-error-trait 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