Skip to content

Commit 7508a90

Browse files
committed
Enable destructure_struct/destructure_tuple simplifications in silcombine
1 parent 124df8e commit 7508a90

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyDestructure.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SIL
1414

15-
extension DestructureTupleInst : OnoneSimplifyable {
15+
extension DestructureTupleInst : OnoneSimplifyable, SILCombineSimplifyable {
1616
func simplify(_ context: SimplifyContext) {
1717

1818
// Eliminate the redundant instruction pair
@@ -28,7 +28,7 @@ extension DestructureTupleInst : OnoneSimplifyable {
2828
}
2929
}
3030

31-
extension DestructureStructInst : OnoneSimplifyable {
31+
extension DestructureStructInst : OnoneSimplifyable, SILCombineSimplifyable {
3232
func simplify(_ context: SimplifyContext) {
3333

3434
switch self.struct {

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ private func registerSwiftPasses() {
103103
registerForSILCombine(LoadInst.self, { run(LoadInst.self, $0) })
104104
registerForSILCombine(CopyValueInst.self, { run(CopyValueInst.self, $0) })
105105
registerForSILCombine(DestroyValueInst.self, { run(DestroyValueInst.self, $0) })
106+
registerForSILCombine(DestructureStructInst.self, { run(DestructureStructInst.self, $0) })
107+
registerForSILCombine(DestructureTupleInst.self, { run(DestructureTupleInst.self, $0) })
106108

107109
// Test passes
108110
registerPass(functionUsesDumper, { functionUsesDumper.run($0) })

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ SWIFT_SILCOMBINE_PASS(ReleaseValueInst)
510510
SWIFT_SILCOMBINE_PASS(LoadInst)
511511
SWIFT_SILCOMBINE_PASS(CopyValueInst)
512512
SWIFT_SILCOMBINE_PASS(DestroyValueInst)
513+
SWIFT_SILCOMBINE_PASS(DestructureStructInst)
514+
SWIFT_SILCOMBINE_PASS(DestructureTupleInst)
513515

514516
#undef IRGEN_PASS
515517
#undef SWIFT_MODULE_PASS
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// RUN: %target-sil-opt -enable-objc-interop -enforce-exclusivity=none -enable-sil-verify-all %s -sil-combine -sil-combine-disable-alloc-stack-opts | %FileCheck %s
2+
// REQUIRES: swift_in_compiler
3+
4+
sil_stage canonical
5+
6+
import Builtin
7+
import Swift
8+
9+
class Klass {}
10+
11+
struct Wrapper {
12+
var val1: Klass
13+
var val2: Klass
14+
}
15+
16+
// CHECK-LABEL: sil [ossa] @test_destructure_struct1 : $@convention(thin) () -> () {
17+
// CHECK-NOT: destructure_struct
18+
// CHECK-LABEL: } // end sil function 'test_destructure_struct1'
19+
sil [ossa] @test_destructure_struct1 : $@convention(thin) () -> () {
20+
bb0:
21+
%0 = integer_literal $Builtin.Int32, 100
22+
%1 = struct $Int32(%0 : $Builtin.Int32)
23+
(%2) = destructure_struct %1 : $Int32
24+
apply undef(%2) : $@convention(thin) (Builtin.Int32) -> ()
25+
%t = tuple ()
26+
return %t : $()
27+
}
28+
29+
// CHECK-LABEL: sil [ossa] @test_destructure_struct2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
30+
// CHECK-NOT: destructure_struct
31+
// CHECK-LABEL: } // end sil function 'test_destructure_struct2'
32+
sil [ossa] @test_destructure_struct2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
33+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
34+
%3 = struct $Wrapper(%0 : $Klass, %1 : $Klass)
35+
debug_value %3 : $Wrapper
36+
(%4, %5) = destructure_struct %3 : $Wrapper
37+
apply undef(%4) : $@convention(thin) (@owned Klass) -> ()
38+
destroy_value %5 : $Klass
39+
%t = tuple ()
40+
return %t : $()
41+
}
42+
43+
// CHECK-LABEL: sil [ossa] @test_destructure_tuple1 : $@convention(thin) () -> () {
44+
// CHECK-NOT: destructure_tuple
45+
// CHECK-LABEL: } // end sil function 'test_destructure_tuple1'
46+
sil [ossa] @test_destructure_tuple1 : $@convention(thin) () -> () {
47+
bb0:
48+
%0 = integer_literal $Builtin.Int32, 100
49+
%1 = integer_literal $Builtin.Int32, 200
50+
%2 = tuple (%0 : $Builtin.Int32, %1 : $Builtin.Int32)
51+
(%3, %4) = destructure_tuple %2 : $(Builtin.Int32, Builtin.Int32)
52+
apply undef(%4) : $@convention(thin) (Builtin.Int32) -> ()
53+
%t = tuple ()
54+
return %t : $()
55+
}
56+
57+
// CHECK-LABEL: sil [ossa] @test_destructure_tuple2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
58+
// CHECK-NOT: destructure_tuple
59+
// CHECK-LABEL: } // end sil function 'test_destructure_tuple2'
60+
sil [ossa] @test_destructure_tuple2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
61+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
62+
%3 = tuple(%0 : $Klass, %1 : $Klass)
63+
debug_value %3 : $(Klass, Klass)
64+
(%4, %5) = destructure_tuple %3 : $(Klass, Klass)
65+
apply undef(%4) : $@convention(thin) (@owned Klass) -> ()
66+
destroy_value %5 : $Klass
67+
%t = tuple ()
68+
return %t : $()
69+
}
70+

0 commit comments

Comments
 (0)