Skip to content

Commit 80b45cc

Browse files
committed
[MLIR][Arith] Add ValueBoundsOpInterface for DivSI
1 parent 9eefa92 commit 80b45cc

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ struct MulIOpInterface
7575
}
7676
};
7777

78+
struct DivSIOpInterface
79+
: public ValueBoundsOpInterface::ExternalModel<DivSIOpInterface, DivSIOp> {
80+
void populateBoundsForIndexValue(Operation *op, Value value,
81+
ValueBoundsConstraintSet &cstr) const {
82+
auto divSIOp = cast<DivSIOp>(op);
83+
assert(value == divSIOp.getResult() && "invalid value");
84+
85+
AffineExpr lhs = cstr.getExpr(divSIOp.getLhs());
86+
AffineExpr rhs = cstr.getExpr(divSIOp.getRhs());
87+
cstr.bound(value) == lhs.floorDiv(rhs);
88+
}
89+
};
90+
7891
struct SelectOpInterface
7992
: public ValueBoundsOpInterface::ExternalModel<SelectOpInterface,
8093
SelectOp> {
@@ -157,6 +170,7 @@ void mlir::arith::registerValueBoundsOpInterfaceExternalModels(
157170
arith::ConstantOp::attachInterface<arith::ConstantOpInterface>(*ctx);
158171
arith::SubIOp::attachInterface<arith::SubIOpInterface>(*ctx);
159172
arith::MulIOp::attachInterface<arith::MulIOpInterface>(*ctx);
173+
arith::DivSIOp::attachInterface<arith::DivSIOpInterface>(*ctx);
160174
arith::SelectOp::attachInterface<arith::SelectOpInterface>(*ctx);
161175
});
162176
}

mlir/test/Dialect/Arith/value-bounds-op-interface-impl.mlir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ func.func @arith_muli_non_pure(%a: index, %b: index) -> index {
6565

6666
// -----
6767

68+
// CHECK: #[[$map:.*]] = affine_map<()[s0] -> (s0 floordiv 5)>
69+
// CHECK-LABEL: func @arith_divsi(
70+
// CHECK-SAME: %[[a:.*]]: index
71+
// CHECK: %[[apply:.*]] = affine.apply #[[$map]]()[%[[a]]]
72+
// CHECK: return %[[apply]]
73+
func.func @arith_divsi(%a: index) -> index {
74+
%0 = arith.constant 5 : index
75+
%1 = arith.divsi %a, %0 : index
76+
%2 = "test.reify_bound"(%1) : (index) -> (index)
77+
return %2 : index
78+
}
79+
80+
// -----
81+
82+
func.func @arith_divsi_non_pure(%a: index, %b: index) -> index {
83+
%0 = arith.divsi %a, %b : index
84+
// Semi-affine expressions (such as "symbol * symbol") are not supported.
85+
// expected-error @below{{could not reify bound}}
86+
%1 = "test.reify_bound"(%0) : (index) -> (index)
87+
return %1 : index
88+
}
89+
90+
// -----
91+
6892
// CHECK-LABEL: func @arith_const()
6993
// CHECK: %[[c5:.*]] = arith.constant 5 : index
7094
// CHECK: %[[c5:.*]] = arith.constant 5 : index

0 commit comments

Comments
 (0)