Skip to content

Commit 3ade98c

Browse files
committed
SILPrinter: add an option -sil-print-ownership to print the ownership of instruction results
With this option the ownership of instruction results is printed in the comments at the end of the line, e.g. ``` %3 = struct $S (%2, %1) // ownership: owned ``` And even without enabling this option, ownership comments are printed if the ownership of a result mismatches with its type. That can happen e.g. for non-trivial enums which are constructed with a trivial case: ``` enum E { case A case B(AnyObject) } %1 = enum $E, #E.A!enumelt // type of %1 is non trivial, but ownership is "none" ```
1 parent bf760aa commit 3ade98c

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

lib/SIL/IR/SILPrinter.cpp

Lines changed: 57 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());

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)