Files
swift-mirror/test/SILOptimizer/simplify_cfg_args_crash.sil
Erik Eckstein 0f0aa0c17b Optimizer: require that there are no unreachable blocks and infinite loops in OSSA
These two new invariants eliminate corner cases which caused bugs if optimization didn't handle them.
Also, it will significantly simplify lifetime completion.

The implementation basically consists of these changes:
* add a flag in SILFunction which tells optimization if they need to take care of infinite loops
* add a utility to break infinite loops
* let all optimizations remove unreachable blocks and break infinite loops if necessary
* add verification to check the new SIL invariants

The new `breakIfniniteLoops` utility breaks infinite loops in the control flow by inserting an "artificial" loop exit to a new dead-end block with an `unreachable`.
It inserts a `cond_br` with a `builtin "infinite_loop_true_condition"`:
```
bb0:
  br bb1
bb1:
  br bb1              // back-end branch
```
->
```
bb0:
  br bb1
bb1:
  %1 = builtin "infinite_loop_true_condition"() // always true, but the compiler doesn't know
  cond_br %1, bb2, bb3
bb2:                  // new back-end block
  br bb1
bb3:                  // new dead-end block
  unreachable
```
2026-01-22 17:41:23 +01:00

59 lines
1.2 KiB
Plaintext

// RUN: %target-sil-opt -enable-sil-verify-all %s -simplify-bb-args -sroa-bb-args | %FileCheck %s
sil_stage canonical
import Builtin
import Swift
import SwiftShims
enum E {
case A
case B
}
// Check that we don't crash in simplifyToSelectValue (rdar://problem/20037686)
// CHECK-LABEL: @test
sil public @test : $@convention(thin) (Builtin.Int64, @inout E) -> () {
bb0(%0 : $Builtin.Int64, %x : $*E):
%1 = integer_literal $Builtin.Int64, 0
%2 = builtin "cmp_eq_Int64"(%0 : $Builtin.Int64, %1 : $Builtin.Int64) : $Builtin.Int1
cond_br %2, bb3, bb1
bb1:
%4 = enum $E, #E.A!enumelt
br bb2(%4 : $E)
bb2(%6 : $E):
store %6 to %x : $*E
br bb3
bb3:
%8 = tuple ()
return %8 : $()
}
// Verify that we do not crash in argument splitting (rdar://problem/25008398).
class C {
@_hasStorage let x: Builtin.Int32
init()
}
struct Pair {
@_hasStorage let first: C
@_hasStorage let second: C
}
// CHECK-LABEL: @simplify_args_crash
sil @simplify_args_crash : $@convention(thin) (Pair) -> () {
bb0(%1 : $Pair):
// CHECK: bb0
// CHECK-NEXT: br bb1
br bb1(%1 : $Pair)
bb1(%2 : $Pair):
br bb1(%2 : $Pair)
}