[Mlir-commits] [mlir] 1486a2e - [MLIR][Presburger] SlowMPInt::gcd: fix crash when sizes differ

Arjun P llvmlistbot at llvm.org
Thu Aug 4 11:15:39 PDT 2022


Author: Arjun P
Date: 2022-08-04T19:15:50+01:00
New Revision: 1486a2eaf0bd676f89b6e42d2a2620f618f33674

URL: https://github.com/llvm/llvm-project/commit/1486a2eaf0bd676f89b6e42d2a2620f618f33674
DIFF: https://github.com/llvm/llvm-project/commit/1486a2eaf0bd676f89b6e42d2a2620f618f33674.diff

LOG: [MLIR][Presburger] SlowMPInt::gcd: fix crash when sizes differ

Reviewed By: Groverkss

Differential Revision: https://reviews.llvm.org/D131186

Added: 
    

Modified: 
    mlir/lib/Analysis/Presburger/SlowMPInt.cpp
    mlir/unittests/Analysis/Presburger/MPIntTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Analysis/Presburger/SlowMPInt.cpp b/mlir/lib/Analysis/Presburger/SlowMPInt.cpp
index 622aa465f7ae0..bd1f0e2d2e537 100644
--- a/mlir/lib/Analysis/Presburger/SlowMPInt.cpp
+++ b/mlir/lib/Analysis/Presburger/SlowMPInt.cpp
@@ -221,7 +221,9 @@ SlowMPInt detail::mod(const SlowMPInt &lhs, const SlowMPInt &rhs) {
 
 SlowMPInt detail::gcd(const SlowMPInt &a, const SlowMPInt &b) {
   assert(a >= 0 && b >= 0 && "operands must be non-negative!");
-  return SlowMPInt(llvm::APIntOps::GreatestCommonDivisor(a.val, b.val));
+  unsigned width = getMaxWidth(a.val, b.val);
+  return SlowMPInt(llvm::APIntOps::GreatestCommonDivisor(a.val.sext(width),
+                                                         b.val.sext(width)));
 }
 
 /// Returns the least common multiple of 'a' and 'b'.

diff  --git a/mlir/unittests/Analysis/Presburger/MPIntTest.cpp b/mlir/unittests/Analysis/Presburger/MPIntTest.cpp
index aa954a383cf76..3c145d39352c3 100644
--- a/mlir/unittests/Analysis/Presburger/MPIntTest.cpp
+++ b/mlir/unittests/Analysis/Presburger/MPIntTest.cpp
@@ -190,6 +190,8 @@ TYPED_TEST(IntTest, floorCeilModAbsLcmGcd) {
     EXPECT_EQ(abs(y), y);
     EXPECT_EQ(abs(-y), y);
 
+    EXPECT_EQ(gcd(3 * y, three), three);
+    EXPECT_EQ(lcm(y, three), 3 * y);
     EXPECT_EQ(gcd(2 * y, 3 * y), y);
     EXPECT_EQ(lcm(2 * y, 3 * y), 6 * y);
     EXPECT_EQ(gcd(15 * y, 6 * y), 3 * y);


        


More information about the Mlir-commits mailing list