Skip to content

[OnoneSimplify] Handle tuple(destructure_tuple()). #72992

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
Apr 12, 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 @@ -31,6 +31,7 @@ swift_compiler_sources(Optimizer
SimplifyStrongRetainRelease.swift
SimplifyStructExtract.swift
SimplifySwitchEnum.swift
SimplifyTuple.swift
SimplifyTupleExtract.swift
SimplifyUncheckedEnumData.swift
SimplifyValueToBridgeObject.swift)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===--- SimplifyTuple.swift ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SIL

extension TupleInst : OnoneSimplifyable {
func simplify(_ context: SimplifyContext) {

// Eliminate the redundant instruction pair
// ```
// (%3, %4, %5) = destructure_tuple %input
// %output = tuple (%3, %4, %5)
// ```
// and replace the result %output with %input
//
var destructure: DestructureTupleInst?
for operand in operands {
guard let def = operand.value.definingInstruction as? DestructureTupleInst else {
return
}
guard let destructure else {
destructure = def
continue
}
if destructure != def {
return
}
}
guard let destructure else {
return
}
guard destructure.operand.value.type == type else {
return
}
// The destructure's operand having the same type as the tuple ensures that
// the count of results of the destructure is equal to the count of operands
// of the tuple.
assert(destructure.results.count == operands.count)
for (result, operand) in zip(destructure.results, operands) {
if result != operand.value {
return
}
}
tryReplaceDestructConstructPair(destruct: destructure, construct: self, context)
}
}

private func tryReplaceDestructConstructPair(destruct: MultipleValueInstruction & UnaryInstruction,
construct: SingleValueInstruction,
_ context: SimplifyContext) {
let everyResultUsedOnce = context.preserveDebugInfo
? destruct.results.allSatisfy { $0.uses.singleUse != nil }
: destruct.results.allSatisfy { $0.uses.ignoreDebugUses.singleUse != nil }
let anyOwned = destruct.results.contains { $0.ownership == .owned }

if !everyResultUsedOnce && construct.parentFunction.hasOwnership && anyOwned {
// We cannot add more uses to this destructure without inserting a copy.
return
}

construct.uses.replaceAll(with: destruct.operand.value, context)

if everyResultUsedOnce {
context.erase(instructionIncludingDebugUses: construct)
}
}
79 changes: 79 additions & 0 deletions test/SILOptimizer/simplify_tuple.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=tuple | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-ONONE
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplification -simplify-instruction=tuple | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-O

// REQUIRES: swift_in_compiler

import Swift
import Builtin

// CHECK-LABEL: sil [ossa] @forward_owned :
// CHECK-NOT: tuple
// CHECK: return %0
// CHECK: } // end sil function 'forward_owned'
sil [ossa] @forward_owned : $@convention(thin) (@owned (String, Int)) -> @owned (String, Int) {
bb0(%0 : @owned $(String, Int)):
(%3, %4) = destructure_tuple %0 : $(String, Int)
%2 = tuple (%3 : $String, %4 : $Int)
return %2 : $(String, Int)
}

// CHECK-LABEL: sil [ossa] @forward_borrowed :
// CHECK-NOT: tuple
// CHECK: fix_lifetime %0
// CHECK: } // end sil function 'forward_borrowed'
sil [ossa] @forward_borrowed : $@convention(thin) (@guaranteed (String, Int)) -> () {
bb0(%0 : @guaranteed $(String, Int)):
(%3, %4) = destructure_tuple %0 : $(String, Int)
%2 = tuple (%3 : $String, %4 : $Int)
fix_lifetime %2 : $(String, Int)
%7 = tuple ()
return %7 : $()
}

// CHECK-LABEL: sil [ossa] @dont_forward_owned_with_uses :
// CHECK: destructure_tuple
// CHECK: tuple
// CHECK: } // end sil function 'dont_forward_owned_with_uses'
sil [ossa] @dont_forward_owned_with_uses : $@convention(thin) (@owned (String, Int)) -> @owned (String, Int) {
bb0(%0 : @owned $(String, Int)):
(%5, %6) = destructure_tuple %0 : $(String, Int)
%3 = begin_borrow %5 : $String
end_borrow %3 : $String
%2 = tuple (%5 : $String, %6 : $Int)
return %2 : $(String, Int)
}

// CHECK-LABEL: sil [ossa] @forward_owned_with_debug_use :
// CHECK-ONONE: destructure_tuple
// CHECK-ONONE: tuple
// CHECK-O: return %0
// CHECK: } // end sil function 'forward_owned_with_debug_use'
sil [ossa] @forward_owned_with_debug_use : $@convention(thin) (@owned (String, Int)) -> @owned (String, Int) {
bb0(%0 : @owned $(String, Int)):
(%4, %5) = destructure_tuple %0 : $(String, Int)
debug_value %4 : $String, let, name "t"
%2 = tuple (%4 : $String, %5 : $Int)
return %2 : $(String, Int)
}

// CHECK-LABEL: sil [ossa] @dont_forward_label_change :
// CHECK: destructure_tuple
// CHECK: tuple
// CHECK-LABEL: } // end sil function 'dont_forward_label_change'
sil [ossa] @dont_forward_label_change : $@convention(thin) (@owned (s: String, i: Int)) -> @owned (String, Int) {
bb0(%0 : @owned $(s: String, i: Int)):
(%4, %5) = destructure_tuple %0 : $(s: String, i: Int)
%2 = tuple (%4 : $String, %5 : $Int)
return %2 : $(String, Int)
}

// CHECK-LABEL: sil [ossa] @dont_forward_flipped_arguments :
// CHECK: destructure_tuple
// CHECK: tuple
// CHECK-LABEL: } // end sil function 'dont_forward_flipped_arguments'
sil [ossa] @dont_forward_flipped_arguments : $@convention(thin) (@owned (String, String)) -> @owned (String, String) {
bb0(%0 : @owned $(String, String)):
(%4, %5) = destructure_tuple %0 : $(String, String)
%2 = tuple (%5 : $String, %4 : $String)
return %2 : $(String, String)
}