[MLIR][Presburger] Remove `spaceKind` from PresburgerSpace

This patch remove `spaceKind` from PresburgerSpace, making PresburgerSpace only
a space supporting relations.

Sets are still implemented in the same way, i.e. with a zero domain but instead
the asserts to check if the space is still set are added to users of
PresburgerSpace which treat it as a Set space.

Reviewed By: arjunp

Differential Revision: https://reviews.llvm.org/D121357
This commit is contained in:
Groverkss 2022-03-10 22:18:44 +05:30
parent 04b87cf0e7
commit 58966dd42b
8 changed files with 48 additions and 121 deletions

View File

@ -406,21 +406,6 @@ public:
void dump() const;
protected:
/// Constructs a set reserving memory for the specified number
/// of constraints and identifiers. This constructor should not be used
/// directly to create a relation and should only be used to create Sets.
/// Internally this constructs a relation with with no domain and a
/// space with no distinction between domain and range identifiers.
IntegerRelation(unsigned numReservedInequalities,
unsigned numReservedEqualities, unsigned numReservedCols,
unsigned numDims, unsigned numSymbols, unsigned numLocals)
: PresburgerLocalSpace(numDims, numSymbols, numLocals),
equalities(0, getNumIds() + 1, numReservedEqualities, numReservedCols),
inequalities(0, getNumIds() + 1, numReservedInequalities,
numReservedCols) {
assert(numReservedCols >= getNumIds() + 1);
}
/// Checks all rows of equality/inequality constraints for trivial
/// contradictions (for example: 1 == 0, 0 >= 1), which may have surfaced
/// after elimination. Returns true if an invalid constraint is found;
@ -540,7 +525,8 @@ public:
unsigned numReservedEqualities, unsigned numReservedCols,
unsigned numDims, unsigned numSymbols, unsigned numLocals)
: IntegerRelation(numReservedInequalities, numReservedEqualities,
numReservedCols, numDims, numSymbols, numLocals) {}
numReservedCols, /*numDomain=*/0, /*numRange=*/numDims,
numSymbols, numLocals) {}
/// Constructs a relation with the specified number of dimensions and symbols.
IntegerPolyhedron(unsigned numDims = 0, unsigned numSymbols = 0,
@ -567,6 +553,12 @@ public:
// Clones this object.
std::unique_ptr<IntegerPolyhedron> clone() const;
/// Insert `num` identifiers of the specified kind at position `pos`.
/// Positions are relative to the kind of identifier. Return the absolute
/// column position (i.e., not relative to the kind of identifier) of the
/// first added identifier.
unsigned insertId(IdKind kind, unsigned pos, unsigned num = 1) override;
};
} // namespace presburger

View File

