[ppc] unnecessary cr instructions and isel instruction
| Bugzilla Link | 25548 |
| Resolution | FIXED |
| Resolved on | Sep 12, 2016 17:41 |
| Version | trunk |
| OS | Linux |
| CC | @echristo,@hfinkel |
Extended Description
The following function is from 453.povray
bool Ray_In_Bound (RAY *Ray, OBJECT *Bounding_Object) { OBJECT *Bound; INTERSECTION Local;
for (Bound = Bounding_Object; Bound != NULL; Bound = Bound->Sibling) { Increase_Counter(stats[Bounding_Region_Tests]);
if (!Intersection (&Local, Bound, Ray))
{
if (!Inside_Object(Ray->Initial, Bound))
{
return (false);
}
}
Increase_Counter(stats[Bounding_Region_Tests_Succeeded]);
}
return (true); }
When compiled with options -O2 -m64 -mvsx -mcpu=power8, I got following code:
_ZN3pov12Ray_In_BoundEPNS_10Ray_StructEPNS_13Object_StructE: # @_ZN3pov12Ray_In_BoundEPNS_10Ray_StructEPNS_13Object_StructE .Lfunc_begin1: .cfi_startproc .Ltmp12: addis 2, 12, .TOC.-.Ltmp12@ha addi 2, 2, .TOC.-.Ltmp12@l .Ltmp13: .localentry _ZN3pov12Ray_In_BoundEPNS_10Ray_StructEPNS_13Object_StructE, .Ltmp13-.Ltmp12
BB#0: # %entry
mfcr 12
mflr 0
std 31, -8(1)
std 0, 16(1)
stw 12, 8(1)
stdu 1, -336(1)
mr 31, 1
creqv 8, 8, 8 // A
std 29, 312(31) # 8-byte Folded Spill
std 30, 320(31) # 8-byte Folded Spill
mr 30, 4
mr 29, 3
std 27, 296(31) # 8-byte Folded Spill
std 28, 304(31) # 8-byte Folded Spill
cmpldi 30, 0
beq 0, .LBB1_6
BB#1: # %for.body.lr.ph
addis 3, 2, .LC0@toc@ha
addi 28, 31, 96
crxor 9, 9, 9 // B
ld 27, .LC0@toc@l(3)
.align 4
.LBB1_2: # %for.body # =>This Inner Loop Header: Depth=1 ld 3, 704(27) mr 4, 30 mr 5, 29 addi 3, 3, 1 std 3, 704(27) mr 3, 28 bl _ZN3pov12IntersectionEPNS_10istk_entryEPNS_13Object_StructEPNS_10Ray_StructE andi. 3, 3, 1 bc 12, 1, .LBB1_4
BB#3: # %if.then
# in Loop: Header=BB1_2 Depth=1
mr 3, 29
mr 4, 30
bl _ZN3pov13Inside_ObjectEPdPNS_13Object_StructE
cmpldi 3, 0
beq 0, .LBB1_5
.LBB1_4: # %if.end.3 # in Loop: Header=BB1_2 Depth=1 ld 3, 712(27) addi 3, 3, 1 std 3, 712(27) ori 2, 2, 0 ld 30, 16(30) cmpldi 30, 0 bne 0, .LBB1_2 b .LBB1_6 .LBB1_5: crmove 8, 9 // C .LBB1_6: # %cleanup li 3, 0 // D li 4, 1 // E ld 30, 320(31) # 8-byte Folded Reload ld 29, 312(31) # 8-byte Folded Reload ld 28, 304(31) # 8-byte Folded Reload ld 27, 296(31) # 8-byte Folded Reload isel 3, 4, 3, 8 // F addi 1, 1, 336 ld 0, 16(1) lwz 12, 8(1) ld 31, -8(1) mtocrf 32, 12 mtlr 0 blr
Instructions ABC use condition register to represent the result, and instructions DEF set actual result according to the cr value.
We can use simpler instructions. Instruction A can set reg 8 to 1, instruction C can set reg 8 to 0, then instructions BDEF can be removed. Note that all slow cr manipulation instructions and isel have been removed.
Yes, it is fixed by my patch r277655.
The last testcase also seems fixed:
echristo@ghostwheel ~/tmp> ~/builds/build-llvm/bin/clang++ foo.c -target powerpc64le-linux-gnu -O2 -S -o - -fno-strict-aliasing -mcpu=pwr8 -mvsx .text .abiversion 2 .file "foo.c" .globl Z4testRK2pbS1 .p2align 4 .type Z4testRK2pbS1,@function Z4testRK2pbS1: # @Z4testRK2pbS1 .Lfunc_begin0: .cfi_startproc .Lfunc_gep0: addis 2, 12, .TOC.-.Lfunc_gep0@ha addi 2, 2, .TOC.-.Lfunc_gep0@l .Lfunc_lep0: .localentry Z4testRK2pbS1, .Lfunc_lep0-.Lfunc_gep0
BB#0: # %entry
mflr 0
std 31, -8(1)
std 0, 16(1)
stdu 1, -128(1)
.Ltmp0: .cfi_def_cfa_offset 128 .Ltmp1: .cfi_offset r31, -8 .Ltmp2: .cfi_offset lr, 16 mr 31, 1 .Ltmp3: .cfi_def_cfa_register r31 .Ltmp4: .cfi_offset r28, -32 .Ltmp5: .cfi_offset r29, -24 .Ltmp6: .cfi_offset r30, -16 std 29, 104(31) # 8-byte Folded Spill std 28, 96(31) # 8-byte Folded Spill std 30, 112(31) # 8-byte Folded Spill mr 30, 4 mr 28, 3 li 29, 0 lwz 3, 0(28) lwz 4, 0(30) xor 3, 3, 4 andi. 3, 3, 1 bc 12, 1, .LBB0_3
BB#1: # %if.end
mr 3, 28
mr 4, 30
bl _Z3fooRK2pbS1_
nop
cmpldi 3, 0
beq 0, .LBB0_3
BB#2: # %if.end5
mr 3, 28
mr 4, 30
bl _Z3barRK2pbS1_
nop
mr 29, 3
.LBB0_3: # %return clrldi 3, 29, 32 ld 30, 112(31) # 8-byte Folded Reload ld 29, 104(31) # 8-byte Folded Reload ld 28, 96(31) # 8-byte Folded Reload addi 1, 1, 128 ld 0, 16(1) ld 31, -8(1) mtlr 0 blr .long 0 .quad 0 .Lfunc_end0: .size Z4testRK2pbS1, .Lfunc_end0-.Lfunc_begin0 .cfi_endproc
.ident "clang version 4.0.0 (trunk 280921) (llvm/trunk 280960)"
.section ".note.GNU-stack","",@progbits
Current llvm can't reproduce this problem on povray, but it still reproduces it in our protocol buffer benchmark. I have simplified it to the following testcase:
$clang++ --target=powerpc64le-grtev4-linux-gnu -mvsx -mcpu=power8 -O2
struct pb{ bool fff() const {return (f0 & 0x00000001u) != 0;}; unsigned int f0; };
bool foo(const pb& a, const pb& b); bool bar(const pb& a, const pb& b);
bool test(const pb& a, const pb& b) { if (a.fff() != b.fff()) return false; if (!foo(a, b)) return false; if (!bar(a, b)) return false; return true; }
Following is a simplified test case without povray headers.
bool Intersection (); bool Inside_Object ();
bool Ray_In_Bound (int x) { for (int i=0; i<x; i++) { if (!Intersection ()) if (!Inside_Object()) return false; }
return true; }
Thanks Carrot - I didn't realize ISEL played such a large role. Then it is likely that the above fix will not remove the performance difference completely.
Once I have the ISEL patch ready and posted on Phabricator, I will post a link here.
This should be (partially) addressed by the following patch, currently under review:http://reviews.llvm.org/D14064.
I am also working on a different patch to remove the ISEL from code generation, although I don't know how much impact that will have in this specific case.
Good to know this.
I profiled povray built with slightly older llvm, the generated code is not exactly same as today but similar. This function consumes 4.46% of total execution time, and the isel instruction consumes 25.81% time of this function.
: 000000001006bc10 <pov::Ray_In_Bound(pov::Ray_Struct*, pov::Object_Struct*)>:
0.00 : 1006bc10: addis r2,r12,33
0.00 : 1006bc14: addi r2,r2,2656
17.93 : 1006bc18: mfcr r12 17.12 : 1006bc1c: mflr r0 0.03 : 1006bc20: std r31,-8(r1) 0.00 : 1006bc24: std r0,16(r1) 0.10 : 1006bc28: stw r12,8(r1) 0.00 : 1006bc2c: stdu r1,-336(r1) 4.29 : 1006bc30: mr r31,r1 6.55 : 1006bc34: crset 4cr2+lt 0.08 : 1006bc38: std r29,312(r31) 0.10 : 1006bc3c: std r30,320(r31) 0.09 : 1006bc40: mr r30,r4 0.00 : 1006bc44: mr r29,r3 0.00 : 1006bc48: std r27,296(r31) 5.56 : 1006bc4c: std r28,304(r31) 0.06 : 1006bc50: cmpldi r30,0 0.00 : 1006bc54: beq 1006bccc <pov::Ray_In_Bound(pov::Ray_Struct, pov::Object_Struct*)+0xbc> 0.00 : 1006bc58: nop 0.00 : 1006bc5c: addi r28,r31,96 0.00 : 1006bc60: crclr 4cr2+gt 0.00 : 1006bc64: ld r27,-30960(r2) 0.00 : 1006bc68: nop 0.00 : 1006bc6c: nop 0.00 : 1006bc70: ld r3,704(r27) 0.00 : 1006bc74: mr r4,r30 0.00 : 1006bc78: mr r5,r29 0.00 : 1006bc7c: addi r3,r3,1 0.00 : 1006bc80: std r3,704(r27) 0.00 : 1006bc84: mr r3,r28 0.00 : 1006bc88: bl 1006ba48 <pov::Intersection(pov::istk_entry, pov::Object_Struct*, pov::Ray_Struct*)+0x8> 0.00 : 1006bc8c: andi. r3,r3,1 0.00 : 1006bc90: bgt 1006bca8 <pov::Ray_In_Bound(pov::Ray_Struct*, pov::Object_Struct*)+0x98> 0.00 : 1006bc94: mr r3,r29 0.00 : 1006bc98: mr r4,r30 0.00 : 1006bc9c: bl 1006be98 <pov::Inside_Object(double*, pov::Object_Struct*)+0x8> 0.00 : 1006bca0: cmpldi r3,0 0.00 : 1006bca4: beq 1006bcc8 <pov::Ray_In_Bound(pov::Ray_Struct*, pov::Object_Struct*)+0xb8> 0.00 : 1006bca8: ld r3,712(r27) 0.00 : 1006bcac: addi r3,r3,1 0.00 : 1006bcb0: std r3,712(r27) 0.00 : 1006bcb4: ori r2,r2,0 0.00 : 1006bcb8: ld r30,16(r30) 0.00 : 1006bcbc: cmpldi r30,0 0.00 : 1006bcc0: bne 1006bc70 <pov::Ray_In_Bound(pov::Ray_Struct*, pov::Object_Struct*)+0x60> 0.00 : 1006bcc4: b 1006bccc <pov::Ray_In_Bound(pov::Ray_Struct*, pov::Object_Struct*)+0xbc> 0.00 : 1006bcc8: crmove 4cr2+lt,4cr2+gt 12.35 : 1006bccc: li r3,0 0.11 : 1006bcd0: li r4,1 0.00 : 1006bcd4: ld r30,320(r31) 0.03 : 1006bcd8: ld r29,312(r31) 0.08 : 1006bcdc: ld r28,304(r31) 4.97 : 1006bce0: ld r27,296(r31) 25.81 : 1006bce4: isel r3,r4,r3,8 0.00 : 1006bce8: addi r1,r1,336 0.00 : 1006bcec: ld r0,16(r1) 0.11 : 1006bcf0: lwz r12,8(r1) 0.00 : 1006bcf4: ld r31,-8(r1) 0.00 : 1006bcf8: mtocrf 32,r12 4.63 : 1006bcfc: mtlr r0 0.01 : 1006bd00: blr
This should be (partially) addressed by the following patch, currently under review:http://reviews.llvm.org/D14064.
I am also working on a different patch to remove the ISEL from code generation, although I don't know how much impact that will have in this specific case.