Skip to content

[MLIR][Presburger] add iterVarKind for convenient iterating over variables #152091

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
Aug 5, 2025
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
8 changes: 8 additions & 0 deletions mlir/include/mlir/Analysis/Presburger/IntegerRelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
#include "mlir/Analysis/Presburger/Utils.h"
#include "llvm/ADT/DynamicAPInt.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/LogicalResult.h"
#include <optional>
Expand Down Expand Up @@ -268,6 +269,13 @@ class IntegerRelation {
return space.getVarKindEnd(kind);
}

/// Return an interator over the variables of the specified kind
/// starting at the relevant offset. The return type is auto in
/// keeping with the convention for iterators.
auto iterVarKind(VarKind kind) {
return llvm::seq(getVarKindOffset(kind), getVarKindEnd(kind));
}

/// Get the number of elements of the specified kind in the range
/// [varStart, varLimit).
unsigned getVarKindOverlap(VarKind kind, unsigned varStart,
Expand Down
12 changes: 12 additions & 0 deletions mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using namespace mlir;
using namespace presburger;
using ::testing::ElementsAre;

TEST(IntegerRelationTest, getDomainAndRangeSet) {
IntegerRelation rel = parseRelationFromSet(
Expand Down Expand Up @@ -702,3 +703,14 @@ TEST(IntegerRelationTest, rangeProductSymbols) {

EXPECT_TRUE(expected.isEqual(rangeProd));
}

TEST(IntegerRelationTest, getVarKindRange) {
IntegerRelation r1 = parseRelationFromSet(
"(i1, i2, i3, i4, i5) : (i1 >= 0, i2 >= 0, i3 >= 0, i4 >= 0, i5 >= 0)",
2);
SmallVector<unsigned> actual;
for (unsigned var : r1.iterVarKind(VarKind::Range)) {
actual.push_back(var);
}
EXPECT_THAT(actual, ElementsAre(2, 3, 4));
}