[all-commits] [llvm/llvm-project] a7b662: [BranchProbabilityInfo] Fix eraseBlock

kazutakahirata via All-commits all-commits at lists.llvm.org
Tue Oct 27 16:14:57 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: a7b662d0f4098371b96ce4446fb0eba79b0b649f
      https://github.com/llvm/llvm-project/commit/a7b662d0f4098371b96ce4446fb0eba79b0b649f
  Author: Kazu Hirata <kazu at google.com>
  Date:   2020-10-27 (Tue, 27 Oct 2020)

  Changed paths:
    M llvm/include/llvm/Analysis/BranchProbabilityInfo.h
    M llvm/lib/Analysis/BranchProbabilityInfo.cpp

  Log Message:
  -----------
  [BranchProbabilityInfo] Fix eraseBlock

This patch ensures that BranchProbabilityInfo::eraseBlock(BB) deletes
all entries in Probs associated with with BB.

Without this patch, stale entries for BB may remain in Probs after
eraseBlock(BB), leading to a situation where a newly created basic
block has an edge probability associated with it even before the pass
responsible for creating the basic block adds any edge probability to
it.

Consider the current implementation of eraseBlock(BB):

  for (const_succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
    auto MapI = Probs.find(std::make_pair(BB, I.getSuccessorIndex()));
    if (MapI != Probs.end())
      Probs.erase(MapI);
  }

Notice that it uses succ_begin(BB) and succ_end(BB), which are based
on BB->getTerminator().  This means that if the terminator changes
between calls to setEdgeProbability and eraseBlock, then we may not
examine all pairs associated with BB.

This is exactly what happens in MaybeMergeBasicBlockIntoOnlyPred,
which merges basic blocks A into B if A is the sole predecessor of B,
and B is the sole successor of A.  It replaces the terminator of A
with UnreachableInst before (indirectly) calling eraseBlock(A).

The patch fixes the problem by keeping track of all edge probablities
entered with setEdgeProbability in a map from BasicBlock* to a
successor index.

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




More information about the All-commits mailing list