[PATCH] D53050: [IR] Make TypeFinder check function metadata attachments.

Sean Bartell via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 9 15:52:27 PDT 2018


bartell created this revision.
bartell added a reviewer: lattner.
Herald added subscribers: llvm-commits, mgorny.

Fixes PR39237.


Repository:
  rL LLVM

https://reviews.llvm.org/D53050

Files:
  lib/IR/TypeFinder.cpp
  unittests/IR/CMakeLists.txt
  unittests/IR/TypeFinderTest.cpp


Index: unittests/IR/TypeFinderTest.cpp
===================================================================
--- /dev/null
+++ unittests/IR/TypeFinderTest.cpp
@@ -0,0 +1,43 @@
+//===- llvm/unittest/IR/TypeFinderTest.cpp - TypeFinder unit tests --------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/TypeFinder.h"
+
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using ::testing::UnorderedElementsAre;
+
+namespace {
+
+TEST(TypeFinderTest, FunctionMetadata) {
+  LLVMContext C;
+
+  // PR39237
+  Module M("Test", C);
+  Function *F = cast<Function>(M.getOrInsertFunction(
+      "foo", FunctionType::get(Type::getVoidTy(C), None, false)));
+  StructType *Struct = StructType::create(C, "FooBar");
+  Metadata *Value = ConstantAsMetadata::get(ConstantAggregateZero::get(Struct));
+  MDNode *Node = MDNode::get(C, Value);
+  F->setMetadata("bar", Node);
+
+  TypeFinder Finder;
+  Finder.run(M, /*onlyNamed*/ false);
+  EXPECT_THAT(Finder, UnorderedElementsAre(Struct));
+}
+
+} // end anonymous namespace
Index: unittests/IR/CMakeLists.txt
===================================================================
--- unittests/IR/CMakeLists.txt
+++ unittests/IR/CMakeLists.txt
@@ -31,6 +31,7 @@
   PassManagerTest.cpp
   PatternMatch.cpp
   TypeBuilderTest.cpp
+  TypeFinderTest.cpp
   TypesTest.cpp
   UseTest.cpp
   UserTest.cpp
Index: lib/IR/TypeFinder.cpp
===================================================================
--- lib/IR/TypeFinder.cpp
+++ lib/IR/TypeFinder.cpp
@@ -47,13 +47,19 @@
   }
 
   // Get types from functions.
-  SmallVector<std::pair<unsigned, MDNode *>, 4> MDForInst;
+  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
   for (const Function &FI : M) {
     incorporateType(FI.getType());
 
     for (const Use &U : FI.operands())
       incorporateValue(U.get());
 
+    // Incorporate types hiding in metadata.
+    FI.getAllMetadata(MDs);
+    for (const auto &MD : MDs)
+      incorporateMDNode(MD.second);
+    MDs.clear();
+
     // First incorporate the arguments.
     for (const auto &A : FI.args())
       incorporateValue(&A);
@@ -70,10 +76,10 @@
             incorporateValue(&*O);
 
         // Incorporate types hiding in metadata.
-        I.getAllMetadataOtherThanDebugLoc(MDForInst);
-        for (const auto &MD : MDForInst)
+        I.getAllMetadataOtherThanDebugLoc(MDs);
+        for (const auto &MD : MDs)
           incorporateMDNode(MD.second);
-        MDForInst.clear();
+        MDs.clear();
       }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53050.168891.patch
Type: text/x-patch
Size: 2895 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181009/5d45512a/attachment.bin>


More information about the llvm-commits mailing list