1 //===--------- Definition of the MemProfiler class --------------*- 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 // This file declares the MemProfiler class. 10 // 11 //===----------------------------------------------------------------------===// 12 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFILER_H 13 #define LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFILER_H 14 15 #include "llvm/ADT/IntrusiveRefCntPtr.h" 16 #include "llvm/IR/PassManager.h" 17 #include "llvm/ProfileData/MemProf.h" 18 19 #include <unordered_map> 20 21 namespace llvm { 22 class Function; 23 class IndexedInstrProfReader; 24 class Module; 25 class TargetLibraryInfo; 26 27 namespace vfs { 28 class FileSystem; 29 } // namespace vfs 30 31 /// Public interface to the memory profiler pass for instrumenting code to 32 /// profile memory accesses. 33 /// 34 /// The profiler itself is a function pass that works by inserting various 35 /// calls to the MemProfiler runtime library functions. The runtime library 36 /// essentially replaces malloc() and free() with custom implementations that 37 /// record data about the allocations. 38 class MemProfilerPass : public PassInfoMixin<MemProfilerPass> { 39 public: 40 explicit MemProfilerPass(); 41 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 42 static bool isRequired() { return true; } 43 }; 44 45 /// Public interface to the memory profiler module pass for instrumenting code 46 /// to profile memory allocations and accesses. 47 class ModuleMemProfilerPass : public PassInfoMixin<ModuleMemProfilerPass> { 48 public: 49 explicit ModuleMemProfilerPass(); 50 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 51 static bool isRequired() { return true; } 52 }; 53 54 class MemProfUsePass : public PassInfoMixin<MemProfUsePass> { 55 public: 56 explicit MemProfUsePass(std::string MemoryProfileFile, 57 IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr); 58 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 59 60 private: 61 std::string MemoryProfileFileName; 62 IntrusiveRefCntPtr<vfs::FileSystem> FS; 63 }; 64 65 namespace memprof { 66 67 // Extract all calls from the IR. Arrange them in a map from caller GUIDs to a 68 // list of call sites, each of the form {LineLocation, CalleeGUID}. 69 DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>> extractCallsFromIR( 70 Module &M, const TargetLibraryInfo &TLI, 71 function_ref<bool(uint64_t)> IsPresentInProfile = [](uint64_t) { 72 return true; 73 }); 74 75 struct LineLocationHash { 76 uint64_t operator()(const LineLocation &Loc) const { 77 return Loc.getHashCode(); 78 } 79 }; 80 81 using LocToLocMap = 82 std::unordered_map<LineLocation, LineLocation, LineLocationHash>; 83 84 // Compute an undrifting map. The result is a map from caller GUIDs to an inner 85 // map that maps source locations in the profile to those in the current IR. 86 DenseMap<uint64_t, LocToLocMap> 87 computeUndriftMap(Module &M, IndexedInstrProfReader *MemProfReader, 88 const TargetLibraryInfo &TLI); 89 90 } // namespace memprof 91 } // namespace llvm 92 93 #endif 94