Fix some fallout.

This commit is contained in:
Gabor Horvath
2025-10-06 15:25:23 +01:00
parent 5a63808235
commit 8b3b16cb5b
6 changed files with 132 additions and 7 deletions

View File

@@ -408,13 +408,17 @@ bool OwnershipModelEliminatorVisitor::visitApplyInst(ApplyInst *ai) {
// Insert a retain for unowned results.
SILBuilderWithScope builder(ai->getNextInstruction(), builderCtx);
builder.emitDestructureValueOperation(
ai->getLoc(), ai, [&](unsigned idx, SILValue v) {
auto resultsIt = fnConv.getDirectSILResults().begin();
if (resultsIt->getConvention() == ResultConvention::Unowned)
builder.emitCopyValueOperation(ai->getLoc(), v);
++resultsIt;
});
auto resultIt = fnConv.getDirectSILResults().begin();
auto copyValue = [&](unsigned idx, SILValue v) {
auto result = *resultIt;
if (result.getConvention() == ResultConvention::Unowned)
builder.emitCopyValueOperation(ai->getLoc(), v);
++resultIt;
};
if (fnConv.getNumDirectSILResults() == 1)
copyValue(0, ai);
else
builder.emitDestructureValueOperation(ai->getLoc(), ai, copyValue);
return changed;
}

View File

@@ -9,6 +9,20 @@ import Foundation
@objc(XX) protocol XX {
}
// CHECK-LABEL: sil [ossa] @cse_objc_protocol :
// CHECK: objc_protocol #XX : $Protocol
// CHECK-NOT: objc_protocol
// CHECK: tuple (%0 : $Protocol, %0 : $Protocol)
// CHECK-LABEL: } // end sil function 'cse_objc_protocol'
sil [ossa] @cse_objc_protocol : $@convention(thin) () -> @owned (Protocol, Protocol) {
bb0:
%0 = objc_protocol #XX : $Protocol
%1 = objc_protocol #XX : $Protocol
%2 = tuple (%0: $Protocol, %1: $Protocol)
%3 = copy_value %2
return %3
}
@objc protocol Walkable {
func walk()
}

View File

@@ -695,6 +695,20 @@ bb0(%0 : $*Builtin.Int8):
return %4 : $Builtin.Int64
}
// CHECK-LABEL: sil [ossa] @cse_raw_pointer_to_ref :
// CHECK: raw_pointer_to_ref
// CHECK-NOT: raw_pointer_to_ref
// CHECK: tuple
// CHECK-LABEL: } // end sil function 'cse_raw_pointer_to_ref'
sil [ossa] @cse_raw_pointer_to_ref : $@convention(thin) (Builtin.RawPointer) -> @owned (C, C) {
bb0(%0 : $Builtin.RawPointer):
%1 = raw_pointer_to_ref %0 : $Builtin.RawPointer to $C
%2 = raw_pointer_to_ref %0 : $Builtin.RawPointer to $C
%6 = tuple(%1: $C, %2: $C)
%7 = copy_value %6
return %7
}
// CHECK-LABEL: sil [ossa] @cse_unchecked_addr_cast :
// CHECK: unchecked_addr_cast
// CHECK-NOT: unchecked_addr_cast

View File

