diff --git a/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp b/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp index 6de151594e3e9..bd0c262f932e3 100644 --- a/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp +++ b/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp @@ -75,6 +75,20 @@ struct MulIOpInterface } }; +struct FloorDivSIOpInterface + : public ValueBoundsOpInterface::ExternalModel { + void populateBoundsForIndexValue(Operation *op, Value value, + ValueBoundsConstraintSet &cstr) const { + auto divSIOp = cast(op); + assert(value == divSIOp.getResult() && "invalid value"); + + AffineExpr lhs = cstr.getExpr(divSIOp.getLhs()); + AffineExpr rhs = cstr.getExpr(divSIOp.getRhs()); + cstr.bound(value) == lhs.floorDiv(rhs); + } +}; + struct SelectOpInterface : public ValueBoundsOpInterface::ExternalModel { @@ -157,6 +171,7 @@ void mlir::arith::registerValueBoundsOpInterfaceExternalModels( arith::ConstantOp::attachInterface(*ctx); arith::SubIOp::attachInterface(*ctx); arith::MulIOp::attachInterface(*ctx); + arith::FloorDivSIOp::attachInterface(*ctx); arith::SelectOp::attachInterface(*ctx); }); } diff --git a/mlir/test/Dialect/Arith/value-bounds-op-interface-impl.mlir b/mlir/test/Dialect/Arith/value-bounds-op-interface-impl.mlir index 8fb3ba1a1ecce..ec223c1b026bd 100644 --- a/mlir/test/Dialect/Arith/value-bounds-op-interface-impl.mlir +++ b/mlir/test/Dialect/Arith/value-bounds-op-interface-impl.mlir @@ -65,6 +65,30 @@ func.func @arith_muli_non_pure(%a: index, %b: index) -> index { // ----- +// CHECK: #[[$map:.*]] = affine_map<()[s0] -> (s0 floordiv 5)> +// CHECK-LABEL: func @arith_floordivsi( +// CHECK-SAME: %[[a:.*]]: index +// CHECK: %[[apply:.*]] = affine.apply #[[$map]]()[%[[a]]] +// CHECK: return %[[apply]] +func.func @arith_floordivsi(%a: index) -> index { + %0 = arith.constant 5 : index + %1 = arith.floordivsi %a, %0 : index + %2 = "test.reify_bound"(%1) : (index) -> (index) + return %2 : index +} + +// ----- + +func.func @arith_floordivsi_non_pure(%a: index, %b: index) -> index { + %0 = arith.floordivsi %a, %b : index + // Semi-affine expressions (such as "symbol * symbol") are not supported. + // expected-error @below{{could not reify bound}} + %1 = "test.reify_bound"(%0) : (index) -> (index) + return %1 : index +} + +// ----- + // CHECK-LABEL: func @arith_const() // CHECK: %[[c5:.*]] = arith.constant 5 : index // CHECK: %[[c5:.*]] = arith.constant 5 : index