[llvm] [RegAllocFast] fold foldable inline asm (PR #74344)

Nick Desaulniers via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 4 12:46:23 PST 2023


nickdesaulniers wrote:

> Yes I was referring to the loop prefixed with // Allocate virtreg uses and insert reloads as necessary.

Ah, yeah so this was the original tact I pursued.  Not necessarily the `assignInlineAsmUses` 2 pass part, but instead I basically had the check:


```
// Allocate virtreg uses and insert reloads as necessary.
...
if (MI.isInlineAsm() && MO.isFoldable()) {
   spillInlineAsmOperand()
} else {  // existing code:
   ...
   useVirtReg()
   ...
```
(basically, avoid creating the virtreg in the first place). (this was alluded to in my added comment mentioning useVirtReg)

> But going that road seems to make more sense than to pessimistically always spill/pick "m" in the fastregalloc...

Note: the road you suggest also pessimistically always spill/pick "m", too.  It just does so in one pass over every MachineInst in the MachineBasic block, rather than 2.

I think I remember getting this working for inputs, or outputs, but not for an inline asm with BOTH an input and output.  I'd have to do extensive archaeology in my `git reflog` to go back to what the previous attempt I was working on was doing, but...

> looking around there would probably need to be some additional changes as RegAllocFast::useVirtReg assumes that a register is necessary for every "use" in an instruction, while for foldable ones that is actually not strictly required.

that was a massive pain point.  It wasn't clean for me to be able to say "nevermind this virtreg, I've made it unnecessary by transforming the MachineInstr itself (or replacing it)."

Another issue I ran into was that `TargetInstructionInfo::foldMemoryOperand` doesn't modify the MachineInstr; it returns new MachineInstr that you're expected to insert.  I was having a hard time in the existing implementation of `RegAllocFast` wrt iterator invalidation (of the MachineIntrs and MachineOperands).

https://github.com/llvm/llvm-project/pull/74344


More information about the llvm-commits mailing list