llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Reg alloc reloads register three times in innermost vector loop, although there are many free regs

Open JonPsson opened this issue 9 years ago • 36 comments

Bugzilla Link 32722
Version trunk
OS Linux
Attachments reduced test case
CC @MatzeB,@JonPsson,@qcolombet,@uweigand

Extended Description

I noticed that there are reloads of a register which is used as an address inside a vectorized loop.

The loop is innermost, is a single block, and there are 10 unused registers in the loop.

I am thinking that RA should be able to split live ranges around the loop?

bin/llc -O3 -mtriple=s390x-linux-gnu -mcpu=z13 -o out.s vecloopspills.ll

.LBB0_18: # %vector.body # =>This Inner Loop Header: Depth=1 lgfr %r5, %r3 sllg %r5, %r5, 3 vlrepg %v0, 0(%r5,%r14) ahik %r14, %r3, -2 lg %r13, 200(%r15) # 8-byte Folded Reload lgfr %r14, %r14 sllg %r14, %r14, 3 vlrepg %v1, 0(%r14,%r13) vmrhg %v0, %v1, %v0 lg %r13, 200(%r15) # 8-byte Folded Reload vflcdb %v0, %v0 vsteg %v0, 0(%r14,%r13), 0 lg %r14, 200(%r15) # 8-byte Folded Reload ahi %r3, 4 aghi %r4, -2 vsteg %v0, 0(%r5,%r14), 1 jne .LBB0_18

JonPsson avatar Apr 20 '17 16:04 JonPsson

Now down to ~ 20 cases left.

bin/llc -mtriple=s390x-linux-gnu -mcpu=z13 tc_badloopreg_18.ll -tail-dup-placement=false

Two reloads in loop, even though R0D, R1D and R6D are available.

JonPsson avatar Nov 21 '17 15:11 JonPsson

reduced testcase

JonPsson avatar Nov 21 '17 15:11 JonPsson

reduced testcase We are now down to ~ 50 known cases on SystemZ / SPEC.

Next test case does spilling three times in loop while there are many free regs available.

Loop of .LBB0_7: 3 spills, yet many regs unused: %R1D, %R2D, %R3D, %R4D, %R5D, %R14D, %R12D, %R11D, %R10D, %R9D, %R8D.

/bin/llc -mtriple=s390x-linux-gnu -mcpu=z13 tc_badloopreg_17.ll -tail-dup-placement=false

Note this may depend on a patch of LiveIntervals.cpp:

Index: lib/CodeGen/LiveInterval.cpp

--- lib/CodeGen/LiveInterval.cpp (revision 316856) +++ lib/CodeGen/LiveInterval.cpp (working copy) @@ -468,8 +468,9 @@ bool LiveRange::overlaps(const LiveRange // I and J are overlapping. Find the later start. SlotIndex Def = std::max(I->start, J->start); // Allow the overlap if Def is a coalescable copy.

  •  if (Def.isBlock() ||
    
  •      !CP.isCoalescable(Indexes.getInstructionFromIndex(Def)))
    
  •  if ((Def.isBlock() ||
    
  •       !CP.isCoalescable(Indexes.getInstructionFromIndex(Def))) &&
    
  •      J->end > I->start)
       return true;
    
    } // Advance the iterator that ends first to check for more overlaps.

JonPsson avatar Nov 14 '17 09:11 JonPsson

reduced testcase Found a case of an inner loop (depth 2) which is reloading a function argument into %r1. This is unnecessary, since %r6-%r8 are unused:

.LBB1_6: # %while.body40 # Parent Loop BB1_2 Depth=1 # => This Inner Loop Header: Depth=2 lg %r1, 160(%r15) # 8-byte Folded Reload lgr %r2, %r12 lgr %r3, %r10 lgr %r4, %r11 basr %r14, %r1 la %r10, 0(%r9,%r10) sgr %r13, %r9 clgrjhe %r13, %r9, .LBB1_6 j .LBB1_1

bin/llc -mtriple=s390x-linux-gnu -mcpu=z13 -o out.s ./tc_badloopreg_12.ll -tail-dup-placement=false

JonPsson avatar Oct 24 '17 15:10 JonPsson

reduced testcase which crashes I found that tc_badloopreg_11.ll (latest test case) indeed got remedied by enabling subreg liveness.

I then wanted to see how much of an effect that would have on the remaining 317 loops I see with unecessary spilling / reloading inside loops.

Unfortunately, I however then ran into an assert with your patch. To reproduce, first apply enableSubRegLiveness.patch (always return true in MRI / subRegLivenessEnabled())

