[polly] r329880 - Add isl operator overloads for isl::pw_aff (Try II)

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 11 23:15:17 PDT 2018


Author: grosser
Date: Wed Apr 11 23:15:17 2018
New Revision: 329880

URL: http://llvm.org/viewvc/llvm-project?rev=329880&view=rev
Log:
Add isl operator overloads for isl::pw_aff (Try II)

Piecewise affine expressions have directly corresponding mathematical
operators. Introduce these operators as overloads as this makes writing
code with isl::pw_aff expressions more directly readable.

We can now write:

  A = B + C    instead of    A = B.add(C)

Reviewers: Meinersbur, bollu, sebpop

Reviewed By: Meinersbur

Subscribers: philip.pfaffe, pollydev, llvm-commits

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

Added:
    polly/trunk/include/polly/Support/ISLOperators.h
Modified:
    polly/trunk/unittests/Isl/IslTest.cpp

Added: polly/trunk/include/polly/Support/ISLOperators.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/Support/ISLOperators.h?rev=329880&view=auto
==============================================================================
--- polly/trunk/include/polly/Support/ISLOperators.h (added)
+++ polly/trunk/include/polly/Support/ISLOperators.h Wed Apr 11 23:15:17 2018
@@ -0,0 +1,185 @@
+//===------ ISLOperators.h --------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Operator overloads for isl C++ objects.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef POLLY_ISLOPERATORS_H
+#define POLLY_ISLOPERATORS_H
+
+#include "isl/isl-noexceptions.h"
+
+namespace polly {
+
+/// Addition
+/// @{
+inline isl::pw_aff operator+(isl::pw_aff Left, isl::pw_aff Right) {
+  return Left.add(Right);
+}
+
+inline isl::pw_aff operator+(isl::val ValLeft, isl::pw_aff Right) {
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.add(Right);
+}
+
+inline isl::pw_aff operator+(isl::pw_aff Left, isl::val ValRight) {
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.add(Right);
+}
+
+inline isl::pw_aff operator+(long IntLeft, isl::pw_aff Right) {
+  isl::ctx Ctx = Right.get_ctx();
+  isl::val ValLeft(Ctx, IntLeft);
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.add(Right);
+}
+
+inline isl::pw_aff operator+(isl::pw_aff Left, long IntRight) {
+  isl::ctx Ctx = Left.get_ctx();
+  isl::val ValRight(Ctx, IntRight);
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.add(Right);
+}
+/// @}
+
+/// Multiplication
+/// @{
+inline isl::pw_aff operator*(isl::pw_aff Left, isl::pw_aff Right) {
+  return Left.mul(Right);
+}
+
+inline isl::pw_aff operator*(isl::val ValLeft, isl::pw_aff Right) {
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.mul(Right);
+}
+
+inline isl::pw_aff operator*(isl::pw_aff Left, isl::val ValRight) {
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.mul(Right);
+}
+
+inline isl::pw_aff operator*(long IntLeft, isl::pw_aff Right) {
+  isl::ctx Ctx = Right.get_ctx();
+  isl::val ValLeft(Ctx, IntLeft);
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.mul(Right);
+}
+
+inline isl::pw_aff operator*(isl::pw_aff Left, long IntRight) {
+  isl::ctx Ctx = Left.get_ctx();
+  isl::val ValRight(Ctx, IntRight);
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.mul(Right);
+}
+/// @}
+
+/// Subtraction
+/// @{
+inline isl::pw_aff operator-(isl::pw_aff Left, isl::pw_aff Right) {
+  return Left.sub(Right);
+}
+
+inline isl::pw_aff operator-(isl::val ValLeft, isl::pw_aff Right) {
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.sub(Right);
+}
+
+inline isl::pw_aff operator-(isl::pw_aff Left, isl::val ValRight) {
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.sub(Right);
+}
+
+inline isl::pw_aff operator-(long IntLeft, isl::pw_aff Right) {
+  isl::ctx Ctx = Right.get_ctx();
+  isl::val ValLeft(Ctx, IntLeft);
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.sub(Right);
+}
+
+inline isl::pw_aff operator-(isl::pw_aff Left, long IntRight) {
+  isl::ctx Ctx = Left.get_ctx();
+  isl::val ValRight(Ctx, IntRight);
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.sub(Right);
+}
+/// @}
+
+/// Division
+///
+/// This division rounds towards zero. This follows the semantics of C/C++.
+///
+/// @{
+inline isl::pw_aff operator/(isl::pw_aff Left, isl::pw_aff Right) {
+  return Left.tdiv_q(Right);
+}
+
+inline isl::pw_aff operator/(isl::val ValLeft, isl::pw_aff Right) {
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.tdiv_q(Right);
+}
+
+inline isl::pw_aff operator/(isl::pw_aff Left, isl::val ValRight) {
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.tdiv_q(Right);
+}
+
+inline isl::pw_aff operator/(long IntLeft, isl::pw_aff Right) {
+  isl::ctx Ctx = Right.get_ctx();
+  isl::val ValLeft(Ctx, IntLeft);
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.tdiv_q(Right);
+}
+
+inline isl::pw_aff operator/(isl::pw_aff Left, long IntRight) {
+  isl::ctx Ctx = Left.get_ctx();
+  isl::val ValRight(Ctx, IntRight);
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.tdiv_q(Right);
+}
+/// @}
+
+/// Remainder
+///
+/// This is the remainder of a division which rounds towards zero. This follows
+/// the semantics of C/C++.
+///
+/// @{
+inline isl::pw_aff operator%(isl::pw_aff Left, isl::pw_aff Right) {
+  return Left.tdiv_r(Right);
+}
+
+inline isl::pw_aff operator%(isl::val ValLeft, isl::pw_aff Right) {
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.tdiv_r(Right);
+}
+
+inline isl::pw_aff operator%(isl::pw_aff Left, isl::val ValRight) {
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.tdiv_r(Right);
+}
+
+inline isl::pw_aff operator%(long IntLeft, isl::pw_aff Right) {
+  isl::ctx Ctx = Right.get_ctx();
+  isl::val ValLeft(Ctx, IntLeft);
+  isl::pw_aff Left(Right.domain(), ValLeft);
+  return Left.tdiv_r(Right);
+}
+
+inline isl::pw_aff operator%(isl::pw_aff Left, long IntRight) {
+  isl::ctx Ctx = Left.get_ctx();
+  isl::val ValRight(Ctx, IntRight);
+  isl::pw_aff Right(Left.domain(), ValRight);
+  return Left.tdiv_r(Right);
+}
+/// @}
+
+} // namespace polly
+
+#endif // POLLY_ISLOPERATORS_H

