-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[AMDGPU] Add mark last scratch load pass #75512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5414a67
[AMDGPU] Add last_use operand for SI_SPILL_*_RESTORE insts
mbrkusanin b4721de
[AMDGPU] Add mark last scratch load pass
mbrkusanin ba1f940
move pass to addRegAssignAndRewriteOptimized
mbrkusanin 34607ea
change expected output in .ll test
mbrkusanin 96578b8
set last use in cache policy
mbrkusanin a472a0e
Remove some LLVM_DEBUG lines
mbrkusanin 31f5d4a
move segmentstart
mbrkusanin d1c1b2f
clang-format
mbrkusanin 750604e
address comments
mbrkusanin b519a1e
Add new MMO target flag amdgpu-last-use
mbrkusanin 7e9404a
Revert "[AMDGPU] Add last_use operand for SI_SPILL_*_RESTORE insts"
mbrkusanin d4fbb7d
regenerate .mir test
mbrkusanin 9d7952f
address comments
mbrkusanin 5d9acc6
Handle deleted instructions at segment end
mbrkusanin 843da2f
clang-format
mbrkusanin 752221d
Check if MMO exists
mbrkusanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
//===-- AMDGPUMarkLastScratchLoad.cpp -------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Mark scratch load/spill instructions which are guaranteed to be the last time | ||
// this scratch slot is used so it can be evicted from caches. | ||
// | ||
// TODO: Handle general stack accesses not just spilling. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "AMDGPU.h" | ||
#include "GCNSubtarget.h" | ||
#include "llvm/CodeGen/LiveIntervals.h" | ||
#include "llvm/CodeGen/LiveStacks.h" | ||
#include "llvm/CodeGen/MachineOperand.h" | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "amdgpu-mark-last-scratch-load" | ||
|
||
namespace { | ||
|
||
class AMDGPUMarkLastScratchLoad : public MachineFunctionPass { | ||
private: | ||
LiveStacks *LS = nullptr; | ||
LiveIntervals *LIS = nullptr; | ||
SlotIndexes *SI = nullptr; | ||
const SIInstrInfo *SII = nullptr; | ||
|
||
public: | ||
static char ID; | ||
|
||
AMDGPUMarkLastScratchLoad() : MachineFunctionPass(ID) { | ||
initializeAMDGPUMarkLastScratchLoadPass(*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
bool runOnMachineFunction(MachineFunction &MF) override; | ||
|
||
void getAnalysisUsage(AnalysisUsage &AU) const override { | ||
AU.addRequired<SlotIndexes>(); | ||
AU.addRequired<LiveIntervals>(); | ||
AU.addRequired<LiveStacks>(); | ||
AU.setPreservesAll(); | ||
MachineFunctionPass::getAnalysisUsage(AU); | ||
} | ||
|
||
StringRef getPassName() const override { | ||
return "AMDGPU Mark Last Scratch Load"; | ||
} | ||
}; | ||
|
||
} // end anonymous namespace | ||
|
||
bool AMDGPUMarkLastScratchLoad::runOnMachineFunction(MachineFunction &MF) { | ||
if (skipFunction(MF.getFunction())) | ||
return false; | ||
|
||
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); | ||
if (ST.getGeneration() < AMDGPUSubtarget::GFX12) | ||
return false; | ||
|
||
LS = &getAnalysis<LiveStacks>(); | ||
LIS = &getAnalysis<LiveIntervals>(); | ||
SI = &getAnalysis<SlotIndexes>(); | ||
SII = ST.getInstrInfo(); | ||
SlotIndexes &Slots = *LIS->getSlotIndexes(); | ||
|
||
const unsigned NumSlots = LS->getNumIntervals(); | ||
if (NumSlots == 0) { | ||
LLVM_DEBUG(dbgs() << "No live slots, skipping\n"); | ||
return false; | ||
} | ||
|
||
LLVM_DEBUG(dbgs() << LS->getNumIntervals() << " intervals\n"); | ||
|
||
bool Changed = false; | ||
|
||
for (auto &[SS, LI] : *LS) { | ||
for (const LiveRange::Segment &Segment : LI.segments) { | ||
|
||
// Ignore segments that run to the end of basic block because in this case | ||
// slot is still live at the end of it. | ||
if (Segment.end.isBlock()) | ||
continue; | ||
|
||
const int FrameIndex = Register::stackSlot2Index(LI.reg()); | ||
MachineInstr *LastLoad = nullptr; | ||
|
||
MachineInstr *MISegmentEnd = SI->getInstructionFromIndex(Segment.end); | ||
|
||
// If there is no instruction at this slot because it was deleted take the | ||
// instruction from the next slot. | ||
if (!MISegmentEnd) { | ||
SlotIndex NextSlot = Slots.getNextNonNullIndex(Segment.end); | ||
MISegmentEnd = SI->getInstructionFromIndex(NextSlot); | ||
} | ||
|
||
MachineInstr *MISegmentStart = SI->getInstructionFromIndex(Segment.start); | ||
MachineBasicBlock *BB = MISegmentEnd->getParent(); | ||
|
||
// Start iteration backwards from segment end until the start of basic | ||
// block or start of segment if it is in the same basic block. | ||
auto End = BB->rend(); | ||
if (MISegmentStart && MISegmentStart->getParent() == BB) | ||
End = MISegmentStart->getReverseIterator(); | ||
|
||
for (auto MI = MISegmentEnd->getReverseIterator(); MI != End; ++MI) { | ||
int LoadFI = 0; | ||
|
||
if (SII->isLoadFromStackSlot(*MI, LoadFI) && LoadFI == FrameIndex) { | ||
LastLoad = &*MI; | ||
break; | ||
} | ||
} | ||
|
||
if (LastLoad && !LastLoad->memoperands_empty()) { | ||
MachineMemOperand *MMO = *LastLoad->memoperands_begin(); | ||
mbrkusanin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MMO->setFlags(MOLastUse); | ||
Changed = true; | ||
LLVM_DEBUG(dbgs() << " Found last load: " << *LastLoad); | ||
} | ||
} | ||
} | ||
|
||
return Changed; | ||
} | ||
|
||
char AMDGPUMarkLastScratchLoad::ID = 0; | ||
|
||
char &llvm::AMDGPUMarkLastScratchLoadID = AMDGPUMarkLastScratchLoad::ID; | ||
|
||
INITIALIZE_PASS_BEGIN(AMDGPUMarkLastScratchLoad, DEBUG_TYPE, | ||
"AMDGPU Mark last scratch load", false, false) | ||
INITIALIZE_PASS_DEPENDENCY(SlotIndexes) | ||
INITIALIZE_PASS_DEPENDENCY(LiveStacks) | ||
INITIALIZE_PASS_END(AMDGPUMarkLastScratchLoad, DEBUG_TYPE, | ||
"AMDGPU Mark last scratch load", false, false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,10 @@ class ScheduleHazardRecognizer; | |
static const MachineMemOperand::Flags MONoClobber = | ||
MachineMemOperand::MOTargetFlag1; | ||
|
||
/// Mark the MMO of a load as the last use. | ||
static const MachineMemOperand::Flags MOLastUse = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Theoretically we need something to ensure the stores aren't reordered, but it's probably not a practical concern as this runs late enough |
||
MachineMemOperand::MOTargetFlag2; | ||
|
||
/// Utility to store machine instructions worklist. | ||
struct SIInstrWorklist { | ||
SIInstrWorklist() = default; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.