Skip to content

Commit

Permalink
Fix delete of functions that becomes unused (intel#2109)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wenju-he authored and jsji committed Aug 11, 2023
1 parent 89287a0 commit 7a4574a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
9 changes: 2 additions & 7 deletions llvm-spirv/lib/SPIRV/OCLToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,8 @@ bool OCLToSPIRVBase::runOCLToSPIRV(Module &Module) {

visit(*M);

for (auto &I : ValuesToDelete)
if (auto Inst = dyn_cast<Instruction>(I))
Inst->eraseFromParent();
for (auto &I : ValuesToDelete)
if (auto GV = dyn_cast<GlobalValue>(I))
GV->eraseFromParent();
for (Instruction *I : ValuesToDelete)
I->eraseFromParent();

eraseUselessFunctions(M); // remove unused functions declarations
LLVM_DEBUG(dbgs() << "After OCLToSPIRV:\n" << *M);
Expand Down Expand Up @@ -1060,7 +1056,6 @@ bool OCLToSPIRVBase::eraseUselessConvert(CallInst *CI, StringRef MangledName,
<< *CI->getArgOperand(0) << '\n');
CI->replaceAllUsesWith(CI->getArgOperand(0));
ValuesToDelete.insert(CI);
ValuesToDelete.insert(CI->getCalledFunction());
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion llvm-spirv/lib/SPIRV/OCLToSPIRV.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class OCLToSPIRVBase : public InstVisitor<OCLToSPIRVBase>, BuiltinCallHelper {
private:
LLVMContext *Ctx;
unsigned CLVer; /// OpenCL version as major*10+minor
std::set<Value *> ValuesToDelete;
std::set<Instruction *> ValuesToDelete;
OCLTypeToSPIRVBase *OCLTypeToSPIRVPtr;

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

0 comments on commit 7a4574a

Please sign in to comment.