1e8d8bef9SDimitry Andric //===- MemProfiler.cpp - memory allocation and access profiler ------------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric // 9e8d8bef9SDimitry Andric // This file is a part of MemProfiler. Memory accesses are instrumented 10e8d8bef9SDimitry Andric // to increment the access count held in a shadow memory location, or 11e8d8bef9SDimitry Andric // alternatively to call into the runtime. Memory intrinsic calls (memmove, 12e8d8bef9SDimitry Andric // memcpy, memset) are changed to call the memory profiling runtime version 13e8d8bef9SDimitry Andric // instead. 14e8d8bef9SDimitry Andric // 15e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 16e8d8bef9SDimitry Andric 17e8d8bef9SDimitry Andric #include "llvm/Transforms/Instrumentation/MemProfiler.h" 18e8d8bef9SDimitry Andric #include "llvm/ADT/SmallVector.h" 19e8d8bef9SDimitry Andric #include "llvm/ADT/Statistic.h" 20e8d8bef9SDimitry Andric #include "llvm/ADT/StringRef.h" 2106c3fb27SDimitry Andric #include "llvm/Analysis/MemoryBuiltins.h" 2206c3fb27SDimitry Andric #include "llvm/Analysis/MemoryProfileInfo.h" 23349cc55cSDimitry Andric #include "llvm/Analysis/ValueTracking.h" 24e8d8bef9SDimitry Andric #include "llvm/IR/Constant.h" 25e8d8bef9SDimitry Andric #include "llvm/IR/DataLayout.h" 2606c3fb27SDimitry Andric #include "llvm/IR/DiagnosticInfo.h" 27e8d8bef9SDimitry Andric #include "llvm/IR/Function.h" 28e8d8bef9SDimitry Andric #include "llvm/IR/GlobalValue.h" 29e8d8bef9SDimitry Andric #include "llvm/IR/IRBuilder.h" 30e8d8bef9SDimitry Andric #include "llvm/IR/Instruction.h" 311fd87a68SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 32e8d8bef9SDimitry Andric #include "llvm/IR/Module.h" 33e8d8bef9SDimitry Andric #include "llvm/IR/Type.h" 34e8d8bef9SDimitry Andric #include "llvm/IR/Value.h" 3581ad6265SDimitry Andric #include "llvm/ProfileData/InstrProf.h" 3606c3fb27SDimitry Andric #include "llvm/ProfileData/InstrProfReader.h" 3706c3fb27SDimitry Andric #include "llvm/Support/BLAKE3.h" 38e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h" 39e8d8bef9SDimitry Andric #include "llvm/Support/Debug.h" 4006c3fb27SDimitry Andric #include "llvm/Support/HashBuilder.h" 4106c3fb27SDimitry Andric #include "llvm/Support/VirtualFileSystem.h" 4206c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h" 43e8d8bef9SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 44e8d8bef9SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h" 4506c3fb27SDimitry Andric #include <map> 4606c3fb27SDimitry Andric #include <set> 47e8d8bef9SDimitry Andric 48e8d8bef9SDimitry Andric using namespace llvm; 4906c3fb27SDimitry Andric using namespace llvm::memprof; 50e8d8bef9SDimitry Andric 51e8d8bef9SDimitry Andric #define DEBUG_TYPE "memprof" 52e8d8bef9SDimitry Andric 5306c3fb27SDimitry Andric namespace llvm { 5406c3fb27SDimitry Andric extern cl::opt<bool> PGOWarnMissing; 5506c3fb27SDimitry Andric extern cl::opt<bool> NoPGOWarnMismatch; 5606c3fb27SDimitry Andric extern cl::opt<bool> NoPGOWarnMismatchComdatWeak; 5706c3fb27SDimitry Andric } // namespace llvm 5806c3fb27SDimitry Andric 59e8d8bef9SDimitry Andric constexpr int LLVM_MEM_PROFILER_VERSION = 1; 60e8d8bef9SDimitry Andric 61e8d8bef9SDimitry Andric // Size of memory mapped to a single shadow location. 62e8d8bef9SDimitry Andric constexpr uint64_t DefaultShadowGranularity = 64; 63e8d8bef9SDimitry Andric 64e8d8bef9SDimitry Andric // Scale from granularity down to shadow size. 65e8d8bef9SDimitry Andric constexpr uint64_t DefaultShadowScale = 3; 66e8d8bef9SDimitry Andric 67e8d8bef9SDimitry Andric constexpr char MemProfModuleCtorName[] = "memprof.module_ctor"; 68e8d8bef9SDimitry Andric constexpr uint64_t MemProfCtorAndDtorPriority = 1; 69e8d8bef9SDimitry Andric // On Emscripten, the system needs more than one priorities for constructors. 70e8d8bef9SDimitry Andric constexpr uint64_t MemProfEmscriptenCtorAndDtorPriority = 50; 71e8d8bef9SDimitry Andric constexpr char MemProfInitName[] = "__memprof_init"; 72e8d8bef9SDimitry Andric constexpr char MemProfVersionCheckNamePrefix[] = 73e8d8bef9SDimitry Andric "__memprof_version_mismatch_check_v"; 74e8d8bef9SDimitry Andric 75e8d8bef9SDimitry Andric constexpr char MemProfShadowMemoryDynamicAddress[] = 76e8d8bef9SDimitry Andric "__memprof_shadow_memory_dynamic_address"; 77e8d8bef9SDimitry Andric 78e8d8bef9SDimitry Andric constexpr char MemProfFilenameVar[] = "__memprof_profile_filename"; 79e8d8bef9SDimitry Andric 80e8d8bef9SDimitry Andric // Command-line flags. 81e8d8bef9SDimitry Andric 82e8d8bef9SDimitry Andric static cl::opt<bool> ClInsertVersionCheck( 83e8d8bef9SDimitry Andric "memprof-guard-against-version-mismatch", 84e8d8bef9SDimitry Andric cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden, 85e8d8bef9SDimitry Andric cl::init(true)); 86e8d8bef9SDimitry Andric 87e8d8bef9SDimitry Andric // This flag may need to be replaced with -f[no-]memprof-reads. 88e8d8bef9SDimitry Andric static cl::opt<bool> ClInstrumentReads("memprof-instrument-reads", 89e8d8bef9SDimitry Andric cl::desc("instrument read instructions"), 90e8d8bef9SDimitry Andric cl::Hidden, cl::init(true)); 91e8d8bef9SDimitry Andric 92e8d8bef9SDimitry Andric static cl::opt<bool> 93e8d8bef9SDimitry Andric ClInstrumentWrites("memprof-instrument-writes", 94e8d8bef9SDimitry Andric cl::desc("instrument write instructions"), cl::Hidden, 95e8d8bef9SDimitry Andric cl::init(true)); 96e8d8bef9SDimitry Andric 97e8d8bef9SDimitry Andric static cl::opt<bool> ClInstrumentAtomics( 98e8d8bef9SDimitry Andric "memprof-instrument-atomics", 99e8d8bef9SDimitry Andric cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, 100e8d8bef9SDimitry Andric cl::init(true)); 101e8d8bef9SDimitry Andric 102e8d8bef9SDimitry Andric static cl::opt<bool> ClUseCalls( 103e8d8bef9SDimitry Andric "memprof-use-callbacks", 104e8d8bef9SDimitry Andric cl::desc("Use callbacks instead of inline instrumentation sequences."), 105e8d8bef9SDimitry Andric cl::Hidden, cl::init(false)); 106e8d8bef9SDimitry Andric 107e8d8bef9SDimitry Andric static cl::opt<std::string> 108e8d8bef9SDimitry Andric ClMemoryAccessCallbackPrefix("memprof-memory-access-callback-prefix", 109e8d8bef9SDimitry Andric cl::desc("Prefix for memory access callbacks"), 110e8d8bef9SDimitry Andric cl::Hidden, cl::init("__memprof_")); 111e8d8bef9SDimitry Andric 112e8d8bef9SDimitry Andric // These flags allow to change the shadow mapping. 113e8d8bef9SDimitry Andric // The shadow mapping looks like 114e8d8bef9SDimitry Andric // Shadow = ((Mem & mask) >> scale) + offset 115e8d8bef9SDimitry Andric 116e8d8bef9SDimitry Andric static cl::opt<int> ClMappingScale("memprof-mapping-scale", 117e8d8bef9SDimitry Andric cl::desc("scale of memprof shadow mapping"), 118e8d8bef9SDimitry Andric cl::Hidden, cl::init(DefaultShadowScale)); 119e8d8bef9SDimitry Andric 120e8d8bef9SDimitry Andric static cl::opt<int> 121e8d8bef9SDimitry Andric ClMappingGranularity("memprof-mapping-granularity", 122e8d8bef9SDimitry Andric cl::desc("granularity of memprof shadow mapping"), 123e8d8bef9SDimitry Andric cl::Hidden, cl::init(DefaultShadowGranularity)); 124e8d8bef9SDimitry Andric 125349cc55cSDimitry Andric static cl::opt<bool> ClStack("memprof-instrument-stack", 126349cc55cSDimitry Andric cl::desc("Instrument scalar stack variables"), 127349cc55cSDimitry Andric cl::Hidden, cl::init(false)); 128349cc55cSDimitry Andric 129e8d8bef9SDimitry Andric // Debug flags. 130e8d8bef9SDimitry Andric 131e8d8bef9SDimitry Andric static cl::opt<int> ClDebug("memprof-debug", cl::desc("debug"), cl::Hidden, 132e8d8bef9SDimitry Andric cl::init(0)); 133e8d8bef9SDimitry Andric 134e8d8bef9SDimitry Andric static cl::opt<std::string> ClDebugFunc("memprof-debug-func", cl::Hidden, 135e8d8bef9SDimitry Andric cl::desc("Debug func")); 136e8d8bef9SDimitry Andric 137e8d8bef9SDimitry Andric static cl::opt<int> ClDebugMin("memprof-debug-min", cl::desc("Debug min inst"), 138e8d8bef9SDimitry Andric cl::Hidden, cl::init(-1)); 139e8d8bef9SDimitry Andric 140e8d8bef9SDimitry Andric static cl::opt<int> ClDebugMax("memprof-debug-max", cl::desc("Debug max inst"), 141e8d8bef9SDimitry Andric cl::Hidden, cl::init(-1)); 142e8d8bef9SDimitry Andric 143e8d8bef9SDimitry Andric STATISTIC(NumInstrumentedReads, "Number of instrumented reads"); 144e8d8bef9SDimitry Andric STATISTIC(NumInstrumentedWrites, "Number of instrumented writes"); 145349cc55cSDimitry Andric STATISTIC(NumSkippedStackReads, "Number of non-instrumented stack reads"); 146349cc55cSDimitry Andric STATISTIC(NumSkippedStackWrites, "Number of non-instrumented stack writes"); 14706c3fb27SDimitry Andric STATISTIC(NumOfMemProfMissing, "Number of functions without memory profile."); 148e8d8bef9SDimitry Andric 149e8d8bef9SDimitry Andric namespace { 150e8d8bef9SDimitry Andric 151e8d8bef9SDimitry Andric /// This struct defines the shadow mapping using the rule: 152e8d8bef9SDimitry Andric /// shadow = ((mem & mask) >> Scale) ADD DynamicShadowOffset. 153e8d8bef9SDimitry Andric struct ShadowMapping { 154e8d8bef9SDimitry Andric ShadowMapping() { 155e8d8bef9SDimitry Andric Scale = ClMappingScale; 156e8d8bef9SDimitry Andric Granularity = ClMappingGranularity; 157e8d8bef9SDimitry Andric Mask = ~(Granularity - 1); 158e8d8bef9SDimitry Andric } 159e8d8bef9SDimitry Andric 160e8d8bef9SDimitry Andric int Scale; 161e8d8bef9SDimitry Andric int Granularity; 162e8d8bef9SDimitry Andric uint64_t Mask; // Computed as ~(Granularity-1) 163e8d8bef9SDimitry Andric }; 164e8d8bef9SDimitry Andric 165e8d8bef9SDimitry Andric static uint64_t getCtorAndDtorPriority(Triple &TargetTriple) { 166e8d8bef9SDimitry Andric return TargetTriple.isOSEmscripten() ? MemProfEmscriptenCtorAndDtorPriority 167e8d8bef9SDimitry Andric : MemProfCtorAndDtorPriority; 168e8d8bef9SDimitry Andric } 169e8d8bef9SDimitry Andric 170e8d8bef9SDimitry Andric struct InterestingMemoryAccess { 171e8d8bef9SDimitry Andric Value *Addr = nullptr; 172e8d8bef9SDimitry Andric bool IsWrite; 17304eeddc0SDimitry Andric Type *AccessTy; 174e8d8bef9SDimitry Andric uint64_t TypeSize; 175e8d8bef9SDimitry Andric Value *MaybeMask = nullptr; 176e8d8bef9SDimitry Andric }; 177e8d8bef9SDimitry Andric 178e8d8bef9SDimitry Andric /// Instrument the code in module to profile memory accesses. 179e8d8bef9SDimitry Andric class MemProfiler { 180e8d8bef9SDimitry Andric public: 181e8d8bef9SDimitry Andric MemProfiler(Module &M) { 182e8d8bef9SDimitry Andric C = &(M.getContext()); 183e8d8bef9SDimitry Andric LongSize = M.getDataLayout().getPointerSizeInBits(); 184e8d8bef9SDimitry Andric IntptrTy = Type::getIntNTy(*C, LongSize); 1855f757f3fSDimitry Andric PtrTy = PointerType::getUnqual(*C); 186e8d8bef9SDimitry Andric } 187e8d8bef9SDimitry Andric 188e8d8bef9SDimitry Andric /// If it is an interesting memory access, populate information 189e8d8bef9SDimitry Andric /// about the access and return a InterestingMemoryAccess struct. 190bdd1243dSDimitry Andric /// Otherwise return std::nullopt. 191bdd1243dSDimitry Andric std::optional<InterestingMemoryAccess> 192e8d8bef9SDimitry Andric isInterestingMemoryAccess(Instruction *I) const; 193e8d8bef9SDimitry Andric 194e8d8bef9SDimitry Andric void instrumentMop(Instruction *I, const DataLayout &DL, 195e8d8bef9SDimitry Andric InterestingMemoryAccess &Access); 196e8d8bef9SDimitry Andric void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore, 197e8d8bef9SDimitry Andric Value *Addr, uint32_t TypeSize, bool IsWrite); 198e8d8bef9SDimitry Andric void instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask, 19981ad6265SDimitry Andric Instruction *I, Value *Addr, Type *AccessTy, 200e8d8bef9SDimitry Andric bool IsWrite); 201e8d8bef9SDimitry Andric void instrumentMemIntrinsic(MemIntrinsic *MI); 202e8d8bef9SDimitry Andric Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); 203e8d8bef9SDimitry Andric bool instrumentFunction(Function &F); 204e8d8bef9SDimitry Andric bool maybeInsertMemProfInitAtFunctionEntry(Function &F); 205e8d8bef9SDimitry Andric bool insertDynamicShadowAtFunctionEntry(Function &F); 206e8d8bef9SDimitry Andric 207e8d8bef9SDimitry Andric private: 208e8d8bef9SDimitry Andric void initializeCallbacks(Module &M); 209e8d8bef9SDimitry Andric 210e8d8bef9SDimitry Andric LLVMContext *C; 211e8d8bef9SDimitry Andric int LongSize; 212e8d8bef9SDimitry Andric Type *IntptrTy; 2135f757f3fSDimitry Andric PointerType *PtrTy; 214e8d8bef9SDimitry Andric ShadowMapping Mapping; 215e8d8bef9SDimitry Andric 216e8d8bef9SDimitry Andric // These arrays is indexed by AccessIsWrite 217e8d8bef9SDimitry Andric FunctionCallee MemProfMemoryAccessCallback[2]; 218e8d8bef9SDimitry Andric FunctionCallee MemProfMemoryAccessCallbackSized[2]; 219e8d8bef9SDimitry Andric 220e8d8bef9SDimitry Andric FunctionCallee MemProfMemmove, MemProfMemcpy, MemProfMemset; 221e8d8bef9SDimitry Andric Value *DynamicShadowOffset = nullptr; 222e8d8bef9SDimitry Andric }; 223e8d8bef9SDimitry Andric 224e8d8bef9SDimitry Andric class ModuleMemProfiler { 225e8d8bef9SDimitry Andric public: 226e8d8bef9SDimitry Andric ModuleMemProfiler(Module &M) { TargetTriple = Triple(M.getTargetTriple()); } 227e8d8bef9SDimitry Andric 228e8d8bef9SDimitry Andric bool instrumentModule(Module &); 229e8d8bef9SDimitry Andric 230e8d8bef9SDimitry Andric private: 231e8d8bef9SDimitry Andric Triple TargetTriple; 232e8d8bef9SDimitry Andric ShadowMapping Mapping; 233e8d8bef9SDimitry Andric Function *MemProfCtorFunction = nullptr; 234e8d8bef9SDimitry Andric }; 235e8d8bef9SDimitry Andric 236e8d8bef9SDimitry Andric } // end anonymous namespace 237e8d8bef9SDimitry Andric 23881ad6265SDimitry Andric MemProfilerPass::MemProfilerPass() = default; 239e8d8bef9SDimitry Andric 240e8d8bef9SDimitry Andric PreservedAnalyses MemProfilerPass::run(Function &F, 241e8d8bef9SDimitry Andric AnalysisManager<Function> &AM) { 242e8d8bef9SDimitry Andric Module &M = *F.getParent(); 243e8d8bef9SDimitry Andric MemProfiler Profiler(M); 244e8d8bef9SDimitry Andric if (Profiler.instrumentFunction(F)) 245e8d8bef9SDimitry Andric return PreservedAnalyses::none(); 246e8d8bef9SDimitry Andric return PreservedAnalyses::all(); 247e8d8bef9SDimitry Andric } 248e8d8bef9SDimitry Andric 24981ad6265SDimitry Andric ModuleMemProfilerPass::ModuleMemProfilerPass() = default; 250e8d8bef9SDimitry Andric 251e8d8bef9SDimitry Andric PreservedAnalyses ModuleMemProfilerPass::run(Module &M, 252e8d8bef9SDimitry Andric AnalysisManager<Module> &AM) { 253e8d8bef9SDimitry Andric ModuleMemProfiler Profiler(M); 254e8d8bef9SDimitry Andric if (Profiler.instrumentModule(M)) 255e8d8bef9SDimitry Andric return PreservedAnalyses::none(); 256e8d8bef9SDimitry Andric return PreservedAnalyses::all(); 257e8d8bef9SDimitry Andric } 258e8d8bef9SDimitry Andric 259e8d8bef9SDimitry Andric Value *MemProfiler::memToShadow(Value *Shadow, IRBuilder<> &IRB) { 260e8d8bef9SDimitry Andric // (Shadow & mask) >> scale 261e8d8bef9SDimitry Andric Shadow = IRB.CreateAnd(Shadow, Mapping.Mask); 262e8d8bef9SDimitry Andric Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); 263e8d8bef9SDimitry Andric // (Shadow >> scale) | offset 264e8d8bef9SDimitry Andric assert(DynamicShadowOffset); 265e8d8bef9SDimitry Andric return IRB.CreateAdd(Shadow, DynamicShadowOffset); 266e8d8bef9SDimitry Andric } 267e8d8bef9SDimitry Andric 268e8d8bef9SDimitry Andric // Instrument memset/memmove/memcpy 269e8d8bef9SDimitry Andric void MemProfiler::instrumentMemIntrinsic(MemIntrinsic *MI) { 270e8d8bef9SDimitry Andric IRBuilder<> IRB(MI); 271e8d8bef9SDimitry Andric if (isa<MemTransferInst>(MI)) { 2725f757f3fSDimitry Andric IRB.CreateCall(isa<MemMoveInst>(MI) ? MemProfMemmove : MemProfMemcpy, 2735f757f3fSDimitry Andric {MI->getOperand(0), MI->getOperand(1), 274e8d8bef9SDimitry Andric IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)}); 275e8d8bef9SDimitry Andric } else if (isa<MemSetInst>(MI)) { 276e8d8bef9SDimitry Andric IRB.CreateCall( 277e8d8bef9SDimitry Andric MemProfMemset, 2785f757f3fSDimitry Andric {MI->getOperand(0), 279e8d8bef9SDimitry Andric IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false), 280e8d8bef9SDimitry Andric IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)}); 281e8d8bef9SDimitry Andric } 282e8d8bef9SDimitry Andric MI->eraseFromParent(); 283e8d8bef9SDimitry Andric } 284e8d8bef9SDimitry Andric 285bdd1243dSDimitry Andric std::optional<InterestingMemoryAccess> 286e8d8bef9SDimitry Andric MemProfiler::isInterestingMemoryAccess(Instruction *I) const { 287e8d8bef9SDimitry Andric // Do not instrument the load fetching the dynamic shadow address. 288e8d8bef9SDimitry Andric if (DynamicShadowOffset == I) 289bdd1243dSDimitry Andric return std::nullopt; 290e8d8bef9SDimitry Andric 291e8d8bef9SDimitry Andric InterestingMemoryAccess Access; 292e8d8bef9SDimitry Andric 293e8d8bef9SDimitry Andric if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 294e8d8bef9SDimitry Andric if (!ClInstrumentReads) 295bdd1243dSDimitry Andric return std::nullopt; 296e8d8bef9SDimitry Andric Access.IsWrite = false; 29704eeddc0SDimitry Andric Access.AccessTy = LI->getType(); 298e8d8bef9SDimitry Andric Access.Addr = LI->getPointerOperand(); 299e8d8bef9SDimitry Andric } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 300e8d8bef9SDimitry Andric if (!ClInstrumentWrites) 301bdd1243dSDimitry Andric return std::nullopt; 302e8d8bef9SDimitry Andric Access.IsWrite = true; 30304eeddc0SDimitry Andric Access.AccessTy = SI->getValueOperand()->getType(); 304e8d8bef9SDimitry Andric Access.Addr = SI->getPointerOperand(); 305e8d8bef9SDimitry Andric } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { 306e8d8bef9SDimitry Andric if (!ClInstrumentAtomics) 307bdd1243dSDimitry Andric return std::nullopt; 308e8d8bef9SDimitry Andric Access.IsWrite = true; 30904eeddc0SDimitry Andric Access.AccessTy = RMW->getValOperand()->getType(); 310e8d8bef9SDimitry Andric Access.Addr = RMW->getPointerOperand(); 311e8d8bef9SDimitry Andric } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { 312e8d8bef9SDimitry Andric if (!ClInstrumentAtomics) 313bdd1243dSDimitry Andric return std::nullopt; 314e8d8bef9SDimitry Andric Access.IsWrite = true; 31504eeddc0SDimitry Andric Access.AccessTy = XCHG->getCompareOperand()->getType(); 316e8d8bef9SDimitry Andric Access.Addr = XCHG->getPointerOperand(); 317e8d8bef9SDimitry Andric } else if (auto *CI = dyn_cast<CallInst>(I)) { 318e8d8bef9SDimitry Andric auto *F = CI->getCalledFunction(); 319e8d8bef9SDimitry Andric if (F && (F->getIntrinsicID() == Intrinsic::masked_load || 320e8d8bef9SDimitry Andric F->getIntrinsicID() == Intrinsic::masked_store)) { 321e8d8bef9SDimitry Andric unsigned OpOffset = 0; 322e8d8bef9SDimitry Andric if (F->getIntrinsicID() == Intrinsic::masked_store) { 323e8d8bef9SDimitry Andric if (!ClInstrumentWrites) 324bdd1243dSDimitry Andric return std::nullopt; 325e8d8bef9SDimitry Andric // Masked store has an initial operand for the value. 326e8d8bef9SDimitry Andric OpOffset = 1; 32704eeddc0SDimitry Andric Access.AccessTy = CI->getArgOperand(0)->getType(); 328e8d8bef9SDimitry Andric Access.IsWrite = true; 329e8d8bef9SDimitry Andric } else { 330e8d8bef9SDimitry Andric if (!ClInstrumentReads) 331bdd1243dSDimitry Andric return std::nullopt; 33204eeddc0SDimitry Andric Access.AccessTy = CI->getType(); 333e8d8bef9SDimitry Andric Access.IsWrite = false; 334e8d8bef9SDimitry Andric } 335e8d8bef9SDimitry Andric 336e8d8bef9SDimitry Andric auto *BasePtr = CI->getOperand(0 + OpOffset); 337e8d8bef9SDimitry Andric Access.MaybeMask = CI->getOperand(2 + OpOffset); 338e8d8bef9SDimitry Andric Access.Addr = BasePtr; 339e8d8bef9SDimitry Andric } 340e8d8bef9SDimitry Andric } 341e8d8bef9SDimitry Andric 342e8d8bef9SDimitry Andric if (!Access.Addr) 343bdd1243dSDimitry Andric return std::nullopt; 344e8d8bef9SDimitry Andric 345bdd1243dSDimitry Andric // Do not instrument accesses from different address spaces; we cannot deal 346e8d8bef9SDimitry Andric // with them. 347e8d8bef9SDimitry Andric Type *PtrTy = cast<PointerType>(Access.Addr->getType()->getScalarType()); 348e8d8bef9SDimitry Andric if (PtrTy->getPointerAddressSpace() != 0) 349bdd1243dSDimitry Andric return std::nullopt; 350e8d8bef9SDimitry Andric 351e8d8bef9SDimitry Andric // Ignore swifterror addresses. 352e8d8bef9SDimitry Andric // swifterror memory addresses are mem2reg promoted by instruction 353e8d8bef9SDimitry Andric // selection. As such they cannot have regular uses like an instrumentation 354e8d8bef9SDimitry Andric // function and it makes no sense to track them as memory. 355e8d8bef9SDimitry Andric if (Access.Addr->isSwiftError()) 356bdd1243dSDimitry Andric return std::nullopt; 357e8d8bef9SDimitry Andric 35881ad6265SDimitry Andric // Peel off GEPs and BitCasts. 35981ad6265SDimitry Andric auto *Addr = Access.Addr->stripInBoundsOffsets(); 36081ad6265SDimitry Andric 36181ad6265SDimitry Andric if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) { 36281ad6265SDimitry Andric // Do not instrument PGO counter updates. 36381ad6265SDimitry Andric if (GV->hasSection()) { 36481ad6265SDimitry Andric StringRef SectionName = GV->getSection(); 36581ad6265SDimitry Andric // Check if the global is in the PGO counters section. 36681ad6265SDimitry Andric auto OF = Triple(I->getModule()->getTargetTriple()).getObjectFormat(); 3675f757f3fSDimitry Andric if (SectionName.ends_with( 36881ad6265SDimitry Andric getInstrProfSectionName(IPSK_cnts, OF, /*AddSegmentInfo=*/false))) 369bdd1243dSDimitry Andric return std::nullopt; 37081ad6265SDimitry Andric } 37181ad6265SDimitry Andric 37281ad6265SDimitry Andric // Do not instrument accesses to LLVM internal variables. 3735f757f3fSDimitry Andric if (GV->getName().starts_with("__llvm")) 374bdd1243dSDimitry Andric return std::nullopt; 37581ad6265SDimitry Andric } 37681ad6265SDimitry Andric 37704eeddc0SDimitry Andric const DataLayout &DL = I->getModule()->getDataLayout(); 37804eeddc0SDimitry Andric Access.TypeSize = DL.getTypeStoreSizeInBits(Access.AccessTy); 379e8d8bef9SDimitry Andric return Access; 380e8d8bef9SDimitry Andric } 381e8d8bef9SDimitry Andric 382e8d8bef9SDimitry Andric void MemProfiler::instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask, 383e8d8bef9SDimitry Andric Instruction *I, Value *Addr, 38404eeddc0SDimitry Andric Type *AccessTy, bool IsWrite) { 38504eeddc0SDimitry Andric auto *VTy = cast<FixedVectorType>(AccessTy); 386e8d8bef9SDimitry Andric uint64_t ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType()); 387e8d8bef9SDimitry Andric unsigned Num = VTy->getNumElements(); 388e8d8bef9SDimitry Andric auto *Zero = ConstantInt::get(IntptrTy, 0); 389e8d8bef9SDimitry Andric for (unsigned Idx = 0; Idx < Num; ++Idx) { 390e8d8bef9SDimitry Andric Value *InstrumentedAddress = nullptr; 391e8d8bef9SDimitry Andric Instruction *InsertBefore = I; 392e8d8bef9SDimitry Andric if (auto *Vector = dyn_cast<ConstantVector>(Mask)) { 393e8d8bef9SDimitry Andric // dyn_cast as we might get UndefValue 394e8d8bef9SDimitry Andric if (auto *Masked = dyn_cast<ConstantInt>(Vector->getOperand(Idx))) { 395e8d8bef9SDimitry Andric if (Masked->isZero()) 396e8d8bef9SDimitry Andric // Mask is constant false, so no instrumentation needed. 397e8d8bef9SDimitry Andric continue; 398e8d8bef9SDimitry Andric // If we have a true or undef value, fall through to instrumentAddress. 399e8d8bef9SDimitry Andric // with InsertBefore == I 400e8d8bef9SDimitry Andric } 401e8d8bef9SDimitry Andric } else { 402e8d8bef9SDimitry Andric IRBuilder<> IRB(I); 403e8d8bef9SDimitry Andric Value *MaskElem = IRB.CreateExtractElement(Mask, Idx); 404e8d8bef9SDimitry Andric Instruction *ThenTerm = SplitBlockAndInsertIfThen(MaskElem, I, false); 405e8d8bef9SDimitry Andric InsertBefore = ThenTerm; 406e8d8bef9SDimitry Andric } 407e8d8bef9SDimitry Andric 408e8d8bef9SDimitry Andric IRBuilder<> IRB(InsertBefore); 409e8d8bef9SDimitry Andric InstrumentedAddress = 410e8d8bef9SDimitry Andric IRB.CreateGEP(VTy, Addr, {Zero, ConstantInt::get(IntptrTy, Idx)}); 411e8d8bef9SDimitry Andric instrumentAddress(I, InsertBefore, InstrumentedAddress, ElemTypeSize, 412e8d8bef9SDimitry Andric IsWrite); 413e8d8bef9SDimitry Andric } 414e8d8bef9SDimitry Andric } 415e8d8bef9SDimitry Andric 416e8d8bef9SDimitry Andric void MemProfiler::instrumentMop(Instruction *I, const DataLayout &DL, 417e8d8bef9SDimitry Andric InterestingMemoryAccess &Access) { 418349cc55cSDimitry Andric // Skip instrumentation of stack accesses unless requested. 419349cc55cSDimitry Andric if (!ClStack && isa<AllocaInst>(getUnderlyingObject(Access.Addr))) { 420349cc55cSDimitry Andric if (Access.IsWrite) 421349cc55cSDimitry Andric ++NumSkippedStackWrites; 422349cc55cSDimitry Andric else 423349cc55cSDimitry Andric ++NumSkippedStackReads; 424349cc55cSDimitry Andric return; 425349cc55cSDimitry Andric } 426349cc55cSDimitry Andric 427e8d8bef9SDimitry Andric if (Access.IsWrite) 428e8d8bef9SDimitry Andric NumInstrumentedWrites++; 429e8d8bef9SDimitry Andric else 430e8d8bef9SDimitry Andric NumInstrumentedReads++; 431e8d8bef9SDimitry Andric 432e8d8bef9SDimitry Andric if (Access.MaybeMask) { 433e8d8bef9SDimitry Andric instrumentMaskedLoadOrStore(DL, Access.MaybeMask, I, Access.Addr, 43481ad6265SDimitry Andric Access.AccessTy, Access.IsWrite); 435e8d8bef9SDimitry Andric } else { 436e8d8bef9SDimitry Andric // Since the access counts will be accumulated across the entire allocation, 437e8d8bef9SDimitry Andric // we only update the shadow access count for the first location and thus 438e8d8bef9SDimitry Andric // don't need to worry about alignment and type size. 439e8d8bef9SDimitry Andric instrumentAddress(I, I, Access.Addr, Access.TypeSize, Access.IsWrite); 440e8d8bef9SDimitry Andric } 441e8d8bef9SDimitry Andric } 442e8d8bef9SDimitry Andric 443e8d8bef9SDimitry Andric void MemProfiler::instrumentAddress(Instruction *OrigIns, 444e8d8bef9SDimitry Andric Instruction *InsertBefore, Value *Addr, 445e8d8bef9SDimitry Andric uint32_t TypeSize, bool IsWrite) { 446e8d8bef9SDimitry Andric IRBuilder<> IRB(InsertBefore); 447e8d8bef9SDimitry Andric Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); 448e8d8bef9SDimitry Andric 449e8d8bef9SDimitry Andric if (ClUseCalls) { 450e8d8bef9SDimitry Andric IRB.CreateCall(MemProfMemoryAccessCallback[IsWrite], AddrLong); 451e8d8bef9SDimitry Andric return; 452e8d8bef9SDimitry Andric } 453e8d8bef9SDimitry Andric 454e8d8bef9SDimitry Andric // Create an inline sequence to compute shadow location, and increment the 455e8d8bef9SDimitry Andric // value by one. 456e8d8bef9SDimitry Andric Type *ShadowTy = Type::getInt64Ty(*C); 457e8d8bef9SDimitry Andric Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); 458e8d8bef9SDimitry Andric Value *ShadowPtr = memToShadow(AddrLong, IRB); 459e8d8bef9SDimitry Andric Value *ShadowAddr = IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy); 460e8d8bef9SDimitry Andric Value *ShadowValue = IRB.CreateLoad(ShadowTy, ShadowAddr); 461e8d8bef9SDimitry Andric Value *Inc = ConstantInt::get(Type::getInt64Ty(*C), 1); 462e8d8bef9SDimitry Andric ShadowValue = IRB.CreateAdd(ShadowValue, Inc); 463e8d8bef9SDimitry Andric IRB.CreateStore(ShadowValue, ShadowAddr); 464e8d8bef9SDimitry Andric } 465e8d8bef9SDimitry Andric 466e8d8bef9SDimitry Andric // Create the variable for the profile file name. 467e8d8bef9SDimitry Andric void createProfileFileNameVar(Module &M) { 468e8d8bef9SDimitry Andric const MDString *MemProfFilename = 469e8d8bef9SDimitry Andric dyn_cast_or_null<MDString>(M.getModuleFlag("MemProfProfileFilename")); 470e8d8bef9SDimitry Andric if (!MemProfFilename) 471e8d8bef9SDimitry Andric return; 472e8d8bef9SDimitry Andric assert(!MemProfFilename->getString().empty() && 473e8d8bef9SDimitry Andric "Unexpected MemProfProfileFilename metadata with empty string"); 474e8d8bef9SDimitry Andric Constant *ProfileNameConst = ConstantDataArray::getString( 475e8d8bef9SDimitry Andric M.getContext(), MemProfFilename->getString(), true); 476e8d8bef9SDimitry Andric GlobalVariable *ProfileNameVar = new GlobalVariable( 477e8d8bef9SDimitry Andric M, ProfileNameConst->getType(), /*isConstant=*/true, 478e8d8bef9SDimitry Andric GlobalValue::WeakAnyLinkage, ProfileNameConst, MemProfFilenameVar); 479e8d8bef9SDimitry Andric Triple TT(M.getTargetTriple()); 480e8d8bef9SDimitry Andric if (TT.supportsCOMDAT()) { 481e8d8bef9SDimitry Andric ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage); 482e8d8bef9SDimitry Andric ProfileNameVar->setComdat(M.getOrInsertComdat(MemProfFilenameVar)); 483e8d8bef9SDimitry Andric } 484e8d8bef9SDimitry Andric } 485e8d8bef9SDimitry Andric 486e8d8bef9SDimitry Andric bool ModuleMemProfiler::instrumentModule(Module &M) { 487e8d8bef9SDimitry Andric // Create a module constructor. 488e8d8bef9SDimitry Andric std::string MemProfVersion = std::to_string(LLVM_MEM_PROFILER_VERSION); 489e8d8bef9SDimitry Andric std::string VersionCheckName = 490e8d8bef9SDimitry Andric ClInsertVersionCheck ? (MemProfVersionCheckNamePrefix + MemProfVersion) 491e8d8bef9SDimitry Andric : ""; 492e8d8bef9SDimitry Andric std::tie(MemProfCtorFunction, std::ignore) = 493e8d8bef9SDimitry Andric createSanitizerCtorAndInitFunctions(M, MemProfModuleCtorName, 494e8d8bef9SDimitry Andric MemProfInitName, /*InitArgTypes=*/{}, 495e8d8bef9SDimitry Andric /*InitArgs=*/{}, VersionCheckName); 496e8d8bef9SDimitry Andric 497e8d8bef9SDimitry Andric const uint64_t Priority = getCtorAndDtorPriority(TargetTriple); 498e8d8bef9SDimitry Andric appendToGlobalCtors(M, MemProfCtorFunction, Priority); 499e8d8bef9SDimitry Andric 500e8d8bef9SDimitry Andric createProfileFileNameVar(M); 501e8d8bef9SDimitry Andric 502e8d8bef9SDimitry Andric return true; 503e8d8bef9SDimitry Andric } 504e8d8bef9SDimitry Andric 505e8d8bef9SDimitry Andric void MemProfiler::initializeCallbacks(Module &M) { 506e8d8bef9SDimitry Andric IRBuilder<> IRB(*C); 507e8d8bef9SDimitry Andric 508e8d8bef9SDimitry Andric for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) { 509e8d8bef9SDimitry Andric const std::string TypeStr = AccessIsWrite ? "store" : "load"; 510e8d8bef9SDimitry Andric 511e8d8bef9SDimitry Andric SmallVector<Type *, 3> Args2 = {IntptrTy, IntptrTy}; 512e8d8bef9SDimitry Andric SmallVector<Type *, 2> Args1{1, IntptrTy}; 513e8d8bef9SDimitry Andric MemProfMemoryAccessCallbackSized[AccessIsWrite] = 514e8d8bef9SDimitry Andric M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr + "N", 515e8d8bef9SDimitry Andric FunctionType::get(IRB.getVoidTy(), Args2, false)); 516e8d8bef9SDimitry Andric 517e8d8bef9SDimitry Andric MemProfMemoryAccessCallback[AccessIsWrite] = 518e8d8bef9SDimitry Andric M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr, 519e8d8bef9SDimitry Andric FunctionType::get(IRB.getVoidTy(), Args1, false)); 520e8d8bef9SDimitry Andric } 521e8d8bef9SDimitry Andric MemProfMemmove = M.getOrInsertFunction( 5225f757f3fSDimitry Andric ClMemoryAccessCallbackPrefix + "memmove", PtrTy, PtrTy, PtrTy, IntptrTy); 523e8d8bef9SDimitry Andric MemProfMemcpy = M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memcpy", 5245f757f3fSDimitry Andric PtrTy, PtrTy, PtrTy, IntptrTy); 5255f757f3fSDimitry Andric MemProfMemset = 5265f757f3fSDimitry Andric M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memset", PtrTy, 5275f757f3fSDimitry Andric PtrTy, IRB.getInt32Ty(), IntptrTy); 528e8d8bef9SDimitry Andric } 529e8d8bef9SDimitry Andric 530e8d8bef9SDimitry Andric bool MemProfiler::maybeInsertMemProfInitAtFunctionEntry(Function &F) { 531e8d8bef9SDimitry Andric // For each NSObject descendant having a +load method, this method is invoked 532e8d8bef9SDimitry Andric // by the ObjC runtime before any of the static constructors is called. 533e8d8bef9SDimitry Andric // Therefore we need to instrument such methods with a call to __memprof_init 534e8d8bef9SDimitry Andric // at the beginning in order to initialize our runtime before any access to 535e8d8bef9SDimitry Andric // the shadow memory. 536e8d8bef9SDimitry Andric // We cannot just ignore these methods, because they may call other 537e8d8bef9SDimitry Andric // instrumented functions. 538*cb14a3feSDimitry Andric if (F.getName().contains(" load]")) { 539e8d8bef9SDimitry Andric FunctionCallee MemProfInitFunction = 540e8d8bef9SDimitry Andric declareSanitizerInitFunction(*F.getParent(), MemProfInitName, {}); 541e8d8bef9SDimitry Andric IRBuilder<> IRB(&F.front(), F.front().begin()); 542e8d8bef9SDimitry Andric IRB.CreateCall(MemProfInitFunction, {}); 543e8d8bef9SDimitry Andric return true; 544e8d8bef9SDimitry Andric } 545e8d8bef9SDimitry Andric return false; 546e8d8bef9SDimitry Andric } 547e8d8bef9SDimitry Andric 548e8d8bef9SDimitry Andric bool MemProfiler::insertDynamicShadowAtFunctionEntry(Function &F) { 549e8d8bef9SDimitry Andric IRBuilder<> IRB(&F.front().front()); 550e8d8bef9SDimitry Andric Value *GlobalDynamicAddress = F.getParent()->getOrInsertGlobal( 551e8d8bef9SDimitry Andric MemProfShadowMemoryDynamicAddress, IntptrTy); 552e8d8bef9SDimitry Andric if (F.getParent()->getPICLevel() == PICLevel::NotPIC) 553e8d8bef9SDimitry Andric cast<GlobalVariable>(GlobalDynamicAddress)->setDSOLocal(true); 554e8d8bef9SDimitry Andric DynamicShadowOffset = IRB.CreateLoad(IntptrTy, GlobalDynamicAddress); 555e8d8bef9SDimitry Andric return true; 556e8d8bef9SDimitry Andric } 557e8d8bef9SDimitry Andric 558e8d8bef9SDimitry Andric bool MemProfiler::instrumentFunction(Function &F) { 559e8d8bef9SDimitry Andric if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) 560e8d8bef9SDimitry Andric return false; 561e8d8bef9SDimitry Andric if (ClDebugFunc == F.getName()) 562e8d8bef9SDimitry Andric return false; 5635f757f3fSDimitry Andric if (F.getName().starts_with("__memprof_")) 564e8d8bef9SDimitry Andric return false; 565e8d8bef9SDimitry Andric 566e8d8bef9SDimitry Andric bool FunctionModified = false; 567e8d8bef9SDimitry Andric 568e8d8bef9SDimitry Andric // If needed, insert __memprof_init. 569e8d8bef9SDimitry Andric // This function needs to be called even if the function body is not 570e8d8bef9SDimitry Andric // instrumented. 571e8d8bef9SDimitry Andric if (maybeInsertMemProfInitAtFunctionEntry(F)) 572e8d8bef9SDimitry Andric FunctionModified = true; 573e8d8bef9SDimitry Andric 574e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "MEMPROF instrumenting:\n" << F << "\n"); 575e8d8bef9SDimitry Andric 576e8d8bef9SDimitry Andric initializeCallbacks(*F.getParent()); 577e8d8bef9SDimitry Andric 578e8d8bef9SDimitry Andric SmallVector<Instruction *, 16> ToInstrument; 579e8d8bef9SDimitry Andric 580e8d8bef9SDimitry Andric // Fill the set of memory operations to instrument. 581e8d8bef9SDimitry Andric for (auto &BB : F) { 582e8d8bef9SDimitry Andric for (auto &Inst : BB) { 583e8d8bef9SDimitry Andric if (isInterestingMemoryAccess(&Inst) || isa<MemIntrinsic>(Inst)) 584e8d8bef9SDimitry Andric ToInstrument.push_back(&Inst); 585e8d8bef9SDimitry Andric } 586e8d8bef9SDimitry Andric } 587e8d8bef9SDimitry Andric 58881ad6265SDimitry Andric if (ToInstrument.empty()) { 58981ad6265SDimitry Andric LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified 59081ad6265SDimitry Andric << " " << F << "\n"); 59181ad6265SDimitry Andric 59281ad6265SDimitry Andric return FunctionModified; 59381ad6265SDimitry Andric } 59481ad6265SDimitry Andric 59581ad6265SDimitry Andric FunctionModified |= insertDynamicShadowAtFunctionEntry(F); 59681ad6265SDimitry Andric 597e8d8bef9SDimitry Andric int NumInstrumented = 0; 598e8d8bef9SDimitry Andric for (auto *Inst : ToInstrument) { 599e8d8bef9SDimitry Andric if (ClDebugMin < 0 || ClDebugMax < 0 || 600e8d8bef9SDimitry Andric (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) { 601bdd1243dSDimitry Andric std::optional<InterestingMemoryAccess> Access = 602e8d8bef9SDimitry Andric isInterestingMemoryAccess(Inst); 603e8d8bef9SDimitry Andric if (Access) 604e8d8bef9SDimitry Andric instrumentMop(Inst, F.getParent()->getDataLayout(), *Access); 605e8d8bef9SDimitry Andric else 606e8d8bef9SDimitry Andric instrumentMemIntrinsic(cast<MemIntrinsic>(Inst)); 607e8d8bef9SDimitry Andric } 608e8d8bef9SDimitry Andric NumInstrumented++; 609e8d8bef9SDimitry Andric } 610e8d8bef9SDimitry Andric 611e8d8bef9SDimitry Andric if (NumInstrumented > 0) 612e8d8bef9SDimitry Andric FunctionModified = true; 613e8d8bef9SDimitry Andric 614e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified << " " 615e8d8bef9SDimitry Andric << F << "\n"); 616e8d8bef9SDimitry Andric 617e8d8bef9SDimitry Andric return FunctionModified; 618e8d8bef9SDimitry Andric } 61906c3fb27SDimitry Andric 62006c3fb27SDimitry Andric static void addCallsiteMetadata(Instruction &I, 62106c3fb27SDimitry Andric std::vector<uint64_t> &InlinedCallStack, 62206c3fb27SDimitry Andric LLVMContext &Ctx) { 62306c3fb27SDimitry Andric I.setMetadata(LLVMContext::MD_callsite, 62406c3fb27SDimitry Andric buildCallstackMetadata(InlinedCallStack, Ctx)); 62506c3fb27SDimitry Andric } 62606c3fb27SDimitry Andric 62706c3fb27SDimitry Andric static uint64_t computeStackId(GlobalValue::GUID Function, uint32_t LineOffset, 62806c3fb27SDimitry Andric uint32_t Column) { 6295f757f3fSDimitry Andric llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> 63006c3fb27SDimitry Andric HashBuilder; 63106c3fb27SDimitry Andric HashBuilder.add(Function, LineOffset, Column); 63206c3fb27SDimitry Andric llvm::BLAKE3Result<8> Hash = HashBuilder.final(); 63306c3fb27SDimitry Andric uint64_t Id; 63406c3fb27SDimitry Andric std::memcpy(&Id, Hash.data(), sizeof(Hash)); 63506c3fb27SDimitry Andric return Id; 63606c3fb27SDimitry Andric } 63706c3fb27SDimitry Andric 63806c3fb27SDimitry Andric static uint64_t computeStackId(const memprof::Frame &Frame) { 63906c3fb27SDimitry Andric return computeStackId(Frame.Function, Frame.LineOffset, Frame.Column); 64006c3fb27SDimitry Andric } 64106c3fb27SDimitry Andric 64206c3fb27SDimitry Andric static void addCallStack(CallStackTrie &AllocTrie, 64306c3fb27SDimitry Andric const AllocationInfo *AllocInfo) { 64406c3fb27SDimitry Andric SmallVector<uint64_t> StackIds; 64506c3fb27SDimitry Andric for (const auto &StackFrame : AllocInfo->CallStack) 64606c3fb27SDimitry Andric StackIds.push_back(computeStackId(StackFrame)); 64706c3fb27SDimitry Andric auto AllocType = getAllocType(AllocInfo->Info.getTotalLifetimeAccessDensity(), 64806c3fb27SDimitry Andric AllocInfo->Info.getAllocCount(), 64906c3fb27SDimitry Andric AllocInfo->Info.getTotalLifetime()); 65006c3fb27SDimitry Andric AllocTrie.addCallStack(AllocType, StackIds); 65106c3fb27SDimitry Andric } 65206c3fb27SDimitry Andric 65306c3fb27SDimitry Andric // Helper to compare the InlinedCallStack computed from an instruction's debug 65406c3fb27SDimitry Andric // info to a list of Frames from profile data (either the allocation data or a 65506c3fb27SDimitry Andric // callsite). For callsites, the StartIndex to use in the Frame array may be 65606c3fb27SDimitry Andric // non-zero. 65706c3fb27SDimitry Andric static bool 65806c3fb27SDimitry Andric stackFrameIncludesInlinedCallStack(ArrayRef<Frame> ProfileCallStack, 65906c3fb27SDimitry Andric ArrayRef<uint64_t> InlinedCallStack, 66006c3fb27SDimitry Andric unsigned StartIndex = 0) { 66106c3fb27SDimitry Andric auto StackFrame = ProfileCallStack.begin() + StartIndex; 66206c3fb27SDimitry Andric auto InlCallStackIter = InlinedCallStack.begin(); 66306c3fb27SDimitry Andric for (; StackFrame != ProfileCallStack.end() && 66406c3fb27SDimitry Andric InlCallStackIter != InlinedCallStack.end(); 66506c3fb27SDimitry Andric ++StackFrame, ++InlCallStackIter) { 66606c3fb27SDimitry Andric uint64_t StackId = computeStackId(*StackFrame); 66706c3fb27SDimitry Andric if (StackId != *InlCallStackIter) 66806c3fb27SDimitry Andric return false; 66906c3fb27SDimitry Andric } 67006c3fb27SDimitry Andric // Return true if we found and matched all stack ids from the call 67106c3fb27SDimitry Andric // instruction. 67206c3fb27SDimitry Andric return InlCallStackIter == InlinedCallStack.end(); 67306c3fb27SDimitry Andric } 67406c3fb27SDimitry Andric 67506c3fb27SDimitry Andric static void readMemprof(Module &M, Function &F, 67606c3fb27SDimitry Andric IndexedInstrProfReader *MemProfReader, 67706c3fb27SDimitry Andric const TargetLibraryInfo &TLI) { 67806c3fb27SDimitry Andric auto &Ctx = M.getContext(); 6795f757f3fSDimitry Andric // Previously we used getIRPGOFuncName() here. If F is local linkage, 6805f757f3fSDimitry Andric // getIRPGOFuncName() returns FuncName with prefix 'FileName;'. But 6815f757f3fSDimitry Andric // llvm-profdata uses FuncName in dwarf to create GUID which doesn't 6825f757f3fSDimitry Andric // contain FileName's prefix. It caused local linkage function can't 6835f757f3fSDimitry Andric // find MemProfRecord. So we use getName() now. 6845f757f3fSDimitry Andric // 'unique-internal-linkage-names' can make MemProf work better for local 6855f757f3fSDimitry Andric // linkage function. 6865f757f3fSDimitry Andric auto FuncName = F.getName(); 68706c3fb27SDimitry Andric auto FuncGUID = Function::getGUID(FuncName); 6885f757f3fSDimitry Andric std::optional<memprof::MemProfRecord> MemProfRec; 6895f757f3fSDimitry Andric auto Err = MemProfReader->getMemProfRecord(FuncGUID).moveInto(MemProfRec); 6905f757f3fSDimitry Andric if (Err) { 6915f757f3fSDimitry Andric handleAllErrors(std::move(Err), [&](const InstrProfError &IPE) { 69206c3fb27SDimitry Andric auto Err = IPE.get(); 69306c3fb27SDimitry Andric bool SkipWarning = false; 69406c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "Error in reading profile for Func " << FuncName 69506c3fb27SDimitry Andric << ": "); 69606c3fb27SDimitry Andric if (Err == instrprof_error::unknown_function) { 69706c3fb27SDimitry Andric NumOfMemProfMissing++; 69806c3fb27SDimitry Andric SkipWarning = !PGOWarnMissing; 69906c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "unknown function"); 70006c3fb27SDimitry Andric } else if (Err == instrprof_error::hash_mismatch) { 70106c3fb27SDimitry Andric SkipWarning = 70206c3fb27SDimitry Andric NoPGOWarnMismatch || 70306c3fb27SDimitry Andric (NoPGOWarnMismatchComdatWeak && 70406c3fb27SDimitry Andric (F.hasComdat() || 70506c3fb27SDimitry Andric F.getLinkage() == GlobalValue::AvailableExternallyLinkage)); 70606c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "hash mismatch (skip=" << SkipWarning << ")"); 70706c3fb27SDimitry Andric } 70806c3fb27SDimitry Andric 70906c3fb27SDimitry Andric if (SkipWarning) 71006c3fb27SDimitry Andric return; 71106c3fb27SDimitry Andric 71206c3fb27SDimitry Andric std::string Msg = (IPE.message() + Twine(" ") + F.getName().str() + 71306c3fb27SDimitry Andric Twine(" Hash = ") + std::to_string(FuncGUID)) 71406c3fb27SDimitry Andric .str(); 71506c3fb27SDimitry Andric 71606c3fb27SDimitry Andric Ctx.diagnose( 71706c3fb27SDimitry Andric DiagnosticInfoPGOProfile(M.getName().data(), Msg, DS_Warning)); 71806c3fb27SDimitry Andric }); 71906c3fb27SDimitry Andric return; 72006c3fb27SDimitry Andric } 72106c3fb27SDimitry Andric 7225f757f3fSDimitry Andric // Detect if there are non-zero column numbers in the profile. If not, 7235f757f3fSDimitry Andric // treat all column numbers as 0 when matching (i.e. ignore any non-zero 7245f757f3fSDimitry Andric // columns in the IR). The profiled binary might have been built with 7255f757f3fSDimitry Andric // column numbers disabled, for example. 7265f757f3fSDimitry Andric bool ProfileHasColumns = false; 7275f757f3fSDimitry Andric 72806c3fb27SDimitry Andric // Build maps of the location hash to all profile data with that leaf location 72906c3fb27SDimitry Andric // (allocation info and the callsites). 73006c3fb27SDimitry Andric std::map<uint64_t, std::set<const AllocationInfo *>> LocHashToAllocInfo; 73106c3fb27SDimitry Andric // For the callsites we need to record the index of the associated frame in 73206c3fb27SDimitry Andric // the frame array (see comments below where the map entries are added). 73306c3fb27SDimitry Andric std::map<uint64_t, std::set<std::pair<const SmallVector<Frame> *, unsigned>>> 73406c3fb27SDimitry Andric LocHashToCallSites; 7355f757f3fSDimitry Andric for (auto &AI : MemProfRec->AllocSites) { 73606c3fb27SDimitry Andric // Associate the allocation info with the leaf frame. The later matching 73706c3fb27SDimitry Andric // code will match any inlined call sequences in the IR with a longer prefix 73806c3fb27SDimitry Andric // of call stack frames. 73906c3fb27SDimitry Andric uint64_t StackId = computeStackId(AI.CallStack[0]); 74006c3fb27SDimitry Andric LocHashToAllocInfo[StackId].insert(&AI); 7415f757f3fSDimitry Andric ProfileHasColumns |= AI.CallStack[0].Column; 74206c3fb27SDimitry Andric } 7435f757f3fSDimitry Andric for (auto &CS : MemProfRec->CallSites) { 74406c3fb27SDimitry Andric // Need to record all frames from leaf up to and including this function, 74506c3fb27SDimitry Andric // as any of these may or may not have been inlined at this point. 74606c3fb27SDimitry Andric unsigned Idx = 0; 74706c3fb27SDimitry Andric for (auto &StackFrame : CS) { 74806c3fb27SDimitry Andric uint64_t StackId = computeStackId(StackFrame); 74906c3fb27SDimitry Andric LocHashToCallSites[StackId].insert(std::make_pair(&CS, Idx++)); 7505f757f3fSDimitry Andric ProfileHasColumns |= StackFrame.Column; 75106c3fb27SDimitry Andric // Once we find this function, we can stop recording. 75206c3fb27SDimitry Andric if (StackFrame.Function == FuncGUID) 75306c3fb27SDimitry Andric break; 75406c3fb27SDimitry Andric } 75506c3fb27SDimitry Andric assert(Idx <= CS.size() && CS[Idx - 1].Function == FuncGUID); 75606c3fb27SDimitry Andric } 75706c3fb27SDimitry Andric 75806c3fb27SDimitry Andric auto GetOffset = [](const DILocation *DIL) { 75906c3fb27SDimitry Andric return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) & 76006c3fb27SDimitry Andric 0xffff; 76106c3fb27SDimitry Andric }; 76206c3fb27SDimitry Andric 76306c3fb27SDimitry Andric // Now walk the instructions, looking up the associated profile data using 76406c3fb27SDimitry Andric // dbug locations. 76506c3fb27SDimitry Andric for (auto &BB : F) { 76606c3fb27SDimitry Andric for (auto &I : BB) { 76706c3fb27SDimitry Andric if (I.isDebugOrPseudoInst()) 76806c3fb27SDimitry Andric continue; 76906c3fb27SDimitry Andric // We are only interested in calls (allocation or interior call stack 77006c3fb27SDimitry Andric // context calls). 77106c3fb27SDimitry Andric auto *CI = dyn_cast<CallBase>(&I); 77206c3fb27SDimitry Andric if (!CI) 77306c3fb27SDimitry Andric continue; 77406c3fb27SDimitry Andric auto *CalledFunction = CI->getCalledFunction(); 77506c3fb27SDimitry Andric if (CalledFunction && CalledFunction->isIntrinsic()) 77606c3fb27SDimitry Andric continue; 77706c3fb27SDimitry Andric // List of call stack ids computed from the location hashes on debug 77806c3fb27SDimitry Andric // locations (leaf to inlined at root). 77906c3fb27SDimitry Andric std::vector<uint64_t> InlinedCallStack; 78006c3fb27SDimitry Andric // Was the leaf location found in one of the profile maps? 78106c3fb27SDimitry Andric bool LeafFound = false; 78206c3fb27SDimitry Andric // If leaf was found in a map, iterators pointing to its location in both 78306c3fb27SDimitry Andric // of the maps. It might exist in neither, one, or both (the latter case 78406c3fb27SDimitry Andric // can happen because we don't currently have discriminators to 78506c3fb27SDimitry Andric // distinguish the case when a single line/col maps to both an allocation 78606c3fb27SDimitry Andric // and another callsite). 78706c3fb27SDimitry Andric std::map<uint64_t, std::set<const AllocationInfo *>>::iterator 78806c3fb27SDimitry Andric AllocInfoIter; 78906c3fb27SDimitry Andric std::map<uint64_t, std::set<std::pair<const SmallVector<Frame> *, 79006c3fb27SDimitry Andric unsigned>>>::iterator CallSitesIter; 79106c3fb27SDimitry Andric for (const DILocation *DIL = I.getDebugLoc(); DIL != nullptr; 79206c3fb27SDimitry Andric DIL = DIL->getInlinedAt()) { 79306c3fb27SDimitry Andric // Use C++ linkage name if possible. Need to compile with 79406c3fb27SDimitry Andric // -fdebug-info-for-profiling to get linkage name. 79506c3fb27SDimitry Andric StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName(); 79606c3fb27SDimitry Andric if (Name.empty()) 79706c3fb27SDimitry Andric Name = DIL->getScope()->getSubprogram()->getName(); 79806c3fb27SDimitry Andric auto CalleeGUID = Function::getGUID(Name); 7995f757f3fSDimitry Andric auto StackId = computeStackId(CalleeGUID, GetOffset(DIL), 8005f757f3fSDimitry Andric ProfileHasColumns ? DIL->getColumn() : 0); 8015f757f3fSDimitry Andric // Check if we have found the profile's leaf frame. If yes, collect 8025f757f3fSDimitry Andric // the rest of the call's inlined context starting here. If not, see if 8035f757f3fSDimitry Andric // we find a match further up the inlined context (in case the profile 8045f757f3fSDimitry Andric // was missing debug frames at the leaf). 80506c3fb27SDimitry Andric if (!LeafFound) { 80606c3fb27SDimitry Andric AllocInfoIter = LocHashToAllocInfo.find(StackId); 80706c3fb27SDimitry Andric CallSitesIter = LocHashToCallSites.find(StackId); 8085f757f3fSDimitry Andric if (AllocInfoIter != LocHashToAllocInfo.end() || 8095f757f3fSDimitry Andric CallSitesIter != LocHashToCallSites.end()) 81006c3fb27SDimitry Andric LeafFound = true; 81106c3fb27SDimitry Andric } 8125f757f3fSDimitry Andric if (LeafFound) 81306c3fb27SDimitry Andric InlinedCallStack.push_back(StackId); 81406c3fb27SDimitry Andric } 81506c3fb27SDimitry Andric // If leaf not in either of the maps, skip inst. 81606c3fb27SDimitry Andric if (!LeafFound) 81706c3fb27SDimitry Andric continue; 81806c3fb27SDimitry Andric 81906c3fb27SDimitry Andric // First add !memprof metadata from allocation info, if we found the 82006c3fb27SDimitry Andric // instruction's leaf location in that map, and if the rest of the 82106c3fb27SDimitry Andric // instruction's locations match the prefix Frame locations on an 82206c3fb27SDimitry Andric // allocation context with the same leaf. 82306c3fb27SDimitry Andric if (AllocInfoIter != LocHashToAllocInfo.end()) { 82406c3fb27SDimitry Andric // Only consider allocations via new, to reduce unnecessary metadata, 82506c3fb27SDimitry Andric // since those are the only allocations that will be targeted initially. 82606c3fb27SDimitry Andric if (!isNewLikeFn(CI, &TLI)) 82706c3fb27SDimitry Andric continue; 82806c3fb27SDimitry Andric // We may match this instruction's location list to multiple MIB 82906c3fb27SDimitry Andric // contexts. Add them to a Trie specialized for trimming the contexts to 83006c3fb27SDimitry Andric // the minimal needed to disambiguate contexts with unique behavior. 83106c3fb27SDimitry Andric CallStackTrie AllocTrie; 83206c3fb27SDimitry Andric for (auto *AllocInfo : AllocInfoIter->second) { 83306c3fb27SDimitry Andric // Check the full inlined call stack against this one. 83406c3fb27SDimitry Andric // If we found and thus matched all frames on the call, include 83506c3fb27SDimitry Andric // this MIB. 83606c3fb27SDimitry Andric if (stackFrameIncludesInlinedCallStack(AllocInfo->CallStack, 83706c3fb27SDimitry Andric InlinedCallStack)) 83806c3fb27SDimitry Andric addCallStack(AllocTrie, AllocInfo); 83906c3fb27SDimitry Andric } 84006c3fb27SDimitry Andric // We might not have matched any to the full inlined call stack. 84106c3fb27SDimitry Andric // But if we did, create and attach metadata, or a function attribute if 84206c3fb27SDimitry Andric // all contexts have identical profiled behavior. 84306c3fb27SDimitry Andric if (!AllocTrie.empty()) { 84406c3fb27SDimitry Andric // MemprofMDAttached will be false if a function attribute was 84506c3fb27SDimitry Andric // attached. 84606c3fb27SDimitry Andric bool MemprofMDAttached = AllocTrie.buildAndAttachMIBMetadata(CI); 84706c3fb27SDimitry Andric assert(MemprofMDAttached == I.hasMetadata(LLVMContext::MD_memprof)); 84806c3fb27SDimitry Andric if (MemprofMDAttached) { 84906c3fb27SDimitry Andric // Add callsite metadata for the instruction's location list so that 85006c3fb27SDimitry Andric // it simpler later on to identify which part of the MIB contexts 85106c3fb27SDimitry Andric // are from this particular instruction (including during inlining, 85206c3fb27SDimitry Andric // when the callsite metdata will be updated appropriately). 85306c3fb27SDimitry Andric // FIXME: can this be changed to strip out the matching stack 85406c3fb27SDimitry Andric // context ids from the MIB contexts and not add any callsite 85506c3fb27SDimitry Andric // metadata here to save space? 85606c3fb27SDimitry Andric addCallsiteMetadata(I, InlinedCallStack, Ctx); 85706c3fb27SDimitry Andric } 85806c3fb27SDimitry Andric } 85906c3fb27SDimitry Andric continue; 86006c3fb27SDimitry Andric } 86106c3fb27SDimitry Andric 86206c3fb27SDimitry Andric // Otherwise, add callsite metadata. If we reach here then we found the 86306c3fb27SDimitry Andric // instruction's leaf location in the callsites map and not the allocation 86406c3fb27SDimitry Andric // map. 86506c3fb27SDimitry Andric assert(CallSitesIter != LocHashToCallSites.end()); 86606c3fb27SDimitry Andric for (auto CallStackIdx : CallSitesIter->second) { 86706c3fb27SDimitry Andric // If we found and thus matched all frames on the call, create and 86806c3fb27SDimitry Andric // attach call stack metadata. 86906c3fb27SDimitry Andric if (stackFrameIncludesInlinedCallStack( 87006c3fb27SDimitry Andric *CallStackIdx.first, InlinedCallStack, CallStackIdx.second)) { 87106c3fb27SDimitry Andric addCallsiteMetadata(I, InlinedCallStack, Ctx); 87206c3fb27SDimitry Andric // Only need to find one with a matching call stack and add a single 87306c3fb27SDimitry Andric // callsite metadata. 87406c3fb27SDimitry Andric break; 87506c3fb27SDimitry Andric } 87606c3fb27SDimitry Andric } 87706c3fb27SDimitry Andric } 87806c3fb27SDimitry Andric } 87906c3fb27SDimitry Andric } 88006c3fb27SDimitry Andric 88106c3fb27SDimitry Andric MemProfUsePass::MemProfUsePass(std::string MemoryProfileFile, 88206c3fb27SDimitry Andric IntrusiveRefCntPtr<vfs::FileSystem> FS) 88306c3fb27SDimitry Andric : MemoryProfileFileName(MemoryProfileFile), FS(FS) { 88406c3fb27SDimitry Andric if (!FS) 88506c3fb27SDimitry Andric this->FS = vfs::getRealFileSystem(); 88606c3fb27SDimitry Andric } 88706c3fb27SDimitry Andric 88806c3fb27SDimitry Andric PreservedAnalyses MemProfUsePass::run(Module &M, ModuleAnalysisManager &AM) { 88906c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "Read in memory profile:"); 89006c3fb27SDimitry Andric auto &Ctx = M.getContext(); 89106c3fb27SDimitry Andric auto ReaderOrErr = IndexedInstrProfReader::create(MemoryProfileFileName, *FS); 89206c3fb27SDimitry Andric if (Error E = ReaderOrErr.takeError()) { 89306c3fb27SDimitry Andric handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { 89406c3fb27SDimitry Andric Ctx.diagnose( 89506c3fb27SDimitry Andric DiagnosticInfoPGOProfile(MemoryProfileFileName.data(), EI.message())); 89606c3fb27SDimitry Andric }); 89706c3fb27SDimitry Andric return PreservedAnalyses::all(); 89806c3fb27SDimitry Andric } 89906c3fb27SDimitry Andric 90006c3fb27SDimitry Andric std::unique_ptr<IndexedInstrProfReader> MemProfReader = 90106c3fb27SDimitry Andric std::move(ReaderOrErr.get()); 90206c3fb27SDimitry Andric if (!MemProfReader) { 90306c3fb27SDimitry Andric Ctx.diagnose(DiagnosticInfoPGOProfile( 90406c3fb27SDimitry Andric MemoryProfileFileName.data(), StringRef("Cannot get MemProfReader"))); 90506c3fb27SDimitry Andric return PreservedAnalyses::all(); 90606c3fb27SDimitry Andric } 90706c3fb27SDimitry Andric 90806c3fb27SDimitry Andric if (!MemProfReader->hasMemoryProfile()) { 90906c3fb27SDimitry Andric Ctx.diagnose(DiagnosticInfoPGOProfile(MemoryProfileFileName.data(), 91006c3fb27SDimitry Andric "Not a memory profile")); 91106c3fb27SDimitry Andric return PreservedAnalyses::all(); 91206c3fb27SDimitry Andric } 91306c3fb27SDimitry Andric 91406c3fb27SDimitry Andric auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 91506c3fb27SDimitry Andric 91606c3fb27SDimitry Andric for (auto &F : M) { 91706c3fb27SDimitry Andric if (F.isDeclaration()) 91806c3fb27SDimitry Andric continue; 91906c3fb27SDimitry Andric 92006c3fb27SDimitry Andric const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F); 92106c3fb27SDimitry Andric readMemprof(M, F, MemProfReader.get(), TLI); 92206c3fb27SDimitry Andric } 92306c3fb27SDimitry Andric 92406c3fb27SDimitry Andric return PreservedAnalyses::none(); 92506c3fb27SDimitry Andric } 926