clang 20.0.0git
CodeGenPGO.h
Go to the documentation of this file.
1//===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- 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// Instrumentation-based profile-guided optimization
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
15
16#include "CGBuilder.h"
17#include "CodeGenModule.h"
18#include "CodeGenTypes.h"
19#include "MCDCState.h"
20#include "llvm/ProfileData/InstrProfReader.h"
21#include <array>
22#include <memory>
23#include <optional>
24
25namespace clang {
26namespace CodeGen {
27
28/// Per-function PGO state.
30private:
31 CodeGenModule &CGM;
32 std::string FuncName;
33 llvm::GlobalVariable *FuncNameVar;
34
35 std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
36 unsigned NumRegionCounters;
37 uint64_t FunctionHash;
38 std::unique_ptr<llvm::DenseMap<const Stmt *, CounterPair>> RegionCounterMap;
39 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
40 std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
41 std::unique_ptr<MCDC::State> RegionMCDCState;
42 std::vector<uint64_t> RegionCounts;
43 uint64_t CurrentRegionCount;
44
45public:
47 : CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}),
48 NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {}
49
50 /// Whether or not we have PGO region data for the current function. This is
51 /// false both when we have no data at all and when our data has been
52 /// discarded.
53 bool haveRegionCounts() const { return !RegionCounts.empty(); }
54
55 /// Return the counter value of the current region.
56 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
57
58 /// Set the counter value for the current region. This is used to keep track
59 /// of changes to the most recent counter from control flow and non-local
60 /// exits.
61 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
62
63 /// Check if an execution count is known for a given statement. If so, return
64 /// true and put the value in Count; else return false.
65 std::optional<uint64_t> getStmtCount(const Stmt *S) const {
66 if (!StmtCountMap)
67 return std::nullopt;
68 auto I = StmtCountMap->find(S);
69 if (I == StmtCountMap->end())
70 return std::nullopt;
71 return I->second;
72 }
73
74 /// If the execution count for the current statement is known, record that
75 /// as the current count.
76 void setCurrentStmt(const Stmt *S) {
77 if (auto Count = getStmtCount(S))
79 }
80
81 /// Assign counters to regions and configure them for PGO of a given
82 /// function. Does nothing if instrumentation is not enabled and either
83 /// generates global variables or associates PGO data with each of the
84 /// counters depending on whether we are generating or using instrumentation.
85 void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
86 /// Emit a coverage mapping range with a counter zero
87 /// for an unused declaration.
88 void emitEmptyCounterMapping(const