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
9#include "mlir/Dialect/Bufferization/IR/ValueBoundsOpInterfaceImpl.h"
10
11#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
12#include "mlir/Interfaces/ValueBoundsOpInterface.h"
13
14using namespace mlir;
15
16namespace mlir {
17namespace bufferization {
18namespace {
19
20/// Model for a materialization op that keeps the shape of its source, i.e.,
21/// `to_tensor` and `to_buffer`.
22template <typename OpTy>
23struct MaterializationOpInterface
24 : public ValueBoundsOpInterface::ExternalModel<
25 MaterializationOpInterface<OpTy>, OpTy> {
26 void populateBoundsForShapedValueDim(Operation *op, Value value, int64_t dim,
27 ValueBoundsConstraintSet &cstr) const {
28 auto materializationOp = cast<OpTy>(op);
29 assert(value == materializationOp.getResult() && "invalid value");
30
31 // The op also accepts tensor-like and buffer-like types that are not
32 // shaped, for which no bound can be computed.
33 Value source = materializationOp.getOperand();
34 if (isa<ShapedType>(Val: value.getType()) && isa<ShapedType>(Val: source.getType()))
35 cstr.bound(value)[dim] == cstr.getExpr(value: source, dim);
36 }
37};
38
39} // namespace
40} // namespace bufferization
41} // namespace mlir
42
43void mlir::bufferization::registerValueBoundsOpInterfaceExternalModels(
44 DialectRegistry &registry) {
45 registry.addExtension(extensionFn: +[](MLIRContext *ctx,
46 bufferization::BufferizationDialect *dialect) {
47 bufferization::ToBufferOp::attachInterface<
48 bufferization::MaterializationOpInterface<bufferization::ToBufferOp>>(
49 context&: *ctx);
50 bufferization::ToTensorOp::attachInterface<
51 bufferization::MaterializationOpInterface<bufferization::ToTensorOp>>(
52 context&: *ctx);
53 });
54}
55