Skip to content

codegen(win64): materialize dyn-trait fat pointers, sync + async (#806) - #817

Merged
ropoctl merged 3 commits into
mainfrom
windows-codegen-806
Aug 17, 2026
Merged

ropoctl merged 3 commits into
mainfrom
windows-codegen-806

Conversation

@ropoctl

@ropoctl ropoctl commented Aug 17, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #816.

Makes win64 codegen materialize dyn Trait fat pointers across the indirect
ABI, fixing trait-object dispatch that previously produced wrong output on
Windows. Two parts:

  1. Sync dispatch — the fat-pointer materialization that un-skips
    sealed_trait_match (non-async match s: c: Circle => ... dispatch).
  2. Async dispatch — dyn_trait_method_fn_type applies the native win64
    ABI to vtable slots, which sret-lowers an aggregate return >8B. An async
    Task is {i32,ptr} (16B), so its slot becomes void(sret, ...). The
    vtable wrapper (create_dyn_wrapper) now carries that sret slot as its
    leading parameter, reconstructs the real Task type for the coroutine
    spawn, and stores the result through the sret slot — matching the vtable
    slot and the dispatch call site (D6: one ABI, agreed at every reader).
    Previously the wrapper seeded the Task aggregate from the sret-lowered
    void return (insertvalue void undef), an LLVM verify error that
    aborted win64 codegen for any async dyn-trait program.

On SysV the 16B Task returns in registers (no sret), so the async path is
byte-identical to before — linux/mac and the sync path are untouched.

Un-skips (now passing on win64): sealed_trait_match,
spec_ss11_5_dyn_async_trait_methods, spec_ss14_3_async_calling_is_unrestricted.

@ropoctl
ropoctl force-pushed the windows-codegen-806 branch 2 times, most recently from 46a0e29 to 2799a22 Compare August 17, 2026 16:42
@ropoctl
ropoctl changed the base branch from main to windows-unskip-799 August 17, 2026 16:42
@ropoctl
ropoctl force-pushed the windows-codegen-806 branch from 2799a22 to 382b7e8 Compare August 17, 2026 18:54
@ropoctl ropoctl changed the title codegen(win64): materialize dyn-trait fat pointers across the indirect ABI (#806) codegen(win64): materialize dyn-trait fat pointers (#806) — HELD, async regression Aug 17, 2026
ropoctl added a commit that referenced this pull request Aug 17, 2026
…806)

The #806 fat-pointer fix taught `dyn_trait_method_fn_type` to apply the
native win64 ABI to vtable slots. That correctly sret-lowers an aggregate
return >8B — but the `create_dyn_wrapper` vtable wrapper still built its
signature and return from the pre-ABI shape, so for an async method
(Task `{i32,ptr}`, 16B) the wrapper:

  - returned by value against a slot type that is now `void(sret, ...)`,
    shifting every argument by one, and
  - seeded the async Task aggregate from the sret-lowered `void` return,
    emitting `insertvalue void undef` — an LLVM verify error that aborted
    win64 codegen for any async dyn-trait program.

Detect the sret lowering (async method whose `dyn_ft` returns void with one
extra leading parameter), carry the sret slot as parameter 0, place
self/args after it, reconstruct the real `{i32,ptr}` Task type for the
spawn, and store the produced Task through the sret slot instead of
returning it by value. The wrapper signature now matches the vtable slot
and the dispatch call site (D6: one ABI, agreed at every reader).

On SysV the 16B Task returns in registers (no sret), so `has_sret` is false
and the wrapper is byte-identical to before — linux/mac and the sync
sealed-trait path are untouched. Un-holds #817:
`spec_ss11_5_dyn_async_trait_methods` and
`spec_ss14_3_async_calling_is_unrestricted` now pass on win64.
@ropoctl ropoctl changed the title codegen(win64): materialize dyn-trait fat pointers (#806) — HELD, async regression codegen(win64): materialize dyn-trait fat pointers, sync + async (#806) Aug 17, 2026
Base automatically changed from windows-unskip-799 to main August 17, 2026 20:18
…t ABI (#806)

On x86_64-pc-windows-msvc a `dyn Trait` value is a 16-byte fat pointer
`{data_ptr, vtable_ptr}` — larger than 8 bytes, so win64 classifies it as
an indirect-aggregate parameter (passed by address) and a `str`-returning
trait method as sret. Two call-lowering paths dropped the fat-pointer
construction under that ABI, so the callee read a fat pointer out of a raw
concrete's storage and dispatch/return went to garbage. The concrete →
`dyn Trait` unsize coercion must survive the physical passing mode.

Two fixes, both reading the single FnAbi rather than re-deriving per path:

- By-value `dyn Trait` parameter. `mir_emit_call_term`'s two
  indirect-aggregate spill branches (win64 byval-mask and the
  `internal_abi_needs_indirect_param` fallback) ran before the dyn
  fat-pointer build and short-circuited through the raw place pointer,
  passing `&Concrete` where the callee loads `{ptr, ptr}`. Extract
  `mir_build_dyn_arg_fat_value`, hoist the callee's dyn-parameter trait
  lookup above the byval branch, and when the parameter is a dyn trait,
  build the fat pointer and spill *that* to a temporary, passing the
  temporary's address.

- sret dyn dispatch. `dyn_trait_method_fn_type` rebuilt the call type from
  raw sema types with no ABI lowering, so a `str`-returning method
  dispatched as by-value against a vtable wrapper whose real ABI is
  `void(sret, self)`. Apply `internal_abi_needs_sret` /
  `internal_abi_needs_indirect_param` to the reconstructed type, and have
  `mir_emit_dyn_trait_call` materialize the sret destination, pass it as
  arg 0, and shift the remaining parameter indices.

Both predicates are false off win64, so host/mac codegen is unchanged —
verified byte-identical linux IR, and `with build` + `:fixpoint` +
`:drop-audit` + `:move-audit` stay green. Confirmed on Windows x86_64
hardware: `test/behavior/sealed_trait_match.w` prints `ok`, exit 0 (was
empty output before).
…ters materialize (#806)

The preceding commit makes win64 codegen materialize dyn-trait fat
pointers across the indirect ABI, which fixes the non-async
`match s: c: Circle => ... / r: Rect => ...` dispatch that
sealed_trait_match exercises.

KNOWN-RED (held): this same #806 fix regresses async dyn-trait dispatch —
spec_ss11_5_dyn_async_trait_methods and spec_ss14_3_async_calling_is_unrestricted
are green on main and fail with this fix. This PR stays red until the async
path is fixed; see #80. It must NOT merge ahead of that fix.
…806)

The #806 fat-pointer fix taught `dyn_trait_method_fn_type` to apply the
native win64 ABI to vtable slots. That correctly sret-lowers an aggregate
return >8B — but the `create_dyn_wrapper` vtable wrapper still built its
signature and return from the pre-ABI shape, so for an async method
(Task `{i32,ptr}`, 16B) the wrapper:

  - returned by value against a slot type that is now `void(sret, ...)`,
    shifting every argument by one, and
  - seeded the async Task aggregate from the sret-lowered `void` return,
    emitting `insertvalue void undef` — an LLVM verify error that aborted
    win64 codegen for any async dyn-trait program.

Detect the sret lowering (async method whose `dyn_ft` returns void with one
extra leading parameter), carry the sret slot as parameter 0, place
self/args after it, reconstruct the real `{i32,ptr}` Task type for the
spawn, and store the produced Task through the sret slot instead of
returning it by value. The wrapper signature now matches the vtable slot
and the dispatch call site (D6: one ABI, agreed at every reader).

On SysV the 16B Task returns in registers (no sret), so `has_sret` is false
and the wrapper is byte-identical to before — linux/mac and the sync
sealed-trait path are untouched. Un-holds #817:
`spec_ss11_5_dyn_async_trait_methods` and
`spec_ss14_3_async_calling_is_unrestricted` now pass on win64.
@ropoctl
ropoctl force-pushed the windows-codegen-806 branch from 7b433b7 to ef8bd59 Compare August 17, 2026 20:18
@ropoctl
ropoctl merged commit f091e9f into main Aug 17, 2026
3 checks passed
@ehartford
ehartford deleted the windows-codegen-806 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