then run

bin/llc -mtriple=s390x-linux-gnu -mcpu=z13 tc_regsplit_crash.ll

lc: /home/ijonpan/llvm/llvm-dev-2/include/llvm/ADT/IntervalMap.h:635: unsigned int llvm::IntervalMapImpl::LeafNode<Key T, ValT, N, Traits>::insertFrom(unsigned int&, unsigned int, KeyT, KeyT, ValT) [with KeyT = llvm::SlotIndex; ValT = llv m::LiveInterval*; unsigned int N = 8u; Traits = llvm::IntervalMapInfollvm::SlotIndex]: Assertion `(i == Size || Trait s::stopLess(b, start(i))) && "Overlapping insert"' failed. #​0 0x0000000002b812a0 llvm::sys::PrintStackTrace(llvm::raw_ostream&) (bin/llc+0x2b812a0) #​1 0x0000000002b7f23e llvm::sys::RunSignalHandlers() (bin/llc+0x2b7f23e) #​2 0x0000000002b7f3b0 SignalHandler(int) (bin/llc+0x2b7f3b0) #​3 0x0000000006e46c8e #​4 0x000003ff9123d4f8 __GI_raise (/lib64/libc.so.6+0x3d4f8) #​5 0x000003ff9123edee __GI_abort (/lib64/libc.so.6+0x3edee) #​6 0x000003ff912351ac __assert_fail_base (/lib64/libc.so.6+0x351ac) #​7 0x000003ff91235254 (/lib64/libc.so.6+0x35254) #​8 0x00000000024afadc llvm::IntervalMapImpl::LeafNode<llvm::SlotIndex, llvm::LiveInterval*, 8u, llvm::IntervalMapInfollvm::SlotIndex >::insertFrom(unsigned int&, unsigned int, llvm::SlotIndex, llvm::SlotIndex, llvm::LiveInterval*) (bin/llc+0x24afadc) #​9 0x00000000024b9228 llvm::LiveIntervalUnion::unify(llvm::LiveInterval&, llvm::LiveRange const&) (bin/llc+0x24b9228) #​10 0x00000000024beefc llvm::LiveRegMatrix::assign(llvm::LiveInterval&, unsigned int) (bin/llc+0x24beefc) #​11 0x000000000237e206 (anonymous namespace)::RAGreedy::trySplitLoopInAll(llvm::LiveInt ...

JonPsson avatar Oct 03 '17 11:10 JonPsson

reduced testcase with reload in loop New test-case with an unnecessary reload inside loop.

%vreg6 is the function argument, which is stored unmodified in the loop in each iteration. Regalloc decides to spill this, and then reloads it every time. Yet, %R12D is unused in the loop.

I think this is due to the fact that %R12Q (%R12D + %R13D) is defined by a division (which is always done with a 128bit register). Then, in the loop only %R13D is used. Would it somehow be reasonable to fix this so that "if only one subreg is used inside loop, then split the wide interval around loop and make a new interval for the subreg for use by the loop"? This would make one more register free in the loop.

I am not sure what is the real problem / best handling... Could it be that instead of coalescing the GR128 (creating the long GR128 interval with the subreg use), it would be better to keep the COPY and then give a regalloc hint for the subreg? Somehow it seems suboptimal to have this long GR128 live range regardless if it's inside a loop or not...

bin/llc -mtriple=s390x-linux-gnu -mcpu=z13 tc_badloopreg_11.ll -tail-dup-placement=false

JonPsson avatar Oct 03 '17 11:10 JonPsson

reduced testcase bin/llc -mtriple=s390x-linux-gnu -mcpu=z13 -o out.s tc_badloopreg_10.ll

A single block loop (%vector.body) spills and reloads, yet %R2D seems unused in the block.

JonPsson avatar Sep 27 '17 13:09 JonPsson

New test cases:

tc_badreload.ll:

Tiny loop with a reload from loop-invariant stack-slot:

.LBB0_1: # %for.end8280 # =>This Inner Loop Header: Depth=1 ar %r4, %r5 ahi %r3, 1 lt %r0, 164(%r15) # 4-byte Folded Reload lgfr %r4, %r4 jle .LBB0_1

tc_badspill_reload.ll: A small inner loop with disjoint blocks: { BB0_6, BB0_3 } spills and reloads, even though %R1D, %R2D, %R5D, %R14D, %R13D, %R11D, %R10D, %R9D, %R8D, %R6D seem unused.

JonPsson avatar Sep 15 '17 13:09 JonPsson

reduced testcase with reload in loop Thanks for help on number 5. I have now fixed the callee saved regs list (only on my experimental branch so far) to include the GR128 and FP128 registers.

Disabling the hot-loops heuristic (to get maximal splitting), I now have ~200 cases left (was ~2500).

Next test case:

A simple increment (with no particular use in the reduced function) in the loop

%vreg4 = LA %vreg4, 1, %noreg; ADDR64Bit:%vreg4

becomes

AGSI <fi#0>, 0, 1, %CC<imp-def,dead>; mem:LDST8[FixedStack0]

which is a adding the constant to memory (fi#0).

R10D is unused in the loop, but live-through the entire loop, so it seems like R10D could have been split around the loop.

bin/llc -O3 -mtriple=s390x-linux-gnu -mcpu=z13 ./tc_reloadinloop_6.ll

JonPsson avatar Aug 10 '17 12:08 JonPsson

reduced testcase Next test case with two reloads in loop:

.LBB0_4: # %cond.end171 # =>This Inner Loop Header: Depth=1 chi %r5, 0 lgr %r11, %r0 locghie %r11, 0 lg %r11, -8(%r4,%r11) lg %r13, 200(%r15) # 8-byte Folded Reload clg %r11, -8(%r4,%r13) jle .LBB0_22

BB#5: # %if.end178

                                    #   in Loop: Header=BB0_4 Depth=1
    clgijl  %r11, 0, .LBB0_20

BB#6: # %if.end178.1

                                    #   in Loop: Header=BB0_4 Depth=1
    lg      %r11, 0(%r4,%r2)
    lg      %r13, 200(%r15)         # 8-byte Folded Reload
    ag      %r11, -16(%r4,%r13)
    clgrjh  %r11, %r0, .LBB0_20

BB#7: # %for.cond150.1

                                    #   in Loop: Header=BB0_4 Depth=1
    aghi    %r14, -2
    ahi     %r5, 2
    aghi    %r4, -16
    aghi    %r3, -2
    cgijh   %r3, 2, .LBB0_4

Unused regs: %r1, %r12, %r10, %r9, %r8, %r7, %r6

JonPsson avatar Jun 05 '17 06:06 JonPsson

Comment to last test case (3rd June):

.LBB0_4: # %for.body46 # Parent Loop BB0_3 Depth=1 # => This Inner Loop Header: Depth=2 lg %r0, 192(%r15) # 16-byte Folded Reload lg %r1, 200(%r15) # 16-byte Folded Reload lgr %r4, %r1 stg %r2, 176(%r15) # 16-byte Folded Spill stg %r3, 184(%r15) # 16-byte Folded Spill brasl %r14, memcpy@PLT lg %r0, 192(%r15) # 16-byte Folded Reload lg %r1, 200(%r15) # 16-byte Folded Reload lgr %r4, %r1 brasl %r14, memcpy@PLT lg %r0, 192(%r15) # 16-byte Folded Reload lg %r1, 200(%r15) # 16-byte Folded Reload lgr %r4, %r1 brasl %r14, memcpy@PLT lg %r0, 192(%r15) # 16-byte Folded Reload lg %r1, 200(%r15) # 16-byte Folded Reload lgr %r4, %r1 brasl %r14, memcpy@PLT lg %r2, 176(%r15) # 16-byte Folded Reload lg %r3, 184(%r15) # 16-byte Folded Reload lg %r0, 192(%r15) # 16-byte Folded Reload lg %r1, 200(%r15) # 16-byte Folded Reload aghi %r3, -4 stg %r1, 0(%r1) jne .LBB0_4

call clobbered: %r0-%r5 free: %r13, %r12, %r11, %r10L, %r9, %r8, %r7, %r6

JonPsson avatar Jun 05 '17 06:06 JonPsson

reduced testcase Thank you Wei. I am now down to 850. Attaching next test case for you.

JonPsson avatar Jun 03 '17 08:06 JonPsson

Another patch to handle all the three testcases. It is the logic to handle critical edges exiting loop which needs a small improvement.

llvmbot avatar Jun 01 '17 22:06 llvmbot

Currently, on SPEC-2006/SystemZ, total number (regardless of loops) of spill /reload instructions go up with ~1%.

Total number of spill / reload instruction in innermost loops however go down with ~17% :-)

JonPsson avatar May 29 '17 07:05 JonPsson

Thanks - with the second version some additional ~50 loops got handled, so there are still ~1000 loops to go...

JonPsson avatar May 27 '17 07:05 JonPsson