Skip to content

mlir/Presburger: strip dependency on MLIRSupport #96517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion mlir/include/mlir/Analysis/Presburger/Barvinok.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "mlir/Analysis/Presburger/Matrix.h"
#include "mlir/Analysis/Presburger/PresburgerRelation.h"
#include "mlir/Analysis/Presburger/QuasiPolynomial.h"
#include <bitset>
#include <optional>

namespace mlir {
Expand Down
11 changes: 6 additions & 5 deletions mlir/include/mlir/Analysis/Presburger/IntegerRelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
#include "mlir/Analysis/Presburger/Matrix.h"
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
#include "mlir/Analysis/Presburger/Utils.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/DynamicAPInt.h"
#include "llvm/ADT/SmallVector.h"
#include <optional>

namespace mlir {
namespace presburger {
using llvm::DynamicAPInt;
using llvm::int64fromDynamicAPInt;
using llvm::SmallVectorImpl;

class IntegerRelation;
class IntegerPolyhedron;
Expand Down Expand Up @@ -477,7 +478,7 @@ class IntegerRelation {
/// equality detection; if successful, the constant is substituted for the
/// variable everywhere in the constraint system and then removed from the
/// system.
LogicalResult constantFoldVar(unsigned pos);
bool constantFoldVar(unsigned pos);

/// This method calls `constantFoldVar` for the specified range of variables,
/// `num` variables starting at position `pos`.
Expand All @@ -500,7 +501,7 @@ class IntegerRelation {
/// 3) this = {0 <= d0 <= 5, 1 <= d1 <= 9}
/// other = {2 <= d0 <= 6, 5 <= d1 <= 15},
/// output = {0 <= d0 <= 6, 1 <= d1 <= 15}
LogicalResult unionBoundingBox(const IntegerRelation &other);
bool unionBoundingBox(const IntegerRelation &other);

/// Returns the smallest known constant bound for the extent of the specified
/// variable (pos^th), i.e., the smallest known constant that is greater
Expand Down Expand Up @@ -773,8 +774,8 @@ class IntegerRelation {
/// Eliminates a single variable at `position` from equality and inequality
/// constraints. Returns `success` if the variable was eliminated, and
/// `failure` otherwise.
inline LogicalResult gaussianEliminateVar(unsigned position) {
return success(gaussianEliminateVars(position, position + 1) == 1);
inline bool gaussianEliminateVar(unsigned position) {
return gaussianEliminateVars(position, position + 1) == 1;
}

/// Removes local variables using equalities. Each equality is checked if it
Expand Down
7 changes: 4 additions & 3 deletions mlir/include/mlir/Analysis/Presburger/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
#define MLIR_ANALYSIS_PRESBURGER_MATRIX_H

#include "mlir/Analysis/Presburger/Fraction.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/raw_ostream.h"

#include <bitset>
#include <cassert>

namespace mlir {
namespace presburger {
using llvm::ArrayRef;
using llvm::MutableArrayRef;
using llvm::raw_ostream;
using llvm::SmallVector;

/// This is a class to represent a resizable matrix.
///
Expand Down
12 changes: 7 additions & 5 deletions mlir/include/mlir/Analysis/Presburger/PresburgerSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
#ifndef MLIR_ANALYSIS_PRESBURGER_PRESBURGERSPACE_H
#define MLIR_ANALYSIS_PRESBURGER_PRESBURGERSPACE_H

#include "mlir/Support/TypeID.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include "llvm/Support/TypeName.h"
#include "llvm/Support/raw_ostream.h"

namespace mlir {
namespace presburger {
using llvm::ArrayRef;
using llvm::SmallVector;

/// Kind of variable. Implementation wise SetDims are treated as Range
/// vars, and spaces with no distinction between dimension vars are treated
Expand Down Expand Up @@ -74,7 +76,7 @@ class Identifier {
explicit Identifier(T value)
: value(llvm::PointerLikeTypeTraits<T>::getAsVoidPointer(value)) {
#ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
idType = TypeID::get<T>();
idType = llvm::getTypeName<T>();
#endif
}

Expand All @@ -83,7 +85,7 @@ class Identifier {
template <typename T>
T getValue() const {
#ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
assert(TypeID::get<T>() == idType &&
assert(llvm::getTypeName<T>() == idType &&
"Identifier was initialized with a different type than the one used "
"to retrieve it.");
#endif
Expand All @@ -108,7 +110,7 @@ class Identifier {

#ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
/// TypeID of the identifiers in space. This should be used in asserts only.
TypeID idType = TypeID::get<void>();
llvm::StringRef idType;
#endif
};

Expand Down
17 changes: 6 additions & 11 deletions mlir/include/mlir/Analysis/Presburger/Simplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
#include "mlir/Analysis/Presburger/Matrix.h"
#include "mlir/Analysis/Presburger/PWMAFunction.h"
#include "mlir/Analysis/Presburger/Utils.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>

namespace mlir {
Expand Down Expand Up @@ -450,7 +445,7 @@ class LexSimplexBase : public SimplexBase {
/// lexicopositivity of the basis transform. The row must have a non-positive
/// sample value. If this is not possible, return failure. This occurs when
/// the constraints have no solution or the sample value is zero.
LogicalResult moveRowUnknownToColumn(unsigned row);
bool moveRowUnknownToColumn(unsigned row);

/// Given a row that has a non-integer sample value, add an inequality to cut
/// away this fractional sample value from the polytope without removing any
Expand All @@ -464,7 +459,7 @@ class LexSimplexBase : public SimplexBase {
///
/// Return failure if the tableau became empty, and success if it didn't.
/// Failure status indicates that the polytope was integer empty.
LogicalResult addCut(unsigned row);
bool addCut(unsigned row);

/// Undo the addition of the last constraint. This is only called while
/// rolling back.
Expand Down Expand Up @@ -516,7 +511,7 @@ class LexSimplex : public LexSimplexBase {
MaybeOptimum<SmallVector<Fraction, 8>> getRationalSample() const;

/// Make the tableau configuration consistent.
LogicalResult restoreRationalConsistency();
bool restoreRationalConsistency();

/// Return whether the specified row is violated;
bool rowIsViolated(unsigned row) const;
Expand Down Expand Up @@ -631,7 +626,7 @@ class SymbolicLexSimplex : public LexSimplexBase {
/// Return failure if the tableau became empty, indicating that the polytope
/// is always integer empty in the current symbol domain.
/// Return success otherwise.
LogicalResult doNonBranchingPivots();
bool doNonBranchingPivots();

/// Get a row that is always violated in the current domain, if one exists.
std::optional<unsigned> maybeGetAlwaysViolatedRow();
Expand All @@ -652,7 +647,7 @@ class SymbolicLexSimplex : public LexSimplexBase {
/// at the time of the call. (This function may modify the symbol domain, but
/// failure statu indicates that the polytope was empty for all symbol values
/// in the initial domain.)
LogicalResult addSymbolicCut(unsigned row);
bool addSymbolicCut(unsigned row);

/// Get the numerator of the symbolic sample of the specific row.
/// This is an affine expression in the symbols with integer coefficients.
Expand Down Expand Up @@ -825,7 +820,7 @@ class Simplex : public SimplexBase {
///
/// Returns success if the unknown was successfully restored to a non-negative
/// sample value, failure otherwise.
LogicalResult restoreRow(Unknown &u);
bool restoreRow(Unknown &u);

/// Find a pivot to change the sample value of row in the specified
/// direction while preserving tableau consistency, except that if the
Expand Down
4 changes: 1 addition & 3 deletions mlir/include/mlir/Analysis/Presburger/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
#ifndef MLIR_ANALYSIS_PRESBURGER_UTILS_H
#define MLIR_ANALYSIS_PRESBURGER_UTILS_H

#include "mlir/Support/LLVM.h"
#include "mlir/Analysis/Presburger/Matrix.h"
#include "llvm/ADT/DynamicAPInt.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"

#include "mlir/Analysis/Presburger/Matrix.h"
#include <optional>

namespace mlir {
Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Analysis/FlatLinearValueConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "mlir/IR/Builders.h"
#include "mlir/IR/IntegerSet.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -1247,10 +1248,10 @@ LogicalResult FlatLinearValueConstraints::unionBoundingBox(
if (!areVarsAligned(*this, otherCst)) {
FlatLinearValueConstraints otherCopy(otherCst);
mergeAndAlignVars(/*offset=*/getNumDimVars(), this, &otherCopy);
return IntegerPolyhedron::unionBoundingBox(otherCopy);
return success(IntegerPolyhedron::unionBoundingBox(otherCopy));
}

return IntegerPolyhedron::unionBoundingBox(otherCst);
return success(IntegerPolyhedron::unionBoundingBox(otherCst));
}

//===----------------------------------------------------------------------===//
Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Analysis/Presburger/Barvinok.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "mlir/Analysis/Presburger/Utils.h"
#include "llvm/ADT/Sequence.h"
#include <algorithm>
#include <bitset>

using namespace mlir;
using namespace presburger;
Expand Down
6 changes: 1 addition & 5 deletions mlir/lib/Analysis/Presburger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ add_mlir_library(MLIRPresburger
PWMAFunction.cpp
QuasiPolynomial.cpp
Simplex.cpp
Utils.cpp

LINK_LIBS PUBLIC
MLIRSupport
)
Utils.cpp)
27 changes: 12 additions & 15 deletions mlir/lib/Analysis/Presburger/IntegerRelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
#include "mlir/Analysis/Presburger/Simplex.h"
#include "mlir/Analysis/Presburger/Utils.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
Expand Down Expand Up @@ -1554,22 +1552,22 @@ static int findEqualityToConstant(const IntegerRelation &cst, unsigned pos,
return -1;
}

LogicalResult IntegerRelation::constantFoldVar(unsigned pos) {
bool IntegerRelation::constantFoldVar(unsigned pos) {
assert(pos < getNumVars() && "invalid position");
int rowIdx;
if ((rowIdx = findEqualityToConstant(*this, pos)) == -1)
return failure();
return false;

// atEq(rowIdx, pos) is either -1 or 1.
assert(atEq(rowIdx, pos) * atEq(rowIdx, pos) == 1);
DynamicAPInt constVal = -atEq(rowIdx, getNumCols() - 1) / atEq(rowIdx, pos);
setAndEliminate(pos, constVal);
return success();
return true;
}

void IntegerRelation::constantFoldVarRange(unsigned pos, unsigned num) {
for (unsigned s = pos, t = pos, e = pos + num; s < e; s++) {
if (failed(constantFoldVar(t)))
if (!constantFoldVar(t))
t++;
}
}
Expand Down Expand Up @@ -1946,9 +1944,9 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
for (unsigned r = 0, e = getNumEqualities(); r < e; r++) {
if (atEq(r, pos) != 0) {
// Use Gaussian elimination here (since we have an equality).
LogicalResult ret = gaussianEliminateVar(pos);
bool ret = gaussianEliminateVar(pos);
(void)ret;
assert(succeeded(ret) && "Gaussian elimination guaranteed to succeed");
assert(ret && "Gaussian elimination guaranteed to succeed");
LLVM_DEBUG(llvm::dbgs() << "FM output (through Gaussian elimination):\n");
LLVM_DEBUG(dump());
return;
Expand Down Expand Up @@ -2175,8 +2173,7 @@ static void getCommonConstraints(const IntegerRelation &a,

// Computes the bounding box with respect to 'other' by finding the min of the
// lower bounds and the max of the upper bounds along each of the dimensions.
LogicalResult
IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
bool IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
assert(space.isEqual(otherCst.getSpace()) && "Spaces should match.");
assert(getNumLocalVars() == 0 && "local ids not supported yet here");

Expand Down Expand Up @@ -2204,13 +2201,13 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
if (!extent.has_value())
// TODO: symbolic extents when necessary.
// TODO: handle union if a dimension is unbounded.
return failure();
return false;

auto otherExtent = otherCst.getConstantBoundOnDimSize(
d, &otherLb, &otherLbFloorDivisor, &otherUb);
if (!otherExtent.has_value() || lbFloorDivisor != otherLbFloorDivisor)
// TODO: symbolic extents when necessary.
return failure();
return false;

assert(lbFloorDivisor > 0 && "divisor always expected to be positive");

Expand All @@ -2230,7 +2227,7 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
auto constLb = getConstantBound(BoundType::LB, d);
auto constOtherLb = otherCst.getConstantBound(BoundType::LB, d);
if (!constLb.has_value() || !constOtherLb.has_value())
return failure();
return false;
std::fill(minLb.begin(), minLb.end(), 0);
minLb.back() = std::min(*constLb, *constOtherLb);
}
Expand All @@ -2246,7 +2243,7 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
auto constUb = getConstantBound(BoundType::UB, d);
auto constOtherUb = otherCst.getConstantBound(BoundType::UB, d);
if (!constUb.has_value() || !constOtherUb.has_value())
return failure();
return false;
std::fill(maxUb.begin(), maxUb.end(), 0);
maxUb.back() = std::max(*constUb, *constOtherUb);
}
Expand Down Expand Up @@ -2284,7 +2281,7 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
// union (since the above are just the union along dimensions); we shouldn't
// be discarding any other constraints on the symbols.

return success();
return true;
}

bool IntegerRelation::isColZero(unsigned pos) const {
Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Analysis/Presburger/LinearTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "mlir/Analysis/Presburger/LinearTransform.h"
#include "mlir/Analysis/Presburger/IntegerRelation.h"
#include "mlir/Analysis/Presburger/Matrix.h"
#include "mlir/Support/LLVM.h"
#include <utility>

using namespace mlir;
Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Analysis/Presburger/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "mlir/Analysis/Presburger/Matrix.h"
#include "mlir/Analysis/Presburger/Fraction.h"
#include "mlir/Analysis/Presburger/Utils.h"
#include "mlir/Support/LLVM.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
Expand Down
1 change: 0 additions & 1 deletion mlir/lib/Analysis/Presburger/PWMAFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "mlir/Analysis/Presburger/PresburgerRelation.h"
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
#include "mlir/Analysis/Presburger/Utils.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
Expand Down
Loading
Loading