This patch moves the Presburger library to a new `presburger` namespace.
This allows to shorten some names, helps to avoid polluting the mlir namespace,
and also provides some structure.
Reviewed By: arjunp
Differential Revision: https://reviews.llvm.org/D120505
#include "mlir/Support/MathExtras.h"
namespace mlir {
+namespace presburger {
/// A class to represent fractions. The sign of the fraction is represented
/// in the sign of the numerator; the denominator is always positive.
return Fraction(x.num * y.num, x.den * y.den);
}
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_FRACTION_H
#include "mlir/Support/LogicalResult.h"
namespace mlir {
+namespace presburger {
/// An IntegerRelation is a PresburgerLocalSpace subject to affine constraints.
/// Affine constraints can be inequalities or equalities in the form:
/// constraints. Returns an empty optional if the polyhedron is empty or if
/// the lexmin is unbounded. Symbols are not supported and will result in
/// assert-failure.
- presburger_utils::MaybeOptimum<SmallVector<Fraction, 8>>
- findRationalLexMin() const;
+ MaybeOptimum<SmallVector<Fraction, 8>> findRationalLexMin() const;
/// Same as above, but returns lexicographically minimal integer point.
/// Note: this should be used only when the lexmin is really required.
/// For a generic integer sampling operation, findIntegerSample is more
/// robust and should be preferred.
- presburger_utils::MaybeOptimum<SmallVector<int64_t, 8>>
- findIntegerLexMin() const;
+ MaybeOptimum<SmallVector<int64_t, 8>> findIntegerLexMin() const;
/// Swap the posA^th identifier with the posB^th identifier.
virtual void swapId(unsigned posA, unsigned posB);
/// to 0.
void getLocalReprs(std::vector<SmallVector<int64_t, 8>> ÷nds,
SmallVector<unsigned, 4> &denominators,
- std::vector<presburger_utils::MaybeLocalRepr> &repr) const;
- void getLocalReprs(std::vector<presburger_utils::MaybeLocalRepr> &repr) const;
+ std::vector<MaybeLocalRepr> &repr) const;
+ void getLocalReprs(std::vector<MaybeLocalRepr> &repr) const;
void getLocalReprs(std::vector<SmallVector<int64_t, 8>> ÷nds,
SmallVector<unsigned, 4> &denominators) const;
constexpr static unsigned kExplosionFactor = 32;
};
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_INTEGERPOLYHEDRON_H
#include "llvm/ADT/SmallVector.h"
namespace mlir {
+namespace presburger {
class LinearTransform {
public:
Matrix matrix;
};
+} // namespace presburger
} // namespace mlir
+
#endif // MLIR_ANALYSIS_PRESBURGER_LINEARTRANSFORM_H
#include <cassert>
namespace mlir {
+namespace presburger {
/// This is a class to represent a resizable matrix.
///
SmallVector<int64_t, 64> data;
};
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_MATRIX_H
#include "mlir/Analysis/Presburger/PresburgerSet.h"
namespace mlir {
+namespace presburger {
/// This class represents a multi-affine function whose domain is given by an
/// IntegerPolyhedron. This can be thought of as an IntegerPolyhedron with a
unsigned numOutputs;
};
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_PWMAFUNCTION_H
#include "mlir/Analysis/Presburger/IntegerPolyhedron.h"
namespace mlir {
+namespace presburger {
/// This class can represent a union of IntegerPolyhedrons, with support for
/// union, intersection, subtraction and complement operations, as well as
SmallVector<IntegerPolyhedron, 2> integerPolyhedrons;
};
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_PRESBURGERSET_H
#include "llvm/Support/raw_ostream.h"
namespace mlir {
+namespace presburger {
class PresburgerLocalSpace;
: PresburgerSpace(Set, /*numDomain=*/0, numDims, numSymbols, numLocals) {}
};
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_PRESBURGERSPACE_H
#include "llvm/Support/raw_ostream.h"
namespace mlir {
+namespace presburger {
class GBRSimplex;
unsigned getSnapshot() { return SimplexBase::getSnapshotBasis(); }
/// Return the lexicographically minimum rational solution to the constraints.
- presburger_utils::MaybeOptimum<SmallVector<Fraction, 8>> findRationalLexMin();
+ MaybeOptimum<SmallVector<Fraction, 8>> findRationalLexMin();
/// Return the lexicographically minimum integer solution to the constraints.
///
/// Note: this should be used only when the lexmin is really needed. To obtain
/// any integer sample, use Simplex::findIntegerSample as that is more robust.
- presburger_utils::MaybeOptimum<SmallVector<int64_t, 8>> findIntegerLexMin();
+ MaybeOptimum<SmallVector<int64_t, 8>> findIntegerLexMin();
protected:
/// Returns the current sample point, which may contain non-integer (rational)
/// Returns an unbounded optimum when the big M parameter is used and a
/// variable has a non-zero big M coefficient, meaning its value is infinite
/// or unbounded.
- presburger_utils::MaybeOptimum<SmallVector<Fraction, 8>>
- getRationalSample() const;
+ MaybeOptimum<SmallVector<Fraction, 8>> getRationalSample() const;
/// Given a row that has a non-integer sample value, add an inequality such
/// that this fractional sample value is cut away from the polytope. The added
///
/// Returns a Fraction denoting the optimum, or a null value if no optimum
/// exists, i.e., if the expression is unbounded in this direction.
- presburger_utils::MaybeOptimum<Fraction>
- computeRowOptimum(Direction direction, unsigned row);
+ MaybeOptimum<Fraction> computeRowOptimum(Direction direction, unsigned row);
/// Compute the maximum or minimum value of the given expression, depending on
/// direction. Should not be called when the Simplex is empty.
///
/// Returns a Fraction denoting the optimum, or a null value if no optimum
/// exists, i.e., if the expression is unbounded in this direction.
- presburger_utils::MaybeOptimum<Fraction>
- computeOptimum(Direction direction, ArrayRef<int64_t> coeffs);
+ MaybeOptimum<Fraction> computeOptimum(Direction direction,
+ ArrayRef<int64_t> coeffs);
/// Returns whether the perpendicular of the specified constraint is a
/// is a direction along which the polytope is bounded.
/// Returns a (min, max) pair denoting the minimum and maximum integer values
/// of the given expression. If no integer value exists, both results will be
/// of kind Empty.
- std::pair<presburger_utils::MaybeOptimum<int64_t>,
- presburger_utils::MaybeOptimum<int64_t>>
+ std::pair<MaybeOptimum<int64_t>, MaybeOptimum<int64_t>>
computeIntegerBounds(ArrayRef<int64_t> coeffs);
/// Returns true if the polytope is unbounded, i.e., extends to infinity in
///
/// Returns a Fraction denoting the optimum, or a null value if no optimum
/// exists, i.e., if the expression is unbounded in this direction.
- presburger_utils::MaybeOptimum<Fraction> computeOptimum(Direction direction,
- Unknown &u);
+ MaybeOptimum<Fraction> computeOptimum(Direction direction, Unknown &u);
/// Mark the specified unknown redundant. This operation is added to the undo
/// log and will be undone by rollbacks. The specified unknown must be in row
void reduceBasis(Matrix &basis, unsigned level);
};
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_SIMPLEX_H
#include "llvm/ADT/STLExtras.h"
namespace mlir {
+namespace presburger {
class IntegerPolyhedron;
-namespace presburger_utils {
-
/// This class represents the result of operations optimizing something subject
/// to some constraints. If the constraints were not satisfiable the, kind will
/// be Empty. If the optimum is unbounded, the kind is Unbounded, and if the
SmallVectorImpl<unsigned> &denoms, unsigned localOffset,
llvm::function_ref<bool(unsigned i, unsigned j)> merge);
-} // namespace presburger_utils
+} // namespace presburger
} // namespace mlir
#endif // MLIR_ANALYSIS_PRESBURGER_UTILS_H
/// that some floordiv combinations are converted to mod's by AffineExpr
/// construction.
///
-class FlatAffineConstraints : public IntegerPolyhedron {
+class FlatAffineConstraints : public presburger::IntegerPolyhedron {
public:
/// Constructs a constraint system reserving memory for the specified number
/// of constraints and identifiers.
#define DEBUG_TYPE "presburger"
using namespace mlir;
-using namespace presburger_utils;
+using namespace presburger;
+
using llvm::SmallDenseMap;
using llvm::SmallDenseSet;
#include "mlir/Analysis/Presburger/LinearTransform.h"
#include "mlir/Analysis/Presburger/IntegerPolyhedron.h"
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
LinearTransform::LinearTransform(Matrix &&oMatrix) : matrix(oMatrix) {}
LinearTransform::LinearTransform(const Matrix &oMatrix) : matrix(oMatrix) {}
return result;
}
-
-} // namespace mlir
#include "mlir/Analysis/Presburger/Matrix.h"
#include "llvm/Support/MathExtras.h"
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
Matrix::Matrix(unsigned rows, unsigned columns, unsigned reservedRows,
unsigned reservedColumns)
return false;
return true;
}
-
-} // namespace mlir
#include "mlir/Analysis/Presburger/Simplex.h"
using namespace mlir;
+using namespace presburger;
// Return the result of subtracting the two given vectors pointwise.
// The vectors must be of the same size.
#include "llvm/ADT/SmallBitVector.h"
using namespace mlir;
-using namespace presburger_utils;
+using namespace presburger;
PresburgerSet::PresburgerSet(const IntegerPolyhedron &poly)
: PresburgerSpace(poly) {
#include <cassert>
using namespace mlir;
+using namespace presburger;
PresburgerSpace PresburgerSpace::getRelationSpace(unsigned numDomain,
unsigned numRange,
#include "mlir/Support/MathExtras.h"
#include "llvm/ADT/Optional.h"
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
-using namespace presburger_utils;
using Direction = Simplex::Direction;
const int nullIndex = std::numeric_limits<int>::max();
/// also supports rolling back this addition, by maintaining a snapshot stack
/// that contains a snapshot of the Simplex's state for each equality, just
/// before that equality was added.
-class GBRSimplex {
+class presburger::GBRSimplex {
using Orientation = Simplex::Orientation;
public:
return minimum.isBounded() && maximum.isBounded() &&
*maximum == Fraction(0, 1) && *minimum == Fraction(0, 1);
}
-
-} // namespace mlir
#include "mlir/Support/MathExtras.h"
using namespace mlir;
-using namespace presburger_utils;
+using namespace presburger;
/// Normalize a division's `dividend` and the `divisor` by their GCD. For
/// example: if the dividend and divisor are [2,0,4] and 4 respectively,
/// the representation could be computed, `dividend` and `denominator` are set.
/// If the representation could not be computed, the kind attribute in
/// `MaybeLocalRepr` is set to None.
-MaybeLocalRepr presburger_utils::computeSingleVarRepr(
+MaybeLocalRepr presburger::computeSingleVarRepr(
const IntegerPolyhedron &cst, ArrayRef<bool> foundRepr, unsigned pos,
SmallVector<int64_t, 8> ÷nd, unsigned &divisor) {
assert(pos < cst.getNumIds() && "invalid position");
return repr;
}
-void presburger_utils::removeDuplicateDivs(
+void presburger::removeDuplicateDivs(
std::vector<SmallVector<int64_t, 8>> &divs,
SmallVectorImpl<unsigned> &denoms, unsigned localOffset,
llvm::function_ref<bool(unsigned i, unsigned j)> merge) {
#define DEBUG_TYPE "affine-structures"
using namespace mlir;
-using namespace presburger_utils;
+using namespace presburger;
namespace {
#define DEBUG_TYPE "analysis-utils"
using namespace mlir;
+using namespace presburger;
using llvm::SmallDenseMap;
#include <numeric>
-namespace mlir {
-using namespace presburger_utils;
+using namespace mlir;
+using namespace presburger;
+
using testing::ElementsAre;
enum class TestFunction { Sample, Empty };
parsePoly("(x, y) : (2*x - y >= 0, y - 3*x >= 0)"),
/*trueVolume=*/{}, /*resultBound=*/{});
}
-
-} // namespace mlir
#include <gmock/gmock.h>
#include <gtest/gtest.h>
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
void testColumnEchelonForm(const Matrix &m, unsigned expectedRank) {
unsigned lastAllowedNonZeroCol = 0;
m6(1, 1) = -7;
testColumnEchelonForm(m5, 2u);
}
-} // namespace mlir
#include <gmock/gmock.h>
#include <gtest/gtest.h>
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
TEST(MatrixTest, ReadWrite) {
Matrix mat(5, 5);
for (unsigned col = 0; col < 7; ++col)
EXPECT_EQ(mat(row, col), row >= 3 || col >= 3 ? 0 : int(10 * row + col));
}
-
-} // namespace mlir
#include <gmock/gmock.h>
#include <gtest/gtest.h>
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
+
using testing::ElementsAre;
static Matrix makeMatrix(unsigned numRow, unsigned numColumns,
EXPECT_THAT(*nonNegPWAF.valueAt({2, -3}), ElementsAre(-1, -1));
EXPECT_FALSE(nonNegPWAF.valueAt({-2, -3}).hasValue());
}
-
-} // namespace mlir
#include <gmock/gmock.h>
#include <gtest/gtest.h>
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
+
/// Parse a list of StringRefs to IntegerPolyhedron and combine them into a
/// PresburgerSet be using the union operation. It is expected that the strings
/// are all valid IntegerSet representation and that all of them have the same
/*trueVolume=*/{},
/*resultBound=*/{});
}
-
-} // namespace mlir
#include <gtest/gtest.h>
using namespace mlir;
+using namespace presburger;
+
using IdKind = PresburgerSpace::IdKind;
TEST(PresburgerSpaceTest, insertId) {
#include <gmock/gmock.h>
#include <gtest/gtest.h>
-namespace mlir {
-using namespace presburger_utils;
+using namespace mlir;
+using namespace presburger;
/// Take a snapshot, add constraints making the set empty, and rollback.
/// The set should not be empty after rolling back. We add additional
EXPECT_TRUE(sim2.isRationalSubsetOf(s2));
EXPECT_FALSE(sim2.isRationalSubsetOf(empty));
}
-
-} // namespace mlir
#include <gtest/gtest.h>
namespace mlir {
+namespace presburger {
+
/// Parses a IntegerPolyhedron from a StringRef. It is expected that the
/// string represents a valid IntegerSet, otherwise it will violate a gtest
/// assertion.
EXPECT_TRUE(infinityOrUInt64LE(trueVolume, computedVolume));
EXPECT_TRUE(infinityOrUInt64LE(computedVolume, resultBound));
}
+
+} // namespace presburger
} // namespace mlir
#endif // MLIR_UNITTESTS_ANALYSIS_PRESBURGER_UTILS_H
#include "mlir/Support/LogicalResult.h"
namespace mlir {
+
/// This parses a single IntegerSet to an MLIR context and transforms it to
/// FlatAffineConstraints if it was valid. If not, a failure is returned. If the
/// passed `str` has additional tokens that were not part of the IntegerSet, a
#include <gtest/gtest.h>
-namespace mlir {
+using namespace mlir;
+using namespace presburger;
/// Construct a FlatAffineConstraints from a set of inequality, equality, and
/// division onstraints.
{{{0, 1, 0}, 2}, {{1, 0, 1, 0}, 3}}),
&context));
}
-
-} // namespace mlir