Skip to content

Commit 0560f8c

Browse files
authored
Merge pull request #31133 from zoecarver/lva/dead-access
2 parents 0959370 + e22e7bc commit 0560f8c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ namespace {
4040
// FIXME: Reconcile the similarities between this and
4141
// isInstructionTriviallyDead.
4242
static bool seemsUseful(SILInstruction *I) {
43+
// begin_access is defined to have side effects, but this is not relevant for
44+
// DCE.
45+
if (isa<BeginAccessInst>(I))
46+
return false;
47+
4348
if (I->mayHaveSideEffects())
4449
return true;
4550

@@ -258,6 +263,10 @@ void DCE::markLive(SILFunction &F) {
258263
}
259264
continue;
260265
}
266+
if (auto *endAccess = dyn_cast<EndAccessInst>(&I)) {
267+
addReverseDependency(endAccess->getBeginAccess(), &I);
268+
continue;
269+
}
261270
if (seemsUseful(&I))
262271
markValueLive(&I);
263272
}

test/SILOptimizer/dead_code_elimination.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,21 @@ bb2:
259259
bb3:
260260
br bb1
261261
}
262+
263+
// Check that DCE eliminates dead access instructions.
264+
// CHECK-LABEL: sil @dead_access
265+
// CHECK: bb0
266+
// CHECK-NEXT: tuple
267+
// CHECK-NEXT: return
268+
// CHECK-LABEL: end sil function 'dead_access'
269+
sil @dead_access : $@convention(thin) (@in Container) -> () {
270+
bb0(%0 : $*Container):
271+
%1 = begin_access [modify] [dynamic] %0 : $*Container
272+
end_access %1 : $*Container
273+
274+
%3 = begin_access [read] [static] %0 : $*Container
275+
end_access %3 : $*Container
276+
277+
%999 = tuple ()
278+
return %999 : $()
279+
}

0 commit comments

Comments
 (0)