Skip to content

SimplifyBeginCOWMutation: don't eliminate end_cow_mutation instructions with the [keep_unique] flag #70233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2023
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 @@ -74,7 +74,13 @@ private extension BeginCOWMutationInst {
return false
}
let buffer = instanceResult
if buffer.uses.ignoreDebugUses.contains(where: { !($0.instruction is EndCOWMutationInst) }) {
guard buffer.uses.ignoreDebugUses.allSatisfy({
if let endCOW = $0.instruction as? EndCOWMutationInst {
return !endCOW.doKeepUnique
}
return false
}) else
{
return false
}

Expand All @@ -91,7 +97,8 @@ private extension BeginCOWMutationInst {
if !uniquenessResult.uses.ignoreDebugUses.isEmpty {
return false
}
guard let endCOW = instance as? EndCOWMutationInst else {
guard let endCOW = instance as? EndCOWMutationInst,
!endCOW.doKeepUnique else {
return false
}
if endCOW.uses.ignoreDebugUses.contains(where: { $0.instruction != self }) {
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ SILInstruction *SILCombiner::visitEndCOWMutationInst(EndCOWMutationInst *ECM) {

SingleValueInstruction *refCast = cast<SingleValueInstruction>(op);
auto *newECM = Builder.createEndCOWMutation(ECM->getLoc(),
refCast->getOperand(0));
refCast->getOperand(0),
ECM->doKeepUnique());
ECM->replaceAllUsesWith(refCast);
refCast->setOperand(0, newECM);
refCast->moveAfter(newECM);
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/reverse-array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public func reverseArray(_ a: [Int]) -> [Int] {
// CHECK: begin_cow_mutation
// CHECK-NOT: {{.*(_cow_mutation|cond_fail)}}
// CHECK: end_cow_mutation
// CHECK-NOT: {{.*(_cow_mutation|cond_fail)}}
// CHECK: end_cow_mutation

// In SIL we fail to eliminate the bounds check of the input array.
// But that's okay, because LLVM can do that.
Expand Down
21 changes: 21 additions & 0 deletions test/SILOptimizer/sil_combine_inst_passes.sil
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ bb0(%0 : @owned $Buffer):
return %t : $(Builtin.Int1, Buffer)
}

// CHECK-LABEL: sil [ossa] @dont_remove_end_begin_cow_pair3
// CHECK: end_cow_mutation
// CHECK: begin_cow_mutation
// CHECK: } // end sil function 'dont_remove_end_begin_cow_pair3'
sil [ossa] @dont_remove_end_begin_cow_pair3 : $@convention(thin) (@owned Buffer) -> @owned Buffer {
bb0(%0 : @owned $Buffer):
%e = end_cow_mutation [keep_unique] %0 : $Buffer
(%u, %b) = begin_cow_mutation %e : $Buffer
return %b : $Buffer
}

// CHECK-LABEL: sil [ossa] @remove_begin_end_cow_pair
// CHECK-NOT: end_cow_mutation
// CHECK-NOT: begin_cow_mutation
Expand Down Expand Up @@ -87,6 +98,16 @@ bb0(%0 : @owned $Buffer):
return %t : $(Builtin.Int1, Buffer)
}

// CHECK-LABEL: sil [ossa] @dont_remove_begin_end_cow_pair3
// CHECK: begin_cow_mutation
// CHECK: end_cow_mutation
// CHECK: } // end sil function 'dont_remove_begin_end_cow_pair3'
sil [ossa] @dont_remove_begin_end_cow_pair3 : $@convention(thin) (@owned Buffer) -> @owned Buffer {
bb0(%0 : @owned $Buffer):
(%u, %b) = begin_cow_mutation %0 : $Buffer
%e = end_cow_mutation [keep_unique] %b : $Buffer
return %e : $Buffer
}
// CHECK-LABEL: sil @optimize_empty_cow_singleton
// CHECK: [[I:%[0-9]+]] = integer_literal $Builtin.Int1, 0
// CHECK: begin_cow_mutation
Expand Down
24 changes: 24 additions & 0 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,30 @@ bb0(%0 : @owned $C1):
return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @end_cow_mutation_of_cast :
// CHECK: %1 = end_cow_mutation %0
// CHECK-NEXT: %2 = upcast %1
// CHECK-NEXT: return %2
// CHECK: } // end sil function 'end_cow_mutation_of_cast'
sil [ossa] @end_cow_mutation_of_cast : $@convention(thin) (@owned C2) -> @owned C1 {
bb0(%0 : @owned $C2):
%1 = upcast %0 : $C2 to $C1
%2 = end_cow_mutation %1 : $C1
return %2 : $C1
}

// CHECK-LABEL: sil [ossa] @end_cow_mutation_of_cast2 :
// CHECK: %1 = end_cow_mutation [keep_unique] %0
// CHECK-NEXT: %2 = upcast %1
// CHECK-NEXT: return %2
// CHECK: } // end sil function 'end_cow_mutation_of_cast2'
sil [ossa] @end_cow_mutation_of_cast2 : $@convention(thin) (@owned C2) -> @owned C1 {
bb0(%0 : @owned $C2):
%1 = upcast %0 : $C2 to $C1
%2 = end_cow_mutation [keep_unique] %1 : $C1
return %2 : $C1
}

struct XS {
var m: Int
var k: Float
Expand Down