Skip to content

Commit d2c4d1e

Browse files
authored
[memprof] Export __memprof_default_options_str on Darwin (#128920)
The `-memprof-runtime-default-options` LLVM flag introduced in #118874 creates the `__memprof_default_options_str` symbol with `WeakAnyLinkage` on Darwin. https://github.com/ellishg/llvm-project/blob/fa0202169af23419c4bcbf66eabd1beb6b6e8e34/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp#L573-L576 This ensures Darwin passes `-exported_symbol ___memprof_default_options_str` to the linker so that the runtime library has visibility into this symbol. This will replace the earlier PR #128615
1 parent 0739ce8 commit d2c4d1e

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/StringSwitch.h"
2222
#include "llvm/Option/ArgList.h"
2323
#include "llvm/ProfileData/InstrProf.h"
24+
#include "llvm/ProfileData/MemProf.h"
2425
#include "llvm/Support/Path.h"
2526
#include "llvm/Support/ScopedPrinter.h"
2627
#include "llvm/Support/Threading.h"
@@ -1617,6 +1618,12 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
16171618
}
16181619
}
16191620

1621+
if (Sanitize.needsMemProfRt())
1622+
if (hasExportSymbolDirective(Args))
1623+
addExportedSymbol(
1624+
CmdArgs,
1625+
llvm::memprof::getMemprofOptionsSymbolDarwinLinkageName().data());
1626+
16201627
const XRayArgs &XRay = getXRayArgs();
16211628
if (XRay.needsXRayRt()) {
16221629
AddLinkRuntimeLib(Args, CmdArgs, "xray");

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+
// Test that we export the __memprof_default_options_str on Darwin because it has WeakAnyLinkage
22+
// RUN: %clangxx --target=arm64-apple-ios -fmemory-profile %s -### 2>&1 | FileCheck %s --check-prefix=EXPORT-BASE --implicit-check-not=exported_symbol
23+
// RUN: %clangxx --target=arm64-apple-ios -fmemory-profile -exported_symbols_list /dev/null %s -### 2>&1 | FileCheck %s --check-prefixes=EXPORT-BASE,EXPORT
24+
// FIXME: Darwin needs to link in the runtime, then we can use the regular CHECK prefix
25+
// EXPORT-BASE: "-cc1" {{.*}} "-fmemory-profile"
26+
// EXPORT: "-exported_symbol" "___memprof_default_options_str"

llvm/include/llvm/ProfileData/MemProf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "llvm/ADT/STLForwardCompat.h"
77
#include "llvm/ADT/STLFunctionalExtras.h"
88
#include "llvm/ADT/SmallVector.h"
9+
#include "llvm/ADT/StringRef.h"
910
#include "llvm/IR/GlobalValue.h"
1011
#include "llvm/ProfileData/MemProfData.inc"
1112
#include "llvm/Support/BLAKE3.h"
@@ -42,6 +43,16 @@ constexpr uint64_t MaximumSupportedVersion = Version3;
4243
// Verify that the minimum and maximum satisfy the obvious constraint.
4344
static_assert(MinimumSupportedVersion <= MaximumSupportedVersion);
4445

46+
inline llvm::StringRef getMemprofOptionsSymbolDarwinLinkageName() {
47+
return "___memprof_default_options_str";
48+
}
49+
50+
inline llvm::StringRef getMemprofOptionsSymbolName() {
51+
// Darwin linkage names are prefixed with an extra "_". See
52+
// DataLayout::getGlobalPrefix().
53+
return getMemprofOptionsSymbolDarwinLinkageName().drop_front();
54+
}
55+
4556
enum class Meta : uint64_t {
4657
Start = 0,
4758
#define MIBEntryDef(NameTag, Name, Type) NameTag,

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,9 @@ void createMemprofHistogramFlagVar(Module &M) {
568568
void createMemprofDefaultOptionsVar(Module &M) {
569569
Constant *OptionsConst = ConstantDataArray::getString(
570570
M.getContext(), MemprofRuntimeDefaultOptions, /*AddNull=*/true);
571-
GlobalVariable *OptionsVar =
572-
new GlobalVariable(M, OptionsConst->getType(), /*isConstant=*/true,
573-
GlobalValue::WeakAnyLinkage, OptionsConst,
574-
"__memprof_default_options_str");
571+
GlobalVariable *OptionsVar = new GlobalVariable(
572+
M, OptionsConst->getType(), /*isConstant=*/true,
573+
GlobalValue::WeakAnyLinkage, OptionsConst, getMemprofOptionsSymbolName());
575574
Triple TT(M.getTargetTriple());
576575
if (TT.supportsCOMDAT()) {
577576
OptionsVar->setLinkage(GlobalValue::ExternalLinkage);

0 commit comments

Comments
 (0)