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

[ppc] LLVM generates an extra loop

Open weiguozhi opened this issue 10 years ago • 22 comments

Bugzilla Link 30342
Version trunk
OS Linux
CC @hfinkel,@nemanjai

Extended Description

Compile the following code with options

--target=powerpc64le-grtev4-linux-gnu -m64 -O2 -mvsx -mcpu=power8
typedef unsigned short T;

int foo(T* array_a, T* array_b, int size_a, int size_b, T* buf) {
    int i_a = 0, i_b = 0, idx = 0;
    while (i_a < size_a && i_b < size_b) {
      if (array_a[i_a] == array_b[i_b]) {
        buf[idx++] = array_a[i_a];
        i_a++;
        i_b++;
      } else if (array_a[i_a] > array_b[i_b]) {
        i_b++;
      } else {
        i_a++;
      }
    }
    return idx;
}

Trunk llvm generates:

  9 # BB#0:                                 # %entry
 10         li 12, 0
 11         cmpwi    5, 0
 12         std 28, -32(1)                  # 8-byte Folded Spill
 13         std 29, -24(1)                  # 8-byte Folded Spill
 14         std 30, -16(1)                  # 8-byte Folded Spill
 15         cmpwi 1, 6, 0
 16         crand 20, 1, 5
 17         bc 4, 20, .LBB0_10
 18 # BB#1:                                 # %while.body.lr.ph.lr.ph.preheader
 19         extsw 8, 6
 20         extsw 9, 5
 21         addi 4, 4, -2
 22         li 11, 0
 23         li 10, 0
 24         li 30, 0
 25 .LBB0_2:                                # %while.body.lr.ph.lr.ph
 26                                         # =>This Loop Header: Depth=1
 27                                         #     Child Loop BB0_3 Depth 2
 28                                         #       Child Loop BB0_4 Depth 3
 29         extsw 12, 30
 30 .LBB0_3:                                # %while.body.lr.ph
 31                                         #   Parent Loop BB0_2 Depth=1
 32                                         # =>  This Loop Header: Depth=2
 33                                         #       Child Loop BB0_4 Depth 3
 34         extsw 11, 11
 35         sldi 0, 12, 1
 36         addi 30, 11, 1
 37         lhzx 0, 3, 0
 38         sldi 29, 11, 1
 39         cmpd     30, 8
 40         isel 30, 30, 8, 1
 41         sub      28, 30, 11
 42         add 30, 4, 29
 43         mtctr 28
 44         .p2align        5
 45 .LBB0_4:                                # %while.body
 46                                         #   Parent Loop BB0_2 Depth=1
 47                                         #     Parent Loop BB0_3 Depth=2
 48                                         # =>    This Inner Loop Header: Depth=3
 49         lhzu 29, 2(30)
 50         cmplw    0, 29
 51         beq      0, .LBB0_8
 52 # BB#5:                                 # %if.else
 53                                         #   in Loop: Header=BB0_4 Depth=3
 54         ble 0, .LBB0_7
 55 # BB#6:                                 # %if.then21
 56                                         #   in Loop: Header=BB0_4 Depth=3
 57         addi 11, 11, 1
 58         bdnz .LBB0_4
 59         b .LBB0_9
 60         .p2align        4
 61 .LBB0_7:                                # %if.else23
 62                                         #   in Loop: Header=BB0_3 Depth=2
 63         addi 12, 12, 1
 64         cmpw 1, 11, 6
 65         cmpd     12, 9
 66         crand 20, 0, 4
 67         bc 12, 20, .LBB0_3
 68         b .LBB0_9
 69         .p2align        4
 70 .LBB0_8:                                # %if.then
 71                                         #   in Loop: Header=BB0_2 Depth=1
 72         addi 30, 12, 1
 73         addi 11, 11, 1
 74         addi 12, 10, 1
 75         sldi 10, 10, 1
 76         sthx 0, 7, 10
 77         mr 10, 12
 78         cmpw     30, 5
 79         cmpw 1, 11, 6
 80         crand 20, 0, 4
 81         bc 12, 20, .LBB0_2
 82         b .LBB0_10
 83 .LBB0_9:
 84         mr 12, 10
 85 .LBB0_10:                               # %while.end
 86         extsw 3, 12
 87         ld 30, -16(1)                   # 8-byte Folded Reload
 88         ld 29, -24(1)                   # 8-byte Folded Reload
 89         ld 28, -32(1)                   # 8-byte Folded Reload
 90         blr

Although there is only loop in the source code, llvm generates an extra loop start from .LBB0_4. And the loop preparation work in .LBB0_3 is very slow because of isel and mtctr.

weiguozhi avatar Sep 10 '16 00:09 weiguozhi

mentioned in issue llvm/llvm-bugzilla-archive#30864

sylvestre avatar Nov 26 '21 22:11 sylvestre

