[llvm-dev] Understanding and Cleaning Up Machine Instruction Bundles

Matthias Braun via llvm-dev llvm-dev at lists.llvm.org
Thu Oct 27 19:04:50 PDT 2016


> On Oct 27, 2016, at 6:45 PM, Andrew Trick <atrick at apple.com> wrote:
> 
> 
>> On Oct 27, 2016, at 5:30 PM, Matthias Braun <mbraun at apple.com <mailto:mbraun at apple.com>> wrote:
>> 
>>> 
>>> On Oct 27, 2016, at 5:05 PM, Andrew Trick <atrick at apple.com <mailto:atrick at apple.com>> wrote:
>>> 
>>>> The system works because the default basic block iterator moves from bundle to
>>>> bundle skipping the instructions inside the bundle. Iterating over the operands
>>>> will only give us the operands of the BUNDLE instruction but that is fine,
>>>> because it basically has a copy of everything inside the bundle.
>>> 
>>> The BUNDLE instruction simply isn’t necessary to do anything you just described.
>> 
>> That may all be true. However I'd like to point out that this is the status quo! finalizeBundle() will give you the BUNDLE instruction in the header and it is used by everyone using bundles: ARM, and AMDGPU target and the DFAPacketizer (which is used by Hexagon).
>> 
>> Not using BUNDLE and correctly using MIBundleOperands at the right places in the register allocator is not where the code is today! I believe that we are far enough away from it that we should rather fix the status quo first to avoid all the confusion and then move forward to the header-less scheme in a targetted change. That is why I added the last paragraph in my mail.
> 
> I don’t understand. BUNDLE was never meant for vreg operands. Are you saying that regalloc breaks if you form preRA bundles (without “finalizing” them)?
Most of the regalloc code appears to be setup to deal with headerless bundles. Though even in that scheme you need some form of finalization (like marking some uses with the internal flag) so I couldn't really test it. The only target that I am aware of forming bundles pre-RA (which is an out of tree target) does not use this and finalizes the bundles immediately after creation and adds use/def operands to the header instruction.
In practice requiring the usage of MIBundleOperator pre-RA and allowing MachineInstr::operands() only post-RA is confusing. And looking around there is alot of code using MachineInstr::operands() even though it probably shouldn't then: Pretty much all of the register coalescer, LiveIntervals::HMEditor::findLastUseBefore(), ScheduleDAGMI construction, MachineScheduler only displacing single instructions instead of bundles (you can bundle immediately after the scheduler moved an instruction though). In practice I expect this hard to get consistently right especially when all existing targets finalize immediately.

The model without BUNDLE instruction is indeed the better model. I'd also like us to get there long term. However I don't think I can motivate spending my time on that just to fix macroop fusion. I spend hours just understanding what is happening, because the current code finalizes always and at the same time uses MIBundleOperands (at least in some places). So what I wanted to do here is declaring the finalizing as standard and making the status quo less confusing than having a half-finished future design fighting actual usage leading to confusion.

