[MLIR] Cleanup AffineExpr
This CL introduces a series of cleanups for AffineExpr value types:
1. to make it clear that the value types should be used, the pointer
AffineExpr types are put in the detail namespace. Unfortunately, since the
value type operator-> only forwards to the underlying pointer type, we
still
need to expose this in the include file for now;
2. AffineExprKind is ok to use, it thus comes out of detail and thus of
AffineExpr
3. getAffineDimExpr, getAffineSymbolExpr, getAffineConstantExpr are
similarly
extracted as free functions and their naming is mande consistent across
Builder, MLContext and AffineExpr
4. AffineBinaryOpEx::simplify functions are made into static free
functions.
In particular it is moved away from AffineMap.cpp where it does not belong
5. operator AffineExprType is made explicit
6. uses the binary operators everywhere possible
7. drops the pointer usage everywhere outside of AffineExpr.cpp,
MLIRContext.cpp and AsmPrinter.cpp
PiperOrigin-RevId: 216207212
diff --git a/lib/Analysis/AffineAnalysis.cpp b/lib/Analysis/AffineAnalysis.cpp
index 2f58500..cd49070 100644
--- a/lib/Analysis/AffineAnalysis.cpp
+++ b/lib/Analysis/AffineAnalysis.cpp
@@ -40,14 +40,14 @@
assert(eq.size() - numDims - numSymbols - 1 == localExprs.size() &&
"unexpected number of local expressions");
- auto expr = AffineConstantExpr::get(0, context);
+ auto expr = getAffineConstantExpr(0, context);
// Dimensions and symbols.
for (unsigned j = 0; j < numDims + numSymbols; j++) {
if (eq[j] == 0) {
continue;
}
- auto id = j < numDims ? AffineDimExpr::get(j, context)
- : AffineSymbolExpr::get(j - numDims, context);
+ auto id = j < numDims ? getAffineDimExpr(j, context)
+ : getAffineSymbolExpr(j - numDims, context);
expr = expr + id * eq[j];
}
@@ -190,9 +190,9 @@
// Add an existential quantifier. expr1 % expr2 is replaced by (expr1 -
// q * expr2) where q is the existential quantifier introduced.
- addLocalId(AffineBinaryOpExpr::getFloorDiv(
- toAffineExpr(lhs, numDims, numSymbols, localExprs, context),
- AffineConstantExpr::get(rhsConst, context), context));
+ auto a = toAffineExpr(lhs, numDims, numSymbols, localExprs, context);
+ auto b = getAffineConstantExpr(rhsConst, context);
+ addLocalId(a.floorDiv(b));
lhs[getLocalVarStartIndex() + numLocals - 1] = -rhsConst;
}
void visitCeilDivExpr(AffineBinaryOpExprRef expr) {
@@ -249,11 +249,13 @@
// the ceil/floor expr (simplified up until here). Add an existential
// quantifier to express its result, i.e., expr1 div expr2 is replaced
// by a new identifier, q.
- auto divKind =
- isCeil ? AffineExpr::Kind::CeilDiv : AffineExpr::Kind::FloorDiv;
- addLocalId(AffineBinaryOpExpr::get(
- divKind, toAffineExpr(lhs, numDims, numSymbols, localExprs, context),
- AffineConstantExpr::get(denominator, context), context));
+ auto a = toAffineExpr(lhs, numDims, numSymbols, localExprs, context);
+ auto b = getAffineConstantExpr(denominator, context);
+ if (isCeil) {
+ addLocalId(a.ceilDiv(b));
+ } else {
+ addLocalId(a.floorDiv(b));
+ }
lhs.assign(lhs.size(), 0);
lhs[getLocalVarStartIndex() + numLocals - 1] = 1;
}
diff --git a/lib/Analysis/AffineStructures.cpp b/lib/Analysis/AffineStructures.cpp
index 3a85bfb..2148456 100644
--- a/lib/Analysis/AffineStructures.cpp
+++ b/lib/Analysis/AffineStructures.cpp
@@ -37,7 +37,7 @@
}
bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {
- if (const_cast<AffineExprRef &>(results[idx])->isMultipleOf(factor))
+ if (results[idx]->isMultipleOf(factor))
return true;
// TODO(bondhugula): use simplifyAffineExpr and FlatAffineConstraints to
diff --git a/lib/Analysis/LoopAnalysis.cpp b/lib/Analysis/LoopAnalysis.cpp
index 0b50494..522720e 100644
--- a/lib/Analysis/LoopAnalysis.cpp
+++ b/lib/Analysis/LoopAnalysis.cpp
@@ -63,7 +63,7 @@
std::max(lbMap->getNumSymbols(), ubMap->getNumSymbols()));
auto cExpr = loopSpanExpr.dyn_cast<AffineConstantExprRef>();
if (!cExpr)
- return AffineBinaryOpExpr::getCeilDiv(loopSpanExpr, step, context);
+ return loopSpanExpr.ceilDiv(step);
loopSpan = cExpr->getValue();
}
@@ -71,8 +71,8 @@
if (loopSpan < 0)
return 0;
- return AffineConstantExpr::get(static_cast<uint64_t>(ceilDiv(loopSpan, step)),
- context);
+ return getAffineConstantExpr(static_cast<uint64_t>(ceilDiv(loopSpan, step)),
+ context);
}
/// Returns the trip count of the loop if it's a constant, None otherwise. This