2020-09-30 17:10:44 +02:00
|
|
|
//===---- DivergenceAnalysis.cpp --- Divergence Analysis Implementation ----==//
|
2018-10-18 11:38:44 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-10-18 11:38:44 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a general divergence analysis for loop vectorization
|
|
|
|
// and GPU programs. It determines which branches and values in a loop or GPU
|
|
|
|
// program are divergent. It can help branch optimizations such as jump
|
|
|
|
// threading and loop unswitching to make better decisions.
|
|
|
|
//
|
|
|
|
// GPU programs typically use the SIMD execution model, where multiple threads
|
|
|
|
// in the same execution group have to execute in lock-step. Therefore, if the
|
|
|
|
// code contains divergent branches (i.e., threads in a group do not agree on
|
|
|
|
// which path of the branch to take), the group of threads has to execute all
|
|
|
|
// the paths from that branch with different subsets of threads enabled until
|
|
|
|
// they re-converge.
|
|
|
|
//
|
|
|
|
// Due to this execution model, some optimizations such as jump
|
|
|
|
// threading and loop unswitching can interfere with thread re-convergence.
|
|
|
|
// Therefore, an analysis that computes which branches in a GPU program are
|
|
|
|
// divergent can help the compiler to selectively run these optimizations.
|
|
|
|
//
|
|
|
|
// This implementation is derived from the Vectorization Analysis of the
|
|
|
|
// Region Vectorizer (RV). That implementation in turn is based on the approach
|
|
|
|
// described in
|
|
|
|
//
|
|
|
|
// Improving Performance of OpenCL on CPUs
|
|
|
|
// Ralf Karrenberg and Sebastian Hack
|
|
|
|
// CC '12
|
|
|
|
//
|
2021-02-16 05:56:45 +01:00
|
|
|
// This implementation is generic in the sense that it does
|
2018-10-18 11:38:44 +02:00
|
|
|
// not itself identify original sources of divergence.
|
|
|
|
// Instead specialized adapter classes, (LoopDivergenceAnalysis) for loops and
|
2021-02-16 05:56:45 +01:00
|
|
|
// (DivergenceAnalysis) for functions, identify the sources of divergence
|
2018-10-18 11:38:44 +02:00
|
|
|
// (e.g., special variables that hold the thread ID or the iteration variable).
|
|
|
|
//
|
|
|
|
// The generic implementation propagates divergence to variables that are data
|
|
|
|
// or sync dependent on a source of divergence.
|
|
|
|
//
|
|
|
|
// While data dependency is a well-known concept, the notion of sync dependency
|
|
|
|
// is worth more explanation. Sync dependence characterizes the control flow
|
|
|
|
// aspect of the propagation of branch divergence. For example,
|
|
|
|
//
|
|
|
|
// %cond = icmp slt i32 %tid, 10
|
|
|
|
// br i1 %cond, label %then, label %else
|
|
|
|
// then:
|
|
|
|
// br label %merge
|
|
|
|
// else:
|
|
|
|
// br label %merge
|
|
|
|
// merge:
|
|
|
|
// %a = phi i32 [ 0, %then ], [ 1, %else ]
|
|
|
|
//
|
|
|
|
// Suppose %tid holds the thread ID. Although %a is not data dependent on %tid
|
|
|
|
// because %tid is not on its use-def chains, %a is sync dependent on %tid
|
|
|
|
// because the branch "br i1 %cond" depends on %tid and affects which value %a
|
|
|
|
// is assigned to.
|
|
|
|
//
|
|
|
|
// The sync dependence detection (which branch induces divergence in which join
|
|
|
|
// points) is implemented in the SyncDependenceAnalysis.
|
|
|
|
//
|
2021-02-16 05:56:45 +01:00
|
|
|
// The current implementation has the following limitations:
|
2018-10-18 11:38:44 +02:00
|
|
|
// 1. intra-procedural. It conservatively considers the arguments of a
|
|
|
|
// non-kernel-entry function and the return value of a function call as
|
|
|
|
// divergent.
|
|
|
|
// 2. memory as black box. It conservatively considers values loaded from
|
|
|
|
// generic or local address as divergent. This can be improved by leveraging
|
|
|
|
// pointer analysis and/or by modelling non-escaping memory objects in SSA
|
|
|
|
// as done in RV.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/DivergenceAnalysis.h"
|
2021-02-16 05:56:45 +01:00
|
|
|
#include "llvm/Analysis/CFG.h"
|
2018-10-18 11:38:44 +02:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/InstIterator.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
#define DEBUG_TYPE "divergence"
|
2018-10-18 11:38:44 +02:00
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
DivergenceAnalysisImpl::DivergenceAnalysisImpl(
|
2018-10-18 11:38:44 +02:00
|
|
|
const Function &F, const Loop *RegionLoop, const DominatorTree &DT,
|
|
|
|
const LoopInfo &LI, SyncDependenceAnalysis &SDA, bool IsLCSSAForm)
|
|
|
|
: F(F), RegionLoop(RegionLoop), DT(DT), LI(LI), SDA(SDA),
|
|
|
|
IsLCSSAForm(IsLCSSAForm) {}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
bool DivergenceAnalysisImpl::markDivergent(const Value &DivVal) {
|
2020-09-30 17:10:44 +02:00
|
|
|
if (isAlwaysUniform(DivVal))
|
|
|
|
return false;
|
2018-10-18 11:38:44 +02:00
|
|
|
assert(isa<Instruction>(DivVal) || isa<Argument>(DivVal));
|
|
|
|
assert(!isAlwaysUniform(DivVal) && "cannot be a divergent");
|
2020-09-30 17:10:44 +02:00
|
|
|
return DivergentValues.insert(&DivVal).second;
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::addUniformOverride(const Value &UniVal) {
|
2018-10-18 11:38:44 +02:00
|
|
|
UniformOverrides.insert(&UniVal);
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
bool DivergenceAnalysisImpl::isTemporalDivergent(
|
|
|
|
const BasicBlock &ObservingBlock, const Value &Val) const {
|
2018-10-18 11:38:44 +02:00
|
|
|
const auto *Inst = dyn_cast<const Instruction>(&Val);
|
|
|
|
if (!Inst)
|
|
|
|
return false;
|
|
|
|
// check whether any divergent loop carrying Val terminates before control
|
|
|
|
// proceeds to ObservingBlock
|
|
|
|
for (const auto *Loop = LI.getLoopFor(Inst->getParent());
|
|
|
|
Loop != RegionLoop && !Loop->contains(&ObservingBlock);
|
|
|
|
Loop = Loop->getParentLoop()) {
|
2020-12-19 04:08:17 +01:00
|
|
|
if (DivergentLoops.contains(Loop))
|
2018-10-18 11:38:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
bool DivergenceAnalysisImpl::inRegion(const Instruction &I) const {
|
2018-10-18 11:38:44 +02:00
|
|
|
return I.getParent() && inRegion(*I.getParent());
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
bool DivergenceAnalysisImpl::inRegion(const BasicBlock &BB) const {
|
2018-10-18 11:38:44 +02:00
|
|
|
return (!RegionLoop && BB.getParent() == &F) || RegionLoop->contains(&BB);
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::pushUsers(const Value &V) {
|
2020-09-30 17:10:44 +02:00
|
|
|
const auto *I = dyn_cast<const Instruction>(&V);
|
|
|
|
|
|
|
|
if (I && I->isTerminator()) {
|
|
|
|
analyzeControlDivergence(*I);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto *User : V.users()) {
|
|
|
|
const auto *UserInst = dyn_cast<const Instruction>(User);
|
|
|
|
if (!UserInst)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// only compute divergent inside loop
|
|
|
|
if (!inRegion(*UserInst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// All users of divergent values are immediate divergent
|
|
|
|
if (markDivergent(*UserInst))
|
|
|
|
Worklist.push_back(UserInst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Instruction *getIfCarriedInstruction(const Use &U,
|
|
|
|
const Loop &DivLoop) {
|
|
|
|
const auto *I = dyn_cast<const Instruction>(&U);
|
|
|
|
if (!I)
|
|
|
|
return nullptr;
|
|
|
|
if (!DivLoop.contains(I))
|
|
|
|
return nullptr;
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::analyzeTemporalDivergence(
|
|
|
|
const Instruction &I, const Loop &OuterDivLoop) {
|
2020-09-30 17:10:44 +02:00
|
|
|
if (isAlwaysUniform(I))
|
|
|
|
return;
|
|
|
|
if (isDivergent(I))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Analyze temporal divergence: " << I.getName() << "\n");
|
|
|
|
assert((isa<PHINode>(I) || !IsLCSSAForm) &&
|
|
|
|
"In LCSSA form all users of loop-exiting defs are Phi nodes.");
|
|
|
|
for (const Use &Op : I.operands()) {
|
|
|
|
const auto *OpInst = getIfCarriedInstruction(Op, OuterDivLoop);
|
2020-06-17 04:44:50 +02:00
|
|
|
if (!OpInst)
|
|
|
|
continue;
|
2020-09-30 17:10:44 +02:00
|
|
|
if (markDivergent(I))
|
|
|
|
pushUsers(I);
|
|
|
|
return;
|
2020-06-17 04:44:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 11:38:44 +02:00
|
|
|
// marks all users of loop-carried values of the loop headed by LoopHeader as
|
|
|
|
// divergent
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::analyzeLoopExitDivergence(
|
|
|
|
const BasicBlock &DivExit, const Loop &OuterDivLoop) {
|
2020-09-30 17:10:44 +02:00
|
|
|
// All users are in immediate exit blocks
|
|
|
|
if (IsLCSSAForm) {
|
|
|
|
for (const auto &Phi : DivExit.phis()) {
|
|
|
|
analyzeTemporalDivergence(Phi, OuterDivLoop);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-10-18 11:38:44 +02:00
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
// For non-LCSSA we have to follow all live out edges wherever they may lead.
|
|
|
|
const BasicBlock &LoopHeader = *OuterDivLoop.getHeader();
|
|
|
|
SmallVector<const BasicBlock *, 8> TaintStack;
|
|
|
|
TaintStack.push_back(&DivExit);
|
2018-10-18 11:38:44 +02:00
|
|
|
|
|
|
|
// Otherwise potential users of loop-carried values could be anywhere in the
|
|
|
|
// dominance region of DivLoop (including its fringes for phi nodes)
|
|
|
|
DenseSet<const BasicBlock *> Visited;
|
2020-09-30 17:10:44 +02:00
|
|
|
Visited.insert(&DivExit);
|
2018-10-18 11:38:44 +02:00
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
do {
|
2021-01-24 21:18:57 +01:00
|
|
|
auto *UserBlock = TaintStack.pop_back_val();
|
2018-10-18 11:38:44 +02:00
|
|
|
|
|
|
|
// don't spread divergence beyond the region
|
|
|
|
if (!inRegion(*UserBlock))
|
|
|
|
continue;
|
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
assert(!OuterDivLoop.contains(UserBlock) &&
|
2018-10-18 11:38:44 +02:00
|
|
|
"irreducible control flow detected");
|
|
|
|
|
|
|
|
// phi nodes at the fringes of the dominance region
|
|
|
|
if (!DT.dominates(&LoopHeader, UserBlock)) {
|
|
|
|
// all PHI nodes of UserBlock become divergent
|
|
|
|
for (auto &Phi : UserBlock->phis()) {
|
2020-09-30 17:10:44 +02:00
|
|
|
analyzeTemporalDivergence(Phi, OuterDivLoop);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
// Taint outside users of values carried by OuterDivLoop.
|
2018-10-18 11:38:44 +02:00
|
|
|
for (auto &I : *UserBlock) {
|
2020-09-30 17:10:44 +02:00
|
|
|
analyzeTemporalDivergence(I, OuterDivLoop);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// visit all blocks in the dominance region
|
|
|
|
for (auto *SuccBlock : successors(UserBlock)) {
|
|
|
|
if (!Visited.insert(SuccBlock).second) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TaintStack.push_back(SuccBlock);
|
|
|
|
}
|
2020-09-30 17:10:44 +02:00
|
|
|
} while (!TaintStack.empty());
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::propagateLoopExitDivergence(
|
|
|
|
const BasicBlock &DivExit, const Loop &InnerDivLoop) {
|
2020-09-30 17:10:44 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "\tpropLoopExitDiv " << DivExit.getName() << "\n");
|
|
|
|
|
|
|
|
// Find outer-most loop that does not contain \p DivExit
|
|
|
|
const Loop *DivLoop = &InnerDivLoop;
|
|
|
|
const Loop *OuterDivLoop = DivLoop;
|
|
|
|
const Loop *ExitLevelLoop = LI.getLoopFor(&DivExit);
|
|
|
|
const unsigned LoopExitDepth =
|
|
|
|
ExitLevelLoop ? ExitLevelLoop->getLoopDepth() : 0;
|
|
|
|
while (DivLoop && DivLoop->getLoopDepth() > LoopExitDepth) {
|
|
|
|
DivergentLoops.insert(DivLoop); // all crossed loops are divergent
|
|
|
|
OuterDivLoop = DivLoop;
|
|
|
|
DivLoop = DivLoop->getParentLoop();
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
2020-09-30 17:10:44 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "\tOuter-most left loop: " << OuterDivLoop->getName()
|
|
|
|
<< "\n");
|
2018-10-18 11:38:44 +02:00
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
analyzeLoopExitDivergence(DivExit, *OuterDivLoop);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
// this is a divergent join point - mark all phi nodes as divergent and push
|
|
|
|
// them onto the stack.
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::taintAndPushPhiNodes(const BasicBlock &JoinBlock) {
|
2020-09-30 17:10:44 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "taintAndPushPhiNodes in " << JoinBlock.getName()
|
|
|
|
<< "\n");
|
2018-10-18 11:38:44 +02:00
|
|
|
|
|
|
|
// ignore divergence outside the region
|
|
|
|
if (!inRegion(JoinBlock)) {
|
2020-09-30 17:10:44 +02:00
|
|
|
return;
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// push non-divergent phi nodes in JoinBlock to the worklist
|
2020-09-30 17:10:44 +02:00
|
|
|
for (const auto &Phi : JoinBlock.phis()) {
|
|
|
|
if (isDivergent(Phi))
|
|
|
|
continue;
|
|
|
|
// FIXME Theoretically ,the 'undef' value could be replaced by any other
|
|
|
|
// value causing spurious divergence.
|
|
|
|
if (Phi.hasConstantOrUndefValue())
|
|
|
|
continue;
|
|
|
|
if (markDivergent(Phi))
|
|
|
|
Worklist.push_back(&Phi);
|
|
|
|
}
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::analyzeControlDivergence(const Instruction &Term) {
|
2020-09-30 17:10:44 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "analyzeControlDiv " << Term.getParent()->getName()
|
|
|
|
<< "\n");
|
2018-10-18 11:38:44 +02:00
|
|
|
|
2020-01-24 04:49:20 +01:00
|
|
|
// Don't propagate divergence from unreachable blocks.
|
|
|
|
if (!DT.isReachableFromEntry(Term.getParent()))
|
|
|
|
return;
|
|
|
|
|
2018-10-18 11:38:44 +02:00
|
|
|
const auto *BranchLoop = LI.getLoopFor(Term.getParent());
|
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
const auto &DivDesc = SDA.getJoinBlocks(Term);
|
2018-10-18 11:38:44 +02:00
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
// Iterate over all blocks now reachable by a disjoint path join
|
|
|
|
for (const auto *JoinBlock : DivDesc.JoinDivBlocks) {
|
|
|
|
taintAndPushPhiNodes(*JoinBlock);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
assert(DivDesc.LoopDivBlocks.empty() || BranchLoop);
|
|
|
|
for (const auto *DivExitBlock : DivDesc.LoopDivBlocks) {
|
|
|
|
propagateLoopExitDivergence(*DivExitBlock, *BranchLoop);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
void DivergenceAnalysisImpl::compute() {
|
2020-09-30 17:10:44 +02:00
|
|
|
// Initialize worklist.
|
|
|
|
auto DivValuesCopy = DivergentValues;
|
|
|
|
for (const auto *DivVal : DivValuesCopy) {
|
|
|
|
assert(isDivergent(*DivVal) && "Worklist invariant violated!");
|
2018-10-18 11:38:44 +02:00
|
|
|
pushUsers(*DivVal);
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:10:44 +02:00
|
|
|
// All values on the Worklist are divergent.
|
|
|
|
// Their users may not have been updated yed.
|
2018-10-18 11:38:44 +02:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
const Instruction &I = *Worklist.back();
|
|
|
|
Worklist.pop_back();
|
|
|
|
|
|
|
|
// propagate value divergence to users
|
2020-09-30 17:10:44 +02:00
|
|
|
assert(isDivergent(I) && "Worklist invariant violated!");
|
|
|
|
pushUsers(I);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
bool DivergenceAnalysisImpl::isAlwaysUniform(const Value &V) const {
|
2020-12-19 04:08:17 +01:00
|
|
|
return UniformOverrides.contains(&V);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
bool DivergenceAnalysisImpl::isDivergent(const Value &V) const {
|
2020-12-19 04:08:17 +01:00
|
|
|
return DivergentValues.contains(&V);
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
bool DivergenceAnalysisImpl::isDivergentUse(const Use &U) const {
|
[DivergenceAnalysis] Add methods for querying divergence at use
Summary:
The existing isDivergent(Value) methods query whether a value is
divergent at its definition. However even if a value is uniform at its
definition, a use of it in another basic block can be divergent because
of divergent control flow between the def and the use.
This patch adds new isDivergent(Use) methods to DivergenceAnalysis,
LegacyDivergenceAnalysis and GPUDivergenceAnalysis.
This might allow D63953 or other similar workarounds to be removed.
Reviewers: alex-t, nhaehnle, arsenm, rtaylor, rampitec, simoll, jingyue
Reviewed By: nhaehnle
Subscribers: jfb, jvesely, wdng, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65141
llvm-svn: 367218
2019-07-29 12:22:09 +02:00
|
|
|
Value &V = *U.get();
|
|
|
|
Instruction &I = *cast<Instruction>(U.getUser());
|
|
|
|
return isDivergent(V) || isTemporalDivergent(*I.getParent(), V);
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
DivergenceInfo::DivergenceInfo(Function &F, const DominatorTree &DT,
|
|
|
|
const PostDominatorTree &PDT, const LoopInfo &LI,
|
|
|
|
const TargetTransformInfo &TTI,
|
|
|
|
bool KnownReducible)
|
|
|
|
: F(F), ContainsIrreducible(false) {
|
|
|
|
if (!KnownReducible) {
|
|
|
|
using RPOTraversal = ReversePostOrderTraversal<const Function *>;
|
|
|
|
RPOTraversal FuncRPOT(&F);
|
|
|
|
if (containsIrreducibleCFG<const BasicBlock *, const RPOTraversal,
|
|
|
|
const LoopInfo>(FuncRPOT, LI)) {
|
|
|
|
ContainsIrreducible = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-10-18 11:38:44 +02:00
|
|
|
}
|
2021-02-16 05:56:45 +01:00
|
|
|
SDA = std::make_unique<SyncDependenceAnalysis>(DT, PDT, LI);
|
|
|
|
DA = std::make_unique<DivergenceAnalysisImpl>(F, nullptr, DT, LI, *SDA,
|
|
|
|
/* LCSSA */ false);
|
2018-11-30 23:55:20 +01:00
|
|
|
for (auto &I : instructions(F)) {
|
|
|
|
if (TTI.isSourceOfDivergence(&I)) {
|
2021-02-16 05:56:45 +01:00
|
|
|
DA->markDivergent(I);
|
2018-11-30 23:55:20 +01:00
|
|
|
} else if (TTI.isAlwaysUniform(&I)) {
|
2021-02-16 05:56:45 +01:00
|
|
|
DA->addUniformOverride(I);
|
2018-11-30 23:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &Arg : F.args()) {
|
|
|
|
if (TTI.isSourceOfDivergence(&Arg)) {
|
2021-02-16 05:56:45 +01:00
|
|
|
DA->markDivergent(Arg);
|
2018-11-30 23:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
DA->compute();
|
2018-11-30 23:55:20 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
AnalysisKey DivergenceAnalysis::Key;
|
2018-11-30 23:55:20 +01:00
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
DivergenceAnalysis::Result
|
|
|
|
DivergenceAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
|
|
|
|
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
|
|
|
auto &PDT = AM.getResult<PostDominatorTreeAnalysis>(F);
|
|
|
|
auto &LI = AM.getResult<LoopAnalysis>(F);
|
|
|
|
auto &TTI = AM.getResult<TargetIRAnalysis>(F);
|
|
|
|
|
|
|
|
return DivergenceInfo(F, DT, PDT, LI, TTI, /* KnownReducible = */ false);
|
[DivergenceAnalysis] Add methods for querying divergence at use
Summary:
The existing isDivergent(Value) methods query whether a value is
divergent at its definition. However even if a value is uniform at its
definition, a use of it in another basic block can be divergent because
of divergent control flow between the def and the use.
This patch adds new isDivergent(Use) methods to DivergenceAnalysis,
LegacyDivergenceAnalysis and GPUDivergenceAnalysis.
This might allow D63953 or other similar workarounds to be removed.
Reviewers: alex-t, nhaehnle, arsenm, rtaylor, rampitec, simoll, jingyue
Reviewed By: nhaehnle
Subscribers: jfb, jvesely, wdng, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65141
llvm-svn: 367218
2019-07-29 12:22:09 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 05:56:45 +01:00
|
|
|
PreservedAnalyses
|
|
|
|
DivergenceAnalysisPrinterPass::run(Function &F, FunctionAnalysisManager &FAM) {
|
|
|
|
auto &DI = FAM.getResult<DivergenceAnalysis>(F);
|
|
|
|
OS << "'Divergence Analysis' for function '" << F.getName() << "':\n";
|
|
|
|
if (DI.hasDivergence()) {
|
|
|
|
for (auto &Arg : F.args()) {
|
|
|
|
OS << (DI.isDivergent(Arg) ? "DIVERGENT: " : " ");
|
|
|
|
OS << Arg << "\n";
|
|
|
|
}
|
2021-02-23 05:17:18 +01:00
|
|
|
for (const BasicBlock &BB : F) {
|
2021-02-16 05:56:45 +01:00
|
|
|
OS << "\n " << BB.getName() << ":\n";
|
|
|
|
for (auto &I : BB.instructionsWithoutDebug()) {
|
|
|
|
OS << (DI.isDivergent(I) ? "DIVERGENT: " : " ");
|
|
|
|
OS << I << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PreservedAnalyses::all();
|
2018-11-30 23:55:20 +01:00
|
|
|
}
|