Skip to content

Commit 2e33ed9

Browse files
authored
[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.
1 parent d42ab5d commit 2e33ed9

File tree

10 files changed

+70
-12
lines changed

10 files changed

+70
-12
lines changed

compiler-rt/include/sanitizer/memprof_interface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ void SANITIZER_CDECL __memprof_print_accumulated_stats(void);
4747

4848
/// User-provided default option settings.
4949
///
50-
/// You can provide your own implementation of this function to return a string
51-
/// containing MemProf runtime options (for example,
52-
/// <c>verbosity=1:print_stats=1</c>).
50+
/// You can set these options via the -memprof-runtime-default-options LLVM flag
51+
/// or you can provide your own implementation of this function. See
52+
/// memprof_flags.h for more info.
5353
///
5454
/// \returns Default options string.
5555
const char *SANITIZER_CDECL __memprof_default_options(void);

compiler-rt/lib/memprof/memprof_flags.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,5 @@ void InitializeFlags() {
8989
} // namespace __memprof
9090

9191
SANITIZER_INTERFACE_WEAK_DEF(const char *, __memprof_default_options, void) {
92-
return "";
92+
return __memprof_default_options_str;
9393
}

compiler-rt/lib/memprof/memprof_flags.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
#include "sanitizer_common/sanitizer_flag_parser.h"
1818
#include "sanitizer_common/sanitizer_internal_defs.h"
1919

20-
// MemProf flag values can be defined in four ways:
21-
// 1) initialized with default values at startup.
22-
// 2) overriden during compilation of MemProf runtime by providing
23-
// compile definition MEMPROF_DEFAULT_OPTIONS.
24-
// 3) overriden from string returned by user-specified function
25-
// __memprof_default_options().
26-
// 4) overriden from env variable MEMPROF_OPTIONS.
20+
// Default MemProf flags are defined in memprof_flags.inc and sancov_flags.inc.
21+
// These values can be overridded in a number of ways, each option overrides the
22+
// prior one:
23+
// 1) by setting MEMPROF_DEFAULT_OPTIONS during the compilation of the MemProf
24+
// runtime
25+
// 2) by setting the LLVM flag -memprof-runtime-default-options during the
26+
// compilation of your binary
27+
// 3) by overriding the user-specified function __memprof_default_options()
28+
// 4) by setting the environment variable MEMPROF_OPTIONS during runtime
2729

2830
namespace __memprof {
2931

compiler-rt/lib/memprof/memprof_interface_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ void __memprof_record_access_range(void const volatile *addr, uptr size);
4040

4141
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_print_accumulated_stats();
4242

43+
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
44+
__memprof_default_options_str[1];
45+
4346
SANITIZER_INTERFACE_ATTRIBUTE
4447
const char *__memprof_default_options();
4548

compiler-rt/lib/memprof/memprof_rtl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include <time.h>
2929

30+
SANITIZER_WEAK_ATTRIBUTE char __memprof_default_options_str[1];
31+
3032
uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
3133

3234
// Allow the user to specify a profile output file via the binary.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
___memprof_default_options __memprof_profile_filename
1+
___memprof_default_options_str ___memprof_default_options __memprof_profile_filename

compiler-rt/test/memprof/TestCases/default_options.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: %clangxx_memprof -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
22

3+
// Check that overriding __memprof_default_options() takes precedence over the LLVM flag
4+
// RUN: %clangxx_memprof -O2 %s -o %t-flag -mllvm -memprof-runtime-default-options="verbosity=0 help=0" && %run %t-flag 2>&1 | FileCheck %s
5+
36
const char *kMemProfDefaultOptions = "verbosity=1 help=1";
47

58
extern "C" const char *__memprof_default_options() {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clangxx_memprof %s -o %t-default
2+
// RUN: %run %t-default | FileCheck %s --check-prefix=DEFAULT
3+
4+
// RUN: %clangxx_memprof %s -mllvm -memprof-runtime-default-options="print_text=true,log_path=stdout,atexit=false" -o %t
5+
// RUN: %run %t | FileCheck %s
6+
7+
#include <sanitizer/memprof_interface.h>
8+
#include <stdio.h>
9+
10+
int main() {
11+
printf("Options: \"%s\"\n", __memprof_default_options());
12+
return 0;
13+
}
14+
15+
// DEFAULT: Options: ""
16+
// CHECK: Options: "print_text=true,log_path=stdout,atexit=false"

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ static cl::opt<bool>
166166
"context in this module's profiles"),
167167
cl::Hidden, cl::init(false));
168168

169+
static cl::opt<std::string>
170+
MemprofRuntimeDefaultOptions("memprof-runtime-default-options",
171+
cl::desc("The default memprof options"),
172+
cl::Hidden, cl::init(""));
173+
169174
extern cl::opt<bool> MemProfReportHintedSizes;
170175

171176
// Instrumentation statistics
@@ -547,6 +552,20 @@ void createMemprofHistogramFlagVar(Module &M) {
547552
appendToCompilerUsed(M, MemprofHistogramFlag);
548553
}
549554

555+
void createMemprofDefaultOptionsVar(Module &M) {
556+
Constant *OptionsConst = ConstantDataArray::getString(
557+
M.getContext(), MemprofRuntimeDefaultOptions, /*AddNull=*/true);
558+
GlobalVariable *OptionsVar =
559+
new GlobalVariable(M, OptionsConst->getType(), /*isConstant=*/true,
560+
GlobalValue::WeakAnyLinkage, OptionsConst,
561+
"__memprof_default_options_str");
562+
Triple TT(M.getTargetTriple());
563+
if (TT.supportsCOMDAT()) {
564+
OptionsVar->setLinkage(GlobalValue::ExternalLinkage);
565+
OptionsVar->setComdat(M.getOrInsertComdat(OptionsVar->getName()));
566+
}
567+
}
568+
550569
bool ModuleMemProfiler::instrumentModule(Module &M) {
551570

552571
// Create a module constructor.
@@ -566,6 +585,8 @@ bool ModuleMemProfiler::instrumentModule(Module &M) {
566585

567586
createMemprofHistogramFlagVar(M);
568587

588+
createMemprofDefaultOptionsVar(M);
589+
569590
return true;
570591
}
571592

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes='function(memprof),memprof-module' -S | FileCheck %s --check-prefixes=CHECK,EMPTY
2+
; 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
3+
4+
define i32 @main() {
5+
entry:
6+
ret i32 0
7+
}
8+
9+
; CHECK: $__memprof_default_options_str = comdat any
10+
; EMPTY: @__memprof_default_options_str = constant [1 x i8] zeroinitializer, comdat
11+
; VERBOSE: @__memprof_default_options_str = constant [10 x i8] c"verbose=1\00", comdat

0 commit comments

Comments
 (0)