Skip to content

Commit 36dd842

Browse files
authored
[ESIMD] Fix intermittent memory corruption in ESIMDOptimizeVecArgCallConv.cpp (#6889)
Functions were erased from Module too early, while iterating over them. Also fix signed/unsigned comparison warnings. Tests: intel/llvm-test-suite#1289 This patch also fixes Linux failures. Signed-off-by: Konstantin S Bobrovsky <konstantin.s.bobrovsky@intel.com>
1 parent 7b02697 commit 36dd842

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

llvm/lib/SYCLLowerIR/ESIMD/ESIMDOptimizeVecArgCallConv.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ optimizeFunction(Function *OldF,
374374
}
375375
Instruction *At = &*(NewF->getEntryBlock().begin());
376376

377-
for (int I = 0; I < NewInsts.size(); ++I) {
377+
for (unsigned I = 0; I < NewInsts.size(); ++I) {
378378
NewInsts[I]->insertBefore(At);
379379
}
380380
return NewF;
@@ -394,7 +394,7 @@ void optimizeCall(CallInst *CI, Function *OptF,
394394
int SretInd = -1;
395395
IRBuilder<> Bld(CI); // insert before CI
396396

397-
for (int I = 0; I < OptimizeableParams.size(); ++I) {
397+
for (unsigned I = 0; I < OptimizeableParams.size(); ++I) {
398398
const auto &PI = OptimizeableParams[I];
399399
auto ArgNo = PI.getFormalParam().getArgNo();
400400

@@ -475,20 +475,14 @@ static bool processFunction(Function *F) {
475475
}
476476
// Optimize the function.
477477
Function *NewF = optimizeFunction(F, OptimizeableParams, NewParamTs);
478-
// Copy users to a separate container, to enable safe eraseFromParent.
479-
SmallVector<User *> FUsers;
480-
std::copy(F->users().begin(), F->users().end(), std::back_inserter(FUsers));
481478

482479
// Optimize calls to the function.
483-
// Iterate over FUsers, to enable safe eraseFromParent in optimizeCall.
484-
for (auto *U : FUsers) {
480+
for (auto *U : F->users()) {
485481
auto *Call = cast<CallInst>(U);
486482
assert(Call->getCalledFunction() == F);
487483
optimizeCall(Call, NewF, OptimizeableParams);
488484
}
489-
std::string Name = F->getName().str();
490-
F->eraseFromParent();
491-
NewF->setName(Name);
485+
NewF->takeName(F);
492486
return true;
493487
}
494488

@@ -506,13 +500,18 @@ ESIMDOptimizeVecArgCallConvPass::run(Module &M, ModuleAnalysisManager &MAM) {
506500
}
507501
#endif // DEBUG_OPT_VEC_ARG_CALL_CONV
508502

509-
SmallVector<Function *, 16> Funcs;
510-
std::for_each(M.begin(), M.end(), [&](Function &F) { Funcs.push_back(&F); });
503+
SmallVector<Function *, 16> ToErase;
511504

512-
// Iterate over Funcs, to enable safe eraseFromParent in processFunction.
513-
for (Function *F : Funcs) {
514-
Modified &= processFunction(F);
505+
for (Function &F : M) {
506+
const bool FReplaced = processFunction(&F);
507+
Modified &= FReplaced;
508+
509+
if (FReplaced) {
510+
ToErase.push_back(&F);
511+
}
515512
}
513+
std::for_each(ToErase.begin(), ToErase.end(),
514+
[](Function *F) { F->eraseFromParent(); });
516515
#ifdef DEBUG_OPT_VEC_ARG_CALL_CONV
517516
{
518517
std::error_code EC;

0 commit comments

Comments
 (0)