25 if (isa<Function>(GV)) {
26 auto &
F = cast<Function>(GV);
28 F.setPersonalityFn(
nullptr);
29 }
else if (isa<GlobalVariable>(GV)) {
30 cast<GlobalVariable>(GV).setInitializer(
nullptr);
31 }
else if (isa<GlobalAlias>(GV)) {
34 auto &
A = cast<GlobalAlias>(GV);
36 assert(
A.hasName() &&
"Anonymous alias?");
37 assert(Aliasee->hasName() &&
"Anonymous aliasee");
38 std::string AliasName =
A.getName();
40 if (isa<Function>(Aliasee)) {
42 A.replaceAllUsesWith(
F);
44 F->setName(AliasName);
45 }
else if (isa<GlobalVariable>(Aliasee)) {
47 *cast<GlobalVariable>(Aliasee));
48 A.replaceAllUsesWith(
G);
50 G->setName(AliasName);
58 NewTSM.withModuleDo([&](
Module &M) {
82 std::move(SymbolFlags),
83 std::move(SymbolToDefinition)),
88 Parent.emitPartition(std::move(R), std::move(TSM),
89 std::move(SymbolToDefinition));
96 "ExtractingIRMaterializationUnit");
99 mutable std::mutex SourceModuleMutex;
105 return std::move(Requested);
116 :
IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer),
118 BuildIndirectStubsManager(
std::move(BuildIndirectStubsManager)) {}
121 this->Partition = std::move(Partition);
125 this->AliaseeImpls = Imp;
129 assert(TSM &&
"Null module");
145 auto &
Name = KV.first;
146 auto &Flags = KV.second;
147 if (Flags.isCallable())
155 if (
auto Err = PDR.getImplDylib().define(
156 std::make_unique<PartitioningIRMaterializationUnit>(
159 ES.reportError(std::move(Err));
167 std::move(Callables), AliaseeImpls));
170 CompileOnDemandLayer::PerDylibResources &
171 CompileOnDemandLayer::getPerDylibResources(
JITDylib &TargetD) {
172 auto I = DylibResources.find(&TargetD);
173 if (
I == DylibResources.end()) {
178 auto NewSearchOrder = TargetSearchOrder;
180 !NewSearchOrder.empty() &&
181 NewSearchOrder.front().first == &TargetD &&
182 NewSearchOrder.front().second ==
184 "TargetD must be at the front of its own search order and match " 185 "non-exported symbol");
186 NewSearchOrder.insert(std::next(NewSearchOrder.begin()),
188 ImplD.setSearchOrder(std::move(NewSearchOrder),
false);
190 PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
191 I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
197 void CompileOnDemandLayer::cleanUpModule(
Module &M) {
199 if (
F.isDeclaration())
202 if (
F.hasAvailableExternallyLinkage()) {
204 F.setPersonalityFn(
nullptr);
210 void CompileOnDemandLayer::expandPartition(
GlobalValueSet &Partition) {
217 assert(!Partition.empty() &&
"Unexpected empty partition");
220 bool ContainsGlobalVariables =
false;
221 std::vector<const GlobalValue *> GVsToAdd;
223 for (
auto *GV : Partition)
224 if (isa<GlobalAlias>(GV))
226 cast<GlobalValue>(cast<GlobalAlias>(GV)->getAliasee()));
227 else if (isa<GlobalVariable>(GV))
228 ContainsGlobalVariables =
true;
230 for (
auto &A : M.aliases())
231 if (Partition.count(cast<GlobalValue>(A.getAliasee())))
232 GVsToAdd.push_back(&A);
234 if (ContainsGlobalVariables)
235 for (
auto &
G : M.globals())
236 GVsToAdd.push_back(&
G);
238 for (
auto *GV : GVsToAdd)
239 Partition.insert(GV);
242 void CompileOnDemandLayer::emitPartition(
254 assert(Defs.count(
Name) &&
"No definition for symbol");
255 RequestedGVs.insert(Defs[
Name]);
266 if (GVsToExtract ==
None) {
268 BaseLayer.
emit(std::move(R), std::move(TSM));
273 if (GVsToExtract->empty()) {
274 R.
replace(std::make_unique<PartitioningIRMaterializationUnit>(
275 std::move(TSM), R.
getSymbols(), std::move(Defs), *
this));
289 auto PromotedGlobals = PromoteSymbols(M);
290 if (!PromotedGlobals.empty()) {
293 for (
auto &GV : PromotedGlobals)
294 SymbolFlags[Mangle(GV->getName())] =
297 return std::move(Err);
300 expandPartition(*GVsToExtract);
304 auto ShouldExtract = [&](
const GlobalValue &GV) ->
bool {
305 return GVsToExtract->count(&GV);
312 ES.reportError(ExtractedTSM.takeError());
317 R.
replace(std::make_unique<PartitioningIRMaterializationUnit>(
319 BaseLayer.
emit(std::move(R), std::move(*ExtractedTSM));
JITDylib & createJITDylib(std::string Name)
Add a new JITDylib to this ExecutionSession.
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.
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)...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void setModuleIdentifier(StringRef ID)
Set the module identifier.
void replace(std::unique_ptr< MaterializationUnit > MU)
Transfers responsibility to the given MaterializationUnit for all symbols defined by that Materializa...
auto withModuleDo(Func &&F) -> decltype(F(std::declval< Module &>()))
Locks the associated ThreadSafeContext and calls the given function on the contained Module...
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup...
Externally visible function.
std::function< std::unique_ptr< IndirectStubsManager >()> IndirectStubsManagerBuilder
Builder for IndirectStubsManagers.
std::function< bool(const GlobalValue &)> GVPredicate
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
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
auto withSearchOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder &>()))
Do something with the search order (run under the session lock).
Tagged union holding either a T or a Error.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR...
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
const IRMaterializationUnit::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
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.
iterator_range< iterator > functions()
VModuleKey getVModuleKey() const
Returns the VModuleKey for this instance.
This is an important base class in LLVM.
std::unique_ptr< LazyReexportsMaterializationUnit > lazyReexports(LazyCallThroughManager &LCTManager, IndirectStubsManager &ISManager, JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc=nullptr, VModuleKey K=VModuleKey())
Define lazy-reexports based on the given SymbolAliasMap.
PartitioningIRMaterializationUnit(ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags, SymbolNameToDefinitionMap SymbolToDefinition, CompileOnDemandLayer &Parent)
An LLVM Module together with a shared ThreadSafeContext.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setImplMap(ImplSymbolMap *Imp)
Sets the ImplSymbolMap.
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.
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...
PartitioningIRMaterializationUnit(ExecutionSession &ES, const ManglingOptions &MO, ThreadSafeModule TSM, VModuleKey K, CompileOnDemandLayer &Parent)
SymbolNameSet getRequestedSymbols() const
Returns the names of any symbols covered by this MaterializationResponsibility object that have queri...
An ExecutionSession represents a running JIT program.
const SymbolFlagsMap & getSymbols() const
Returns the symbol flags map for this responsibility instance.
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr, IndirectStubsManagerBuilder BuildIndirectStubsManager)
Construct a CompileOnDemandLayer.
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
std::unique_ptr< ReExportsMaterializationUnit > reexports(JITDylib &SourceJD, SymbolAliasMap Aliases, JITDylibLookupFlags SourceJDLookupFlags=JITDylibLookupFlags::MatchExportedSymbolsOnly, VModuleKey K=VModuleKey())
Create a materialization unit for re-exporting symbols from another JITDylib with alternative names/f...
void setPartitionFunction(PartitionFunction Partition)
Sets the partition function.
Error defineMaterializing(SymbolFlagsMap SymbolFlags)
Attempt to claim responsibility for new definitions.
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void failMaterialization()
Notify all not-yet-emitted covered by this MaterializationResponsibility instance that an error has o...
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.