LLVM 22.0.0git
MachineUniformityAnalysis.cpp
Go to the documentation of this file.
1//===- MachineUniformityAnalysis.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
18
19using namespace llvm;
20
21template <>
23 const MachineInstr &I) const {
24 for (auto &op : I.all_defs()) {
25 if (isDivergent(op.getReg()))
26 return true;
27 }
28 return false;
29}
30
31template <>
33 const MachineInstr &Instr) {
34 bool insertedDivergent = false;
35 const auto &MRI = F.getRegInfo();
36 const auto &RBI = *F.getSubtarget().getRegBankInfo();
37 const auto &TRI = *MRI.getTargetRegisterInfo();
38 for (auto &op : Instr.all_defs()) {
39 if (!op.getReg().isVirtual())
40 continue;
41 assert(!op.getSubReg());
42 if (TRI.isUniformReg(MRI, RBI, op.getReg()))
43 continue;
44 insertedDivergent |= markDivergent(op.getReg());
45 }
46 return insertedDivergent;
47}
48
49template <>
51 const auto &InstrInfo = *F.getSubtarget().getInstrInfo();
52
53 for (const MachineBasicBlock &block : F) {
54 for (const MachineInstr &instr : block) {
55 auto uniformity = InstrInfo.getInstructionUniformity(instr);
56 if (uniformity == InstructionUniformity::AlwaysUniform) {
57 addUniformOverride(instr);
58 continue;
59 }
60
61 if (uniformity == InstructionUniformity::NeverUniform) {
62 markDivergent(instr);
63 }
64 }
65 }
66}
67
68template <>
70 Register Reg) {
71 assert(isDivergent(Reg));
72 const auto &RegInfo = F.getRegInfo();
73 for (MachineInstr &UserInstr : RegInfo.use_instructions(Reg)) {
74 markDivergent(UserInstr);
75 }
76}
77
78template <>
80 const MachineInstr &Instr) {
81 assert(!isAlwaysUniform(Instr));
82 if (Instr.isTerminator())
83 return;
84 for (const MachineOperand &op : Instr.all_defs()) {
85 auto Reg = op.getReg();
86 if (isDivergent(Reg))
87 pushUsers(Reg);
88 }
89}
90
91template <>
93 const MachineInstr &I, const MachineCycle &DefCycle) const {
94 assert(!isAlwaysUniform(I));
95 for (auto &Op : I.operands()) {
96 if (!Op.isReg() || !Op.readsReg())
97 continue;
98 auto Reg = Op.getReg();
99
100 // FIXME: Physical registers need to be properly checked instead of always
101 // returning true
102 if (Reg.isPhysical())
103 return true;
104
105 auto *Def = F.getRegInfo().getVRegDef(Reg);
106 if (DefCycle.contains(Def->getParent()))
107 return true;
108 }
109 return false;
110}
111
112template <>
115 const MachineCycle &DefCycle) {
116 const auto &RegInfo = F.getRegInfo();
117 for (auto &Op : I.all_defs()) {
118 if (!Op.getReg().isVirtual())
119 continue;
120 auto Reg = Op.getReg();
121 for (MachineInstr &UserInstr : RegInfo.use_instructions(Reg)) {
122 if (DefCycle.contains(UserInstr.getParent()))
123 continue;
124 markDivergent(UserInstr);
125
126 recordTemporalDivergence(Reg, &UserInstr, &DefCycle);
127 }
128 }
129}
130
131template <>
133 const MachineOperand &U) const {
134 if (!U.isReg())
135 return false;
136
137 auto Reg = U.getReg();
138 if (isDivergent(Reg))
139 return true;
140
141 const auto &RegInfo = F.getRegInfo();
142 auto *Def = RegInfo.getOneDef(Reg);
143 if (!Def)
144 return true;
145
146 auto *DefInstr = Def->getParent();
147 auto *UseInstr = U.getParent();
148 return isTemporalDivergent(*UseInstr->getParent(), *DefInstr);
149}
150
151// This ensures explicit instantiation of
152// GenericUniformityAnalysisImpl::ImplDeleter::operator()
156
158 MachineFunction &F, const MachineCycleInfo &cycleInfo,
159 const MachineDominatorTree &domTree, bool HasBranchDivergence) {
160 assert(F.getRegInfo().isSSA() && "Expected to be run on SSA form!");
161 MachineUniformityInfo UI(domTree, cycleInfo);
162 if (HasBranchDivergence)
163 UI.compute();
164 return UI;
165}
166
167namespace {
168
169class MachineUniformityInfoPrinterPass : public MachineFunctionPass {
170public:
171 static char ID;
172
173 MachineUniformityInfoPrinterPass();
174
175 bool runOnMachineFunction(MachineFunction &F) override;
176 void getAnalysisUsage(AnalysisUsage &AU) const override;
177};
178
179} // namespace
180
181AnalysisKey MachineUniformityAnalysis::Key;
182
186 auto &DomTree = MFAM.getResult<MachineDominatorTreeAnalysis>(MF);
187 auto &CI = MFAM.getResult<MachineCycleAnalysis>(MF);
189 .getManager();
190 auto &F = MF.getFunction();
192 return computeMachineUniformityInfo(MF, CI, DomTree,
194}
195
199 auto &MUI = MFAM.getResult<MachineUniformityAnalysis>(MF);
200 OS << "MachineUniformityInfo for function: ";
201 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
202 OS << '\n';
203 MUI.print(OS);
204 return PreservedAnalyses::all();
205}
206
208
212}
213
215 "Machine Uniformity Info Analysis", false, true)
219 "Machine Uniformity Info Analysis", false, true)
220
221void MachineUniformityAnalysisPass::getAnalysisUsage(AnalysisUsage &AU) const {
222 AU.setPreservesAll();
223 AU.addRequiredTransitive<MachineCycleInfoWrapperPass>();
224 AU.addRequired<MachineDominatorTreeWrapperPass>();
226}
227
229 auto &DomTree = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
230 auto &CI = getAnalysis<MachineCycleInfoWrapperPass>().getCycleInfo();
231 // FIXME: Query TTI::hasBranchDivergence. -run-pass seems to end up with a
232 // default NoTTI
233 UI = computeMachineUniformityInfo(MF, CI, DomTree, true);
234 return false;
235}
236
238 const Module *) const {
239 OS << "MachineUniformityInfo for function: ";
240 UI.getFunction().getFunction().printAsOperand(OS, /*PrintType=*/false);
241 OS << '\n';
242 UI.print(OS);
243}
244
245char MachineUniformityInfoPrinterPass::ID = 0;
246
247MachineUniformityInfoPrinterPass::MachineUniformityInfoPrinterPass()
251}
252
253INITIALIZE_PASS_BEGIN(MachineUniformityInfoPrinterPass,
254 "print-machine-uniformity",
255 "Print Machine Uniformity Info Analysis", true, true)
257INITIALIZE_PASS_END(MachineUniformityInfoPrinterPass,
258 "print-machine-uniformity",
259 "Print Machine Uniformity Info Analysis", true, true)
260
261void MachineUniformityInfoPrinterPass::getAnalysisUsage(
262 AnalysisUsage &AU) const {
263 AU.setPreservesAll();
264 AU.addRequired<MachineUniformityAnalysisPass>();
266}
267
268bool MachineUniformityInfoPrinterPass::runOnMachineFunction(
270 auto &UI = getAnalysis<MachineUniformityAnalysisPass>();
271 UI.print(errs());
272 return false;
273}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
block Block Frequency Analysis
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:390
Implementation of uniformity analysis.
#define op(i)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares a specialization of the GenericSSAContext<X> template class for Machine IR.
Register const TargetRegisterInfo * TRI
machine uniformity
Machine IR instance of the generic uniformity analysis.
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
raw_pwrite_stream & OS
This pass exposes codegen information to IR-level passes.
unify loop Fixup each natural loop to have a single exit block
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
Represent the analysis usage information of a pass.
This class represents an Operation in the Expression.
A possibly irreducible generalization of a Loop.
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.
void print(raw_ostream &Out) const
T helper function for printing.
const FunctionT & getFunction() const
The GPU kernel this analysis result is for.
Legacy analysis pass which computes a MachineCycleInfo.
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:72
MachineOperand class - Representation of each machine instruction operand.
Legacy analysis pass which computes a MachineUniformityInfo.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI bool hasBranchDivergence(const Function *F=nullptr) const
Return true if branch divergence exists.
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:5305
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Reg
All possible values of the reg field in the ModR/M byte.
NodeAddr< InstrNode * > Instr
Definition: RDFGraph.h:389
NodeAddr< DefNode * > Def
Definition: RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
LLVM_ABI void initializeMachineUniformityAnalysisPassPass(PassRegistry &)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MachineUniformityInfo computeMachineUniformityInfo(MachineFunction &F, const MachineCycleInfo &cycleInfo, const MachineDominatorTree &domTree, bool HasBranchDivergence)
Compute uniformity information for a Machine IR function.
LLVM_ABI void initializeMachineUniformityInfoPrinterPassPass(PassRegistry &)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29