Skip to content

Commit 775c507

Browse files
authored
[ctx_prof] Flattened profile lowering pass (#107329)
Pass to flatten and lower the contextual profile to profile (i.e. `MD_prof`) metadata. This is expected to be used after all IPO transformations have happened. Prior to lowering, the instrumentation is maintained during IPO and the contextual profile is kept in sync (see PRs #105469, #106154). Flattening (#104539) sums up all the counters belonging to all a function's context nodes. We first propagate counter values (from the flattened profile) using the same propagation algorithm as `PGOUseFunc::populateCounters`, then map the edge values to `branch_weights`. Functions. in the module that don't have an entry in the flattened profile are deemed cold, and any `MD_prof` metadata they may have is reset. The profile summary is also reset at this point. Issue [#89287](#89287)
1 parent 6cb2d40 commit 775c507

File tree

9 files changed

+506
-3
lines changed

9 files changed

+506
-3
lines changed

llvm/include/llvm/ProfileData/ProfileCommon.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ class ProfileSummaryBuilder {
7979
class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
8080
uint64_t MaxInternalBlockCount = 0;
8181

82-
inline void addEntryCount(uint64_t Count);
83-
inline void addInternalCount(uint64_t Count);
84-
8582
public:
8683
InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
8784
: ProfileSummaryBuilder(std::move(Cutoffs)) {}
8885

86+
void addEntryCount(uint64_t Count);
87+
void addInternalCount(uint64_t Count);
88+
8989
void addRecord(const InstrProfRecord &);
9090
std::unique_ptr<ProfileSummary> getSummary();
9191
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- PGOCtxProfFlattening.h - Contextual Instr. Flattening ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares the PGOCtxProfFlattening class.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOCTXPROFFLATTENING_H
13+
#define LLVM_TRANSFORMS_INSTRUMENTATION_PGOCTXPROFFLATTENING_H
14+
15+
#include "llvm/IR/PassManager.h"
16+
namespace llvm {
17+
18+
class PGOCtxProfFlatteningPass
19+
: public PassInfoMixin<PGOCtxProfFlatteningPass> {
20+
public:
21+
explicit PGOCtxProfFlatteningPass() = default;
22+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
23+
};
24+
} // namespace llvm
25+
#endif

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
199199
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
200200
#include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h"
201+
#include "llvm/Transforms/Instrumentation/PGOCtxProfFlattening.h"
201202
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
202203
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
203204
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
7777
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
7878
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
79+
#include "llvm/Transforms/Instrumentation/PGOCtxProfFlattening.h"
7980
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
8081
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
8182
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ MODULE_PASS("coro-early", CoroEarlyPass())
5858
MODULE_PASS("cross-dso-cfi", CrossDSOCFIPass())
5959
MODULE_PASS("ctx-instr-gen",
6060
PGOInstrumentationGen(PGOInstrumentationType::CTXPROF))
61+
MODULE_PASS("ctx-prof-flatten", PGOCtxProfFlatteningPass())
6162
MODULE_PASS("deadargelim", DeadArgumentEliminationPass())
6263
MODULE_PASS("debugify", NewPMDebugifyPass())
6364
MODULE_PASS("dfsan", DataFlowSanitizerPass())

llvm/lib/Transforms/Instrumentation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_llvm_component_library(LLVMInstrumentation
1515
InstrProfiling.cpp
1616
KCFI.cpp
1717
LowerAllowCheckPass.cpp
18+
PGOCtxProfFlattening.cpp
1819
PGOCtxProfLowering.cpp
1920
PGOForceFunctionAttrs.cpp
2021
PGOInstrumentation.cpp

0 commit comments

Comments
 (0)