Skip to content

Commit 97dee32

Browse files
authored
[MLIR][Presburger] add iterVarKind for convenient iterating over variables (#152091)
I find myself doing this alot ``` for (unsigned varIndex = rel.getVarKindOffset(VarKind::Domain); varIndex < rel.getVarKindEnd(VarKind::Domain); ++varIndex) { ... } ``` Adding this convenience method so I can instead do ``` for (unsigned varIndex : rel.iterVarKind(VarKind::Domain)) { ... } ``` --------- Co-authored-by: Jeremy Kun <[email protected]>
1 parent d478502 commit 97dee32

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
2121
#include "mlir/Analysis/Presburger/Utils.h"
2222
#include "llvm/ADT/DynamicAPInt.h"
23+
#include "llvm/ADT/Sequence.h"
2324
#include "llvm/ADT/SmallVector.h"
2425
#include "llvm/Support/LogicalResult.h"
2526
#include <optional>
@@ -268,6 +269,13 @@ class IntegerRelation {
268269
return space.getVarKindEnd(kind);
269270
}
270271

272+
/// Return an interator over the variables of the specified kind
273+
/// starting at the relevant offset. The return type is auto in
274+
/// keeping with the convention for iterators.
275+
auto iterVarKind(VarKind kind) {
276+
return llvm::seq(getVarKindOffset(kind), getVarKindEnd(kind));
277+
}
278+
271279
/// Get the number of elements of the specified kind in the range
272280
/// [varStart, varLimit).
273281
unsigned getVarKindOverlap(VarKind kind, unsigned varStart,

mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using namespace mlir;
1818
using namespace presburger;
19+
using ::testing::ElementsAre;
1920

2021
TEST(IntegerRelationTest, getDomainAndRangeSet) {
2122
IntegerRelation rel = parseRelationFromSet(
@@ -702,3 +703,14 @@ TEST(IntegerRelationTest, rangeProductSymbols) {
702703

703704
EXPECT_TRUE(expected.isEqual(rangeProd));
704705
}
706+
707+
TEST(IntegerRelationTest, getVarKindRange) {
708+
IntegerRelation r1 = parseRelationFromSet(
709+
"(i1, i2, i3, i4, i5) : (i1 >= 0, i2 >= 0, i3 >= 0, i4 >= 0, i5 >= 0)",
710+
2);
711+
SmallVector<unsigned> actual;
712+
for (unsigned var : r1.iterVarKind(VarKind::Range)) {
713+
actual.push_back(var);
714+
}
715+
EXPECT_THAT(actual, ElementsAre(2, 3, 4));
716+
}

0 commit comments

Comments
 (0)