Skip to content

Commit c1b9ff7

Browse files
committed
[sil-opaque-values] Fix FunctionSigOpts for opaque owned-to-guaranteed.
Do not create destroy_addr for opaque values. Do not create strong_retain/release for opaque types.
1 parent 30e3062 commit c1b9ff7

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

lib/SILOptimizer/Transforms/FunctionSignatureOpts.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -967,18 +967,17 @@ OwnedToGuaranteedFinalizeThunkFunction(SILBuilder &Builder, SILFunction *F) {
967967

968968
static void createArgumentRelease(SILBuilder &Builder, ArgumentDescriptor &AD) {
969969
auto &F = Builder.getFunction();
970-
if (AD.PInfo->getConvention() == ParameterConvention::Direct_Owned) {
971-
Builder.createReleaseValue(RegularLocation(SourceLoc()),
972-
F.getArguments()[AD.Index],
973-
Builder.getDefaultAtomicity());
974-
return;
975-
}
976-
if (AD.PInfo->getConvention() == ParameterConvention::Indirect_In) {
970+
SILArgument *Arg = F.getArguments()[AD.Index];
971+
if (Arg->getType().isAddress()) {
972+
assert(AD.PInfo->getConvention() == ParameterConvention::Indirect_In
973+
&& F.getConventions().useLoweredAddresses());
977974
Builder.createDestroyAddr(RegularLocation(SourceLoc()),
978975
F.getArguments()[AD.Index]);
979976
return;
980977
}
981-
llvm_unreachable("Parameter convention is not supported");
978+
Builder.createReleaseValue(RegularLocation(SourceLoc()),
979+
F.getArguments()[AD.Index],
980+
Builder.getDefaultAtomicity());
982981
}
983982

984983
/// Set up epilogue work for the thunk arguments based in the given argument.

lib/SILOptimizer/Utils/Local.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ swift::createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
5353

5454
// If Ptr is refcounted itself, create the strong_retain and
5555
// return.
56-
if (Ptr->getType().isReferenceCounted(B.getModule()))
56+
auto &TL = B.getModule().getTypeLowering(Ptr->getType());
57+
if (!TL.isAddressOnly() && TL.isReferenceCounted())
5758
return B.createStrongRetain(Loc, Ptr, B.getDefaultAtomicity());
5859

5960
// Otherwise, create the retain_value.
@@ -73,7 +74,8 @@ swift::createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
7374
auto Loc = RegularLocation(SourceLoc());
7475

7576
// If Ptr has reference semantics itself, create a strong_release.
76-
if (Ptr->getType().isReferenceCounted(B.getModule()))
77+
auto &TL = B.getModule().getTypeLowering(Ptr->getType());
78+
if (!TL.isAddressOnly() && TL.isReferenceCounted())
7779
return B.createStrongRelease(Loc, Ptr, B.getDefaultAtomicity());
7880

7981
// Otherwise create a release value.

test/SILOptimizer/funcsig_opaque.sil

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %target-sil-opt -enable-sil-opaque-values -assume-parsing-unqualified-ownership-sil -function-signature-opts %s | %FileCheck %s
2+
3+
import Builtin
4+
5+
struct R<T> {
6+
var base: T
7+
}
8+
9+
struct S {
10+
var val: Builtin.Int32
11+
}
12+
13+
sil @testAggregateArgHelper : $@convention(thin) (@owned R<S>) -> ()
14+
15+
// Test owned-to-guaranteed transformation of opaque arguments.
16+
//
17+
// CHECK-LABEL: sil [thunk] [always_inline] @testAggregateArg : $@convention(thin) (@in R<S>) -> @out () {
18+
// CHECK: bb0(%0 : $R<S>):
19+
// CHECK: [[F:%.*]] = function_ref @_T016testAggregateArgTf4g_n : $@convention(thin) (@in_guaranteed R<S>) -> @out ()
20+
// CHECK: [[CALL:%.*]] = apply [[F]](%0) : $@convention(thin) (@in_guaranteed R<S>) -> @out ()
21+
// CHECK: release_value %0 : $R<S>
22+
// CHECK: return [[CALL]] : $()
23+
// CHECK-LABEL: } // end sil function 'testAggregateArg'
24+
//
25+
// CHECK-LABEL: sil shared @_T016testAggregateArgTf4g_n : $@convention(thin) (@in_guaranteed R<S>) -> @out () {
26+
// CHECK: bb0(%0 : $R<S>):
27+
// CHECK: [[COPY:%.*]] = copy_value %0 : $R<S>
28+
// CHECK: retain_value %0 : $R<S>
29+
// CHECK: [[F:%.*]] = function_ref @testAggregateArgHelper : $@convention(thin) (@owned R<S>) -> ()
30+
// CHECK: [[CALL:%.*]] = apply [[F]]([[COPY]]) : $@convention(thin) (@owned R<S>) -> ()
31+
// CHECK: destroy_value %0 : $R<S>
32+
// CHECK: return %{{.*}} : $()
33+
// CHECK-LABEL: } // end sil function '_T016testAggregateArgTf4g_n'
34+
sil @testAggregateArg : $@convention(thin) (@in R<S>) -> @out () {
35+
bb0(%0 : $R<S>):
36+
%9 = copy_value %0 : $R<S>
37+
retain_value %0 : $R<S>
38+
%10 = function_ref @testAggregateArgHelper : $@convention(thin) (@owned R<S>) -> ()
39+
%12 = apply %10(%9) : $@convention(thin) (@owned R<S>) -> ()
40+
destroy_value %0 : $R<S>
41+
release_value %0 : $R<S>
42+
%1284 = tuple ()
43+
return %1284 : $()
44+
}

0 commit comments

Comments
 (0)