
This reduces the size of chrome.dll.pdb built with optimizations, coverage, and line table info from 4,690,210,816 to 2,181,128,192, which makes it possible to fit under the 4GB limit. This change can greatly reduce binary size in coverage builds, which do not need value profiling. IR PGO builds are unaffected. There is a minor behavior change for frontend PGO. PGO and coverage both use InstrProfiling to create profile data with counters. PGO records the address of each function in the __profd_ global. It is used later to map runtime function pointer values back to source-level function names. Coverage does not appear to use this information. Recording the address of every function with code coverage drastically increases code size. Consider this program: void foo(); void bar(); inline void inlineMe(int x) { if (x > 0) foo(); else bar(); } int getVal(); int main() { inlineMe(getVal()); } With code coverage, the InstrProfiling pass runs before inlining, and it captures the address of inlineMe in the __profd_ global. This greatly increases code size, because now the compiler can no longer delete trivial code. One downside to this approach is that users of frontend PGO must apply the -mllvm -enable-value-profiling flag globally in TUs that enable PGO. Otherwise, some inline virtual method addresses may not be recorded and will not be able to be promoted. My assumption is that this mllvm flag is not popular, and most frontend PGO users don't enable it. Differential Revision: https://reviews.llvm.org/D102818
124 lines
4.5 KiB
C++
124 lines
4.5 KiB
C++
//===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Instrumentation-based profile-guided optimization
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
|
|
#define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
|
|
|
|
#include "CGBuilder.h"
|
|
#include "CodeGenModule.h"
|
|
#include "CodeGenTypes.h"
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
|
#include <array>
|
|
#include <memory>
|
|
|
|
namespace clang {
|
|
namespace CodeGen {
|
|
|
|
/// Per-function PGO state.
|
|
class CodeGenPGO {
|
|
private:
|
|
CodeGenModule &CGM;
|
|
std::string FuncName;
|
|
llvm::GlobalVariable *FuncNameVar;
|
|
|
|
std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
|
|
unsigned NumRegionCounters;
|
|
uint64_t FunctionHash;
|
|
std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
|
|
std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
|
|
std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
|
|
std::vector<uint64_t> RegionCounts;
|
|
uint64_t CurrentRegionCount;
|
|
|
|
public:
|
|
CodeGenPGO(CodeGenModule &CGModule)
|
|
: CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}),
|
|
NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {}
|
|
|
|
/// Whether or not we have PGO region data for the current function. This is
|
|
/// false both when we have no data at all and when our data has been
|
|
/// discarded.
|
|
bool haveRegionCounts() const { return !RegionCounts.empty(); }
|
|
|
|
/// Return the counter value of the current region.
|
|
uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
|
|
|
|
/// Set the counter value for the current region. This is used to keep track
|
|
/// of changes to the most recent counter from control flow and non-local
|
|
/// exits.
|
|
void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
|
|
|
|
/// Check if an execution count is known for a given statement. If so, return
|
|
/// true and put the value in Count; else return false.
|
|
Optional<uint64_t> getStmtCount(const Stmt *S) const {
|
|
if (!StmtCountMap)
|
|
return None;
|
|
auto I = StmtCountMap->find(S);
|
|
if (I == StmtCountMap->end())
|
|
return None;
|
|
return I->second;
|
|
}
|
|
|
|
/// If the execution count for the current statement is known, record that
|
|
/// as the current count.
|
|
void setCurrentStmt(const Stmt *S) {
|
|
if (auto Count = getStmtCount(S))
|
|
setCurrentRegionCount(*Count);
|
|
}
|
|
|
|
/// Assign counters to regions and configure them for PGO of a given
|
|
/// function. Does nothing if instrumentation is not enabled and either
|
|
/// generates global variables or associates PGO data with each of the
|
|
/// counters depending on whether we are generating or using instrumentation.
|
|
void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
|
|
/// Emit a coverage mapping range with a counter zero
|
|
/// for an unused declaration.
|
|
void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
|
|
llvm::GlobalValue::LinkageTypes Linkage);
|
|
// Insert instrumentation or attach profile metadata at value sites
|
|
void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
|
|
llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
|
|
|
|
// Set a module flag indicating if value profiling is enabled.
|
|
void setValueProfilingFlag(llvm::Module &M);
|
|
|
|
private:
|
|
void setFuncName(llvm::Function *Fn);
|
|
void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
|
|
void mapRegionCounters(const Decl *D);
|
|
void computeRegionCounts(const Decl *D);
|
|
void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
|
|
llvm::Function *Fn);
|
|
void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
|
|
bool IsInMainFile);
|
|
bool skipRegionMappingForDecl(const Decl *D);
|
|
void emitCounterRegionMapping(const Decl *D);
|
|
|
|
public:
|
|
void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
|
|
llvm::Value *StepV);
|
|
|
|
/// Return the region count for the counter at the given index.
|
|
uint64_t getRegionCount(const Stmt *S) {
|
|
if (!RegionCounterMap)
|
|
return 0;
|
|
if (!haveRegionCounts())
|
|
return 0;
|
|
return RegionCounts[(*RegionCounterMap)[S]];
|
|
}
|
|
};
|
|
|
|
} // end namespace CodeGen
|
|
} // end namespace clang
|
|
|
|
#endif
|