LLVM 20.0.0git
CompileOnDemandLayer.cpp
Go to the documentation of this file.
1//===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
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
11#include "llvm/IR/Module.h"
12
13using namespace llvm;
14using namespace llvm::orc;
15
17 ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr,
18 IndirectStubsManagerBuilder BuildIndirectStubsManager)
19 : IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer),
20 LCTMgr(LCTMgr),
21 BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}
22
24 this->AliaseeImpls = Imp;
25}
26
28 std::unique_ptr<MaterializationResponsibility> R, ThreadSafeModule TSM) {
29 assert(TSM && "Null module");
30
31 auto &ES = getExecutionSession();
32
33 // Sort the callables and non-callables, build re-exports and lodge the
34 // actual module with the implementation dylib.
35 auto &PDR = getPerDylibResources(R->getTargetJITDylib());
36
37 SymbolAliasMap NonCallables;
38 SymbolAliasMap Callables;
39
40 for (auto &KV : R->getSymbols()) {
41 auto &Name = KV.first;
42 auto &Flags = KV.second;
43 if (Flags.isCallable())
44 Callables[Name] = SymbolAliasMapEntry(Name, Flags);
45 else
46 NonCallables[Name] = SymbolAliasMapEntry(Name, Flags);
47 }
48
49 // Lodge symbols with the implementation dylib.
50 if (auto Err = PDR.getImplDylib().define(
51 std::make_unique<BasicIRLayerMaterializationUnit>(
52 BaseLayer, *getManglingOptions(), std::move(TSM)))) {
53 ES.reportError(std::move(Err));
54 R->failMaterialization();
55 return;
56 }
57
58 if (!NonCallables.empty())
59 if (auto Err =
60 R->replace(reexports(PDR.getImplDylib(), std::move(NonCallables),
62 getExecutionSession().reportError(std::move(Err));
63 R->failMaterialization();
64 return;
65 }
66 if (!Callables.empty()) {
67 if (auto Err = R->replace(
68 lazyReexports(LCTMgr, PDR.getISManager(), PDR.getImplDylib(),
69 std::move(Callables), AliaseeImpls))) {
70 getExecutionSession().reportError(std::move(Err));
71 R->failMaterialization();
72 return;
73 }
74 }
75}
76
77CompileOnDemandLayer::PerDylibResources &
78CompileOnDemandLayer::getPerDylibResources(JITDylib &TargetD) {
79 std::lock_guard<std::mutex> Lock(CODLayerMutex);
80
81 auto I = DylibResources.find(&TargetD);
82 if (I == DylibResources.end()) {
83 auto &ImplD =
84 getExecutionSession().createBareJITDylib(TargetD.getName() + ".impl");
85 JITDylibSearchOrder NewLinkOrder;
86 TargetD.withLinkOrderDo([&](const JITDylibSearchOrder &TargetLinkOrder) {
87 NewLinkOrder = TargetLinkOrder;
88 });
89
90 assert(!NewLinkOrder.empty() && NewLinkOrder.front().first == &TargetD &&
91 NewLinkOrder.front().second ==
93 "TargetD must be at the front of its own search order and match "
94 "non-exported symbol");
95 NewLinkOrder.insert(std::next(NewLinkOrder.begin()),
96 {&ImplD, JITDylibLookupFlags::MatchAllSymbols});
97 ImplD.setLinkOrder(NewLinkOrder, false);
98 TargetD.setLinkOrder(std::move(NewLinkOrder), false);
99
100 PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
101 I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
102 }
103
104 return I->second;
105}
std::string Name
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool empty() const
Definition: DenseMap.h:98
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilder
Builder for IndirectStubsManagers.
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emits the given module.
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, IndirectStubsManagerBuilder BuildIndirectStubsManager)
Construct a CompileOnDemandLayer.
void setImplMap(ImplSymbolMap *Imp)
Sets the ImplSymbolMap.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1339
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:1474
JITDylib & createBareJITDylib(std::string Name)
Add a new bare JITDylib to this ExecutionSession.
Definition: Core.cpp:1650
Interface for layers that accept LLVM IR.
Definition: Layer.h:67
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
Definition: Layer.h:75
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:78
Represents a JIT'd dynamic library.
Definition: Core.h:897
auto withLinkOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder & >()))
Do something with the link order (run under the session lock).
Definition: Core.h:1815
void setLinkOrder(JITDylibSearchOrder NewSearchOrder, bool LinkAgainstThisJITDylibFirst=true)
Set the link order to be used when fixing up definitions in JITDylib.
Definition: Core.cpp:1004
Manages a set of 'lazy call-through' trampolines.
Definition: LazyReexports.h:39
An LLVM Module together with a shared ThreadSafeContext.
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition: Core.h:173
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, JITDylibLookupFlags SourceJDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
Definition: Core.h:754
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, RedirectableSymbolManager &RSManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc=nullptr)
Define lazy-reexports based on the given SymbolAliasMap.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858