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 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 static_assert(
29 OpTy::template hasTrait<OpTrait::SameOperandsAndResultShape>(),
30 "expected the result and the source to have the same shape");
31 auto materializationOp = cast<OpTy>(op);
32 assert(value == materializationOp.getResult() && "invalid value");
33
34 // The op also accepts tensor-like and buffer-like types that are not
35 // shaped, for which no bound can be computed.
36 Value source = materializationOp.getOperand();
37 if (isa<ShapedType>(value.getType()) && isa<ShapedType>(source.getType()))
38 cstr.bound(value)[dim] == cstr.getExpr(source, dim);
39 }
40};
41
42} // namespace
43} // namespace bufferization
44} // namespace mlir
45
47 DialectRegistry &registry) {
48 registry.addExtension(+[](MLIRContext *ctx,
49 bufferization::BufferizationDialect *dialect) {
50 bufferization::ToBufferOp::attachInterface<
51 bufferization::MaterializationOpInterface<bufferization::ToBufferOp>>(
52 *ctx);
53 bufferization::ToTensorOp::attachInterface<
54 bufferization::MaterializationOpInterface<bufferization::ToTensorOp>>(
55 *ctx);
56 });
57}
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:63
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.
Type getType() const
Return the type of this value.
Definition Value.h:105
void registerValueBoundsOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.