As suggested in review for r255909, add a way to ensure that temporary
MD used as keys in the MetadataToID map during ThinLTO importing are not
RAUWed.
Add support for marking an MDNode as not replaceable. Clear the new
CanReplace flag when adding a temporary MD node to the MetadataToID map
and clear it when destroying the map.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256648
91177308-0d34-0410-b5e6-
96231b3b80d8
LLVMContext &Context;
uint64_t NextIndex;
SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap;
+ /// Flag that can be set to false if this metadata should not be
+ /// RAUW'ed, e.g. if it is used as the key of a map.
+ bool CanReplace;
public:
ReplaceableMetadataImpl(LLVMContext &Context)
- : Context(Context), NextIndex(0) {}
+ : Context(Context), NextIndex(0), CanReplace(true) {}
~ReplaceableMetadataImpl() {
assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
}
+ /// Set the CanReplace flag to the given value.
+ void setCanReplace(bool Replaceable) { CanReplace = Replaceable; }
+
LLVMContext &getContext() const { return Context; }
/// \brief Replace all uses of this with MD.
Context.getReplaceableUses()->replaceAllUsesWith(MD);
}
+ /// Set the CanReplace flag to the given value.
+ void setCanReplace(bool Replaceable) {
+ Context.getReplaceableUses()->setCanReplace(Replaceable);
+ }
+
/// \brief Resolve cycles.
///
/// Once all forward declarations have been resolved, force cycles to be
assert(MetadataToIDs[MD] == ID && "Inconsistent metadata value id");
continue;
}
+ if (N && N->isTemporary())
+ // Ensure that we assert if someone tries to RAUW this temporary
+ // metadata while it is the key of a map. The flag will be set back
+ // to true when the saved metadata list is destroyed.
+ N->setCanReplace(false);
MetadataToIDs[MD] = ID;
}
}
void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) {
assert(!(MD && isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary()) &&
"Expected non-temp node");
+ assert(CanReplace &&
+ "Attempted to replace Metadata marked for no replacement");
if (UseMap.empty())
return;
ValueMapperFlags = ValueMapperFlags | RF_HaveUnmaterializedMetadata;
}
+ ~IRLinker() {
+ // In the case where we are not linking metadata, we unset the CanReplace
+ // flag on all temporary metadata in the MetadataToIDs map to ensure
+ // none was replaced while being a map key. Now that we are destructing
+ // the map, set the flag back to true, so that it is replaceable during
+ // metadata linking.
+ if (!shouldLinkMetadata()) {
+ for (auto MDI : MetadataToIDs) {
+ Metadata *MD = const_cast<Metadata *>(MDI.first);
+ MDNode *Node = dyn_cast<MDNode>(MD);
+ assert((Node && Node->isTemporary()) &&
+ "Found non-temp metadata in map when not linking metadata");
+ Node->setCanReplace(true);
+ }
+ }
+ }
+
bool run();
Value *materializeDeclFor(Value *V, bool ForAlias);
void materializeInitFor(GlobalValue *New, GlobalValue *Old, bool ForAlias);