Skip to content

Commit

Permalink
[IRGen] Fix RecordTypeInfoImpl::initWithTake.
Browse files Browse the repository at this point in the history
Apply the same control-flow changes to the method that were applied on
main to avoid duplicated implementations one of which crashes with

```
Metadata for archetype not bound in function.
  The metadata could be missing entirely because it needs to be passed to the function.
  Or the metadata is present and not bound in which case setScopedLocalTypeMetadata or similar must be called.
Archetype without metadata: Value
(primary_archetype_type address=0x14207ebe0 conforms_to="Swift.(file).Escapable" name="Value"
  (interface_type=generic_type_param_type depth=0 index=0 decl="Synchronization.(file)._Cell.Value@/Volumes/ams1_ext4/Submit/SWE/DevTools/swift/6_0_0_6/swift/stdlib/public/Synchronization/Cell.swift:19:23"))
Function:
; Function Attrs: noinline nounwind
define linkonce_odr hidden ptr @"$s15Synchronization5_CellVyxGRi_zlWOb"(ptr %0, ptr %1, ptr %2) swiftlang#14 !dbg !8 {
  %4 = alloca i1, align 1
}
LocalTypeData:
(0x14207f5f8 (_Cell<Value>), FormalTypeMetadata) => [
  (universal) concrete: 0x600003d1add0
    ptr %2
]
```

when building Synchronization.
  • Loading branch information
nate-chandler committed Jun 26, 2024
1 parent 54898ce commit 7fcbedd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/IRGen/GenRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,7 @@ class RecordTypeInfoImpl : public Base,
// If the fields are not ABI-accessible, use the value witness table.
if (!AreFieldsABIAccessible) {
return emitInitializeWithTakeCall(IGF, T, dest, src);
}

if (auto rawLayout = T.getRawLayout()) {
} else if (auto rawLayout = T.getRawLayout()) {
// Because we have a rawlayout attribute, we know this has to be a struct.
auto structDecl = T.getStructOrBoundGenericStruct();

Expand All @@ -292,9 +290,7 @@ class RecordTypeInfoImpl : public Base,
isOutlined);
}
}
}

if (isOutlined || T.hasParameterizedExistential()) {
} else if (isOutlined || T.hasParameterizedExistential()) {
auto offsets = asImpl().getNonFixedOffsets(IGF, T);
for (auto &field : getFields()) {
if (field.isEmpty())
Expand Down
54 changes: 54 additions & 0 deletions test/IRGen/moveonly_value_functions.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: %target-swift-emit-irgen -O \
// RUN: -parse-sil \
// RUN: -disable-type-layout \
// RUN: %s \
// RUN: -enable-experimental-feature RawLayout \
// RUN: | \
// RUN: %IRGenFileCheck %s

import Builtin
import Swift

@usableFromInline
@frozen @_rawLayout(like: Value, movesAsLike) internal struct _Cell<Value> : ~Copyable where Value : ~Copyable {
@_alwaysEmitIntoClient @inlinable deinit
}

sil @$sSp12deinitialize5countSvSi_tF : $@convention(method) <τ_0_0 where τ_0_0 : ~Copyable> (Int, UnsafeMutablePointer<τ_0_0>) -> UnsafeMutableRawPointer

// CHECK-LABEL: define swiftcc void @"$s15Synchronization5_CellVAARi_zrlEfD"(
// CHECK-SAME: ptr %"_Cell<Value>",
// CHECK-SAME: ptr noalias swiftself [[SOURCE:%[0-9]+]]
// CHECK-SAME: )
// CHECK-SAME: {
// CHECK: entry:
// CHECK: [[CELL_VWT_ADDR:%[^,]+]] = getelementptr inbounds ptr, ptr %"_Cell<Value>", i64 -1
// CHECK: %"_Cell<Value>.valueWitnesses" = load ptr, ptr [[CELL_VWT_ADDR]]
// CHECK: [[CELL_SIZE_ADDR:%[^,]+]] = getelementptr inbounds %swift.vwtable, ptr %"_Cell<Value>.valueWitnesses", i32 0, i32 8
// CHECK: %size = load i64, ptr [[CELL_SIZE_ADDR]]
// CHECK: [[DEST:%[^,]+]] = alloca i8, i64 %size
// CHECK: call void @llvm.lifetime.start.p0(i64 -1, ptr [[DEST]])
// CHECK: [[VALUE_METADATA_ADDR:%[^,]+]] = getelementptr inbounds ptr, ptr %"_Cell<Value>", i64 2
// CHECK: %Value = load ptr, ptr [[VALUE_METADATA_ADDR]]
// CHECK: [[VALUE_VWT_ADDR:%[^,]+]] = getelementptr inbounds ptr, ptr %Value, i64 -1
// CHECK: %Value.valueWitnesses = load ptr, ptr [[VALUE_VWT_ADDR]]
// CHECK: [[VALUE_INIT_WITH_TAKE_ADDR:%[^,]+]] = getelementptr inbounds ptr, ptr %Value.valueWitnesses, i32 4
// CHECK: %InitializeWithTake = load ptr, ptr [[VALUE_INIT_WITH_TAKE_ADDR]]
// CHECK: call ptr %InitializeWithTake(ptr noalias [[DEST]], ptr noalias [[SOURCE]], ptr %Value)
// CHECK: call swiftcc ptr @"$sSp12deinitialize5countSvSi_tF"(i64 1, ptr [[DEST]], ptr %Value)
// CHECK: call void @llvm.lifetime.end.p0(i64 -1, ptr [[DEST]])
// CHECK: }
sil @$s15Synchronization5_CellVAARi_zrlEfD : $@convention(method) <Value where Value : ~Copyable> (@in _Cell<Value>) -> () {
bb0(%0 : $*_Cell<Value>):
%1 = alloc_stack $_Cell<Value>
copy_addr [take] %0 to [init] %1 : $*_Cell<Value>
%7 = builtin "addressOfRawLayout"<_Cell<Value>>(%1 : $*_Cell<Value>) : $Builtin.RawPointer
%8 = struct $UnsafeMutablePointer<Value> (%7 : $Builtin.RawPointer)
%9 = integer_literal $Builtin.Int64, 1
%10 = struct $Int (%9 : $Builtin.Int64)
%11 = function_ref @$sSp12deinitialize5countSvSi_tF : $@convention(method) <τ_0_0 where τ_0_0 : ~Copyable> (Int, UnsafeMutablePointer<τ_0_0>) -> UnsafeMutableRawPointer
%12 = apply %11<Value>(%10, %8) : $@convention(method) <τ_0_0 where τ_0_0 : ~Copyable> (Int, UnsafeMutablePointer<τ_0_0>) -> UnsafeMutableRawPointer
%13 = tuple ()
dealloc_stack %1 : $*_Cell<Value>
return %13 : $()
}

0 comments on commit 7fcbedd

Please sign in to comment.