Skip to content

Commit

Permalink
Introduce fix-irreducible pass
Browse files Browse the repository at this point in the history
An irreducible SCC is one which has multiple "header" blocks, i.e., blocks
with control-flow edges incident from outside the SCC. This pass converts an
irreducible SCC into a natural loop by introducing a single new header
block and redirecting all the edges on the original headers to this
new block.

This is a useful workaround for a limitation in the structurizer
which, which produces incorrect control flow in the presence of
irreducible regions. The AMDGPU backend provides an option to
enable this pass before the structurizer, which may eventually be
enabled by default.

Reviewed By: nhaehnle

Differential Revision: https://reviews.llvm.org/D77198
  • Loading branch information
ssahasra committed Apr 15, 2020
1 parent d83541d commit 2ada8e2
Show file tree
Hide file tree
Showing 14 changed files with 1,403 additions and 38 deletions.
6 changes: 6 additions & 0 deletions llvm/include/llvm/Analysis/LoopInfo.h
Expand Up @@ -962,6 +962,12 @@ template <class BlockT, class LoopT> class LoopInfoBase {
return L && L->getHeader() == BB;
}

/// Return the top-level loops.
const std::vector<LoopT *> &getTopLevelLoops() const { return TopLevelLoops; }

/// Return the top-level loops.
std::vector<LoopT *> &getTopLevelLoopsVector() { return TopLevelLoops; }

/// This removes the specified top-level loop from this loop info object.
/// The loop is not deleted, as it will presumably be inserted into
/// another loop.
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/InitializePasses.h
Expand Up @@ -152,6 +152,7 @@ void initializeExternalAAWrapperPassPass(PassRegistry&);
void initializeFEntryInserterPass(PassRegistry&);
void initializeFinalizeISelPass(PassRegistry&);
void initializeFinalizeMachineBundlesPass(PassRegistry&);
void initializeFixIrreduciblePass(PassRegistry &);
void initializeFixupStatepointCallerSavedPass(PassRegistry&);
void initializeFlattenCFGPassPass(PassRegistry&);
void initializeFloat2IntLegacyPassPass(PassRegistry&);
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/LinkAllPasses.h
Expand Up @@ -231,6 +231,7 @@ namespace {
(void) llvm::createHardwareLoopsPass();
(void) llvm::createInjectTLIMappingsLegacyPass();
(void) llvm::createUnifyLoopExitsPass();
(void) llvm::createFixIrreduciblePass();

(void)new llvm::IntervalPartition();
(void)new llvm::ScalarEvolutionWrapperPass();
Expand Down
7 changes: 7 additions & 0 deletions llvm/include/llvm/Transforms/Utils.h
Expand Up @@ -134,6 +134,13 @@ FunctionPass *createInjectTLIMappingsLegacyPass();
// exit blocks.
//
FunctionPass *createUnifyLoopExitsPass();

//===----------------------------------------------------------------------===//
//
// FixIrreducible - Convert each SCC with irreducible control-flow
// into a natural loop.
//
FunctionPass *createFixIrreduciblePass();
}

