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"
```
This commit is contained in:
Erik Eckstein
2025-11-28 09:45:05 +01:00
parent bf760aa3dd
commit 3ade98c84e
2 changed files with 113 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
// RUN: %target-sil-opt -sil-print-ownership %s | %FileCheck %s --check-prefix=CHECK --check-prefix=ENABLE
// RUN: %target-sil-opt %s | %FileCheck %s --check-prefix=CHECK --check-prefix=DISABLE
sil_stage canonical
import Builtin
import Swift
import SwiftShims
enum E {
case A
case B(AnyObject)
}
struct S {
var e: E
var o: AnyObject
}
struct T {
var s: S
}
// CHECK-LABEL: sil [ossa] @ossa_function :
// CHECK: enum $E{{.*}} ownership: none
// ENABLE: struct $S{{.*}} ownership: owned
// ENABLE: begin_apply {{.*}} ownership: -, guaranteed
// DISABLE-NOT: ownership
// CHECK: } // end sil function 'ossa_function'
sil [ossa] @ossa_function : $@convention(thin) (@inout T, @owned AnyObject) -> () {
bb0(%0 : $*T, %1 : @owned $AnyObject):
%2 = enum $E, #E.A!enumelt
%3 = struct $S (%2, %1)
%4 = struct_element_addr %0, #T.s
store %3 to [assign] %4
(%6, %7) = begin_apply undef() : $@yield_once () -> (@yields @in_guaranteed AnyObject)
end_apply %7 as $()
%9 = tuple ()
return %9
}
// CHECK-LABEL: sil @non_ossa_function :
// CHECK-NOT: ownership
// CHECK: } // end sil function 'non_ossa_function'
sil @non_ossa_function : $@convention(thin) (@inout T, @owned AnyObject) -> () {
bb0(%0 : $*T, %1 : $AnyObject):
%2 = enum $E, #E.A!enumelt
%3 = struct $S (%2, %1)
%4 = struct_element_addr %0, #T.s
store %3 to %4
(%6, %7) = begin_apply undef() : $@yield_once () -> (@yields @in_guaranteed AnyObject)
end_apply %7 as $()
%9 = tuple ()
return %9
}