Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Instrumentation/CilkSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ static void setupBlock(BasicBlock *BB, DominatorTree *DT, LoopInfo *LI,
// Partition the predecessors of the landing pad.
for (BasicBlock *Pred : predecessors(BB)) {
if (isa<DetachInst>(Pred->getTerminator()) ||
isa<ReattachInst>(Pred->getTerminator()) ||
isDetachedRethrow(Pred->getTerminator()))
DetachPreds.push_back(Pred);
else if (isTaskFrameResume(Pred->getTerminator()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ static void setupBlock(BasicBlock *BB, const TargetLibraryInfo *TLI,
// Partition the predecessors of the landing pad.
for (BasicBlock *Pred : predecessors(BB)) {
if (isa<DetachInst>(Pred->getTerminator()) ||
isa<ReattachInst>(Pred->getTerminator()) ||
isDetachedRethrow(Pred->getTerminator()))
DetachPreds.push_back(Pred);
else if (isTaskFrameResume(Pred->getTerminator()))
Expand Down
44 changes: 34 additions & 10 deletions llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,17 +508,41 @@ BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
// block.
assert(BB->getTerminator()->getNumSuccessors() == 1 &&
"Should have a single succ!");
BasicBlock *NewBB = SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU);
if (SyncInst *OldSI = dyn_cast<SyncInst>(NewBB->getTerminator())) {
// Make sure the original BB is terminated by the sync.
SyncInst *SI = SyncInst::Create(NewBB, OldSI->getSyncRegion(),
BB->getTerminator());
BranchInst::Create(Succ, OldSI);
SI->setDebugLoc(OldSI->getDebugLoc());
BB->getTerminator()->eraseFromParent();
OldSI->eraseFromParent();
if (SyncInst *OldSI = dyn_cast<SyncInst>(BB->getTerminator())) {
// Insert a new basic block after BB.
BasicBlock *NewBB =
BasicBlock::Create(BB->getContext(), BB->getName() + ".split",
BB->getParent(), BB->getNextNode());
// Terminate that block with an unconditional branch to Succ.
BranchInst::Create(Succ, NewBB);
// Update the successor of the sync instruction to be NewBB.
OldSI->setSuccessor(0, NewBB);
// Update any PHI ndes in Succ.
NewBB->replaceSuccessorsPhiUsesWith(BB, NewBB);

// The new block lives in whichever loop the old one did. This preserves
// LCSSA as well, because we force the split point to be after any PHI
// nodes.
if (LI)
if (Loop *L = LI->getLoopFor(BB))
L->addBasicBlockToLoop(NewBB, *LI);

if (DT)
// Old dominates New. New node dominates all other nodes dominated by Old.
if (DomTreeNode *OldNode = DT->getNode(BB)) {
std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());

DomTreeNode *NewNode = DT->addNewBlock(NewBB, BB);
for (DomTreeNode *I : Children)
DT->changeImmediateDominator(I, NewNode);
}

// Note: We don't need to update MSSA in this case, because the sync
// instruction remains in the original basic block.
return NewBB;
} else {
return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU);
}
return NewBB;
}

unsigned
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Utils/LoopSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist,
if (Preheader && isa<SyncInst>(Preheader->getTerminator())) {
LLVM_DEBUG(dbgs()
<< "LoopSimplify: Splitting sync-terminated preheader.\n");
SplitEdge(Preheader, L->getHeader(), DT, LI);
SplitEdge(Preheader, L->getHeader(), DT, LI, MSSAU);
Changed = true;
Preheader = L->getLoopPreheader();
}

Expand Down
67 changes: 67 additions & 0 deletions llvm/test/Transforms/Tapir/csi-detach-continue-split.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
; Check that the CSI pass properly splits the predecessors of a detach-continuation block.
;
; RUN: opt < %s -csi -S -o - | FileCheck %s
; RUN: opt < %s -passes='csi' -S -o - | FileCheck %s

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: argmemonly nounwind willreturn
declare token @llvm.syncregion.start() #0

define dso_local void @QuadNode_addIntersections() local_unnamed_addr #1 {
entry:
%syncreg.i28 = tail call token @llvm.syncregion.start()
br i1 undef, label %if.then.tf, label %if.else

if.then.tf: ; preds = %entry
detach within %syncreg.i28, label %det.achd, label %det.cont.tf

det.achd: ; preds = %if.then.tf
unreachable

det.cont.tf: ; preds = %if.then.tf
detach within %syncreg.i28, label %det.achd1.tf.tf.tf, label %if.end

; CHECK: det.cont.tf:
; CHECK: detach within %syncreg.i28, label %[[DETACHED:.+]], label %[[CONTINUE:.+]]

; CHECK: [[CONTINUE]]:
; CHECK-NEXT: call void @__csi_detach_continue(
; CHECK-NEXT: br label %if.end

det.achd1.tf.tf.tf: ; preds = %det.cont.tf
br label %det.achd1.tf.tf.tf.tf

det.achd1.tf.tf.tf.tf: ; preds = %det.achd1.tf.tf.tf
%tf.i = tail call token @llvm.taskframe.create()
%syncreg.i = tail call token @llvm.syncregion.start() #2
reattach within %syncreg.i28, label %if.end

; CHECK: [[DETACHED]]:
; CHECK: reattach within %syncreg.i28, label %[[CONTINUE]]

if.else: ; preds = %entry
sync within %syncreg.i28, label %if.end

; CHECK: if.else:
; CHECK: sync within %syncreg.i28, label %[[SYNC_SPLIT:.+]]

; CHECK: [[SYNC_SPLIT]]:
; CHECK: call void @__csi_after_sync(
; CHECK: br label %if.end

if.end: ; preds = %if.else, %det.achd1.tf.tf.tf.tf, %det.cont.tf
unreachable
}

; Function Attrs: argmemonly nounwind willreturn
declare token @llvm.taskframe.create() #0

attributes #0 = { argmemonly nounwind willreturn }
attributes #1 = { "use-soft-float"="false" }
attributes #2 = { nounwind }

!llvm.ident = !{!0}

!0 = !{!"clang version 10.0.1 (git@github.com:OpenCilk/opencilk-project.git 94a2a3eb6c9f8dd53def6e21c5ab8ff45acb586f)"}
Loading