Code right before instcombine The IR is attached.

The compare in question is

%cmp144 = icmp slt i32 %i_b.0.lcssa42, %size_b

in if.else23. All values of %i_b.0.lcssa42 reaching here, have already been compared with %size_b but there are two complications:

1- In entry size_b is compared against zero. But there is no direct comparison between size_b and %i_b.0.lcssa42 (or its reaching defs) before entering the outermost loop. 2- There is back edge from if.else23 to while.body.lr.ph. But if this edge is taken, in the last iteration loop %i_b.0.lcssa42 is not bumped. So a new comparison is not needed.

llvmbot avatar Sep 15 '16 21:09 llvmbot

Roughly speaking in any path of CFG reaching to this compare, the same check is already done.

To be clear, it is more complicated than this (because of a back edge) but the first question is whether the simpler case above (assuming no back edge) is handled or not? If yes, then we need to see how much support for back edge is there...

llvmbot avatar Sep 15 '16 18:09 llvmbot

Looking at the extra compare (and redundant crand caused by that), we cannot remove it during loop rotation. (The other compare that is removed during loop rotation is loop invariant, but this one is not).

GVN cannot remove it. There is no other compare, equivalent to this, to expect GVN to prove redundancy of this one.

I suspect that InstCombine should be able to remove it. Roughly speaking in any path of CFG reaching to this compare, the same check is already done.

I don't know where InstCombine code currently stands for this kind of optimization. This might be a simple bug or a whole new feature. I need to look more closely and figure that out.

llvmbot avatar Sep 15 '16 17:09 llvmbot

Here is the annotated code for the whole while loop

   │       cmpwi  r29,0                                                                                                                                                                                                      
   │       ble    1002374c <foo(int, int)+0x16c>                                                                                                                                                  
   │       ld     r4,96(r31)                                                                                                                                                                                                 
   │       ld     r3,128(r31)                                                                                                                                                                                                
   │       cmpwi  r30,0                                                                                                                                                                                                      
   │       li     r5,0                                                                                                                                                                                                       
   │       addi   r4,r4,-2                                                                                                                                                                                                   

0.13 │ ble 10023740 <foo(int, int)+0x160>
│ li r6,0
│ li r7,0
3.14 │ extsw r7,r7
1.90 │ extsw r6,r6
│ rldicr r9,r7,1,62
0.18 │ addi r8,r6,1
0.15 │ cmpd cr1,r8,r30
19.87 │ isel r10,r8,r30,5
│ lhzx r8,r3,r9
│ rldicr r9,r6,1,62
0.30 │ subf r10,r6,r10
│ add r9,r4,r9
9.20 │ mtctr r10
│ nop
│ nop
15.00 │ lhzu r10,2(r9)
│ cmplw cr1,r8,r10
│ beq cr1,10023720 <foo(int, int)+0x140>
8.56 │ ble cr1,10023700 <foo(int, int)+0x120>
5.81 │ addi r6,r6,1
0.04 │ bdnz 100236e0 <foo(int, int)+0x100>
│ b 10023740 <foo(int, int)+0x160>
│ nop
8.80 │ addi r7,r7,1
│ cmpw cr5,r6,r30
│ cmpd cr1,r7,r30
7.93 │ crand 4cr5+lt,4cr1+lt,4cr5+lt
│ blt cr5,100236b0 <foo(int, int)+0xd0>
0.24 │ b 10023740 <foo(int, int)+0x160>
│ nop
│ nop
10.75 │ addi r7,r7,1
│ addi r6,r6,1
│ cmpw cr1,r7,r30
0.16 │ cmpw cr5,r6,r30
7.60 │ crand 4
cr5+lt,4cr1+lt,4cr5+lt
│ blt cr5,100236ac <foo(int, int)+0xcc>
│ nop
│ nop
0.25 │ addi r5,r5,1
│ cmplw cr1,r5,r29
│ bne cr1,100236a0 <foo(int, int)+0xc0>

weiguozhi avatar Sep 15 '16 00:09 weiguozhi

Carrot, is the other problem (the extra compare) is something that has an impact on the performance that you see? I'd like to know how to prioritize it.

Besides the code I have pasted, there are only 4 hot instructions: the 2 crand instructions and 2 addi instructions which are branch target.

Do you know how this actually works on the P8? When I used perf on the P7 one of the issues was that it seemed as though all of the time was associated with the instructions that were last in each dispatch group. Thus, you had to be a bit careful when interpreting the numbers.

Yes, I think that is an issue on P8 as well (I am not positive though). But for the purpose of my earlier question, this indicates that removing the extra compare will be helpful. Crand in LBB0_7 should be removed once the extra compare is removed (and this should reduce the time spent in that bb/dispatch group or whatever).

Could you paste the perf annotation or mention how much of the ticks are on that block?