Modified: polly/trunk/unittests/Isl/IslTest.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/unittests/Isl/IslTest.cpp?rev=329880&r1=329879&r2=329880&view=diff
==============================================================================
--- polly/trunk/unittests/Isl/IslTest.cpp (original)
+++ polly/trunk/unittests/Isl/IslTest.cpp Wed Apr 11 23:15:17 2018
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "polly/Support/GICHelper.h"
+#include "polly/Support/ISLOperators.h"
 #include "polly/Support/ISLTools.h"
 #include "gtest/gtest.h"
 #include "isl/stream.h"
@@ -75,6 +76,9 @@ static bool operator==(const isl::val &L
   return bool(LHS.eq(RHS));
 }
 
+static bool operator==(const isl::pw_aff &LHS, const isl::pw_aff &RHS) {
+  return bool(LHS.is_equal(RHS));
+}
 } // namespace noexceptions
 } // namespace isl
 
@@ -282,6 +286,105 @@ TEST(Isl, IslValToAPInt) {
   isl_ctx_free(IslCtx);
 }
 
+TEST(Isl, Operators) {
+  std::unique_ptr<isl_ctx, decltype(&isl_ctx_free)> IslCtx(isl_ctx_alloc(),
+                                                           &isl_ctx_free);
+
+  isl::val ValOne = isl::val(IslCtx.get(), 1);
+  isl::val ValTwo = isl::val(IslCtx.get(), 2);
+  isl::val ValThree = isl::val(IslCtx.get(), 3);
+  isl::val ValFour = isl::val(IslCtx.get(), 4);
+  isl::val ValNegOne = isl::val(IslCtx.get(), -1);
+  isl::val ValNegTwo = isl::val(IslCtx.get(), -2);
+  isl::val ValNegThree = isl::val(IslCtx.get(), -3);
+  isl::val ValNegFour = isl::val(IslCtx.get(), -4);
+
+  isl::space Space = isl::space(IslCtx.get(), 0, 0);
+  isl::local_space LS = isl::local_space(Space);
+
+  isl::pw_aff AffOne = isl::aff(LS, ValOne);
+  isl::pw_aff AffTwo = isl::aff(LS, ValTwo);
+  isl::pw_aff AffThree = isl::aff(LS, ValThree);
+  isl::pw_aff AffFour = isl::aff(LS, ValFour);
+  isl::pw_aff AffNegOne = isl::aff(LS, ValNegOne);
+  isl::pw_aff AffNegTwo = isl::aff(LS, ValNegTwo);
+  isl::pw_aff AffNegThree = isl::aff(LS, ValNegThree);
+  isl::pw_aff AffNegFour = isl::aff(LS, ValNegFour);
+
+  // Addition
+  {
+    EXPECT_EQ(AffOne + AffOne, AffTwo);
+    EXPECT_EQ(AffOne + 1, AffTwo);
+    EXPECT_EQ(1 + AffOne, AffTwo);
+    EXPECT_EQ(AffOne + ValOne, AffTwo);
+    EXPECT_EQ(ValOne + AffOne, AffTwo);
+  }
+
+  // Multiplication
+  {
+    EXPECT_EQ(AffTwo * AffTwo, AffFour);
+    EXPECT_EQ(AffTwo * 2, AffFour);
+    EXPECT_EQ(2 * AffTwo, AffFour);
+    EXPECT_EQ(AffTwo * ValTwo, AffFour);
+    EXPECT_EQ(ValTwo * AffTwo, AffFour);
+  }
+
+  // Subtraction
+  {
+    EXPECT_EQ(AffTwo - AffOne, AffOne);
+    EXPECT_EQ(AffTwo - 1, AffOne);
+    EXPECT_EQ(2 - AffOne, AffOne);
+    EXPECT_EQ(AffTwo - ValOne, AffOne);
+    EXPECT_EQ(ValTwo - AffOne, AffOne);
+  }
+
+  // Division
+  {
+    EXPECT_EQ(AffFour / AffTwo, AffTwo);
+    EXPECT_EQ(AffFour / 2, AffTwo);
+    EXPECT_EQ(4 / AffTwo, AffTwo);
+    EXPECT_EQ(AffFour / ValTwo, AffTwo);
+    EXPECT_EQ(AffFour / 2, AffTwo);
+
+    // Dividend is negative (should be rounded towards zero)
+    EXPECT_EQ(AffNegFour / AffThree, AffNegOne);
+    EXPECT_EQ(AffNegFour / 3, AffNegOne);
+    EXPECT_EQ((-4) / AffThree, AffNegOne);
+    EXPECT_EQ(AffNegFour / ValThree, AffNegOne);
+    EXPECT_EQ(AffNegFour / 3, AffNegOne);
+
+    // Divisor is negative (should be rounded towards zero)
+    EXPECT_EQ(AffFour / AffNegThree, AffNegOne);
+    EXPECT_EQ(AffFour / -3, AffNegOne);
+    EXPECT_EQ(4 / AffNegThree, AffNegOne);
+    EXPECT_EQ(AffFour / ValNegThree, AffNegOne);
+    EXPECT_EQ(AffFour / -3, AffNegOne);
+  }
+
+  // Remainder
+  {
+    EXPECT_EQ(AffThree % AffTwo, AffOne);
+    EXPECT_EQ(AffThree % 2, AffOne);
+    EXPECT_EQ(3 % AffTwo, AffOne);
+    EXPECT_EQ(AffThree % ValTwo, AffOne);
+    EXPECT_EQ(ValThree % AffTwo, AffOne);
+
+    // Dividend is negative (should be rounded towards zero)
+    EXPECT_EQ(AffNegFour % AffThree, AffNegOne);
+    EXPECT_EQ(AffNegFour % 3, AffNegOne);
+    EXPECT_EQ((-4) % AffThree, AffNegOne);
+    EXPECT_EQ(AffNegFour % ValThree, AffNegOne);
+    EXPECT_EQ(AffNegFour % 3, AffNegOne);
+
+    // Divisor is negative (should be rounded towards zero)
+    EXPECT_EQ(AffFour % AffNegThree, AffOne);
+    EXPECT_EQ(AffFour % -3, AffOne);
+    EXPECT_EQ(4 % AffNegThree, AffOne);
+    EXPECT_EQ(AffFour % ValNegThree, AffOne);
+    EXPECT_EQ(AffFour % -3, AffOne);
+  }
+}
+
 TEST(Isl, Foreach) {
   std::unique_ptr<isl_ctx, decltype(&isl_ctx_free)> Ctx(isl_ctx_alloc(),
                                                         &isl_ctx_free);




More information about the llvm-commits mailing list