Skip to content

Commit 8d50f20

Browse files
authored
Merge pull request #85728 from eeckstein/print-sil-ownership
SIL: add an option `-sil-print-ownership` to print ownership
2 parents 88fc259 + 103d3c2 commit 8d50f20

File tree

3 files changed

+165
-2
lines changed

3 files changed

+165
-2
lines changed

lib/SIL/IR/SILPrinter.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ llvm::cl::opt<bool>
8686
SILPrintTypes("sil-print-types", llvm::cl::init(false),
8787
llvm::cl::desc("always print type annotations for instruction operands in SIL output"));
8888

89+
llvm::cl::opt<bool>
90+
SILPrintOwnership("sil-print-ownership", llvm::cl::init(false),
91+
llvm::cl::desc("print ownership of instruction results in SIL output"));
92+
8993
llvm::cl::opt<bool>
9094
SILPrintNoUses("sil-print-no-uses", llvm::cl::init(false),
9195
llvm::cl::desc("omit use comments in SIL output"));
@@ -693,6 +697,35 @@ class LineComments : public raw_ostream {
693697
}
694698
};
695699

700+
static bool hasNonAddressResults(const SILInstruction *inst) {
701+
for (SILValue result : inst->getResults()) {
702+
if (result->getType().isObject())
703+
return true;
704+
}
705+
return false;
706+
}
707+
708+
/// Returns true if the ownership of a result of `inst` mismatches with its type.
709+
/// That can happen e.g. for non-trivial enums which are constructed with a trivial case:
710+
/// ```
711+
/// enum E {
712+
/// case A
713+
/// case B(AnyObject)
714+
/// }
715+
///
716+
/// %1 = enum $E, #E.A!enumelt // type of %1 is non trivial, but ownership is "none"
717+
/// ```
718+
static bool hasUnusualResultOwnership(const SILInstruction *inst) {
719+
for (SILValue result : inst->getResults()) {
720+
if (result->getType().isObject() &&
721+
result->getOwnershipKind() == OwnershipKind::None &&
722+
!result->getType().isTrivial(*inst->getFunction())) {
723+
return true;
724+
}
725+
}
726+
return false;
727+
}
728+
696729
} // namespace
697730

698731
namespace swift {
@@ -1092,6 +1125,28 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
10921125
printBranchTargets(inst);
10931126
}
10941127

1128+
void printOwnershipOfInstruction(const SILInstruction *inst) {
1129+
1130+
if (!inst->isStaticInitializerInst() &&
1131+
inst->getFunction()->hasOwnership() &&
1132+
hasNonAddressResults(inst) &&
1133+
(SILPrintOwnership || hasUnusualResultOwnership(inst)))
1134+
{
1135+
lineComments.delim();
1136+
1137+
*this << "ownership: ";
1138+
llvm::interleave(inst->getResults(),
1139+
[&](SILValue result) {
1140+
if (result->getType().isAddress()) {
1141+
*this << '-';
1142+
} else {
1143+
*this << result->getOwnershipKind();
1144+
}
1145+
},
1146+
[&] { *this << ", "; });
1147+
}
1148+
}
1149+
10951150
void printUserList(ArrayRef<SILValue> values, SILNodePointer node) {
10961151
if (SILPrintNoUses)
10971152
return;
@@ -1364,6 +1419,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
13641419
// Print users, or id for valueless instructions.
13651420
printUsersOfInstruction(I);
13661421

1422+
printOwnershipOfInstruction(I);
1423+
13671424
// Print SIL location.
13681425
if (Ctx.printVerbose()) {
13691426
printSILLocation(I->getLoc(), I->getModule(), I->getDebugScope());
@@ -1965,6 +2022,23 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
19652022
}
19662023
}
19672024

2025+
template <typename Inst>
2026+
void printForwardingOwnershipKind(Inst *inst) {
2027+
if (inst->getNumRealOperands() == 0) {
2028+
return;
2029+
}
2030+
bool matching = false;
2031+
for (Operand *op : inst->getRealOperands()) {
2032+
if (inst->getForwardingOwnershipKind() == op->get()->getOwnershipKind()) {
2033+
matching = true;
2034+
break;
2035+
}
2036+
}
2037+
if (!matching) {
2038+
*this << ", forwarding: @" << inst->getForwardingOwnershipKind();
2039+
}
2040+
}
2041+
19682042
void visitStoreInst(StoreInst *SI) {
19692043
*this << Ctx.getID(SI->getSrc()) << " to ";
19702044
printStoreOwnershipQualifier(SI->getOwnershipQualifier());
@@ -2409,6 +2483,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24092483
SI->getElements(), [&](const SILValue &V) { *this << getIDAndType(V); },
24102484
[&] { *this << ", "; });
24112485
*this << ')';
2486+
printForwardingOwnershipKind(SI);
24122487
}
24132488

24142489
void visitObjectInst(ObjectInst *OI) {
@@ -2464,6 +2539,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24642539
[&] { *this << ", "; });
24652540
*this << ')';
24662541
}
2542+
printForwardingOwnershipKind(TI);
24672543
}
24682544

