Skip to content

Revert "[SimplifyCFG] Skip threading if the target may have divergent branches" #100994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
27 changes: 12 additions & 15 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3246,12 +3246,7 @@ bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI,
}

/// Return true if we can thread a branch across this block.
static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB,
const TargetTransformInfo &TTI) {
// Skip threading if the branch may be divergent.
if (TTI.hasBranchDivergence(BB->getParent()))
return false;

static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
int Size = 0;
EphemeralValueTracker EphTracker;

Expand Down Expand Up @@ -3306,9 +3301,10 @@ static ConstantInt *getKnownValueOnEdge(Value *V, BasicBlock *From,
/// If we have a conditional branch on something for which we know the constant
/// value in predecessors (e.g. a phi node in the current block), thread edges
/// from the predecessor to their ultimate destination.
static std::optional<bool> FoldCondBranchOnValueKnownInPredecessorImpl(
BranchInst *BI, DomTreeUpdater *DTU, const DataLayout &DL,
const TargetTransformInfo &TTI, AssumptionCache *AC) {
static std::optional<bool>
FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
const DataLayout &DL,
AssumptionCache *AC) {
SmallMapVector<ConstantInt *, SmallSetVector<BasicBlock *, 2>, 2> KnownValues;
BasicBlock *BB = BI->getParent();
Value *Cond = BI->getCondition();
Expand Down Expand Up @@ -3336,7 +3332,7 @@ static std::optional<bool> FoldCondBranchOnValueKnownInPredecessorImpl(
// Now we know that this block has multiple preds and two succs.
// Check that the block is small enough and values defined in the block are
// not used outside of it.
if (!BlockIsSimpleEnoughToThreadThrough(BB, TTI))
if (!BlockIsSimpleEnoughToThreadThrough(BB))
return false;

for (const auto &Pair : KnownValues) {
Expand Down Expand Up @@ -3463,14 +3459,15 @@ static std::optional<bool> FoldCondBranchOnValueKnownInPredecessorImpl(
return false;
}

static bool FoldCondBranchOnValueKnownInPredecessor(
BranchInst *BI, DomTreeUpdater *DTU, const DataLayout &DL,
const TargetTransformInfo &TTI, AssumptionCache *AC) {
static bool FoldCondBranchOnValueKnownInPredecessor(BranchInst *BI,
DomTreeUpdater *DTU,
const DataLayout &DL,
AssumptionCache *AC) {
std::optional<bool> Result;
bool EverChanged = false;
do {
// Note that None means "we changed things, but recurse further."
Result = FoldCondBranchOnValueKnownInPredecessorImpl(BI, DTU, DL, TTI, AC);
Result = FoldCondBranchOnValueKnownInPredecessorImpl(BI, DTU, DL, AC);
EverChanged |= Result == std::nullopt || *Result;
} while (Result == std::nullopt);
return EverChanged;
Expand Down Expand Up @@ -7546,7 +7543,7 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
// If this is a branch on something for which we know the constant value in
// predecessors (e.g. a phi node in the current block), thread control
// through this block.
if (FoldCondBranchOnValueKnownInPredecessor(BI, DTU, DL, TTI, Options.AC))
if (FoldCondBranchOnValueKnownInPredecessor(BI, DTU, DL, Options.AC))
return requestResimplify();

// Scan predecessor blocks for conditional branches.
Expand Down
44 changes: 0 additions & 44 deletions llvm/test/Transforms/SimplifyCFG/AMDGPU/skip-threading.ll

This file was deleted.

39 changes: 0 additions & 39 deletions llvm/test/Transforms/SimplifyCFG/convergent.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
; RUN: opt -S -passes='simplifycfg<hoist-common-insts;sink-common-insts>' < %s | FileCheck -check-prefixes=CHECK,SINK %s

declare void @foo() convergent
declare void @bar1()
declare void @bar2()
declare void @bar3()
declare i32 @tid()
declare i32 @mbcnt(i32 %a, i32 %b) convergent
declare i32 @bpermute(i32 %a, i32 %b) convergent
Expand Down Expand Up @@ -48,42 +45,6 @@ exit:
ret i32 %a
}

define i32 @test_01a(i32 %a) {
; CHECK-LABEL: @test_01a(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[A:%.*]], 0
; CHECK-NEXT: br i1 [[COND]], label [[EXIT_CRITEDGE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.false:
; CHECK-NEXT: call void @bar1()
; CHECK-NEXT: call void @bar2()
; CHECK-NEXT: call void @bar3()
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: exit.critedge:
; CHECK-NEXT: call void @bar2()
; CHECK-NEXT: br label [[EXIT]]
; CHECK: exit:
; CHECK-NEXT: ret i32 [[A]]
;
entry:
%cond = icmp eq i32 %a, 0
br i1 %cond, label %merge, label %if.false

if.false:
call void @bar1()
br label %merge

merge:
call void @bar2()
br i1 %cond, label %exit, label %if.false.2

if.false.2:
call void @bar3()
br label %exit

exit:
ret i32 %a
}

define void @test_02(ptr %y.coerce) convergent {
; NOSINK-LABEL: @test_02(
; NOSINK-NEXT: entry:
Expand Down
Loading