[PATCH] D15272: [Verifier] Verifier that a GlobalValue is only used in this Module

Keno Fischer via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 23 08:39:01 PST 2015


loladiro updated this revision to Diff 43540.
loladiro added a comment.

Address review comments and rebase. Rebasing required changing a few uses() to
materialized_uses(). I hope that's the correct thing.


http://reviews.llvm.org/D15272

Files:
  lib/IR/Verifier.cpp
  unittests/IR/VerifierTest.cpp

Index: unittests/IR/VerifierTest.cpp
===================================================================
--- unittests/IR/VerifierTest.cpp
+++ unittests/IR/VerifierTest.cpp
@@ -64,7 +64,7 @@
   LLVMContext &C = getGlobalContext();
   Module M1("M1", C);
   Module M2("M2", C);
-  Module M3("M2", C);
+  Module M3("M3", C);
   FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
   Function *F1 = cast<Function>(M1.getOrInsertFunction("foo1", FTy));
   Function *F2 = cast<Function>(M2.getOrInsertFunction("foo2", FTy));
@@ -86,7 +86,21 @@
 
   std::string Error;
   raw_string_ostream ErrorOS(Error);
-  EXPECT_FALSE(verifyModule(M2, &ErrorOS));
+  EXPECT_TRUE(verifyModule(M2, &ErrorOS));
+  EXPECT_TRUE(StringRef(ErrorOS.str())
+                  .equals("Global is used by function in a different module\n"
+                          "i32 ()* @foo2\n"
+                          "; ModuleID = 'M2'\n"
+                          "i32 ()* @foo3\n"
+                          "; ModuleID = 'M3'\n"
+                          "Global is referenced in a different module!\n"
+                          "i32 ()* @foo2\n"
+                          "; ModuleID = 'M2'\n"
+                          "  %call = call i32 @foo2()\n"
+                          "i32 ()* @foo1\n"
+                          "; ModuleID = 'M1'\n"));
+
+  Error.clear();
   EXPECT_TRUE(verifyModule(M1, &ErrorOS));
   EXPECT_TRUE(StringRef(ErrorOS.str()).equals(
       "Referencing function in another module!\n"
Index: lib/IR/Verifier.cpp
===================================================================
--- lib/IR/Verifier.cpp
+++ lib/IR/Verifier.cpp
@@ -448,6 +448,22 @@
   InstVisitor<Verifier>::visit(I);
 }
 
+static void forEachInstructionAndFunctionUser(
+    const Use &TheUse, SmallPtrSet<const Value *, 32> &Visited,
+    llvm::function_ref<void(Instruction *)> InstCallback,
+    llvm::function_ref<void(Function *)> FuncCallback) {
+  Value *User = TheUse.getUser();
+  if (!Visited.insert(User).second)
+    return;
+  if (Instruction *I = dyn_cast<Instruction>(User))
+    InstCallback(I);
+  else if (Function *F = dyn_cast<Function>(User))
+    FuncCallback(F);
+  else
+    for (const Use &TheNextUse : User->materialized_uses())
+      forEachInstructionAndFunctionUser(TheNextUse, Visited, InstCallback,
+                                        FuncCallback);
+}
 
 void Verifier::visitGlobalValue(const GlobalValue &GV) {
   Assert(!GV.isDeclaration() || GV.hasExternalLinkage() ||
@@ -467,6 +483,29 @@
 
   if (GV.isDeclarationForLinker())
     Assert(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
+
+  // Verify that this GlobalValue is only used in this module.
+  // This map is used to avoid visiting uses twice. We can arrive at a user
+  // twice, if they have multiple operands. In particular for very large
+  // constant expressions, we can arrive at a particular user many times.
+  SmallPtrSet<const Value *, 32> Visited;
+  for (const Use &TheUse : GV.materialized_uses())
+    forEachInstructionAndFunctionUser(
+        TheUse, Visited,
+        [&](Instruction *I) {
+          Assert(I->getParent() && I->getParent()->getParent(),
+                 "Global is referenced by parentless instruction!", &GV,
+                 GV.getParent(), I);
+          Assert(I->getParent()->getParent()->getParent() == GV.getParent(),
+                 "Global is referenced in a different module!", &GV,
+                 GV.getParent(), I, I->getParent()->getParent(),
+                 I->getParent()->getParent()->getParent());
+        },
+        [&](Function *F) {
+          Assert(F->getParent() == GV.getParent(),
+                 "Global is used by function in a different module", &GV,
+                 GV.getParent(), F, F->getParent());
+        });
 }
 
 void Verifier::visitGlobalVariable(const GlobalVariable &GV) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15272.43540.patch
Type: text/x-patch
Size: 3878 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151223/29ee62e2/attachment.bin>


More information about the llvm-commits mailing list