Skip to content

mir-lower: legalize AS3 slice fat-pointer casts in SSA #1060

Closed

Description

@uurl

Context

#883 identifies a correctness gap in emit_pointer_cast: aggregate-valued
pointer casts can currently fall through to the memory transmute path, which
may round-trip raw addrspace(3) pointer bytes through a stack slot.

For slice fat pointers, this can be legalized without observing the physical
representation of the shared pointer.

A slice pointer has the semantic shape:

{ data pointer, length }

The data pointer can be converted explicitly between address spaces while the
metadata is preserved unchanged.

Proposed lowering

For semantic slice fat-pointer casts:

{ src_data, len }
    |
    | extractvalue
    v
src_data
    |
    | addrspacecast, when address spaces differ
    v
dst_data

result = { dst_data, len }

The aggregate should be reconstructed directly in SSA using
extractvalue / insertvalue.

The lowering must not use:

alloca
store
load
ptrtoint
inttoptr

for the fat-pointer representation conversion.

Scope

Keep this legalization intentionally narrow:

  • semantic pointer casts only
  • slice-shaped fat pointers only
  • exactly two fields
  • field 0 is a pointer
  • field 1 is integer metadata
  • source and destination metadata types must match
  • preserve existing behavior for arbitrary aggregates
  • do not extend aggregate Transmute
  • do not generalize this to cluster-shared addrspace(7)

Trait-object-like { ptr, ptr } values must not enter this path.

Acceptance criteria

The lowering should:

  • legalize shared-to-generic slice fat-pointer casts
  • legalize generic-to-shared slice fat-pointer casts
  • handle same-address-space slice casts without a memory round-trip
  • preserve slice metadata exactly
  • emit an addrspacecast only for the data-pointer field when required
  • reconstruct the destination aggregate in SSA
  • emit no temporary alloca / store / load sequence

Add lowering tests covering the SSA shape and extend the
shared_ptr_transmute GPU regression to validate that:

  • the slice length is preserved
  • the resulting data pointer denotes the original shared allocation
  • stores through the recast slice are visible through the original shared storage

Related: #883

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions