-
Notifications
You must be signed in to change notification settings - Fork 30
[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?
Conversation
|
Thanks you @andcarminati, very much !! |
|
Also do you think following commit will help ? |
Maybe yes! As mentioned before, I prefer to keep just the minimal necessary changes. We can test after, on top of this PR. |
31b7e71 to
a27561f
Compare
a27561f to
e124649
Compare
80e6f7c to
4c47705
Compare
| MI.eraseFromParent(); | ||
| // As we don't handle all registers now (selective LI filter), | ||
| // We should make sure that all LiveIntervals are correct. | ||
| // If we dont't repair, MI will compose the LIs of some registers, |
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: don't
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.
What do you mean with compose? the dead MI will block some LI ?
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.
A dead MI will have an invalid SlotIndex that will appear in some instruction`s LI, what is wrong. The original copy should not appear in any LI.
| } | ||
| Register SrcReg = RegOp.getParent()->getOperand(1).getReg(); | ||
| if (!VisitedVRegs.count(SrcReg) && | ||
| getRewritableSubRegs(SrcReg, MRI, TRI, VisitedVRegs).empty()) { |
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.
Does it ever happen, that SrcReg has no SubRegs, but DstReg has them or vis versa?
Can they also have different SubRegs?
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.
This part was a refactor, but as we are handling a full copy here, we can expect subregs on both sides.
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.
As a full copy, I mean a 2d to 2d copy or a 3d to 3d copy.
a88a541 to
ab8c08d
Compare
|
|
||
| MachineInstr *FirstMI = nullptr; | ||
| SmallSet<Register, 8> RegistersToRepair; | ||
| bool IsFirstCopy = true; |
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: We can use FirstMI as a replacement for IsFirstCopy.
| @@ -0,0 +1,172 @@ | |||
| //===----- AIEUnallocatedSuperRegRewriter.cpp - Constrain tied sub-registers | |||
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.
Fix this.
| # RUN: llc -O2 -mtriple=aie2p -start-before=greedy \ | ||
| # RUN: -stop-before=aie-unallocated-superreg-rewrite %s -o - | FileCheck %s | ||
|
|
||
| # The goal of this test is to check if we properly insert undef flag on the def side |
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.
I could extend the verifier to cover partial definitions. It is far away from be trivial, but it could be done.
| static bool isRegUsedBy2DOr3DInstruction(const MachineRegisterInfo &MRI, | ||
| const Register &R) { | ||
|
|
||
| return std::any_of( |
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.
llvm::any_of on the range?
|
|
||
| for (MachineInstr &MI : MRI.def_instructions(VReg)) { | ||
|
|
||
| if (!MI.isBundledWithPred() || MI.isBundledWithSucc()) |
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.
Can I have a comment explaining this directional asymmetry?
731a410 to
eeedaa7
Compare
|
There is no failures across all benchmarks. |
Now we filter by register class and usage. Basically, we exclude here instructions like copies and non-2D/3D ones. Co-Authored-By: Krishnam Tibrewala <[email protected]>
…gisters Co-Authored-By: Krishnam Tibrewala <[email protected]>
Co-Authored-By: Krishnam Tibrewala <[email protected]>
The goal of this test is to check if we properly insert undef flag on the def side of a expanded full copy. On a sub-register def operand, it refers to the part of the register that isn't written. A sub-register def implicitly reads the other parts of the register being redefined unless the <undef> flag is set, and a missing flag can force the related register to be inserted in liveout set of the predecessors block, causing dominance problems. Co-Authored-By: Krishnam Tibrewala <[email protected]>
This will handle properly use of non-dominating definitions. We also change the handling of the destination registers in two parts: *Copy expansion: we replace the ogininal index by the index of the first lane copy to avoid the creation LRs with just one instruction, in this way we keep que LI correct. *Rewrite: reset dead flags if necessary. Co-Authored-By: Krishnam Tibrewala <[email protected]>
…reedy run Co-Authored-By: Krishnam Tibrewala <[email protected]>
If we don't need a full register, we can expand to individual lanes. Co-Authored-By: Krishnam Tibrewala <[email protected]>
Co-Authored-By: Krishnam Tibrewala <[email protected]>
This avoids cycles in bundles that appear in VirtRegRewriter. We also update LIs related to src and dst operands of those expanded copies. Co-Authored-By: Krishnam Tibrewala <[email protected]>
eeedaa7 to
925f9d9
Compare
|
|
||
| MachineInstr *PartCopy = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), | ||
| TII.get(TargetOpcode::COPY)) | ||
| .addReg(DstReg, RegState::Define, SubRegIdx) |
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: we do not need undefs here because we fully define the subregisters that we copy to?
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.
Yes, we start the definition on the first copy, and we consider that next copies will read the previous one. In practice, inserting undefs in all copies will not hurt in general, but as is, we have a more accurate model for incremental definitions.
|
|
||
| // Only set undefined on the first partial copy. The first copy doesn't read | ||
| // other lanes, but subsequent copies do read the previously written lanes. | ||
| // Setting undefined on all copies breaks live interval tracking and causes |
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.
oh yeah i also found that out with subreg spilling XD
|
|
||
| // Replace the original copy by the first one, so we automatically repair | ||
| // DstReg's LI. | ||
| LIS.ReplaceMachineInstrInMaps(MI, *FirstMI); |
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.
why aren't we inserting all the newly created machineinstructions into LIS?
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.
Because, later on, each new lane copy will make dead the previous one if we try to fix the LI. In this case, we say that the LR will start on the first copy and then it will simply continue though the next ones, until the next last use. I this way we prevent false dead flags everywhere.
| } | ||
|
|
||
| /// Rewrite a full copy into multiple copies using the subregs in \p CopySubRegs | ||
| void rewriteFullCopy(MachineInstr &MI, const std::set<int> &CopySubRegs, |
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.
MI is a copy, right? Could you rename MI to CopyMI, so it becomes clearer?
| for (MachineInstr &MI : make_early_inc_range(MRI.reg_instructions(Reg))) { | ||
| if (MI.isFullCopy()) | ||
| AIESuperRegUtils::rewriteFullCopy( | ||
| MI, TRI.getSubRegSplit(MRI.getRegClass(Reg)->getID()), LIS, *TII, TRI, |
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.
you are also passing TRI, I think you don't need to pass CopySubRegs here
You can get MRI like this:
MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
| VisitedVRegs.insert(Reg); | ||
| SmallSet<int, 8> UsedSubRegs; | ||
| for (MachineOperand &RegOp : MRI.reg_operands(Reg)) { | ||
| int SubReg = RegOp.getSubReg(); |
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
| SmallSet<int, 8> UsedSubRegs; | ||
| for (MachineOperand &RegOp : MRI.reg_operands(Reg)) { | ||
| int SubReg = RegOp.getSubReg(); | ||
| if (SubReg && SubRegSplit.count(SubReg)) { |
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.
if we are a subreg, shouldn't we automatically be in the SubRegSplit?
I think we can assert it instead of checking it in the condition.
|
|
||
| VisitedVRegs.insert(Reg); | ||
| SmallSet<int, 8> UsedSubRegs; | ||
| for (MachineOperand &RegOp : MRI.reg_operands(Reg)) { |
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.
Maybe add a comment that we are walking all defs and uses to find subreg uses and full copies.
| int SubReg = RegOp.getSubReg(); | ||
| if (SubReg && SubRegSplit.count(SubReg)) { | ||
| UsedSubRegs.insert(SubReg); | ||
| } else if (RegOp.getParent()->isFullCopy()) { |
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.
Why are we performing this check on uses?
Isn't this a check we can perform once on the DefI?
| } | ||
| UsedSubRegs.insert(SubRegSplit.begin(), SubRegSplit.end()); | ||
| } else { | ||
| LLVM_DEBUG(dbgs() << " Cannot rewrite " << RegOp << " in " |
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.
Is this a regular use/Def of the full register?



This work is intended to avoid 2D/3D (when possible) register spills.
The idea and rationale behind this work is in a previous Draft PR: #442.
To review, I recommend to follow this PR commit by commit.
Credits also for the co-author @krishnamtibrewala.