Skip to content

Commit fa445a6

Browse files
committed
Create copy_value and compensating destroys for owned concrete values.
1 parent 08c1974 commit fa445a6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ SILInstruction *SILCombiner::createApplyWithConcreteType(
11251125
// Create the new set of arguments to apply including their substitutions.
11261126
SubstitutionMap NewCallSubs = Apply.getSubstitutionMap();
11271127
SmallVector<SILValue, 8> NewArgs;
1128+
SmallVector<SILValue, 8> valuesToClean;
11281129
unsigned ArgIdx = 0;
11291130
// Push the indirect result arguments.
11301131
for (unsigned EndIdx = Apply.getSubstCalleeConv().getSILArgIndexOfFirstParam();
@@ -1186,7 +1187,15 @@ SILInstruction *SILCombiner::createApplyWithConcreteType(
11861187
concreteArgCopies.push_back(*argSub);
11871188
NewArgs.push_back(argSub->tempArg);
11881189
} else {
1189-
NewArgs.push_back(CEI.ConcreteValue);
1190+
auto argValue = CEI.ConcreteValue;
1191+
if (argValue->getOwnershipKind() == OwnershipKind::Owned) {
1192+
SILBuilderWithScope copyBuilder(argValue->getNextInstruction(),
1193+
BuilderCtx);
1194+
argValue = copyBuilder.createCopyValue(
1195+
argValue->getNextInstruction()->getLoc(), argValue);
1196+
valuesToClean.push_back(argValue);
1197+
}
1198+
NewArgs.push_back(argValue);
11901199
}
11911200

11921201
// Form a new set of substitutions where the argument is
@@ -1290,6 +1299,10 @@ SILInstruction *SILCombiner::createApplyWithConcreteType(
12901299
cleanupBuilder.createDestroyAddr(cleanupLoc, argCopy.origArg);
12911300
cleanupBuilder.createDeallocStack(cleanupLoc, argCopy.tempArg);
12921301
}
1302+
// Insert compensating destroys for owned values.
1303+
for (auto value : valuesToClean) {
1304+
cleanupBuilder.createDestroyValue(cleanupLoc, value);
1305+
}
12931306
}
12941307
return NewApply.getInstruction();
12951308
}

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,21 @@ bb0(%0 : @owned $Klass):
28602860
return %3 : $Builtin.NativeObject
28612861
}
28622862

2863+
// CHECK-LABEL: sil [ossa] @collapse_existential_pack_unpack_unchecked_ref_cast_owned4 :
2864+
// CHECK: apply {{.*}}<Klass>
2865+
// CHECK: } // end sil function 'collapse_existential_pack_unpack_unchecked_ref_cast_owned4'
2866+
sil [ossa] @collapse_existential_pack_unpack_unchecked_ref_cast_owned4 : $@convention(thin) (@owned Klass) -> @owned Builtin.NativeObject {
2867+
bb0(%0 : @owned $Klass):
2868+
%1 = init_existential_ref %0 : $Klass : $Klass, $AnyObject
2869+
%f1 = function_ref @use_anyobject_guaranteed : $@convention(thin) (@guaranteed AnyObject) -> ()
2870+
apply %f1(%1) : $@convention(thin) (@guaranteed AnyObject) -> ()
2871+
%2 = open_existential_ref %1 : $AnyObject to $@opened("7CAE06CE-5F10-11E4-AF13-C82A1428F987", AnyObject) Self
2872+
%f2 = function_ref @use_generic_obj_guaranteed : $@convention(thin) <τ_0_0> (@guaranteed τ_0_0) -> ()
2873+
apply %f2<@opened("7CAE06CE-5F10-11E4-AF13-C82A1428F987", AnyObject) Self>(%2) : $@convention(thin) <τ_0_0> (@guaranteed τ_0_0) -> ()
2874+
%3 = unchecked_ref_cast %2 : $@opened("7CAE06CE-5F10-11E4-AF13-C82A1428F987", AnyObject) Self to $Builtin.NativeObject
2875+
return %3 : $Builtin.NativeObject
2876+
}
2877+
28632878
// CHECK-LABEL: sil [ossa] @collapse_existential_pack_unpack_ref_to_raw_pointer
28642879
// CHECK: bb0([[Ref:%.*]] : @guaranteed $MyClass):
28652880
// CHECK-NOT: init_existential_ref

0 commit comments

Comments
 (0)