LLVM 24.0.0git
MemProfContextDisambiguation.h
Go to the documentation of this file.
1//==- MemProfContextDisambiguation.h - Context Disambiguation ----*- 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// Implements support for context disambiguation of allocation calls for
10// profile-guided memory optimization (MemPGO) using memprof metadata. See
11// implementation file for details.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
16#define LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
17
18#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/GlobalValue.h"
24#include "llvm/IR/PassManager.h"
26#include <functional>
27
28namespace llvm {
30class LLVMContext;
31class Module;
33
34inline constexpr StringLiteral MemProfCloneSuffix = ".memprof.";
35
36/// Returns true if \p GV is a MemProf context-disambiguated clone.
37inline bool isMemProfClone(const GlobalValue &GV) {
39}
40
42 : public OptionalPassInfoMixin<MemProfContextDisambiguation> {
43 /// Run the context disambiguator on \p M, returns true if any changes made.
44 bool processModule(
45 Module &M,
47
48 /// In the ThinLTO backend, apply the cloning decisions in ImportSummary to
49 /// the IR.
50 bool applyImport(Module &M);
51
52 // Builds the symtab and analysis used for ICP during ThinLTO backends.
53 bool initializeIndirectCallPromotionInfo(Module &M);
54
55 // Data structure for saving indirect call profile info for use in ICP with
56 // cloning.
57 struct ICallAnalysisData {
58 CallBase *CB;
59 std::vector<InstrProfValueData> CandidateProfileData;
60 uint32_t NumCandidates;
61 uint64_t TotalCount;
62 // The number of clones recorded for the call.
63 unsigned NumClones;
64 // The CallsiteInfo summary record for each profiled target in
65 // CandidateProfileData (in the same order), or null if there is none, in
66 // which case all clones of the call should call the original target.
68 };
69
70 // Record information needed for ICP of an indirect call, depending on its
71 // profile information and the clone information recorded in the
72 // CallsiteInfo records for its profiled targets. GetCallsite is invoked once
73 // for each profiled target, in order, and returns the corresponding record
74 // (or null if there is none). The number of clones recorded for this
75 // indirect call is returned (0 if there are no records). The necessary
76 // information is recorded in the ICallAnalysisInfo list for later ICP if any
77 // clone of the call should call a cloned target.
78 unsigned
79 recordICPInfo(CallBase *CB,
80 function_ref<const CallsiteInfo *(const InstrProfValueData &)>
81 GetCallsite,
82 SmallVector<ICallAnalysisData> &ICallAnalysisInfo);
83
84 // Actually performs any needed ICP in the function, using the information
85 // recorded in the ICallAnalysisInfo list.
86 void performICP(Module &M, ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
87 ArrayRef<ICallAnalysisData> ICallAnalysisInfo,
89
90 /// Import summary containing cloning decisions for the ThinLTO backend.
91 const ModuleSummaryIndex *ImportSummary;
92
93 // Owns the import summary specified by internal options for testing the
94 // ThinLTO backend via opt (to simulate distributed ThinLTO).
95 std::unique_ptr<ModuleSummaryIndex> ImportSummaryForTesting;
96
97 // Whether we are building with SamplePGO. This is needed for correctly
98 // updating profile metadata on speculatively promoted calls.
99 bool isSamplePGO;
100
101 // Used when performing indirect call analysis and promotion when cloning in
102 // the ThinLTO backend during applyImport.
103 std::unique_ptr<InstrProfSymtab> Symtab;
104 std::unique_ptr<ICallPromotionAnalysis> ICallAnalysis;
105 // Maps the aliasee GUID of each imported copy of an alias to that copy. Used
106 // by performICP to promote to an alias copy when the profiled target is its
107 // aliasee, which is not in the module. Lazily built on first use.
108 std::optional<DenseMap<GlobalValue::GUID, Function *>>
109 AliaseeGUIDToImportedAlias;
110
111public:
113 MemProfContextDisambiguation(const ModuleSummaryIndex *Summary = nullptr,
114 bool isSamplePGO = false);
115
117
118 LLVM_ABI void
119 run(ModuleSummaryIndex &Index,
121 isPrevailing,
122 LLVMContext &Ctx,
123 function_ref<void(StringRef, StringRef, const Twine &)> EmitRemark =
124 nullptr);
125};
126
127/// Strips MemProf attributes and metadata. Can be invoked by the pass pipeline
128/// when we don't have an index that has recorded that we are linking with
129/// allocation libraries containing the necessary APIs for downstream
130/// transformations.
131class MemProfRemoveInfo : public OptionalPassInfoMixin<MemProfRemoveInfo> {
132public:
134};
135
136} // end namespace llvm
137
138#endif // LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
Interface to identify indirect call promotion candidates.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function and variable summary information to aid decisions and implementation of importing.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
LLVM_ABI MemProfContextDisambiguation(const ModuleSummaryIndex *Summary=nullptr, bool isSamplePGO=false)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Strips MemProf attributes and metadata.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
The optimization diagnostic interface.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:880
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition StringRef.h:438
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
bool isMemProfClone(const GlobalValue &GV)
Returns true if GV is a MemProf context-disambiguated clone.
constexpr StringLiteral MemProfCloneSuffix
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Summary of memprof callsite metadata.
A CRTP mix-in for passes that can be skipped.