Skip to content

Commit 43baf90

Browse files
authored
Fix delete of functions that becomes unused (#2109) (#2114)
After the first loop of deleting instructions in ValuesToDelete, deleted instructions in ValuesToDelete are in an unstable state. Then in the second loop of deleting, dyn_cast to GlobalValue could return true for an instruction and double eraseFromParent causes crash. Global values in ValuesToDelete are functions. Unused functions are deleted by eraseUselessFunctions anyway. (cherry picked from commit aea1ac7)
1 parent 34e1327 commit 43baf90

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

lib/SPIRV/OCLToSPIRV.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,8 @@ bool OCLToSPIRVBase::runOCLToSPIRV(Module &Module) {
159159

160160
visit(*M);
161161

162-
for (auto &I : ValuesToDelete)
163-
if (auto Inst = dyn_cast<Instruction>(I))
164-
Inst->eraseFromParent();
165-
for (auto &I : ValuesToDelete)
166-
if (auto GV = dyn_cast<GlobalValue>(I))
167-
GV->eraseFromParent();
162+
for (Instruction *I : ValuesToDelete)
163+
I->eraseFromParent();
168164

169165
eraseUselessFunctions(M); // remove unused functions declarations
170166
LLVM_DEBUG(dbgs() << "After OCLToSPIRV:\n" << *M);
@@ -1135,7 +1131,6 @@ bool OCLToSPIRVBase::eraseUselessConvert(CallInst *CI, StringRef MangledName,
11351131
<< *CI->getArgOperand(0) << '\n');
11361132
CI->replaceAllUsesWith(CI->getArgOperand(0));
11371133
ValuesToDelete.insert(CI);
1138-
ValuesToDelete.insert(CI->getCalledFunction());
11391134
return true;
11401135
}
11411136
return false;

lib/SPIRV/OCLToSPIRV.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class OCLToSPIRVBase : public InstVisitor<OCLToSPIRVBase> {
267267
Module *M;
268268
LLVMContext *Ctx;
269269
unsigned CLVer; /// OpenCL version as major*10+minor
270-
std::set<Value *> ValuesToDelete;
270+
std::set<Instruction *> ValuesToDelete;
271271
OCLTypeToSPIRVBase *OCLTypeToSPIRVPtr;
272272

273273
ConstantInt *addInt32(int I) { return getInt32(M, I); }

0 commit comments

Comments
 (0)