[memprof] Use -memprof-runtime-default-options to set options during compile time (#118874)
Add the `__memprof_default_options_str` variable, initialized via the `-memprof-runtime-default-options` LLVM flag, to hold the default options string for memprof. This allows us to set these options during compile time in the clang invocation. Also update the docs to describe the various ways to set these options.
This commit is contained in:
parent
d42ab5d0f0
commit
2e33ed9ecc
@ -47,9 +47,9 @@ void SANITIZER_CDECL __memprof_print_accumulated_stats(void);
|
|||||||
|
|
||||||
/// User-provided default option settings.
|
/// User-provided default option settings.
|
||||||
///
|
///
|
||||||
/// You can provide your own implementation of this function to return a string
|
/// You can set these options via the -memprof-runtime-default-options LLVM flag
|
||||||
/// containing MemProf runtime options (for example,
|
/// or you can provide your own implementation of this function. See
|
||||||
/// <c>verbosity=1:print_stats=1</c>).
|
/// memprof_flags.h for more info.
|
||||||
///
|
///
|
||||||
/// \returns Default options string.
|
/// \returns Default options string.
|
||||||
const char *SANITIZER_CDECL __memprof_default_options(void);
|
const char *SANITIZER_CDECL __memprof_default_options(void);
|
||||||
|
@ -89,5 +89,5 @@ void InitializeFlags() {
|
|||||||
} // namespace __memprof
|
} // namespace __memprof
|
||||||
|
|
||||||
SANITIZER_INTERFACE_WEAK_DEF(const char *, __memprof_default_options, void) {
|
SANITIZER_INTERFACE_WEAK_DEF(const char *, __memprof_default_options, void) {
|
||||||
return "";
|
return __memprof_default_options_str;
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,15 @@
|
|||||||
#include "sanitizer_common/sanitizer_flag_parser.h"
|
#include "sanitizer_common/sanitizer_flag_parser.h"
|
||||||
#include "sanitizer_common/sanitizer_internal_defs.h"
|
#include "sanitizer_common/sanitizer_internal_defs.h"
|
||||||
|
|
||||||
// MemProf flag values can be defined in four ways:
|
// Default MemProf flags are defined in memprof_flags.inc and sancov_flags.inc.
|
||||||
// 1) initialized with default values at startup.
|
// These values can be overridded in a number of ways, each option overrides the
|
||||||
// 2) overriden during compilation of MemProf runtime by providing
|
// prior one:
|
||||||
// compile definition MEMPROF_DEFAULT_OPTIONS.
|
// 1) by setting MEMPROF_DEFAULT_OPTIONS during the compilation of the MemProf
|
||||||
// 3) overriden from string returned by user-specified function
|
// runtime
|
||||||
// __memprof_default_options().
|
// 2) by setting the LLVM flag -memprof-runtime-default-options during the
|
||||||
// 4) overriden from env variable MEMPROF_OPTIONS.
|
// compilation of your binary
|
||||||
|
// 3) by overriding the user-specified function __memprof_default_options()
|
||||||
|
// 4) by setting the environment variable MEMPROF_OPTIONS during runtime
|
||||||
|
|
||||||
namespace __memprof {
|
namespace __memprof {
|
||||||
|
|
||||||
|
@ -40,6 +40,9 @@ void __memprof_record_access_range(void const volatile *addr, uptr size);
|
|||||||
|
|
||||||
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_print_accumulated_stats();
|
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_print_accumulated_stats();
|
||||||
|
|
||||||
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
|
||||||
|
__memprof_default_options_str[1];
|
||||||
|
|
||||||
SANITIZER_INTERFACE_ATTRIBUTE
|
SANITIZER_INTERFACE_ATTRIBUTE
|
||||||
const char *__memprof_default_options();
|
const char *__memprof_default_options();
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@
|
|||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
SANITIZER_WEAK_ATTRIBUTE char __memprof_default_options_str[1];
|
||||||
|
|
||||||
uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
|
uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
|
||||||
|
|
||||||
// Allow the user to specify a profile output file via the binary.
|
// Allow the user to specify a profile output file via the binary.
|
||||||
|
@ -1 +1 @@
|
|||||||
___memprof_default_options __memprof_profile_filename
|
___memprof_default_options_str ___memprof_default_options __memprof_profile_filename
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
// RUN: %clangxx_memprof -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
|
// RUN: %clangxx_memprof -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
|
||||||
|
|
||||||
|
// Check that overriding __memprof_default_options() takes precedence over the LLVM flag
|
||||||
|
// RUN: %clangxx_memprof -O2 %s -o %t-flag -mllvm -memprof-runtime-default-options="verbosity=0 help=0" && %run %t-flag 2>&1 | FileCheck %s
|
||||||
|
|
||||||
const char *kMemProfDefaultOptions = "verbosity=1 help=1";
|
const char *kMemProfDefaultOptions = "verbosity=1 help=1";
|
||||||
|
|
||||||
extern "C" const char *__memprof_default_options() {
|
extern "C" const char *__memprof_default_options() {
|
||||||
|
16
compiler-rt/test/memprof/TestCases/set_options.cpp
Normal file
16
compiler-rt/test/memprof/TestCases/set_options.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// RUN: %clangxx_memprof %s -o %t-default
|
||||||
|
// RUN: %run %t-default | FileCheck %s --check-prefix=DEFAULT
|
||||||
|
|
||||||
|
// RUN: %clangxx_memprof %s -mllvm -memprof-runtime-default-options="print_text=true,log_path=stdout,atexit=false" -o %t
|
||||||
|
// RUN: %run %t | FileCheck %s
|
||||||
|
|
||||||
|
#include <sanitizer/memprof_interface.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("Options: \"%s\"\n", __memprof_default_options());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// DEFAULT: Options: ""
|
||||||
|
// CHECK: Options: "print_text=true,log_path=stdout,atexit=false"
|
@ -166,6 +166,11 @@ static cl::opt<bool>
|
|||||||
"context in this module's profiles"),
|
"context in this module's profiles"),
|
||||||
cl::Hidden, cl::init(false));
|
cl::Hidden, cl::init(false));
|
||||||
|
|
||||||
|
static cl::opt<std::string>
|
||||||
|
MemprofRuntimeDefaultOptions("memprof-runtime-default-options",
|
||||||
|
cl::desc("The default memprof options"),
|
||||||
|
cl::Hidden, cl::init(""));
|
||||||
|
|
||||||
extern cl::opt<bool> MemProfReportHintedSizes;
|
extern cl::opt<bool> MemProfReportHintedSizes;
|
||||||
|
|
||||||
// Instrumentation statistics
|
// Instrumentation statistics
|
||||||
@ -547,6 +552,20 @@ void createMemprofHistogramFlagVar(Module &M) {
|
|||||||
appendToCompilerUsed(M, MemprofHistogramFlag);
|
appendToCompilerUsed(M, MemprofHistogramFlag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void createMemprofDefaultOptionsVar(Module &M) {
|
||||||
|
Constant *OptionsConst = ConstantDataArray::getString(
|
||||||
|
M.getContext(), MemprofRuntimeDefaultOptions, /*AddNull=*/true);
|
||||||
|
GlobalVariable *OptionsVar =
|
||||||
|
new GlobalVariable(M, OptionsConst->getType(), /*isConstant=*/true,
|
||||||
|
GlobalValue::WeakAnyLinkage, OptionsConst,
|
||||||
|
"__memprof_default_options_str");
|
||||||
|
Triple TT(M.getTargetTriple());
|
||||||
|
if (TT.supportsCOMDAT()) {
|
||||||
|
OptionsVar->setLinkage(GlobalValue::ExternalLinkage);
|
||||||
|
OptionsVar->setComdat(M.getOrInsertComdat(OptionsVar->getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool ModuleMemProfiler::instrumentModule(Module &M) {
|
bool ModuleMemProfiler::instrumentModule(Module &M) {
|
||||||
|
|
||||||
// Create a module constructor.
|
// Create a module constructor.
|
||||||
@ -566,6 +585,8 @@ bool ModuleMemProfiler::instrumentModule(Module &M) {
|
|||||||
|
|
||||||
createMemprofHistogramFlagVar(M);
|
createMemprofHistogramFlagVar(M);
|
||||||
|
|
||||||
|
createMemprofDefaultOptionsVar(M);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
llvm/test/Instrumentation/HeapProfiler/memprof-options.ll
Normal file
11
llvm/test/Instrumentation/HeapProfiler/memprof-options.ll
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes='function(memprof),memprof-module' -S | FileCheck %s --check-prefixes=CHECK,EMPTY
|
||||||
|
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes='function(memprof),memprof-module' -S -memprof-runtime-default-options="verbose=1" | FileCheck %s --check-prefixes=CHECK,VERBOSE
|
||||||
|
|
||||||
|
define i32 @main() {
|
||||||
|
entry:
|
||||||
|
ret i32 0
|
||||||
|
}
|
||||||
|
|
||||||
|
; CHECK: $__memprof_default_options_str = comdat any
|
||||||
|
; EMPTY: @__memprof_default_options_str = constant [1 x i8] zeroinitializer, comdat
|
||||||
|
; VERBOSE: @__memprof_default_options_str = constant [10 x i8] c"verbose=1\00", comdat
|
Loading…
x
Reference in New Issue
Block a user