Skip to content

Commit 69f3e00

Browse files
authored
[MLIR] NFC. Refactor IntegerRelation getSliceBounds (llvm#127308)
Refactor FlatLinearConstraints getSliceBounds. The method was too long and nested. NFC.
1 parent 9e8cd73 commit 69f3e00

File tree

2 files changed

+51
-40
lines changed

2 files changed

+51
-40
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,12 @@ class IntegerRelation {
738738
/// Same as findSymbolicIntegerLexMin but produces lexmax instead of lexmin
739739
SymbolicLexOpt findSymbolicIntegerLexMax() const;
740740

741+
/// Searches for a constraint with a non-zero coefficient at `colIdx` in
742+
/// equality (isEq=true) or inequality (isEq=false) constraints.
743+
/// Returns true and sets row found in search in `rowIdx`, false otherwise.
744+
bool findConstraintWithNonZeroAt(unsigned colIdx, bool isEq,
745+
unsigned *rowIdx) const;
746+
741747
/// Return the set difference of this set and the given set, i.e.,
742748
/// return `this \ set`.
743749
PresburgerRelation subtract(const PresburgerRelation &set) const;
@@ -820,12 +826,6 @@ class IntegerRelation {
820826
/// Normalized each constraints by the GCD of its coefficients.
821827
void normalizeConstraintsByGCD();
822828

823-
/// Searches for a constraint with a non-zero coefficient at `colIdx` in
824-
/// equality (isEq=true) or inequality (isEq=false) constraints.
825-
/// Returns true and sets row found in search in `rowIdx`, false otherwise.
826-
bool findConstraintWithNonZeroAt(unsigned colIdx, bool isEq,
827-
unsigned *rowIdx) const;
828-
829829
/// Returns true if the pos^th column is all zero for both inequalities and
830830
/// equalities.
831831
bool isColZero(unsigned pos) const;

mlir/lib/Analysis/FlatLinearValueConstraints.cpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -581,48 +581,35 @@ std::pair<AffineMap, AffineMap> FlatLinearConstraints::getLowerAndUpperBound(
581581
return {lbMap, ubMap};
582582
}
583583

584-
/// Computes the lower and upper bounds of the first 'num' dimensional
585-
/// variables (starting at 'offset') as affine maps of the remaining
586-
/// variables (dimensional and symbolic variables). Local variables are
587-
/// themselves explicitly computed as affine functions of other variables in
588-
/// this process if needed.
589-
void FlatLinearConstraints::getSliceBounds(unsigned offset, unsigned num,
590-
MLIRContext *context,
591-
SmallVectorImpl<AffineMap> *lbMaps,
592-
SmallVectorImpl<AffineMap> *ubMaps,
593-
bool closedUB) {
594-
assert(offset + num <= getNumDimVars() && "invalid range");
595-
596-
// Basic simplification.
597-
normalizeConstraintsByGCD();
598-
599-
LLVM_DEBUG(llvm::dbgs() << "getSliceBounds for variables at positions ["
600-
<< offset << ", " << offset + num << ")\n");
601-
LLVM_DEBUG(dumpPretty());
602-
603-
// Record computed/detected variables.
604-
SmallVector<AffineExpr, 8> memo(getNumVars());
584+
/// Compute a representation of `num` identifiers starting at `offset` in `cst`
585+
/// as affine expressions involving other known identifiers. Each identifier's
586+
/// expression (in terms of known identifiers) is populated into `memo`.
587+
static void computeUnknownVars(const FlatLinearConstraints &cst,
588+
MLIRContext *context, unsigned offset,
589+
unsigned num,
590+
SmallVectorImpl<AffineExpr> &memo) {
605591
// Initialize dimensional and symbolic variables.
606-
for (unsigned i = 0, e = getNumDimVars(); i < e; i++) {
592+
for (unsigned i = 0, e = cst.getNumDimVars(); i < e; i++) {
607593
if (i < offset)
608594
memo[i] = getAffineDimExpr(i, context);
609595
else if (i >= offset + num)
610596
memo[i] = getAffineDimExpr(i - num, context);
611597
}
612-
for (unsigned i = getNumDimVars(), e = getNumDimAndSymbolVars(); i < e; i++)
613-
memo[i] = getAffineSymbolExpr(i - getNumDimVars(), context);
598+
for (unsigned i = cst.getNumDimVars(), e = cst.getNumDimAndSymbolVars();
599+
i < e; i++)
600+
memo[i] = getAffineSymbolExpr(i - cst.getNumDimVars(), context);
614601

615602
bool changed;
616603
do {
617604
changed = false;
618605
// Identify yet unknown variables as constants or mod's / floordiv's of
619606
// other variables if possible.
620-
for (unsigned pos = 0; pos < getNumVars(); pos++) {
607+
for (unsigned pos = 0, f = cst.getNumVars(); pos < f; pos++) {
621608
if (memo[pos])
622609
continue;
623610

624-
auto lbConst = getConstantBound64(BoundType::LB, pos);
625-
auto ubConst = getConstantBound64(BoundType::UB, pos);
611+
auto lbConst = cst.getConstantBound64(BoundType::LB, pos);
612+
auto ubConst = cst.getConstantBound64(BoundType::UB, pos);
626613
if (lbConst.has_value() && ubConst.has_value()) {
627614
// Detect equality to a constant.
628615
if (*lbConst == *ubConst) {
@@ -633,7 +620,7 @@ void FlatLinearConstraints::getSliceBounds(unsigned offset, unsigned num,
633620

634621
// Detect a variable as modulo of another variable w.r.t a
635622
// constant.
636-
if (detectAsMod(*this, pos, offset, num, *lbConst, *ubConst, context,
623+
if (detectAsMod(cst, pos, offset, num, *lbConst, *ubConst, context,
637624
memo)) {
638625
changed = true;
639626
continue;
@@ -642,24 +629,24 @@ void FlatLinearConstraints::getSliceBounds(unsigned offset, unsigned num,
642629

643630
// Detect a variable as a floordiv of an affine function of other
644631
// variables (divisor is a positive constant).
645-
if (detectAsFloorDiv(*this, pos, context, memo)) {
632+
if (detectAsFloorDiv(cst, pos, context, memo)) {
646633
changed = true;
647634
continue;
648635
}
649636

650637
// Detect a variable as an expression of other variables.
651638
unsigned idx;
652-
if (!findConstraintWithNonZeroAt(pos, /*isEq=*/true, &idx)) {
639+
if (!cst.findConstraintWithNonZeroAt(pos, /*isEq=*/true, &idx)) {
653640
continue;
654641
}
655642

656643
// Build AffineExpr solving for variable 'pos' in terms of all others.
657644
auto expr = getAffineConstantExpr(0, context);
658645
unsigned j, e;
659-
for (j = 0, e = getNumVars(); j < e; ++j) {
646+
for (j = 0, e = cst.getNumVars(); j < e; ++j) {
660647
if (j == pos)
661648
continue;
662-
int64_t c = atEq64(idx, j);
649+
int64_t c = cst.atEq64(idx, j);
663650
if (c == 0)
664651
continue;
665652
// If any of the involved IDs hasn't been found yet, we can't proceed.
@@ -673,8 +660,8 @@ void FlatLinearConstraints::getSliceBounds(unsigned offset, unsigned num,
673660
continue;
674661

675662
// Add constant term to AffineExpr.
676-
expr = expr + atEq64(idx, getNumVars());
677-
int64_t vPos = atEq64(idx, pos);
663+
expr = expr + cst.atEq64(idx, cst.getNumVars());
664+
int64_t vPos = cst.atEq64(idx, pos);
678665
assert(vPos != 0 && "expected non-zero here");
679666
if (vPos > 0)
680667
expr = (-expr).floorDiv(vPos);
@@ -689,6 +676,30 @@ void FlatLinearConstraints::getSliceBounds(unsigned offset, unsigned num,
689676
// variable's explicit form is computed (in memo[pos]), it's not updated
690677
// again.
691678
} while (changed);
679+
}
680+
681+
/// Computes the lower and upper bounds of the first 'num' dimensional
682+
/// variables (starting at 'offset') as affine maps of the remaining
683+
/// variables (dimensional and symbolic variables). Local variables are
684+
/// themselves explicitly computed as affine functions of other variables in
685+
/// this process if needed.
686+
void FlatLinearConstraints::getSliceBounds(unsigned offset, unsigned num,
687+
MLIRContext *context,
688+
SmallVectorImpl<AffineMap> *lbMaps,
689+
SmallVectorImpl<AffineMap> *ubMaps,
690+
bool closedUB) {
691+
assert(offset + num <= getNumDimVars() && "invalid range");
692+
693+
// Basic simplification.
694+
normalizeConstraintsByGCD();
695+
696+
LLVM_DEBUG(llvm::dbgs() << "getSliceBounds for variables at positions ["
697+
<< offset << ", " << offset + num << ")\n");
698+
LLVM_DEBUG(dumpPretty());
699+
700+
// Record computed/detected variables.
701+
SmallVector<AffineExpr, 8> memo(getNumVars());
702+
computeUnknownVars(*this, context, offset, num, memo);
692703

693704
int64_t ubAdjustment = closedUB ? 0 : 1;
694705

0 commit comments

Comments
 (0)