2018-06-28 02:03:08 +08:00
|
|
|
//===- AffineMap.cpp - MLIR Affine Map Classes ----------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
2018-07-10 00:00:25 +08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
2018-10-06 09:24:18 +08:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2018-10-04 01:07:54 +08:00
|
|
|
#include "mlir/Support/MathExtras.h"
|
2018-06-28 02:03:08 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2018-10-06 09:24:18 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// AffineExprConstantFolder evaluates an affine expression using constant
|
|
|
|
// operands passed in 'operandConsts'. Returns a pointer to an IntegerAttr
|
|
|
|
// attribute representing the constant value of the affine expression
|
|
|
|
// evaluated on constant 'operandConsts'.
|
|
|
|
class AffineExprConstantFolder {
|
|
|
|
public:
|
|
|
|
AffineExprConstantFolder(unsigned numDims,
|
|
|
|
ArrayRef<Attribute *> operandConsts)
|
|
|
|
: numDims(numDims), operandConsts(operandConsts) {}
|
|
|
|
|
|
|
|
/// Attempt to constant fold the specified affine expr, or return null on
|
|
|
|
/// failure.
|
|
|
|
IntegerAttr *constantFold(AffineExprRef expr) {
|
|
|
|
switch (expr->getKind()) {
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Add:
|
2018-10-06 09:24:18 +08:00
|
|
|
return constantFoldBinExpr(
|
|
|
|
expr, [](int64_t lhs, int64_t rhs) { return lhs + rhs; });
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Mul:
|
2018-10-06 09:24:18 +08:00
|
|
|
return constantFoldBinExpr(
|
|
|
|
expr, [](int64_t lhs, int64_t rhs) { return lhs * rhs; });
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Mod:
|
2018-10-06 09:24:18 +08:00
|
|
|
return constantFoldBinExpr(
|
|
|
|
expr, [](int64_t lhs, uint64_t rhs) { return mod(lhs, rhs); });
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::FloorDiv:
|
2018-10-06 09:24:18 +08:00
|
|
|
return constantFoldBinExpr(
|
|
|
|
expr, [](int64_t lhs, uint64_t rhs) { return floorDiv(lhs, rhs); });
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::CeilDiv:
|
2018-10-06 09:24:18 +08:00
|
|
|
return constantFoldBinExpr(
|
|
|
|
expr, [](int64_t lhs, uint64_t rhs) { return ceilDiv(lhs, rhs); });
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::Constant:
|
2018-10-08 23:09:50 +08:00
|
|
|
return IntegerAttr::get(expr.cast<AffineConstantExprRef>()->getValue(),
|
2018-10-06 09:24:18 +08:00
|
|
|
expr->getContext());
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::DimId:
|
2018-10-06 09:24:18 +08:00
|
|
|
return dyn_cast_or_null<IntegerAttr>(
|
2018-10-08 23:09:50 +08:00
|
|
|
operandConsts[expr.cast<AffineDimExprRef>()->getPosition()]);
|
2018-10-09 01:20:25 +08:00
|
|
|
case AffineExprKind::SymbolId:
|
2018-10-06 09:24:18 +08:00
|
|
|
return dyn_cast_or_null<IntegerAttr>(
|
2018-10-08 23:09:50 +08:00
|
|
|
operandConsts[numDims +
|
|
|
|
expr.cast<AffineSymbolExprRef>()->getPosition()]);
|
2018-10-06 09:24:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IntegerAttr *
|
|
|
|
constantFoldBinExpr(AffineExprRef expr,
|
|
|
|
std::function<uint64_t(int64_t, uint64_t)> op) {
|
2018-10-08 23:09:50 +08:00
|
|
|
auto binOpExpr = expr.cast<AffineBinaryOpExprRef>();
|
2018-10-06 09:24:18 +08:00
|
|
|
auto *lhs = constantFold(binOpExpr->getLHS());
|
|
|
|
auto *rhs = constantFold(binOpExpr->getRHS());
|
|
|
|
if (!lhs || !rhs)
|
|
|
|
return nullptr;
|
|
|
|
return IntegerAttr::get(op(lhs->getValue(), rhs->getValue()),
|
|
|
|
expr->getContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The number of dimension operands in AffineMap containing this expression.
|
|
|
|
unsigned numDims;
|
|
|
|
// The constant valued operands used to evaluate this AffineExpr.
|
|
|
|
ArrayRef<Attribute *> operandConsts;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-07-04 11:16:08 +08:00
|
|
|
AffineMap::AffineMap(unsigned numDims, unsigned numSymbols, unsigned numResults,
|
[RFC][MLIR] Use AffineExprRef in place of AffineExpr* in IR
This CL starts by replacing AffineExpr* with value-type AffineExprRef in a few
places in the IR. By a domino effect that is pretty telling of the
inconsistencies in the codebase, const is removed where it makes sense.
The rationale is that the decision was concisously made that unique'd types
have pointer semantics without const specifier. This is fine but we should be
consistent. In the end, the only logical invariant is that there should never
be such a thing as a const AffineExpr*, const AffineMap* or const IntegerSet*
in our codebase.
This CL takes a number of shortcuts to killing const with fire, in particular
forcing const AffineExprRef to return the underlying non-const
AffineExpr*. This will be removed once AffineExpr* has disappeared in
containers but for now such shortcuts allow a bit of sanity in this long quest
for cleanups.
The **only** places where const AffineExpr*, const AffineMap* or const
IntegerSet* may still appear is by transitive needs from containers,
comparison operators etc.
There is still one major thing remaining here: figure out why cast/dyn_cast
return me a const AffineXXX*, which in turn requires a bunch of ugly
const_casts. I suspect this is due to the classof
taking const AffineXXXExpr*. I wonder whether this is a side effect of 1., if
it is coming from llvm itself (I'd doubt it) or something else (clattner@?)
In light of this, the whole discussion about const makes total sense to me now
and I would systematically apply the rule that in the end, we should never
have any const XXX in our codebase for unique'd types (assuming we can remove
them all in containers and no additional constness constraint is added on us
from the outside world).
PiperOrigin-RevId: 215811554
2018-10-05 06:10:33 +08:00
|
|
|
ArrayRef<AffineExprRef> results,
|
|
|
|
ArrayRef<AffineExprRef> rangeSizes)
|
2018-07-04 11:16:08 +08:00
|
|
|
: numDims(numDims), numSymbols(numSymbols), numResults(numResults),
|
2018-07-12 12:31:07 +08:00
|
|
|
results(results), rangeSizes(rangeSizes) {}
|
2018-07-10 00:00:25 +08:00
|
|
|
|
2018-09-13 23:12:38 +08:00
|
|
|
/// Returns a single constant result affine map.
|
|
|
|
AffineMap *AffineMap::getConstantMap(int64_t val, MLIRContext *context) {
|
|
|
|
return get(/*dimCount=*/0, /*symbolCount=*/0,
|
2018-10-09 01:20:25 +08:00
|
|
|
{getAffineConstantExpr(val, context)}, {}, context);
|
2018-09-13 23:12:38 +08:00
|
|
|
}
|
|
|
|
|
[RFC][MLIR] Use AffineExprRef in place of AffineExpr* in IR
This CL starts by replacing AffineExpr* with value-type AffineExprRef in a few
places in the IR. By a domino effect that is pretty telling of the
inconsistencies in the codebase, const is removed where it makes sense.
The rationale is that the decision was concisously made that unique'd types
have pointer semantics without const specifier. This is fine but we should be
consistent. In the end, the only logical invariant is that there should never
be such a thing as a const AffineExpr*, const AffineMap* or const IntegerSet*
in our codebase.
This CL takes a number of shortcuts to killing const with fire, in particular
forcing const AffineExprRef to return the underlying non-const
AffineExpr*. This will be removed once AffineExpr* has disappeared in
containers but for now such shortcuts allow a bit of sanity in this long quest
for cleanups.
The **only** places where const AffineExpr*, const AffineMap* or const
IntegerSet* may still appear is by transitive needs from containers,
comparison operators etc.
There is still one major thing remaining here: figure out why cast/dyn_cast
return me a const AffineXXX*, which in turn requires a bunch of ugly
const_casts. I suspect this is due to the classof
taking const AffineXXXExpr*. I wonder whether this is a side effect of 1., if
it is coming from llvm itself (I'd doubt it) or something else (clattner@?)
In light of this, the whole discussion about const makes total sense to me now
and I would systematically apply the rule that in the end, we should never
have any const XXX in our codebase for unique'd types (assuming we can remove
them all in containers and no additional constness constraint is added on us
from the outside world).
PiperOrigin-RevId: 215811554
2018-10-05 06:10:33 +08:00
|
|
|
bool AffineMap::isIdentity() {
|
2018-08-15 03:43:51 +08:00
|
|
|
if (getNumDims() != getNumResults())
|
|
|
|
return false;
|
[RFC][MLIR] Use AffineExprRef in place of AffineExpr* in IR
This CL starts by replacing AffineExpr* with value-type AffineExprRef in a few
places in the IR. By a domino effect that is pretty telling of the
inconsistencies in the codebase, const is removed where it makes sense.
The rationale is that the decision was concisously made that unique'd types
have pointer semantics without const specifier. This is fine but we should be
consistent. In the end, the only logical invariant is that there should never
be such a thing as a const AffineExpr*, const AffineMap* or const IntegerSet*
in our codebase.
This CL takes a number of shortcuts to killing const with fire, in particular
forcing const AffineExprRef to return the underlying non-const
AffineExpr*. This will be removed once AffineExpr* has disappeared in
containers but for now such shortcuts allow a bit of sanity in this long quest
for cleanups.
The **only** places where const AffineExpr*, const AffineMap* or const
IntegerSet* may still appear is by transitive needs from containers,
comparison operators etc.
There is still one major thing remaining here: figure out why cast/dyn_cast
return me a const AffineXXX*, which in turn requires a bunch of ugly
const_casts. I suspect this is due to the classof
taking const AffineXXXExpr*. I wonder whether this is a side effect of 1., if
it is coming from llvm itself (I'd doubt it) or something else (clattner@?)
In light of this, the whole discussion about const makes total sense to me now
and I would systematically apply the rule that in the end, we should never
have any const XXX in our codebase for unique'd types (assuming we can remove
them all in containers and no additional constness constraint is added on us
from the outside world).
PiperOrigin-RevId: 215811554
2018-10-05 06:10:33 +08:00
|
|
|
ArrayRef<AffineExprRef> results = getResults();
|
2018-08-16 06:14:45 +08:00
|
|
|
for (unsigned i = 0, numDims = getNumDims(); i < numDims; ++i) {
|
2018-10-08 23:09:50 +08:00
|
|
|
auto expr = results[i].dyn_cast<AffineDimExprRef>();
|
2018-08-16 06:14:45 +08:00
|
|
|
if (!expr || expr->getPosition() != i)
|
2018-08-15 03:43:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
[RFC][MLIR] Use AffineExprRef in place of AffineExpr* in IR
This CL starts by replacing AffineExpr* with value-type AffineExprRef in a few
places in the IR. By a domino effect that is pretty telling of the
inconsistencies in the codebase, const is removed where it makes sense.
The rationale is that the decision was concisously made that unique'd types
have pointer semantics without const specifier. This is fine but we should be
consistent. In the end, the only logical invariant is that there should never
be such a thing as a const AffineExpr*, const AffineMap* or const IntegerSet*
in our codebase.
This CL takes a number of shortcuts to killing const with fire, in particular
forcing const AffineExprRef to return the underlying non-const
AffineExpr*. This will be removed once AffineExpr* has disappeared in
containers but for now such shortcuts allow a bit of sanity in this long quest
for cleanups.
The **only** places where const AffineExpr*, const AffineMap* or const
IntegerSet* may still appear is by transitive needs from containers,
comparison operators etc.
There is still one major thing remaining here: figure out why cast/dyn_cast
return me a const AffineXXX*, which in turn requires a bunch of ugly
const_casts. I suspect this is due to the classof
taking const AffineXXXExpr*. I wonder whether this is a side effect of 1., if
it is coming from llvm itself (I'd doubt it) or something else (clattner@?)
In light of this, the whole discussion about const makes total sense to me now
and I would systematically apply the rule that in the end, we should never
have any const XXX in our codebase for unique'd types (assuming we can remove
them all in containers and no additional constness constraint is added on us
from the outside world).
PiperOrigin-RevId: 215811554
2018-10-05 06:10:33 +08:00
|
|
|
bool AffineMap::isSingleConstant() {
|
2018-10-08 23:09:50 +08:00
|
|
|
return getNumResults() == 1 && getResult(0).isa<AffineConstantExprRef>();
|
2018-08-25 14:38:14 +08:00
|
|
|
}
|
|
|
|
|
[RFC][MLIR] Use AffineExprRef in place of AffineExpr* in IR
This CL starts by replacing AffineExpr* with value-type AffineExprRef in a few
places in the IR. By a domino effect that is pretty telling of the
inconsistencies in the codebase, const is removed where it makes sense.
The rationale is that the decision was concisously made that unique'd types
have pointer semantics without const specifier. This is fine but we should be
consistent. In the end, the only logical invariant is that there should never
be such a thing as a const AffineExpr*, const AffineMap* or const IntegerSet*
in our codebase.
This CL takes a number of shortcuts to killing const with fire, in particular
forcing const AffineExprRef to return the underlying non-const
AffineExpr*. This will be removed once AffineExpr* has disappeared in
containers but for now such shortcuts allow a bit of sanity in this long quest
for cleanups.
The **only** places where const AffineExpr*, const AffineMap* or const
IntegerSet* may still appear is by transitive needs from containers,
comparison operators etc.
There is still one major thing remaining here: figure out why cast/dyn_cast
return me a const AffineXXX*, which in turn requires a bunch of ugly
const_casts. I suspect this is due to the classof
taking const AffineXXXExpr*. I wonder whether this is a side effect of 1., if
it is coming from llvm itself (I'd doubt it) or something else (clattner@?)
In light of this, the whole discussion about const makes total sense to me now
and I would systematically apply the rule that in the end, we should never
have any const XXX in our codebase for unique'd types (assuming we can remove
them all in containers and no additional constness constraint is added on us
from the outside world).
PiperOrigin-RevId: 215811554
2018-10-05 06:10:33 +08:00
|
|
|
int64_t AffineMap::getSingleConstantResult() {
|
2018-08-25 14:38:14 +08:00
|
|
|
assert(isSingleConstant() && "map must have a single constant result");
|
2018-10-08 23:09:50 +08:00
|
|
|
return getResult(0).cast<AffineConstantExprRef>()->getValue();
|
2018-08-25 14:38:14 +08:00
|
|
|
}
|
|
|
|
|
[RFC][MLIR] Use AffineExprRef in place of AffineExpr* in IR
This CL starts by replacing AffineExpr* with value-type AffineExprRef in a few
places in the IR. By a domino effect that is pretty telling of the
inconsistencies in the codebase, const is removed where it makes sense.
The rationale is that the decision was concisously made that unique'd types
have pointer semantics without const specifier. This is fine but we should be
consistent. In the end, the only logical invariant is that there should never
be such a thing as a const AffineExpr*, const AffineMap* or const IntegerSet*
in our codebase.
This CL takes a number of shortcuts to killing const with fire, in particular
forcing const AffineExprRef to return the underlying non-const
AffineExpr*. This will be removed once AffineExpr* has disappeared in
containers but for now such shortcuts allow a bit of sanity in this long quest
for cleanups.
The **only** places where const AffineExpr*, const AffineMap* or const
IntegerSet* may still appear is by transitive needs from containers,
comparison operators etc.
There is still one major thing remaining here: figure out why cast/dyn_cast
return me a const AffineXXX*, which in turn requires a bunch of ugly
const_casts. I suspect this is due to the classof
taking const AffineXXXExpr*. I wonder whether this is a side effect of 1., if
it is coming from llvm itself (I'd doubt it) or something else (clattner@?)
In light of this, the whole discussion about const makes total sense to me now
and I would systematically apply the rule that in the end, we should never
have any const XXX in our codebase for unique'd types (assuming we can remove
them all in containers and no additional constness constraint is added on us
from the outside world).
PiperOrigin-RevId: 215811554
2018-10-05 06:10:33 +08:00
|
|
|
AffineExprRef AffineMap::getResult(unsigned idx) { return results[idx]; }
|
|
|
|
|
2018-10-06 09:24:18 +08:00
|
|
|
/// Folds the results of the application of an affine map on the provided
|
|
|
|
/// operands to a constant if possible. Returns false if the folding happens,
|
|
|
|
/// true otherwise.
|
|
|
|
bool AffineMap::constantFold(ArrayRef<Attribute *> operandConstants,
|
|
|
|
SmallVectorImpl<Attribute *> &results) {
|
|
|
|
assert(getNumInputs() == operandConstants.size());
|
|
|
|
|
|
|
|
// Fold each of the result expressions.
|
|
|
|
AffineExprConstantFolder exprFolder(getNumDims(), operandConstants);
|
|
|
|
// Constant fold each AffineExpr in AffineMap and add to 'results'.
|
|
|
|
for (auto expr : getResults()) {
|
|
|
|
auto *folded = exprFolder.constantFold(expr);
|
|
|
|
// If we didn't fold to a constant, then folding fails.
|
|
|
|
if (!folded)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
results.push_back(folded);
|
|
|
|
}
|
|
|
|
assert(results.size() == getNumResults() &&
|
|
|
|
"constant folding produced the wrong number of results");
|
|
|
|
return false;
|
|
|
|
}
|