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?");
38 std::string AliasName = std::string(
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) {
59 M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());
84 void materialize(std::unique_ptr<MaterializationResponsibility> R)
override {
85 Parent.emitPartition(std::move(R), std::move(
TSM),
93 "ExtractingIRMaterializationUnit");
103 :
IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer) {}
106 this->Partition = Partition;
109std::optional<IRPartitionLayer::GlobalValueSet>
111 return std::move(Requested);
114std::optional<IRPartitionLayer::GlobalValueSet>
121 assert(TSM &&
"Null module");
130 if (
auto Err = R->replace(std::make_unique<PartitioningIRMaterializationUnit>(
132 ES.reportError(std::move(Err));
133 R->failMaterialization();
138void IRPartitionLayer::cleanUpModule(
Module &M) {
139 for (
auto &
F : M.functions()) {
140 if (
F.isDeclaration())
143 if (
F.hasAvailableExternallyLinkage()) {
145 F.setPersonalityFn(
nullptr);
151void IRPartitionLayer::expandPartition(GlobalValueSet &Partition) {
158 assert(!Partition.empty() &&
"Unexpected empty partition");
161 bool ContainsGlobalVariables =
false;
162 std::vector<const GlobalValue *> GVsToAdd;
164 for (
const auto *GV : Partition)
165 if (isa<GlobalAlias>(GV))
167 cast<GlobalValue>(cast<GlobalAlias>(GV)->getAliasee()));
168 else if (isa<GlobalVariable>(GV))
169 ContainsGlobalVariables =
true;
171 for (
auto &
A :
M.aliases())
172 if (Partition.count(cast<GlobalValue>(
A.getAliasee())))
173 GVsToAdd.push_back(&
A);
175 if (ContainsGlobalVariables)
176 for (
auto &
G :
M.globals())
177 GVsToAdd.push_back(&
G);
179 for (
const auto *GV : GVsToAdd)
183void IRPartitionLayer::emitPartition(
194 for (
auto &
Name :
R->getRequestedSymbols()) {
195 if (
Name ==
R->getInitializerSymbol())
198 RequestedGVs.insert(&GV);
201 assert(Defs.count(
Name) &&
"No definition for symbol");
202 RequestedGVs.insert(Defs[
Name]);
214 if (GVsToExtract == std::nullopt) {
216 BaseLayer.
emit(std::move(R), std::move(TSM));
221 if (GVsToExtract->empty()) {
223 R->replace(std::make_unique<PartitioningIRMaterializationUnit>(
226 R->getInitializerSymbol()),
227 std::move(Defs), *
this))) {
229 R->failMaterialization();
244 auto PromotedGlobals = PromoteSymbols(M);
245 if (!PromotedGlobals.empty()) {
252 if (
auto Err =
R->defineMaterializing(SymbolFlags))
253 return std::move(Err);
256 expandPartition(*GVsToExtract);
259 std::string SubModuleName;
261 std::vector<const GlobalValue *> HashGVs;
262 HashGVs.reserve(GVsToExtract->size());
263 for (
const auto *GV : *GVsToExtract)
264 HashGVs.push_back(GV);
269 for (
const auto *GV : HashGVs) {
270 assert(GV->hasName() &&
"All GVs to extract should be named by now");
271 auto GVName = GV->getName();
276 <<
formatv(
sizeof(
size_t) == 8 ?
"{0:x16}" :
"{0:x8}",
277 static_cast<size_t>(HC))
283 auto ShouldExtract = [&](
const GlobalValue &GV) ->
bool {
284 return GVsToExtract->count(&GV);
291 ES.reportError(ExtractedTSM.takeError());
292 R->failMaterialization();
296 if (
auto Err =
R->replace(std::make_unique<PartitioningIRMaterializationUnit>(
298 ES.reportError(std::move(Err));
299 R->failMaterialization();
302 BaseLayer.
emit(std::move(R), std::move(*ExtractedTSM));
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static ThreadSafeModule extractSubModule(ThreadSafeModule &TSM, StringRef Suffix, GVPredicate ShouldExtract)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is an important base class in LLVM.
Tagged union holding either a T or a Error.
Function::iterator insert(Function::iterator Position, BasicBlock *BB)
Insert BB in the basic block list at Position.
@ ExternalLinkage
Externally visible function.
A Module instance is used to store all the information related to an LLVM module.
StringRef - Represent a constant reference to a string, i.e.
StringRef getName() const
Return a constant reference to the value's name.
An opaque object representing a hash code.
An ExecutionSession represents a running JIT program.
void reportError(Error Err)
Report a error for this execution session.
Interface for layers that accept LLVM IR.
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR.
SymbolNameToDefinitionMap SymbolToDefinition
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
A layer that breaks up IR modules into smaller submodules that only contains looked up symbols.
void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM) override
Emits the given module.
static std::optional< GlobalValueSet > compileWholeModule(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles whole modules whenever any symbol in them is requested.
std::function< std::optional< GlobalValueSet >(GlobalValueSet Requested)> PartitionFunction
Partitioning function.
std::set< const GlobalValue * > GlobalValueSet
IRPartitionLayer(ExecutionSession &ES, IRLayer &BaseLayer)
Construct a IRPartitionLayer.
void setPartitionFunction(PartitionFunction Partition)
Sets the partition function.
static std::optional< GlobalValueSet > compileRequested(GlobalValueSet Requested)
Off-the-shelf partitioning which compiles all requested symbols (usually a single function at a time)...
static void add(ExecutionSession &ES, const ManglingOptions &MO, ArrayRef< GlobalValue * > GVs, SymbolFlagsMap &SymbolFlags, SymbolNameToDefinitionMap *SymbolToDefinition=nullptr)
Add mangled symbols for the given GlobalValues to SymbolFlags.
Represents a JIT'd dynamic library.
Mangles symbol names then uniques them in the context of an ExecutionSession.
PartitioningIRMaterializationUnit(ThreadSafeModule TSM, Interface I, SymbolNameToDefinitionMap SymbolToDefinition, IRPartitionLayer &Parent)
PartitioningIRMaterializationUnit(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM, IRPartitionLayer &Parent)
Pointer to a pooled string representing a symbol name.
An LLVM Module together with a shared ThreadSafeContext.
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
iterator_range< StaticInitGVIterator > getStaticInitGVs(Module &M)
Create an iterator range over the GlobalValues that contribute to static initialization.
std::function< bool(const GlobalValue &)> GVPredicate
GlobalVariable * cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap=nullptr)
Clone a global variable declaration into a new module.
Function * cloneFunctionDecl(Module &Dst, const Function &F, ValueToValueMapTy *VMap=nullptr)
Clone a function declaration into a new module.
ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
void sort(IteratorTy Start, IteratorTy End)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Implement std::hash so that hash_code can be used in STL containers.