Skip to content

Commit a4ad806

Browse files
authored
Merge pull request #82890 from meg-gupta/simplifyendcowaddr
Add simplification for end_cow_mutation_addr
2 parents cbc0e80 + e317a60 commit a4ad806

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ swift_compiler_sources(Optimizer
2424
SimplifyDebugStep.swift
2525
SimplifyDestroyValue.swift
2626
SimplifyDestructure.swift
27+
SimplifyEndCOWMutationAddr.swift
2728
SimplifyFixLifetime.swift
2829
SimplifyGlobalValue.swift
2930
SimplifyInitEnumDataAddr.swift
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===--- SimplifyEndCOWMutationAddr.swift ---------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 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+
// Simplify end_cow_mutation_addr to end_cow_mutation when it's operand is loadable
16+
extension EndCOWMutationAddrInst: OnoneSimplifiable, SILCombineSimplifiable {
17+
func simplify(_ context: SimplifyContext) {
18+
let address = operand.value
19+
if !address.type.isLoadable(in: parentFunction) {
20+
return
21+
}
22+
if address.type.isTrivial(in: parentFunction) {
23+
context.erase(instruction: self)
24+
return
25+
}
26+
let builder = Builder(before: self, context)
27+
let load = builder.createLoad(fromAddress: address, ownership: parentFunction.hasOwnership ? .take : .unqualified)
28+
let endCOWMutation = builder.createEndCOWMutation(instance: load, keepUnique: false)
29+
builder.createStore(source: endCOWMutation, destination: address, ownership: parentFunction.hasOwnership ? .initialize : .unqualified)
30+
context.erase(instruction: self)
31+
}
32+
}

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyMisc.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ extension TypeValueInst: OnoneSimplifiable, SILCombineSimplifiable {
3030
context.erase(instruction: self)
3131
}
3232
}
33+

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ private func registerSwiftPasses() {
136136
registerForSILCombine(AllocStackInst.self, { run(AllocStackInst.self, $0) })
137137
registerForSILCombine(ApplyInst.self, { run(ApplyInst.self, $0) })
138138
registerForSILCombine(TryApplyInst.self, { run(TryApplyInst.self, $0) })
139+
registerForSILCombine(EndCOWMutationAddrInst.self, { run(EndCOWMutationAddrInst.self, $0) })
139140

140141
// Test passes
141142
registerPass(aliasInfoDumper, { aliasInfoDumper.run($0) })

lib/IRGen/LoadableByAddress.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,6 +3984,14 @@ class AssignAddressToDef : SILInstructionVisitor<AssignAddressToDef> {
39843984
singleValueInstructionFallback(kp);
39853985
}
39863986

3987+
void visitEndCOWMutationInst(EndCOWMutationInst *endCOW) {
3988+
// This instruction is purely for semantic tracking in SIL.
3989+
// Simply forward the value and delete the instruction.
3990+
auto valAddr = assignment.getAddressForValue(endCOW->getOperand());
3991+
assignment.mapValueToAddress(endCOW, valAddr);
3992+
assignment.markForDeletion(endCOW);
3993+
}
3994+
39873995
void visitMarkDependenceInst(MarkDependenceInst *mark) {
39883996
// This instruction is purely for semantic tracking in SIL.
39893997
// Simply forward the value and delete the instruction.

lib/SILOptimizer/SILCombiner/Simplifications.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ INSTRUCTION_SIMPLIFICATION(TypeValueInst)
5252
INSTRUCTION_SIMPLIFICATION(UncheckedAddrCastInst)
5353
INSTRUCTION_SIMPLIFICATION(UncheckedEnumDataInst)
5454
INSTRUCTION_SIMPLIFICATION(WitnessMethodInst)
55+
INSTRUCTION_SIMPLIFICATION(EndCOWMutationAddrInst)
5556
INSTRUCTION_SIMPLIFICATION_WITH_LEGACY(AllocStackInst)
5657
INSTRUCTION_SIMPLIFICATION_WITH_LEGACY(UnconditionalCheckedCastInst)
5758
INSTRUCTION_SIMPLIFICATION_WITH_LEGACY(ApplyInst)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %target-sil-opt %s -onone-simplification -simplify-instruction=end_cow_mutation_addr -enable-experimental-feature Lifetimes | %FileCheck %s
2+
3+
// REQUIRES: swift_feature_Lifetimes
4+
5+
import Swift
6+
import Builtin
7+
8+
protocol P {
9+
mutating func getMutableSpan() -> MutableSpan<Int>
10+
}
11+
12+
struct C : P {
13+
@_hasStorage var array: [Int] { get set }
14+
mutating func getMutableSpan() -> MutableSpan<Int>
15+
init(array: [Int])
16+
}
17+
18+
extension Array : P where Element == Int {
19+
mutating func getMutableSpan() -> MutableSpan<Int>
20+
}
21+
22+
sil @getMutableSpan : $@convention(method) (@inout Array<Int>) -> @lifetime(borrow 0) @owned MutableSpan<Int>
23+
sil @write : $@convention(thin) (@lifetime(copy 0) @inout MutableSpan<Int>) -> ()
24+
25+
// CHECK-LABEL: sil hidden @test_end_cow_mutation_addr :
26+
// CHECK-NOT: end_cow_mutation_addr
27+
// CHECK: end_cow_mutation
28+
// CHECK: } // end sil function 'test_end_cow_mutation_addr'
29+
sil hidden @test_end_cow_mutation_addr : $@convention(thin) (@inout C) -> () {
30+
bb0(%0 : $*C):
31+
%3 = alloc_stack [lexical] [var_decl] $MutableSpan<Int>, var, name "span"
32+
%5 = struct_element_addr %0, #C.array
33+
%6 = function_ref @getMutableSpan : $@convention(method) (@inout Array<Int>) -> @lifetime(borrow 0) @owned MutableSpan<Int>
34+
%7 = apply %6(%5) : $@convention(method) (@inout Array<Int>) -> @lifetime(borrow 0) @owned MutableSpan<Int>
35+
%8 = mark_dependence [nonescaping] %7 on %0
36+
%9 = mark_dependence [nonescaping] %8 on %0
37+
%10 = mark_dependence [nonescaping] %9 on %0
38+
store %10 to %3
39+
%12 = function_ref @write : $@convention(thin) (@lifetime(copy 0) @inout MutableSpan<Int>) -> ()
40+
%13 = apply %12(%3) : $@convention(thin) (@lifetime(copy 0) @inout MutableSpan<Int>) -> ()
41+
end_cow_mutation_addr %0
42+
dealloc_stack %3
43+
%17 = tuple ()
44+
return %17
45+
}

0 commit comments

Comments
 (0)