LLVM built 445.gobmk is 17% slower than gcc on power8
| Bugzilla Link | 24850 |
| Resolution | WORKSFORME |
| Resolved on | Mar 15, 2016 11:27 |
| Version | trunk |
| OS | Linux |
| CC | @asl,@echristo,@hfinkel |
Extended Description
LLVM built 445.gobmk is 17% slower than gcc built binary on power8.
gcc 438s llvm 512s
For input data trevord.tst, llvm is 18% slower.
The problem is in function popgo. In gcc built binary it consumes 4.11% of time, in llvm built binary it consumes 13.98% of time.
The related code snippet is in engine/board.c:
struct change_stack_entry { int *address; int value; }; static struct change_stack_entry *change_stack_pointer;
#define POP_MOVE()
while ((--change_stack_pointer)->address)
*(change_stack_pointer->address) =
change_stack_pointer->value
LLVM generated code sequence is:
68.05 : 1000a9f0: ld r3,-22832(r29) // A 0.66 : 1000a9f4: addi r4,r3,-16 0.17 : 1000a9f8: std r4,-22832(r29) // B 0.02 : 1000a9fc: ori r2,r2,0 14.30 : 1000aa00: ld r4,-16(r3) 0.00 : 1000aa04: cmpldi r4,0 0.00 : 1000aa08: beq 1000aa18 <popgo+0xa8> 0.53 : 1000aa0c: lwz r3,-8(r3) 0.11 : 1000aa10: stw r3,0(r4) 0.00 : 1000aa14: b 1000a9f0 <popgo+0x80>
Instruction A reads variable change_stack_pointer, instruction B writes change_stack_pointer.
GCC generated code sequence is:
48.30 : 10010280: lwz r8,24(r9) 0.00 : 10010284: mr r7,r9 0.00 : 10010288: addi r9,r9,-16 0.63 : 1001028c: stw r8,0(r10) 0.00 : 10010290: ld r10,16(r9) 0.00 : 10010294: cmpdi cr7,r10,0 0.00 : 10010298: bne cr7,10010280 <popgo+0x90> 15.54 : 1001029c: nop
Note that variable change_stack_pointer is in register r9, it reads it at the start of the function, and writes it after the loop. Since the address of change_stack_pointer is never assigned to another variable, and it's a static variable, so it can't be aliased with any other pointer, so it is safe to do this optimization.
Even if I add -fstrict-aliasing explicitly to llvm command line, it can move the read of change_stack_pointer out of the loop, but still contains write of change_stack_pointer in the loop.
Command line options are:
-DSPEC_CPU -DNDEBUG -DHAVE_CONFIG_H -I. -I.. -I../include -I./include -fno-strict-aliasing -O2 -m64 -mvsx -mcpu=power8 -DSPEC_CPU_LP64
the problem reported here is not reproducible anymore and the gap is now seems to be much smaller. (assuming the gcc options and versions that I have used are the same as the original report). But I have been able to confirm that popgo function now takes the same time that it takes in gcc-built binary,
The problem seems to be resolved already. This is the code that I see for the loop now
0.86 │1000a05c: nop 33.45 │1000a060: lwzu r6,-16(r5) │1000a064: addi r3,r3,-16 0.07 │1000a068: stw r6,0(r4) // A │1000a06c: ori r2,r2,0 15.84 │1000a070: ld r4,-24(r5) │1000a074: cmpldi r4,0 │1000a078: bne 1000a060 <popgo+0xb0>
Here the only store instruction in the loop body is instruction A which writes the value of change_stack_pointer->value to the address change_stack_pointer->address.
Also function popgo now consumes 4.06% of the time which is almost exactly the same as gcc-built binary behavior that you mentioned above.
Also when I compared with gcc the gap was much smaller (less than 5% if I remember correctly), but I used gcc 4.9 and passed the same options to it that I passed to LLVM. I am not sure if that is how you compared LLVM and gcc though.
So, the clang ABI lowering for power generates significantly different LLVM IR than x86, and this has exposed these kinds of issues before (even when not dealing with arguments/etc)
IIRC, it lowers pointers as 64 bit ints ina lot of cases, and then does a lot of inttoptr and ptrtoint calls that cause alias analysis and other things to simply not succeed very well.
There are numerous ways to fix this, but i suspect that is the underlying issue here (again, just guessing, but that is what i would imagine is the reason it would be much worse on powerpc)
What happens on other targets? This seems like a generic forward store motion issue. Shouldn't that be handled at the IR optimization level? I'm just curious why this is specifically called out for power8.