Skip to content

Commit abd2dc9

Browse files
authored
[VectorCombine] Avoid double deletion in eraseInstruction (#155621)
Consider the following pattern: ``` C = op A B D = op C E = op D, C ``` As `E` is dead, we call `eraseInstruction(E)` and see if its operands become dead. `RecursivelyDeleteTriviallyDeadInstructions(D)` also erases `C`, which causes a UAF crash in the subsequent call `RecursivelyDeleteTriviallyDeadInstructions(C)`. This patch also adds deleted ops into the visit list to avoid double deletion. Closes #155543.
1 parent f3a5c16 commit abd2dc9

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,16 @@ class VectorCombine {
173173
// further folds that were hindered by OneUse limits.
174174
SmallPtrSet<Value *, 4> Visited;
175175
for (Value *Op : Ops) {
176-
if (Visited.insert(Op).second) {
176+
if (!Visited.contains(Op)) {
177177
if (auto *OpI = dyn_cast<Instruction>(Op)) {
178178
if (RecursivelyDeleteTriviallyDeadInstructions(
179-
OpI, nullptr, nullptr, [this](Value *V) {
179+
OpI, nullptr, nullptr, [&](Value *V) {
180180
if (auto *I = dyn_cast<Instruction>(V)) {
181181
LLVM_DEBUG(dbgs() << "VC: Erased: " << *I << '\n');
182182
Worklist.remove(I);
183183
if (I == NextInst)
184184
NextInst = NextInst->getNextNode();
185+
Visited.insert(I);
185186
}
186187
}))
187188
continue;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- | FileCheck %s
3+
4+
; Make sure we don't double delete a dead instruction.
5+
6+
define void @pr155543() {
7+
; CHECK-LABEL: define void @pr155543() {
8+
; CHECK-NEXT: ret void
9+
;
10+
%shuffle1 = shufflevector <4 x double> poison, <4 x double> poison, <8 x i32> <i32 poison, i32 poison, i32 poison, i32 poison, i32 0, i32 1, i32 2, i32 3>
11+
%shuffle2 = shufflevector <8 x double> poison, <8 x double> %shuffle1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 12, i32 13, i32 14, i32 15>
12+
%fadd = fadd <8 x double> %shuffle1, zeroinitializer
13+
%dead = shufflevector <8 x double> %fadd, <8 x double> %shuffle2, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 12, i32 13, i32 14, i32 15>
14+
ret void
15+
}

0 commit comments

Comments
 (0)