Skip to content

Commit fa02021

Browse files
committed
[memprof] Add memprof options clang frontend flag
1 parent 62ec7b8 commit fa02021

File tree

6 files changed

+55
-25
lines changed

6 files changed

+55
-25
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,15 @@ def fmemory_profile_use_EQ : Joined<["-"], "fmemory-profile-use=">,
22882288
MetaVarName<"<pathname>">,
22892289
HelpText<"Use memory profile for profile-guided memory optimization">,
22902290
MarshallingInfoString<CodeGenOpts<"MemoryProfileUsePath">>;
2291+
def fmemory_profile_runtime_default_options_EQ
2292+
: Joined<["-"], "fmemory-profile-runtime-default-options=">,
2293+
Group<f_Group>,
2294+
Visibility<[ClangOption, CC1Option, CLOption]>,
2295+
MetaVarName<"<options>">,
2296+
HelpText<"Set the default memprof runtime options to <options>">;
2297+
def fmemory_profile_runtime_default_options
2298+
: Separate<["-"], "fmemory-profile-runtime-default-options">,
2299+
Alias<fmemory_profile_runtime_default_options_EQ>;
22912300

22922301
// Begin sanitizer flags. These should all be core options exposed in all driver
22932302
// modes.

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5456,6 +5456,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
54565456
<< MemProfUseArg->getAsString(Args) << PGOInstrArg->getAsString(Args);
54575457
MemProfUseArg->render(Args, CmdArgs);
54585458
}
5459+
StringRef MemprofOptionsStr = Args.getLastArgValue(
5460+
options::OPT_fmemory_profile_runtime_default_options_EQ);
5461+
if (!MemprofOptionsStr.empty()) {
5462+
CmdArgs.push_back("-mllvm");
5463+
CmdArgs.push_back(Args.MakeArgString("-memprof-runtime-default-options=" +
5464+
MemprofOptionsStr));
5465+
}
54595466

54605467
// Embed-bitcode option.
54615468
// Only white-listed flags below are allowed to be embedded.

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,27 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
570570

571571
static void AppendPlatformPrefix(SmallString<128> &Path, const llvm::Triple &T);
572572

573+
/// Check if the link command contains a symbol export directive.
574+
static bool hasExportSymbolDirective(const ArgList &Args) {
575+
for (Arg *A : Args) {
576+
if (A->getOption().matches(options::OPT_exported__symbols__list))
577+
return true;
578+
if (!A->getOption().matches(options::OPT_Wl_COMMA) &&
579+
!A->getOption().matches(options::OPT_Xlinker))
580+
continue;
581+
if (A->containsValue("-exported_symbols_list") ||
582+
A->containsValue("-exported_symbol"))
583+
return true;
584+
}
585+
return false;
586+
}
587+
588+
/// Add an export directive for \p Symbol to the link command.
589+
static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol) {
590+
CmdArgs.push_back("-exported_symbol");
591+
CmdArgs.push_back(Symbol);
592+
}
593+
573594
void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
574595
const InputInfo &Output,
575596
const InputInfoList &Inputs,
@@ -734,6 +755,10 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
734755

735756
getMachOToolChain().addProfileRTLibs(Args, CmdArgs);
736757

758+
if (Args.hasArg(options::OPT_fmemory_profile_runtime_default_options_EQ))
759+
if (hasExportSymbolDirective(Args))
760+
addExportedSymbol(CmdArgs, "___memprof_default_options_str");
761+
737762
StringRef Parallelism = getLTOParallelism(Args, getToolChain().getDriver());
738763
if (!Parallelism.empty()) {
739764
CmdArgs.push_back("-mllvm");
@@ -1433,27 +1458,6 @@ StringRef Darwin::getOSLibraryNameSuffix(bool IgnoreSim) const {
14331458
llvm_unreachable("Unsupported platform");
14341459
}
14351460

1436-
/// Check if the link command contains a symbol export directive.
1437-
static bool hasExportSymbolDirective(const ArgList &Args) {
1438-
for (Arg *A : Args) {
1439-
if (A->getOption().matches(options::OPT_exported__symbols__list))
1440-
return true;
1441-
if (!A->getOption().matches(options::OPT_Wl_COMMA) &&
1442-
!A->getOption().matches(options::OPT_Xlinker))
1443-
continue;
1444-
if (A->containsValue("-exported_symbols_list") ||
1445-
A->containsValue("-exported_symbol"))
1446-
return true;
1447-
}
1448-
return false;
1449-
}
1450-
1451-
/// Add an export directive for \p Symbol to the link command.
1452-
static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol) {
1453-
CmdArgs.push_back("-exported_symbol");
1454-
CmdArgs.push_back(Symbol);
1455-
}
1456-
14571461
/// Add a sectalign directive for \p Segment and \p Section to the maximum
14581462
/// expected page size for Darwin.
14591463
///

clang/test/Driver/fmemprof.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@
1717

1818
// RUN: not %clangxx --target=x86_64-linux-gnu -fprofile-generate -fmemory-profile-use=foo %s -### 2>&1 | FileCheck %s --check-prefix=CONFLICTWITHPGOINSTR
1919
// CONFLICTWITHPGOINSTR: error: invalid argument '-fmemory-profile-use=foo' not allowed with '-fprofile-generate'
20+
21+
// RUN: %clangxx -target arm64-apple-ios -fmemory-profile -fmemory-profile-runtime-default-options="verbose=1" %s -### 2>&1 | FileCheck %s --check-prefix=OPTS
22+
// RUN: %clangxx -target arm64-apple-ios -fmemory-profile -fmemory-profile-runtime-default-options "verbose=1" %s -### 2>&1 | FileCheck %s --check-prefix=OPTS
23+
// RUN: %clangxx -target arm64-apple-ios -fmemory-profile -fmemory-profile-runtime-default-options="verbose=1" -exported_symbols_list /dev/null %s -### 2>&1 | FileCheck %s --check-prefixes=OPTS,OPTS-EXPORT
24+
// RUN: %clangxx -target arm64-apple-ios -fmemory-profile -fmemory-profile-runtime-default-options "verbose=1" -exported_symbols_list /dev/null %s -### 2>&1 | FileCheck %s --check-prefixes=OPTS,OPTS-EXPORT
25+
// OPTS: "-mllvm" "-memprof-runtime-default-options=verbose=1"
26+
// OPTS-EXPORT: "-exported_symbol" "___memprof_default_options_str"

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ void createMemprofHistogramFlagVar(Module &M) {
566566
}
567567

568568
void createMemprofDefaultOptionsVar(Module &M) {
569+
if (!MemprofRuntimeDefaultOptions.getNumOccurrences())
570+
return;
569571
Constant *OptionsConst = ConstantDataArray::getString(
570572
M.getContext(), MemprofRuntimeDefaultOptions, /*AddNull=*/true);
571573
GlobalVariable *OptionsVar =
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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
1+
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes='function(memprof),memprof-module' -S | FileCheck %s --check-prefix=EMPTY
2+
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes='function(memprof),memprof-module' -S -memprof-runtime-default-options="verbose=1" | FileCheck %s
33

44
define i32 @main() {
55
entry:
66
ret i32 0
77
}
88

9+
; EMPTY-NOT: memprof_default_options_str
10+
911
; 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
12+
; CHECK: @__memprof_default_options_str = constant [10 x i8] c"verbose=1\00", comdat

0 commit comments

Comments
 (0)