LLVM 22.0.0git
AMDGPUMarkLastScratchLoad.cpp
Go to the documentation of this file.
1//===-- AMDGPUMarkLastScratchLoad.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//
9// Mark scratch load/spill instructions which are guaranteed to be the last time
10// this scratch slot is used so it can be evicted from caches.
11//
12// TODO: Handle general stack accesses not just spilling.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AMDGPU.h"
17#include "GCNSubtarget.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "amdgpu-mark-last-scratch-load"
25
26namespace {
27
28class AMDGPUMarkLastScratchLoad {
29private:
30 LiveStacks *LS = nullptr;
31 LiveIntervals *LIS = nullptr;
32 SlotIndexes *SI = nullptr;
33 const SIInstrInfo *SII = nullptr;
34
35public:
36 AMDGPUMarkLastScratchLoad(LiveStacks *LS, LiveIntervals *LIS, SlotIndexes *SI)
37 : LS(LS), LIS(LIS), SI(SI) {}
38 bool run(MachineFunction &MF);
39};
40
41class AMDGPUMarkLastScratchLoadLegacy : public MachineFunctionPass {
42public:
43 static char ID;
44
45 AMDGPUMarkLastScratchLoadLegacy() : MachineFunctionPass(ID) {}
46
47 bool runOnMachineFunction(MachineFunction &MF) override;
48
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
53 AU.setPreservesAll();
55 }
56
57 StringRef getPassName() const override {
58 return "AMDGPU Mark Last Scratch Load";
59 }
60};
61
62} // end anonymous namespace
63
64bool AMDGPUMarkLastScratchLoadLegacy::runOnMachineFunction(
65 MachineFunction &MF) {
66 if (skipFunction(MF.getFunction()))
67 return false;
68
69 auto &LS = getAnalysis<LiveStacksWrapperLegacy>().getLS();
70 auto &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
71 auto &SI = getAnalysis<SlotIndexesWrapperPass>().getSI();
72
73 return AMDGPUMarkLastScratchLoad(&LS, &LIS, &SI).run(MF);
74}
75
79 auto &LS = MFAM.getResult<LiveStacksAnalysis>(MF);
80 auto &LIS = MFAM.getResult<LiveIntervalsAnalysis>(MF);
81 auto &SI = MFAM.getResult<SlotIndexesAnalysis>(MF);
82
83 AMDGPUMarkLastScratchLoad(&LS, &LIS, &SI).run(MF);
85}
86
87bool AMDGPUMarkLastScratchLoad::run(MachineFunction &MF) {
88 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
89 if (ST.getGeneration() < AMDGPUSubtarget::GFX12)
90 return false;
91
92 SII = ST.getInstrInfo();
93 SlotIndexes &Slots = *LIS->getSlotIndexes();
94
95 const unsigned NumSlots = LS->getNumIntervals();
96 if (NumSlots == 0) {
97 LLVM_DEBUG(dbgs() << "No live slots, skipping\n");
98 return false;
99 }
100
101 LLVM_DEBUG(dbgs() << LS->getNumIntervals() << " intervals\n");
102
103 bool Changed = false;
104
105 for (auto &[SS, LI] : *LS) {
106 for (const LiveRange::Segment &Segment : LI.segments) {
107
108 // Ignore segments that run to the end of basic block because in this case
109 // slot is still live at the end of it.
110 if (Segment.end.isBlock())
111 continue;
112
113 const int FrameIndex = LI.reg().stackSlotIndex();
114 MachineInstr *LastLoad = nullptr;
115
116 MachineInstr *MISegmentEnd = SI->getInstructionFromIndex(Segment.end);
117
118 // If there is no instruction at this slot because it was deleted take the
119 // instruction from the next slot.
120 if (!MISegmentEnd) {
121 SlotIndex NextSlot = Slots.getNextNonNullIndex(Segment.end);
122 MISegmentEnd = SI->getInstructionFromIndex(NextSlot);
123 }
124
125 MachineInstr *MISegmentStart = SI->getInstructionFromIndex(Segment.start);
126 MachineBasicBlock *BB = MISegmentEnd->getParent();
127
128 // Start iteration backwards from segment end until the start of basic
129 // block or start of segment if it is in the same basic block.
130 auto End = BB->rend();
131 if (MISegmentStart && MISegmentStart->getParent() == BB)
132 End = MISegmentStart->getReverseIterator();
133
134 for (auto MI = MISegmentEnd->getReverseIterator(); MI != End; ++MI) {
135 int LoadFI = 0;
136
137 if (SII->isLoadFromStackSlot(*MI, LoadFI) && LoadFI == FrameIndex) {
138 LastLoad = &*MI;
139 break;
140 }
141 }
142
143 if (LastLoad && !LastLoad->memoperands_empty()) {
144 MachineMemOperand *MMO = *LastLoad->memoperands_begin();
145 MMO->setFlags(MOLastUse);
146 Changed = true;
147 LLVM_DEBUG(dbgs() << " Found last load: " << *LastLoad);
148 }
149 }
150 }
151
152 return Changed;
153}
154
155char AMDGPUMarkLastScratchLoadLegacy::ID = 0;
156
157char &llvm::AMDGPUMarkLastScratchLoadID = AMDGPUMarkLastScratchLoadLegacy::ID;
158
159INITIALIZE_PASS_BEGIN(AMDGPUMarkLastScratchLoadLegacy, DEBUG_TYPE,
160 "AMDGPU Mark last scratch load", false, false)
163INITIALIZE_PASS_END(AMDGPUMarkLastScratchLoadLegacy, DEBUG_TYPE,
164 "AMDGPU Mark last scratch load", false, false)
AMDGPU Mark last scratch load
bool End
Definition: ELF_riscv.cpp:480
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#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
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &AM)
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.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
reverse_iterator rend()
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...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:72
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:359
bool memoperands_empty() const
Return true if we don't have any memory operands which described the memory access done by this instr...
Definition: MachineInstr.h:810
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:798
A description of a memory reference used in the backend.
void setFlags(Flags f)
Bitwise OR the current flags with the given flags.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
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
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:66
bool isBlock() const
isBlock - Returns true if this is a block boundary slot.
Definition: SlotIndexes.h:210
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &)
Definition: SlotIndexes.cpp:24
SlotIndexes pass.
Definition: SlotIndexes.h:298
SlotIndex getNextNonNullIndex(SlotIndex Index)
Returns the next non-null index, if one exists.
Definition: SlotIndexes.h:404
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
reverse_self_iterator getReverseIterator()
Definition: ilist_node.h:137
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
static const MachineMemOperand::Flags MOLastUse
Mark the MMO of a load as the last use.
Definition: SIInstrInfo.h:48
char & AMDGPUMarkLastScratchLoadID
This represents a simple continuous liveness interval for a value.
Definition: LiveInterval.h:163