Skip to content

Add silcombines for destructure_tuple and destructure_struct #71880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import SIL

extension DestructureTupleInst : OnoneSimplifyable {
extension DestructureTupleInst : OnoneSimplifyable, SILCombineSimplifyable {
func simplify(_ context: SimplifyContext) {

// Eliminate the redundant instruction pair
Expand All @@ -28,7 +28,7 @@ extension DestructureTupleInst : OnoneSimplifyable {
}
}

extension DestructureStructInst : OnoneSimplifyable {
extension DestructureStructInst : OnoneSimplifyable, SILCombineSimplifyable {
func simplify(_ context: SimplifyContext) {

switch self.struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ private func registerSwiftPasses() {
registerForSILCombine(LoadInst.self, { run(LoadInst.self, $0) })
registerForSILCombine(CopyValueInst.self, { run(CopyValueInst.self, $0) })
registerForSILCombine(DestroyValueInst.self, { run(DestroyValueInst.self, $0) })
registerForSILCombine(DestructureStructInst.self, { run(DestructureStructInst.self, $0) })
registerForSILCombine(DestructureTupleInst.self, { run(DestructureTupleInst.self, $0) })

// Test passes
registerPass(functionUsesDumper, { functionUsesDumper.run($0) })
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SILOptimizer/PassManager/Passes.def
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ SWIFT_SILCOMBINE_PASS(ReleaseValueInst)
SWIFT_SILCOMBINE_PASS(LoadInst)
SWIFT_SILCOMBINE_PASS(CopyValueInst)
SWIFT_SILCOMBINE_PASS(DestroyValueInst)
SWIFT_SILCOMBINE_PASS(DestructureStructInst)
SWIFT_SILCOMBINE_PASS(DestructureTupleInst)

#undef IRGEN_PASS
#undef SWIFT_MODULE_PASS
Expand Down
70 changes: 70 additions & 0 deletions test/SILOptimizer/swift_sil_combine.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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
// REQUIRES: swift_in_compiler

sil_stage canonical

import Builtin
import Swift

class Klass {}

struct Wrapper {
var val1: Klass
var val2: Klass
}

// CHECK-LABEL: sil [ossa] @test_destructure_struct1 : $@convention(thin) () -> () {
// CHECK-NOT: destructure_struct
// CHECK-LABEL: } // end sil function 'test_destructure_struct1'
sil [ossa] @test_destructure_struct1 : $@convention(thin) () -> () {
bb0:
%0 = integer_literal $Builtin.Int32, 100
%1 = struct $Int32(%0 : $Builtin.Int32)
(%2) = destructure_struct %1 : $Int32
apply undef(%2) : $@convention(thin) (Builtin.Int32) -> ()
%t = tuple ()
return %t : $()
}

// CHECK-LABEL: sil [ossa] @test_destructure_struct2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
// CHECK-NOT: destructure_struct
// CHECK-LABEL: } // end sil function 'test_destructure_struct2'
sil [ossa] @test_destructure_struct2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
%3 = struct $Wrapper(%0 : $Klass, %1 : $Klass)
debug_value %3 : $Wrapper
(%4, %5) = destructure_struct %3 : $Wrapper
apply undef(%4) : $@convention(thin) (@owned Klass) -> ()
destroy_value %5 : $Klass
%t = tuple ()
return %t : $()
}

// CHECK-LABEL: sil [ossa] @test_destructure_tuple1 : $@convention(thin) () -> () {
// CHECK-NOT: destructure_tuple
// CHECK-LABEL: } // end sil function 'test_destructure_tuple1'
sil [ossa] @test_destructure_tuple1 : $@convention(thin) () -> () {
bb0:
%0 = integer_literal $Builtin.Int32, 100
%1 = integer_literal $Builtin.Int32, 200
%2 = tuple (%0 : $Builtin.Int32, %1 : $Builtin.Int32)
(%3, %4) = destructure_tuple %2 : $(Builtin.Int32, Builtin.Int32)
apply undef(%4) : $@convention(thin) (Builtin.Int32) -> ()
%t = tuple ()
return %t : $()
}

// CHECK-LABEL: sil [ossa] @test_destructure_tuple2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
// CHECK-NOT: destructure_tuple
// CHECK-LABEL: } // end sil function 'test_destructure_tuple2'
sil [ossa] @test_destructure_tuple2 : $@convention(thin) (@owned Klass, @owned Klass) -> () {
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
%3 = tuple(%0 : $Klass, %1 : $Klass)
debug_value %3 : $(Klass, Klass)
(%4, %5) = destructure_tuple %3 : $(Klass, Klass)
apply undef(%4) : $@convention(thin) (@owned Klass) -> ()
destroy_value %5 : $Klass
%t = tuple ()
return %t : $()
}