[CGSCC] Add pass which counts the max number of times we visit a function

This will help with finding potential pathological CGSCC cases.

Reviewed By: asbirlea

Differential Revision: https://reviews.llvm.org/D142853
This commit is contained in:
Arthur Eubanks 2023-01-29 15:48:04 -08:00
parent 4e607ec498
commit 4ce34bb2a9
8 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,28 @@
//===- CountVisits.h --------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TRANSFORMS_UTILS_COUNT_VISITS_H
#define LLVM_TRANSFORMS_UTILS_COUNT_VISITS_H
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/PassManager.h"
namespace llvm {
class Function;
struct CountVisitsPass : PassInfoMixin<CountVisitsPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &);
private:
StringMap<uint32_t> Counts;
};
} // end namespace llvm
#endif // LLVM_TRANSFORMS_UTILS_COUNT_VISITS_H

View File

@ -227,6 +227,7 @@
#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
#include "llvm/Transforms/Utils/CanonicalizeFreezeInLoops.h"
#include "llvm/Transforms/Utils/CountVisits.h"
#include "llvm/Transforms/Utils/Debugify.h"
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
#include "llvm/Transforms/Utils/FixIrreducible.h"

View File

@ -118,6 +118,7 @@
#include "llvm/Transforms/Utils/AddDiscriminators.h"
#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
#include "llvm/Transforms/Utils/CountVisits.h"
#include "llvm/Transforms/Utils/InjectTLIMappings.h"
#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
#include "llvm/Transforms/Utils/Mem2Reg.h"
@ -262,6 +263,11 @@ static cl::opt<bool>
EnableMatrix("enable-matrix", cl::init(false), cl::Hidden,
cl::desc("Enable lowering of the matrix intrinsics"));
static cl::opt<bool> CountCGSCCVisits(
"count-cgscc-max-visits", cl::init(false), cl::Hidden,
cl::desc("Keep track of the max number of times we visit a function in the "
"CGSCC pipeline as a statistic"));
static cl::opt<bool> EnableConstraintElimination(
"enable-constraint-elimination", cl::init(false), cl::Hidden,
cl::desc(
@ -322,6 +328,9 @@ PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level,
FunctionPassManager FPM;
if (CountCGSCCVisits)
FPM.addPass(CountVisitsPass());
// Form SSA out of local memory accesses after breaking apart aggregates into
// scalars.
FPM.addPass(SROAPass(SROAOptions::ModifyCFG));
@ -472,6 +481,9 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
FunctionPassManager FPM;
if (CountCGSCCVisits)
FPM.addPass(CountVisitsPass());
// Form SSA out of local memory accesses after breaking apart aggregates into
// scalars.
FPM.addPass(SROAPass(SROAOptions::ModifyCFG));

View File

@ -273,6 +273,7 @@ FUNCTION_PASS("bounds-checking", BoundsCheckingPass())
FUNCTION_PASS("break-crit-edges", BreakCriticalEdgesPass())
FUNCTION_PASS("callsite-splitting", CallSiteSplittingPass())
FUNCTION_PASS("consthoist", ConstantHoistingPass())
FUNCTION_PASS("count-visits", CountVisitsPass())
FUNCTION_PASS("constraint-elimination", ConstraintEliminationPass())
FUNCTION_PASS("chr", ControlHeightReductionPass())
FUNCTION_PASS("coro-elide", CoroElidePass())

View File

@ -17,6 +17,7 @@ add_llvm_component_library(LLVMTransformUtils
CodeLayout.cpp
CodeMoverUtils.cpp
CtorUtils.cpp
CountVisits.cpp
Debugify.cpp
DemoteRegToStack.cpp
EntryExitInstrumenter.cpp

View File

@ -0,0 +1,25 @@
//===- CountVisits.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/CountVisits.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/PassManager.h"
using namespace llvm;
#define DEBUG_TYPE "count-visits"
STATISTIC(MaxVisited, "Max number of times we visited a function");
PreservedAnalyses CountVisitsPass::run(Function &F, FunctionAnalysisManager &) {
uint32_t Count = Counts[F.getName()] + 1;
Counts[F.getName()] = Count;
if (Count > MaxVisited)
MaxVisited = Count;
return PreservedAnalyses::all();
}

View File

@ -0,0 +1,20 @@
; RUN: opt -passes=count-visits -stats 2>&1 -disable-output < %s | FileCheck %s --check-prefix=ONE
; RUN: opt -passes='cgscc(count-visits)' -stats 2>&1 -disable-output < %s | FileCheck %s --check-prefix=ONE
; RUN: opt -passes='cgscc(count-visits,instcombine)' -stats 2>&1 -disable-output < %s | FileCheck %s --check-prefix=TWO
; RUN: opt -passes='default<O1>' -count-cgscc-max-visits -stats 2>&1 -disable-output < %s | FileCheck %s --check-prefix=PIPELINE
; RUN: opt -passes='default<O3>' -count-cgscc-max-visits -stats 2>&1 -disable-output < %s | FileCheck %s --check-prefix=PIPELINE
; ONE: 1 count-visits - Max number of times we visited a function
; TWO: 2 count-visits - Max number of times we visited a function
; PIPELINE: count-visits - Max number of times we visited a function
define void @f() {
%a = bitcast ptr @g to ptr
call void %a()
ret void
}
define void @g() {
call void @f()
ret void
}

View File

@ -11,6 +11,7 @@ static_library("Utils") {
"ASanStackFrameLayout.cpp",
"AddDiscriminators.cpp",
"AssumeBundleBuilder.cpp",
"CountVisits.cpp",
"BasicBlockUtils.cpp",
"BreakCriticalEdges.cpp",
"BuildLibCalls.cpp",