Skip to content

Commit 2eb6d48

Browse files
committed
[AccessMarker] Fix a reverse instruction iterator.
I reversed this loop's direction over the instruction list and forgot to change the order of erasing an instruction with respect to advancing the iterator. Thankfully ASAN is far smarter than I. Converting between forward/reverse iterators makes the loop unreadable. Add an iterator return value to BasicBlock::erase(SILInstruction*).
1 parent 45fbd87 commit 2eb6d48

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
9494
void push_back(SILInstruction *I);
9595
void push_front(SILInstruction *I);
9696
void remove(SILInstruction *I);
97-
void erase(SILInstruction *I);
97+
iterator erase(SILInstruction *I);
9898

9999
SILInstruction &back() { return InstList.back(); }
100100
const SILInstruction &back() const {

lib/SIL/SILBasicBlock.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ void SILBasicBlock::remove(SILInstruction *I) {
9090
InstList.remove(I);
9191
}
9292

93-
void SILBasicBlock::erase(SILInstruction *I) {
93+
SILBasicBlock::iterator SILBasicBlock::erase(SILInstruction *I) {
9494
// Notify the delete handlers that this instruction is going away.
9595
getModule().notifyDeleteHandlers(&*I);
9696
auto *F = getParent();
97-
InstList.erase(I);
97+
auto II = InstList.erase(I);
9898
F->getModule().deallocateInst(I);
99+
return II;
99100
}
100101

101102
/// This method unlinks 'self' from the containing SILFunction and deletes it.

lib/SILOptimizer/Mandatory/AccessMarkerElimination.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ struct AccessMarkerElimination : SILModuleTransform {
4545
// iterating in reverse eliminates more begin_access users before they
4646
// need to be replaced.
4747
for (auto &BB : reversed(F)) {
48-
for (auto II = BB.rbegin(), IE = BB.rend(); II != IE;) {
49-
SILInstruction *inst = &*II;
50-
++II;
48+
// Don't cache the begin iterator since we're reverse iterating.
49+
for (auto II = BB.end(); II != BB.begin();) {
50+
SILInstruction *inst = &*(--II);
5151

5252
if (auto beginAccess = dyn_cast<BeginAccessInst>(inst)) {
5353
beginAccess->replaceAllUsesWith(beginAccess->getSource());
54-
beginAccess->eraseFromParent();
54+
II = BB.erase(beginAccess);
5555
}
5656
if (auto endAccess = dyn_cast<EndAccessInst>(inst)) {
5757
assert(endAccess->use_empty());
58-
endAccess->eraseFromParent();
58+
II = BB.erase(endAccess);
5959
}
6060
}
6161
}

0 commit comments

Comments
 (0)