Skip to content

Commit 8d7c979

Browse files
authored
[MLIR][Presburger] Fix IntegerRelation::swapVar not swapping identifiers (#74407)
This commit fixes a bug where identifiers were not swapped when doing a IntegerRelation::swapVar.
1 parent 7481851 commit 8d7c979

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ void IntegerRelation::swapVar(unsigned posA, unsigned posB) {
449449
if (posA == posB)
450450
return;
451451

452+
VarKind kindA = space.getVarKindAt(posA);
453+
VarKind kindB = space.getVarKindAt(posB);
454+
unsigned relativePosA = posA - getVarKindOffset(kindA);
455+
unsigned relativePosB = posB - getVarKindOffset(kindB);
456+
space.swapVar(kindA, kindB, relativePosA, relativePosB);
457+
452458
inequalities.swapColumns(posA, posB);
453459
equalities.swapColumns(posA, posB);
454460
}

mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "mlir/Analysis/Presburger/IntegerRelation.h"
1010
#include "Parser.h"
11+
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
1112
#include "mlir/Analysis/Presburger/Simplex.h"
1213

1314
#include <gmock/gmock.h>
@@ -167,3 +168,42 @@ TEST(IntegerRelationTest, symbolicLexmax) {
167168
EXPECT_TRUE(lexmax3.unboundedDomain.isIntegerEmpty());
168169
EXPECT_TRUE(lexmax3.lexopt.isEqual(expectedLexmax3));
169170
}
171+
172+
TEST(IntegerRelationTest, swapVar) {
173+
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 1, 2, 0);
174+
space.resetIds();
175+
176+
int identifiers[6] = {0, 1, 2, 3, 4};
177+
178+
// Attach identifiers to domain identifiers.
179+
space.getId(VarKind::Domain, 0) = Identifier(&identifiers[0]);
180+
space.getId(VarKind::Domain, 1) = Identifier(&identifiers[1]);
181+
182+
// Attach identifiers to range identifiers.
183+
space.getId(VarKind::Range, 0) = Identifier(&identifiers[2]);
184+
185+
// Attach identifiers to symbol identifiers.
186+
space.getId(VarKind::Symbol, 0) = Identifier(&identifiers[3]);
187+
space.getId(VarKind::Symbol, 1) = Identifier(&identifiers[4]);
188+
189+
IntegerRelation rel =
190+
parseRelationFromSet("(x, y, z)[N, M] : (z - x - y == 0, x >= 0, N - x "
191+
">= 0, y >= 0, M - y >= 0)",
192+
2);
193+
rel.setSpace(space);
194+
// Swap (Domain 0, Range 0)
195+
rel.swapVar(0, 2);
196+
// Swap (Domain 1, Symbol 1)
197+
rel.swapVar(1, 4);
198+
199+
PresburgerSpace swappedSpace = rel.getSpace();
200+
201+
EXPECT_TRUE(swappedSpace.getId(VarKind::Domain, 0)
202+
.isEqual(space.getId(VarKind::Range, 0)));
203+
EXPECT_TRUE(swappedSpace.getId(VarKind::Domain, 1)
204+
.isEqual(space.getId(VarKind::Symbol, 1)));
205+
EXPECT_TRUE(swappedSpace.getId(VarKind::Range, 0)
206+
.isEqual(space.getId(VarKind::Domain, 0)));
207+
EXPECT_TRUE(swappedSpace.getId(VarKind::Symbol, 1)
208+
.isEqual(space.getId(VarKind::Domain, 1)));
209+
}

0 commit comments

Comments
 (0)