Skip to content

Commit 313c130

Browse files
committed
[LVA] Don't invalidate reads/writes that don't alias any tracked values.
In DSE and RLE any unkown read/write often causes most if not all tracked values to be invalidated. This doesn't need to be the case. If the tracked value doesn't alias any of the operands of the unkown instruction, then we can safely keep the tracked value validated.
1 parent dc122a3 commit 313c130

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

lib/SILOptimizer/Transforms/DeadStoreElimination.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,12 @@ void DSEContext::processUnknownReadInstForGenKillSet(SILInstruction *I) {
10871087
for (unsigned i = 0; i < S->LocationNum; ++i) {
10881088
if (!S->BBMaxStoreSet.test(i))
10891089
continue;
1090-
if (!AA->mayReadFromMemory(I, LocationVault[i].getBase()))
1090+
auto val = LocationVault[i].getBase();
1091+
if (!AA->mayReadFromMemory(I, val))
1092+
continue;
1093+
if (llvm::all_of(I->getAllOperands(), [&AA = AA, &val](Operand &op) {
1094+
return AA->isNoAlias(op.get(), val);
1095+
}))
10911096
continue;
10921097
// Update the genset and kill set.
10931098
S->startTrackingLocation(S->BBKillSet, i);
@@ -1100,7 +1105,12 @@ void DSEContext::processUnknownReadInstForDSE(SILInstruction *I) {
11001105
for (unsigned i = 0; i < S->LocationNum; ++i) {
11011106
if (!S->isTrackingLocation(S->BBWriteSetMid, i))
11021107
continue;
1103-
if (!AA->mayReadFromMemory(I, LocationVault[i].getBase()))
1108+
auto val = LocationVault[i].getBase();
1109+
if (!AA->mayReadFromMemory(I, val))
1110+
continue;
1111+
if (llvm::all_of(I->getAllOperands(), [&AA = AA, &val](Operand &op) {
1112+
return AA->isNoAlias(op.get(), val);
1113+
}))
11041114
continue;
11051115
S->stopTrackingLocation(S->BBWriteSetMid, i);
11061116
}

lib/SILOptimizer/Transforms/RedundantLoadElimination.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,10 @@ void BlockState::processUnknownWriteInstForGenKillSet(RLEContext &Ctx,
977977
LSLocation &R = Ctx.getLocation(i);
978978
if (!AA->mayWriteToMemory(I, R.getBase()))
979979
continue;
980+
if (llvm::all_of(I->getAllOperands(), [&AA, &R](Operand &op) {
981+
return AA->isNoAlias(op.get(), R.getBase());
982+
}))
983+
continue;
980984
// MayAlias.
981985
stopTrackingLocation(BBGenSet, i);
982986
startTrackingLocation(BBKillSet, i);
@@ -996,6 +1000,10 @@ void BlockState::processUnknownWriteInstForRLE(RLEContext &Ctx,
9961000
LSLocation &R = Ctx.getLocation(i);
9971001
if (!AA->mayWriteToMemory(I, R.getBase()))
9981002
continue;
1003+
if (llvm::all_of(I->getAllOperands(), [&AA, &R](Operand &op) {
1004+
return AA->isNoAlias(op.get(), R.getBase());
1005+
}))
1006+
continue;
9991007
// MayAlias.
10001008
stopTrackingLocation(ForwardSetIn, i);
10011009
stopTrackingValue(ForwardValIn, i);

test/SILOptimizer/dead_store_elim.sil

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,3 +1501,43 @@ bb0(%0 : $*foo):
15011501
%20 = tuple()
15021502
return %20 : $()
15031503
}
1504+
1505+
class Foo {
1506+
@_hasStorage @_hasInitialValue var x: Int { get set }
1507+
deinit
1508+
init()
1509+
}
1510+
1511+
class Bar {
1512+
@_hasStorage @_hasInitialValue var x: Int { get set }
1513+
deinit
1514+
init()
1515+
}
1516+
1517+
// CHECK-LABEL: @unknown_unrelated_read
1518+
// CHECK: store
1519+
// CHECK-NOT: store
1520+
// CHECK-LABEL: end sil function 'unknown_unrelated_read'
1521+
sil hidden @unknown_unrelated_read : $@convention(thin) (@inout Int) -> () {
1522+
bb0(%0 : $*Int):
1523+
%1 = alloc_ref $Foo
1524+
%3 = integer_literal $Builtin.Int64, 0
1525+
%4 = struct $Int (%3 : $Builtin.Int64)
1526+
%5 = ref_element_addr %1 : $Foo, #Foo.x
1527+
store %4 to %5 : $*Int
1528+
1529+
%10 = alloc_ref [stack] $Bar
1530+
%12 = ref_element_addr %10 : $Bar, #Bar.x
1531+
%15 = integer_literal $Builtin.Int64, 1
1532+
%16 = struct $Int (%15 : $Builtin.Int64)
1533+
store %16 to %12 : $*Int
1534+
1535+
set_deallocating %10 : $Bar
1536+
dealloc_ref %10 : $Bar
1537+
dealloc_ref [stack] %10 : $Bar
1538+
1539+
copy_addr %5 to %0 : $*Int
1540+
1541+
%20 = tuple()
1542+
return %20 : $()
1543+
}

0 commit comments

Comments
 (0)