LLVM 24.0.0git
SILowerWWMCopies.cpp
Go to the documentation of this file.
1//===-- SILowerWWMCopies.cpp - Lower Copies after regalloc ---===//
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//
9/// \file
10/// Lowering the WWM_COPY instructions for various register classes.
11/// AMDGPU target generates WWM_COPY instruction to differentiate WWM
12/// copy from COPY. This pass generates the necessary exec mask manipulation
13/// instructions to replicate 'Whole Wave Mode' and lowers WWM_COPY back to
14/// COPY.
15//
16//===----------------------------------------------------------------------===//
17
18#include "SILowerWWMCopies.h"
19#include "AMDGPU.h"
20#include "GCNSubtarget.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "si-lower-wwm-copies"
30
31namespace {
32
33class SILowerWWMCopies {
34public:
35 SILowerWWMCopies(LiveIntervals *LIS, SlotIndexes *SI, VirtRegMap *VRM)
36 : LIS(LIS), Indexes(SI), VRM(VRM) {}
37 bool run(MachineFunction &MF);
38
39private:
40 bool isSCCLiveAtMI(const MachineInstr &MI);
41 void addToWWMSpills(MachineFunction &MF, Register Reg);
42
43 LiveIntervals *LIS;
44 SlotIndexes *Indexes;
45 VirtRegMap *VRM;
46 const SIRegisterInfo *TRI;
48};
49
50class SILowerWWMCopiesLegacy : public MachineFunctionPass {
51public:
52 static char ID;
53
54 SILowerWWMCopiesLegacy() : MachineFunctionPass(ID) {}
55
56 bool runOnMachineFunction(MachineFunction &MF) override;
57
58 StringRef getPassName() const override { return "SI Lower WWM Copies"; }
59
60 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 AU.setPreservesAll();
66 }
67};
68
69} // End anonymous namespace.
70
71INITIALIZE_PASS_BEGIN(SILowerWWMCopiesLegacy, DEBUG_TYPE, "SI Lower WWM Copies",
72 false, false)
75INITIALIZE_PASS_END(SILowerWWMCopiesLegacy, DEBUG_TYPE, "SI Lower WWM Copies",
77
78char SILowerWWMCopiesLegacy::ID = 0;
79
80char &llvm::SILowerWWMCopiesLegacyID = SILowerWWMCopiesLegacy::ID;
81
82bool SILowerWWMCopies::isSCCLiveAtMI(const MachineInstr &MI) {
83 // We can't determine the liveness info if LIS isn't available. Early return
84 // in that case and always assume SCC is live.
85 if (!LIS)
86 return true;
87
88 LiveRange &LR =
89 LIS->getRegUnit(*MCRegUnitIterator(MCRegister::from(AMDGPU::SCC), TRI));
90 SlotIndex Idx = LIS->getInstructionIndex(MI);
91 return LR.liveAt(Idx);
92}
93
94// If \p Reg is assigned with a physical VGPR, add the latter into wwm-spills
95// for preserving its entire lanes at function prolog/epilog.
96void SILowerWWMCopies::addToWWMSpills(MachineFunction &MF, Register Reg) {
97 if (Reg.isPhysical())
98 return;
99
100 // FIXME: VRM may be null here.
101 MCRegister PhysReg = VRM->getPhys(Reg);
102 assert(PhysReg && "should have allocated a physical register");
103
104 MFI->allocateWWMSpill(MF, PhysReg);
105}
106
107bool SILowerWWMCopiesLegacy::runOnMachineFunction(MachineFunction &MF) {
108 auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
109 auto *LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
110
111 auto *SIWrapper = getAnalysisIfAvailable<SlotIndexesWrapperPass>();
112 auto *Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
113
114 auto *VRMWrapper = getAnalysisIfAvailable<VirtRegMapWrapperLegacy>();
115 auto *VRM = VRMWrapper ? &VRMWrapper->getVRM() : nullptr;
116
117 SILowerWWMCopies Impl(LIS, Indexes, VRM);
118 return Impl.run(MF);
119}
120
121PreservedAnalyses
124 auto *LIS = MFAM.getCachedResult<LiveIntervalsAnalysis>(MF);
125 auto *Indexes = MFAM.getCachedResult<SlotIndexesAnalysis>(MF);
126 auto *VRM = MFAM.getCachedResult<VirtRegMapAnalysis>(MF);
127
128 SILowerWWMCopies Impl(LIS, Indexes, VRM);
129 Impl.run(MF);
130 return PreservedAnalyses::all();
131}
132
133bool SILowerWWMCopies::run(MachineFunction &MF) {
134 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
135 const SIInstrInfo *TII = ST.getInstrInfo();
136
137 MFI = MF.getInfo<SIMachineFunctionInfo>();
138 TRI = ST.getRegisterInfo();
139
140 if (!MFI->hasVRegFlags())
141 return false;
142
143 bool Changed = false;
144 for (MachineBasicBlock &MBB : MF) {
145 for (MachineInstr &MI : MBB) {
146 if (MI.getOpcode() != AMDGPU::WWM_COPY)
147 continue;
148
149 // TODO: Club adjacent WWM ops between same exec save/restore
150 assert(TII->isVGPRCopy(MI));
151
152 // For WWM vector copies, manipulate the exec mask around the copy
153 // instruction.
154 const DebugLoc &DL = MI.getDebugLoc();
155 MachineBasicBlock::iterator InsertPt = MI.getIterator();
156 Register RegForExecCopy = MFI->getSGPRForEXECCopy();
157 TII->insertScratchExecCopy(MF, MBB, InsertPt, DL, RegForExecCopy,
158 isSCCLiveAtMI(MI), Indexes);
159 TII->restoreExec(MF, MBB, ++InsertPt, DL, RegForExecCopy, Indexes);
160 addToWWMSpills(MF, MI.getOperand(0).getReg());
161 LLVM_DEBUG(dbgs() << "WWM copy manipulation for " << MI);
162
163 // Lower WWM_COPY back to COPY
164 MI.setDesc(TII->get(AMDGPU::COPY));
165 Changed |= true;
166 }
167 }
168
169 return Changed;
170}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register Reg
Register const TargetRegisterInfo * TRI
#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
#define LLVM_DEBUG(...)
Definition Debug.h:119
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
A debug info location.
Definition DebugLoc.h:127
This class represents the liveness of a register, stack slot, etc.
bool liveAt(SlotIndex index) const
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
static MCRegister from(unsigned Val)
Check the provided unsigned value is a valid MCRegister.
Definition MCRegister.h:77
MachineInstrBundleIterator< MachineInstr > iterator
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.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Representation of each machine instruction.
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:20
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
void allocateWWMSpill(MachineFunction &MF, Register VGPR, uint64_t Size=4, Align Alignment=Align(4))
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:70
SlotIndexes pass.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:89
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
char & SILowerWWMCopiesLegacyID