[PATCH] D104733: [llvm-diff] Explicitly check ConstantArrays

Bill Wendling via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 22 11:49:31 PDT 2021


void created this revision.
void added reviewers: rjmccall, dexonsmith, arsenm.
void requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

Global initializers may be ConstantArrays. They need to be checked
explicitly, because different-yet-still-equivalent type names may be
used for each, and/or a GEP instruction may appear in one.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104733

Files:
  llvm/test/tools/llvm-diff/initializers.ll
  llvm/tools/llvm-diff/DifferenceEngine.cpp


Index: llvm/tools/llvm-diff/DifferenceEngine.cpp
===================================================================
--- llvm/tools/llvm-diff/DifferenceEngine.cpp
+++ llvm/tools/llvm-diff/DifferenceEngine.cpp
@@ -404,6 +404,7 @@
     return false;
   }
 
+public:
   bool equivalentAsOperands(const Constant *L, const Constant *R) {
     // Use equality as a preliminary filter.
     if (L == R)
@@ -446,6 +447,24 @@
       return true;
     }
 
+    // If L and R are ConstantArrays, compare the element count and types.
+    if (isa<ConstantArray>(L)) {
+      const ConstantArray *CAL = cast<ConstantArray>(L);
+      const ConstantArray *CAR = cast<ConstantArray>(R);
+      // Sometimes a type may be equivalent, but not uniquified---e.g. it may
+      // contain a GEP instruction. Do a deeper comparison of the types.
+      if (CAL->getType()->getNumElements() != CAR->getType()->getNumElements())
+        return false;
+
+      for (unsigned I = 0; I < CAL->getType()->getNumElements(); ++I) {
+        if (!equivalentAsOperands(CAL->getAggregateElement(I),
+                                  CAR->getAggregateElement(I)))
+          return false;
+      }
+
+      return true;
+    }
+
     return false;
   }
 
@@ -766,7 +785,8 @@
     const GlobalVariable *GVR = cast<GlobalVariable>(R);
     if (GVL->hasLocalLinkage() && GVL->hasUniqueInitializer() &&
         GVR->hasLocalLinkage() && GVR->hasUniqueInitializer())
-      return GVL->getInitializer() == GVR->getInitializer();
+      return FunctionDifferenceEngine(*this).equivalentAsOperands(
+          GVL->getInitializer(), GVR->getInitializer());
   }
 
   return L->getName() == R->getName();
Index: llvm/test/tools/llvm-diff/initializers.ll
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-diff/initializers.ll
@@ -0,0 +1,12 @@
+; RUN: llvm-diff %s %s
+
+; An initializer that has a GEP instruction in it won't match itself in
+; llvm-diff unless the a deep comparison is done on the initializer.
+
+ at gv1 = external dso_local global [28 x i16], align 16
+ at gv2 = private unnamed_addr constant [2 x i16*] [i16* getelementptr inbounds ([28 x i16], [28 x i16]* @gv1, i32 0, i32 0), i16* poison], align 16
+
+define void @foo() {
+  %1 = getelementptr [2 x i16*], [2 x i16*]* @gv2, i64 0, i64 undef
+  ret void
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104733.353736.patch
Type: text/x-patch
Size: 2342 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210622/5f90e594/attachment.bin>


More information about the llvm-commits mailing list