#endif
1 change: 1 addition & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Expand Up @@ -864,6 +864,7 @@ bool GCNPassConfig::addPreISel() {
addPass(&AMDGPUUnifyDivergentExitNodesID);
if (!LateCFGStructurize) {
if (EnableStructurizerWorkarounds) {
addPass(createFixIrreduciblePass());
addPass(createUnifyLoopExitsPass());
}
addPass(createStructurizeCFGPass(false)); // true -> SkipUniformRegions
Expand Down
100 changes: 62 additions & 38 deletions llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
Expand Up @@ -1144,18 +1144,62 @@ static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
using BBPredicates = DenseMap<BasicBlock *, PHINode *>;
using BBSetVector = SetVector<BasicBlock *>;

// Collect predicates for each outgoing block. If control reaches the
// Hub from an incoming block InBB, then the predicate for each
// outgoing block OutBB decides whether control is forwarded to OutBB.
// Redirects the terminator of the incoming block to the first guard
// block in the hub. The condition of the original terminator (if it
// was conditional) and its original successors are returned as a
// tuple <condition, succ0, succ1>. The function additionally filters
// out successors that are not in the set of outgoing blocks.
//
// These predicates are not orthogonal. The Hub evaluates them in the
// same order as the Outgoing set-vector, and control branches to the
// first outgoing block whose predicate evaluates to true.
static void createGuardPredicates(BasicBlock *FirstGuardBlock,
BBPredicates &GuardPredicates,
SmallVectorImpl<WeakVH> &DeletionCandidates,
const BBSetVector &Incoming,
const BBSetVector &Outgoing) {
// - condition is non-null iff the branch is conditional.
// - Succ1 is non-null iff the sole/taken target is an outgoing block.
// - Succ2 is non-null iff condition is non-null and the fallthrough
// target is an outgoing block.
static std::tuple<Value *, BasicBlock *, BasicBlock *>
redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock,
const BBSetVector &Outgoing) {
auto Branch = cast<BranchInst>(BB->getTerminator());
auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr;

BasicBlock *Succ0 = Branch->getSuccessor(0);
BasicBlock *Succ1 = nullptr;
Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;

if (Branch->isUnconditional()) {
Branch->setSuccessor(0, FirstGuardBlock);
assert(Succ0);
} else {
Succ1 = Branch->getSuccessor(1);
Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;
assert(Succ0 || Succ1);
if (Succ0 && !Succ1) {
Branch->setSuccessor(0, FirstGuardBlock);
} else if (Succ1 && !Succ0) {
Branch->setSuccessor(1, FirstGuardBlock);
} else {
Branch->eraseFromParent();
BranchInst::Create(FirstGuardBlock, BB);
}
}

assert(Succ0 || Succ1);
return std::make_tuple(Condition, Succ0, Succ1);
}

// Capture the existing control flow as guard predicates, and redirect
// control flow from every incoming block to the first guard block in
// the hub.
//
// There is one guard predicate for each outgoing block OutBB. The
// predicate is a PHINode with one input for each InBB which
// represents whether the hub should transfer control flow to OutBB if
// it arrived from InBB. These predicates are NOT ORTHOGONAL. The Hub
// evaluates them in the same order as the Outgoing set-vector, and
// control branches to the first outgoing block whose predicate
// evaluates to true.
static void convertToGuardPredicates(
BasicBlock *FirstGuardBlock, BBPredicates &GuardPredicates,
SmallVectorImpl<WeakVH> &DeletionCandidates, const BBSetVector &Incoming,
const BBSetVector &Outgoing) {
auto &Context = Incoming.front()->getContext();
auto BoolTrue = ConstantInt::getTrue(Context);
auto BoolFalse = ConstantInt::getFalse(Context);
Expand All @@ -1172,30 +1216,11 @@ static void createGuardPredicates(BasicBlock *FirstGuardBlock,
}

for (auto In : Incoming) {
auto Branch = cast<BranchInst>(In->getTerminator());
BasicBlock *Succ0 = Branch->getSuccessor(0);
BasicBlock *Succ1 = nullptr;

Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;

if (Branch->isUnconditional()) {
Branch->setSuccessor(0, FirstGuardBlock);
assert(Succ0);
} else {
Succ1 = Branch->getSuccessor(1);
Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;
assert(Succ0 || Succ1);
if (Succ0 && !Succ1) {
Branch->setSuccessor(0, FirstGuardBlock);
} else if (Succ1 && !Succ0) {
Branch->setSuccessor(1, FirstGuardBlock);
} else {
Branch->eraseFromParent();
BranchInst::Create(FirstGuardBlock, In);
}
}

assert(Succ0 || Succ1);
Value *Condition;
BasicBlock *Succ0;
BasicBlock *Succ1;
std::tie(Condition, Succ0, Succ1) =
redirectToHub(In, FirstGuardBlock, Outgoing);

// Optimization: Consider an incoming block A with both successors
// Succ0 and Succ1 in the set of outgoing blocks. The predicates
Expand All @@ -1205,7 +1230,6 @@ static void createGuardPredicates(BasicBlock *FirstGuardBlock,
// control must reach Succ1, which means that the predicate for
// Succ1 is always true.
bool OneSuccessorDone = false;
auto Condition = Branch->getCondition();
for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
auto Out = Outgoing[i];
auto Phi = GuardPredicates[Out];
Expand Down Expand Up @@ -1287,8 +1311,8 @@ BasicBlock *llvm::CreateControlFlowHub(

BBPredicates GuardPredicates;
SmallVector<WeakVH, 8> DeletionCandidates;
createGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates,
Incoming, Outgoing);
convertToGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates,
Incoming, Outgoing);

GuardBlocks.push_back(FirstGuardBlock);
createGuardBlocks(GuardBlocks, F, Outgoing, GuardPredicates, Prefix);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Utils/CMakeLists.txt
Expand Up @@ -20,6 +20,7 @@ add_llvm_component_library(LLVMTransformUtils
EntryExitInstrumenter.cpp
EscapeEnumerator.cpp
Evaluator.cpp
FixIrreducible.cpp
FlattenCFG.cpp
FunctionComparator.cpp
FunctionImportUtils.cpp
Expand Down

0 comments on commit 2ada8e2

Please sign in to comment.