@ -151,7 +151,8 @@ private:
class PWMAFunction : public PresburgerSpace {
public:
PWMAFunction(unsigned numDims, unsigned numSymbols, unsigned numOutputs)
: PresburgerSpace(numDims, numSymbols), numOutputs(numOutputs) {
: PresburgerSpace(/*numDomain=*/0, /*numRange=*/numDims, numSymbols),
numOutputs(numOutputs) {
assert(numOutputs >= 1 && "The function must output something!");
}

View File

@ -114,7 +114,7 @@ private:
/// Construct an empty PresburgerSet with the specified number of dimension
/// and symbols.
PresburgerSet(unsigned numDims = 0, unsigned numSymbols = 0)
: PresburgerSpace(numDims, numSymbols) {}
: PresburgerSpace(/*numDomain=*/0, /*numRange=*/numDims, numSymbols) {}
/// The list of polyhedrons that this set is the union of.
SmallVector<IntegerPolyhedron, 2> integerPolyhedrons;

View File

@ -54,15 +54,12 @@ enum class IdKind { Symbol, Local, Domain, Range, SetDim = Range };
/// Dimension identifiers are further divided into Domain and Range identifiers
/// to support building relations.
///
/// Spaces with distinction between domain and range identifiers should use
/// IdKind::Domain and IdKind::Range to refer to domain and range identifiers.
/// Identifiers for such spaces are stored in the following order:
/// Identifiers are stored in the following order:
/// [Domain, Range, Symbols, Locals]
///
/// Spaces with no distinction between domain and range identifiers should use
/// IdKind::SetDim to refer to dimension identifiers. Identifiers for such
/// spaces are stored in the following order:
/// [SetDim, Symbol, Locals]
/// A space with no distinction between types of dimension identifiers can
/// be implemented as a space with zero domain. IdKind::SetDim should be used
/// to refer to dimensions in such spaces.
///
/// PresburgerSpace does not allow identifiers of kind Local. See
/// PresburgerLocalSpace for an extension that does allow local identifiers.
@ -70,10 +67,8 @@ class PresburgerSpace {
friend PresburgerLocalSpace;
public:
static PresburgerSpace getRelationSpace(unsigned numDomain, unsigned numRange,
unsigned numSymbols);
static PresburgerSpace getSetSpace(unsigned numDims, unsigned numSymbols);
PresburgerSpace(unsigned numDomain, unsigned numRange, unsigned numSymbols)
: PresburgerSpace(numDomain, numRange, numSymbols, 0) {}
virtual ~PresburgerSpace() = default;
@ -127,27 +122,11 @@ public:
void print(llvm::raw_ostream &os) const;
void dump() const;
protected:
/// Space constructor for Relation space type.
PresburgerSpace(unsigned numDomain, unsigned numRange, unsigned numSymbols)
: PresburgerSpace(Relation, numDomain, numRange, numSymbols,
/*numLocals=*/0) {}
/// Space constructor for Set space type.
PresburgerSpace(unsigned numDims, unsigned numSymbols)
: PresburgerSpace(Set, /*numDomain=*/0, numDims, numSymbols,
/*numLocals=*/0) {}
private:
/// Kind of space.
enum SpaceKind { Set, Relation };
PresburgerSpace(SpaceKind spaceKind, unsigned numDomain, unsigned numRange,
unsigned numSymbols, unsigned numLocals)
: spaceKind(spaceKind), numDomain(numDomain), numRange(numRange),
numSymbols(numSymbols), numLocals(numLocals) {}
SpaceKind spaceKind;
PresburgerSpace(unsigned numDomain, unsigned numRange, unsigned numSymbols,
unsigned numLocals)
: numDomain(numDomain), numRange(numRange), numSymbols(numSymbols),
numLocals(numLocals) {}
// Number of identifiers corresponding to domain identifiers.
unsigned numDomain;
@ -166,13 +145,9 @@ private:
/// Extension of PresburgerSpace supporting Local identifiers.
class PresburgerLocalSpace : public PresburgerSpace {
public:
static PresburgerLocalSpace getRelationSpace(unsigned numDomain,
unsigned numRange,
unsigned numSymbols,
unsigned numLocals);
static PresburgerLocalSpace getSetSpace(unsigned numDims, unsigned numSymbols,
unsigned numLocals);
PresburgerLocalSpace(unsigned numDomain, unsigned numRange,
unsigned numSymbols, unsigned numLocals)
: PresburgerSpace(numDomain, numRange, numSymbols, numLocals) {}
unsigned getNumLocalIds() const { return numLocals; }
@ -191,17 +166,6 @@ public:
void print(llvm::raw_ostream &os) const;
void dump() const;
protected:
/// Local Space constructor for Relation space type.
PresburgerLocalSpace(unsigned numDomain, unsigned numRange,
unsigned numSymbols, unsigned numLocals)
: PresburgerSpace(Relation, numDomain, numRange, numSymbols, numLocals) {}
/// Local Space constructor for Set space type.
PresburgerLocalSpace(unsigned numDims, unsigned numSymbols,
unsigned numLocals)
: PresburgerSpace(Set, /*numDomain=*/0, numDims, numSymbols, numLocals) {}
};
} // namespace presburger

View File

@ -2048,3 +2048,9 @@ void IntegerRelation::print(raw_ostream &os) const {
}
void IntegerRelation::dump() const { print(llvm::errs()); }
unsigned IntegerPolyhedron::insertId(IdKind kind, unsigned pos, unsigned num) {
assert((kind != IdKind::Domain || num == 0) &&
"Domain has to be zero in a set");
return IntegerRelation::insertId(kind, pos, num);
}

View File

@ -84,6 +84,8 @@ bool MultiAffineFunction::isEqual(const MultiAffineFunction &other) const {
unsigned MultiAffineFunction::insertId(IdKind kind, unsigned pos,
unsigned num) {
assert((kind != IdKind::Domain || num == 0) &&
"Domain has to be zero in a set");
unsigned absolutePos = getIdKindOffset(kind) + pos;
output.insertColumns(absolutePos, num);
return IntegerPolyhedron::insertId(kind, pos, num);

View File

@ -13,30 +13,6 @@
using namespace mlir;
using namespace presburger;
PresburgerSpace PresburgerSpace::getRelationSpace(unsigned numDomain,
unsigned numRange,
unsigned numSymbols) {
return PresburgerSpace(numDomain, numRange, numSymbols);
}
PresburgerSpace PresburgerSpace::getSetSpace(unsigned numDims,
unsigned numSymbols) {
return PresburgerSpace(numDims, numSymbols);
}
PresburgerLocalSpace
PresburgerLocalSpace::getRelationSpace(unsigned numDomain, unsigned numRange,
unsigned numSymbols,
unsigned numLocals) {
return PresburgerLocalSpace(numDomain, numRange, numSymbols, numLocals);
}
PresburgerLocalSpace PresburgerLocalSpace::getSetSpace(unsigned numDims,
unsigned numSymbols,
unsigned numLocals) {
return PresburgerLocalSpace(numDims, numSymbols, numLocals);
}
unsigned PresburgerSpace::getNumIdKind(IdKind kind) const {
if (kind == IdKind::Domain)
return getNumDomainIds();
@ -85,16 +61,14 @@ unsigned PresburgerSpace::insertId(IdKind kind, unsigned pos, unsigned num) {
unsigned absolutePos = getIdKindOffset(kind) + pos;
if (kind == IdKind::Domain) {
assert(spaceKind == Relation && "IdKind::Domain is not supported in Set.");
if (kind == IdKind::Domain)
numDomain += num;
} else if (kind == IdKind::Range) {
else if (kind == IdKind::Range)
numRange += num;
} else if (kind == IdKind::Symbol) {
else if (kind == IdKind::Symbol)
numSymbols += num;
} else {
else
llvm_unreachable("PresburgerSpace does not support local identifiers!");
}
return absolutePos;
}
@ -107,16 +81,14 @@ void PresburgerSpace::removeIdRange(IdKind kind, unsigned idStart,
return;
unsigned numIdsEliminated = idLimit - idStart;
if (kind == IdKind::Domain) {
assert(spaceKind == Relation && "IdKind::Domain is not supported in Set.");
if (kind == IdKind::Domain)
numDomain -= numIdsEliminated;
} else if (kind == IdKind::Range) {
else if (kind == IdKind::Range)
numRange -= numIdsEliminated;
} else if (kind == IdKind::Symbol) {
else if (kind == IdKind::Symbol)
numSymbols -= numIdsEliminated;
} else {
else
llvm_unreachable("PresburgerSpace does not support local identifiers!");
}
}
unsigned PresburgerLocalSpace::insertId(IdKind kind, unsigned pos,
@ -160,26 +132,16 @@ void PresburgerSpace::setDimSymbolSeparation(unsigned newSymbolCount) {
}
void PresburgerSpace::print(llvm::raw_ostream &os) const {
if (spaceKind == Relation) {
os << "Domain: " << getNumDomainIds() << ", "
<< "Range: " << getNumRangeIds() << ", ";
} else {
os << "Dimension: " << getNumDomainIds() << ", ";
}
os << "Symbols: " << getNumSymbolIds() << "\n";
os << "Domain: " << getNumDomainIds() << ", "
<< "Range: " << getNumRangeIds() << ", "
<< "Symbols: " << getNumSymbolIds() << "\n";
}
void PresburgerSpace::dump() const { print(llvm::errs()); }
void PresburgerLocalSpace::print(llvm::raw_ostream &os) const {
if (spaceKind == Relation) {
os << "Domain: " << getNumDomainIds() << ", "
<< "Range: " << getNumRangeIds() << ", ";
} else {
os << "Dimension: " << getNumDomainIds() << ", ";
}
os << "Symbols: " << getNumSymbolIds() << ", "
<< "Locals: " << getNumLocalIds() << "\n";
PresburgerSpace::print(os);
os << "Locals: " << getNumLocalIds() << "\n";
}
void PresburgerLocalSpace::dump() const { print(llvm::errs()); }

View File

@ -14,7 +14,7 @@ using namespace mlir;
using namespace presburger;
TEST(PresburgerSpaceTest, insertId) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 1);
PresburgerSpace space(2, 2, 1);
// Try inserting 2 domain ids.
space.insertId(IdKind::Domain, 0, 2);
@ -26,7 +26,7 @@ TEST(PresburgerSpaceTest, insertId) {
}
TEST(PresburgerSpaceTest, insertIdSet) {
PresburgerSpace space = PresburgerSpace::getSetSpace(2, 1);
PresburgerSpace space(0, 2, 1);
// Try inserting 2 dimension ids. The space should have 4 range ids since
// spaces which do not distinguish between domain, range are implemented like
@ -36,7 +36,7 @@ TEST(PresburgerSpaceTest, insertIdSet) {
}
TEST(PresburgerSpaceTest, removeIdRange) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 1, 3);
PresburgerSpace space(2, 1, 3);
// Remove 1 domain identifier.
space.removeIdRange(IdKind::Domain, 0, 1);