diff options
author | Jeremy Kun <jkun@google.com> | 2025-08-05 13:18:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-08-05 13:18:59 -0700 |
commit | 97dee3244535b4bc6cfb65a1dfa8f3ae93bd5752 (patch) | |
tree | a35aa01377ae13ae50fc06a580c86bf1fb0640c0 | |
parent | d478502a420c75ca6c52205a58e465998a249826 (diff) | |
download | llvm-97dee3244535b4bc6cfb65a1dfa8f3ae93bd5752.zip llvm-97dee3244535b4bc6cfb65a1dfa8f3ae93bd5752.tar.gz llvm-97dee3244535b4bc6cfb65a1dfa8f3ae93bd5752.tar.bz2 |
[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 <j2kun@users.noreply.github.com>
-rw-r--r-- | mlir/include/mlir/Analysis/Presburger/IntegerRelation.h | 8 | ||||
-rw-r--r-- | mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp | 12 |
2 files changed, 20 insertions, 0 deletions
diff --git a/mlir/include/mlir/Analysis/Presburger/IntegerRelation.h b/mlir/include/mlir/Analysis/Presburger/IntegerRelation.h index ee401cc..4b18024 100644 --- a/mlir/include/mlir/Analysis/Presburger/IntegerRelation.h +++ b/mlir/include/mlir/Analysis/Presburger/IntegerRelation.h @@ -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> @@ -268,6 +269,13 @@ public: 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, diff --git a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp index dd0b09f..a6ed5c5 100644 --- a/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp +++ b/mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp @@ -16,6 +16,7 @@ using namespace mlir; using namespace presburger; +using ::testing::ElementsAre; TEST(IntegerRelationTest, getDomainAndRangeSet) { IntegerRelation rel = parseRelationFromSet( @@ -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)); +} |