Skip to content

TempLValue opt: don't insert destroy_addr too early due to ignoring deinit barriers #68903

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
Oct 4, 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
11 changes: 11 additions & 0 deletions lib/SILOptimizer/Transforms/TempLValueOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
#include "swift/SIL/NodeBits.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILBasicBlock.h"
Expand Down Expand Up @@ -176,6 +177,13 @@ void TempLValueOptPass::tempLValueOpt(CopyAddrInst *copyInst) {
// but a block argument.
SILInstruction *destRootInst = destRootAddr->getDefiningInstruction();

bool needDestroyEarly = false;
BasicCalleeAnalysis *bca = nullptr;
if (!copyInst->isInitializationOfDest()) {
needDestroyEarly = true;
bca = PM->getAnalysis<BasicCalleeAnalysis>();
}

// Iterate over the liferange of the temporary and make some validity checks.
AliasAnalysis *AA = nullptr;
SILInstruction *beginOfLiferange = nullptr;
Expand Down Expand Up @@ -220,6 +228,9 @@ void TempLValueOptPass::tempLValueOpt(CopyAddrInst *copyInst) {
// Needed to treat init_existential_addr as not-writing projection.
projections.contains(inst) == 0)
return;

if (needDestroyEarly && isDeinitBarrier(inst, bca))
return;
}
}
assert(endOfLiferangeReached);
Expand Down
34 changes: 34 additions & 0 deletions test/SILOptimizer/templvalueopt_ossa.sil
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -temp-lvalue-opt | %FileCheck %s

// REQUIRES: swift_in_compiler

import Swift
import Builtin

Expand Down Expand Up @@ -300,3 +302,35 @@ bb0(%0 : $*T):
return %78 : $()
}

sil @createAny : $@convention(thin) () -> @out Any
sil @createAny_no_barrier : $@convention(thin) () -> @out Any {
[global:]
}

// CHECK-LABEL: sil [ossa] @test_deinit_barrier :
// CHECK: copy_addr [take]
// CHECK-LABEL: } // end sil function 'test_deinit_barrier'
sil [ossa] @test_deinit_barrier : $@convention(thin) (@guaranteed Any, @inout Any) -> () {
bb0(%0 : @guaranteed $Any, %1 : $*Any):
%2 = alloc_stack $Any
%4 = function_ref @createAny : $@convention(thin) () -> @out Any
%5 = apply %4(%2) : $@convention(thin) () -> @out Any
copy_addr [take] %2 to %1 : $*Any
dealloc_stack %2 : $*Any
%11 = tuple ()
return %11 : $()
}

// CHECK-LABEL: sil [ossa] @test_no_deinit_barrier :
// CHECK-NOT: copy_addr
// CHECK-LABEL: } // end sil function 'test_no_deinit_barrier'
sil [ossa] @test_no_deinit_barrier : $@convention(thin) (@guaranteed Any, @inout Any) -> () {
bb0(%0 : @guaranteed $Any, %1 : $*Any):
%2 = alloc_stack $Any
%4 = function_ref @createAny_no_barrier : $@convention(thin) () -> @out Any
%5 = apply %4(%2) : $@convention(thin) () -> @out Any
copy_addr [take] %2 to %1 : $*Any
dealloc_stack %2 : $*Any
%11 = tuple ()
return %11 : $()
}