LLVM 22.0.0git
UniformityAnalysis.cpp
Go to the documentation of this file.
1//===- UniformityAnalysis.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
13#include "llvm/IR/Dominators.h"
17
18using namespace llvm;
19
20template <>
25
26template <>
31
33 for (auto &I : instructions(F)) {
34 InstructionUniformity IU = TTI->getInstructionUniformity(&I);
35 switch (IU) {
38 continue;
41 continue;
43 break;
44 }
45 }
46 for (auto &Arg : F.args()) {
47 if (TTI->getInstructionUniformity(&Arg) ==
49 markDivergent(&Arg);
50 }
51}
52
53template <>
54void llvm::GenericUniformityAnalysisImpl<SSAContext>::pushUsers(
55 const Value *V) {
56 for (const auto *User : V->users()) {
57 if (const auto *UserInstr = dyn_cast<const Instruction>(User)) {
58 markDivergent(*UserInstr);
59 }
60 }
61}
62
63template <>
64void llvm::GenericUniformityAnalysisImpl<SSAContext>::pushUsers(
65 const Instruction &Instr) {
66 assert(!isAlwaysUniform(Instr));
67 if (Instr.isTerminator())
68 return;
69 pushUsers(cast<Value>(&Instr));
70}
71
72template <>
73bool llvm::GenericUniformityAnalysisImpl<SSAContext>::usesValueFromCycle(
74 const Instruction &I, const Cycle &DefCycle) const {
75 assert(!isAlwaysUniform(I));
76 for (const Use &U : I.operands()) {
77 if (auto *I = dyn_cast<Instruction>(&U)) {
78 if (DefCycle.contains(I->getParent()))
79 return true;
80 }
81 }
82 return false;
83}
84
85template <>
87 SSAContext>::propagateTemporalDivergence(const Instruction &I,
88 const Cycle &DefCycle) {
89 for (auto *User : I.users()) {
90 auto *UserInstr = cast<Instruction>(User);
91 if (DefCycle.contains(UserInstr->getParent()))
92 continue;
93 markDivergent(*UserInstr);
94 recordTemporalDivergence(&I, UserInstr, &DefCycle);
95 }
96}
97
98template <>
100 const Use &U) const {
101 const auto *V = U.get();
102 if (isDivergent(V))
103 return true;
104 if (const auto *DefInstr = dyn_cast<Instruction>(V)) {
105 const auto *UseInstr = cast<Instruction>(U.getUser());
106 return isTemporalDivergent(*UseInstr->getParent(), *DefInstr);
107 }
108 return false;
109}
110
111// This ensures explicit instantiation of
112// GenericUniformityAnalysisImpl::ImplDeleter::operator()
116
117//===----------------------------------------------------------------------===//
118// UniformityInfoAnalysis and related pass implementations
119//===----------------------------------------------------------------------===//
120
123 auto &DT = FAM.getResult<DominatorTreeAnalysis>(F);
124 auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
125 auto &CI = FAM.getResult<CycleAnalysis>(F);
126 UniformityInfo UI{DT, CI, &TTI};
127 // Skip computation if we can assume everything is uniform.
128 if (TTI.hasBranchDivergence(&F))
129 UI.compute();
130
131 return UI;
132}
133
134AnalysisKey UniformityInfoAnalysis::Key;
135
138
141 OS << "UniformityInfo for function '" << F.getName() << "':\n";
143
144 return PreservedAnalyses::all();
145}
146
147//===----------------------------------------------------------------------===//
148// UniformityInfoWrapperPass Implementation
149//===----------------------------------------------------------------------===//
150
152
154
156 "Uniformity Analysis", false, true)
161 "Uniformity Analysis", false, true)
162
164 AU.setPreservesAll();
165 AU.addRequired<DominatorTreeWrapperPass>();
166 AU.addRequiredTransitive<CycleInfoWrapperPass>();
167 AU.addRequired<TargetTransformInfoWrapperPass>();
168}
169
171 auto &cycleInfo = getAnalysis<CycleInfoWrapperPass>().getResult();
172 auto &domTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
173 auto &targetTransformInfo =
175
176 m_function = &F;
177 m_uniformityInfo = UniformityInfo{domTree, cycleInfo, &targetTransformInfo};
178
179 // Skip computation if we can assume everything is uniform.
180 if (targetTransformInfo.hasBranchDivergence(m_function))
181 m_uniformityInfo.compute();
182
183 return false;
184}
185
187 OS << "UniformityInfo for function '" << m_function->getName() << "':\n";
188 m_uniformityInfo.print(OS);
189}
190
192 m_uniformityInfo = UniformityInfo{};
193 m_function = nullptr;
194}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
This file declares an analysis pass that computes CycleInfo for LLVM IR, specialized from GenericCycl...
Implementation of uniformity analysis.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This pass exposes codegen information to IR-level passes.
LLVM IR instance of the generic uniformity analysis.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
Analysis pass which computes a CycleInfo.
Legacy analysis pass which computes a CycleInfo.
Analysis pass which computes a DominatorTree.
Definition Dominators.h:283
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:321
FunctionPass(char &pid)
Definition Pass.h:316
bool contains(const BlockT *Block) const
Return whether Block is contained in the cycle.
Analysis that identifies uniform values in a data-parallel execution.
bool isDivergentUse(const UseT &U) const
bool hasDivergentDefs(const InstructionT &I) const
bool markDefsDivergent(const InstructionT &Instr)
Mark outputs of Instr as divergent.
bool isDivergent(const InstructionT &I) const
void markDivergent(const InstructionT &I)
Examine I for divergent outputs and add to the worklist.
void addUniformOverride(const InstructionT &Instr)
Mark UniVal as a value that is always uniform.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Analysis pass providing the TargetTransformInfo.
Wrapper pass for TargetTransformInfo.
Analysis pass which computes UniformityInfo.
UniformityInfo run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Legacy analysis pass which computes a CycleInfo.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
iterator_range< user_iterator > users()
Definition Value.h:426
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
GenericSSAContext< Function > SSAContext
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
CycleInfo::CycleT Cycle
Definition CycleInfo.h:24
TargetTransformInfo TTI
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
InstructionUniformity
Enum describing how instructions behave with respect to uniformity and divergence,...
Definition Uniformity.h:18
@ AlwaysUniform
The result values are always uniform.
Definition Uniformity.h:23
@ NeverUniform
The result values can never be assumed to be uniform.
Definition Uniformity.h:26
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29