fix(compiler): Batching: Do not attempt to extract static bounds for dynamic loops

The batching pass erroneously assumes that any expression solely
composed of an induction variable has static bounds. This commit adds
a test for the lower bound, upper bound and step checking that they
are indeed static before attempting to determine their static values.
This commit is contained in:
Andi Drebes
2023-04-05 15:58:11 +02:00
committed by Antoniu Pop
parent 3a679a6f0a
commit 5a6ed84076

View File

@@ -88,11 +88,16 @@ static mlir::OpFoldResult getValueAsOpFoldResult(mlir::Value v) {
return v;
}
// Checks whether `v` is a constant value of type index
static bool isConstantIndexValue(mlir::Value v) {
return v.getDefiningOp() &&
llvm::isa<mlir::arith::ConstantIndexOp>(*v.getDefiningOp());
}
/// Assumes that `v` is a constant index operation and returns the
/// constant value as an `int64_t`.
static int64_t getConstantIndexValue(mlir::Value v) {
assert(v.getDefiningOp() &&
llvm::isa<mlir::arith::ConstantIndexOp>(*v.getDefiningOp()));
assert(isConstantIndexValue(v));
return llvm::dyn_cast<mlir::arith::ConstantIndexOp>(*v.getDefiningOp())
.value();
@@ -223,9 +228,12 @@ struct BoundsAndStep {
/// operation.
static std::optional<BoundsAndStep>
getBoundsOfQuasiAffineIVExpression(mlir::Value expr, mlir::scf::ForOp forOp) {
// Base case: expression is the induction variable itself -> return
// loop bounds
if (expr == forOp.getInductionVar()) {
// Base case: expression is the induction variable itself -> check
// if the bounds are static and return them
if (expr == forOp.getInductionVar() &&
isConstantIndexValue(forOp.getLowerBound()) &&
isConstantIndexValue(forOp.getUpperBound()) &&
isConstantIndexValue(forOp.getStep())) {
return BoundsAndStep{getConstantIndexValue(forOp.getLowerBound()),
getConstantIndexValue(forOp.getUpperBound()),
getConstantIndexValue(forOp.getStep())};
@@ -273,7 +281,7 @@ getBoundsOfQuasiAffineIVExpression(mlir::Value expr, mlir::scf::ForOp forOp) {
}
}
llvm_unreachable("Expression could not be evaluated statically");
return std::nullopt;
}
/// Checks whether the expression `expr` is a quasi-affine expression