26 if (isa<Function>(GV)) {
27 auto &
F = cast<Function>(GV);
29 F.setPersonalityFn(
nullptr);
30 }
else if (isa<GlobalVariable>(GV)) {
31 cast<GlobalVariable>(GV).setInitializer(
nullptr);
32 }
else if (isa<GlobalAlias>(GV)) {
35 auto &
A = cast<GlobalAlias>(GV);
37 assert(
A.hasName() &&
"Anonymous alias?");
38 assert(Aliasee->hasName() &&
"Anonymous aliasee");
39 std::string AliasName =
A.getName();
41 if (isa<Function>(Aliasee)) {
43 A.replaceAllUsesWith(
F);
45 F->setName(AliasName);
46 }
else if (isa<GlobalVariable>(Aliasee)) {
48 *cast<GlobalVariable>(Aliasee));
49 A.replaceAllUsesWith(
G);
51 G->setName(AliasName);
59 auto &M = *NewTSMod.getModule();
60 M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());
80 std::move(SymbolFlags),
81 std::move(SymbolToDefinition)),
86 Parent.emitPartition(std::move(R), std::move(TSM),
87 std::move(SymbolToDefinition));
94 "ExtractingIRMaterializationUnit");
97 mutable std::mutex SourceModuleMutex;
103 return std::move(Requested);
114 :
IRLayer(ES), BaseLayer(BaseLayer), LCTMgr(LCTMgr),
115 BuildIndirectStubsManager(
std::move(BuildIndirectStubsManager)) {}
118 this->Partition = std::move(Partition);
138 for (
auto &GV : M.global_values()) {
139 if (GV.isDeclaration() || GV.hasLocalLinkage() || GV.hasAppendingLinkage())
142 auto Name = Mangle(GV.getName());
144 if (Flags.isCallable())
152 if (
auto Err = PDR.getImplDylib().define(
153 llvm::make_unique<PartitioningIRMaterializationUnit>(
155 ES.reportError(std::move(Err));
162 std::move(Callables)));
165 CompileOnDemandLayer::PerDylibResources &
166 CompileOnDemandLayer::getPerDylibResources(
JITDylib &TargetD) {
167 auto I = DylibResources.find(&TargetD);
168 if (
I == DylibResources.end()) {
170 TargetD.
getName() +
".impl",
false);
172 auto NewSearchOrder = TargetSearchOrder;
173 assert(!NewSearchOrder.empty() &&
174 NewSearchOrder.front().first == &TargetD &&
175 NewSearchOrder.front().second ==
true &&
176 "TargetD must be at the front of its own search order and match " 177 "non-exported symbol");
178 NewSearchOrder.insert(std::next(NewSearchOrder.begin()), {&ImplD,
true});
179 ImplD.setSearchOrder(std::move(NewSearchOrder),
false);
181 PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
182 I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
188 void CompileOnDemandLayer::cleanUpModule(
Module &M) {
190 if (
F.isDeclaration())
193 if (
F.hasAvailableExternallyLinkage()) {
195 F.setPersonalityFn(
nullptr);
201 void CompileOnDemandLayer::expandPartition(
GlobalValueSet &Partition) {
208 assert(!Partition.empty() &&
"Unexpected empty partition");
211 bool ContainsGlobalVariables =
false;
212 std::vector<const GlobalValue *> GVsToAdd;
214 for (
auto *GV : Partition)
215 if (isa<GlobalAlias>(GV))
217 cast<GlobalValue>(cast<GlobalAlias>(GV)->getAliasee()));
218 else if (isa<GlobalVariable>(GV))
219 ContainsGlobalVariables =
true;
221 for (
auto &A : M.aliases())
222 if (Partition.count(cast<GlobalValue>(A.getAliasee())))
223 GVsToAdd.push_back(&A);
225 if (ContainsGlobalVariables)
226 for (
auto &
G : M.globals())
227 GVsToAdd.push_back(&
G);
229 for (
auto *GV : GVsToAdd)
230 Partition.insert(GV);
233 void CompileOnDemandLayer::emitPartition(
246 assert(Defs.count(
Name) &&
"No definition for symbol");
247 RequestedGVs.insert(Defs[
Name]);
250 auto GVsToExtract = Partition(RequestedGVs);
255 if (GVsToExtract ==
None) {
257 BaseLayer.
emit(std::move(R), std::move(TSM));
262 if (GVsToExtract->empty()) {
263 R.
replace(llvm::make_unique<PartitioningIRMaterializationUnit>(
264 std::move(TSM), R.
getSymbols(), std::move(Defs), *
this));
272 auto PromotedGlobals = PromoteSymbols(*TSM.
getModule());
273 if (!PromotedGlobals.empty()) {
276 for (
auto &GV : PromotedGlobals)
277 SymbolFlags[Mangle(GV->getName())] =
280 ES.reportError(std::move(Err));
287 expandPartition(*GVsToExtract);
291 auto ShouldExtract = [&](
const GlobalValue &GV) ->
bool {
292 return GVsToExtract->count(&GV);
296 R.
replace(llvm::make_unique<PartitioningIRMaterializationUnit>(
299 BaseLayer.
emit(std::move(R), std::move(ExtractedTSM));
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
static ThreadSafeModule extractSubModule(ThreadSafeModule &TSM, StringRef Suffix, GVPredicate ShouldExtract)
This class represents lattice values for constants.
Error defineMaterializing(const SymbolFlagsMap &SymbolFlags)
Adds new symbols to the JITDylib and this responsibility instance.
A Module instance is used to store all the information related to an LLVM module. ...
static Optional< GlobalValueSet > compileRequested(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles all requested symbols (usually a single function at a time)...
void replace(std::unique_ptr< MaterializationUnit > MU)
Transfers responsibility to the given MaterializationUnit for all symbols defined by that Materializa...
Externally visible function.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilder
Builder for IndirectStubsManagers.
std::function< bool(const GlobalValue &)> GVPredicate
amdgpu Simplify well known AMD library false Value Value const Twine & Name
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
std::vector< std::pair< JITDylib *, bool > > JITDylibSearchList
A list of (JITDylib*, bool) pairs.
std::function< Optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
Mangles symbol names then uniques them in the context of an ExecutionSession.
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR...
auto withSearchOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchList &>()))
Do something with the search order (run under the session lock).
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, bool MatchNonExported=false, VModuleKey K=VModuleKey())
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
GlobalVariable * cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap=nullptr)
Clone a global variable declaration into a new module.
Pointer to a pooled string representing a symbol name.
virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
Module * getModule()
Get the module wrapped by this ThreadSafeModule.
iterator_range< iterator > functions()
VModuleKey getVModuleKey() const
Returns the VModuleKey for this instance.
This is an important base class in LLVM.
PartitioningIRMaterializationUnit(ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags, SymbolNameToDefinitionMap SymbolToDefinition, CompileOnDemandLayer &Parent)
An LLVM Module together with a shared ThreadSafeContext.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Interface for layers that accept LLVM IR.
Function * cloneFunctionDecl(Module &Dst, const Function &F, ValueToValueMapTy *VMap=nullptr)
Clone a function declaration into a new module.
JITDylib & createJITDylib(std::string Name, bool AddToMainDylibSearchOrder=true)
Add a new JITDylib to this ExecutionSession.
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, VModuleKey K=VModuleKey())
Define lazy-reexports based on the given SymbolAliasMap.
Module.h This file contains the declarations for the Module class.
static Optional< GlobalValueSet > compileWholeModule(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles whole modules whenever any symbol in them is requested...
SymbolNameSet getRequestedSymbols() const
Returns the names of any symbols covered by this MaterializationResponsibility object that have queri...
An ExecutionSession represents a running JIT program.
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, IndirectStubsManagerBuilder BuildIndirectStubsManager)
Construct a CompileOnDemandLayer.
void setPartitionFunction(PartitionFunction Partition)
Sets the partition function.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
const std::string & getName() const
Get the name for this JITDylib.
ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
PartitioningIRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM, VModuleKey K, CompileOnDemandLayer &Parent)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void failMaterialization()
Notify all not-yet-emitted covered by this MaterializationResponsibility instance that an error has o...
const SymbolFlagsMap & getSymbols()
Returns the symbol flags map for this responsibility instance.
Manages a set of 'lazy call-through' trampolines.
static const Function * getParent(const Value *V)
StringRef - Represent a constant reference to a string, i.e.
void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override
Emits the given module.
std::set< const GlobalValue * > GlobalValueSet
A symbol table that supports asynchoronous symbol queries.