1 //===--------- MemProfUse.h - Memory profiler use pass ----*- 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 MemProfUsePass class and related utilities. 10 // 11 //===----------------------------------------------------------------------===// 12 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFUSE_H 13 #define LLVM_TRANSFORMS_INSTRUMENTATION_MEMPROFUSE_H 14 15 #include "llvm/ADT/IntrusiveRefCntPtr.h" 16 #include "llvm/IR/PassManager.h" 17 #include "llvm/ProfileData/MemProf.h" 18 #include "llvm/Support/Compiler.h" 19 20 #include <unordered_map> 21 22 namespace llvm { 23 class IndexedInstrProfReader; 24 class Module; 25 class TargetLibraryInfo; 26 27 namespace vfs { 28 class FileSystem; 29 } // namespace vfs 30 31 class MemProfUsePass : public PassInfoMixin<MemProfUsePass> { 32 public: 33 LLVM_ABI explicit MemProfUsePass( 34 std::string MemoryProfileFile, 35 IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr); 36 LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 37 38 private: 39 std::string MemoryProfileFileName; 40 IntrusiveRefCntPtr<vfs::FileSystem> FS; 41 }; 42 43 namespace memprof { 44 45 // Extract all calls from the IR. Arrange them in a map from caller GUIDs to a 46 // list of call sites, each of the form {LineLocation, CalleeGUID}. 47 LLVM_ABI DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>> extractCallsFromIR( 48 Module &M, const TargetLibraryInfo &TLI, 49 function_ref<bool(uint64_t)> IsPresentInProfile = [](uint64_t) { 50 return true; 51 }); 52 53 struct LineLocationHash { operatorLineLocationHash54 uint64_t operator()(const LineLocation &Loc) const { 55 return Loc.getHashCode(); 56 } 57 }; 58 59 using LocToLocMap = 60 std::unordered_map<LineLocation, LineLocation, LineLocationHash>; 61 62 // Compute an undrifting map. The result is a map from caller GUIDs to an inner 63 // map that maps source locations in the profile to those in the current IR. 64 LLVM_ABI DenseMap<uint64_t, LocToLocMap> 65 computeUndriftMap(Module &M, IndexedInstrProfReader *MemProfReader, 66 const TargetLibraryInfo &TLI); 67 68 } // namespace memprof 69 } // namespace llvm 70 71 #endif 72