- Matthias
> 
> Incidentally, it’s fine to temporarily bundle, e.g. during scheduling, erase bundles afterward, and potentially rebundle again later. That works great if you don’t have to keep materializing BUNDLE instructions.
> 
>>>> == Too many different iterators ==
>>>> 
>>>> Another source of confusion even for experience register allocation developers
>>>> is that we have 3 kinds of iterators for MachineOperands:
>>>> 
>>>> - There is MachineInstr::iterator which is used by the majority of passes and
>>>>   gives you the operands of a single instruction.
>>>> - There is (Const)MIOperands which appears to be equivalent to
>>>>   MachineInstr::iterator. I think we do not need a 2nd iterator and should get
>>>>   rid of this one (the only real reason to use it today is
>>>>   analyze{Virt|Phys}Reg() but that can be changed).
>>>> - There is (Const)MIBundleOperands which iterates all machine operands of all
>>>>   instructions inside a bundle.
>>> 
>>> A pass needs to know whether it’s cares about bundles or instructions.
>>> I don’t understand how adding an extra BUNDLE instruction does anything to solve this problem or make the MIR more robust. 
>>> 
>>> A pass that cares about liveness, dependencies, instruction insertion or reordering needs to work on bundles.
>>> Machine-independent passes should probably work on bundles.
>>> 
>>> By default, passes now use the bundle iterator for instructions and non-bundle iterator for operands. That allows passes to limp along in the presence of bundles without actually handling the bundles. I think the bundles will just silently defeat optimizations. It’s not safe, but it’s not too badly broken either.
>>> 
>>> The MIBundleOperands iterator simply makes more sense to me than the BUNDLE instruction. It seems straightforward to migrate passes to the new iterator, but it’s a lot of places that need updating.
>> I am convinced that as soon as we decide for a scheme with or without BUNDLE instruction we should remove all but one iterator (or at least write a long comment on the other iterator why you should not use it in most situation). Whatever the result it should use a C++ style iterator so at the very least MIBundleOperators needs to be rewritten for that.
>> 
>>> 
>>>> The last one appears to be necessary in a world without the initial BUNDLE
>>>> instruction repeating all the operands inside the bundle. In a setting where
>>>> finalization happens as a separate pass at the end of register allocation this
>>>> would be necessary for earlier register allocation passes.
>>>> 
>>>> However given that delaying finalization to a pass appears broken/unused it
>>>> seems we could just as well use MachineInstr::iterator instead and remove
>>>> MIBundleOperands. Any objections?
>>> 
>>> IIUC, live intervals, the register allocator, and the scheduler already handle bundles.
>>> 
>>> I’m fairly sure that adding new vreg uses is not what we want to do.
>> The code looks like it can handle it, but as I said above it is not exercised by any of the existing targets and I can show you some places where uses of MachineInstr::iterator sneaked in even in the regalloc passes which would be invalid in the BUNDLE-header-less scheme.
>> 
>>> 
>>>> == Moving to a scheme without repeating the operands in the bundle header ==
>>>> 
>>>> I've heard some comments that the long term plan was to move to a scheme where
>>>> the operands inside the bundle are not repeated in a bundle header and instead
>>>> everyone uses an iterator like MIBundleOperands. I could not find any mails
>>>> documenting this, so it would be nice if some people could chime in here if
>>>> that was indeed the plan.
>>>> 
>>>> Even with this long term plan in mind I would suggest to remove
>>>> MIBundleOperands. If we implement this plan we should rather change
>>>> MachineInstr::iterator later instead of being in the confusin in-between state
>>>> that we have today.
>>>> 
>>>> - Matthias
>>> 
>>> I’m not sure what you mean by changing MachineInstr::iterator. You mean mop_iterator?
>> Oh sorry, I was talking about mop_iterator indeed. The thing you get when you use
>>     `for (MachineOperand &MO : someinstruction.operands()) { ... }` which is the standard for the majority of codegen passes today.
>> 
>>> 
>>> You can’t replace an instr iterator with a bundle iterator without breaking some basic invariants:
>>> MI == MI->operands_begin()->getParent()
>>> 
>>> That’s why passes should explicitly ask for the bundle operands.
>> If we move to a BUNDLE-less world then the majority of passes will need something like MIBundleOperands, in that case we really should replace MachineInstr::iterator anyway, make the typical use the most convenient one and adapt the passes to not expect those invariants. But again I consider this a change of the status quo, not something we already do or just need to fix in a handful of places.
>> 
>> - Matthias
> 
> It seems to me that machine-specific passes still want to iterate over per-instruction operands in a lot of cases.
> 
> Of course, I haven’t looked at any of this code in a very long time. I can only explain the intent and you’ll have to use your judgement about migrating things.
> 
> -Andy
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161027/d418312d/attachment.html>


More information about the llvm-dev mailing list