Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:40

0001 //==- MemProfContextDisambiguation.h - Context Disambiguation ----*- C++ -*-==//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Implements support for context disambiguation of allocation calls for profile
0010 // guided heap optimization using memprof metadata. See implementation file for
0011 // details.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
0016 #define LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
0017 
0018 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
0019 #include "llvm/IR/GlobalValue.h"
0020 #include "llvm/IR/ModuleSummaryIndex.h"
0021 #include "llvm/IR/PassManager.h"
0022 #include "llvm/Transforms/Utils/ValueMapper.h"
0023 #include <functional>
0024 
0025 namespace llvm {
0026 class GlobalValueSummary;
0027 class Module;
0028 class OptimizationRemarkEmitter;
0029 
0030 class MemProfContextDisambiguation
0031     : public PassInfoMixin<MemProfContextDisambiguation> {
0032   /// Run the context disambiguator on \p M, returns true if any changes made.
0033   bool processModule(
0034       Module &M,
0035       function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter);
0036 
0037   /// In the ThinLTO backend, apply the cloning decisions in ImportSummary to
0038   /// the IR.
0039   bool applyImport(Module &M);
0040 
0041   // Builds the symtab and analysis used for ICP during ThinLTO backends.
0042   bool initializeIndirectCallPromotionInfo(Module &M);
0043 
0044   // Data structure for saving indirect call profile info for use in ICP with
0045   // cloning.
0046   struct ICallAnalysisData {
0047     CallBase *CB;
0048     std::vector<InstrProfValueData> CandidateProfileData;
0049     uint32_t NumCandidates;
0050     uint64_t TotalCount;
0051     size_t CallsiteInfoStartIndex;
0052   };
0053 
0054   // Record information needed for ICP of an indirect call, depending on its
0055   // profile information and the clone information recorded in the corresponding
0056   // CallsiteInfo records. The SI iterator point to the current iteration point
0057   // through AllCallsites in this function, and will be updated in this method
0058   // as we iterate through profiled targets. The number of clones recorded for
0059   // this indirect call is returned. The necessary information is recorded in
0060   // the ICallAnalysisInfo list for later ICP.
0061   unsigned recordICPInfo(CallBase *CB, ArrayRef<CallsiteInfo> AllCallsites,
0062                          ArrayRef<CallsiteInfo>::iterator &SI,
0063                          SmallVector<ICallAnalysisData> &ICallAnalysisInfo);
0064 
0065   // Actually performs any needed ICP in the function, using the information
0066   // recorded in the ICallAnalysisInfo list.
0067   void performICP(Module &M, ArrayRef<CallsiteInfo> AllCallsites,
0068                   ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
0069                   ArrayRef<ICallAnalysisData> ICallAnalysisInfo,
0070                   OptimizationRemarkEmitter &ORE);
0071 
0072   /// Import summary containing cloning decisions for the ThinLTO backend.
0073   const ModuleSummaryIndex *ImportSummary;
0074 
0075   // Owns the import summary specified by internal options for testing the
0076   // ThinLTO backend via opt (to simulate distributed ThinLTO).
0077   std::unique_ptr<ModuleSummaryIndex> ImportSummaryForTesting;
0078 
0079   // Whether we are building with SamplePGO. This is needed for correctly
0080   // updating profile metadata on speculatively promoted calls.
0081   bool isSamplePGO;
0082 
0083   // Used when performing indirect call analysis and promotion when cloning in
0084   // the ThinLTO backend during applyImport.
0085   std::unique_ptr<InstrProfSymtab> Symtab;
0086   std::unique_ptr<ICallPromotionAnalysis> ICallAnalysis;
0087 
0088 public:
0089   MemProfContextDisambiguation(const ModuleSummaryIndex *Summary = nullptr,
0090                                bool isSamplePGO = false);
0091 
0092   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0093 
0094   void run(ModuleSummaryIndex &Index,
0095            function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
0096                isPrevailing);
0097 };
0098 } // end namespace llvm
0099 
0100 #endif // LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H