Skip to content

Commit

Permalink
[Tapir,Cilk] Fix a memory error and some memory leaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
neboat committed Oct 13, 2023
1 parent 03a724d commit b779ea4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGCilk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,13 @@ llvm::Instruction *CodeGenFunction::EmitSyncRegionStart() {

/// EmitCilkSyncStmt - Emit a _Cilk_sync node.
void CodeGenFunction::EmitCilkSyncStmt(const CilkSyncStmt &S) {
llvm::BasicBlock *ContinueBlock = createBasicBlock("sync.continue");

// Check if we are generating unreachable code.
if (!HaveInsertPoint())
// We don't need to generate actual code.
return;

llvm::BasicBlock *ContinueBlock = createBasicBlock("sync.continue");

// Generate a stoppoint if we are emitting debug info.
EmitStopPoint(&S);

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,11 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
ReturnValue = Address::invalid();
}
}

if (CurSyncRegion) {
PopSyncRegion();
assert(!CurSyncRegion && "Nested sync regions at end of function.");
}
}

/// ShouldInstrumentFunction - Return true if the current function should be
Expand Down
14 changes: 11 additions & 3 deletions llvm/include/llvm/Analysis/TapirTaskInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class Spindle {
friend class TaskInfo;

/// This creates an empty spindle.
Spindle() {}
Spindle() = default;

explicit Spindle(BasicBlock *BB, SPType Ty) : Ty(Ty) {
Blocks.push_back(BB);
Expand Down Expand Up @@ -1154,7 +1154,7 @@ class TaskInfo {
SmallPtrSetImpl<Spindle *> &SubTFVisited);

public:
TaskInfo() {}
TaskInfo() = default;
~TaskInfo() { releaseMemory(); }

TaskInfo(TaskInfo &&Arg)
Expand All @@ -1178,14 +1178,22 @@ class TaskInfo {
}

void releaseMemory() {
for (auto BBToSpindle : BBMap)
if (!BBToSpindle.getSecond()->getParentTask())
BBToSpindle.getSecond()->~Spindle();
for (auto SpindleToTask : SpindleMap)
if (RootTask != SpindleToTask.getSecond() &&
!SpindleToTask.getSecond()->getParentTask())
SpindleToTask.getSecond()->~Task();

BBMap.clear();
SpindleMap.clear();
if (RootTask)
RootTask->~Task();
RootTask = nullptr;
if (MPTasks) {
MPTasks->TaskList.clear();
MPTasks.release();
MPTasks.reset();
}
ComputedTaskFrameTree = false;
TaskAllocator.Reset();
Expand Down
4 changes: 3 additions & 1 deletion llvm/include/llvm/Transforms/Tapir/Outline.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class OutlineMaterializer : public ValueMaterializer {
public:
OutlineMaterializer(const Value *SrcSyncRegion = nullptr)
: SrcSyncRegion(SrcSyncRegion) {}
virtual ~OutlineMaterializer() {}
virtual ~OutlineMaterializer() {
BlocksToRemap.clear();
}

Value *materialize(Value *V) override;

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/Tapir/LoopSpawningTI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,10 +1332,12 @@ Function *LoopSpawningImpl::createHelperForTapirLoop(
// argument, undo the eariler temporarily mapping.
if (!isa<Constant>(TL->getTripCount()) && !Args.count(TL->getTripCount())) {
VMap.erase(TL->getTripCount());
// Delete the ArgEndMaterializer.
delete Mat;
}

// Delete the ArgEndMaterializer or OutlineMaterializer.
if (Mat)
delete Mat;

// Rewrite cloned IV's to start at their start-iteration arguments.
updateClonedIVs(TL, Preheader, Args, VMap, IVArgIndex);

Expand Down
13 changes: 9 additions & 4 deletions llvm/lib/Transforms/Utils/TapirUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,15 +712,19 @@ void llvm::cloneEHBlocks(Function *F,
// relevant loops.
for (BasicBlock *EHBlock : EHBlocksToClone) {
if (!DT->isReachableFromEntry(EHBlock)) {
Loop *L = nullptr;
if (LI->isLoopHeader(EHBlock)) {
// Delete the whole loop.
Loop *L = LI->getLoopFor(EHBlock);
L = LI->getLoopFor(EHBlock);
if (Loop *ParentL = L->getParentLoop())
ParentL->removeChildLoop(llvm::find(*ParentL, L));
else
LI->removeLoop(llvm::find(*LI, L));
}
LI->removeBlock(EHBlock);
// If EHBlock is a loop header, finish destroying the whole loop.
if (L)
LI->destroy(L);
}
}
}
Expand Down Expand Up @@ -913,7 +917,8 @@ void llvm::SerializeDetach(DetachInst *DI, BasicBlock *ParentEntry,
}

// Handle any detached-rethrows in the task.
if (DI->hasUnwindDest()) {
bool HasUnwind = DI->hasUnwindDest();
if (HasUnwind) {
assert(InlinedLPads && "Missing set of landing pads in task.");
assert(DetachedRethrows && "Missing set of detached rethrows in task.");
if (ReplaceWithTaskFrame) {
Expand Down Expand Up @@ -960,7 +965,7 @@ void llvm::SerializeDetach(DetachInst *DI, BasicBlock *ParentEntry,

// Replace the detach with an unconditional branch to the task entry.
Continue->removePredecessor(Spawner);
if (DI->hasUnwindDest())
if (HasUnwind)
Unwind->removePredecessor(Spawner);
ReplaceInstWithInst(DI, BranchInst::Create(TaskEntry));

Expand All @@ -972,7 +977,7 @@ void llvm::SerializeDetach(DetachInst *DI, BasicBlock *ParentEntry,
if (DT) {
if (ReattachDom && DT->dominates(Spawner, Continue))
DT->changeImmediateDominator(Continue, ReattachDom);
if (DI->hasUnwindDest())
if (HasUnwind)
DT->deleteEdge(Spawner, Unwind);
}
}
Expand Down

0 comments on commit b779ea4

Please sign in to comment.