llvmbot avatar Sep 15 '16 00:09 llvmbot

Carrot, is the other problem (the extra compare) is something that has an impact on the performance that you see? I'd like to know how to prioritize it.

Besides the code I have pasted, there are only 4 hot instructions: the 2 crand instructions and 2 addi instructions which are branch target.

Do you know how this actually works on the P8? When I used perf on the P7 one of the issues was that it seemed as though all of the time was associated with the instructions that were last in each dispatch group. Thus, you had to be a bit careful when interpreting the numbers.

hfinkel avatar Sep 14 '16 23:09 hfinkel

Carrot, is the other problem (the extra compare) is something that has an impact on the performance that you see? I'd like to know how to prioritize it.

Besides the code I have pasted, there are only 4 hot instructions: the 2 crand instructions and 2 addi instructions which are branch target.

weiguozhi avatar Sep 14 '16 22:09 weiguozhi

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

Yes, I agree that what we do today, might be the best. Mostly wanted to add more details to see if we can find a reasonable way of excluding some undesirable cases.

I certainly appreciate more information :-)

Why exactly is the setup so expensive? Is there a stall before/after the mtctr? What happens if we disable the use of isel?

Following is the perf result of the extra loop and setup code,

3.14 │ extsw r7,r7
1.90 │ extsw r6,r6

   │       rldicr r9,r7,1,62                                            

0.18 │ addi r8,r6,1

0.15 │ cmpd cr1,r8,r30

19.87 │ isel r10,r8,r30,5

   │       lhzx   r8,r3,r9                                              

   │       rldicr r9,r6,1,62                                            

0.30 │ subf r10,r6,r10

   │       add    r9,r4,r9                                              

9.20 │ mtctr r10

Can you rerun this compiled with -mno-isel? I'm curious if this changes this at all. It is not clear why this is so expensive, and how the dispatch groups are actually breaking here.

Following is the code with option -mno-isel, the total execution time is 15% slower than without.

4.32 │ extsw r7,r7
1.75 │ rldicr r8,r7,1,62
│ extsw r6,r6
0.04 │ lhzx r8,r3,r8
0.02 │ addi r10,r6,1
2.58 │ rldicr r9,r6,1,62
0.00 │ cmpd cr1,r10,r30
16.80 │ bgt cr1,100236f0 <foo(int, int)+0xf0>
1.22 │ mr r10,r30
0.04 │ subf r10,r6,r10
0.01 │ add r9,r4,r9
8.01 │ mtctr r10

weiguozhi avatar Sep 14 '16 22:09 weiguozhi

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

Yes, I agree that what we do today, might be the best. Mostly wanted to add more details to see if we can find a reasonable way of excluding some undesirable cases.

I certainly appreciate more information :-)

Why exactly is the setup so expensive? Is there a stall before/after the mtctr? What happens if we disable the use of isel?

Following is the perf result of the extra loop and setup code,

3.14 │ extsw r7,r7
1.90 │ extsw r6,r6

   │       rldicr r9,r7,1,62                                            

0.18 │ addi r8,r6,1

0.15 │ cmpd cr1,r8,r30

19.87 │ isel r10,r8,r30,5

   │       lhzx   r8,r3,r9                                              

   │       rldicr r9,r6,1,62                                            

0.30 │ subf r10,r6,r10

   │       add    r9,r4,r9                                              

9.20 │ mtctr r10

Can you rerun this compiled with -mno-isel? I'm curious if this changes this at all. It is not clear why this is so expensive, and how the dispatch groups are actually breaking here.

hfinkel avatar Sep 14 '16 21:09 hfinkel

Carrot, is the other problem (the extra compare) is something that has an impact on the performance that you see? I'd like to know how to prioritize it.

llvmbot avatar Sep 14 '16 21:09 llvmbot

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

Yes, I agree that what we do today, might be the best. Mostly wanted to add more details to see if we can find a reasonable way of excluding some undesirable cases.

I certainly appreciate more information :-)

Why exactly is the setup so expensive? Is there a stall before/after the mtctr? What happens if we disable the use of isel?

Following is the perf result of the extra loop and setup code,

3.14 │ extsw r7,r7 1.90 │ extsw r6,r6
│ rldicr r9,r7,1,62
0.18 │ addi r8,r6,1
0.15 │ cmpd cr1,r8,r30
19.87 │ isel r10,r8,r30,5
│ lhzx r8,r3,r9
│ rldicr r9,r6,1,62
0.30 │ subf r10,r6,r10
│ add r9,r4,r9
9.20 │ mtctr r10
│ nop
│ nop
15.00 │ lhzu r10,2(r9)
│ cmplw cr1,r8,r10
│ beq cr1,10023720 <foo(int, int)+0x140>
8.56 │ ble cr1,10023700 <foo(int, int)+0x120>
5.81 │ addi r6,r6,1
0.04 │ bdnz 100236e0 <foo(int, int)+0x100>

