[RISCV] Missed macrofusion due to bad register allocation
LUI+ADDI macrofusion depends on the LUI instruction and the ADDI instruction writing to the same register. But register allocation may not guarantee this. The LUI may get assigned a register from allocation order while the ADDI gets assigned a register based on a constraint from a later instruction. Or the ADDI could have to pick a register to avoid the LUI register being clobbered before the ADDI result is used.
Here's a simple example where fusion fails https://godbolt.org/z/5bxz5W73T
cc: @preames @wangpc-pp
A few possible solutions
-Macrofuse pre-regalloc into new pseudoinstructions that represent the fused operation. Expand post regalloc and post scheduling. We could fuse either in isel or a new pass. -Add a new pass to backwards propagate the destination register to the first instruction of the fusion pattern after regalloc. -Somehow improve regalloc.
cc @mgudim
@llvm/issue-subscribers-backend-risc-v
Author: Craig Topper (topperc)
Here's a simple example where fusion fails https://godbolt.org/z/5bxz5W73T
cc: @preames @wangpc-pp
If LUI+ADDI come from generation of constants / globals mostly can we try this:
we create a (CodeGenOnly) instruction which is same as ADDI, but its operands are tied. We can use that constrained version of ADD in lowering of globals / constants.
If LUI+ADDI come from generation of constants / globals mostly can we try this:
we create a (CodeGenOnly) instruction which is same as ADDI, but its operands are tied. We can use that constrained version of ADD in lowering of globals / constants.
That would probably work.
The bigger thing I wanted to highlight is that this same problem exists for any of the macrofusions we've defined so far.
this same problem exists for any of the macrofusions we've defined so far.
We could define corresponding constrained instructions and do matching in ISel patterns?
this same problem exists for any of the macrofusions we've defined so far.
We could define corresponding constrained instructions and do matching in ISel patterns?
That would be too much efforts I think, we need a way that's not so intrusive.
A few possible solutions
-Macrofuse pre-regalloc into new pseudoinstructions that represent the fused operation. Expand post regalloc and post scheduling. We could fuse either in isel or a new pass. -Add a new pass to backwards propagate the destination register to the first instruction of the fusion pattern after regalloc. -Somehow improve regalloc.
I'm interested in the third, is it possible to attach some hints to fusible pairs so that the reg allocator can do the favor?
For example:
- Add a MI flag to indicate the constraint.
- Set this flag to true for fusible pairs in pre-regalloc macrofusion.
- During regalloc, the allocator will choose the same register according to the flag.
From briefly looking at the code, it seems like we can return new register allocation hints in the begining of each selectOrSplitImpl. The hints can be updated based on what has been assigned so far. We are adding the "two address hints" already to improve the chances to use compressed instructions. Will this work?
From briefly looking at the code, it seems like we can return new register allocation hints in the begining of each
selectOrSplitImpl. The hints can be updated based on what has been assigned so far. We are adding the "two address hints" already to improve the chances to use compressed instructions. Will this work?
The addi in my case should have already had the 2 address hint and it didn't work.
Here's the debug output from register allocator:
# Machine code for function foo: NoPHIs, TracksLiveness, TiedOpsRewritten, TracksDebugUserValues
Function Live Ins: $x11 in %1
0B bb.0.entry:
liveins: $x11
32B %2:gpr = LUI 3014
48B %3:gpr = ADDIW %2:gpr, 334
64B %1:gpr = COPY $x11
80B $x10 = COPY %1:gpr
96B $x11 = COPY %3:gpr
112B PseudoTAIL target-flags(riscv-plt) @bar, implicit $x2, implicit killed $x10, implicit $x11
# End machine code for function foo.
Enqueuing %1
AllocationOrder(GPR) = [ $x10 $x11 $x12 $x13 $x14 $x15 $x16 $x17 $x5 $x6 $x7 $x28 $x29 $x30 $x31 $x8 $x9 $x18 $x19 $x20 $x21 $x22 $x23 $x24 $x25 $x26 $x27 $x1 ]
Enqueuing %2
Enqueuing %3
selectOrSplit GPR:%3 [48r,96r:0) 0@48r weight:4.508928e-03 w=4.508928e-03
hints: $x11
missed hint $x11
Analyze counted 2 instrs in 1 blocks, through 0 blocks.
$x11 no positive bundles
assigning %3 to $x12: X12 [48r,96r:0) 0@48r
selectOrSplit GPR:%1 [64r,80r:0) 0@64r weight:INF w=INF
hints: $x10 $x11
assigning %1 to $x10: X10 [64r,80r:0) 0@64r
selectOrSplit GPR:%2 [32r,48r:0) 0@32r weight:INF w=INF
assigning %2 to $x10: X10 [32r,48r:0) 0@32r
Trying to reconcile hints for: %3($x12)
%3($x12) is recolorable.
********** REWRITE VIRTUAL REGISTERS **********
********** Function: foo
********** REGISTER MAP **********
[%1 -> $x10] GPR
[%2 -> $x10] GPR
[%3 -> $x12] GPR
If it assigned %2 to $x12 we would be OK.
Now I actually think that fixing this in register allocator is a better option. Using fake two-address instructions seems not flexible enough because we don't have the opportunity to break the constrained if really needed.
Now I actually think that fixing this in register allocator is a better option. Using fake two-address instructions seems not flexible enough because we don't have the opportunity to break the constrained if really needed.
We could add isConvertibleToThreeAddress to break the constraint. But I still don't think its a good idea. You'd have to identify the macrofusion somewhere to create the two-address instruction. And if you're going to do that, I'm not sure why you wouldn't just have a pseudoinstruction for the whole macrofusion pattern. Otherwise you're identifying the pattern to create the two address instruction, then identifying it again in the scheduler to keep it together.
The addi in my case should have already had the 2 address hint and it didn't work.
I was wrong, we don't have the hint because the immediate isn't simm<6> for c.addi. If I change the immediate to satisfy simm<6> for the addi, the hint works.
@topperc So do you think modifying the getRegisterAllocationHint so that it covers macro-fusions is a good solution? I can work on that in near future.
A few possible solutions -Macrofuse pre-regalloc into new pseudoinstructions that represent the fused operation. Expand post regalloc and post scheduling. We could fuse either in isel or a new pass. -Add a new pass to backwards propagate the destination register to the first instruction of the fusion pattern after regalloc. -Somehow improve regalloc.
I'm interested in the third, is it possible to attach some hints to fusible pairs so that the reg allocator can do the favor?
For example:
1. Add a MI flag to indicate the constraint. 2. Set this flag to true for fusible pairs in pre-regalloc macrofusion. 3. During regalloc, the allocator will choose the same register according to the flag.
I posted an implementation of this method: https://github.com/llvm/llvm-project/pull/77461.