Skip to content

Commit f819302

Browse files
authored
mlir/Presburger: reinstate use of LogicalResult (#97415)
Follow up on a desire post-landing d0fee98 (mlir/Presburger: strip dependency on MLIRSupport) to reinstate the use of LogicalResult in Presburger. Since db791b2 (mlir/LogicalResult: move into llvm), LogicalResult is in LLVM, and fulfilling this desire is possible while still maintaining the goal of stripping the Presburger library of mlir dependencies.
1 parent 915ee0b commit f819302

File tree

7 files changed

+106
-94
lines changed

7 files changed

+106
-94
lines changed

mlir/include/mlir/Analysis/Presburger/IntegerRelation.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
#include "mlir/Analysis/Presburger/Utils.h"
2222
#include "llvm/ADT/DynamicAPInt.h"
2323
#include "llvm/ADT/SmallVector.h"
24+
#include "llvm/Support/LogicalResult.h"
2425
#include <optional>
2526

2627
namespace mlir {
2728
namespace presburger {
2829
using llvm::DynamicAPInt;
30+
using llvm::failure;
2931
using llvm::int64fromDynamicAPInt;
32+
using llvm::LogicalResult;
3033
using llvm::SmallVectorImpl;
34+
using llvm::success;
3135

3236
class IntegerRelation;
3337
class IntegerPolyhedron;
@@ -478,7 +482,7 @@ class IntegerRelation {
478482
/// equality detection; if successful, the constant is substituted for the
479483
/// variable everywhere in the constraint system and then removed from the
480484
/// system.
481-
bool constantFoldVar(unsigned pos);
485+
LogicalResult constantFoldVar(unsigned pos);
482486

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

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

781785
/// Removes local variables using equalities. Each equality is checked if it

mlir/include/mlir/Analysis/Presburger/Simplex.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class LexSimplexBase : public SimplexBase {
445445
/// lexicopositivity of the basis transform. The row must have a non-positive
446446
/// sample value. If this is not possible, return failure. This occurs when
447447
/// the constraints have no solution or the sample value is zero.
448-
bool moveRowUnknownToColumn(unsigned row);
448+
LogicalResult moveRowUnknownToColumn(unsigned row);
449449

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

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

513513
/// Make the tableau configuration consistent.
514-
bool restoreRationalConsistency();
514+
LogicalResult restoreRationalConsistency();
515515

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

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

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

825825
/// Find a pivot to change the sample value of row in the specified
826826
/// direction while preserving tableau consistency, except that if the

mlir/lib/Analysis/FlatLinearValueConstraints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,10 +1247,10 @@ LogicalResult FlatLinearValueConstraints::unionBoundingBox(
12471247
if (!areVarsAligned(*this, otherCst)) {
12481248
FlatLinearValueConstraints otherCopy(otherCst);
12491249
mergeAndAlignVars(/*offset=*/getNumDimVars(), this, &otherCopy);
1250-
return success(IntegerPolyhedron::unionBoundingBox(otherCopy));
1250+
return IntegerPolyhedron::unionBoundingBox(otherCopy);
12511251
}
12521252

1253-
return success(IntegerPolyhedron::unionBoundingBox(otherCst));
1253+
return IntegerPolyhedron::unionBoundingBox(otherCst);
12541254
}
12551255

12561256
//===----------------------------------------------------------------------===//

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/ADT/Sequence.h"
2727
#include "llvm/ADT/SmallBitVector.h"
2828
#include "llvm/Support/Debug.h"
29+
#include "llvm/Support/LogicalResult.h"
2930
#include "llvm/Support/raw_ostream.h"
3031
#include <algorithm>
3132
#include <cassert>
@@ -1552,22 +1553,22 @@ static int findEqualityToConstant(const IntegerRelation &cst, unsigned pos,
15521553
return -1;
15531554
}
15541555

1555-
bool IntegerRelation::constantFoldVar(unsigned pos) {
1556+
LogicalResult IntegerRelation::constantFoldVar(unsigned pos) {
15561557
assert(pos < getNumVars() && "invalid position");
15571558
int rowIdx;
15581559
if ((rowIdx = findEqualityToConstant(*this, pos)) == -1)
1559-
return false;
1560+
return failure();
15601561

15611562
// atEq(rowIdx, pos) is either -1 or 1.
15621563
assert(atEq(rowIdx, pos) * atEq(rowIdx, pos) == 1);
15631564
DynamicAPInt constVal = -atEq(rowIdx, getNumCols() - 1) / atEq(rowIdx, pos);
15641565
setAndEliminate(pos, constVal);
1565-
return true;
1566+
return success();
15661567
}
15671568

15681569
void IntegerRelation::constantFoldVarRange(unsigned pos, unsigned num) {
15691570
for (unsigned s = pos, t = pos, e = pos + num; s < e; s++) {
1570-
if (!constantFoldVar(t))
1571+
if (constantFoldVar(t).failed())
15711572
t++;
15721573
}
15731574
}
@@ -1944,9 +1945,9 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
19441945
for (unsigned r = 0, e = getNumEqualities(); r < e; r++) {
19451946
if (atEq(r, pos) != 0) {
19461947
// Use Gaussian elimination here (since we have an equality).
1947-
bool ret = gaussianEliminateVar(pos);
1948+
LogicalResult ret = gaussianEliminateVar(pos);
19481949
(void)ret;
1949-
assert(ret && "Gaussian elimination guaranteed to succeed");
1950+
assert(ret.succeeded() && "Gaussian elimination guaranteed to succeed");
19501951
LLVM_DEBUG(llvm::dbgs() << "FM output (through Gaussian elimination):\n");
19511952
LLVM_DEBUG(dump());
19521953
return;
@@ -2173,7 +2174,8 @@ static void getCommonConstraints(const IntegerRelation &a,
21732174

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

@@ -2201,13 +2203,13 @@ bool IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22012203
if (!extent.has_value())
22022204
// TODO: symbolic extents when necessary.
22032205
// TODO: handle union if a dimension is unbounded.
2204-
return false;
2206+
return failure();
22052207

22062208
auto otherExtent = otherCst.getConstantBoundOnDimSize(
22072209
d, &otherLb, &otherLbFloorDivisor, &otherUb);
22082210
if (!otherExtent.has_value() || lbFloorDivisor != otherLbFloorDivisor)
22092211
// TODO: symbolic extents when necessary.
2210-
return false;
2212+
return failure();
22112213

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

@@ -2227,7 +2229,7 @@ bool IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22272229
auto constLb = getConstantBound(BoundType::LB, d);
22282230
auto constOtherLb = otherCst.getConstantBound(BoundType::LB, d);
22292231
if (!constLb.has_value() || !constOtherLb.has_value())
2230-
return false;
2232+
return failure();
22312233
std::fill(minLb.begin(), minLb.end(), 0);
22322234
minLb.back() = std::min(*constLb, *constOtherLb);
22332235
}
@@ -2243,7 +2245,7 @@ bool IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22432245
auto constUb = getConstantBound(BoundType::UB, d);
22442246
auto constOtherUb = otherCst.getConstantBound(BoundType::UB, d);
22452247
if (!constUb.has_value() || !constOtherUb.has_value())
2246-
return false;
2248+
return failure();
22472249
std::fill(maxUb.begin(), maxUb.end(), 0);
22482250
maxUb.back() = std::max(*constUb, *constOtherUb);
22492251
}
@@ -2281,7 +2283,7 @@ bool IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22812283
// union (since the above are just the union along dimensions); we shouldn't
22822284
// be discarding any other constraints on the symbols.
22832285

2284-
return true;
2286+
return success();
22852287
}
22862288

22872289
bool IntegerRelation::isColZero(unsigned pos) const {

mlir/lib/Analysis/Presburger/PresburgerRelation.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/ADT/SmallBitVector.h"
1717
#include "llvm/ADT/SmallVector.h"
18+
#include "llvm/Support/LogicalResult.h"
1819
#include "llvm/Support/raw_ostream.h"
1920
#include <cassert>
2021
#include <functional>
@@ -753,18 +754,18 @@ class presburger::SetCoalescer {
753754
/// \___\|/ \_____/
754755
///
755756
///
756-
bool coalescePairCutCase(unsigned i, unsigned j);
757+
LogicalResult coalescePairCutCase(unsigned i, unsigned j);
757758

758759
/// Types the inequality `ineq` according to its `IneqType` for `simp` into
759760
/// `redundantIneqsB` and `cuttingIneqsB`. Returns success, if no separate
760761
/// inequalities were encountered. Otherwise, returns failure.
761-
bool typeInequality(ArrayRef<DynamicAPInt> ineq, Simplex &simp);
762+
LogicalResult typeInequality(ArrayRef<DynamicAPInt> ineq, Simplex &simp);
762763

763764
/// Types the equality `eq`, i.e. for `eq` == 0, types both `eq` >= 0 and
764765
/// -`eq` >= 0 according to their `IneqType` for `simp` into
765766
/// `redundantIneqsB` and `cuttingIneqsB`. Returns success, if no separate
766767
/// inequalities were encountered. Otherwise, returns failure.
767-
bool typeEquality(ArrayRef<DynamicAPInt> eq, Simplex &simp);
768+
LogicalResult typeEquality(ArrayRef<DynamicAPInt> eq, Simplex &simp);
768769

769770
/// Replaces the element at position `i` with the last element and erases
770771
/// the last element for both `disjuncts` and `simplices`.
@@ -775,7 +776,7 @@ class presburger::SetCoalescer {
775776
/// successfully coalesced. The simplices in `simplices` need to be the ones
776777
/// constructed from `disjuncts`. At this point, there are no empty
777778
/// disjuncts in `disjuncts` left.
778-
bool coalescePair(unsigned i, unsigned j);
779+
LogicalResult coalescePair(unsigned i, unsigned j);
779780
};
780781

781782
/// Constructs a `SetCoalescer` from a `PresburgerRelation`. Only adds non-empty
@@ -818,7 +819,7 @@ PresburgerRelation SetCoalescer::coalesce() {
818819
cuttingIneqsB.clear();
819820
if (i == j)
820821
continue;
821-
if (coalescePair(i, j)) {
822+
if (coalescePair(i, j).succeeded()) {
822823
broken = true;
823824
break;
824825
}
@@ -902,15 +903,15 @@ void SetCoalescer::addCoalescedDisjunct(unsigned i, unsigned j,
902903
/// \___\|/ \_____/
903904
///
904905
///
905-
bool SetCoalescer::coalescePairCutCase(unsigned i, unsigned j) {
906+
LogicalResult SetCoalescer::coalescePairCutCase(unsigned i, unsigned j) {
906907
/// All inequalities of `b` need to be redundant. We already know that the
907908
/// redundant ones are, so only the cutting ones remain to be checked.
908909
Simplex &simp = simplices[i];
909910
IntegerRelation &disjunct = disjuncts[i];
910911
if (llvm::any_of(cuttingIneqsA, [this, &simp](ArrayRef<DynamicAPInt> curr) {
911912
return !isFacetContained(curr, simp);
912913
}))
913-
return false;
914+
return failure();
914915
IntegerRelation newSet(disjunct.getSpace());
915916

916917
for (ArrayRef<DynamicAPInt> curr : redundantIneqsA)
@@ -920,23 +921,25 @@ bool SetCoalescer::coalescePairCutCase(unsigned i, unsigned j) {
920921
newSet.addInequality(curr);
921922

922923
addCoalescedDisjunct(i, j, newSet);
923-
return true;
924+
return success();
924925
}
925926

926-
bool SetCoalescer::typeInequality(ArrayRef<DynamicAPInt> ineq, Simplex &simp) {
927+
LogicalResult SetCoalescer::typeInequality(ArrayRef<DynamicAPInt> ineq,
928+
Simplex &simp) {
927929
Simplex::IneqType type = simp.findIneqType(ineq);
928930
if (type == Simplex::IneqType::Redundant)
929931
redundantIneqsB.push_back(ineq);
930932
else if (type == Simplex::IneqType::Cut)
931933
cuttingIneqsB.push_back(ineq);
932934
else
933-
return false;
934-
return true;
935+
return failure();
936+
return success();
935937
}
936938

937-
bool SetCoalescer::typeEquality(ArrayRef<DynamicAPInt> eq, Simplex &simp) {
938-
if (!typeInequality(eq, simp))
939-
return false;
939+
LogicalResult SetCoalescer::typeEquality(ArrayRef<DynamicAPInt> eq,
940+
Simplex &simp) {
941+
if (typeInequality(eq, simp).failed())
942+
return failure();
940943
negEqs.push_back(getNegatedCoeffs(eq));
941944
ArrayRef<DynamicAPInt> inv(negEqs.back());
942945
return typeInequality(inv, simp);
@@ -951,15 +954,15 @@ void SetCoalescer::eraseDisjunct(unsigned i) {
951954
simplices.pop_back();
952955
}
953956

954-
bool SetCoalescer::coalescePair(unsigned i, unsigned j) {
957+
LogicalResult SetCoalescer::coalescePair(unsigned i, unsigned j) {
955958

956959
IntegerRelation &a = disjuncts[i];
957960
IntegerRelation &b = disjuncts[j];
958961
/// Handling of local ids is not yet implemented, so these cases are
959962
/// skipped.
960963
/// TODO: implement local id support.
961964
if (a.getNumLocalVars() != 0 || b.getNumLocalVars() != 0)
962-
return false;
965+
return failure();
963966
Simplex &simpA = simplices[i];
964967
Simplex &simpB = simplices[j];
965968

@@ -969,34 +972,34 @@ bool SetCoalescer::coalescePair(unsigned i, unsigned j) {
969972
// inequality is encountered during typing, the two IntegerRelations
970973
// cannot be coalesced.
971974
for (int k = 0, e = a.getNumInequalities(); k < e; ++k)
972-
if (!typeInequality(a.getInequality(k), simpB))
973-
return false;
975+
if (typeInequality(a.getInequality(k), simpB).failed())
976+
return failure();
974977

975978
for (int k = 0, e = a.getNumEqualities(); k < e; ++k)
976-
if (!typeEquality(a.getEquality(k), simpB))
977-
return false;
979+
if (typeEquality(a.getEquality(k), simpB).failed())
980+
return failure();
978981

979982
std::swap(redundantIneqsA, redundantIneqsB);
980983
std::swap(cuttingIneqsA, cuttingIneqsB);
981984

982985
for (int k = 0, e = b.getNumInequalities(); k < e; ++k)
983-
if (!typeInequality(b.getInequality(k), simpA))
984-
return false;
986+
if (typeInequality(b.getInequality(k), simpA).failed())
987+
return failure();
985988

986989
for (int k = 0, e = b.getNumEqualities(); k < e; ++k)
987-
if (!typeEquality(b.getEquality(k), simpA))
988-
return false;
990+
if (typeEquality(b.getEquality(k), simpA).failed())
991+
return failure();
989992

990993
// If there are no cutting inequalities of `a`, `b` is contained
991994
// within `a`.
992995
if (cuttingIneqsA.empty()) {
993996
eraseDisjunct(j);
994-
return true;
997+
return success();
995998
}
996999

9971000
// Try to apply the cut case
998-
if (coalescePairCutCase(i, j))
999-
return true;
1001+
if (coalescePairCutCase(i, j).succeeded())
1002+
return success();
10001003

10011004
// Swap the vectors to compare the pair (j,i) instead of (i,j).
10021005
std::swap(redundantIneqsA, redundantIneqsB);
@@ -1006,7 +1009,7 @@ bool SetCoalescer::coalescePair(unsigned i, unsigned j) {
10061009
// within `a`.
10071010
if (cuttingIneqsA.empty()) {
10081011
eraseDisjunct(i);
1009-
return true;
1012+
return success();
10101013
}
10111014

10121015
// Try to apply the cut case

0 commit comments

Comments
 (0)