Skip to content

Commit 844217d

Browse files
committed
Optimizer: replace explicit_copy_value and explicit_copy_addr with their non-explicit counterparts
The `explicit_copy_value` and `explicit_copy_addr` instructions are only used for non-copyable diagnostics in the mandatory pipeline. After that we can replace them by their non-explicit counterparts so that optimizations (which only know of `copy_value` and `copy_addr`) can do their work. rdar://159039552
1 parent e26e2a6 commit 844217d

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ swift_compiler_sources(Optimizer
2525
SimplifyDestroyValue.swift
2626
SimplifyDestructure.swift
2727
SimplifyEndCOWMutationAddr.swift
28+
SimplifyExplicitCopy.swift
2829
SimplifyFixLifetime.swift
2930
SimplifyGlobalValue.swift
3031
SimplifyInitEnumDataAddr.swift
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===--- SimplifyExplicitCopy.swift ---------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 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+
// The `explicit_copy_value` and `explicit_copy_addr` instructions are only used for non-copyable
16+
// diagnostics in the mandatory pipeline. After that we can replace them by their non-explicit counterparts
17+
// so that optimizations (which only know of `copy_value` and `copy_addr`) can do their work.
18+
19+
/// Replaces
20+
///
21+
/// ```
22+
/// %1 = explicit_copy_value %0
23+
/// ```
24+
/// ->
25+
/// ```
26+
/// %1 = copy_value %0
27+
/// ```
28+
///
29+
extension ExplicitCopyValueInst : Simplifiable, SILCombineSimplifiable {
30+
func simplify(_ context: SimplifyContext) {
31+
if context.silStage == .raw {
32+
// Make sure we don't remove `explicit_copy_value` in the diagnostic pipeline.
33+
return
34+
}
35+
36+
let builder = Builder(before: self, context)
37+
let copyValue = builder.createCopyValue(operand: fromValue)
38+
replace(with: copyValue, context)
39+
}
40+
}
41+
42+
/// Replaces
43+
///
44+
/// ```
45+
/// explicit_copy_addr %0 to %1
46+
/// ```
47+
/// ->
48+
/// ```
49+
/// copy_addr %0 to %1
50+
/// ```
51+
///
52+
extension ExplicitCopyAddrInst : Simplifiable, SILCombineSimplifiable {
53+
func simplify(_ context: SimplifyContext) {
54+
if context.silStage == .raw {
55+
// Make sure we don't remove `explicit_copy_addr` in the diagnostic pipeline.
56+
return
57+
}
58+
59+
let builder = Builder(before: self, context)
60+
builder.createCopyAddr(from: source, to: destination,
61+
takeSource: isTakeOfSource, initializeDest: isInitializationOfDestination)
62+
context.erase(instruction: self)
63+
}
64+
}

lib/SILOptimizer/SILCombiner/Simplifications.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
INSTRUCTION_SIMPLIFICATION(BeginBorrowInst)
3333
INSTRUCTION_SIMPLIFICATION(BeginCOWMutationInst)
3434
INSTRUCTION_SIMPLIFICATION(ClassifyBridgeObjectInst)
35+
INSTRUCTION_SIMPLIFICATION(ExplicitCopyAddrInst)
36+
INSTRUCTION_SIMPLIFICATION(ExplicitCopyValueInst)
3537
INSTRUCTION_SIMPLIFICATION(FixLifetimeInst)
3638
INSTRUCTION_SIMPLIFICATION(GlobalValueInst)
3739
INSTRUCTION_SIMPLIFICATION(StrongRetainInst)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=explicit_copy_addr | %FileCheck %s --check-prefix=CHECK-ONONE --check-prefix=CHECK
2+
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplification -simplify-instruction=explicit_copy_addr | %FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK
3+
4+
// REQUIRES: swift_in_compiler
5+
6+
sil_stage canonical
7+
8+
import Swift
9+
import Builtin
10+
11+
// CHECK-LABEL: sil [ossa] @simplify_copy_take_init :
12+
// CHECK: bb0(%0 : $*String, %1 : $*String):
13+
// CHECK-O-NEXT: {{ }}copy_addr [take] %1 to [init] %0
14+
// CHECK-ONONE-NEXT: explicit_copy_addr [take] %1 to [init] %0
15+
// CHECK-NEXT: %3 = tuple ()
16+
// CHECK: } // end sil function 'simplify_copy_take_init'
17+
sil [ossa] @simplify_copy_take_init : $@convention(thin) (@in String) -> @out String {
18+
bb0(%0 : $*String, %1 : $*String):
19+
explicit_copy_addr [take] %1 to [init] %0
20+
%3 = tuple ()
21+
return %3
22+
}
23+
24+
// CHECK-LABEL: sil [ossa] @simplify_copy_assign :
25+
// CHECK: bb0(%0 : $*String, %1 : $*String):
26+
// CHECK-O-NEXT: {{ }}copy_addr %1 to %0
27+
// CHECK-ONONE-NEXT: explicit_copy_addr %1 to %0
28+
// CHECK-NEXT: %3 = tuple ()
29+
// CHECK: } // end sil function 'simplify_copy_assign'
30+
sil [ossa] @simplify_copy_assign : $@convention(thin) (@inout String, @inout String) -> () {
31+
bb0(%0 : $*String, %1 : $*String):
32+
explicit_copy_addr %1 to %0
33+
%3 = tuple ()
34+
return %3
35+
}
36+
37+
38+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification -simplify-instruction=explicit_copy_value | %FileCheck %s --check-prefix=CHECK-ONONE --check-prefix=CHECK
2+
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplification -simplify-instruction=explicit_copy_value | %FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK
3+
4+
// REQUIRES: swift_in_compiler
5+
6+
sil_stage canonical
7+
8+
import Swift
9+
import Builtin
10+
11+
// CHECK-LABEL: sil [ossa] @simplify_explicit_copy_value :
12+
// CHECK-O: %1 = copy_value %0
13+
// CHECK-ONONE: %1 = explicit_copy_value %0
14+
// CHECK-NEXT: return %1
15+
// CHECK: } // end sil function 'simplify_explicit_copy_value'
16+
sil [ossa] @simplify_explicit_copy_value : $@convention(thin) (@guaranteed String) -> @owned String {
17+
bb0(%0 : @guaranteed $String):
18+
%1 = explicit_copy_value %0
19+
return %1
20+
}
21+

0 commit comments

Comments
 (0)