[llvm] r265276 - ValueMapper: Disallow metadata mapping recursion through mapValue

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 3 13:54:52 PDT 2016


Author: dexonsmith
Date: Sun Apr  3 15:54:51 2016
New Revision: 265276

URL: http://llvm.org/viewvc/llvm-project?rev=265276&view=rev
Log:
ValueMapper: Disallow metadata mapping recursion through mapValue

This adds an assertion to maintain the property from r265273.  When
Mapper::mapSimpleMetadata calls Mapper::mapValue, it should not find its
way back to mapMetadataImpl.  This guarantees that mapSimpleMetadata is
not involved in any recursion.

Since Mapper::mapValue calls out to arbitrary materializers, we need to
save a bit on the ValueMap to make this assertion effective.

There should be no functionality change here.  This co-recursion should
already have been impossible.

Modified:
    llvm/trunk/include/llvm/IR/ValueMap.h
    llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp

Modified: llvm/trunk/include/llvm/IR/ValueMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/ValueMap.h?rev=265276&r1=265275&r2=265276&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/ValueMap.h (original)
+++ llvm/trunk/include/llvm/IR/ValueMap.h Sun Apr  3 15:54:51 2016
@@ -87,6 +87,9 @@ class ValueMap {
   MapT Map;
   std::unique_ptr<MDMapT> MDMap;
   ExtraData Data;
+
+  bool MayMapMetadata = true;
+
   ValueMap(const ValueMap&) = delete;
   ValueMap& operator=(const ValueMap&) = delete;
 public:
@@ -107,6 +110,10 @@ public:
     return *MDMap;
   }
 
+  bool mayMapMetadata() const { return MayMapMetadata; }
+  void enableMapMetadata() { MayMapMetadata = true; }
+  void disableMapMetadata() { MayMapMetadata = false; }
+
   /// Get the mapped metadata, if it's in the map.
   Optional<Metadata *> getMappedMD(const Metadata *MD) const {
     if (!MDMap)

Modified: llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp?rev=265276&r1=265275&r2=265276&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/ValueMapper.cpp Sun Apr  3 15:54:51 2016
@@ -379,7 +379,11 @@ Optional<Metadata *> Mapper::mapSimpleMe
       return mapToSelf(MD);
 
   if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
+    // Disallow recursion into metadata mapping through mapValue.
+    VM.disableMapMetadata();
     Value *MappedV = mapValue(VMD->getValue());
+    VM.enableMapMetadata();
+
     if (VMD->getValue() == MappedV ||
         (!MappedV && (Flags & RF_IgnoreMissingEntries)))
       return mapToSelf(MD);
@@ -406,6 +410,7 @@ Optional<Metadata *> Mapper::mapSimpleMe
 }
 
 Metadata *Mapper::mapMetadataImpl(const Metadata *MD) {
+  assert(VM.mayMapMetadata() && "Unexpected co-recursion through mapValue");
   if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
     return *NewMD;
 




More information about the llvm-commits mailing list