Files
clang-p2996/llvm/tools/llvm-jitlink/llvm-jitlink.h
Jared Wyles 2ccf7ed277 [JITLink] Switch to SymbolStringPtr for Symbol names (#115796)
Use SymbolStringPtr for Symbol names in LinkGraph. This reduces string interning
on the boundary between JITLink and ORC, and allows pointer comparisons (rather
than string comparisons) between Symbol names. This should improve the
performance and readability of code that bridges between JITLink and ORC (e.g.
ObjectLinkingLayer and ObjectLinkingLayer::Plugins).

To enable use of SymbolStringPtr a std::shared_ptr<SymbolStringPool> is added to
LinkGraph and threaded through to its construction sites in LLVM and Bolt. All
LinkGraphs that are to have symbol names compared by pointer equality must point
to the same SymbolStringPool instance, which in ORC sessions should be the pool
attached to the ExecutionSession.
---------

Co-authored-by: Lang Hames <lhames@gmail.com>
2024-12-06 10:22:09 +11:00

148 lines
5.7 KiB
C++

//===---- llvm-jitlink.h - Session and format-specific decls ----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// llvm-jitlink Session class and tool utilities.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
#define LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H
#include "llvm/ADT/StringSet.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/EPCIndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
#include "llvm/ExecutionEngine/Orc/LazyObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/RedirectionManager.h"
#include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/SubtargetFeature.h"
#include "llvm/TargetParser/Triple.h"
namespace llvm {
struct Session {
struct LazyLinkingSupport {
LazyLinkingSupport(std::unique_ptr<orc::EPCIndirectionUtils> EPCIU,
std::unique_ptr<orc::RedirectableSymbolManager> RSMgr,
orc::ObjectLinkingLayer &ObjLinkingLayer)
: EPCIU(std::move(EPCIU)), RSMgr(std::move(RSMgr)),
LazyObjLinkingLayer(ObjLinkingLayer,
this->EPCIU->getLazyCallThroughManager(),
*this->RSMgr) {}
~LazyLinkingSupport() {
if (auto Err = EPCIU->cleanup())
LazyObjLinkingLayer.getExecutionSession().reportError(std::move(Err));
}
std::unique_ptr<orc::EPCIndirectionUtils> EPCIU;
std::unique_ptr<orc::RedirectableSymbolManager> RSMgr;
orc::LazyObjectLinkingLayer LazyObjLinkingLayer;
};
orc::ExecutionSession ES;
orc::JITDylib *MainJD = nullptr;
orc::JITDylib *ProcessSymsJD = nullptr;
orc::JITDylib *PlatformJD = nullptr;
orc::ObjectLinkingLayer ObjLayer;
std::unique_ptr<LazyLinkingSupport> LazyLinking;
orc::JITDylibSearchOrder JDSearchOrder;
SubtargetFeatures Features;
~Session();
static Expected<std::unique_ptr<Session>> Create(Triple TT,
SubtargetFeatures Features);
void dumpSessionInfo(raw_ostream &OS);
void modifyPassConfig(jitlink::LinkGraph &G,
jitlink::PassConfiguration &PassConfig);
using MemoryRegionInfo = RuntimeDyldChecker::MemoryRegionInfo;
struct FileInfo {
StringMap<MemoryRegionInfo> SectionInfos;
StringMap<SmallVector<MemoryRegionInfo, 1>> StubInfos;
StringMap<MemoryRegionInfo> GOTEntryInfos;
using Symbol = jitlink::Symbol;
using LinkGraph = jitlink::LinkGraph;
using GetSymbolTargetFunction =
unique_function<Expected<Symbol &>(LinkGraph &G, jitlink::Block &)>;
Error registerGOTEntry(LinkGraph &G, Symbol &Sym,
GetSymbolTargetFunction GetSymbolTarget);
Error registerStubEntry(LinkGraph &G, Symbol &Sym,
GetSymbolTargetFunction GetSymbolTarget);
Error registerMultiStubEntry(LinkGraph &G, Symbol &Sym,
GetSymbolTargetFunction GetSymbolTarget);
};
using DynLibJDMap = std::map<std::string, orc::JITDylib *, std::less<>>;
using SymbolInfoMap = DenseMap<orc::SymbolStringPtr, MemoryRegionInfo>;
using FileInfoMap = StringMap<FileInfo>;
Expected<orc::JITDylib *> getOrLoadDynamicLibrary(StringRef LibPath);
Error loadAndLinkDynamicLibrary(orc::JITDylib &JD, StringRef LibPath);
orc::ObjectLayer &getLinkLayer(bool Lazy) {
assert((!Lazy || LazyLinking) &&
"Lazy linking requested but not available");
return Lazy ? static_cast<orc::ObjectLayer &>(
LazyLinking->LazyObjLinkingLayer)
: static_cast<orc::ObjectLayer &>(ObjLayer);
}
Expected<FileInfo &> findFileInfo(StringRef FileName);
Expected<MemoryRegionInfo &> findSectionInfo(StringRef FileName,
StringRef SectionName);
Expected<MemoryRegionInfo &> findStubInfo(StringRef FileName,
StringRef TargetName,
StringRef KindNameFilter);
Expected<MemoryRegionInfo &> findGOTEntryInfo(StringRef FileName,
StringRef TargetName);
bool isSymbolRegistered(const orc::SymbolStringPtr &Name);
Expected<MemoryRegionInfo &> findSymbolInfo(const orc::SymbolStringPtr &Name,
Twine ErrorMsgStem);
DynLibJDMap DynLibJDs;
SymbolInfoMap SymbolInfos;
FileInfoMap FileInfos;
StringSet<> HarnessFiles;
StringSet<> HarnessExternals;
StringSet<> HarnessDefinitions;
DenseMap<StringRef, StringRef> CanonicalWeakDefs;
std::optional<Regex> ShowGraphsRegex;
private:
Session(std::unique_ptr<orc::ExecutorProcessControl> EPC, Error &Err);
};
/// Record symbols, GOT entries, stubs, and sections for ELF file.
Error registerELFGraphInfo(Session &S, jitlink::LinkGraph &G);
/// Record symbols, GOT entries, stubs, and sections for MachO file.
Error registerMachOGraphInfo(Session &S, jitlink::LinkGraph &G);
/// Record symbols, GOT entries, stubs, and sections for COFF file.
Error registerCOFFGraphInfo(Session &S, jitlink::LinkGraph &G);
/// Adds a statistics gathering plugin if any stats options are used.
void enableStatistics(Session &S, bool UsingOrcRuntime);
} // end namespace llvm
#endif // LLVM_TOOLS_LLVM_JITLINK_LLVM_JITLINK_H