MLIR 24.0.0git
ValueBoundsOpInterfaceImpl.cpp
Go to the documentation of this file.
1//===- ValueBoundsOpInterfaceImpl.cpp - Impl. of ValueBoundsOpInterface ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
13
14using namespace mlir;
15
16namespace mlir {
17namespace linalg {
18namespace {
19
20struct IndexOpInterface
21 : public ValueBoundsOpInterface::ExternalModel<IndexOpInterface, IndexOp> {
22 void populateBoundsForIndexValue(Operation *op, Value value,
23 ValueBoundsConstraintSet &cstr) const {
24 auto indexOp = cast<IndexOp>(op);
25 auto linalgOp = indexOp->getParentOfType<LinalgOp>();
26 assert(value == indexOp.getResult() && "invalid value");
27
28 // index >= 0
29 cstr.bound(value) >= 0;
30
31 // index < dim size
32 int64_t flatDimPos =
33 cast<AffineDimExpr>(
34 linalgOp.getShapesToLoopsMap().getResult(indexOp.getDim()))
35 .getPosition();
36 // Find the `flatDimPos`-th operand dimension.
37 int64_t flatDimCtr = 0;
38 for (OpOperand &operand : linalgOp->getOpOperands()) {
39 assert(flatDimPos >= flatDimCtr && "invalid pos");
40 // Scalar operands do not contribute dimensions to the flattened shape.
41 int64_t rank = linalgOp.getRank(&operand);
42 if (flatDimPos < flatDimCtr + rank) {
43 cstr.bound(value) <
44 cstr.getExpr(operand.get(), flatDimPos - flatDimCtr);
45 break;
46 }
47 flatDimCtr += rank;
48 }
49 }
50};
51
52} // namespace
53} // namespace linalg
54} // namespace mlir
55
57 DialectRegistry &registry) {
58 registry.addExtension(+[](MLIRContext *ctx, linalg::LinalgDialect *dialect) {
59 IndexOp::attachInterface<IndexOpInterface>(*ctx);
60 // Note: ValueBoundsOpInterface implementation is not required for ops that
61 // implement `DestinationStyleOpInterface` (for querying shaped OpResults).
62 });
63}
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
AffineExpr getExpr(Value value, std::optional< int64_t > dim=std::nullopt)
Return an expression that represents the given index-typed value or shaped value dimension.
BoundBuilder bound(Value value)
Add a bound for the given index-typed value or shaped value.
void registerValueBoundsOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.