Files
swift-mirror/test/SILOptimizer/moveonly_class_inits_end_to_end.swift
Erik Eckstein 5bc036661c SIL optimizer: add the LetPropertyLowering pass
It lowers let property accesses of classes.
Lowering consists of two tasks:

* In class initializers, insert `end_init_let_ref` instructions at places where all let-fields are initialized.
  This strictly separates the life-range of the class into a region where let fields are still written during
  initialization and a region where let fields are truly immutable.

* Add the `[immutable]` flag to all `ref_element_addr` instructions (for let-fields) which are in the "immutable"
  region. This includes the region after an inserted `end_init_let_ref` in an class initializer, but also all
  let-field accesses in other functions than the initializer and the destructor.

This pass should run after DefiniteInitialization but before RawSILInstLowering (because it relies on `mark_uninitialized` still present in the class initializer).
Note that it's not mandatory to run this pass. If it doesn't run, SIL is still correct.

Simplified example (after lowering):

  bb0(%0 : @owned C):                           // = self of the class initializer
    %1 = mark_uninitialized %0
    %2 = ref_element_addr %1, #C.l              // a let-field
    store %init_value to %2
    %3 = end_init_let_ref %1                    // inserted by lowering
    %4 = ref_element_addr [immutable] %3, #C.l  // set to immutable by lowering
    %5 = load %4
2023-09-19 15:10:30 +02:00

35 lines
1.3 KiB
Swift

// RUN: %target-swift-frontend -module-name moveonly_class_inits_end_to_end %s -emit-sil -o - | %FileCheck %s
// RUN: %target-swift-frontend %s -O -emit-sil -sil-verify-all > /dev/null
// A test that makes sure end to end in a copyable class containing a
// non-copyable type, in the init, we only have a single destroy_addr.
// REQUIRES: swift_in_compiler
public struct MO: ~Copyable {
var x: Int8 = 0
deinit { print("destroyed MO") }
}
public class MOHaver {
var mo: MO
// CHECK-LABEL: sil hidden @$s028moveonly_class_inits_end_to_D07MOHaverCACycfc : $@convention(method) (@owned MOHaver) -> @owned MOHaver {
// CHECK: bb0([[ARG:%.*]] :
// CHECK-NEXT: debug_value [[ARG]]
// CHECK-NEXT: [[EI:%.*]] = end_init_let_ref [[ARG]]
// CHECK-NEXT: [[META:%.*]] = metatype
// CHECK-NEXT: function_ref MO.init()
// CHECK-NEXT: [[FUNC:%.*]] = function_ref @$s028moveonly_class_inits_end_to_D02MOVACycfC :
// CHECK-NEXT: [[RESULT:%.*]] = apply [[FUNC]]([[META]])
// CHECK-NEXT: [[REF:%.*]] = ref_element_addr [[EI]]
// CHECK-NEXT: [[REF_ACCESS:%.*]] = begin_access [modify] [dynamic] [[REF]]
// CHECK-NEXT: store [[RESULT]] to [[REF_ACCESS]]
// CHECK-NEXT: end_access [[REF_ACCESS]]
// CHECK-NEXT: return [[EI]]
// CHECK-NEXT: } // end sil function '$s028moveonly_class_inits_end_to_D07MOHaverCACycfc'
init() {
self.mo = MO()
}
}