[llvm] r235784 - Verifier: Function metadata attachments require a body

Duncan P. N. Exon Smith dexonsmith at apple.com
Fri Apr 24 14:53:27 PDT 2015


Author: dexonsmith
Date: Fri Apr 24 16:53:27 2015
New Revision: 235784

URL: http://llvm.org/viewvc/llvm-project?rev=235784&view=rev
Log:
Verifier: Function metadata attachments require a body

Add a verifier check that only functions with bodies have metadata
attachments.  This should help catch bugs in frontends and
transformation passes.  Part of PR23340.

Modified:
    llvm/trunk/lib/IR/Verifier.cpp
    llvm/trunk/unittests/IR/MetadataTest.cpp

Modified: llvm/trunk/lib/IR/Verifier.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Verifier.cpp?rev=235784&r1=235783&r2=235784&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Verifier.cpp (original)
+++ llvm/trunk/lib/IR/Verifier.cpp Fri Apr 24 16:53:27 2015
@@ -1669,11 +1669,20 @@ void Verifier::visitFunction(const Funct
              "Function takes metadata but isn't an intrinsic", I, &F);
   }
 
+  // Get the function metadata attachments.
+  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
+  F.getAllMetadata(MDs);
+  assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
+
   if (F.isMaterializable()) {
     // Function has a body somewhere we can't see.
+    Assert(MDs.empty(), "unmaterialized function cannot have metadata", &F,
+           MDs.empty() ? nullptr : MDs.front().second);
   } else if (F.isDeclaration()) {
     Assert(F.hasExternalLinkage() || F.hasExternalWeakLinkage(),
            "invalid linkage type for function declaration", &F);
+    Assert(MDs.empty(), "function without a body cannot have metadata", &F,
+           MDs.empty() ? nullptr : MDs.front().second);
   } else {
     // Verify that this function (which has a body) is not named "llvm.*".  It
     // is not legal to define intrinsics.
@@ -1689,6 +1698,10 @@ void Verifier::visitFunction(const Funct
       Assert(!BlockAddress::lookup(Entry)->isConstantUsed(),
              "blockaddress may not be used with the entry block!", Entry);
     }
+
+    // Visit metadata attachments.
+    for (const auto &I : MDs)
+      visitMDNode(*I.second);
   }
 
   // If this function is actually an intrinsic, verify that it is only used in

Modified: llvm/trunk/unittests/IR/MetadataTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/MetadataTest.cpp?rev=235784&r1=235783&r2=235784&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/MetadataTest.cpp (original)
+++ llvm/trunk/unittests/IR/MetadataTest.cpp Fri Apr 24 16:53:27 2015
@@ -17,6 +17,7 @@
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Type.h"
+#include "llvm/IR/Verifier.h"
 #include "llvm/Support/raw_ostream.h"
 #include "gtest/gtest.h"
 using namespace llvm;
@@ -2233,4 +2234,21 @@ TEST_F(FunctionAttachmentTest, dropUnkno
   EXPECT_FALSE(F->hasMetadata());
 }
 
+TEST_F(FunctionAttachmentTest, Verifier) {
+  Function *F = getFunction("foo");
+  F->setMetadata("attach", getTuple());
+
+  // Confirm this has no body.
+  ASSERT_TRUE(F->empty());
+
+  // Functions without a body cannot have metadata attachments (they also can't
+  // be verified directly, so check that the module fails to verify).
+  EXPECT_TRUE(verifyModule(*F->getParent()));
+
+  // Functions with a body can.
+  (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
+  EXPECT_FALSE(verifyModule(*F->getParent()));
+  EXPECT_FALSE(verifyFunction(*F));
+}
+
 }





More information about the llvm-commits mailing list