Skip to content

Commit b2369cf

Browse files
authored
Merge pull request #28 from Xilinx/tina.updateToUnspecifiedIntOverflowSemantics
[FXML-2019] Update existing folds to unspecified int overflow
2 parents 0f83f1a + ee32062 commit b2369cf

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

mlir/lib/Dialect/Tosa/Transforms/TosaFoldConstantAdd.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ struct TosaFoldConstantAdd : public OpRewritePattern<AddOp> {
2626

2727
using OpRewritePattern::OpRewritePattern;
2828

29-
static APInt computeIntAdd(const APInt &first, const APInt &second) {
30-
return first.sadd_sat(second);
31-
}
32-
33-
static APFloat computeFloatAdd(const APFloat &first, const APFloat &second) {
34-
return first + second;
35-
}
36-
3729
LogicalResult matchAndRewrite(AddOp addOp,
3830
PatternRewriter &rewriter) const override {
3931
auto leftOp = addOp.getInput1();
@@ -76,13 +68,27 @@ struct TosaFoldConstantAdd : public OpRewritePattern<AddOp> {
7668
if (isa<IntegerType>(lhsElemType)) {
7769
assert(isa<IntegerType>(rhsElemType) &&
7870
isa<IntegerType>(resultType.getElementType()));
71+
bool addOverflowed = false;
72+
auto intAdd = [&addOverflowed](const APInt &first, const APInt &second) {
73+
bool didOverflow;
74+
auto res = first.sadd_ov(second, didOverflow);
75+
addOverflowed |= didOverflow;
76+
return res;
77+
};
7978
newTensor = applyElementWise<APInt, APInt>(lhsValues, rhsValues,
80-
resultType, &computeIntAdd);
79+
resultType, intAdd);
80+
if (addOverflowed) {
81+
addOp->emitWarning(
82+
"Addition did overflow. The results are unspecified.");
83+
}
8184
} else {
8285
assert(isa<FloatType>(lhsElemType) && isa<FloatType>(rhsElemType) &&
8386
isa<FloatType>(resultType.getElementType()));
84-
newTensor = applyElementWise<APFloat, APFloat>(
85-
lhsValues, rhsValues, resultType, &computeFloatAdd);
87+
auto floatAdd = [](const APFloat &first, const APFloat &second) {
88+
return first + second;
89+
};
90+
newTensor = applyElementWise<APFloat, APFloat>(lhsValues, rhsValues,
91+
resultType, floatAdd);
8692
}
8793
rewriter.replaceOpWithNewOp<ConstOp>(addOp, newTensor.getType(), newTensor);
8894

mlir/test/Dialect/Tosa/constant-add-opt.mlir

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ func.func @add_fold_int() -> tensor<4xi32> {
7575

7676
// CHECK-LABEL: @add_fold_int_overflow
7777
func.func @add_fold_int_overflow() -> tensor<4xi32> {
78-
// CHECK: [[RES:]] ={{.*}}tosa.const{{.*}}2147483647, 2147483647, -2147483648, -2147483648
78+
// Don't expect any specific results for the overflowing addition, just
79+
// expect that it is folded.
80+
// CHECK: [[RES:]] ={{.*}}tosa.const
7981
// CHECK-NOT: tosa.add
8082
// CHECK: return [[RES]]
8183
%0 = "tosa.const"() {value =
@@ -86,6 +88,7 @@ func.func @add_fold_int_overflow() -> tensor<4xi32> {
8688
dense<[1, 10, -1, -30]> :
8789
tensor<4xi32>
8890
} : () -> tensor<4xi32>
91+
// expected-warning@below {{Addition did overflow. The results are unspecified.}}
8992
%2 = "tosa.add"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32>
9093
return %2 : tensor<4xi32>
9194
}

0 commit comments

Comments
 (0)