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

performance regression in LoopStrengthReduce

Open dobbelaj-snps opened this issue 9 years ago • 3 comments

Bugzilla Link 31629
Version 3.9
OS Windows NT
Attachments testcase showing bad behavior, code produced with top-of-tree (20170113), result without instructionSimplifierPass
CC @arpilipe,@fhahn,@zmodem,@sanjoy

Extended Description

During the 3.8-3.9 timeframe, a number of changes were done that exhibit some severe quality-of-code regression.

For the testcase, I am using the xscale target to show the regression, but we noticed it in our custom version of llvm. So this behavior is likely to be present for most targets.

The main change that shows the regression is:

Author: Manuel Jacob [email protected] Date: Thu Jun 2 22:14:26 2016 +0000

[PM] Schedule InstSimplify after late LICM run, to clean up LCSSA nodes.

Summary:
The module pass pipeline includes a late LICM run after loop
unrolling.  LCSSA is implicitly run as a pass dependency of LICM.  However no
cleanup pass was run after this, so the LCSSA nodes ended in the optimized output.

Reviewers: hfinkel, mehdi_amini

Subscribers: majnemer, bruno, mzolotukhin, mehdi_amini, llvm-commits

Differential Revision: http://reviews.llvm.org/D20606

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271602 91177308-0d34-0410-b5e6-96231b3b80d8

This change activates the instructionSimplifyPass, but, this change only allows us to see when LoopStrengthReduce is breaking down.

dobbelaj-snps avatar Jan 13 '17 17:01 dobbelaj-snps

Note: also on x86, the performance issue is seen. It also uses a bigger stack due to the more active registers.

inner loop with base llvm version: (note: also needs an extra 40 bytes of stack) ... .p2align 4, 0x90 .LBB1_2: # %for.body # =>This Inner Loop Header: Depth=1 movl %r9d, %r15d movl %r10d, %r13d movl %ebx, %r14d movq (%rsp), %rdx # 8-byte Reload leal (%r14,%rdx), %r9d movl bar.A+8(,%r8,4), %ebx leal (%r9,%rbx), %edx cmpl $501, %edx # imm = 0x1F5 jge .LBB1_3

BB#4: # %for.inc

                                    #   in Loop: Header=BB1_2 Depth=1
leal	(%r14,%r13), %edx
leal	1(%r8,%rdx), %r10d
addl	%r15d, %r9d
incq	%r8
addl	$5, %r12d
addl	12(%rsp), %ebp          # 4-byte Folded Reload
addl	$3, %esi
addl	%r11d, %eax
addl	$2, %ecx
cmpq	%rdi, %r8
jl	.LBB1_2

...

Inner loop when phi-nodes were kept (aka, without instructionSimplifierPass): .p2align 4, 0x90 .LBB1_2: # %for.body # =>This Inner Loop Header: Depth=1 movl %ebp, %esi movq %rax, %rbx leal (%rsi,%rdi), %eax movl bar.A+8(,%rbx,4), %ebp leal (%rax,%rbp), %r11d cmpl $501, %r11d # imm = 0x1F5 jge .LBB1_3

BB#4: # %for.inc

                                    #   in Loop: Header=BB1_2 Depth=1
movl	%ecx, %edx
addl	%esi, %edx
leal	1(%rbx,%rdx), %ecx
addl	%eax, %r14d
leaq	1(%rbx), %rax
cmpq	%r8, %rax
jl	.LBB1_2

dobbelaj-snps avatar Jan 17 '17 12:01 dobbelaj-snps

Now for the analysis of what is happening:

  • the instructionSimplifierPass is cleaning up PHI nodes from LCSAA
  • because of this, IVUsers.cpp is now adding more complex expressions (whereas earlier, it would stop at the PHI node)

See lib/Analysis/IVUsers.cpp: IVUsers::AddUsersImpl :

