Skip to content

Commit d70bfeb

Browse files
authored
[MLIR][Presburger] Implement IntegerRelation::setId (#77872)
1 parent 7a8f5d9 commit d70bfeb

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ class IntegerRelation {
122122
/// current space; this will result in an assert failure.
123123
void setSpaceExceptLocals(const PresburgerSpace &oSpace);
124124

125+
/// Set the identifier for the ith variable of the specified kind of the
126+
/// IntegerRelation's PresburgerSpace. The index is relative to the kind of
127+
/// the variable.
128+
void setId(VarKind kind, unsigned i, Identifier id);
129+
125130
/// Returns a copy of the space without locals.
126131
PresburgerSpace getSpaceWithoutLocals() const {
127132
return PresburgerSpace::getRelationSpace(space.getNumDomainVars(),

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ void IntegerRelation::setSpaceExceptLocals(const PresburgerSpace &oSpace) {
6666
space.insertVar(VarKind::Local, 0, newNumLocals);
6767
}
6868

69+
void IntegerRelation::setId(VarKind kind, unsigned i, Identifier id) {
70+
assert(space.isUsingIds() &&
71+
"space must be using identifiers to set an identifier");
72+
assert(kind != VarKind::Local && "local variables cannot have identifiers");
73+
assert(i < space.getNumVarKind(kind) && "invalid variable index");
74+
space.getId(kind, i) = id;
75+
}
76+
6977
void IntegerRelation::append(const IntegerRelation &other) {
7078
assert(space.isEqual(other.getSpace()) && "Spaces must be equal.");
7179

mlir/unittests/Analysis/Presburger/IntegerRelationTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,37 @@ TEST(IntegerRelationTest, mergeAndAlignCommonSuffixSymbols) {
451451
EXPECT_EQ(otherSpace.getId(VarKind::Range, 1),
452452
Identifier(&otherIdentifiers[3]));
453453
}
454+
455+
TEST(IntegerRelationTest, setId) {
456+
IntegerRelation rel = parseRelationFromSet(
457+
"(x, y, z)[A, B, C, D] : (x + A - C - y + D - z >= 0)", 2);
458+
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 1, 4, 0);
459+
space.resetIds();
460+
461+
// Attach identifiers.
462+
int identifiers[7] = {'x', 'y', 'z', 'A', 'B', 'C', 'D'};
463+
space.getId(VarKind::Domain, 0) = Identifier(&identifiers[0]);
464+
space.getId(VarKind::Domain, 1) = Identifier(&identifiers[1]);
465+
space.getId(VarKind::Range, 0) = Identifier(&identifiers[2]);
466+
space.getId(VarKind::Symbol, 0) = Identifier(&identifiers[3]);
467+
space.getId(VarKind::Symbol, 1) = Identifier(&identifiers[4]);
468+
space.getId(VarKind::Symbol, 2) = Identifier(&identifiers[5]);
469+
space.getId(VarKind::Symbol, 3) = Identifier(&identifiers[6]);
470+
rel.setSpace(space);
471+
472+
int newIdentifiers[3] = {1, 2, 3};
473+
rel.setId(VarKind::Domain, 1, Identifier(&newIdentifiers[0]));
474+
rel.setId(VarKind::Range, 0, Identifier(&newIdentifiers[1]));
475+
rel.setId(VarKind::Symbol, 2, Identifier(&newIdentifiers[2]));
476+
477+
space = rel.getSpace();
478+
// Check that new identifiers are set correctly.
479+
EXPECT_EQ(space.getId(VarKind::Domain, 1), Identifier(&newIdentifiers[0]));
480+
EXPECT_EQ(space.getId(VarKind::Range, 0), Identifier(&newIdentifiers[1]));
481+
EXPECT_EQ(space.getId(VarKind::Symbol, 2), Identifier(&newIdentifiers[2]));
482+
// Check that old identifier are not changed.
483+
EXPECT_EQ(space.getId(VarKind::Domain, 0), Identifier(&identifiers[0]));
484+
EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[3]));
485+
EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[4]));
486+
EXPECT_EQ(space.getId(VarKind::Symbol, 3), Identifier(&identifiers[6]));
487+
}

0 commit comments

Comments
 (0)