Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===--------- Definition of the MemProfiler class --------------*- 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 // This file declares the MemProfiler class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFILER_H
0013 #define LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFILER_H
0014 
0015 #include "llvm/ADT/IntrusiveRefCntPtr.h"
0016 #include "llvm/IR/PassManager.h"
0017 #include "llvm/ProfileData/MemProf.h"
0018 
0019 #include <unordered_map>
0020 
0021 namespace llvm {
0022 class Function;
0023 class IndexedInstrProfReader;
0024 class Module;
0025 class TargetLibraryInfo;
0026 
0027 namespace vfs {
0028 class FileSystem;
0029 } // namespace vfs
0030 
0031 /// Public interface to the memory profiler pass for instrumenting code to
0032 /// profile memory accesses.
0033 ///
0034 /// The profiler itself is a function pass that works by inserting various
0035 /// calls to the MemProfiler runtime library functions. The runtime library
0036 /// essentially replaces malloc() and free() with custom implementations that
0037 /// record data about the allocations.
0038 class MemProfilerPass : public PassInfoMixin<MemProfilerPass> {
0039 public:
0040   explicit MemProfilerPass();
0041   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0042   static bool isRequired() { return true; }
0043 };
0044 
0045 /// Public interface to the memory profiler module pass for instrumenting code
0046 /// to profile memory allocations and accesses.
0047 class ModuleMemProfilerPass : public PassInfoMixin<ModuleMemProfilerPass> {
0048 public:
0049   explicit ModuleMemProfilerPass();
0050   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0051   static bool isRequired() { return true; }
0052 };
0053 
0054 class MemProfUsePass : public PassInfoMixin<MemProfUsePass> {
0055 public:
0056   explicit MemProfUsePass(std::string MemoryProfileFile,
0057                           IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
0058   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0059 
0060 private:
0061   std::string MemoryProfileFileName;
0062   IntrusiveRefCntPtr<vfs::FileSystem> FS;
0063 };
0064 
0065 namespace memprof {
0066 
0067 // Extract all calls from the IR.  Arrange them in a map from caller GUIDs to a
0068 // list of call sites, each of the form {LineLocation, CalleeGUID}.
0069 DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>> extractCallsFromIR(
0070     Module &M, const TargetLibraryInfo &TLI,
0071     function_ref<bool(uint64_t)> IsPresentInProfile = [](uint64_t) {
0072       return true;
0073     });
0074 
0075 struct LineLocationHash {
0076   uint64_t operator()(const LineLocation &Loc) const {
0077     return Loc.getHashCode();
0078   }
0079 };
0080 
0081 using LocToLocMap =
0082     std::unordered_map<LineLocation, LineLocation, LineLocationHash>;
0083 
0084 // Compute an undrifting map.  The result is a map from caller GUIDs to an inner
0085 // map that maps source locations in the profile to those in the current IR.
0086 DenseMap<uint64_t, LocToLocMap>
0087 computeUndriftMap(Module &M, IndexedInstrProfReader *MemProfReader,
0088                   const TargetLibraryInfo &TLI);
0089 
0090 } // namespace memprof
0091 } // namespace llvm
0092 
0093 #endif