2018-09-13 01:21:23 +08:00
|
|
|
//===- LoopAnalysis.cpp - Misc loop analysis routines //-------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This file implements miscellaneous loop analysis routines.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/Analysis/LoopAnalysis.h"
|
|
|
|
|
|
|
|
#include "mlir/Analysis/AffineAnalysis.h"
|
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
|
|
|
#include "mlir/IR/Statements.h"
|
2018-10-04 01:07:54 +08:00
|
|
|
#include "mlir/Support/MathExtras.h"
|
2018-09-13 01:21:23 +08:00
|
|
|
|
2018-10-04 06:39:12 +08:00
|
|
|
using namespace mlir;
|
2018-09-13 01:21:23 +08:00
|
|
|
|
|
|
|
/// Returns the trip count of the loop as an affine expression if the latter is
|
|
|
|
/// expressible as an affine expression, and nullptr otherwise. The trip count
|
|
|
|
/// expression is simplified before returning.
|
2018-10-04 06:39:12 +08:00
|
|
|
AffineExprWrap mlir::getTripCountExpr(const ForStmt &forStmt) {
|
2018-09-13 01:21:23 +08:00
|
|
|
// upper_bound - lower_bound + 1
|
|
|
|
int64_t loopSpan;
|
|
|
|
|
|
|
|
int64_t step = forStmt.getStep();
|
|
|
|
auto *context = forStmt.getContext();
|
|
|
|
|
|
|
|
if (forStmt.hasConstantBounds()) {
|
|
|
|
int64_t lb = forStmt.getConstantLowerBound();
|
|
|
|
int64_t ub = forStmt.getConstantUpperBound();
|
|
|
|
loopSpan = ub - lb + 1;
|
|
|
|
} else {
|
2018-09-19 01:22:03 +08:00
|
|
|
auto *lbMap = forStmt.getLowerBoundMap();
|
|
|
|
auto *ubMap = forStmt.getUpperBoundMap();
|
2018-09-13 01:21:23 +08:00
|
|
|
// TODO(bondhugula): handle max/min of multiple expressions.
|
2018-09-19 01:22:03 +08:00
|
|
|
if (lbMap->getNumResults() != 1 || ubMap->getNumResults() != 1)
|
2018-09-13 01:21:23 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// TODO(bondhugula): handle bounds with different operands.
|
|
|
|
// Bounds have different operands, unhandled for now.
|
2018-09-19 01:22:03 +08:00
|
|
|
if (!forStmt.matchingBoundOperandList())
|
2018-09-13 01:21:23 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// ub_expr - lb_expr + 1
|
2018-10-04 06:39:12 +08:00
|
|
|
AffineExprWrap lbExpr(lbMap->getResult(0));
|
|
|
|
AffineExprWrap ubExpr(ubMap->getResult(0));
|
|
|
|
auto loopSpanExpr = simplifyAffineExpr(
|
|
|
|
ubExpr - lbExpr + 1, std::max(lbMap->getNumDims(), ubMap->getNumDims()),
|
|
|
|
std::max(lbMap->getNumSymbols(), ubMap->getNumSymbols()));
|
|
|
|
auto *cExpr = dyn_cast<AffineConstantExpr>(loopSpanExpr.expr);
|
2018-09-13 01:21:23 +08:00
|
|
|
if (!cExpr)
|
2018-09-28 09:03:27 +08:00
|
|
|
return AffineBinaryOpExpr::getCeilDiv(loopSpanExpr, step, context);
|
2018-09-13 01:21:23 +08:00
|
|
|
loopSpan = cExpr->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 iteration loops.
|
2018-09-28 09:03:27 +08:00
|
|
|
if (loopSpan < 0)
|
2018-09-13 01:21:23 +08:00
|
|
|
return 0;
|
|
|
|
|
2018-10-04 01:07:54 +08:00
|
|
|
return AffineConstantExpr::get(static_cast<uint64_t>(ceilDiv(loopSpan, step)),
|
|
|
|
context);
|
2018-09-13 01:21:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the trip count of the loop if it's a constant, None otherwise. This
|
|
|
|
/// method uses affine expression analysis (in turn using getTripCount) and is
|
|
|
|
/// able to determine constant trip count in non-trivial cases.
|
|
|
|
llvm::Optional<uint64_t> mlir::getConstantTripCount(const ForStmt &forStmt) {
|
2018-10-04 06:39:12 +08:00
|
|
|
auto tripCountExpr = getTripCountExpr(forStmt);
|
2018-09-13 01:21:23 +08:00
|
|
|
|
2018-10-04 06:39:12 +08:00
|
|
|
if (auto *constExpr =
|
|
|
|
dyn_cast_or_null<AffineConstantExpr>(tripCountExpr.expr))
|
2018-09-13 01:21:23 +08:00
|
|
|
return constExpr->getValue();
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the greatest known integral divisor of the trip count. Affine
|
|
|
|
/// expression analysis is used (indirectly through getTripCount), and
|
|
|
|
/// this method is thus able to determine non-trivial divisors.
|
|
|
|
uint64_t mlir::getLargestDivisorOfTripCount(const ForStmt &forStmt) {
|
2018-10-04 06:39:12 +08:00
|
|
|
auto tripCountExpr = getTripCountExpr(forStmt);
|
2018-09-13 01:21:23 +08:00
|
|
|
|
|
|
|
if (!tripCountExpr)
|
|
|
|
return 1;
|
|
|
|
|
2018-10-04 06:39:12 +08:00
|
|
|
if (auto *constExpr = dyn_cast<AffineConstantExpr>(tripCountExpr.expr)) {
|
2018-09-13 01:21:23 +08:00
|
|
|
uint64_t tripCount = constExpr->getValue();
|
|
|
|
|
|
|
|
// 0 iteration loops (greatest divisor is 2^64 - 1).
|
|
|
|
if (tripCount == 0)
|
|
|
|
return ULONG_MAX;
|
|
|
|
|
|
|
|
// The greatest divisor is the trip count.
|
|
|
|
return tripCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trip count is not a known constant; return its largest known divisor.
|
2018-10-04 06:39:12 +08:00
|
|
|
return tripCountExpr.expr->getLargestKnownDivisor();
|
2018-09-13 01:21:23 +08:00
|
|
|
}
|