From c0b0490aa04d040ceb2460895c63a50a743a68a6 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Tue, 29 Sep 2020 15:53:41 -0700 Subject: [PATCH] [MemProf] Pass down memory profile name with optional path from clang Similar to -fprofile-generate=, add -fmemory-profile= which takes a directory path. This is passed down to LLVM via a new module flag metadata. LLVM in turn provides this name to the runtime via the new __memprof_profile_filename variable. Additionally, always pass a default filename (in $cwd if a directory name is not specified vi the = form of the option). This is also consistent with the behavior of the PGO instrumentation. Since the memory profiles will generally be fairly large, it doesn't make sense to dump them to stderr. Also, importantly, the memory profiles will eventually be dumped in a compact binary format, which is another reason why it does not make sense to send these to stderr by default. Change the existing memprof tests to specify log_path=stderr when that was being relied on. Depends on D89086. Differential Revision: https://reviews.llvm.org/D89087 --- .../Instrumentation/MemProfiler.cpp | 24 +++++++++++++++++++ test/Instrumentation/HeapProfiler/filename.ll | 15 ++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test/Instrumentation/HeapProfiler/filename.ll diff --git a/lib/Transforms/Instrumentation/MemProfiler.cpp b/lib/Transforms/Instrumentation/MemProfiler.cpp index 7f2a5ae1a18..32fd11a258f 100644 --- a/lib/Transforms/Instrumentation/MemProfiler.cpp +++ b/lib/Transforms/Instrumentation/MemProfiler.cpp @@ -60,6 +60,8 @@ constexpr char MemProfVersionCheckNamePrefix[] = constexpr char MemProfShadowMemoryDynamicAddress[] = "__memprof_shadow_memory_dynamic_address"; +constexpr char MemProfFilenameVar[] = "__memprof_profile_filename"; + // Command-line flags. static cl::opt ClInsertVersionCheck( @@ -486,6 +488,26 @@ void MemProfiler::instrumentAddress(Instruction *OrigIns, IRB.CreateStore(ShadowValue, ShadowAddr); } +// Create the variable for the profile file name. +void createProfileFileNameVar(Module &M) { + const MDString *MemProfFilename = + dyn_cast_or_null(M.getModuleFlag("MemProfProfileFilename")); + if (!MemProfFilename) + return; + assert(!MemProfFilename->getString().empty() && + "Unexpected MemProfProfileFilename metadata with empty string"); + Constant *ProfileNameConst = ConstantDataArray::getString( + M.getContext(), MemProfFilename->getString(), true); + GlobalVariable *ProfileNameVar = new GlobalVariable( + M, ProfileNameConst->getType(), /*isConstant=*/true, + GlobalValue::WeakAnyLinkage, ProfileNameConst, MemProfFilenameVar); + Triple TT(M.getTargetTriple()); + if (TT.supportsCOMDAT()) { + ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage); + ProfileNameVar->setComdat(M.getOrInsertComdat(MemProfFilenameVar)); + } +} + bool ModuleMemProfiler::instrumentModule(Module &M) { // Create a module constructor. std::string MemProfVersion = std::to_string(LLVM_MEM_PROFILER_VERSION); @@ -500,6 +522,8 @@ bool ModuleMemProfiler::instrumentModule(Module &M) { const uint64_t Priority = getCtorAndDtorPriority(TargetTriple); appendToGlobalCtors(M, MemProfCtorFunction, Priority); + createProfileFileNameVar(M); + return true; } diff --git a/test/Instrumentation/HeapProfiler/filename.ll b/test/Instrumentation/HeapProfiler/filename.ll new file mode 100644 index 00000000000..f019a9491f0 --- /dev/null +++ b/test/Instrumentation/HeapProfiler/filename.ll @@ -0,0 +1,15 @@ +; Test to ensure that the filename provided by clang in the module flags +; metadata results in the expected __memprof_profile_filename insertion. + +; RUN: opt < %s -mtriple=x86_64-unknown-linux -memprof -memprof-module -S | FileCheck --check-prefixes=CHECK %s + +define i32 @main() { +entry: + ret i32 0 +} + +!llvm.module.flags = !{!0} +!0 = !{i32 1, !"MemProfProfileFilename", !"/tmp/memprof.profraw"} + +; CHECK: $__memprof_profile_filename = comdat any +; CHECK: @__memprof_profile_filename = constant [21 x i8] c"/tmp/memprof.profraw\00", comdat