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 profile
10// guided heap optimization using memprof metadata. See implementation file for
11// 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"
21#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/PassManager.h"
25#include <functional>
26
27namespace llvm {
29class LLVMContext;
30class Module;
32
34 : public OptionalPassInfoMixin<MemProfContextDisambiguation> {
35 /// Run the context disambiguator on \p M, returns true if any changes made.
36 bool processModule(
37 Module &M,
39
40 /// In the ThinLTO backend, apply the cloning decisions in ImportSummary to
41 /// the IR.
42 bool applyImport(Module &M);
43
44 // Builds the symtab and analysis used for ICP during ThinLTO backends.
45 bool initializeIndirectCallPromotionInfo(Module &M);
46
47 // Data structure for saving indirect call profile info for use in ICP with
48 // cloning.
49 struct ICallAnalysisData {
50 CallBase *CB;
51 std::vector<InstrProfValueData> CandidateProfileData;
52 uint32_t NumCandidates;
53 uint64_t TotalCount;
54 // The number of clones recorded for the call.
55 unsigned NumClones;
56 // The CallsiteInfo summary record for each profiled target in
57 // CandidateProfileData (in the same order), or null if there is none, in
58 // which case all clones of the call should call the original target.
60 };
61
62 // Record information needed for ICP of an indirect call, depending on its
63 // profile information and the clone information recorded in the
64 // CallsiteInfo records for its profiled targets. GetCallsite is invoked once
65 // for each profiled target, in order, and returns the corresponding record
66 // (or null if there is none). The number of clones recorded for this
67 // indirect call is returned (0 if there are no records). The necessary
68 // information is recorded in the ICallAnalysisInfo list for later ICP if any
69 // clone of the call should call a cloned target.
70 unsigned
71 recordICPInfo(CallBase *CB,
72 function_ref<const CallsiteInfo *(const InstrProfValueData &)>
73 GetCallsite,
74 SmallVector<ICallAnalysisData> &ICallAnalysisInfo);
75
76 // Actually performs any needed ICP in the function, using the information
77 // recorded in the ICallAnalysisInfo list.
78 void performICP(Module &M, ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
79 ArrayRef<ICallAnalysisData> ICallAnalysisInfo,
81
82 /// Import summary containing cloning decisions for the ThinLTO backend.
83 const ModuleSummaryIndex *ImportSummary;
84
85 // Owns the import summary specified by internal options for testing the
86 // ThinLTO backend via opt (to simulate distributed ThinLTO).
87 std::unique_ptr<ModuleSummaryIndex> ImportSummaryForTesting;
88
89 // Whether we are building with SamplePGO. This is needed for correctly
90 // updating profile metadata on speculatively promoted calls.
91 bool isSamplePGO;
92
93 // Used when performing indirect call analysis and promotion when cloning in
94 // the ThinLTO backend during applyImport.
95 std::unique_ptr<InstrProfSymtab> Symtab;
96 std::unique_ptr<ICallPromotionAnalysis> ICallAnalysis;
97 // Maps the aliasee GUID of each imported copy of an alias to that copy. Used
98 // by performICP to promote to an alias copy when the profiled target is its
99 // aliasee, which is not in the module. Lazily built on first use.
100 std::optional<DenseMap<GlobalValue::GUID, Function *>>
101 AliaseeGUIDToImportedAlias;
102
103public:
105 MemProfContextDisambiguation(const ModuleSummaryIndex *Summary = nullptr,
106 bool isSamplePGO = false);
107
109
110 LLVM_ABI void
111 run(ModuleSummaryIndex &Index,
113 isPrevailing,
114 LLVMContext &Ctx,
115 function_ref<void(StringRef, StringRef, const Twine &)> EmitRemark =
116 nullptr);
117};
118
119/// Strips MemProf attributes and metadata. Can be invoked by the pass pipeline
120/// when we don't have an index that has recorded that we are linking with
121/// allocation libraries containing the necessary APIs for downstream
122/// transformations.
123class MemProfRemoveInfo : public OptionalPassInfoMixin<MemProfRemoveInfo> {
124public:
126};
127
128} // end namespace llvm
129
130#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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
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.