weiguozhi avatar Sep 14 '16 20:09 weiguozhi

2- In LBB0_7 I can see two compares followed by a crand operation. This is corresponding to the last block of code in the loop which has one stmt: i_a++. We should be able to avoid one of the compares and get rid of crand. (i_b is not increased so we do not need to check that).

Interestingly in BB#6 we realize that i_a is not increased and we do not check its value. But we fail to do a very similar thing in LBB0_7

This problem appears after Loop Rotation. So I need to debug that pass.

I leave it to Carrot to comment on Hal's questions in Comment 8

llvmbot avatar Sep 14 '16 20:09 llvmbot

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

Yes, I agree that what we do today, might be the best. Mostly wanted to add more details to see if we can find a reasonable way of excluding some undesirable cases.

I certainly appreciate more information :-)

Why exactly is the setup so expensive? Is there a stall before/after the mtctr? What happens if we disable the use of isel?

hfinkel avatar Sep 14 '16 20:09 hfinkel

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

A heuristic based on trip count will be limited to FDO. Is that correct?

Yes, except in cases where we have a static trip count (unless we also multiversion).

hfinkel avatar Sep 14 '16 20:09 hfinkel

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

Yes, I agree that what we do today, might be the best. Mostly wanted to add more details to see if we can find a reasonable way of excluding some undesirable cases.

llvmbot avatar Sep 14 '16 20:09 llvmbot

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

A heuristic based on trip count will be limited to FDO. Is that correct?

llvmbot avatar Sep 14 '16 20:09 llvmbot

We currently perform the transformation whenever we can (i.e. we can determine an expression for the trip count and the loop won't contain other uses of the counter register).

Looking at LoopInfo data structures, there are three loops here, nested in each other. The inner most loop is the one for which we generate bdnz. This is debug output for that loop:

Exit Count for Loop at depth 3 containing: %while.body

,%if.else,%if.then21 from block if.then21: (-1 + (-1 * (sext i32 %i_b.0.ph52 to i64)) + ((sext i32 %size_b to i64) smax (1 + (sext i32 %i_b.0.ph52 to i64))))

This "inner most loop" corresponds to the path checking array_a[i_a] == array_b[i_b], then checking array_a[i_a] > array_b[i_b] and then increasing i_b.

Now there are three things here that stand out:

1- The loop is nested. 2- It has multiple exiting blocks, 3- Loop preparation work is expensive.

Can we disable generating bdnz based on a heuristic that relies on above properties? (Say when (1) and (2) is true do not generate bdnz?)

It certainly is possible. I'd not be in favor. The core problem here is that the trip count is small, and we should make a heuristic based on that (perhaps in combination with (3)). Either we should bias toward doing it or not doing it -- that's an independent decision -- but we can decide that by running benchmarks suites.

hfinkel avatar Sep 14 '16 20:09 hfinkel

We currently perform the transformation whenever we can (i.e. we can determine an expression for the trip count and the loop won't contain other uses of the counter register).

Looking at LoopInfo data structures, there are three loops here, nested in each other. The inner most loop is the one for which we generate bdnz. This is debug output for that loop:

Exit Count for Loop at depth 3 containing: %while.body

,%if.else,%if.then21 from block if.then21: (-1 + (-1 * (sext i32 %i_b.0.ph52 to i64)) + ((sext i32 %size_b to i64) smax (1 + (sext i32 %i_b.0.ph52 to i64))))

This "inner most loop" corresponds to the path checking array_a[i_a] == array_b[i_b], then checking array_a[i_a] > array_b[i_b] and then increasing i_b.

Now there are three things here that stand out:

1- The loop is nested. 2- It has multiple exiting blocks, 3- Loop preparation work is expensive.

Can we disable generating bdnz based on a heuristic that relies on above properties? (Say when (1) and (2) is true do not generate bdnz?)

llvmbot avatar Sep 14 '16 20:09 llvmbot

It seems to be still one loop but there are some issues that has to be addressed:

1- The isel + mtctr logic that Carrot mentioned is certainly questionable. This is created by "PowerPC CTR Loops" in an attempt to exploit bdnz. We need to double check whether it is really beneficial here or not. (Perhaps some tuning of heuristics of that pass is needed).

We currently perform the transformation whenever we can (i.e. we can determine an expression for the trip count and the loop won't contain other uses of the counter register). Unfortunately, it is not clear to me that we can do better, except perhaps on non-embedded cores where we can determine (either statically or via profiling information) that the trip count is small --and even in those circumstances, there are register-pressure effects which can be just as important. Nevertheless, just looking at the trip count might be enough (if we need do to anything at all).

Kit's isel work might take care of the isel problem regardless.

hfinkel avatar Sep 14 '16 17:09 hfinkel