[llvm-dev] [RFC][InlineCost] Modeling JumpThreading (or similar) in inline cost model

Daniel Berlin via llvm-dev llvm-dev at lists.llvm.org
Fri Aug 4 11:06:26 PDT 2017


A few notes:
I'm a bit surprised IPO copy/constant propagation doesn't get this case,
but i didn't look if the lattice supports variables.
In particular, in your example, given no other call sites, it should
eliminate the dead code.
(In a real program, it may require cloning).

GCC will do IPA-CP/const-prop with cloning, and i'm wildly curious if new
GCC's catch this case for you at higher optimization levels?

If so, it may be worth not looking at this as an inlining problem, but as
an area we need IPO infrastructure improvement

Otherwise,  a couple things:
Approximate dominators (for example, semi-dominators) can be computed fast
(a DFS walk of the CFG with no real additional computation)
Except in strange CFGs that jump around a lot, they are the dominators.

More importantly, the dominator is either the sdom or a proper ancestor of
the sdom.

The practical impact of this is that if you use them as if they were
dominators, the set of conditions you discover will not be "too wrong".
Occasionally wrong, but mostly not.

My guess is the cost of doing approximate dominators is ~50-60% of the cost
of doing dominators.  Nowadays, about half the time was in the DFS walk,
the other half in the computation. At best, it would be 2-3x faster.
I've no idea if this changes whether we'd want dominators, approximate
dominators, or stick with nothing.

If you have some sort of dominatorish tree could then just use earlycse's
method of dominating condition finding:
Process in "dom tree" top-down order, push the equivalences you see, pop
the relevant ones when you exit the relevant dom tree scope.

In practice, you'd only check comparisons against the hash table.

The other option is PredicateInfo, but it requires dominators and modifies
the IR.

My guess is this is undesired/too heavyweight for inline cost analysis,
however the basic principle on how it renames things could also be applied
without IR changing for this specific case.  Unlike the EarlyCSE method,
which is O(all instructons) PredicateInfo is O(branches + number of uses of
variables affected by conditions)  Without going into futher details, if
all you care about is "for each condition, give me the set of possibly
affected variables" (so you can see if they may simplify), we could do that
very very quickly (as fast as we can sort a vector). But it does require
dominators.




On Fri, Aug 4, 2017 at 8:56 AM, Chad Rosier <mcrosier at codeaurora.org> wrote:

> All,
>
> I'm working on an improvement to the inline cost model, but I'm unsure how
> to proceed.  Let me begin by first describing the problem I'm trying to
> solve.  Consider the following pseudo C code:
>
>
>
>
> *typedef struct element {   unsigned idx; } element_t; *
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *static inline unsigned char fn2 (element_t *dst_ptr, const element_t
> *a_ptr,                    const element_t *b_ptr, unsigned char changed) {
>   if (a_ptr && b_ptr && a_ptr->idx == b_ptr->idx) {     if (!changed &&
> dst_ptr && dst_ptr->idx == a_ptr->idx) {       /* Do something */     }
> else {       changed = 1;       if (!dst_ptr)         dst_ptr = fn3();
>       else         dst_ptr->idx = a_ptr->idx;       /* Do something. */
> }   } else {     changed = fn4();   }   return changed; } unsigned char fn1
> (element_t *a_ptr, element_t *b_ptr) {   unsigned char changed = 0;   while
> (b_ptr) {     if (!a_ptr || a_ptr->idx == b_ptr->idx) {       changed = fn2
> (a_ptr, a_ptr, b_ptr, changed);       b_ptr = b_ptr->next;     }   }
> return changed; }*
>
> When the inline cost model computes the inline cost of fn2 it ends up
> being much higher than the inline threshold.  A fair amount of the cost is
> due to the inlining of fn3 into fn2.  However, if fn2 had been inlined into
> fn1 the code from fn3 would have been removed as dead/unreachable.
>
> At the fn2 call site notice the first two arguments are equal.  Thus, in
> the context of fn2 dst_ptr and a_ptr are equal.  The call site of fn3 is
> predicated on dst_ptr being null (i.e., if (!dst_ptr) dst_ptr = fn3()), but
> that code is predicated on a_ptr being non-null.  Therefore, we know the
> condition !dst_ptr is false (because a_ptr == dst_ptr and a_ptr is
> non-null) and the call to fn3 is dead.  I suspect one of JumpThreading,
> EarlyCSE, or GVN does the elimination after inlining, so that's what I'd
> like to try and model in the inline cost model.  (Note fn2 has multiple
> call sides and the property that the first and second arguments are equal
> isn't true for each call site, so something like IPSCCP doesn't actually
> help, AFAICT).
>
> My first attempt at solving this problem did something similar to what is
> done in JumpThreadingPass::ProcessImpliedCondition().  Specifically, it
> tried to prove that dst_ptr was non-null based on a dominating condition.
> The only tricky parts were to deal with hammocks/diamonds when walking up
> the CFG (See: https://reviews.llvm.org/D36287 as a concrete example of
> how I proposed to get an immediate dominator without the domtree) and to
> account for the fact that dst_ptr and a_ptr are equal.
>
> I'm pretty sure I can get this approach to work, however, I'm not
> convinced it's really extensible or general.  Should we consider using the
> full dominator tree in the inline cost model to capture this?
>
> If you have any thoughts on how to tackle the problem, I would love to
> hear your feedback!
>
>  Chad
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170804/f8b349fb/attachment.html>


More information about the llvm-dev mailing list