summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Linker/IRMover.cpp35
1 files changed, 30 insertions, 5 deletions
diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp
index 70d5262fd61..b21b8f33ab4 100644
--- a/llvm/lib/Linker/IRMover.cpp
+++ b/llvm/lib/Linker/IRMover.cpp
@@ -402,6 +402,7 @@ class IRLinker {
DenseSet<GlobalValue *> ValuesToLink;
std::vector<GlobalValue *> Worklist;
+ std::vector<std::pair<GlobalValue *, Value*>> RAUWWorklist;
void maybeAdd(GlobalValue *GV) {
if (ValuesToLink.insert(GV).second)
@@ -494,6 +495,14 @@ class IRLinker {
Function *copyFunctionProto(const Function *SF);
GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
+ /// Perform "replace all uses with" operations. These work items need to be
+ /// performed as part of materialization, but we postpone them to happen after
+ /// materialization is done. The materializer called by ValueMapper is not
+ /// expected to delete constants, as ValueMapper is holding pointers to some
+ /// of them, but constant destruction may be indirectly triggered by RAUW.
+ /// Hence, the need to move this out of the materialization call chain.
+ void flushRAUWWorklist();
+
/// When importing for ThinLTO, prevent importing of types listed on
/// the DICompileUnit that we don't need a copy of in the importing
/// module.
@@ -883,8 +892,8 @@ IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
// Replace any uses of the two global variables with uses of the new
// global.
if (DstGV) {
- DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
- DstGV->eraseFromParent();
+ RAUWWorklist.push_back(
+ std::make_pair(DstGV, ConstantExpr::getBitCast(NG, DstGV->getType())));
}
return Ret;
@@ -983,9 +992,12 @@ Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
}
if (DGV && NewGV != DGV) {
- DGV->replaceAllUsesWith(
- ConstantExpr::getPointerBitCastOrAddrSpaceCast(NewGV, DGV->getType()));
- DGV->eraseFromParent();
+ // Schedule "replace all uses with" to happen after materializing is
+ // done. It is not safe to do it now, since ValueMapper may be holding
+ // pointers to constants that will get deleted if RAUW runs.
+ RAUWWorklist.push_back(std::make_pair(
+ DGV,
+ ConstantExpr::getPointerBitCastOrAddrSpaceCast(NewGV, DGV->getType())));
}
return C;
@@ -1043,6 +1055,18 @@ Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
return Error::success();
}
+void IRLinker::flushRAUWWorklist() {
+ for (const auto Elem : RAUWWorklist) {
+ GlobalValue *Old;
+ Value *New;
+ std::tie(Old, New) = Elem;
+
+ Old->replaceAllUsesWith(New);
+ Old->eraseFromParent();
+ }
+ RAUWWorklist.clear();
+}
+
void IRLinker::prepareCompileUnitsForImport() {
NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");
if (!SrcCompileUnits)
@@ -1368,6 +1392,7 @@ Error IRLinker::run() {
Mapper.mapValue(*GV);
if (FoundError)
return std::move(*FoundError);
+ flushRAUWWorklist();
}
// Note that we are done linking global value bodies. This prevents
OpenPOWER on IntegriCloud