Skip to content

[memprof] Export __memprof_default_options_str on Darwin #128920

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

Merged
merged 3 commits into from
Mar 3, 2025
Merged
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
7 changes: 7 additions & 0 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Option/ArgList.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/MemProf.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/Threading.h"
Expand Down Expand Up @@ -1617,6 +1618,12 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
}
}

if (Sanitize.needsMemProfRt())
if (hasExportSymbolDirective(Args))
addExportedSymbol(
CmdArgs,
llvm::memprof::getMemprofOptionsSymbolDarwinLinkageName().data());

const XRayArgs &XRay = getXRayArgs();
if (XRay.needsXRayRt()) {
AddLinkRuntimeLib(Args, CmdArgs, "xray");
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'

// Test that we export the __memprof_default_options_str on Darwin because it has WeakAnyLinkage
// RUN: %clangxx --target=arm64-apple-ios -fmemory-profile %s -### 2>&1 | FileCheck %s --check-prefix=EXPORT-BASE --implicit-check-not=exported_symbol
// RUN: %clangxx --target=arm64-apple-ios -fmemory-profile -exported_symbols_list /dev/null %s -### 2>&1 | FileCheck %s --check-prefixes=EXPORT-BASE,EXPORT
// FIXME: Darwin needs to link in the runtime, then we can use the regular CHECK prefix
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I plan to follow this up soon. It should be a simple PR. CC @SharonXSharon

// EXPORT-BASE: "-cc1" {{.*}} "-fmemory-profile"
// EXPORT: "-exported_symbol" "___memprof_default_options_str"
11 changes: 11 additions & 0 deletions llvm/include/llvm/ProfileData/MemProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/ProfileData/MemProfData.inc"
#include "llvm/Support/BLAKE3.h"
Expand Down Expand Up @@ -42,6 +43,16 @@ constexpr uint64_t MaximumSupportedVersion = Version3;
// Verify that the minimum and maximum satisfy the obvious constraint.
static_assert(MinimumSupportedVersion <= MaximumSupportedVersion);

inline llvm::StringRef getMemprofOptionsSymbolDarwinLinkageName() {
return "___memprof_default_options_str";
}

inline llvm::StringRef getMemprofOptionsSymbolName() {
// Darwin linkage names are prefixed with an extra "_". See
// DataLayout::getGlobalPrefix().
return getMemprofOptionsSymbolDarwinLinkageName().drop_front();
}

enum class Meta : uint64_t {
Start = 0,
#define MIBEntryDef(NameTag, Name, Type) NameTag,
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,9 @@ void createMemprofHistogramFlagVar(Module &M) {
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");
GlobalVariable *OptionsVar = new GlobalVariable(
M, OptionsConst->getType(), /*isConstant=*/true,
GlobalValue::WeakAnyLinkage, OptionsConst, getMemprofOptionsSymbolName());
Triple TT(M.getTargetTriple());
if (TT.supportsCOMDAT()) {
OptionsVar->setLinkage(GlobalValue::ExternalLinkage);
Expand Down