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;
33namespace cl {
35}
36
37inline constexpr StringLiteral MemProfCloneSuffix = ".memprof.";
38
39/// Returns true if \p GV is a MemProf context-disambiguated clone.
40inline bool isMemProfClone(const GlobalValue &GV) {
42}
43
45 : public OptionalPassInfoMixin<MemProfContextDisambiguation> {
46 /// Run the context disambiguator on \p M, returns true if any changes made.
47 bool processModule(
48 Module &M,
50
51 /// In the ThinLTO backend, apply the cloning decisions in \p Summary to the
52 /// IR.
53 bool applyImport(Module &M, const ModuleSummaryIndex *Summary);
54
55 // Builds the symtab and analysis used for ICP during ThinLTO backends.
56 bool initializeIndirectCallPromotionInfo(Module &M);
57
58 // Data structure for saving indirect call profile info for use in ICP with
59 // cloning.
60 struct ICallAnalysisData {
61 CallBase *CB;
62 std::vector<InstrProfValueData> CandidateProfileData;
63 uint32_t NumCandidates;
64 uint64_t TotalCount;
65 // The number of clones recorded for the call.
66 unsigned NumClones;
67 // The CallsiteInfo summary record for each profiled target in
68 // CandidateProfileData (in the same order), or null if there is none, in
69 // which case all clones of the call should call the original target.
71 };
72
73 // Record information needed for ICP of an indirect call, depending on its
74 // profile information and the clone information recorded in the
75 // CallsiteInfo records for its profiled targets. GetCallsite is invoked once
76 // for each profiled target, in order, and returns the corresponding record
77 // (or null if there is none). The number of clones recorded for this
78 // indirect call is returned (0 if there are no records). The necessary
79 // information is recorded in the ICallAnalysisInfo list for later ICP if any
80 // clone of the call should call a cloned target.
81 unsigned
82 recordICPInfo(CallBase *CB,
83 function_ref<const CallsiteInfo *(const InstrProfValueData &)>
84 GetCallsite,
85 SmallVector<ICallAnalysisData> &ICallAnalysisInfo);
86
87 // Actually performs any needed ICP in the function, using the information
88 // recorded in the ICallAnalysisInfo list.
89 void performICP(Module &M, ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
90 ArrayRef<ICallAnalysisData> ICallAnalysisInfo,
92
93 /// Import summary containing cloning decisions for the ThinLTO backend.
94 const ModuleSummaryIndex *ImportSummary;
95
96 // Whether we are building with SamplePGO. This is needed for correctly
97 // updating profile metadata on speculatively promoted calls.
98 bool isSamplePGO;
99
100 // Used when performing indirect call analysis and promotion when cloning in
101 // the ThinLTO backend during applyImport.
102 std::unique_ptr<InstrProfSymtab> Symtab;
103 std::unique_ptr<ICallPromotionAnalysis> ICallAnalysis;
104 // Maps the aliasee GUID of each imported copy of an alias to that copy. Used
105 // by performICP to promote to an alias copy when the profiled target is its
106 // aliasee, which is not in the module. Lazily built on first use.
107 std::optional<DenseMap<GlobalValue::GUID, Function *>>
108 AliaseeGUIDToImportedAlias;
109
110public:
112 MemProfContextDisambiguation(const ModuleSummaryIndex *Summary = nullptr,
113 bool isSamplePGO = false);
114
116
117 LLVM_ABI void
118 run(ModuleSummaryIndex &Index,
120 isPrevailing,
121 LLVMContext &Ctx,
122 function_ref<void(StringRef, StringRef, const Twine &)> EmitRemark =
123 nullptr);
124
125 /// Return -memprof-context-disambiguation.
126 LLVM_ABI static bool isEnabled(const cl::LibraryOptionsContext &C);
127
128 /// Return -supports-hot-cold-new.
130};
131
132/// Strips MemProf attributes and metadata. Can be invoked by the pass pipeline
133/// when we don't have an index that has recorded that we are linking with
134/// allocation libraries containing the necessary APIs for downstream
135/// transformations.
136class MemProfRemoveInfo : public OptionalPassInfoMixin<MemProfRemoveInfo> {
137public:
139};
140
141} // end namespace llvm
142
143#endif // LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
uint64_t uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#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
static LLVM_ABI bool isEnabled(const cl::LibraryOptionsContext &C)
Return -memprof-context-disambiguation.
LLVM_ABI MemProfContextDisambiguation(const ModuleSummaryIndex *Summary=nullptr, bool isSamplePGO=false)
static LLVM_ABI bool supportsHotColdNew(const cl::LibraryOptionsContext &C)
Return -supports-hot-cold-new.
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 namespace contains all of the command line option processing machinery.
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.