Skip to content
  1. Nov 14, 2011
    • Chandler Carruth's avatar
      Begin chipping away at one of the biggest quadratic-ish behaviors in · 0af6a0bb
      Chandler Carruth authored
      this pass. We're leaving already merged blocks on the worklist, and
      scanning them again and again only to determine each time through that
      indeed they aren't viable. We can instead remove them once we're going
      to have to scan the worklist. This is the easy way to implement removing
      them. If this remains on the profile (as I somewhat suspect it will), we
      can get a lot more clever here, as the worklist's order is essentially
      irrelevant. We can use swapping and fold the two loops to reduce
      overhead even when there are many blocks on the worklist but only a few
      of them are removed.
      
      llvm-svn: 144531
      0af6a0bb
    • Chandler Carruth's avatar
      Under the hood, MBPI is doing a linear scan of every successor every · 84cd44c7
      Chandler Carruth authored
      time it is queried to compute the probability of a single successor.
      This makes computing the probability of every successor of a block in
      sequence... really really slow. ;] This switches to a linear walk of the
      successors rather than a quadratic one. One of several quadratic
      behaviors slowing this pass down.
      
      I'm not really thrilled with moving the sum code into the public
      interface of MBPI, but I don't (at the moment) have ideas for a better
      interface. My direction I'm thinking in for a better interface is to
      have MBPI actually retain much more state and make *all* of these
      queries cheap. That's a lot of work, and would require invasive changes.
      Until then, this seems like the least bad (ie, least quadratic)
      solution. Suggestions welcome.
      
      llvm-svn: 144530
      84cd44c7
    • Chandler Carruth's avatar
      Teach machine block placement to cope with unnatural loops. These don't · 1071cfa4
      Chandler Carruth authored
      get loop info structures associated with them, and so we need some way
      to make forward progress selecting and placing basic blocks. The
      technique used here is pretty brutal -- it just scans the list of blocks
      looking for the first unplaced candidate. It keeps placing blocks like
      this until the CFG becomes tractable.
      
      The cost is somewhat unfortunate, it requires allocating a vector of all
      basic block pointers eagerly. I have some ideas about how to simplify
      and optimize this, but I'm trying to get the logic correct first.
      
      Thanks to Benjamin Kramer for the reduced test case out of GCC. Sadly
      there are other bugs that GCC is tickling that I'm reducing and working
      on now.
      
      llvm-svn: 144516
      1071cfa4
  2. Nov 13, 2011
    • Chandler Carruth's avatar
      Cleanup some 80-columns violations and poor formatting. These snuck by · c4a2cb34
      Chandler Carruth authored
      when I was reading through the code for style.
      
      llvm-svn: 144513
      c4a2cb34
    • Chandler Carruth's avatar
      Enhance the assertion mechanisms in place to make it easier to catch · 8e1d9067
      Chandler Carruth authored
      when we fail to place all the blocks of a loop. Currently this is
      happening for unnatural loops, and this logic helps more immediately
      point to the problem.
      
      llvm-svn: 144504
      8e1d9067
    • Chandler Carruth's avatar
      Teach MBP to force-merge layout successors for blocks with unanalyzable · 0bb42c0f
      Chandler Carruth authored
      branches that also may involve fallthrough. In the case of blocks with
      no fallthrough, we can still re-order the blocks profitably. For example
      instruction decoding will in some cases continue past an indirect jump,
      making laying out its most likely successor there profitable.
      
      Note, no test case. I don't know how to write a test case that exercises
      this logic, but it matches the described desired semantics in
      discussions with Jakob and others. If anyone has a nice example of IR
      that will trigger this, that would be lovely.
      
      Also note, there are still assertion failures in real world code with
      this. I'm digging into those next, now that I know this isn't the cause.
      
      llvm-svn: 144499
      0bb42c0f
    • Chandler Carruth's avatar
      Hoist another gross nested loop into a helper method. · f9213fe7
      Chandler Carruth authored
      llvm-svn: 144498
      f9213fe7
    • Chandler Carruth's avatar
      Add a missing doxygen comment for a helper method. · eb4ec3ae
      Chandler Carruth authored
      llvm-svn: 144497
      eb4ec3ae
    • Chandler Carruth's avatar
      Hoist a nested loop into its own method. · b336172f
      Chandler Carruth authored
      llvm-svn: 144496
      b336172f
    • Chandler Carruth's avatar
      Rewrite #3 of machine block placement. This is based somewhat on the · 8d150789
      Chandler Carruth authored
      second algorithm, but only loosely. It is more heavily based on the last
      discussion I had with Andy. It continues to walk from the inner-most
      loop outward, but there is a key difference. With this algorithm we
      ensure that as we visit each loop, the entire loop is merged into
      a single chain. At the end, the entire function is treated as a "loop",
      and merged into a single chain. This chain forms the desired sequence of
      blocks within the function. Switching to a single algorithm removes my
      biggest problem with the previous approaches -- they had different
      behavior depending on which system triggered the layout. Now there is
      exactly one algorithm and one basis for the decision making.
      
      The other key difference is how the chain is formed. This is based
      heavily on the idea Andy mentioned of keeping a worklist of blocks that
      are viable layout successors based on the CFG. Having this set allows us
      to consistently select the best layout successor for each block. It is
      expensive though.
      
      The code here remains very rough. There is a lot that needs to be done
      to clean up the code, and to make the runtime cost of this pass much
      lower. Very much WIP, but this was a giant chunk of code and I'd rather
      folks see it sooner than later. Everything remains behind a flag of
      course.
      
      I've added a couple of tests to exercise the issues that this iteration
      was motivated by: loop structure preservation. I've also fixed one test
      that was exhibiting the broken behavior of the previous version.
      
      llvm-svn: 144495
      8d150789
  3. Nov 02, 2011
    • Chandler Carruth's avatar
      Begin collecting some of the statistics for block placement discussed on · ae4e800c
      Chandler Carruth authored
      the mailing list. Suggestions for other statistics to collect would be
      awesome. =]
      
      Currently these are implemented as a separate pass guarded by a separate
      flag. I'm not thrilled by that, but I wanted to be able to collect the
      statistics for the old code placement as well as the new in order to
      have a point of comparison. I'm planning on folding them into the single
      pass if / when there is only one pass of interest.
      
      llvm-svn: 143537
      ae4e800c
  4. Oct 24, 2011
  5. Oct 23, 2011
    • Chandler Carruth's avatar
      Now that we have comparison on probabilities, add some static functions · fd7475e9
      Chandler Carruth authored
      to get important constant branch probabilities and use them for finding
      the best branch out of a set of possibilities.
      
      llvm-svn: 142762
      fd7475e9
    • Chandler Carruth's avatar
      Remove a commented out line of code that snuck by my auditing. · 446210b6
      Chandler Carruth authored
      llvm-svn: 142761
      446210b6
    • Chandler Carruth's avatar
      Completely re-write the algorithm behind MachineBlockPlacement based on · bd1be4d0
      Chandler Carruth authored
      discussions with Andy. Fundamentally, the previous algorithm is both
      counter productive on several fronts and prioritizing things which
      aren't necessarily the most important: static branch prediction.
      
      The new algorithm uses the existing loop CFG structure information to
      walk through the CFG itself to layout blocks. It coalesces adjacent
      blocks within the loop where the CFG allows based on the most likely
      path taken. Finally, it topologically orders the block chains that have
      been formed. This allows it to choose a (mostly) topologically valid
      ordering which still priorizes fallthrough within the structural
      constraints.
      
      As a final twist in the algorithm, it does violate the CFG when it
      discovers a "hot" edge, that is an edge that is more than 4x hotter than
      the competing edges in the CFG. These are forcibly merged into
      a fallthrough chain.
      
      Future transformations that need te be added are rotation of loop exit
      conditions to be fallthrough, and better isolation of cold block chains.
      I'm also planning on adding statistics to model how well the algorithm
      does at laying out blocks based on the probabilities it receives.
      
      The old tests mostly still pass, and I have some new tests to add, but
      the nested loops are still behaving very strangely. This almost seems
      like working-as-intended as it rotated the exit branch to be
      fallthrough, but I'm not convinced this is actually the best layout. It
      is well supported by the probabilities for loops we currently get, but
      those are pretty broken for nested loops, so this may change later.
      
      llvm-svn: 142743
      bd1be4d0
  6. Oct 21, 2011
    • Chandler Carruth's avatar
      Add loop aligning to MachineBlockPlacement based on review discussion so · 8b9737cb
      Chandler Carruth authored
      it's a bit more plausible to use this instead of CodePlacementOpt. The
      code for this was shamelessly stolen from CodePlacementOpt, and then
      trimmed down a bit. There doesn't seem to be much utility in returning
      true/false from this pass as we may or may not have rewritten all of the
      blocks. Also, the statistic of counting how many loops were aligned
      doesn't seem terribly important so I removed it. If folks would like it
      to be included, I'm happy to add it back.
      
      This was probably the most egregious of the missing features, and now
      I'm going to start gathering some performance numbers and looking at
      specific loop structures that have different layout between the two.
      
      Test is updated to include both basic loop alignment and nested loop
      alignment.
      
      llvm-svn: 142645
      8b9737cb
    • Chandler Carruth's avatar
      Implement a block placement pass based on the branch probability and · 10281425
      Chandler Carruth authored
      block frequency analyses. This differs substantially from the existing
      block-placement pass in LLVM:
      
      1) It operates on the Machine-IR in the CodeGen layer. This exposes much
         more (and more precise) information and opportunities. Also, the
         results are more stable due to fewer transforms ocurring after the
         pass runs.
      2) It uses the generalized probability and frequency analyses. These can
         model static heuristics, code annotation derived heuristics as well
         as eventual profile loading. By basing the optimization on the
         analysis interface it can work from any (or a combination) of these
         inputs.
      3) It uses a more aggressive algorithm, both building chains from tho
         bottom up to maximize benefit, and using an SCC-based walk to layout
         chains of blocks in a profitable ordering without O(N^2) iterations
         which the old pass involves.
      
      The pass is currently gated behind a flag, and not enabled by default
      because it still needs to grow some important features. Most notably, it
      needs to support loop aligning and careful layout of loop structures
      much as done by hand currently in CodePlacementOpt. Once it supports
      these, and has sufficient testing and quality tuning, it should replace
      both of these passes.
      
      Thanks to Nick Lewycky and Richard Smith for help authoring & debugging
      this, and to Jakob, Andy, Eric, Jim, and probably a few others I'm
      forgetting for reviewing and answering all my questions. Writing
      a backend pass is *sooo* much better now than it used to be. =D
      
      llvm-svn: 142641
      10281425
Loading