[Mlir-commits] [mlir] [MLIR][Presburger] optimize bound computation by pruning orthogonal constraints (PR #164199)
Arjun P
llvmlistbot at llvm.org
Sun Nov 30 11:48:29 PST 2025
================
@@ -1723,12 +1724,78 @@ std::optional<DynamicAPInt> IntegerRelation::getConstantBoundOnDimSize(
return minDiff;
}
+void IntegerRelation::pruneConstraints(unsigned pos) {
+ llvm::DenseSet<unsigned> relatedCols({pos}), relatedRows;
+
+ // Early quit if constraints is empty.
+ unsigned numConstraints = getNumConstraints();
+ if (numConstraints == 0)
+ return;
+
+ llvm::SmallVector<unsigned> rowStack, colStack({pos});
+ // The following code performs a graph traversal, starting from the target
+ // variable, to identify all variables(recorded in relatedCols) and
+ // constraints(recorded in relatedRows) belonging to the same connected
+ // component.
+ while (!rowStack.empty() || !colStack.empty()) {
+ if (!rowStack.empty()) {
+ unsigned currentRow = rowStack.pop_back_val();
+ // Push all variable that accociated to this constrain to relatedCols
+ // and colStack.
+ for (uint64_t colIndex = 0; colIndex < getNumVars(); ++colIndex) {
+ if (currentRow < getNumInequalities()) {
+ if (atIneq(currentRow, colIndex) != 0 &&
+ relatedCols.insert(colIndex).second) {
+ colStack.push_back(colIndex);
+ }
+ } else {
+ if (atEq(currentRow - getNumInequalities(), colIndex) != 0 &&
+ relatedCols.insert(colIndex).second) {
+ colStack.push_back(colIndex);
+ }
+ }
+ }
+ } else {
+ unsigned currentCol = colStack.pop_back_val();
+ // Push all constrains that accociated to this variable to relatedRows
+ // and rowStack.
+ for (uint64_t rowIndex = 0; rowIndex < numConstraints; ++rowIndex) {
+ if (rowIndex < getNumInequalities()) {
+ if (atIneq(rowIndex, currentCol) != 0 &&
+ relatedRows.insert(rowIndex).second) {
+ rowStack.push_back(rowIndex);
+ }
+ } else {
+ if (atEq(rowIndex - getNumInequalities(), currentCol) != 0 &&
+ relatedRows.insert(rowIndex).second) {
+ rowStack.push_back(rowIndex);
+ }
+ }
+ }
+ }
+ }
+
+ // Prune all constraints not related to target variable.
+ for (int64_t constraintId = numConstraints - 1; constraintId >= 0;
+ --constraintId) {
+ if (!relatedRows.contains(constraintId)) {
+ if (constraintId >= getNumInequalities()) {
----------------
Superty wrote:
It's preferred to early-exit if constraintId is relevant and then do the removal otherwise, to reduce nesting (https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code)
https://github.com/llvm/llvm-project/pull/164199
More information about the Mlir-commits
mailing list