[all-commits] [llvm/llvm-project] e5581d: [SimplifyCFG] accumulate bonus insts cost

Yaxun (Sam) Liu via All-commits all-commits at lists.llvm.org
Sun Sep 18 17:24:15 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: e5581df60a35fffb0c69589777e4e126c849405f
      https://github.com/llvm/llvm-project/commit/e5581df60a35fffb0c69589777e4e126c849405f
  Author: Yaxun (Sam) Liu <yaxun.liu at amd.com>
  Date:   2022-09-18 (Sun, 18 Sep 2022)

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

  Log Message:
  -----------
  [SimplifyCFG] accumulate bonus insts cost

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 destinations, 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 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 ValueMap 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.

Reviewed by: Artem Belevich, Florian Hahn, Nikita Popov, Matt Arsenault

Differential Revision: https://reviews.llvm.org/D132408




More information about the All-commits mailing list