MLIR  18.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 
14 using namespace mlir;
15 
16 namespace mlir {
17 namespace arith {
18 namespace {
19 
20 struct AddIOpInterface
21  : public ValueBoundsOpInterface::ExternalModel<AddIOpInterface, AddIOp> {
22  void populateBoundsForIndexValue(Operation *op, Value value,
23  ValueBoundsConstraintSet &cstr) const {
24  auto addIOp = cast<AddIOp>(op);
25  assert(value == addIOp.getResult() && "invalid value");
26 
27  cstr.bound(value) ==
28  cstr.getExpr(addIOp.getLhs()) + cstr.getExpr(addIOp.getRhs());
29  }
30 };
31 
32 struct ConstantOpInterface
33  : public ValueBoundsOpInterface::ExternalModel<ConstantOpInterface,
34  ConstantOp> {
35  void populateBoundsForIndexValue(Operation *op, Value value,
36  ValueBoundsConstraintSet &cstr) const {
37  auto constantOp = cast<ConstantOp>(op);
38  assert(value == constantOp.getResult() && "invalid value");
39 
40  if (auto attr = llvm::dyn_cast<IntegerAttr>(constantOp.getValue()))
41  cstr.bound(value) == attr.getInt();
42  }
43 };
44 
45 struct SubIOpInterface
46  : public ValueBoundsOpInterface::ExternalModel<SubIOpInterface, SubIOp> {
47  void populateBoundsForIndexValue(Operation *op, Value value,
48  ValueBoundsConstraintSet &cstr) const {
49  auto subIOp = cast<SubIOp>(op);
50  assert(value == subIOp.getResult() && "invalid value");
51 
52  cstr.bound(value) ==
53  cstr.getExpr(subIOp.getLhs()) - cstr.getExpr(subIOp.getRhs());
54  }
55 };
56 
57 struct MulIOpInterface
58  : public ValueBoundsOpInterface::ExternalModel<MulIOpInterface, MulIOp> {
59  void populateBoundsForIndexValue(Operation *op, Value value,
60  ValueBoundsConstraintSet &cstr) const {
61  auto mulIOp = cast<MulIOp>(op);
62  assert(value == mulIOp.getResult() && "invalid value");
63 
64  cstr.bound(value) ==
65  cstr.getExpr(mulIOp.getLhs()) * cstr.getExpr(mulIOp.getRhs());
66  }
67 };
68 
69 } // namespace
70 } // namespace arith
71 } // namespace mlir
72 
74  DialectRegistry &registry) {
75  registry.addExtension(+[](MLIRContext *ctx, arith::ArithDialect *dialect) {
76  arith::AddIOp::attachInterface<arith::AddIOpInterface>(*ctx);
77  arith::ConstantOp::attachInterface<arith::ConstantOpInterface>(*ctx);
78  arith::SubIOp::attachInterface<arith::SubIOpInterface>(*ctx);
79  arith::MulIOp::attachInterface<arith::MulIOpInterface>(*ctx);
80  });
81 }
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtension(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:60
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
A helper class to be used with ValueBoundsOpInterface.
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.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
void registerValueBoundsOpInterfaceExternalModels(DialectRegistry &registry)
Include the generated interface declarations.