.. bool AddUserToIVUsers = false; if (LI->getLoopFor(User->getParent()) != L) { if (isa(User) || Processed.count(User) || //***** Here the behavior changes !AddUsersImpl(User, SimpleLoopNests)) { DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n' << " OF SCEV: " << *ISE << '\n'); AddUserToIVUsers = true; } } else if (Processed.count(User) || !AddUsersImpl(User, SimpleLoopNests)) { DEBUG(dbgs() << "FOUND USER: " << *User << '\n' << " OF SCEV: " << *ISE << '\n'); AddUserToIVUsers = true; } ...

Without instructionSimplifierPass, LoopStrengthReduce is considering: FOUND USER: %cmp2 = icmp slt i32 %i.083, %n OF SCEV: {1,+,1}<%for.body> FOUND USER in other loop: %add7.lcssa102 = phi i32 [ %add7, %for.inc ] OF SCEV: {2,+,1}<%for.body> NORMALIZED TO: {1,+,1}<%for.body> FOUND USER: %2 = load i32, i32* %arrayidx8, align 4, !tbaa !​3 OF SCEV: {(8 + @​bar.A),+,4}<%for.body> FOUND USER in other loop: %add.lcssa103 = phi i32 [ %add, %for.inc ] OF SCEV: ({1,+,1}<%for.body> + %1) NORMALIZED TO: ({0,+,1}<%for.body> + %1) FOUND USER in other loop: %add.lcssa = phi i32 [ %add, %for.body ] OF SCEV: ({0,+,1}<%for.body> + %1) NORMALIZED TO: ({0,+,1}<%for.body> + %1) FOUND USER in other loop: %add6.lcssa = phi i32 [ %add6, %for.inc ] OF SCEV: ({1,+,1}<%for.body> + %y.082 + %1) NORMALIZED TO: ({0,+,1}<%for.body> + %y.082 + %1) FOUND USER: %y.082 = phi i32 [ %add6, %for.inc ], [ 0, %for.body.preheader ] OF SCEV: ({0,+,1}<%for.body> + %y.082 + %1) NORMALIZED TO: ({0,+,1}<%for.body> + %y.082 + %1)

with, LoopStrengthReduce is considering: FOUND USER in other loop: %retval.1 = phi i32 [ -1, %entry ], [ %cond.i, %if.then11 ], [ %add25, %for.end24 ] OF SCEV: ((4 * %1) + ((-1 + %n) * ((2 * %1) + {(4 + (5 * %n)),+,5}<%for.body> + %2)) + {(4 + (1431655765 * (trunc i33 (((zext i32 (-3 + %n) to i33) * (zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32)) + ((4 + (2 * (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32))) * %n)),+,(4 + (2 * (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32)))}<%for.body> + %2 + %x.081 + %y.082) NORMALIZED TO: ((4 * %1) + ((-1 + %n) * ((2 * %1) + {(-1 + (5 * %n)),+,5}<%for.body> + %2)) + {((1431655765 * (trunc i33 (((zext i32 (-3 + %n) to i33) * (zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32)) + (-2 * (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32)) + ((4 + (2 * (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32))) * %n)),+,(4 + (2 * (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32)))}<%for.body> + %2 + %x.081 + %y.082) FOUND USER in other loop: tail call arm_aapcscc void @​fum(i32 %38) #​3 OF SCEV: ((2 * %1) + ((-1 + %n) * ({(-1 + (3 * %n)),+,2}<%for.body> + %2 + %1)) + {(1 + (2 * %n) + (1431655765 * (trunc i33 (((zext i32 (-3 + %n) to i33) * (zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32)) + ((trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32) * (-2 + %n))),+,(2 + (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32))}<%for.body> + %2 + %y.082) NORMALIZED TO: ((2 * %1) + ((-1 + %n) * ({(-3 + (3 * %n)),+,2}<%for.body> + %2 + %1)) + {(-1 + (2 * %n) + (1431655765 * (trunc i33 (((zext i32 (-3 + %n) to i33) * (zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32)) + ((trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32) * (-3 + %n))),+,(2 + (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32))}<%for.body> + %2 + %y.082) FOUND USER in other loop: %add25 = add nsw i32 %38, %37 OF SCEV: ((2 * %1) + ((-1 + %n) * ({(5 + (2 * %n)),+,3}<%for.body> + %1)) + {(3 + (2 * %n) + ((trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32) * (2 + %n))),+,(2 + (trunc i33 (((zext i32 (-2 + %n) to i33) * (zext i32 (-1 + %n) to i33)) /u 2) to i32))}<%for.body> + %x.081) ..... FOUND USER in other loop: %4 = add i32 %3, %add OF SCEV: ({1,+,1}<%for.body> + %1) NORMALIZED TO: ({0,+,1}<%for.body> + %1) FOUND USER in other loop: %6 = add i32 %add, %5 OF SCEV: ({0,+,1}<%for.body> + %1) NORMALIZED TO: ({-1,+,1}<%for.body> + %1)

This results in much more complex computations and a lot more active registers :(

IMHO, LoopStrengthReduce should probably be learned to skip these kind of expressions, but I am not sure if this is indeed the way forward...

Any suggestions ?

dobbelaj-snps avatar Jan 13 '17 18:01 dobbelaj-snps

I have done some bisects to find out why the code is so bad. The main effect of the original change by Manual Jacob is that PHI nodes are cleaned up after LCSAA. This seems to trigger some bad behavior in LoopStrengthReduce and IVUsers.

Following change made the situation a little bit better:


commit bf4114e90b9f50a5b7d6b5b54002e7e70f13d3c9 Author: Michael Zolotukhin [email protected] Date: Fri Jul 15 21:08:41 2016 +0000

Make processInstruction from LCSSA.cpp externally available.

Summary:
When a pass tries to keep LCSSA form it's often convenient to be able to update
LCSSA for a set of instructions rather than for the entire loop. This patch makes the
processInstruction from LCSSA externally available under a name
formLCSSAForInstruction.

Reviewers: chandlerc, sanjoy, hfinkel

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D22378

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275613 91177308-0d34-0410-b5e6-96231b3b80d8

and following changes made it even worse:


commit a6da8aab671aae33116af4f0bed4190ede652da2 Author: Artur Pilipenko [email protected] Date: Fri Aug 12 10:05:11 2016 +0000

[LVI] Handle any predicate in comparisons like icmp <pred> (add Val, Offset), ...

Currently LVI can only gather value constraints from comparisons like:

* icmp <pred> Val, ...
* icmp ult (add Val, Offset), ...

In fact we can handle any predicate in latter comparisons.

Reviewed By: sanjoy

Differential Revision: https://reviews.llvm.org/D23357


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278493 91177308-0d34-0410-b5e6-96231b3b80d8

and also:


commit e18cb89a650d9e151b0090c9abd17a87b0bce9c3 Author: Evgeny Stupachenko [email protected] Date: Wed Nov 30 22:23:51 2016 +0000

Fix LSR best register search algorithm.

Summary:
Fix a case when first register in a search has maximum
RegUses.getUsedByIndices(Reg).count()

Reviewers: qcolombet

Differential Revision: http://reviews.llvm.org/D26877

From: Evgeny Stupachenko <[email protected]>


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288278 91177308-0d34-0410-b5e6-96231b3b80d8

So, the situation is bad in llvm-3.9, but in the upcoming llvm-4.0.0, it will be even worse !

Also: disabling the instructionSimplifierPass resolves this use case, but has a bad result on coremark..

dobbelaj-snps avatar Jan 13 '17 17:01 dobbelaj-snps