Skip to content

Commit e718bfe

Browse files
committed
Optimizer: reimplement simplifications for copy_value and destroy_value in swift
So that they can run in the OnoneSimplification pass
1 parent b693847 commit e718bfe

File tree

12 files changed

+107
-37
lines changed

12 files changed

+107
-37
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ swift_compiler_sources(Optimizer
1414
SimplifyCondBranch.swift
1515
SimplifyCondFail.swift
1616
SimplifyConvertEscapeToNoEscape.swift
17+
SimplifyCopyValue.swift
1718
SimplifyDebugStep.swift
19+
SimplifyDestroyValue.swift
1820
SimplifyDestructure.swift
1921
SimplifyGlobalValue.swift
2022
SimplifyInitEnumDataAddr.swift
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===--- SimplifyCopyValue.swift ------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
extension CopyValueInst : OnoneSimplifyable, SILCombineSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
if fromValue.ownership == .none {
18+
uses.replaceAll(with: fromValue, context)
19+
context.erase(instruction: self)
20+
}
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===--- SimplifyDestroyValue.swift ---------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
extension DestroyValueInst : OnoneSimplifyable, SILCombineSimplifyable {
16+
func simplify(_ context: SimplifyContext) {
17+
if destroyedValue.ownership == .none {
18+
context.erase(instruction: self)
19+
}
20+
}
21+
}

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ private func registerSwiftPasses() {
9797
registerForSILCombine(RetainValueInst.self, { run(RetainValueInst.self, $0) })
9898
registerForSILCombine(ReleaseValueInst.self, { run(ReleaseValueInst.self, $0) })
9999
registerForSILCombine(LoadInst.self, { run(LoadInst.self, $0) })
100+
registerForSILCombine(CopyValueInst.self, { run(CopyValueInst.self, $0) })
101+
registerForSILCombine(DestroyValueInst.self, { run(DestroyValueInst.self, $0) })
100102

101103
// Test passes
102104
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
@@ -493,6 +493,8 @@ SWIFT_SILCOMBINE_PASS(StrongReleaseInst)
493493
SWIFT_SILCOMBINE_PASS(RetainValueInst)
494494
SWIFT_SILCOMBINE_PASS(ReleaseValueInst)
495495
SWIFT_SILCOMBINE_PASS(LoadInst)
496+
SWIFT_SILCOMBINE_PASS(CopyValueInst)
497+
SWIFT_SILCOMBINE_PASS(DestroyValueInst)
496498

497499
#undef IRGEN_PASS
498500
#undef SWIFT_MODULE_PASS

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ class SILCombiner :
241241
SILInstruction *optimizeStringObject(BuiltinInst *BI);
242242
SILInstruction *visitBuiltinInst(BuiltinInst *BI);
243243
SILInstruction *visitCondFailInst(CondFailInst *CFI);
244-
SILInstruction *visitCopyValueInst(CopyValueInst *cvi);
245-
SILInstruction *visitDestroyValueInst(DestroyValueInst *dvi);
246244
SILInstruction *visitRefToRawPointerInst(RefToRawPointerInst *RRPI);
247245
SILInstruction *visitUpcastInst(UpcastInst *UCI);
248246

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -865,37 +865,6 @@ SILInstruction *SILCombiner::visitCondFailInst(CondFailInst *CFI) {
865865
return nullptr;
866866
}
867867

868-
SILInstruction *SILCombiner::visitCopyValueInst(CopyValueInst *cvi) {
869-
assert(cvi->getFunction()->hasOwnership());
870-
871-
// Sometimes when RAUWing code we get copy_value on .none values (consider
872-
// transformations around function types that result in given a copy_value a
873-
// thin_to_thick_function argument). In such a case, just RAUW with the
874-
// copy_value's operand since it is a no-op.
875-
if (cvi->getOperand()->getOwnershipKind() == OwnershipKind::None) {
876-
replaceInstUsesWith(*cvi, cvi->getOperand());
877-
return eraseInstFromFunction(*cvi);
878-
}
879-
880-
return nullptr;
881-
}
882-
883-
SILInstruction *SILCombiner::visitDestroyValueInst(DestroyValueInst *dvi) {
884-
assert(dvi->getFunction()->hasOwnership());
885-
886-
// Sometimes when RAUWing code we get destroy_value on .none values. In such a
887-
// case, just delete the destroy_value.
888-
//
889-
// As an example, consider transformations around function types that result
890-
// in a thin_to_thick_function being passed to a destroy_value.
891-
if (dvi->getOperand()->getOwnershipKind() == OwnershipKind::None) {
892-
eraseInstFromFunction(*dvi);
893-
return nullptr;
894-
}
895-
896-
return nullptr;
897-
}
898-
899868
/// Create a value from stores to an address.
900869
///
901870
/// If there are only stores to \p addr, return the stored value. Also, if there

test/SILOptimizer/definite-init-convert-to-escape.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public func returnOptionalEscape() -> (() ->())?
6767
// CHECK: [[BLOCK_PROJ:%.*]] = project_block_storage [[BLOCK_SLOT]]
6868
// CHECK: store [[MDI]] to [[BLOCK_PROJ]]
6969
// CHECK: [[BLOCK:%.*]] = init_block_storage_header [[BLOCK_SLOT]]
70-
// CHECK: release_value [[NONE]]
7170
// CHECK: [[SOME_2:%.*]] = enum $Optional<{{.*}}>, #Optional.some!enumelt, [[MDI]]
7271
// CHECK: [[BLOCK_COPY:%.*]] = copy_block [[BLOCK]]
7372
// CHECK: [[BLOCK_SOME:%.*]] = enum $Optional<{{.*}}>, #Optional.some!enumelt, [[BLOCK_COPY]]

test/SILOptimizer/definite_init_value_types.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %target-swift-frontend -enable-copy-propagation=requested-passes-only -enable-lexical-lifetimes=false -emit-sil %s | %FileCheck %s
22

3+
// REQUIRES: swift_in_compiler
4+
35
enum ValueEnum {
46
case a(String)
57
case b
@@ -57,7 +59,6 @@ enum ValueEnum {
5759
// CHECK: bb6:
5860
// CHECK-NEXT: [[NEW_STATE:%.*]] = integer_literal $Builtin.Int1, -1
5961
// CHECK-NEXT: store [[NEW_STATE]] to [[STATE]]
60-
// CHECK-NEXT: retain_value [[NEW_SELF]]
6162
// CHECK-NEXT: store [[NEW_SELF]] to [[SELF_ACCESS]]
6263
// CHECK-NEXT: end_access [[SELF_ACCESS]]
6364
// CHECK-NEXT: destroy_addr [[SELF_BOX]]

test/SILOptimizer/mandatory_inlining_reasync.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ func reasyncFunction(_ value: Optional<Int>, _ fn: () async throws -> Int) reasy
1313

1414
// CHECK-LABEL: sil hidden @$s26mandatory_inlining_reasync20callsReasyncFunctionSiyF : $@convention(thin) () -> Int {
1515
// CHECK: [[FN:%.*]] = function_ref @$s26mandatory_inlining_reasync20callsReasyncFunctionSiyFSiyXEfU_ : $@convention(thin) () -> Int
16-
// CHECK-NEXT: [[THICK:%.*]] = thin_to_thick_function [[FN]] : $@convention(thin) () -> Int to $@noescape @callee_guaranteed () -> Int
17-
// CHECK-NEXT: [[RESULT:%.*]] = apply [[THICK]]() : $@noescape @callee_guaranteed () -> Int
16+
// CHECK-NEXT: [[RESULT:%.*]] = apply [[FN]]() : $@convention(thin) () -> Int
1817
// CHECK-NEXT: return [[RESULT]] : $Int
1918
func callsReasyncFunction() -> Int {
2019
return reasyncFunction(nil, { return 321 } )
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=copy_value | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
sil_stage canonical
6+
7+
import Builtin
8+
import Swift
9+
import SwiftShims
10+
11+
sil @cl : $@convention(thin) () -> Int
12+
13+
// CHECK-LABEL: sil [ossa] @remove_copy :
14+
// CHECK: [[F:%.*]] = thin_to_thick_function %0 : $@convention(thin) () -> Int to $@callee_guaranteed () -> Int
15+
// CHECK-NEXT: destroy_value [[F]]
16+
// CHECK: } // end sil function 'remove_copy'
17+
sil [ossa] @remove_copy : $@convention(thin) () -> () {
18+
bb0:
19+
%0 = function_ref @cl : $@convention(thin) () -> Int
20+
%1 = thin_to_thick_function %0 : $@convention(thin) () -> Int to $@callee_guaranteed () -> Int
21+
%2 = copy_value %1 : $@callee_guaranteed () -> Int
22+
destroy_value %2 : $@callee_guaranteed () -> Int
23+
%4 = tuple ()
24+
return %4 : $()
25+
}
26+
27+
28+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=destroy_value | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
sil_stage canonical
6+
7+
import Builtin
8+
import Swift
9+
import SwiftShims
10+
11+
sil @cl : $@convention(thin) () -> Int
12+
13+
// CHECK-LABEL: sil [ossa] @remove_copy :
14+
// CHECK-NOT: destroy_value
15+
// CHECK: } // end sil function 'remove_copy'
16+
sil [ossa] @remove_copy : $@convention(thin) () -> () {
17+
bb0:
18+
%0 = function_ref @cl : $@convention(thin) () -> Int
19+
%1 = thin_to_thick_function %0 : $@convention(thin) () -> Int to $@callee_guaranteed () -> Int
20+
destroy_value %1 : $@callee_guaranteed () -> Int
21+
%4 = tuple ()
22+
return %4 : $()
23+
}
24+
25+
26+
27+

0 commit comments

Comments
 (0)