Skip to content

[memprof] Add memprof options as a clang frontend flag #128615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,15 @@ def fmemory_profile_use_EQ : Joined<["-"], "fmemory-profile-use=">,
MetaVarName<"<pathname>">,
HelpText<"Use memory profile for profile-guided memory optimization">,
MarshallingInfoString<CodeGenOpts<"MemoryProfileUsePath">>;
def fmemory_profile_runtime_default_options_EQ
: Joined<["-"], "fmemory-profile-runtime-default-options=">,
Group<f_Group>,
Visibility<[ClangOption, CC1Option, CLOption]>,
MetaVarName<"<options>">,
HelpText<"Set the default memprof runtime options to <options>">;
def fmemory_profile_runtime_default_options
: Separate<["-"], "fmemory-profile-runtime-default-options">,
Alias<fmemory_profile_runtime_default_options_EQ>;

// Begin sanitizer flags. These should all be core options exposed in all driver
// modes.
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5456,6 +5456,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
<< MemProfUseArg->getAsString(Args) << PGOInstrArg->getAsString(Args);
MemProfUseArg->render(Args, CmdArgs);
}
StringRef MemprofOptionsStr = Args.getLastArgValue(
options::OPT_fmemory_profile_runtime_default_options_EQ);
if (!MemprofOptionsStr.empty()) {
CmdArgs.push_back("-mllvm");
CmdArgs.push_back(Args.MakeArgString("-memprof-runtime-default-options=" +
MemprofOptionsStr));
}

// Embed-bitcode option.
// Only white-listed flags below are allowed to be embedded.
Expand Down
46 changes: 25 additions & 21 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,27 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,

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

/// Check if the link command contains a symbol export directive.
static bool hasExportSymbolDirective(const ArgList &Args) {
for (Arg *A : Args) {
if (A->getOption().matches(options::OPT_exported__symbols__list))
return true;
if (!A->getOption().matches(options::OPT_Wl_COMMA) &&
!A->getOption().matches(options::OPT_Xlinker))
continue;
if (A->containsValue("-exported_symbols_list") ||
A->containsValue("-exported_symbol"))
return true;
}
return false;
}

/// Add an export directive for \p Symbol to the link command.
static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol) {
CmdArgs.push_back("-exported_symbol");
CmdArgs.push_back(Symbol);
}

void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output,
const InputInfoList &Inputs,
Expand Down Expand Up @@ -734,6 +755,10 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,

getMachOToolChain().addProfileRTLibs(Args, CmdArgs);

if (Args.hasArg(options::OPT_fmemory_profile_runtime_default_options_EQ))
if (hasExportSymbolDirective(Args))
addExportedSymbol(CmdArgs, "___memprof_default_options_str");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor out the uses of this string to a constexpr string in the MemProfiler.h header?


StringRef Parallelism = getLTOParallelism(Args, getToolChain().getDriver());
if (!Parallelism.empty()) {
CmdArgs.push_back("-mllvm");
Expand Down Expand Up @@ -1433,27 +1458,6 @@ StringRef Darwin::getOSLibraryNameSuffix(bool IgnoreSim) const {
llvm_unreachable("Unsupported platform");
}

/// Check if the link command contains a symbol export directive.
static bool hasExportSymbolDirective(const ArgList &Args) {
for (Arg *A : Args) {
if (A->getOption().matches(options::OPT_exported__symbols__list))
return true;
if (!A->getOption().matches(options::OPT_Wl_COMMA) &&
!A->getOption().matches(options::OPT_Xlinker))
continue;
if (A->containsValue("-exported_symbols_list") ||
A->containsValue("-exported_symbol"))
return true;
}
return false;
}

/// Add an export directive for \p Symbol to the link command.
static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol) {
CmdArgs.push_back("-exported_symbol");
CmdArgs.push_back(Symbol);
}

/// Add a sectalign directive for \p Segment and \p Section to the maximum
/// expected page size for Darwin.
///
Expand Down
7 changes: 7 additions & 0 deletions clang/test/Driver/fmemprof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@

// RUN: not %clangxx --target=x86_64-linux-gnu -fprofile-generate -fmemory-profile-use=foo %s -### 2>&1 | FileCheck %s --check-prefix=CONFLICTWITHPGOINSTR
// CONFLICTWITHPGOINSTR: error: invalid argument '-fmemory-profile-use=foo' not allowed with '-fprofile-generate'

// RUN: %clangxx --target=arm64-apple-ios -fmemory-profile -fmemory-profile-runtime-default-options="verbose=1" %s -### 2>&1 | FileCheck %s --check-prefix=OPTS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment above this block about what is being tested? Also maybe add a non-apple test to check that we don't add an exported symbol option in that case.

// RUN: %clangxx --target=arm64-apple-ios -fmemory-profile -fmemory-profile-runtime-default-options "verbose=1" %s -### 2>&1 | FileCheck %s --check-prefix=OPTS
// 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
// 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
// OPTS: "-mllvm" "-memprof-runtime-default-options=verbose=1"
// OPTS-EXPORT: "-exported_symbol" "___memprof_default_options_str"
2 changes: 2 additions & 0 deletions llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ void createMemprofHistogramFlagVar(Module &M) {
}

void createMemprofDefaultOptionsVar(Module &M) {
if (!MemprofRuntimeDefaultOptions.getNumOccurrences())
return;
Constant *OptionsConst = ConstantDataArray::getString(
M.getContext(), MemprofRuntimeDefaultOptions, /*AddNull=*/true);
GlobalVariable *OptionsVar =
Expand Down
9 changes: 5 additions & 4 deletions llvm/test/Instrumentation/HeapProfiler/memprof-options.ll
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
; 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
; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes='function(memprof),memprof-module' -S | FileCheck %s --check-prefix=EMPTY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use --implicit-check-not instead of --check-prefix just to check the NOT case.

; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes='function(memprof),memprof-module' -S -memprof-runtime-default-options="verbose=1" | FileCheck %s

define i32 @main() {
entry:
ret i32 0
}

; EMPTY-NOT: memprof_default_options_str

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