-
Notifications
You must be signed in to change notification settings - Fork 29
[AIEX] Extend Staged 2D/3D regalloc to avoid spills #685
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
base: aie-public
Are you sure you want to change the base?
Changes from 1 commit
afdc992
12da76d
23c18b4
264a491
9975813
1968bf5
3527e24
fbaf155
925f9d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,21 +184,26 @@ LaneBitmask getLiveLanesAt(SlotIndex Index, Register Reg, | |
| return LiveLanes; | ||
| } | ||
|
|
||
| void rewriteSuperReg(Register Reg, Register AssignedPhysReg, | ||
| void rewriteSuperReg(Register Reg, std::optional<Register> AssignedPhysReg, | ||
| SmallSet<int, 8> &SubRegs, MachineRegisterInfo &MRI, | ||
| const AIEBaseRegisterInfo &TRI, VirtRegMap &VRM, | ||
| LiveRegMatrix &LRM, LiveIntervals &LIS, | ||
| SlotIndexes &Indexes, LiveDebugVariables &DebugVars) { | ||
| LLVM_DEBUG(dbgs() << "Rewriting " << printReg(Reg, &TRI, 0, &MRI) << '\n'); | ||
| auto *TII = static_cast<const AIEBaseInstrInfo *>( | ||
| VRM.getMachineFunction().getSubtarget().getInstrInfo()); | ||
| MachineFunction &MF = VRM.getMachineFunction(); | ||
| auto *TII = | ||
| static_cast<const AIEBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); | ||
|
|
||
| // Collect all the subreg indices to rewrite as independent vregs. | ||
| SmallMapVector<int, Register, 8> SubRegToVReg; | ||
| const TargetRegisterClass *SuperRC = MRI.getRegClass(Reg); | ||
| assert(!SubRegs.empty()); | ||
| for (int SubReg : SubRegs) { | ||
| const TargetRegisterClass *SubRC = TRI.getSubRegisterClass(SuperRC, SubReg); | ||
| const TargetRegisterClass *SubRC = | ||
| AssignedPhysReg.has_value() | ||
|
Collaborator
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. why do we care if the AssignedPhysReg is passed?
Collaborator
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. Shouldn't the check be if a subregisterClass exists and if we fail to retrieve it we should go for largestlegalSuperclass? |
||
| ? TRI.getSubRegisterClass(SuperRC, SubReg) | ||
| : TRI.getLargestLegalSuperClass( | ||
|
Collaborator
Author
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. Why? |
||
| TRI.getSubRegisterClass(SuperRC, SubReg), MF); | ||
| SubRegToVReg[SubReg] = MRI.createVirtualRegister(SubRC); | ||
| } | ||
|
|
||
|
|
@@ -246,7 +251,6 @@ void rewriteSuperReg(Register Reg, Register AssignedPhysReg, | |
| LIS.removeInterval(Reg); | ||
|
|
||
| for (auto &[SubRegIdx, VReg] : SubRegToVReg) { | ||
| MCRegister SubPhysReg = TRI.getSubReg(AssignedPhysReg, SubRegIdx); | ||
| LiveInterval &SubRegLI = LIS.getInterval(VReg); | ||
| LLVM_DEBUG(dbgs() << " Assigning Range: " << SubRegLI << '\n'); | ||
|
|
||
|
|
@@ -257,10 +261,13 @@ void rewriteSuperReg(Register Reg, Register AssignedPhysReg, | |
| LIComponents.push_back(&SubRegLI); | ||
| VRM.grow(); | ||
|
|
||
| for (LiveInterval *LI : LIComponents) { | ||
| LRM.assign(*LI, SubPhysReg); | ||
| VRM.setRequiredPhys(LI->reg(), SubPhysReg); | ||
| LLVM_DEBUG(dbgs() << " Assigned " << printReg(LI->reg()) << "\n"); | ||
| if (AssignedPhysReg.has_value()) { | ||
|
Collaborator
Author
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. early return. |
||
| MCRegister SubPhysReg = TRI.getSubReg(*AssignedPhysReg, SubRegIdx); | ||
| for (LiveInterval *LI : LIComponents) { | ||
| LRM.assign(*LI, SubPhysReg); | ||
| VRM.setRequiredPhys(LI->reg(), SubPhysReg); | ||
| LLVM_DEBUG(dbgs() << " Assigned " << printReg(LI->reg()) << "\n"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| //===-- AIEUnallocatedSuperRegRewriter.cpp - Constrain tied sub-registers -===// | ||
| // | ||
| // This file is licensed 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 | ||
| // | ||
| // (c) Copyright 2025 Advanced Micro Devices, Inc. or its affiliates | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "AIEBaseInstrInfo.h" | ||
| #include "AIEBaseRegisterInfo.h" | ||
| #include "AIESuperRegUtils.h" | ||
|
|
||
| #include "llvm/ADT/MapVector.h" | ||
| #include "llvm/ADT/SmallSet.h" | ||
| #include "llvm/CodeGen/LiveDebugVariables.h" | ||
| #include "llvm/CodeGen/LiveIntervals.h" | ||
| #include "llvm/CodeGen/LiveRegMatrix.h" | ||
| #include "llvm/CodeGen/LiveStacks.h" | ||
| #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" | ||
| #include "llvm/CodeGen/MachineFunction.h" | ||
| #include "llvm/CodeGen/MachineFunctionPass.h" | ||
| #include "llvm/CodeGen/MachineInstr.h" | ||
| #include "llvm/CodeGen/MachineInstrBuilder.h" | ||
| #include "llvm/CodeGen/MachineOperand.h" | ||
| #include "llvm/CodeGen/MachineRegisterInfo.h" | ||
| #include "llvm/CodeGen/Passes.h" | ||
| #include "llvm/CodeGen/SlotIndexes.h" | ||
| #include "llvm/CodeGen/TargetInstrInfo.h" | ||
| #include "llvm/CodeGen/TargetSubtargetInfo.h" | ||
| #include "llvm/CodeGen/VirtRegMap.h" | ||
| #include "llvm/Support/Debug.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| #define DEBUG_TYPE "aie-ra-prepare" | ||
|
|
||
| namespace { | ||
|
|
||
| using RegRewriteInfo = std::vector<std::pair<Register, SmallSet<int, 8>>>; | ||
|
|
||
| /// Split large unallocated compound registers into multiple new smaller vregs | ||
| /// Than can be allocated to scalar registers. | ||
| class AIEUnallocatedSuperRegRewriter : public MachineFunctionPass { | ||
|
|
||
| public: | ||
| static char ID; | ||
| AIEUnallocatedSuperRegRewriter() : MachineFunctionPass(ID) {} | ||
|
|
||
| void getAnalysisUsage(AnalysisUsage &AU) const override { | ||
| AU.setPreservesCFG(); | ||
| AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>(); | ||
| AU.addRequired<VirtRegMapWrapperLegacy>(); | ||
| AU.addPreserved<VirtRegMapWrapperLegacy>(); | ||
| AU.addRequired<SlotIndexesWrapperPass>(); | ||
| AU.addPreserved<SlotIndexesWrapperPass>(); | ||
| AU.addRequired<LiveDebugVariablesWrapperLegacy>(); | ||
| AU.addPreserved<LiveDebugVariablesWrapperLegacy>(); | ||
| AU.addRequired<LiveStacksWrapperLegacy>(); | ||
| AU.addPreserved<LiveStacksWrapperLegacy>(); | ||
| AU.addRequired<LiveIntervalsWrapperPass>(); | ||
| AU.addPreserved<LiveIntervalsWrapperPass>(); | ||
| AU.addRequired<LiveRegMatrixWrapperLegacy>(); | ||
| AU.addPreserved<LiveRegMatrixWrapperLegacy>(); | ||
| MachineFunctionPass::getAnalysisUsage(AU); | ||
| } | ||
|
|
||
| bool runOnMachineFunction(MachineFunction &Fn) override; | ||
| }; | ||
|
|
||
| /// Identify unallocated virtual registers that can be split into subregisters. | ||
| /// Returns a list of candidate registers with their rewritable subregister | ||
| /// indices, excluding unused registers and those already assigned to physical | ||
| /// registers. | ||
| static RegRewriteInfo getRewriteCandidates(MachineRegisterInfo &MRI, | ||
| const AIEBaseRegisterInfo &TRI, | ||
| VirtRegMap &VRM) { | ||
| RegRewriteInfo RegistersToRewrite; | ||
| for (unsigned VRegIdx = 0, End = MRI.getNumVirtRegs(); VRegIdx != End; | ||
| ++VRegIdx) { | ||
| const Register Reg = Register::index2VirtReg(VRegIdx); | ||
|
|
||
| // Ignore un-used registers | ||
|
Collaborator
Author
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. Complete the comment. |
||
| if (MRI.reg_nodbg_empty(Reg) || VRM.hasPhys(Reg)) | ||
| continue; | ||
|
|
||
| SmallSet<int, 8> RewritableSubRegs = | ||
|
Collaborator
Author
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. const |
||
| AIESuperRegUtils::getRewritableSubRegs(Reg, MRI, TRI); | ||
|
|
||
| if (RewritableSubRegs.empty()) | ||
| continue; | ||
|
|
||
| LLVM_DEBUG(dbgs() << "Candidate " << printReg(Reg, &TRI, 0, &MRI) << ":" | ||
| << printRegClassOrBank(Reg, MRI, &TRI) << '\n'); | ||
|
|
||
| RegistersToRewrite.push_back({Reg, RewritableSubRegs}); | ||
| } | ||
|
|
||
| LLVM_DEBUG(dbgs() << "Found " << RegistersToRewrite.size() | ||
| << " candidate register(s) for rewriting\n"); | ||
|
|
||
| return RegistersToRewrite; | ||
| } | ||
|
|
||
| /// Split candidate registers into independent virtual registers for each | ||
| /// subregister. Each composite register is rewritten using its subregister | ||
| /// indices, with live intervals and debug information updated accordingly. | ||
| void rewriteCandidates(RegRewriteInfo &RegistersToRewrite, | ||
|
Collaborator
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. Are these the candidates to rewrite? |
||
| MachineRegisterInfo &MRI, const AIEBaseRegisterInfo &TRI, | ||
| VirtRegMap &VRM, LiveRegMatrix &LRM, LiveIntervals &LIS, | ||
| SlotIndexes &Indexes, LiveDebugVariables &DebugVars) { | ||
|
|
||
| LLVM_DEBUG(dbgs() << "Rewriting " << RegistersToRewrite.size() | ||
| << " candidate register(s)\n"); | ||
|
|
||
| for (auto [VReg, SubRegs] : RegistersToRewrite) { | ||
| LLVM_DEBUG(dbgs() << " Rewriting " << printReg(VReg, &TRI, 0, &MRI) | ||
| << " into " << SubRegs.size() << " subregister(s)\n"); | ||
| AIESuperRegUtils::rewriteSuperReg( | ||
| VReg, /*std::optional<Register> AssignedPhysReg = */ {}, SubRegs, MRI, | ||
|
Collaborator
Author
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. Local var for Assign... |
||
| TRI, VRM, LRM, LIS, Indexes, DebugVars); | ||
| } | ||
| } | ||
|
|
||
| bool AIEUnallocatedSuperRegRewriter::runOnMachineFunction(MachineFunction &MF) { | ||
| LLVM_DEBUG(llvm::dbgs() << "*** Splitting unallocated super-registers: " | ||
| << MF.getName() << " ***\n"); | ||
|
|
||
| MachineRegisterInfo &MRI = MF.getRegInfo(); | ||
| VirtRegMap &VRM = getAnalysis<VirtRegMapWrapperLegacy>().getVRM(); | ||
| LiveRegMatrix &LRM = getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM(); | ||
| LiveIntervals &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS(); | ||
| SlotIndexes &Indexes = getAnalysis<SlotIndexesWrapperPass>().getSI(); | ||
| LiveDebugVariables &DebugVars = | ||
| getAnalysis<LiveDebugVariablesWrapperLegacy>().getLDV(); | ||
| auto &TRI = | ||
| *static_cast<const AIEBaseRegisterInfo *>(MRI.getTargetRegisterInfo()); | ||
|
|
||
| LLVM_DEBUG(dbgs() << "Identifying rewrite candidates...\n"); | ||
| RegRewriteInfo RegistersToRewrite = getRewriteCandidates(MRI, TRI, VRM); | ||
|
|
||
| if (RegistersToRewrite.empty()) { | ||
| LLVM_DEBUG(dbgs() << "No candidates found, skipping rewrite\n"); | ||
| return false; | ||
| } | ||
|
|
||
| LLVM_DEBUG(dbgs() << "Performing register rewrites...\n"); | ||
| rewriteCandidates(RegistersToRewrite, MRI, TRI, VRM, LRM, LIS, Indexes, | ||
| DebugVars); | ||
|
|
||
| LLVM_DEBUG(dbgs() << "Successfully rewrote " << RegistersToRewrite.size() | ||
| << " register(s)\n"); | ||
|
|
||
| return !RegistersToRewrite.empty(); | ||
| } | ||
|
|
||
| } // end anonymous namespace | ||
|
|
||
| char AIEUnallocatedSuperRegRewriter::ID = 0; | ||
| char &llvm::AIEUnallocatedSuperRegRewriterID = | ||
| AIEUnallocatedSuperRegRewriter::ID; | ||
|
|
||
| INITIALIZE_PASS(AIEUnallocatedSuperRegRewriter, | ||
| "aie-unallocated-superreg-rewrite", | ||
| "AIE unallocated super-reg rewrite", false, false) | ||
|
|
||
| llvm::FunctionPass *llvm::createAIEUnallocatedSuperRegRewriter() { | ||
| return new AIEUnallocatedSuperRegRewriter(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: const