[PATCH] D132408: [SimplifyCFG] accumuate bonus insts cost

Yaxun Liu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 22 12:59:35 PDT 2022


yaxunl created this revision.
yaxunl added reviewers: tra, rampitec, ruiling, arsenm, bcahoon, aeubanks, lebedev.ri.
Herald added subscribers: zzheng, hiraditya.
Herald added a project: All.
yaxunl requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.

SimplifyCFG folds

  bool foo() {
  if (cond1) return false;
  if (cond2) return false;
  return true;
  }

as

  bool foo() {
    if (cond1 || cond2) return false
    return true;

'cond2' is called 'bonus insts' in branch folding since they introduce overhead
since the original CFG could do early exit but the folded CFG always executes
them. SimplifyCFG calculates the costs of 'bonus insts' of a folding a BB into
its predecessor BB which shares the destination. If it is below bonus-inst-threshold,
SimplifyCFG will fold that BB into its predecessor and cond2 will always be executed.

When SimplifyCFG calculates the cost of 'bonus insts', it only consider 'bonus' insts
in the current BB to be considered for folding. This causes issue for unrolled loops
which share destination, e.g.

  bool foo(int *a) {
    for (int i = 0; i < 32; i++)
      if (a[i] > 0) return false;
    return true;
  }

After unrolling, it becomes

  bool foo(int *a) {
    if(a[0]>0) return false
    if(a[1]>0) return false;
    //...
    if(a[31]>0) return false;
    return true;
  }

SimplifyCFG will merge each BB with its predecessor BB,
and ends up with 32 'bonus insts' which are always executed, which
is much more slower than the original CFG.

The root cause is that SimplifyCFG does not consider the
accumulated cost of  'bonus insts' which are folded from
different BB's.

This patch fixes that by introducing a DenseMap to track
costs of 'bonus insts' coming from different BB's into
the same BB, and cuts off if the accumulated cost
exceeds a threshold.


https://reviews.llvm.org/D132408

Files:
  llvm/include/llvm/Transforms/Utils/Local.h
  llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
  llvm/lib/Transforms/Utils/LoopSimplify.cpp
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/LoopUnroll/peel-loop-inner.ll
  llvm/test/Transforms/PhaseOrdering/X86/vector-reductions-logical.ll
  llvm/test/Transforms/SimplifyCFG/branch-fold-threshold.ll
  llvm/test/Transforms/SimplifyCFG/branch-fold-unrolled-loop.ll
  llvm/test/Transforms/SimplifyCFG/fold-branch-to-common-dest-two-preds-cost.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132408.454595.patch
Type: text/x-patch
Size: 17545 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220822/32aa1e58/attachment.bin>


More information about the llvm-commits mailing list