[llvm-dev] RFC Storing BB order in llvm::Instruction for faster local dominance

Chris Lattner via llvm-dev llvm-dev at lists.llvm.org
Tue Sep 25 22:45:46 PDT 2018


On Sep 25, 2018, at 2:35 PM, Reid Kleckner <rnk at google.com> wrote:
> On Tue, Sep 25, 2018 at 12:16 PM Sanjoy Das <sanjoy at playingwithpointers.com <mailto:sanjoy at playingwithpointers.com>> wrote:
> Let's not assume a dichotomy between storing a int64 in
> llvm::Instruction and bitwise tricks -- they're both ways of caching
> position within llvm::Instruction.  I think we first need to establish
> that we need such a cache in llvm::Instruction/llvm::BasicBlock at
> all.
> 
> Do you have a sense of where these expensive domtree queries are
> coming from?  Are they from a couple of key places or are they
> scattered throughout the pass pipeline?

+1.

> When I dug into the profile with WPA, most of the time was spent in DSE and memcpyopt, which call AAResults::callCapturesBefore, which calls llvm::PointerMayBeCapturedBefore. Neither pass in an OrderedBasicBlock, so they rebuild the OrderedBasicBlock in linear time on every query. These passes insert instructions, so it's not correct to simply create and reuse an OrderedBasicBlock at this level.

So this is one of the reasons I find your patch to be problematic: it isn’t fixing N^2 behavior, it is merely changing one N^2 situation for another.  AFAICT there are one of two possible cases in these sorts of transformations:

1) These transformations are iteratively removing or inserting instructions, which invalidate the ordering with your approach, causing subsequent queries to rebuild the equivalent of OrderedBasicBlock.

2) These transformations are not doing anything, in which case this is all wasted work.

As a next step, can you please instrument the calls to calls from DSE and/or memcpyopt and see how many of the ones for the huge basic blocks actually turn into a transformation in the code?  If there are zero, then we should just disable these optimizations for large blocks.  If there are a few improvements, then we should see how to characterize them and what the right solution is based on the sorts of information they need.

LLVM is occasionally maligned for the magic numbers like “6” in various optimizations that bound the work in various analyses (e.g. when computing masked bits) but they exist for very real reasons: the cases in which a large search will actually lead to benefits are marginal to non-existent, and the possibility for unbounded queries for core APIs cause all kinds of problems.

I see the dependence analysis queries as the same sort of situation: until something like MemorySSA is able to define away these dense queries, we should be using bounded searches (in this case larger than 6 but less than 11,000 :-).

It would be straight-forward to change llvm::BasicBlock to keep track of the number of instruction’s in it (doing so would not change any ilist algorithmic behavior that I’m aware of given the need to keep instruction parent pointers updated), and having that info would make it easy to cap linear passes like this or switch into local search modes.

The cost of such a thing is the risk of performance regressions.  We control that risk by doing some analysis of the payoff of these transformations on large blocks - if the payoff doesn’t exist then it is a simple answer.  The real question here is not whether DSE is able to eliminate a single store, it is whether eliminating that store actually leads to a measurable performance improvement in the generated code.  Given huge blocks, I suspect the answer is “definitely not”.

> As suggested in the bug, if we were to rewrite these passes to use MemorySSA, this bottleneck would go away. I rebased a patch to do that for DSE, but finishing it off and enabling it by default is probably out of scope for me.

Yes, understood, that would be a major change.  That said, changing the entire architecture of the compiler to work around this isn’t really appealing to me, I’d rather limit the problematic optimizations on the crazy large cases.

-Chris


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180925/ad774625/attachment.html>


More information about the llvm-dev mailing list