Skip to content

[LVA] Don't invalidate reads/writes that don't alias any tracked values. #31136

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

Closed
Closed
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
14 changes: 12 additions & 2 deletions lib/SILOptimizer/Transforms/DeadStoreElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,12 @@ void DSEContext::processUnknownReadInstForGenKillSet(SILInstruction *I) {
for (unsigned i = 0; i < S->LocationNum; ++i) {
if (!S->BBMaxStoreSet.test(i))
continue;
if (!AA->mayReadFromMemory(I, LocationVault[i].getBase()))
auto val = LocationVault[i].getBase();
if (!AA->mayReadFromMemory(I, val))
continue;
if (llvm::all_of(I->getAllOperands(), [&AA = AA, &val](Operand &op) {
return AA->isNoAlias(op.get(), val);
}))
continue;
// Update the genset and kill set.
S->startTrackingLocation(S->BBKillSet, i);
Expand All @@ -1102,7 +1107,12 @@ void DSEContext::processUnknownReadInstForDSE(SILInstruction *I) {
for (unsigned i = 0; i < S->LocationNum; ++i) {
if (!S->isTrackingLocation(S->BBWriteSetMid, i))
continue;
if (!AA->mayReadFromMemory(I, LocationVault[i].getBase()))
auto val = LocationVault[i].getBase();
if (!AA->mayReadFromMemory(I, val))
continue;
if (llvm::all_of(I->getAllOperands(), [&AA = AA, &val](Operand &op) {
return AA->isNoAlias(op.get(), val);
}))
continue;
S->stopTrackingLocation(S->BBWriteSetMid, i);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/SILOptimizer/Transforms/RedundantLoadElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,10 @@ void BlockState::processUnknownWriteInstForGenKillSet(RLEContext &Ctx,
LSLocation &R = Ctx.getLocation(i);
if (!AA->mayWriteToMemory(I, R.getBase()))
continue;
if (llvm::all_of(I->getAllOperands(), [&AA, &R](Operand &op) {
return AA->isNoAlias(op.get(), R.getBase());
}))
continue;
// MayAlias.
stopTrackingLocation(BBGenSet, i);
startTrackingLocation(BBKillSet, i);
Expand All @@ -999,6 +1003,10 @@ void BlockState::processUnknownWriteInstForRLE(RLEContext &Ctx,
LSLocation &R = Ctx.getLocation(i);
if (!AA->mayWriteToMemory(I, R.getBase()))
continue;
if (llvm::all_of(I->getAllOperands(), [&AA, &R](Operand &op) {
return AA->isNoAlias(op.get(), R.getBase());
}))
continue;
// MayAlias.
stopTrackingLocation(ForwardSetIn, i);
stopTrackingValue(ForwardValIn, i);
Expand Down
40 changes: 40 additions & 0 deletions test/SILOptimizer/dead_store_elim.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,46 @@ bb0(%0 : $*foo):
return %20 : $()
}

class Foo {
@_hasStorage @_hasInitialValue var x: Int { get set }
deinit
init()
}

class Bar {
@_hasStorage @_hasInitialValue var x: Int { get set }
deinit
init()
}

// CHECK-LABEL: @unknown_unrelated_read
// CHECK: store
// CHECK-NOT: store
// CHECK-LABEL: end sil function 'unknown_unrelated_read'
sil hidden @unknown_unrelated_read : $@convention(thin) (@inout Int) -> () {
bb0(%0 : $*Int):
%1 = alloc_ref $Foo
%3 = integer_literal $Builtin.Int64, 0
%4 = struct $Int (%3 : $Builtin.Int64)
%5 = ref_element_addr %1 : $Foo, #Foo.x
store %4 to %5 : $*Int

%10 = alloc_ref [stack] $Bar
%12 = ref_element_addr %10 : $Bar, #Bar.x
%15 = integer_literal $Builtin.Int64, 1
%16 = struct $Int (%15 : $Builtin.Int64)
store %16 to %12 : $*Int

set_deallocating %10 : $Bar
dealloc_ref %10 : $Bar
dealloc_ref [stack] %10 : $Bar

copy_addr %5 to %0 : $*Int

%20 = tuple()
return %20 : $()
}

// Check that begin_access, end_access, strong_release, set_deallocating, and dealloc_ref don't prevent optimization.
// CHECK-LABEL: ignore_read_write
// CHECK: bb0
Expand Down