Merge pull request #82890 from meg-gupta/simplifyendcowaddr

Add simplification for end_cow_mutation_addr
This commit is contained in:
Meghana Gupta
2025-07-15 07:37:13 -07:00
committed by GitHub
7 changed files with 89 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ swift_compiler_sources(Optimizer
SimplifyDebugStep.swift
SimplifyDestroyValue.swift
SimplifyDestructure.swift
SimplifyEndCOWMutationAddr.swift
SimplifyFixLifetime.swift
SimplifyGlobalValue.swift
SimplifyInitEnumDataAddr.swift

View File

@@ -0,0 +1,32 @@
//===--- SimplifyEndCOWMutationAddr.swift ---------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 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
// Simplify end_cow_mutation_addr to end_cow_mutation when it's operand is loadable
extension EndCOWMutationAddrInst: OnoneSimplifiable, SILCombineSimplifiable {
func simplify(_ context: SimplifyContext) {
let address = operand.value
if !address.type.isLoadable(in: parentFunction) {
return
}
if address.type.isTrivial(in: parentFunction) {
context.erase(instruction: self)
return
}
let builder = Builder(before: self, context)
let load = builder.createLoad(fromAddress: address, ownership: parentFunction.hasOwnership ? .take : .unqualified)
let endCOWMutation = builder.createEndCOWMutation(instance: load, keepUnique: false)
builder.createStore(source: endCOWMutation, destination: address, ownership: parentFunction.hasOwnership ? .initialize : .unqualified)
context.erase(instruction: self)
}
}

View File

@@ -30,3 +30,4 @@ extension TypeValueInst: OnoneSimplifiable, SILCombineSimplifiable {
context.erase(instruction: self)
}
}

View File

@@ -136,6 +136,7 @@ private func registerSwiftPasses() {
registerForSILCombine(AllocStackInst.self, { run(AllocStackInst.self, $0) })
registerForSILCombine(ApplyInst.self, { run(ApplyInst.self, $0) })
registerForSILCombine(TryApplyInst.self, { run(TryApplyInst.self, $0) })
registerForSILCombine(EndCOWMutationAddrInst.self, { run(EndCOWMutationAddrInst.self, $0) })
// Test passes
registerPass(aliasInfoDumper, { aliasInfoDumper.run($0) })

View File

@@ -3984,6 +3984,14 @@ protected:
singleValueInstructionFallback(kp);
}
void visitEndCOWMutationInst(EndCOWMutationInst *endCOW) {
// This instruction is purely for semantic tracking in SIL.
// Simply forward the value and delete the instruction.
auto valAddr = assignment.getAddressForValue(endCOW->getOperand());
assignment.mapValueToAddress(endCOW, valAddr);
assignment.markForDeletion(endCOW);
}
void visitMarkDependenceInst(MarkDependenceInst *mark) {
// This instruction is purely for semantic tracking in SIL.
// Simply forward the value and delete the instruction.

View File

@@ -52,6 +52,7 @@ INSTRUCTION_SIMPLIFICATION(TypeValueInst)
INSTRUCTION_SIMPLIFICATION(UncheckedAddrCastInst)
INSTRUCTION_SIMPLIFICATION(UncheckedEnumDataInst)
INSTRUCTION_SIMPLIFICATION(WitnessMethodInst)
INSTRUCTION_SIMPLIFICATION(EndCOWMutationAddrInst)
INSTRUCTION_SIMPLIFICATION_WITH_LEGACY(AllocStackInst)
INSTRUCTION_SIMPLIFICATION_WITH_LEGACY(UnconditionalCheckedCastInst)
INSTRUCTION_SIMPLIFICATION_WITH_LEGACY(ApplyInst)

View File

@@ -0,0 +1,45 @@
// RUN: %target-sil-opt %s -onone-simplification -simplify-instruction=end_cow_mutation_addr -enable-experimental-feature Lifetimes | %FileCheck %s
// REQUIRES: swift_feature_Lifetimes
import Swift
import Builtin
protocol P {
mutating func getMutableSpan() -> MutableSpan<Int>
}
struct C : P {
@_hasStorage var array: [Int] { get set }
mutating func getMutableSpan() -> MutableSpan<Int>
init(array: [Int])
}
extension Array : P where Element == Int {
mutating func getMutableSpan() -> MutableSpan<Int>
}
sil @getMutableSpan : $@convention(method) (@inout Array<Int>) -> @lifetime(borrow 0) @owned MutableSpan<Int>
sil @write : $@convention(thin) (@lifetime(copy 0) @inout MutableSpan<Int>) -> ()
// CHECK-LABEL: sil hidden @test_end_cow_mutation_addr :
// CHECK-NOT: end_cow_mutation_addr
// CHECK: end_cow_mutation
// CHECK: } // end sil function 'test_end_cow_mutation_addr'
sil hidden @test_end_cow_mutation_addr : $@convention(thin) (@inout C) -> () {
bb0(%0 : $*C):
%3 = alloc_stack [lexical] [var_decl] $MutableSpan<Int>, var, name "span"
%5 = struct_element_addr %0, #C.array
%6 = function_ref @getMutableSpan : $@convention(method) (@inout Array<Int>) -> @lifetime(borrow 0) @owned MutableSpan<Int>
%7 = apply %6(%5) : $@convention(method) (@inout Array<Int>) -> @lifetime(borrow 0) @owned MutableSpan<Int>
%8 = mark_dependence [nonescaping] %7 on %0
%9 = mark_dependence [nonescaping] %8 on %0
%10 = mark_dependence [nonescaping] %9 on %0
store %10 to %3
%12 = function_ref @write : $@convention(thin) (@lifetime(copy 0) @inout MutableSpan<Int>) -> ()
%13 = apply %12(%3) : $@convention(thin) (@lifetime(copy 0) @inout MutableSpan<Int>) -> ()
end_cow_mutation_addr %0
dealloc_stack %3
%17 = tuple ()
return %17
}