Skip to content

Commit 5ffe092

Browse files
committed
[OSSACanOwned] Extend all lifetimes to dead-ends.
Uses of borrows may occur between the borrow and a dead-end regardless of lexicality, so always extend lifetimes to dead-ends.
1 parent 22fdeff commit 5ffe092

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,7 @@ class CanonicalizeOSSALifetime final {
370370
currentDef = def;
371371
currentLexicalLifetimeEnds = lexicalLifetimeEnds;
372372

373-
if (maximizeLifetime || respectsDeinitBarriers()) {
374-
liveness->initializeDiscoveredBlocks(&discoveredBlocks);
375-
}
373+
liveness->initializeDiscoveredBlocks(&discoveredBlocks);
376374
liveness->initializeDef(getCurrentDef());
377375
}
378376

lib/SILOptimizer/Utils/CanonicalizeOSSALifetime.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,18 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
274274
return true;
275275
}
276276

277+
/// Extend liveness to the copy-extended availability boundary of currentDef.
278+
/// Prevents destroys from being inserted between borrows of (copies of) the
279+
/// def and dead-ends.
280+
///
281+
/// Example:
282+
/// %def need not be lexical
283+
/// %c = copy_value %def
284+
/// %sb = store_borrow %c to %addr
285+
/// // Must extend lifetime of %def up to this point. Otherwise, a
286+
/// // destroy_value could be inserted within a borrow scope or interior
287+
/// // pointer use.
288+
/// unreachable
277289
void CanonicalizeOSSALifetime::extendLivenessToDeadEnds() {
278290
// TODO: OSSALifetimeCompletion: Once lifetimes are always complete, delete
279291
// this method.
@@ -1356,8 +1368,8 @@ bool CanonicalizeOSSALifetime::computeLiveness() {
13561368
clear();
13571369
return false;
13581370
}
1371+
extendLivenessToDeadEnds();
13591372
if (respectsDeinitBarriers()) {
1360-
extendLivenessToDeadEnds();
13611373
extendLivenessToDeinitBarriers();
13621374
}
13631375
if (accessBlockAnalysis) {

test/SILOptimizer/assemblyvision_remark/chacha.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func checkResult(_ plaintext: [UInt8]) {
2828

2929
@_semantics("optremark.sil-assembly-vision-remark-gen")
3030
public func run_ChaCha(_ N: Int) {
31-
let key = Array(repeating: UInt8(1), count: 32) // expected-remark {{release of type '}}
32-
let nonce = Array(repeating: UInt8(2), count: 12) // expected-remark {{release of type '}}
31+
let key = Array(repeating: UInt8(1), count: 32) // expected-note {{of 'key}}
32+
let nonce = Array(repeating: UInt8(2), count: 12) // expected-note {{of 'nonce}}
3333

3434
var checkedtext = Array(repeating: UInt8(0), count: 1024) // expected-note {{of 'checkedtext}}
3535
ChaCha20.encrypt(bytes: &checkedtext, key: key, nonce: nonce)
@@ -44,3 +44,5 @@ public func run_ChaCha(_ N: Int) {
4444
}
4545
} // expected-remark {{release of type '}}
4646
// expected-remark @-1 {{release of type '}}
47+
// expected-remark @-2 {{release of type '}}
48+
// expected-remark @-3 {{release of type '}}

test/SILOptimizer/canonicalize_ossa_lifetime_unit.sil

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,33 @@ die:
885885
unreachable
886886
}
887887

888+
// CHECK-LABEL: begin running test {{.*}} on consume_copy_before_use_in_dead_end_2
889+
// CHECK-LABEL: sil [ossa] @consume_copy_before_use_in_dead_end_2 : {{.*}} {
890+
// CHECK-NOT: destroy_value [dead_end]
891+
// CHECK-LABEL: } // end sil function 'consume_copy_before_use_in_dead_end_2'
892+
// CHECK-LABEL: end running test {{.*}} on consume_copy_before_use_in_dead_end_2
893+
sil [ossa] @consume_copy_before_use_in_dead_end_2 : $@convention(thin) (@owned C) -> () {
894+
entry(%c : @owned $C):
895+
cond_br undef, exit, die
896+
897+
exit:
898+
destroy_value %c
899+
%retval = tuple ()
900+
return %retval
901+
902+
die:
903+
%move = move_value %c
904+
%copy = copy_value %move
905+
specify_test "canonicalize_ossa_lifetime true false true %move"
906+
apply undef(%move) : $@convention(thin) (@owned C) -> ()
907+
%addr = alloc_stack $C
908+
%token = store_borrow %copy to %addr
909+
apply undef() : $@convention(thin) () -> ()
910+
%reload = load_borrow %token
911+
apply undef(%reload) : $@convention(thin) (@guaranteed C) -> ()
912+
unreachable
913+
}
914+
888915
// The destroy of a value must not be hoisted over a destroy of a copy of a
889916
// partial_apply [on_stack] which captures the value.
890917
// CHECK-LABEL: begin running test {{.*}} on destroy_after_pa_copy_destroy

0 commit comments

Comments
 (0)