Skip to content

Commit fa35c1f

Browse files
committed
ValueMapper: Rename RF_MoveDistinctMDs => RF_ReuseAndMutateDistinctMDs, NFC
Rename the `RF_MoveDistinctMDs` flag passed into `MapValue` and `MapMetadata` to `RF_ReuseAndMutateDistinctMDs` in order to more precisely describe its effect and clarify the header documentation. Found this while helping to investigate PR48841, which pointed out an unsound use of the flag in `CloneModule()`. For now I've just added a FIXME there, but I'm hopeful that the new (more precise) name will prevent other similar errors.
1 parent edd365c commit fa35c1f

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

llvm/include/llvm/Transforms/Utils/ValueMapper.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ enum RemapFlags {
8989
/// \a MapMetadata() always ignores this flag.
9090
RF_IgnoreMissingLocals = 2,
9191

92-
/// Instruct the remapper to move distinct metadata instead of duplicating it
93-
/// when there are module-level changes.
94-
RF_MoveDistinctMDs = 4,
92+
/// Instruct the remapper to reuse and mutate distinct metadata (remapping
93+
/// them in place) instead of cloning remapped copies. This flag has no
94+
/// effect when when RF_NoModuleLevelChanges, since that implies an identity
95+
/// mapping.
96+
RF_ReuseAndMutateDistinctMDs = 4,
9597

9698
/// Any global values not in value map are mapped to null instead of mapping
9799
/// to self. Illegal if RF_IgnoreMissingLocals is also set.

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,10 @@ template <> struct MDNodeSubsetEqualImpl<DISubprogram> {
784784

785785
// Compare to the RHS.
786786
// FIXME: We need to compare template parameters here to avoid incorrect
787-
// collisions in mapMetadata when RF_MoveDistinctMDs and a ODR-DISubprogram
788-
// has a non-ODR template parameter (i.e., a DICompositeType that does not
789-
// have an identifier). Eventually we should decouple ODR logic from
790-
// uniquing logic.
787+
// collisions in mapMetadata when RF_ReuseAndMutateDistinctMDs and a
788+
// ODR-DISubprogram has a non-ODR template parameter (i.e., a
789+
// DICompositeType that does not have an identifier). Eventually we should
790+
// decouple ODR logic from uniquing logic.
791791
return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() &&
792792
LinkageName == RHS->getRawLinkageName() &&
793793
TemplateParams == RHS->getRawTemplateParams();

llvm/lib/Linker/IRMover.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ class IRLinker {
520520
: DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
521521
TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
522522
SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),
523-
Mapper(ValueMap, RF_MoveDistinctMDs | RF_IgnoreMissingLocals, &TypeMap,
524-
&GValMaterializer),
523+
Mapper(ValueMap, RF_ReuseAndMutateDistinctMDs | RF_IgnoreMissingLocals,
524+
&TypeMap, &GValMaterializer),
525525
IndirectSymbolMCID(Mapper.registerAlternateMappingContext(
526526
IndirectSymbolValueMap, &LValMaterializer)) {
527527
ValueMap.getMDMap() = std::move(SharedMDs);

llvm/lib/Transforms/Utils/CloneModule.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ std::unique_ptr<Module> llvm::CloneModule(
120120

121121
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
122122
G.getAllMetadata(MDs);
123+
124+
// FIXME: Stop using RF_ReuseAndMutateDistinctMDs here, since it's unsound
125+
// to mutate metadata that is still referenced by the source module unless
126+
// the source is about to be discarded (see IRMover for a valid use).
123127
for (auto MD : MDs)
124-
GV->addMetadata(MD.first,
125-
*MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
128+
GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap,
129+
RF_ReuseAndMutateDistinctMDs));
126130

127131
if (G.isDeclaration())
128132
continue;

llvm/lib/Transforms/Utils/ValueMapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
547547
assert(N.isDistinct() && "Expected a distinct node");
548548
assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
549549
DistinctWorklist.push_back(
550-
cast<MDNode>((M.Flags & RF_MoveDistinctMDs)
550+
cast<MDNode>((M.Flags & RF_ReuseAndMutateDistinctMDs)
551551
? M.mapToSelf(&N)
552552
: M.mapToMetadata(&N, cloneOrBuildODR(N))));
553553
return DistinctWorklist.back();

llvm/unittests/Transforms/Utils/ValueMapperTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ TEST(ValueMapperTest, mapMDNodeDistinct) {
124124
{
125125
// The node should be moved.
126126
ValueToValueMapTy VM;
127-
EXPECT_EQ(D, ValueMapper(VM, RF_MoveDistinctMDs).mapMDNode(*D));
127+
EXPECT_EQ(D, ValueMapper(VM, RF_ReuseAndMutateDistinctMDs).mapMDNode(*D));
128128
}
129129
}
130130

@@ -139,7 +139,7 @@ TEST(ValueMapperTest, mapMDNodeDistinctOperands) {
139139
VM.MD()[Old].reset(New);
140140

141141
// Make sure operands are updated.
142-
EXPECT_EQ(D, ValueMapper(VM, RF_MoveDistinctMDs).mapMDNode(*D));
142+
EXPECT_EQ(D, ValueMapper(VM, RF_ReuseAndMutateDistinctMDs).mapMDNode(*D));
143143
EXPECT_EQ(New, D->getOperand(0));
144144
}
145145

0 commit comments

Comments
 (0)