[llvm] 4ce34bb - [CGSCC] Add pass which counts the max number of times we visit a function
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 30 10:10:38 PST 2023
Author: Arthur Eubanks
Date: 2023-01-30T10:06:53-08:00
New Revision: 4ce34bb2a9c8c2ae21c4d67f8af9046fe1ca434c
URL: https://github.com/llvm/llvm-project/commit/4ce34bb2a9c8c2ae21c4d67f8af9046fe1ca434c
DIFF: https://github.com/llvm/llvm-project/commit/4ce34bb2a9c8c2ae21c4d67f8af9046fe1ca434c.diff
LOG: [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
Added:
llvm/include/llvm/Transforms/Utils/CountVisits.h
llvm/lib/Transforms/Utils/CountVisits.cpp
llvm/test/Other/count-visits.ll
Modified:
llvm/lib/Passes/PassBuilder.cpp
llvm/lib/Passes/PassBuilderPipelines.cpp
llvm/lib/Passes/PassRegistry.def
llvm/lib/Transforms/Utils/CMakeLists.txt
llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/Utils/CountVisits.h b/llvm/include/llvm/Transforms/Utils/CountVisits.h
new file mode 100644
index 0000000000000..7000afbc4985b
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Utils/CountVisits.h
@@ -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
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index e251d56463a3f..4b8754df7fb63 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -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"
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index eed29c25714b0..72432987725ac 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -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));
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 10af4160c5452..73ab87dd88236 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -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())
diff --git a/llvm/lib/Transforms/Utils/CMakeLists.txt b/llvm/lib/Transforms/Utils/CMakeLists.txt
index 0edd42b9efa82..6663ce6c4b0ac 100644
--- a/llvm/lib/Transforms/Utils/CMakeLists.txt
+++ b/llvm/lib/Transforms/Utils/CMakeLists.txt
@@ -17,6 +17,7 @@ add_llvm_component_library(LLVMTransformUtils
CodeLayout.cpp
CodeMoverUtils.cpp
CtorUtils.cpp
+ CountVisits.cpp
Debugify.cpp
DemoteRegToStack.cpp
EntryExitInstrumenter.cpp
diff --git a/llvm/lib/Transforms/Utils/CountVisits.cpp b/llvm/lib/Transforms/Utils/CountVisits.cpp
new file mode 100644
index 0000000000000..4faded8fc6563
--- /dev/null
+++ b/llvm/lib/Transforms/Utils/CountVisits.cpp
@@ -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();
+}
diff --git a/llvm/test/Other/count-visits.ll b/llvm/test/Other/count-visits.ll
new file mode 100644
index 0000000000000..48ebb6880d469
--- /dev/null
+++ b/llvm/test/Other/count-visits.ll
@@ -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
+}
diff --git a/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn b/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn
index 3d4f49b5d3599..b3d84fb813036 100644
--- a/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn
+++ b/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn
@@ -11,6 +11,7 @@ static_library("Utils") {
"ASanStackFrameLayout.cpp",
"AddDiscriminators.cpp",
"AssumeBundleBuilder.cpp",
+ "CountVisits.cpp",
"BasicBlockUtils.cpp",
"BreakCriticalEdges.cpp",
"BuildLibCalls.cpp",
More information about the llvm-commits
mailing list