Skip to content
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 @@ -25,6 +25,7 @@ swift_compiler_sources(Optimizer
SimplifyDestroyValue.swift
SimplifyDestructure.swift
SimplifyEndCOWMutationAddr.swift
SimplifyExplicitCopy.swift
SimplifyFixLifetime.swift
SimplifyGlobalValue.swift
SimplifyInitEnumDataAddr.swift
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//===--- SimplifyExplicitCopy.swift ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 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

// 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.

/// Replaces
///
/// ```
/// %1 = explicit_copy_value %0
/// ```
/// ->
/// ```
/// %1 = copy_value %0
/// ```
///
extension ExplicitCopyValueInst : Simplifiable, SILCombineSimplifiable {
func simplify(_ context: SimplifyContext) {
if context.silStage == .raw {
// Make sure we don't remove `explicit_copy_value` in the diagnostic pipeline.
return
}

let builder = Builder(before: self, context)
let copyValue = builder.createCopyValue(operand: fromValue)
replace(with: copyValue, context)
}
}

/// Replaces
///
/// ```
/// explicit_copy_addr %0 to %1
/// ```
/// ->
/// ```
/// copy_addr %0 to %1
/// ```
///
extension ExplicitCopyAddrInst : Simplifiable, SILCombineSimplifiable {
func simplify(_ context: SimplifyContext) {
if context.silStage == .raw {
// Make sure we don't remove `explicit_copy_addr` in the diagnostic pipeline.
return
}

let builder = Builder(before: self, context)
builder.createCopyAddr(from: source, to: destination,
takeSource: isTakeOfSource, initializeDest: isInitializationOfDestination)
context.erase(instruction: self)
}
}
2 changes: 2 additions & 0 deletions lib/SILOptimizer/SILCombiner/Simplifications.def
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
INSTRUCTION_SIMPLIFICATION(BeginBorrowInst)
INSTRUCTION_SIMPLIFICATION(BeginCOWMutationInst)
INSTRUCTION_SIMPLIFICATION(ClassifyBridgeObjectInst)
INSTRUCTION_SIMPLIFICATION(ExplicitCopyAddrInst)
INSTRUCTION_SIMPLIFICATION(ExplicitCopyValueInst)
INSTRUCTION_SIMPLIFICATION(FixLifetimeInst)
INSTRUCTION_SIMPLIFICATION(GlobalValueInst)
INSTRUCTION_SIMPLIFICATION(StrongRetainInst)
Expand Down
38 changes: 38 additions & 0 deletions test/SILOptimizer/simplify_explicit_copy_addr.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplification -simplify-instruction=explicit_copy_addr | %FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK

// REQUIRES: swift_in_compiler

sil_stage canonical

import Swift
import Builtin

// CHECK-LABEL: sil [ossa] @simplify_copy_take_init :
// CHECK: bb0(%0 : $*String, %1 : $*String):
// CHECK-O-NEXT: {{ }}copy_addr [take] %1 to [init] %0
// CHECK-ONONE-NEXT: explicit_copy_addr [take] %1 to [init] %0
// CHECK-NEXT: %3 = tuple ()
// CHECK: } // end sil function 'simplify_copy_take_init'
sil [ossa] @simplify_copy_take_init : $@convention(thin) (@in String) -> @out String {
bb0(%0 : $*String, %1 : $*String):
explicit_copy_addr [take] %1 to [init] %0
%3 = tuple ()
return %3
}

// CHECK-LABEL: sil [ossa] @simplify_copy_assign :
// CHECK: bb0(%0 : $*String, %1 : $*String):
// CHECK-O-NEXT: {{ }}copy_addr %1 to %0
// CHECK-ONONE-NEXT: explicit_copy_addr %1 to %0
// CHECK-NEXT: %3 = tuple ()
// CHECK: } // end sil function 'simplify_copy_assign'
sil [ossa] @simplify_copy_assign : $@convention(thin) (@inout String, @inout String) -> () {
bb0(%0 : $*String, %1 : $*String):
explicit_copy_addr %1 to %0
%3 = tuple ()
return %3
}



21 changes: 21 additions & 0 deletions test/SILOptimizer/simplify_explicit_copy_value.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplification -simplify-instruction=explicit_copy_value | %FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK

// REQUIRES: swift_in_compiler

sil_stage canonical

import Swift
import Builtin

// CHECK-LABEL: sil [ossa] @simplify_explicit_copy_value :
// CHECK-O: %1 = copy_value %0
// CHECK-ONONE: %1 = explicit_copy_value %0
// CHECK-NEXT: return %1
// CHECK: } // end sil function 'simplify_explicit_copy_value'
sil [ossa] @simplify_explicit_copy_value : $@convention(thin) (@guaranteed String) -> @owned String {
bb0(%0 : @guaranteed $String):
%1 = explicit_copy_value %0
return %1
}