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.
This commit is contained in:
Erik Eckstein
2025-12-01 15:29:33 +01:00
parent 3ade98c84e
commit 103d3c2f85
2 changed files with 52 additions and 2 deletions

View File

@@ -2012,6 +2012,23 @@ public:
}
}
template <typename Inst>
void printForwardingOwnershipKind(Inst *inst) {
if (inst->getNumRealOperands() == 0) {
return;
}
bool matching = false;
for (Operand *op : inst->getRealOperands()) {
if (inst->getForwardingOwnershipKind() == op->get()->getOwnershipKind()) {
matching = true;
break;
}
}
if (!matching) {
*this << ", forwarding: @" << inst->getForwardingOwnershipKind();
}
}
void visitStoreInst(StoreInst *SI) {
*this << Ctx.getID(SI->getSrc()) << " to ";
printStoreOwnershipQualifier(SI->getOwnershipQualifier());
@@ -2456,6 +2473,7 @@ public:
SI->getElements(), [&](const SILValue &V) { *this << getIDAndType(V); },
[&] { *this << ", "; });
*this << ')';
printForwardingOwnershipKind(SI);
}
void visitObjectInst(ObjectInst *OI) {
@@ -2511,6 +2529,7 @@ public:
[&] { *this << ", "; });
*this << ')';
}
printForwardingOwnershipKind(TI);
}
void visitTupleAddrConstructorInst(TupleAddrConstructorInst *TI) {

View File

@@ -1,4 +1,4 @@
// RUN: %target-sil-opt -sil-print-types %s | %FileCheck %s
// RUN: %target-sil-opt %s | %FileCheck %s
sil_stage canonical
import Builtin
@@ -9,15 +9,26 @@ class Klass {
class SubKlass : Klass {
}
enum E {
case A
case B(AnyObject)
}
struct NonTrivialStruct {
var id:Klass
}
struct S {
var i: Int
var e: E
}
// CHECK-LABEL: sil [ossa] @unchecked_ref_cast_test :
sil [ossa] @unchecked_ref_cast_test : $@convention(thin) <T> (@in T, @owned Klass) -> () {
bb0(%0 : $*T, %1 : @owned $Klass):
destroy_addr %0 : $*T
// CHECK: unchecked_ref_cast %1 : $Klass to $Optional<Klass>, forwarding: @unowned
// CHECK: unchecked_ref_cast %1 to $Optional<Klass>, forwarding: @unowned
%3 = unchecked_ref_cast %1 : $Klass to $Optional<Klass>, forwarding: @unowned
%4 = copy_value %3 : $Optional<Klass>
destroy_value %1 : $Klass
@@ -38,6 +49,26 @@ bb0(%0 : @owned $Klass):
}
// CHECK-LABEL: } // end sil function 'struct_test'
// CHECK-LABEL: sil [ossa] @none_to_owned_struct :
// CHECK: %2 = struct $S (%0, %1), forwarding: @owned
// CHECK-LABEL: } // end sil function 'none_to_owned_struct'
sil [ossa] @none_to_owned_struct : $@convention(thin) (Int) -> @owned S {
bb0(%0 : $Int):
%1 = enum $E, #E.A!enumelt
%2 = struct $S (%0, %1), forwarding: @owned
return %2
}
// CHECK-LABEL: sil [ossa] @none_to_owned_tuple :
// CHECK: %2 = tuple (%0, %1), forwarding: @owned
// CHECK-LABEL: } // end sil function 'none_to_owned_tuple'
sil [ossa] @none_to_owned_tuple : $@convention(thin) (Int) -> @owned (Int, E) {
bb0(%0 : $Int):
%1 = enum $E, #E.A!enumelt
%2 = tuple (%0, %1), forwarding: @owned
return %2
}
// CHECK-LABEL: sil [ossa] @switch_test : $@convention(thin) () -> () {
// CHECK: switch_enum {{.*}}, forwarding: @guaranteed
// CHECK-LABEL: } // end sil function 'switch_test'