[Mlir-commits] [mlir] [MLIR][Presburger] Implement arithmetic operations (/, +, -) and printing for Fractions (PR #65310)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Sep 5 08:49:15 PDT 2023


https://github.com/Abhinav271828 updated https://github.com/llvm/llvm-project/pull/65310:

>From 8d3e0dae92f131d54aadf15046145f026d6907a8 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 5 Sep 2023 10:42:03 +0100
Subject: [PATCH 1/5] Shift changes to Fraction

---
 .../mlir/Analysis/Presburger/Fraction.h       | 28 +++++++++++++++++--
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index c51b6c972bf8851..2cb90b708435353 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -15,6 +15,7 @@
 #define MLIR_ANALYSIS_PRESBURGER_FRACTION_H
 
 #include "mlir/Analysis/Presburger/MPInt.h"
+#include "mlir/Analysis/Presburger/Utils.h"
 #include "mlir/Support/MathExtras.h"
 
 namespace mlir {
@@ -30,15 +31,15 @@ struct Fraction {
   Fraction() = default;
 
   /// Construct a Fraction from a numerator and denominator.
-  Fraction(const MPInt &oNum, const MPInt &oDen) : num(oNum), den(oDen) {
+  Fraction(const MPInt &oNum, const MPInt &oDen = MPInt(1)) : num(oNum), den(oDen) {
     if (den < 0) {
       num = -num;
       den = -den;
     }
   }
   /// Overloads for passing literals.
-  Fraction(const MPInt &num, int64_t den) : Fraction(num, MPInt(den)) {}
-  Fraction(int64_t num, const MPInt &den) : Fraction(MPInt(num), den) {}
+  Fraction(const MPInt &num, int64_t den = 1) : Fraction(num, MPInt(den)) {}
+  Fraction(int64_t num, const MPInt &den = MPInt(1)) : Fraction(MPInt(num), den) {}
   Fraction(int64_t num, int64_t den) : Fraction(MPInt(num), MPInt(den)) {}
 
   // Return the value of the fraction as an integer. This should only be called
@@ -48,6 +49,10 @@ struct Fraction {
     return num / den;
   }
 
+  llvm::raw_ostream &print(llvm::raw_ostream &os) const {
+    return os << "(" << num << "/" << den << ")";
+  }
+
   /// The numerator and denominator, respectively. The denominator is always
   /// positive.
   MPInt num{0}, den{1};
@@ -99,6 +104,23 @@ inline Fraction operator*(const Fraction &x, const Fraction &y) {
   return Fraction(x.num * y.num, x.den * y.den);
 }
 
+inline Fraction operator/(const Fraction &x, const Fraction &y) {
+  return Fraction(x.num * y.den, x.den * y.num);
+}
+
+inline Fraction operator+(const Fraction &x, const Fraction &y) {
+  return Fraction(x.num * y.den + x.den * y.num, x.den * y.den);
+}
+
+inline Fraction operator-(const Fraction &x, const Fraction &y) {
+  return Fraction(x.num * y.den - x.den * y.num, x.den * y.den);
+}
+
+inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Fraction &x) {
+  x.print(os);
+  return os;
+}
+
 } // namespace presburger
 } // namespace mlir
 

>From 8a050875adb48d4bc9b220052f36f959e2bad73c Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 5 Sep 2023 11:15:05 +0100
Subject: [PATCH 2/5] Update documentation and remove extraneous include

---
 mlir/include/mlir/Analysis/Presburger/Fraction.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index 2cb90b708435353..e88f5d768f0c40f 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This is a simple class to represent fractions. It supports multiplication,
+// This is a simple class to represent fractions. It supports arithmetic,
 // comparison, floor, and ceiling operations.
 //
 //===----------------------------------------------------------------------===//
@@ -15,7 +15,6 @@
 #define MLIR_ANALYSIS_PRESBURGER_FRACTION_H
 
 #include "mlir/Analysis/Presburger/MPInt.h"
-#include "mlir/Analysis/Presburger/Utils.h"
 #include "mlir/Support/MathExtras.h"
 
 namespace mlir {

>From 504ffcde602d3e5dcd2b799e083abc4b6a4a61ca Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 5 Sep 2023 14:46:10 +0100
Subject: [PATCH 3/5] Add reduction after arithmetic operations

---
 mlir/include/mlir/Analysis/Presburger/Fraction.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index e88f5d768f0c40f..93e791872c36504 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -99,20 +99,27 @@ inline bool operator>=(const Fraction &x, const Fraction &y) {
   return compare(x, y) >= 0;
 }
 
+inline Fraction reduce(const Fraction &f) {
+  if (f == Fraction(0))
+    return f;
+  MPInt g = gcd(f.num, f.den);
+  return Fraction(f.num / g, f.den / g);
+}
+
 inline Fraction operator*(const Fraction &x, const Fraction &y) {
-  return Fraction(x.num * y.num, x.den * y.den);
+  return reduce(Fraction(x.num * y.num, x.den * y.den));
 }
 
 inline Fraction operator/(const Fraction &x, const Fraction &y) {
-  return Fraction(x.num * y.den, x.den * y.num);
+  return reduce(Fraction(x.num * y.den, x.den * y.num));
 }
 
 inline Fraction operator+(const Fraction &x, const Fraction &y) {
-  return Fraction(x.num * y.den + x.den * y.num, x.den * y.den);
+  return reduce(Fraction(x.num * y.den + x.den * y.num, x.den * y.den));
 }
 
 inline Fraction operator-(const Fraction &x, const Fraction &y) {
-  return Fraction(x.num * y.den - x.den * y.num, x.den * y.den);
+  return reduce(Fraction(x.num * y.den - x.den * y.num, x.den * y.den));
 }
 
 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Fraction &x) {

>From 38a86ebc208b66c0a7f2ee2574218f98832c2058 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 5 Sep 2023 15:05:14 +0100
Subject: [PATCH 4/5] Add increment and decrement

---
 mlir/include/mlir/Analysis/Presburger/Fraction.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index 93e791872c36504..3c5de0bd306f6bb 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -122,6 +122,18 @@ inline Fraction operator-(const Fraction &x, const Fraction &y) {
   return reduce(Fraction(x.num * y.den - x.den * y.num, x.den * y.den));
 }
 
+inline Fraction& operator+=(const Fraction &g, const Fraction &f) {
+  Fraction *r = NULL;
+  *r = g+f;
+  return *r;
+}
+
+inline Fraction& operator-=(const Fraction &g, const Fraction &f) {
+  Fraction *r = NULL;
+  *r = g-f;
+  return *r;
+}
+
 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Fraction &x) {
   x.print(os);
   return os;

>From 3bf6dd46890ee69d77b0e4b4bc30587ffd762f76 Mon Sep 17 00:00:00 2001
From: Abhinav271828 <abhinav.m at research.iiit.ac.in>
Date: Tue, 5 Sep 2023 16:49:02 +0100
Subject: [PATCH 5/5] Fix const and overload other arithmetic operators

---
 .../mlir/Analysis/Presburger/Fraction.h       | 24 ++++++++++++-------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/mlir/include/mlir/Analysis/Presburger/Fraction.h b/mlir/include/mlir/Analysis/Presburger/Fraction.h
index 3c5de0bd306f6bb..79b8d444c21d560 100644
--- a/mlir/include/mlir/Analysis/Presburger/Fraction.h
+++ b/mlir/include/mlir/Analysis/Presburger/Fraction.h
@@ -122,16 +122,24 @@ inline Fraction operator-(const Fraction &x, const Fraction &y) {
   return reduce(Fraction(x.num * y.den - x.den * y.num, x.den * y.den));
 }
 
-inline Fraction& operator+=(const Fraction &g, const Fraction &f) {
-  Fraction *r = NULL;
-  *r = g+f;
-  return *r;
+inline Fraction& operator+=(Fraction &x, const Fraction &y) {
+  x = x + y;
+  return x;
 }
 
-inline Fraction& operator-=(const Fraction &g, const Fraction &f) {
-  Fraction *r = NULL;
-  *r = g-f;
-  return *r;
+inline Fraction& operator-=(Fraction &x, const Fraction &y) {
+  x = x - y;
+  return x;
+}
+
+inline Fraction& operator/=(Fraction &x, const Fraction &y) {
+  x = x / y;
+  return x;
+}
+
+inline Fraction& operator*=(Fraction &x, const Fraction &y) {
+  x = x * y;
+  return x;
 }
 
 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Fraction &x) {



More information about the Mlir-commits mailing list