Skip to content

Commit 7a4574a

Browse files
wenju-hejsji
authored andcommitted
Fix delete of functions that becomes unused (#2109)
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. Original commit: KhronosGroup/SPIRV-LLVM-Translator@aea1ac7
1 parent 89287a0 commit 7a4574a

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

llvm-spirv/lib/SPIRV/OCLToSPIRV.cpp

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

177177
visit(*M);
178178

179-
for (auto &I : ValuesToDelete)
180-
if (auto Inst = dyn_cast<Instruction>(I))
181-
Inst->eraseFromParent();
182-
for (auto &I : ValuesToDelete)
183-
if (auto GV = dyn_cast<GlobalValue>(I))
184-
GV->eraseFromParent();
179+
for (Instruction *I : ValuesToDelete)
180+
I->eraseFromParent();
185181

186182
eraseUselessFunctions(M); // remove unused functions declarations
187183
LLVM_DEBUG(dbgs() << "After OCLToSPIRV:\n" << *M);
@@ -1060,7 +1056,6 @@ bool OCLToSPIRVBase::eraseUselessConvert(CallInst *CI, StringRef MangledName,
10601056
<< *CI->getArgOperand(0) << '\n');
10611057
CI->replaceAllUsesWith(CI->getArgOperand(0));
10621058
ValuesToDelete.insert(CI);
1063-
ValuesToDelete.insert(CI->getCalledFunction());
10641059
return true;
10651060
}
10661061
return false;

llvm-spirv/lib/SPIRV/OCLToSPIRV.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class OCLToSPIRVBase : public InstVisitor<OCLToSPIRVBase>, BuiltinCallHelper {
268268
private:
269269
LLVMContext *Ctx;
270270
unsigned CLVer; /// OpenCL version as major*10+minor
271-
std::set<Value *> ValuesToDelete;
271+
std::set<Instruction *> ValuesToDelete;
272272
OCLTypeToSPIRVBase *OCLTypeToSPIRVPtr;
273273

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

0 commit comments

Comments
 (0)