1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[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
This commit is contained in:
Teresa Johnson 2020-09-29 15:53:41 -07:00
parent 405d95e24a
commit c0b0490aa0
2 changed files with 39 additions and 0 deletions

View File

@ -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<bool> 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<MDString>(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;
}

View File

@ -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