24692545
void visitTupleAddrConstructorInst(TupleAddrConstructorInst *TI) {

test/SIL/Parser/forwarding_ownership.sil

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt -sil-print-types %s | %FileCheck %s
1+
// RUN: %target-sil-opt %s | %FileCheck %s
22
sil_stage canonical
33

44
import Builtin
@@ -9,15 +9,26 @@ class Klass {
99
class SubKlass : Klass {
1010
}
1111

12+
enum E {
13+
case A
14+
case B(AnyObject)
15+
}
16+
17+
1218
struct NonTrivialStruct {
1319
var id:Klass
1420
}
1521

22+
struct S {
23+
var i: Int
24+
var e: E
25+
}
26+
1627
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_test :
1728
sil [ossa] @unchecked_ref_cast_test : $@convention(thin) <T> (@in T, @owned Klass) -> () {
1829
bb0(%0 : $*T, %1 : @owned $Klass):
1930
destroy_addr %0 : $*T
20-
// CHECK: unchecked_ref_cast %1 : $Klass to $Optional<Klass>, forwarding: @unowned
31+
// CHECK: unchecked_ref_cast %1 to $Optional<Klass>, forwarding: @unowned
2132
%3 = unchecked_ref_cast %1 : $Klass to $Optional<Klass>, forwarding: @unowned
2233
%4 = copy_value %3 : $Optional<Klass>
2334
destroy_value %1 : $Klass
@@ -38,6 +49,26 @@ bb0(%0 : @owned $Klass):
3849
}
3950
// CHECK-LABEL: } // end sil function 'struct_test'
4051

52+
// CHECK-LABEL: sil [ossa] @none_to_owned_struct :
53+
// CHECK: %2 = struct $S (%0, %1), forwarding: @owned
54+
// CHECK-LABEL: } // end sil function 'none_to_owned_struct'
55+
sil [ossa] @none_to_owned_struct : $@convention(thin) (Int) -> @owned S {
56+
bb0(%0 : $Int):
57+
%1 = enum $E, #E.A!enumelt
58+
%2 = struct $S (%0, %1), forwarding: @owned
59+
return %2
60+
}
61+
62+
// CHECK-LABEL: sil [ossa] @none_to_owned_tuple :
63+
// CHECK: %2 = tuple (%0, %1), forwarding: @owned
64+
// CHECK-LABEL: } // end sil function 'none_to_owned_tuple'
65+
sil [ossa] @none_to_owned_tuple : $@convention(thin) (Int) -> @owned (Int, E) {
66+
bb0(%0 : $Int):
67+
%1 = enum $E, #E.A!enumelt
68+
%2 = tuple (%0, %1), forwarding: @owned
69+
return %2
70+
}
71+
4172
// CHECK-LABEL: sil [ossa] @switch_test : $@convention(thin) () -> () {
4273
// CHECK: switch_enum {{.*}}, forwarding: @guaranteed
4374
// CHECK-LABEL: } // end sil function 'switch_test'

test/SIL/print_ownership.sil

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// RUN: %target-sil-opt -sil-print-ownership %s | %FileCheck %s --check-prefix=CHECK --check-prefix=ENABLE
2+
// RUN: %target-sil-opt %s | %FileCheck %s --check-prefix=CHECK --check-prefix=DISABLE
3+
4+
sil_stage canonical
5+
6+
import Builtin
7+
import Swift
8+
import SwiftShims
9+
10+
enum E {
11+
case A
12+
case B(AnyObject)
13+
}
14+
15+
struct S {
16+
var e: E
17+
var o: AnyObject
18+
}
19+
20+
struct T {
21+
var s: S
22+
}
23+
24+
// CHECK-LABEL: sil [ossa] @ossa_function :
25+
// CHECK: enum $E{{.*}} ownership: none
26+
// ENABLE: struct $S{{.*}} ownership: owned
27+
// ENABLE: begin_apply {{.*}} ownership: -, guaranteed
28+
// DISABLE-NOT: ownership
29+
// CHECK: } // end sil function 'ossa_function'
30+
sil [ossa] @ossa_function : $@convention(thin) (@inout T, @owned AnyObject) -> () {
31+
bb0(%0 : $*T, %1 : @owned $AnyObject):
32+
%2 = enum $E, #E.A!enumelt
33+
%3 = struct $S (%2, %1)
34+
%4 = struct_element_addr %0, #T.s
35+
store %3 to [assign] %4
36+
(%6, %7) = begin_apply undef() : $@yield_once () -> (@yields @in_guaranteed AnyObject)
37+
end_apply %7 as $()
38+
%9 = tuple ()
39+
return %9
40+
}
41+
42+
// CHECK-LABEL: sil @non_ossa_function :
43+
// CHECK-NOT: ownership
44+
// CHECK: } // end sil function 'non_ossa_function'
45+
sil @non_ossa_function : $@convention(thin) (@inout T, @owned AnyObject) -> () {
46+
bb0(%0 : $*T, %1 : $AnyObject):
47+
%2 = enum $E, #E.A!enumelt
48+
%3 = struct $S (%2, %1)
49+
%4 = struct_element_addr %0, #T.s
50+
store %3 to %4
51+
(%6, %7) = begin_apply undef() : $@yield_once () -> (@yields @in_guaranteed AnyObject)
52+
end_apply %7 as $()
53+
%9 = tuple ()
54+
return %9
55+
}
56+

0 commit comments

Comments
 (0)