From 1e68a2a33a22a17149e5a97e09fff554596f5119 Mon Sep 17 00:00:00 2001 From: Shreyas Atre Date: Wed, 10 Jan 2024 20:55:50 +0530 Subject: [PATCH] [Symbol Names] Use compat::getExecutionEngine for data layout and then Global Prefix - Use global prefix by default to retrieve symbols Signed-off-by: Shreyas Atre --- lib/Interpreter/Compatibility.h | 23 +++++++++++------------ lib/Interpreter/CppInterOp.cpp | 12 +++++++----- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/Interpreter/Compatibility.h b/lib/Interpreter/Compatibility.h index 98ace1811..03a44ab65 100644 --- a/lib/Interpreter/Compatibility.h +++ b/lib/Interpreter/Compatibility.h @@ -210,25 +210,24 @@ getSymbolAddress(clang::Interpreter& I, llvm::StringRef IRName) { inline llvm::Expected getSymbolAddress(clang::Interpreter& I, clang::GlobalDecl GD) { - std::string MangledName; -#if __APPLE__ - return getSymbolAddress(I, llvm::StringRef("_" + MangledName)); -#else + auto& DL = getExecutionEngine(I)->getDataLayout(); + char GlobalPrefix = DL.getGlobalPrefix(); + std::string MangledName = std::string(1, GlobalPrefix); compat::maybeMangleDeclName(GD, MangledName); return getSymbolAddress(I, llvm::StringRef(MangledName)); -#endif } inline llvm::Expected -getSymbolAddressFromLinkerName(const clang::Interpreter& I, - llvm::StringRef LinkerName) { +getSymbolAddressFromLinkerName(clang::Interpreter& I, + llvm::StringRef LinkerName_) { #if CLANG_VERSION_MAJOR >= 14 -#if __APPLE__ - /// https://github.com/llvm/llvm-project/blob/0f8615f4dc568f4d7cbf73580eef3e78f64f3bd0/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp#L252-L259 - auto AddrOrErr = I.getSymbolAddressFromLinkerName(("_" + LinkerName).str()); -#else + auto& DL = getExecutionEngine(I)->getDataLayout(); + char GlobalPrefix = DL.getGlobalPrefix(); + std::string LinkerName(LinkerName_); + if (GlobalPrefix != '\0') { + LinkerName = std::string(1, GlobalPrefix) + LinkerName; + } auto AddrOrErr = I.getSymbolAddressFromLinkerName(LinkerName); -#endif if (llvm::Error Err = AddrOrErr.takeError()) return std::move(Err); return AddrOrErr->getValue(); diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index d3fb01725..ad2e104c8 100644 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -2661,11 +2661,13 @@ namespace Cpp { // Let's inject it. SymbolMap::iterator It; llvm::orc::SymbolMap InjectedSymbols; -#if __APPLE__ - auto Name = ES.intern("_" + std::string(linker_mangled_name)); -#else - auto Name = ES.intern(linker_mangled_name); -#endif + auto& DL = compat::getExecutionEngine(I)->getDataLayout(); + char GlobalPrefix = DL.getGlobalPrefix(); + std::string tmp(linker_mangled_name); + if (GlobalPrefix != '\0') { + tmp = std::string(1, GlobalPrefix) + tmp; + } + auto Name = ES.intern(tmp); InjectedSymbols[Name] = #if CLANG_VERSION_MAJOR < 17 JITEvaluatedSymbol(address,