2018-12-01 06:55:20 +08:00
|
|
|
//===- LegacyDivergenceAnalysis.cpp --------- Legacy Divergence Analysis
|
|
|
|
//Implementation -==//
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-09-22 01:58:14 +08:00
|
|
|
// This file implements divergence analysis which determines whether a branch
|
|
|
|
// in a GPU program is divergent.It can help branch optimizations such as jump
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
// 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 converge at the immediately post-dominating BB of the paths.
|
|
|
|
//
|
|
|
|
// Due to this execution model, some optimizations such as jump
|
|
|
|
// threading and loop unswitching can be unfortunately harmful when performed on
|
|
|
|
// divergent branches. Therefore, an analysis that computes which branches in a
|
|
|
|
// GPU program are divergent can help the compiler to selectively run these
|
|
|
|
// optimizations.
|
|
|
|
//
|
|
|
|
// This file defines divergence analysis which computes a conservative but
|
|
|
|
// non-trivial approximation of all divergent branches in a GPU program. It
|
|
|
|
// partially implements the approach described in
|
|
|
|
//
|
|
|
|
// Divergence Analysis
|
|
|
|
// Sampaio, Souza, Collange, Pereira
|
|
|
|
// TOPLAS '13
|
|
|
|
//
|
|
|
|
// The divergence analysis identifies the sources of divergence (e.g., special
|
|
|
|
// variables that hold the thread ID), and recursively marks variables that are
|
|
|
|
// data or sync dependent on a source of divergence as divergent.
|
|
|
|
//
|
|
|
|
// 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 current implementation has the following limitations:
|
|
|
|
// 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.
|
2015-09-22 01:58:14 +08:00
|
|
|
//
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/Analysis/LegacyDivergenceAnalysis.h"
|
2018-12-01 06:55:20 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/Analysis/CFG.h"
|
|
|
|
#include "llvm/Analysis/DivergenceAnalysis.h"
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2015-09-22 01:58:14 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2019-11-15 07:15:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-09-22 01:58:14 +08:00
|
|
|
#include <vector>
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2018-07-13 21:13:30 +08:00
|
|
|
#define DEBUG_TYPE "divergence"
|
|
|
|
|
2018-12-01 06:55:20 +08:00
|
|
|
// transparently use the GPUDivergenceAnalysis
|
|
|
|
static cl::opt<bool> UseGPUDA("use-gpu-divergence-analysis", cl::init(false),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::desc("turn the LegacyDivergenceAnalysis into "
|
|
|
|
"a wrapper for GPUDivergenceAnalysis"));
|
|
|
|
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DivergencePropagator {
|
|
|
|
public:
|
2015-09-22 01:58:14 +08:00
|
|
|
DivergencePropagator(Function &F, TargetTransformInfo &TTI, DominatorTree &DT,
|
[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 18:22:09 +08:00
|
|
|
PostDominatorTree &PDT, DenseSet<const Value *> &DV,
|
|
|
|
DenseSet<const Use *> &DU)
|
|
|
|
: F(F), TTI(TTI), DT(DT), PDT(PDT), DV(DV), DU(DU) {}
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
void populateWithSourcesOfDivergence();
|
|
|
|
void propagate();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// A helper function that explores data dependents of V.
|
|
|
|
void exploreDataDependency(Value *V);
|
|
|
|
// A helper function that explores sync dependents of TI.
|
2018-10-18 08:36:15 +08:00
|
|
|
void exploreSyncDependency(Instruction *TI);
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
// Computes the influence region from Start to End. This region includes all
|
2015-12-19 05:44:26 +08:00
|
|
|
// basic blocks on any simple path from Start to End.
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
void computeInfluenceRegion(BasicBlock *Start, BasicBlock *End,
|
|
|
|
DenseSet<BasicBlock *> &InfluenceRegion);
|
|
|
|
// Finds all users of I that are outside the influence region, and add these
|
|
|
|
// users to Worklist.
|
|
|
|
void findUsersOutsideInfluenceRegion(
|
|
|
|
Instruction &I, const DenseSet<BasicBlock *> &InfluenceRegion);
|
|
|
|
|
|
|
|
Function &F;
|
|
|
|
TargetTransformInfo &TTI;
|
|
|
|
DominatorTree &DT;
|
|
|
|
PostDominatorTree &PDT;
|
|
|
|
std::vector<Value *> Worklist; // Stack for DFS.
|
2015-09-22 01:58:14 +08:00
|
|
|
DenseSet<const Value *> &DV; // Stores all divergent values.
|
[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 18:22:09 +08:00
|
|
|
DenseSet<const Use *> &DU; // Stores divergent uses of possibly uniform
|
|
|
|
// values.
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void DivergencePropagator::populateWithSourcesOfDivergence() {
|
|
|
|
Worklist.clear();
|
|
|
|
DV.clear();
|
[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 18:22:09 +08:00
|
|
|
DU.clear();
|
2015-08-07 03:10:45 +08:00
|
|
|
for (auto &I : instructions(F)) {
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
if (TTI.isSourceOfDivergence(&I)) {
|
|
|
|
Worklist.push_back(&I);
|
|
|
|
DV.insert(&I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto &Arg : F.args()) {
|
|
|
|
if (TTI.isSourceOfDivergence(&Arg)) {
|
|
|
|
Worklist.push_back(&Arg);
|
|
|
|
DV.insert(&Arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 08:36:15 +08:00
|
|
|
void DivergencePropagator::exploreSyncDependency(Instruction *TI) {
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
// Propagation rule 1: if branch TI is divergent, all PHINodes in TI's
|
|
|
|
// immediate post dominator are divergent. This rule handles if-then-else
|
|
|
|
// patterns. For example,
|
|
|
|
//
|
|
|
|
// if (tid < 5)
|
|
|
|
// a1 = 1;
|
|
|
|
// else
|
|
|
|
// a2 = 2;
|
|
|
|
// a = phi(a1, a2); // sync dependent on (tid < 5)
|
|
|
|
BasicBlock *ThisBB = TI->getParent();
|
2016-04-29 14:17:47 +08:00
|
|
|
|
|
|
|
// Unreachable blocks may not be in the dominator tree.
|
|
|
|
if (!DT.isReachableFromEntry(ThisBB))
|
|
|
|
return;
|
|
|
|
|
2016-05-10 00:57:08 +08:00
|
|
|
// If the function has no exit blocks or doesn't reach any exit blocks, the
|
|
|
|
// post dominator may be null.
|
|
|
|
DomTreeNode *ThisNode = PDT.getNode(ThisBB);
|
|
|
|
if (!ThisNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BasicBlock *IPostDom = ThisNode->getIDom()->getBlock();
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
if (IPostDom == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto I = IPostDom->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
// A PHINode is uniform if it returns the same value no matter which path is
|
|
|
|
// taken.
|
2016-04-15 01:42:47 +08:00
|
|
|
if (!cast<PHINode>(I)->hasConstantOrUndefValue() && DV.insert(&*I).second)
|
Analysis: Remove implicit ilist iterator conversions
Remove implicit ilist iterator conversions from LLVMAnalysis.
I came across something really scary in `llvm::isKnownNotFullPoison()`
which relied on `Instruction::getNextNode()` being completely broken
(not surprising, but scary nevertheless). This function is documented
(and coded to) return `nullptr` when it gets to the sentinel, but with
an `ilist_half_node` as a sentinel, the sentinel check looks into some
other memory and we don't recognize we've hit the end.
Rooting out these scary cases is the reason I'm removing the implicit
conversions before doing anything else with `ilist`; I'm not at all
surprised that clients rely on badness.
I found another scary case -- this time, not relying on badness, just
bad (but I guess getting lucky so far) -- in
`ObjectSizeOffsetEvaluator::compute_()`. Here, we save out the
insertion point, do some things, and then restore it. Previously, we
let the iterator auto-convert to `Instruction*`, and then set it back
using the `Instruction*` version:
Instruction *PrevInsertPoint = Builder.GetInsertPoint();
/* Logic that may change insert point */
if (PrevInsertPoint)
Builder.SetInsertPoint(PrevInsertPoint);
The check for `PrevInsertPoint` doesn't protect correctly against bad
accesses. If the insertion point has been set to the end of a basic
block (i.e., `SetInsertPoint(SomeBB)`), then `GetInsertPoint()` returns
an iterator pointing at the list sentinel. The version of
`SetInsertPoint()` that's getting called will then call
`PrevInsertPoint->getParent()`, which explodes horribly. The only
reason this hasn't blown up is that it's fairly unlikely the builder is
adding to the end of the block; usually, we're adding instructions
somewhere before the terminator.
llvm-svn: 249925
2015-10-10 08:53:03 +08:00
|
|
|
Worklist.push_back(&*I);
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Propagation rule 2: if a value defined in a loop is used outside, the user
|
|
|
|
// is sync dependent on the condition of the loop exits that dominate the
|
|
|
|
// user. For example,
|
|
|
|
//
|
|
|
|
// int i = 0;
|
|
|
|
// do {
|
|
|
|
// i++;
|
|
|
|
// if (foo(i)) ... // uniform
|
|
|
|
// } while (i < tid);
|
|
|
|
// if (bar(i)) ... // divergent
|
|
|
|
//
|
|
|
|
// A program may contain unstructured loops. Therefore, we cannot leverage
|
|
|
|
// LoopInfo, which only recognizes natural loops.
|
|
|
|
//
|
|
|
|
// The algorithm used here handles both natural and unstructured loops. Given
|
|
|
|
// a branch TI, we first compute its influence region, the union of all simple
|
|
|
|
// paths from TI to its immediate post dominator (IPostDom). Then, we search
|
|
|
|
// for all the values defined in the influence region but used outside. All
|
|
|
|
// these users are sync dependent on TI.
|
|
|
|
DenseSet<BasicBlock *> InfluenceRegion;
|
|
|
|
computeInfluenceRegion(ThisBB, IPostDom, InfluenceRegion);
|
|
|
|
// An insight that can speed up the search process is that all the in-region
|
|
|
|
// values that are used outside must dominate TI. Therefore, instead of
|
|
|
|
// searching every basic blocks in the influence region, we search all the
|
|
|
|
// dominators of TI until it is outside the influence region.
|
|
|
|
BasicBlock *InfluencedBB = ThisBB;
|
|
|
|
while (InfluenceRegion.count(InfluencedBB)) {
|
[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 18:22:09 +08:00
|
|
|
for (auto &I : *InfluencedBB) {
|
|
|
|
if (!DV.count(&I))
|
|
|
|
findUsersOutsideInfluenceRegion(I, InfluenceRegion);
|
|
|
|
}
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
DomTreeNode *IDomNode = DT.getNode(InfluencedBB)->getIDom();
|
|
|
|
if (IDomNode == nullptr)
|
|
|
|
break;
|
|
|
|
InfluencedBB = IDomNode->getBlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivergencePropagator::findUsersOutsideInfluenceRegion(
|
|
|
|
Instruction &I, const DenseSet<BasicBlock *> &InfluenceRegion) {
|
[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 18:22:09 +08:00
|
|
|
for (Use &Use : I.uses()) {
|
|
|
|
Instruction *UserInst = cast<Instruction>(Use.getUser());
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
if (!InfluenceRegion.count(UserInst->getParent())) {
|
[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 18:22:09 +08:00
|
|
|
DU.insert(&Use);
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
if (DV.insert(UserInst).second)
|
|
|
|
Worklist.push_back(UserInst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:44:26 +08:00
|
|
|
// A helper function for computeInfluenceRegion that adds successors of "ThisBB"
|
|
|
|
// to the influence region.
|
|
|
|
static void
|
|
|
|
addSuccessorsToInfluenceRegion(BasicBlock *ThisBB, BasicBlock *End,
|
|
|
|
DenseSet<BasicBlock *> &InfluenceRegion,
|
|
|
|
std::vector<BasicBlock *> &InfluenceStack) {
|
|
|
|
for (BasicBlock *Succ : successors(ThisBB)) {
|
|
|
|
if (Succ != End && InfluenceRegion.insert(Succ).second)
|
|
|
|
InfluenceStack.push_back(Succ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
void DivergencePropagator::computeInfluenceRegion(
|
|
|
|
BasicBlock *Start, BasicBlock *End,
|
|
|
|
DenseSet<BasicBlock *> &InfluenceRegion) {
|
|
|
|
assert(PDT.properlyDominates(End, Start) &&
|
|
|
|
"End does not properly dominate Start");
|
2015-12-19 05:44:26 +08:00
|
|
|
|
|
|
|
// The influence region starts from the end of "Start" to the beginning of
|
|
|
|
// "End". Therefore, "Start" should not be in the region unless "Start" is in
|
|
|
|
// a loop that doesn't contain "End".
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
std::vector<BasicBlock *> InfluenceStack;
|
2015-12-19 05:44:26 +08:00
|
|
|
addSuccessorsToInfluenceRegion(Start, End, InfluenceRegion, InfluenceStack);
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
while (!InfluenceStack.empty()) {
|
|
|
|
BasicBlock *BB = InfluenceStack.back();
|
|
|
|
InfluenceStack.pop_back();
|
2015-12-19 05:44:26 +08:00
|
|
|
addSuccessorsToInfluenceRegion(BB, End, InfluenceRegion, InfluenceStack);
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivergencePropagator::exploreDataDependency(Value *V) {
|
|
|
|
// Follow def-use chains of V.
|
|
|
|
for (User *U : V->users()) {
|
2019-10-02 16:56:33 +08:00
|
|
|
if (!TTI.isAlwaysUniform(U) && DV.insert(U).second)
|
|
|
|
Worklist.push_back(U);
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivergencePropagator::propagate() {
|
|
|
|
// Traverse the dependency graph using DFS.
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
Value *V = Worklist.back();
|
|
|
|
Worklist.pop_back();
|
2018-10-18 08:36:15 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
// Terminators with less than two successors won't introduce sync
|
|
|
|
// dependency. Ignore them.
|
2018-10-18 08:36:15 +08:00
|
|
|
if (I->isTerminator() && I->getNumSuccessors() > 1)
|
|
|
|
exploreSyncDependency(I);
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
|
|
|
exploreDataDependency(V);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 06:55:20 +08:00
|
|
|
} // namespace
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
|
2015-09-22 01:58:14 +08:00
|
|
|
// Register this pass.
|
2018-08-30 22:21:36 +08:00
|
|
|
char LegacyDivergenceAnalysis::ID = 0;
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
LegacyDivergenceAnalysis::LegacyDivergenceAnalysis() : FunctionPass(ID) {
|
|
|
|
initializeLegacyDivergenceAnalysisPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2018-12-01 06:55:20 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(LegacyDivergenceAnalysis, "divergence",
|
|
|
|
"Legacy Divergence Analysis", false, true)
|
2015-09-22 01:58:14 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2016-02-26 01:54:07 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
|
2018-12-01 06:55:20 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(LegacyDivergenceAnalysis, "divergence",
|
|
|
|
"Legacy Divergence Analysis", false, true)
|
2015-09-22 01:58:14 +08:00
|
|
|
|
2018-08-30 22:21:36 +08:00
|
|
|
FunctionPass *llvm::createLegacyDivergenceAnalysisPass() {
|
|
|
|
return new LegacyDivergenceAnalysis();
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
|
|
|
|
2018-08-30 22:21:36 +08:00
|
|
|
void LegacyDivergenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
|
2020-11-11 00:17:24 +08:00
|
|
|
AU.addRequiredTransitive<DominatorTreeWrapperPass>();
|
|
|
|
AU.addRequiredTransitive<PostDominatorTreeWrapperPass>();
|
|
|
|
AU.addRequiredTransitive<LoopInfoWrapperPass>();
|
2015-09-22 01:58:14 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2018-12-01 06:55:20 +08:00
|
|
|
bool LegacyDivergenceAnalysis::shouldUseGPUDivergenceAnalysis(
|
Resubmit: [DA][TTI][AMDGPU] Add option to select GPUDA with TTI
Summary:
Enable the new diveregence analysis by default for AMDGPU.
Resubmit with test updates since GPUDA was causing failures on Windows.
Reviewers: rampitec, nhaehnle, arsenm, thakis
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, t-tye, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73315
2020-01-20 23:25:20 +08:00
|
|
|
const Function &F, const TargetTransformInfo &TTI) const {
|
|
|
|
if (!(UseGPUDA || TTI.useGPUDivergenceAnalysis()))
|
2018-12-01 06:55:20 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// GPUDivergenceAnalysis requires a reducible CFG.
|
|
|
|
auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
|
|
|
using RPOTraversal = ReversePostOrderTraversal<const Function *>;
|
|
|
|
RPOTraversal FuncRPOT(&F);
|
|
|
|
return !containsIrreducibleCFG<const BasicBlock *, const RPOTraversal,
|
|
|
|
const LoopInfo>(FuncRPOT, LI);
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:21:36 +08:00
|
|
|
bool LegacyDivergenceAnalysis::runOnFunction(Function &F) {
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
|
|
|
|
if (TTIWP == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TargetTransformInfo &TTI = TTIWP->getTTI(F);
|
|
|
|
// Fast path: if the target does not have branch divergence, we do not mark
|
|
|
|
// any branch as divergent.
|
|
|
|
if (!TTI.hasBranchDivergence())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DivergentValues.clear();
|
[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 18:22:09 +08:00
|
|
|
DivergentUses.clear();
|
2018-12-01 06:55:20 +08:00
|
|
|
gpuDA = nullptr;
|
|
|
|
|
|
|
|
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2016-02-26 01:54:07 +08:00
|
|
|
auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
|
2018-12-01 06:55:20 +08:00
|
|
|
|
Resubmit: [DA][TTI][AMDGPU] Add option to select GPUDA with TTI
Summary:
Enable the new diveregence analysis by default for AMDGPU.
Resubmit with test updates since GPUDA was causing failures on Windows.
Reviewers: rampitec, nhaehnle, arsenm, thakis
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, t-tye, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73315
2020-01-20 23:25:20 +08:00
|
|
|
if (shouldUseGPUDivergenceAnalysis(F, TTI)) {
|
2018-12-01 06:55:20 +08:00
|
|
|
// run the new GPU divergence analysis
|
|
|
|
auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2021-02-16 12:56:45 +08:00
|
|
|
gpuDA = std::make_unique<DivergenceInfo>(F, DT, PDT, LI, TTI,
|
|
|
|
/* KnownReducible = */ true);
|
2018-12-01 06:55:20 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// run LLVM's existing DivergenceAnalysis
|
[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 18:22:09 +08:00
|
|
|
DivergencePropagator DP(F, TTI, DT, PDT, DivergentValues, DivergentUses);
|
2018-12-01 06:55:20 +08:00
|
|
|
DP.populateWithSourcesOfDivergence();
|
|
|
|
DP.propagate();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "\nAfter divergence analysis on " << F.getName()
|
|
|
|
<< ":\n";
|
|
|
|
print(dbgs(), F.getParent()));
|
|
|
|
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-01 06:55:20 +08:00
|
|
|
bool LegacyDivergenceAnalysis::isDivergent(const Value *V) const {
|
|
|
|
if (gpuDA) {
|
|
|
|
return gpuDA->isDivergent(*V);
|
|
|
|
}
|
|
|
|
return DivergentValues.count(V);
|
|
|
|
}
|
|
|
|
|
[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 18:22:09 +08:00
|
|
|
bool LegacyDivergenceAnalysis::isDivergentUse(const Use *U) const {
|
|
|
|
if (gpuDA) {
|
|
|
|
return gpuDA->isDivergentUse(*U);
|
|
|
|
}
|
|
|
|
return DivergentValues.count(U->get()) || DivergentUses.count(U);
|
|
|
|
}
|
|
|
|
|
2018-08-30 22:21:36 +08:00
|
|
|
void LegacyDivergenceAnalysis::print(raw_ostream &OS, const Module *) const {
|
2018-12-01 06:55:20 +08:00
|
|
|
if ((!gpuDA || !gpuDA->hasDivergence()) && DivergentValues.empty())
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
return;
|
2018-12-01 06:55:20 +08:00
|
|
|
|
2018-12-01 07:07:49 +08:00
|
|
|
const Function *F = nullptr;
|
2018-12-01 06:55:20 +08:00
|
|
|
if (!DivergentValues.empty()) {
|
|
|
|
const Value *FirstDivergentValue = *DivergentValues.begin();
|
|
|
|
if (const Argument *Arg = dyn_cast<Argument>(FirstDivergentValue)) {
|
|
|
|
F = Arg->getParent();
|
|
|
|
} else if (const Instruction *I =
|
|
|
|
dyn_cast<Instruction>(FirstDivergentValue)) {
|
|
|
|
F = I->getParent()->getParent();
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Only arguments and instructions can be divergent");
|
|
|
|
}
|
|
|
|
} else if (gpuDA) {
|
|
|
|
F = &gpuDA->getFunction();
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
2018-12-01 07:07:49 +08:00
|
|
|
if (!F)
|
|
|
|
return;
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
|
|
|
|
// Dumps all divergent values in F, arguments and then instructions.
|
|
|
|
for (auto &Arg : F->args()) {
|
2018-12-01 06:55:20 +08:00
|
|
|
OS << (isDivergent(&Arg) ? "DIVERGENT: " : " ");
|
2018-07-13 21:13:30 +08:00
|
|
|
OS << Arg << "\n";
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
2015-08-07 03:10:45 +08:00
|
|
|
// Iterate instructions using instructions() to ensure a deterministic order.
|
2021-02-07 03:17:09 +08:00
|
|
|
for (const BasicBlock &BB : *F) {
|
2018-07-13 21:13:30 +08:00
|
|
|
OS << "\n " << BB.getName() << ":\n";
|
|
|
|
for (auto &I : BB.instructionsWithoutDebug()) {
|
2018-12-01 06:55:20 +08:00
|
|
|
OS << (isDivergent(&I) ? "DIVERGENT: " : " ");
|
2018-07-13 21:13:30 +08:00
|
|
|
OS << I << "\n";
|
|
|
|
}
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|
2018-07-13 21:13:30 +08:00
|
|
|
OS << "\n";
|
Divergence analysis for GPU programs
Summary:
Some optimizations such as jump threading and loop unswitching can negatively
affect performance when applied to divergent branches. The divergence analysis
added in this patch conservatively estimates which branches in a GPU program
can diverge. This information can then help LLVM to run certain optimizations
selectively.
Test Plan: test/Analysis/DivergenceAnalysis/NVPTX/diverge.ll
Reviewers: resistor, hfinkel, eliben, meheff, jholewinski
Subscribers: broune, bjarke.roune, madhur13490, tstellarAMD, dberlin, echristo, jholewinski, llvm-commits
Differential Revision: http://reviews.llvm.org/D8576
llvm-svn: 234567
2015-04-10 13:03:50 +08:00
|
|
|
}
|