https://github.com/ellishg updated https://github.com/llvm/llvm-project/pull/128920
>From 2e1946730f7ae2473a4fc94f8918eb3d9d225d17 Mon Sep 17 00:00:00 2001 From: Ellis Hoag <ellish...@meta.com> Date: Wed, 26 Feb 2025 09:48:46 -0800 Subject: [PATCH 1/3] [memprof] Export __memprof_default_options_str on Darwin --- clang/lib/Driver/ToolChains/Darwin.cpp | 4 ++++ clang/test/Driver/fmemprof.cpp | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp index 75f126965e0ac..a2aeef4c4c475 100644 --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -1617,6 +1617,10 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, } } + if (Sanitize.needsMemProfRt()) + if (hasExportSymbolDirective(Args)) + addExportedSymbol(CmdArgs, "___memprof_default_options_str"); + const XRayArgs &XRay = getXRayArgs(); if (XRay.needsXRayRt()) { AddLinkRuntimeLib(Args, CmdArgs, "xray"); diff --git a/clang/test/Driver/fmemprof.cpp b/clang/test/Driver/fmemprof.cpp index 5165c4452fd57..b464320e58a94 100644 --- a/clang/test/Driver/fmemprof.cpp +++ b/clang/test/Driver/fmemprof.cpp @@ -17,3 +17,11 @@ // 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=x86_64-linux-gnu -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 +// EXPORT-BASE: "-cc1" {{.*}} "-fmemory-profile" +// EXPORT: "-exported_symbol" "___memprof_default_options_str" >From f5b85a00fd2d148bbe552ab7916954d75236ef75 Mon Sep 17 00:00:00 2001 From: Ellis Hoag <ellish...@meta.com> Date: Fri, 28 Feb 2025 09:01:00 -0800 Subject: [PATCH 2/3] Add helper functions and comment --- clang/lib/Driver/ToolChains/Darwin.cpp | 5 ++++- llvm/include/llvm/ProfileData/MemProf.h | 11 +++++++++++ llvm/lib/Transforms/Instrumentation/MemProfiler.cpp | 7 +++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp index a2aeef4c4c475..e67997314da36 100644 --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -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" @@ -1619,7 +1620,9 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args, if (Sanitize.needsMemProfRt()) if (hasExportSymbolDirective(Args)) - addExportedSymbol(CmdArgs, "___memprof_default_options_str"); + addExportedSymbol( + CmdArgs, + llvm::memprof::getMemprofOptionsSymbolDarwinLinkageName().data()); const XRayArgs &XRay = getXRayArgs(); if (XRay.needsXRayRt()) { diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h index da0cb47508e32..71e3faa506741 100644 --- a/llvm/include/llvm/ProfileData/MemProf.h +++ b/llvm/include/llvm/ProfileData/MemProf.h @@ -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" @@ -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, diff --git a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp index 7d8bc3aa4c589..6f58a2315547a 100644 --- a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp +++ b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp @@ -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); >From 084e57b4e1f5e8f477890bc8985642a2343af3ac Mon Sep 17 00:00:00 2001 From: Ellis Hoag <ellish...@meta.com> Date: Fri, 28 Feb 2025 09:28:14 -0800 Subject: [PATCH 3/3] remove redundant check line --- clang/test/Driver/fmemprof.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/test/Driver/fmemprof.cpp b/clang/test/Driver/fmemprof.cpp index b464320e58a94..ddf2f851298f2 100644 --- a/clang/test/Driver/fmemprof.cpp +++ b/clang/test/Driver/fmemprof.cpp @@ -19,8 +19,7 @@ // 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=x86_64-linux-gnu -fmemory-profile %s -### 2>&1 | FileCheck %s -check-prefix=EXPORT-BASE --implicit-check-not=exported_symbol +// 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 // EXPORT-BASE: "-cc1" {{.*}} "-fmemory-profile" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits