Skip to content

Commit 6d12b95

Browse files
committed
[ORC] Move EHFrameRegistrationPlugin into its own header + source file. NFC.
1 parent 7bf8190 commit 6d12b95

File tree

8 files changed

+174
-118
lines changed

8 files changed

+174
-118
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===----- EHFrameRegistrationPlugin.h - Register eh-frames -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Register eh-frame sections with a registrar.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_EXECUTIONENGINE_ORC_EHFRAMEREGISTRATIONPLUGIN_H
14+
#define LLVM_EXECUTIONENGINE_ORC_EHFRAMEREGISTRATIONPLUGIN_H
15+
16+
#include "llvm/ExecutionEngine/Orc/LinkGraphLinkingLayer.h"
17+
18+
#include <memory>
19+
#include <mutex>
20+
#include <vector>
21+
22+
namespace llvm {
23+
24+
namespace jitlink {
25+
class EHFrameRegistrar;
26+
} // namespace jitlink
27+
28+
namespace orc {
29+
30+
class EHFrameRegistrationPlugin : public LinkGraphLinkingLayer::Plugin {
31+
public:
32+
EHFrameRegistrationPlugin(
33+
ExecutionSession &ES,
34+
std::unique_ptr<jitlink::EHFrameRegistrar> Registrar);
35+
void modifyPassConfig(MaterializationResponsibility &MR,
36+
jitlink::LinkGraph &G,
37+
jitlink::PassConfiguration &PassConfig) override;
38+
Error notifyEmitted(MaterializationResponsibility &MR) override;
39+
Error notifyFailed(MaterializationResponsibility &MR) override;
40+
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override;
41+
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
42+
ResourceKey SrcKey) override;
43+
44+
private:
45+
std::mutex EHFramePluginMutex;
46+
ExecutionSession &ES;
47+
std::unique_ptr<jitlink::EHFrameRegistrar> Registrar;
48+
DenseMap<MaterializationResponsibility *, ExecutorAddrRange> InProcessLinks;
49+
DenseMap<ResourceKey, std::vector<ExecutorAddrRange>> EHFrameRanges;
50+
};
51+
52+
} // end namespace orc
53+
} // end namespace llvm
54+
55+
#endif // LLVM_EXECUTIONENGINE_ORC_EHFRAMEREGISTRATIONPLUGIN_H

llvm/include/llvm/ExecutionEngine/Orc/LinkGraphLinkingLayer.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,28 +173,6 @@ class LinkGraphLinkingLayer : public LinkGraphLayer, private ResourceManager {
173173
std::vector<std::shared_ptr<Plugin>> Plugins;
174174
};
175175

176-
class EHFrameRegistrationPlugin : public LinkGraphLinkingLayer::Plugin {
177-
public:
178-
EHFrameRegistrationPlugin(
179-
ExecutionSession &ES,
180-
std::unique_ptr<jitlink::EHFrameRegistrar> Registrar);
181-
void modifyPassConfig(MaterializationResponsibility &MR,
182-
jitlink::LinkGraph &G,
183-
jitlink::PassConfiguration &PassConfig) override;
184-
Error notifyEmitted(MaterializationResponsibility &MR) override;
185-
Error notifyFailed(MaterializationResponsibility &MR) override;
186-
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override;
187-
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
188-
ResourceKey SrcKey) override;
189-
190-
private:
191-
std::mutex EHFramePluginMutex;
192-
ExecutionSession &ES;
193-
std::unique_ptr<jitlink::EHFrameRegistrar> Registrar;
194-
DenseMap<MaterializationResponsibility *, ExecutorAddrRange> InProcessLinks;
195-
DenseMap<ResourceKey, std::vector<ExecutorAddrRange>> EHFrameRanges;
196-
};
197-
198176
} // end namespace orc
199177
} // end namespace llvm
200178

llvm/lib/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_llvm_component_library(LLVMOrcJIT
1515
Core.cpp
1616
DebugObjectManagerPlugin.cpp
1717
DebugUtils.cpp
18+
EHFrameRegistrationPlugin.cpp
1819
EPCDynamicLibrarySearchGenerator.cpp
1920
EPCDebugObjectRegistrar.cpp
2021
EPCEHFrameRegistrar.cpp
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//===--------- EHFrameRegistrationPlugin.cpp - Register eh-frames ---------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
10+
11+
#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
12+
13+
#define DEBUG_TYPE "orc"
14+
15+
using namespace llvm::jitlink;
16+
17+
namespace llvm::orc {
18+
19+
EHFrameRegistrationPlugin::EHFrameRegistrationPlugin(
20+
ExecutionSession &ES, std::unique_ptr<EHFrameRegistrar> Registrar)
21+
: ES(ES), Registrar(std::move(Registrar)) {}
22+
23+
void EHFrameRegistrationPlugin::modifyPassConfig(
24+
MaterializationResponsibility &MR, LinkGraph &G,
25+
PassConfiguration &PassConfig) {
26+
27+
PassConfig.PostFixupPasses.push_back(createEHFrameRecorderPass(
28+
G.getTargetTriple(), [this, &MR](ExecutorAddr Addr, size_t Size) {
29+
if (Addr) {
30+
std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
31+
assert(!InProcessLinks.count(&MR) &&
32+
"Link for MR already being tracked?");
33+
InProcessLinks[&MR] = {Addr, Size};
34+
}
35+
}));
36+
}
37+
38+
Error EHFrameRegistrationPlugin::notifyEmitted(
39+
MaterializationResponsibility &MR) {
40+
41+
ExecutorAddrRange EmittedRange;
42+
{
43+
std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
44+
45+
auto EHFrameRangeItr = InProcessLinks.find(&MR);
46+
if (EHFrameRangeItr == InProcessLinks.end())
47+
return Error::success();
48+
49+
EmittedRange = EHFrameRangeItr->second;
50+
assert(EmittedRange.Start && "eh-frame addr to register can not be null");
51+
InProcessLinks.erase(EHFrameRangeItr);
52+
}
53+
54+
if (auto Err = MR.withResourceKeyDo(
55+
[&](ResourceKey K) { EHFrameRanges[K].push_back(EmittedRange); }))
56+
return Err;
57+
58+
return Registrar->registerEHFrames(EmittedRange);
59+
}
60+
61+
Error EHFrameRegistrationPlugin::notifyFailed(
62+
MaterializationResponsibility &MR) {
63+
std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
64+
InProcessLinks.erase(&MR);
65+
return Error::success();
66+
}
67+
68+
Error EHFrameRegistrationPlugin::notifyRemovingResources(JITDylib &JD,
69+
ResourceKey K) {
70+
std::vector<ExecutorAddrRange> RangesToRemove;
71+
72+
ES.runSessionLocked([&] {
73+
auto I = EHFrameRanges.find(K);
74+
if (I != EHFrameRanges.end()) {
75+
RangesToRemove = std::move(I->second);
76+
EHFrameRanges.erase(I);
77+
}
78+
});
79+
80+
Error Err = Error::success();
81+
while (!RangesToRemove.empty()) {
82+
auto RangeToRemove = RangesToRemove.back();
83+
RangesToRemove.pop_back();
84+
assert(RangeToRemove.Start && "Untracked eh-frame range must not be null");
85+
Err = joinErrors(std::move(Err),
86+
Registrar->deregisterEHFrames(RangeToRemove));
87+
}
88+
89+
return Err;
90+
}
91+
92+
void EHFrameRegistrationPlugin::notifyTransferringResources(
93+
JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) {
94+
auto SI = EHFrameRanges.find(SrcKey);
95+
if (SI == EHFrameRanges.end())
96+
return;
97+
98+
auto DI = EHFrameRanges.find(DstKey);
99+
if (DI != EHFrameRanges.end()) {
100+
auto &SrcRanges = SI->second;
101+
auto &DstRanges = DI->second;
102+
DstRanges.reserve(DstRanges.size() + SrcRanges.size());
103+
for (auto &SrcRange : SrcRanges)
104+
DstRanges.push_back(std::move(SrcRange));
105+
EHFrameRanges.erase(SI);
106+
} else {
107+
// We need to move SrcKey's ranges over without invalidating the SI
108+
// iterator.
109+
auto Tmp = std::move(SI->second);
110+
EHFrameRanges.erase(SI);
111+
EHFrameRanges[DstKey] = std::move(Tmp);
112+
}
113+
}
114+
115+
} // namespace llvm::orc

llvm/lib/ExecutionEngine/Orc/LLJIT.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/Analysis/TargetLibraryInfo.h"
1212
#include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS
1313
#include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
14+
#include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
1415
#include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
1516
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
1617
#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"

llvm/lib/ExecutionEngine/Orc/LinkGraphLinkingLayer.cpp

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -581,101 +581,5 @@ void LinkGraphLinkingLayer::handleTransferResources(JITDylib &JD,
581581
P->notifyTransferringResources(JD, DstKey, SrcKey);
582582
}
583583

584-
EHFrameRegistrationPlugin::EHFrameRegistrationPlugin(
585-
ExecutionSession &ES, std::unique_ptr<EHFrameRegistrar> Registrar)
586-
: ES(ES), Registrar(std::move(Registrar)) {}
587-
588-
void EHFrameRegistrationPlugin::modifyPassConfig(
589-
MaterializationResponsibility &MR, LinkGraph &G,
590-
PassConfiguration &PassConfig) {
591-
592-
PassConfig.PostFixupPasses.push_back(createEHFrameRecorderPass(
593-
G.getTargetTriple(), [this, &MR](ExecutorAddr Addr, size_t Size) {
594-
if (Addr) {
595-
std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
596-
assert(!InProcessLinks.count(&MR) &&
597-
"Link for MR already being tracked?");
598-
InProcessLinks[&MR] = {Addr, Size};
599-
}
600-
}));
601-
}
602-
603-
Error EHFrameRegistrationPlugin::notifyEmitted(
604-
MaterializationResponsibility &MR) {
605-
606-
ExecutorAddrRange EmittedRange;
607-
{
608-
std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
609-
610-
auto EHFrameRangeItr = InProcessLinks.find(&MR);
611-
if (EHFrameRangeItr == InProcessLinks.end())
612-
return Error::success();
613-
614-
EmittedRange = EHFrameRangeItr->second;
615-
assert(EmittedRange.Start && "eh-frame addr to register can not be null");
616-
InProcessLinks.erase(EHFrameRangeItr);
617-
}
618-
619-
if (auto Err = MR.withResourceKeyDo(
620-
[&](ResourceKey K) { EHFrameRanges[K].push_back(EmittedRange); }))
621-
return Err;
622-
623-
return Registrar->registerEHFrames(EmittedRange);
624-
}
625-
626-
Error EHFrameRegistrationPlugin::notifyFailed(
627-
MaterializationResponsibility &MR) {
628-
std::lock_guard<std::mutex> Lock(EHFramePluginMutex);
629-
InProcessLinks.erase(&MR);
630-
return Error::success();
631-
}
632-
633-
Error EHFrameRegistrationPlugin::notifyRemovingResources(JITDylib &JD,
634-
ResourceKey K) {
635-
std::vector<ExecutorAddrRange> RangesToRemove;
636-
637-
ES.runSessionLocked([&] {
638-
auto I = EHFrameRanges.find(K);
639-
if (I != EHFrameRanges.end()) {
640-
RangesToRemove = std::move(I->second);
641-
EHFrameRanges.erase(I);
642-
}
643-
});
644-
645-
Error Err = Error::success();
646-
while (!RangesToRemove.empty()) {
647-
auto RangeToRemove = RangesToRemove.back();
648-
RangesToRemove.pop_back();
649-
assert(RangeToRemove.Start && "Untracked eh-frame range must not be null");
650-
Err = joinErrors(std::move(Err),
651-
Registrar->deregisterEHFrames(RangeToRemove));
652-
}
653-
654-
return Err;
655-
}
656-
657-
void EHFrameRegistrationPlugin::notifyTransferringResources(
658-
JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) {
659-
auto SI = EHFrameRanges.find(SrcKey);
660-
if (SI == EHFrameRanges.end())
661-
return;
662-
663-
auto DI = EHFrameRanges.find(DstKey);
664-
if (DI != EHFrameRanges.end()) {
665-
auto &SrcRanges = SI->second;
666-
auto &DstRanges = DI->second;
667-
DstRanges.reserve(DstRanges.size() + SrcRanges.size());
668-
for (auto &SrcRange : SrcRanges)
669-
DstRanges.push_back(std::move(SrcRange));
670-
EHFrameRanges.erase(SI);
671-
} else {
672-
// We need to move SrcKey's ranges over without invalidating the SI
673-
// iterator.
674-
auto Tmp = std::move(SI->second);
675-
EHFrameRanges.erase(SI);
676-
EHFrameRanges[DstKey] = std::move(Tmp);
677-
}
678-
}
679-
680584
} // End namespace orc.
681585
} // End namespace llvm.

llvm/tools/lli/lli.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
2828
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
2929
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
30+
#include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
3031
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
3132
#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
3233
#include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
2424
#include "llvm/ExecutionEngine/Orc/Debugging/PerfSupportPlugin.h"
2525
#include "llvm/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.h"
26+
#include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
2627
#include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
2728
#include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
2829
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"

0 commit comments

Comments
 (0)