[llvm] [llvm-diff] Add associative on BinOp (PR #123314)

Serval MARTINOT-LAGARDE via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 20 02:48:34 PST 2025


https://github.com/Serval6 updated https://github.com/llvm/llvm-project/pull/123314

>From e31872eeb98257a62e040dcd085174b9598fd417 Mon Sep 17 00:00:00 2001
From: Serval Martinot-Lagarde <serval.martinot-lagarde at sipearl.com>
Date: Thu, 16 Jan 2025 16:38:14 +0100
Subject: [PATCH] [llvm-diff] Add commutativity on BinOp

---
 llvm/test/tools/llvm-diff/commutative.ll      | 19 +++++++++++++++++++
 llvm/tools/llvm-diff/lib/DifferenceEngine.cpp | 18 ++++++++++++++++++
 llvm/tools/llvm-diff/llvm-diff.cpp            |  3 +++
 3 files changed, 40 insertions(+)
 create mode 100644 llvm/test/tools/llvm-diff/commutative.ll

diff --git a/llvm/test/tools/llvm-diff/commutative.ll b/llvm/test/tools/llvm-diff/commutative.ll
new file mode 100644
index 00000000000000..af86c1e36cc91a
--- /dev/null
+++ b/llvm/test/tools/llvm-diff/commutative.ll
@@ -0,0 +1,19 @@
+; Check that commutative operations are considered equal when parameters are exchanged.
+;
+; Replace '%a, %b' with '%b, %a' in 'or' (commutative) and 'sub' (non-commutative) operations.
+;
+; RUN: rm -f %t.ll
+; RUN: cat %s | sed -e 's/^\(  .*\) i1 %a, %b$/\1 i1 %b, %a/' > %t.ll
+; RUN: not llvm-diff --commutative %s %t.ll 2>&1 | FileCheck %s
+
+; CHECK:      in function choice:
+; CHECK-NEXT:   in block %entry:
+; CHECK-NEXT:     >   %1 = sub i1 %b, %a
+; CHECK-NEXT:     <   %1 = sub i1 %a, %b
+
+define i1 @choice(i1 %a, i1 %b) {
+entry:
+  %0 = or i1 %a, %b
+  %1 = sub i1 %a, %b
+  ret i1 %0
+}
diff --git a/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp b/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
index 9be0eec7b73f3e..0045ff940513fb 100644
--- a/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
+++ b/llvm/tools/llvm-diff/lib/DifferenceEngine.cpp
@@ -23,6 +23,7 @@
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/type_traits.h"
@@ -30,6 +31,8 @@
 
 using namespace llvm;
 
+extern cl::opt<bool> EnableCommutativeInstructions;
+
 namespace {
 
 /// A priority queue, implemented as a heap.
@@ -536,6 +539,21 @@ class FunctionDifferenceEngine {
       return true;
     }
 
+    if (EnableCommutativeInstructions && L->getNumOperands() == 2 &&
+        Instruction::isCommutative(L->getOpcode())) {
+      Value *LO1 = L->getOperand(0), *RO1 = R->getOperand(0),
+            *LO2 = L->getOperand(1), *RO2 = R->getOperand(1);
+      if ((equivalentAsOperands(LO1, RO1, AC) &&
+           equivalentAsOperands(LO2, RO2, AC)) ||
+          (equivalentAsOperands(LO1, RO2, AC) &&
+           equivalentAsOperands(LO2, RO1, AC)))
+        return false;
+      if (Complain)
+        Engine.logf("operands <%l, %l> and <%r, %r> differ")
+            << LO1 << LO2 << RO1 << RO2;
+      return true;
+    }
+
     for (unsigned I = 0, E = L->getNumOperands(); I != E; ++I) {
       Value *LO = L->getOperand(I), *RO = R->getOperand(I);
       if (!equivalentAsOperands(LO, RO, AC)) {
diff --git a/llvm/tools/llvm-diff/llvm-diff.cpp b/llvm/tools/llvm-diff/llvm-diff.cpp
index 3e77b1ed89b048..d352c3682d9a2a 100644
--- a/llvm/tools/llvm-diff/llvm-diff.cpp
+++ b/llvm/tools/llvm-diff/llvm-diff.cpp
@@ -68,6 +68,9 @@ static cl::list<std::string> GlobalsToCompare(cl::Positional,
                                               cl::desc("<globals to compare>"),
                                               cl::cat(DiffCategory));
 
+cl::opt<bool> EnableCommutativeInstructions(
+    "commutative", cl::desc("Commutative instructions"), cl::cat(DiffCategory));
+
 int main(int argc, char **argv) {
   cl::HideUnrelatedOptions({&DiffCategory, &getColorCategory()});
   cl::ParseCommandLineOptions(argc, argv);



More information about the llvm-commits mailing list