2018-06-27 11:03:08 -07:00
|
|
|
//===- AffineExpr.cpp - MLIR Affine Expr 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/AffineExpr.h"
|
2018-07-09 09:00:25 -07:00
|
|
|
#include "mlir/Support/STLExtras.h"
|
2018-06-27 11:03:08 -07:00
|
|
|
|
|
|
|
using namespace mlir;
|
2018-07-09 09:00:25 -07:00
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
/// Returns true if this expression is made out of only symbols and
|
|
|
|
/// constants (no dimensional identifiers).
|
2018-07-09 09:00:25 -07:00
|
|
|
bool AffineExpr::isSymbolic() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case Kind::Constant:
|
|
|
|
return true;
|
|
|
|
case Kind::DimId:
|
|
|
|
return false;
|
|
|
|
case Kind::SymbolId:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Kind::Add:
|
|
|
|
case Kind::Sub:
|
|
|
|
case Kind::Mul:
|
|
|
|
case Kind::FloorDiv:
|
|
|
|
case Kind::CeilDiv:
|
2018-07-10 10:59:53 -07:00
|
|
|
case Kind::Mod: {
|
|
|
|
auto expr = cast<AffineBinaryOpExpr>(this);
|
|
|
|
return expr->getLHS()->isSymbolic() && expr->getRHS()->isSymbolic();
|
|
|
|
}
|
2018-07-09 09:00:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 10:59:53 -07:00
|
|
|
/// Returns true if this is a pure affine expression, i.e., multiplication,
|
|
|
|
/// floordiv, ceildiv, and mod is only allowed w.r.t constants.
|
2018-07-09 09:00:25 -07:00
|
|
|
bool AffineExpr::isPureAffine() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case Kind::SymbolId:
|
|
|
|
case Kind::DimId:
|
|
|
|
case Kind::Constant:
|
2018-07-10 10:59:53 -07:00
|
|
|
return true;
|
2018-07-09 09:00:25 -07:00
|
|
|
case Kind::Add:
|
2018-07-10 10:59:53 -07:00
|
|
|
case Kind::Sub: {
|
|
|
|
auto op = cast<AffineBinaryOpExpr>(this);
|
|
|
|
return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Kind::Mul: {
|
|
|
|
// TODO: Canonicalize the constants in binary operators to the RHS when
|
|
|
|
// possible, allowing this to merge into the next case.
|
|
|
|
auto op = cast<AffineBinaryOpExpr>(this);
|
|
|
|
return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine() &&
|
|
|
|
(isa<AffineConstantExpr>(op->getLHS()) ||
|
|
|
|
isa<AffineConstantExpr>(op->getRHS()));
|
|
|
|
}
|
2018-07-09 09:00:25 -07:00
|
|
|
case Kind::FloorDiv:
|
|
|
|
case Kind::CeilDiv:
|
2018-07-10 10:59:53 -07:00
|
|
|
case Kind::Mod: {
|
|
|
|
auto op = cast<AffineBinaryOpExpr>(this);
|
|
|
|
return op->getLHS()->isPureAffine() &&
|
|
|
|
isa<AffineConstantExpr>(op->getRHS());
|
|
|
|
}
|
2018-07-09 09:00:25 -07:00
|
|
|
}
|
|
|
|
}
|