@@ -302,6 +302,57 @@ bbExitBlock(%result : @owned $FakeOptional<Klass>):
return %result : $FakeOptional<Klass>
}
// CHECK-LABEL: sil [ossa] @owned_to_guaranteed_rauw_2 : $@convention(thin) (@guaranteed Klass) -> @owned (Klass, Klass) {
// CHECK: bb0(
// CHECK-NEXT: tuple
// CHECK-NEXT: copy_value
// CHECK-NEXT: return
// CHECK: } // end sil function 'owned_to_guaranteed_rauw_2'
sil [ossa] @owned_to_guaranteed_rauw_2 : $@convention(thin) (@guaranteed Klass) -> @owned (Klass, Klass) {
bb0(%0 : @guaranteed $Klass):
%1 = unchecked_bitwise_cast %0 : $Klass to $SubKlass
%2 = unchecked_bitwise_cast %1 : $SubKlass to $Klass
%3 = tuple(%2 : $Klass, %2 : $Klass)
%4 = copy_value %3
return %4 : $(Klass, Klass)
}
// CHECK-LABEL: sil [ossa] @owned_to_guaranteed_rauw_2a : $@convention(thin) (@guaranteed Builtin.NativeObject) -> @owned (Klass, Klass) {
// CHECK: bb0(
// CHECK-NEXT: unchecked_ref_cast
// CHECK-NEXT: tuple
// CHECK-NEXT: copy_value
// CHECK-NEXT: return
// CHECK: } // end sil function 'owned_to_guaranteed_rauw_2a'
sil [ossa] @owned_to_guaranteed_rauw_2a : $@convention(thin) (@guaranteed Builtin.NativeObject) -> @owned (Klass, Klass) {
bb0(%0 : @guaranteed $Builtin.NativeObject):
%0a = unchecked_ref_cast %0 : $Builtin.NativeObject to $Klass
%1 = unchecked_bitwise_cast %0a : $Klass to $SubKlass
%2 = unchecked_bitwise_cast %1 : $SubKlass to $Klass
%3 = tuple(%2 : $Klass, %2 : $Klass)
%4 = copy_value %3
return %4 : $(Klass, Klass)
}
// We need the unchecked_ownership_conversion since our base value is
// guaranteed, not a function argument, and our user is a function exiting
// terminator.
//
// CHECK-LABEL: sil [ossa] @owned_to_guaranteed_rauw_2b : $@convention(thin) (@guaranteed Builtin.NativeObject) -> @owned Klass {
// CHECK: bb0(
// CHECK-NEXT: unchecked_ref_cast
// CHECK-NEXT: copy_value
// CHECK-NEXT: return
// CHECK: } // end sil function 'owned_to_guaranteed_rauw_2b'
sil [ossa] @owned_to_guaranteed_rauw_2b : $@convention(thin) (@guaranteed Builtin.NativeObject) -> @owned Klass {
bb0(%0 : @guaranteed $Builtin.NativeObject):
%0a = unchecked_ref_cast %0 : $Builtin.NativeObject to $Klass
%1 = unchecked_bitwise_cast %0a : $Klass to $SubKlass
%2 = unchecked_bitwise_cast %1 : $SubKlass to $Klass
%3 = copy_value %2
return %3 : $Klass
}
// CHECK-LABEL: sil [ossa] @unowned_to_guaranteed_rauw_2_loop : $@convention(thin) (@guaranteed Klass) -> @owned FakeOptional<(Klass, Klass)> {
// CHECK: bb0([[ARG:%.*]] : @guaranteed $Klass):
// CHECK-NOT: unchecked_bitwise_cast
@@ -358,6 +409,19 @@ bbExitBlock(%result : @owned $FakeOptional<(Klass, Klass)>):
return %result : $FakeOptional<(Klass, Klass)>
}
// CHECK-LABEL: sil [ossa] @owned_to_guaranteed_rauw_3 : $@convention(thin) (@guaranteed Klass) -> @owned Klass {
// CHECK: bb0(
// CHECK-NEXT: copy_value
// CHECK-NEXT: return
// CHECK: } // end sil function 'owned_to_guaranteed_rauw_3'
sil [ossa] @owned_to_guaranteed_rauw_3 : $@convention(thin) (@guaranteed Klass) -> @owned Klass {
bb0(%0 : @guaranteed $Klass):
%1 = unchecked_bitwise_cast %0 : $Klass to $SubKlass
%2 = unchecked_bitwise_cast %1 : $SubKlass to $Klass
%3 = copy_value %2
return %3 : $Klass
}
//===---
// Guaranteed Tests
//

View File

@@ -37,6 +37,17 @@ bb0(%0 : $@thick T.Type):
return %1
}
// CHECK-LABEL: sil [ossa] @test_non_metatype :
// CHECK: %1 = unconditional_checked_cast %0
// CHECK-NEXT: %2 = copy_value %1
// CHECK-NEXT: return %2
// CHECK: } // end sil function 'test_non_metatype'
sil [ossa] @test_non_metatype : $@convention(thin) (@guaranteed C) -> @owned any PC {
bb0(%0 : @guaranteed $C):
%1 = unconditional_checked_cast %0 to any PC
%2 = copy_value %1
return %2
}
// CHECK-LABEL: sil [ossa] @test_non_existential_target :
// CHECK: %1 = unconditional_checked_cast %0

View File

@@ -17,3 +17,21 @@ bb0(%0 : $Builtin.Int64):
%4 = value_to_bridge_object %3 : $Builtin.Int64
return %4 : $Builtin.BridgeObject
}
// CHECK-LABEL: sil [ossa] @keep_both_vtbo_instructions
// CHECK: %1 = value_to_bridge_object %0
// CHECK: %2 = value_to_bridge_object %0
// CHECK: %3 = tuple (%1 : $Builtin.BridgeObject, %2 : $Builtin.BridgeObject)
// CHECK: %4 = copy_value %3
// CHECK: return %4
// CHECK: } // end sil function 'keep_both_vtbo_instructions'
sil [ossa] @keep_both_vtbo_instructions : $@convention(thin) (Builtin.Int64) -> @owned (Builtin.BridgeObject, Builtin.BridgeObject) {
bb0(%0 : $Builtin.Int64):
%1 = value_to_bridge_object %0 : $Builtin.Int64
%2 = unchecked_trivial_bit_cast %1 : $Builtin.BridgeObject to $UInt64
%3 = struct_extract %2 : $UInt64, #UInt64._value
%4 = value_to_bridge_object %3 : $Builtin.Int64
%5 = tuple (%1 : $Builtin.BridgeObject, %4 : $Builtin.BridgeObject)
%6 = copy_value %5
return %6
}