CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
167 lines
6.8 KiB
C++
167 lines
6.8 KiB
C++
//===- ReifyValueBounds.cpp --- Reify value bounds with arith ops -------*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include <utility>
|
|
|
|
#include "mlir/Dialect/Arith/Transforms/Transforms.h"
|
|
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
|
#include "mlir/Interfaces/ValueBoundsOpInterface.h"
|
|
|
|
using namespace mlir;
|
|
using namespace mlir::arith;
|
|
|
|
[[maybe_unused]] static bool isIndexLikeType(Type type,
|
|
ValueBoundsOptions options) {
|
|
return type.isIndex() || (options.allowIntegerType && type.isInteger());
|
|
}
|
|
|
|
static Value castToIndexValue(OpBuilder &b, Location loc, Value value) {
|
|
if (value.getType().isIndex())
|
|
return value;
|
|
assert(value.getType().isSignlessInteger() &&
|
|
"expected index or signless integer type");
|
|
return IndexCastOp::create(b, loc, b.getIndexType(), value);
|
|
}
|
|
|
|
/// Build Arith IR for the given affine map and its operands.
|
|
static Value buildArithValue(OpBuilder &b, Location loc, AffineMap map,
|
|
ValueRange operands) {
|
|
assert(map.getNumResults() == 1 && "multiple results not supported yet");
|
|
std::function<Value(AffineExpr)> buildExpr = [&](AffineExpr e) -> Value {
|
|
switch (e.getKind()) {
|
|
case AffineExprKind::Constant:
|
|
return ConstantIndexOp::create(b, loc,
|
|
cast<AffineConstantExpr>(e).getValue());
|
|
case AffineExprKind::DimId:
|
|
return castToIndexValue(b, loc,
|
|
operands[cast<AffineDimExpr>(e).getPosition()]);
|
|
case AffineExprKind::SymbolId:
|
|
return castToIndexValue(
|
|
b, loc,
|
|
operands[cast<AffineSymbolExpr>(e).getPosition() + map.getNumDims()]);
|
|
case AffineExprKind::Add: {
|
|
auto binaryExpr = cast<AffineBinaryOpExpr>(e);
|
|
return AddIOp::create(b, loc, buildExpr(binaryExpr.getLHS()),
|
|
buildExpr(binaryExpr.getRHS()));
|
|
}
|
|
case AffineExprKind::Mul: {
|
|
auto binaryExpr = cast<AffineBinaryOpExpr>(e);
|
|
return MulIOp::create(b, loc, buildExpr(binaryExpr.getLHS()),
|
|
buildExpr(binaryExpr.getRHS()));
|
|
}
|
|
case AffineExprKind::FloorDiv: {
|
|
auto binaryExpr = cast<AffineBinaryOpExpr>(e);
|
|
return DivSIOp::create(b, loc, buildExpr(binaryExpr.getLHS()),
|
|
buildExpr(binaryExpr.getRHS()));
|
|
}
|
|
case AffineExprKind::CeilDiv: {
|
|
auto binaryExpr = cast<AffineBinaryOpExpr>(e);
|
|
return CeilDivSIOp::create(b, loc, buildExpr(binaryExpr.getLHS()),
|
|
buildExpr(binaryExpr.getRHS()));
|
|
}
|
|
case AffineExprKind::Mod: {
|
|
auto binaryExpr = cast<AffineBinaryOpExpr>(e);
|
|
return RemSIOp::create(b, loc, buildExpr(binaryExpr.getLHS()),
|
|
buildExpr(binaryExpr.getRHS()));
|
|
}
|
|
}
|
|
llvm_unreachable("unsupported AffineExpr kind");
|
|
};
|
|
return buildExpr(map.getResult(0));
|
|
}
|
|
|
|
FailureOr<OpFoldResult> mlir::arith::reifyValueBound(
|
|
OpBuilder &b, Location loc, presburger::BoundType type,
|
|
const ValueBoundsConstraintSet::Variable &var,
|
|
ValueBoundsConstraintSet::StopConditionFn stopCondition,
|
|
ValueBoundsOptions options) {
|
|
// Compute bound.
|
|
AffineMap boundMap;
|
|
ValueDimList mapOperands;
|
|
if (failed(ValueBoundsConstraintSet::computeBound(
|
|
boundMap, mapOperands, type, var, std::move(stopCondition), options)))
|
|
return failure();
|
|
|
|
// Materialize tensor.dim/memref.dim ops.
|
|
SmallVector<Value> operands;
|
|
for (auto valueDim : mapOperands) {
|
|
Value value = valueDim.first;
|
|
std::optional<int64_t> dim = valueDim.second;
|
|
|
|
if (!dim.has_value()) {
|
|
// This is an index-typed/integer-typed value.
|
|
assert(isIndexLikeType(value.getType(), options) &&
|
|
"expected index or integer type");
|
|
operands.push_back(value);
|
|
continue;
|
|
}
|
|
|
|
assert(cast<ShapedType>(value.getType()).isDynamicDim(*dim) &&
|
|
"expected dynamic dim");
|
|
if (isa<RankedTensorType>(value.getType())) {
|
|
// A tensor dimension is used: generate a tensor.dim.
|
|
operands.push_back(tensor::DimOp::create(b, loc, value, *dim));
|
|
} else if (isa<MemRefType>(value.getType())) {
|
|
// A memref dimension is used: generate a memref.dim.
|
|
operands.push_back(memref::DimOp::create(b, loc, value, *dim));
|
|
} else {
|
|
llvm_unreachable("cannot generate DimOp for unsupported shaped type");
|
|
}
|
|
}
|
|
|
|
// Check for special cases where no arith ops are needed.
|
|
if (boundMap.isSingleConstant()) {
|
|
// Bound is a constant: return an IntegerAttr.
|
|
return static_cast<OpFoldResult>(
|
|
b.getIndexAttr(boundMap.getSingleConstantResult()));
|
|
}
|
|
// No arith ops are needed if the bound is a single SSA value.
|
|
if (auto expr = dyn_cast<AffineDimExpr>(boundMap.getResult(0)))
|
|
return static_cast<OpFoldResult>(
|
|
castToIndexValue(b, loc, operands[expr.getPosition()]));
|
|
if (auto expr = dyn_cast<AffineSymbolExpr>(boundMap.getResult(0)))
|
|
return static_cast<OpFoldResult>(castToIndexValue(
|
|
b, loc, operands[expr.getPosition() + boundMap.getNumDims()]));
|
|
// General case: build Arith ops.
|
|
return static_cast<OpFoldResult>(buildArithValue(b, loc, boundMap, operands));
|
|
}
|
|
|
|
FailureOr<OpFoldResult> mlir::arith::reifyShapedValueDimBound(
|
|
OpBuilder &b, Location loc, presburger::BoundType type, Value value,
|
|
int64_t dim, const ValueBoundsConstraintSet::StopConditionFn &stopCondition,
|
|
ValueBoundsOptions options) {
|
|
auto reifyToOperands = [&](Value v, std::optional<int64_t> d,
|
|
ValueBoundsConstraintSet &cstr) {
|
|
// We are trying to reify a bound for `value` in terms of the owning op's
|
|
// operands. Construct a stop condition that evaluates to "true" for any SSA
|
|
// value expect for `value`. I.e., the bound will be computed in terms of
|
|
// any SSA values expect for `value`. The first such values are operands of
|
|
// the owner of `value`.
|
|
return v != value;
|
|
};
|
|
return reifyValueBound(b, loc, type, {value, dim},
|
|
stopCondition ? stopCondition : reifyToOperands,
|
|
options);
|
|
}
|
|
|
|
FailureOr<OpFoldResult> mlir::arith::reifyIndexValueBound(
|
|
OpBuilder &b, Location loc, presburger::BoundType type, Value value,
|
|
const ValueBoundsConstraintSet::StopConditionFn &stopCondition,
|
|
ValueBoundsOptions options) {
|
|
auto reifyToOperands = [&](Value v, std::optional<int64_t> d,
|
|
ValueBoundsConstraintSet &cstr) {
|
|
return v != value;
|
|
};
|
|
return reifyValueBound(b, loc, type, value,
|
|
stopCondition ? stopCondition : reifyToOperands,
|
|
options);
|
|
}
|