<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Mar 12, 2015 at 2:34 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It is not clear to me at all that preventing the merging is the right solution. There are a large number of analysis, including alias analysis, and optimizations that use GetUnderlyingObject, and related routines to search back through GEPs. They only do this up to some small finite depth (six, IIRC). So reducing the GEP depth is likely the right solution for InstCombine (which has the job of canonicalizing the IR).<br>
<br>
We should, however, pull these apart somewhere, and probably in some way that is address-mode aware. I'd recommend trying to split non-free (via the addressing-mode) loop-invariant parts of GEPs out of loops in CodeGenPrep.<br></blockquote><div><br></div><div>Thanks, Hal.  I'll have a look at CGP to see how this might be done.  It's a little more complicated than just pulling the GEP apart, there needs to be a loop-invariant-aware reassociation to undo the damage done by the initial merge.</div><div><br></div><div>Mark</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
 -Hal<br>
<span class=""><br>
----- Original Message -----<br>
> From: "Mark Heffernan" <<a href="mailto:meheff@google.com">meheff@google.com</a>><br>
> To: "Francois Pichet" <<a href="mailto:pichet2000@gmail.com">pichet2000@gmail.com</a>><br>
</span><span class="">> Cc: "Hal Finkel" <<a href="mailto:hfinkel@anl.gov">hfinkel@anl.gov</a>>, "LLVM Developers Mailing List" <<a href="mailto:llvmdev@cs.uiuc.edu">llvmdev@cs.uiuc.edu</a>><br>
> Sent: Thursday, March 12, 2015 4:19:56 PM<br>
> Subject: Re: [LLVMdev] Question about shouldMergeGEPs in InstructionCombining<br>
><br>
><br>
><br>
><br>
</span><span class="">> On Thu, Mar 12, 2015 at 2:03 PM, Francois Pichet <<br>
> <a href="mailto:pichet2000@gmail.com">pichet2000@gmail.com</a> > wrote:<br>
><br>
><br>
><br>
><br>
> I think it would make sense for (1) and (2). I am not sure if (3) is<br>
</span>> feasible in instcombine . (I am not too familiar with LoopInfo)<br>
<span class="">><br>
><br>
> LoopInfo should be available for at least one of the instcombine<br>
> invocations. In the -O2 pipeline it looks like instcombine is called<br>
> 7 times and it should have loopinfo for the third invocation (called<br>
> immediate after some loop passes). Here's output from<br>
> --debug-pass=Structure:<br>
><br>
><br>
><br>
</span>> Pass Arguments: -tti -no- aa -tbaa -scoped-noalias<br>
<span class="">> -assumption-cache-tracker -targetlibinfo -basicaa -verify<br>
> -simplifycfg -domtree -sroa -early-cse -lower-expect<br>
> Target Transform Information<br>
> No Alias Analysis (always returns 'may' alias)<br>
> Type-Based Alias Analysis<br>
> Scoped NoAlias Alias Analysis<br>
> Assumption Cache Tracker<br>
> Target Library Information<br>
> Basic Alias Analysis (stateless AA impl)<br>
> FunctionPass Manager<br>
> Module Verifier<br>
> Simplify the CFG<br>
> Dominator Tree Construction<br>
> SROA<br>
> Early CSE<br>
> Lower 'expect' Intrinsics<br>
> Pass Arguments: -targetlibinfo -tti -no-aa -tbaa -scoped-noalias<br>
> -assumption-cache-tracker -basicaa -verify-di -ipsccp -globalopt<br>
> -deadargelim -domtree -instcombine -simplifycfg -basiccg -prune-eh<br>
> -inline-cost -i<br>
> nline -functionattrs -sroa -domtree -early-cse -lazy-value-info<br>
</span>> -jump-threadi ng -correlated-propagation -simplifycfg -domtree<br>
<span class="">> -instcombine -tailcallelim -simplifycfg -reassociate -domtree -loops<br>
> -loop-simplify -lc<br>
> ssa -loop-rotate -licm -loop-unswitch -instcombine -scalar-evolution<br>
> -loop-simplify -lcssa -indvars -loop-idiom -loop-deletion<br>
> -loop-unroll -memdep -mldst-motion -domtree -memdep -gvn -memdep<br>
> -memcpyopt -sccp -dom<br>
> tree -bdce -instcombine -lazy-value-info -jump-threading<br>
> -correlated-propagation -domtree -memdep -dse -loops -loop-simplify<br>
> -lcssa -licm -adce -simplifycfg -domtree -instcombine -barrier<br>
> -domtree -loops -loop-sim<br>
> plify -lcssa -loop-rotate -branch-prob -block-freq -scalar-evolution<br>
> -loop-accesses -loop-vectorize -instcombine -scalar-evolution<br>
> -slp-vectorizer -simplifycfg -domtree -instcombine -loops<br>
> -loop-simplify -lcssa -s<br>
> calar-evolution -loop-unroll -alignment-from-assumptions<br>
> -strip-dead-prototypes -globaldce -constmerge -verify -verify-di<br>
> Target Library Information<br>
</span>> Tar get Transform Information<br>
<div class="HOEnZb"><div class="h5">> No Alias Analysis (always returns 'may' alias)<br>
> Type-Based Alias Analysis<br>
> Scoped NoAlias Alias Analysis<br>
> Assumption Cache Tracker<br>
> Basic Alias Analysis (stateless AA impl)<br>
> ModulePass Manager<br>
> Debug Info Verifier<br>
> Interprocedural Sparse Conditional Constant Propagation<br>
> Global Variable Optimizer<br>
> Dead Argument Elimination<br>
> FunctionPass Manager<br>
> Dominator Tree Construction<br>
> Combine redundant instructions<br>
> Simplify the CFG<br>
> CallGraph Construction<br>
> Call Graph SCC Pass Manager<br>
> Remove unused exception handling info<br>
> Inline Cost Analysis<br>
> Function Integration/Inlining<br>
> Deduce function attributes<br>
> FunctionPass Manager<br>
> SROA<br>
> Dominator Tree Construction<br>
> Early CSE<br>
> Lazy Value Information Analysis<br>
> Jump Threading<br>
> Value Propagation<br>
> Simplify the CFG<br>
> Dominator Tree Construction<br>
> Combine redundant instructions<br>
> Tail Call Elimination<br>
> Simplify the CFG<br>
> Reassociate expressions<br>
> Dominator Tree Construction<br>
> Natural Loop Information<br>
> Canonicalize natural loops<br>
> Loop-Closed SSA Form Pass<br>
> Loop Pass Manager<br>
> Rotate Loops<br>
> Loop Invariant Code Motion<br>
> Unswitch loops<br>
><br>
> Combine redundant instructions<br>
> Scalar Evolution Analysis<br>
> Canonicalize natural loops<br>
> Loop-Closed SSA Form Pass<br>
> Loop Pass Manager<br>
> Induction Variable Simplification<br>
> Recognize loop idioms<br>
> Delete dead loops<br>
> Unroll loops<br>
> Memory Dependence Analysis<br>
> MergedLoadStoreMotion<br>
> Dominator Tree Construction<br>
> Memory Dependence Analysis<br>
> Global Value Numbering<br>
> Memory Dependence Analysis<br>
> MemCpy Optimization<br>
> Sparse Conditional Constant Propagation<br>
> Dominator Tree Construction<br>
> Bit-Tracking Dead Code Elimination<br>
> Combine redundant instructions<br>
> Lazy Value Information Analysis<br>
> Jump Threading<br>
> Value Propagation<br>
> Dominator Tree Construction<br>
> Memory Dependence Analysis<br>
> Dead Store Elimination<br>
> Natural Loop Information<br>
> Canonicalize natural loops<br>
> Loop-Closed SSA Form Pass<br>
> Loop Pass Manager<br>
> Loop Invariant Code Motion<br>
> Aggressive Dead Code Elimination<br>
> Simplify the CFG<br>
> Dominator Tree Construction<br>
> Combine redundant instructions<br>
> A No-Op Barrier Pass<br>
> FunctionPass Manager<br>
> Dominator Tree Construction<br>
> Natural Loop Information<br>
> Canonicalize natural loops<br>
> Loop-Closed SSA Form Pass<br>
> Loop Pass Manager<br>
> Rotate Loops<br>
> Branch Probability Analysis<br>
> Block Frequency Analysis<br>
> Scalar Evolution Analysis<br>
> Loop Access Analysis<br>
> Loop Vectorization<br>
> Combine redundant instructions<br>
> Scalar Evolution Analysis<br>
> SLP Vectorizer<br>
> Simplify the CFG<br>
> Dominator Tree Construction<br>
> Combine redundant instructions<br>
> Natural Loop Information<br>
> Canonicalize natural loops<br>
> Loop-Closed SSA Form Pass<br>
> Scalar Evolution Analysis<br>
> Loop Pass Manager<br>
> Unroll loops<br>
> Alignment from assumptions<br>
><br>
> Strip Unused Function Prototypes<br>
> Dead Global Elimination<br>
> Merge Duplicate Global Constants<br>
> FunctionPass Manager<br>
> Module Verifier<br>
> Debug Info Verifier Bitcode Writer<br>
><br>
><br>
><br>
><br>
><br>
><br>
> Mark<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
> For the Octasic's Opus platform, I modified shouldMergeGEPs in our<br>
> fork to:<br>
><br>
><br>
><br>
> if (GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&<br>
> !Src.hasOneUse())<br>
> return false;<br>
><br>
><br>
> return Src.hasAllConstantIndices(); // was return false;<br>
><br>
><br>
><br>
> Following that change, I noticed some performance gain for a few<br>
> specific tests and no regression at all in our (admittedly limited)<br>
> benchmarks suite.<br>
><br>
><br>
> Regards,<br>
> Francois Pichet, Octasic.<br>
><br>
><br>
><br>
><br>
> On Thu, Mar 12, 2015 at 4:14 PM, Mark Heffernan < <a href="mailto:meheff@google.com">meheff@google.com</a> ><br>
> wrote:<br>
><br>
><br>
><br>
> Coincidentally, I just ran into this same issue on some of our<br>
> benchmarks for the NVPTX backend. You have something like this<br>
> before instcombine:<br>
><br>
><br>
> %tmp = getelementptr inbounds i32, i32* %input, i64 %offset<br>
><br>
> loop:<br>
><br>
> %loop_variant = ...<br>
> %ptr = getelementptr inbounds i32, i32* %tmp, i64 %loop_variant<br>
><br>
><br>
> Which gets transformed to:<br>
><br>
><br>
><br>
> loop:<br>
><br>
> %loop_variant = ...<br>
> %sum = add nsw i64 %loop_variant, %offset<br>
><br>
> %ptr = getelementptr inbounds i32, i32* %input, i64 %sum<br>
><br>
><br>
><br>
> The merge essentially reassociates the loop-variant term<br>
> (%loop_variant) and loop-invariant terms (%input and %offset) in<br>
> such a way that LICM can't remove it.<br>
><br>
><br>
><br>
><br>
> One idea is to only perform this style of gep merge if at least one<br>
> of the following conditions is true:<br>
> (1) both index terms in the GEP are constant. In this case no new add<br>
> instruction is created, instead the constants are folded.<br>
> (2) the GEPs are in the same BB.<br>
> (3) LoopInfo is available, and we know we're not creating a new<br>
> instruction in a (deeper) loop.<br>
><br>
><br>
><br>
> What do you think?<br>
><br>
><br>
> Mark<br>
><br>
><br>
><br>
><br>
<br>
</div></div><div class="HOEnZb"><div class="h5">--<br>
Hal Finkel<br>
Assistant Computational Scientist<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
</div></div></blockquote></div><br></div></div>