Skip to content

Commit ffda803

Browse files
Merge pull request #68098 from nate-chandler/cherrypick/release/5.9/rdar114323803
5.9: [CopyPropagation] Only delete if canonicalized.
2 parents 106d2df + 2be75f3 commit ffda803

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ void CopyPropagation::run() {
600600
// Canonicalize all owned defs.
601601
while (!defWorklist.ownedValues.empty()) {
602602
SILValue def = defWorklist.ownedValues.pop_back_val();
603-
canonicalizer.canonicalizeValueLifetime(def);
603+
auto canonicalized = canonicalizer.canonicalizeValueLifetime(def);
604+
if (!canonicalized)
605+
continue;
604606
// Copies of borrowed values may be dead.
605607
if (auto *inst = def->getDefiningInstruction())
606608
deleter.trackIfDead(inst);

test/SILOptimizer/copy_propagation.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class C {
1616
var a: Builtin.Int64
1717
}
1818

19+
struct MOS : ~Copyable {
20+
deinit {}
21+
}
22+
1923
sil [ossa] @dummy : $@convention(thin) () -> ()
2024
sil [ossa] @barrier : $@convention(thin) () -> ()
2125
sil [ossa] @getOwnedC : $@convention(thin) () -> (@owned C)
@@ -25,6 +29,7 @@ sil [ossa] @takeOwnedCTwice : $@convention(thin) (@owned C, @owned C) -> ()
2529
sil [ossa] @takeGuaranteedC : $@convention(thin) (@guaranteed C) -> ()
2630
sil [ossa] @borrowB : $@convention(thin) (@guaranteed B) -> ()
2731
sil [ossa] @takeGuaranteedAnyObject : $@convention(thin) (@guaranteed AnyObject) -> ()
32+
sil [ossa] @getMOS : $() -> (@owned MOS)
2833

2934
// -O hoists the destroy
3035
//
@@ -989,3 +994,23 @@ bb0:
989994
%99 = tuple ()
990995
return %99 : $()
991996
}
997+
998+
// CHECK-LABEL: sil [ossa] @dontShortenDeadMoveOnlyLifetime : {{.*}} {
999+
// CHECK: [[GET:%[^,]+]] = function_ref @getMOS
1000+
// CHECK: [[BARRIER:%[^,]+]] = function_ref @barrier
1001+
// CHECK: [[MOS:%[^,]+]] = apply [[GET]]()
1002+
// CHECK: [[MOV:%[^,]+]] = move_value [lexical] [[MOS]]
1003+
// CHECK: apply [[BARRIER]]()
1004+
// CHECK: destroy_value [[MOV]]
1005+
// CHECK-LABEL: } // end sil function 'dontShortenDeadMoveOnlyLifetime'
1006+
sil [ossa] @dontShortenDeadMoveOnlyLifetime : $@convention(thin) () -> () {
1007+
%get = function_ref @getMOS : $@convention(thin) () -> (@owned MOS)
1008+
%barrier = function_ref @barrier : $@convention(thin) () -> ()
1009+
%mos = apply %get() : $@convention(thin) () -> (@owned MOS)
1010+
// Note: This must be lexical so that it doesn't get eliminated as redundant.
1011+
%mov = move_value [lexical] %mos : $MOS
1012+
apply %barrier() : $@convention(thin) () -> ()
1013+
destroy_value %mov : $MOS
1014+
%retval = tuple ()
1015+
return %retval : $()
1016+
}

0 commit comments

Comments
 (0)