Skip to content

Commit 103d3c2

Browse files
committed
SILPrinter: print forwarding ownership for struct and tuple instructions
Print `forwarding: <ownership>` if the ownership of the result mismatches the operand ownership(s). We already do this for all other forwarding instructions, but `struct` and `tuple` were missing.
1 parent 3ade98c commit 103d3c2

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/SIL/IR/SILPrinter.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,23 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
20122012
}
20132013
}
20142014

2015+
template <typename Inst>
2016+
void printForwardingOwnershipKind(Inst *inst) {
2017+
if (inst->getNumRealOperands() == 0) {
2018+
return;
2019+
}
2020+
bool matching = false;
2021+
for (Operand *op : inst->getRealOperands()) {
2022+
if (inst->getForwardingOwnershipKind() == op->get()->getOwnershipKind()) {
2023+
matching = true;
2024+
break;
2025+
}
2026+
}
2027+
if (!matching) {
2028+
*this << ", forwarding: @" << inst->getForwardingOwnershipKind();
2029+
}
2030+
}
2031+
20152032
void visitStoreInst(StoreInst *SI) {
20162033
*this << Ctx.getID(SI->getSrc()) << " to ";
20172034
printStoreOwnershipQualifier(SI->getOwnershipQualifier());
@@ -2456,6 +2473,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
24562473
SI->getElements(), [&](const SILValue &V) { *this << getIDAndType(V); },
24572474
[&] { *this << ", "; });
24582475
*this << ')';
2476+
printForwardingOwnershipKind(SI);
24592477
}
24602478

24612479
void visitObjectInst(ObjectInst *OI) {
@@ -2511,6 +2529,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
25112529
[&] { *this << ", "; });
25122530
*this << ')';
25132531
}
2532+
printForwardingOwnershipKind(TI);
25142533
}
25152534

25162535
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'

0 commit comments

Comments
 (0)