Avoiding VWT copies when passing raw memory to unspecialized generic parameters

Hi everyone,

I've been working on a low-level dispatch bridge where I receive an UnsafeRawPointer and a metatype (Any.Type), and need to forward the underlying value to an unspecialized generic function:

func foo<T>(_ value: T) {
  print(T.self, type(of: value))
}

An argument to an unspecialized generic function foo<T>(_ value: T) is passed indirectly as @in_guaranteed T (a pointer in x0 and type metadata in x1).
Since the value is already initialized in memory at the raw pointer location, my goal is to pass it into foo<T> with zero copies. However, achieving this without triggering a defensive VWT copy has proven surprisingly tricky.

If I dereference the pointer naively:

func foo_thunk(_ value: UnsafeRawPointer, _ t: Any.Type) {
    func handleOpenedType<T>(_: T.Type) {
        let typedPtr = value.assumingMemoryBound(to: T.self)
        foo(typedPtr.pointee) // <--- Triggers VWT initializeWithCopy
    }
    _openExistential(t, do: handleOpenedType)
}
// with -emit-ir -O

define internal swiftcc void @"continueOpenedType #1 <A>(A.Type) -> () in output.foo_thunk(Swift.UnsafeRawPointer, Any.Type) -> ()"(ptr %0, ptr %T) unnamed_addr #3 !dbg !198 {
entry:
    #dbg_value(ptr %T, !203, !DIExpression(), !207)
  %1 = getelementptr inbounds i8, ptr %T, i64 -8, !dbg !208
  %T.valueWitnesses = load ptr, ptr %1, align 8, !dbg !208
  %2 = getelementptr inbounds nuw i8, ptr %T.valueWitnesses, i64 64, !dbg !208
  %size = load i64, ptr %2, align 8, !dbg !208
  %3 = alloca i8, i64 %size, align 16, !dbg !208
  call void @llvm.lifetime.start.p0(i64 -1, ptr nonnull %3), !dbg !208
    #dbg_value(ptr poison, !204, !DIExpression(), !212)
    #dbg_value(ptr %0, !206, !DIExpression(), !213)
  %4 = getelementptr inbounds nuw i8, ptr %T.valueWitnesses, i64 16, !dbg !214
  %InitializeWithCopy = load ptr, ptr %4, align 8, !dbg !214
  %5 = call ptr %InitializeWithCopy(ptr noalias nonnull %3, ptr noalias %0, ptr %T) #2, !dbg !214
  call swiftcc void @"output.foo<A>(A) -> ()"(ptr noalias nonnull %3, ptr %T), !dbg !215
  %6 = getelementptr inbounds nuw i8, ptr %T.valueWitnesses, i64 8, !dbg !216
  %Destroy = load ptr, ptr %6, align 8, !dbg !216
  call void %Destroy(ptr noalias nonnull %3, ptr %T) #2, !dbg !216
  call void @llvm.lifetime.end.p0(i64 -1, ptr nonnull %3), !dbg !216
  ret void, !dbg !216
}

The workaround I found uses the inout adapter trick:

func foo_thunk(_ value: UnsafeRawPointer, _ t: Any.Type) {
  func continueOpenedType<T>(_: T.Type) {
    let ptr = value.assumingMemoryBound(to: T.self)
    withValue(of: ptr) { value in
      foo(value)
    }
  }
  _openExistential(t, do: continueOpenedType)
}

@inline(__always)
func withValue<T, R>(of ptr: UnsafePointer<T>, _ f: (T) -> R) -> R {
  return { value in
    return f(value)
  }(&UnsafeMutablePointer(mutating: ptr).pointee)
}
// with -emit-ir -O

define hidden swiftcc void @"output.foo_thunk(Swift.UnsafeRawPointer, Any.Type) -> ()"(ptr %0, ptr %1) local_unnamed_addr #3 !dbg !183 {
entry:
  tail call swiftcc void @"output.foo<A>(A) -> ()"(ptr noalias %0, ptr %1), !dbg !251
  ret void, !dbg !252
}

Does anyone know if there is a cleaner solution?
Also, is there any way to prevent accesses via unsafeAddress (such as UnsafePointer.pointee) from requiring a copy in this context?