Skip to content

[FXML-2019] Update existing folds to unspecified int overflow #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions mlir/lib/Dialect/Tosa/Transforms/TosaFoldConstantAdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ struct TosaFoldConstantAdd : public OpRewritePattern<AddOp> {

using OpRewritePattern::OpRewritePattern;

static APInt computeIntAdd(const APInt &first, const APInt &second) {
return first.sadd_sat(second);
}

static APFloat computeFloatAdd(const APFloat &first, const APFloat &second) {
return first + second;
}

LogicalResult matchAndRewrite(AddOp addOp,
PatternRewriter &rewriter) const override {
auto leftOp = addOp.getInput1();
Expand Down Expand Up @@ -76,13 +68,27 @@ struct TosaFoldConstantAdd : public OpRewritePattern<AddOp> {
if (isa<IntegerType>(lhsElemType)) {
assert(isa<IntegerType>(rhsElemType) &&
isa<IntegerType>(resultType.getElementType()));
bool addOverflowed = false;
auto intAdd = [&addOverflowed](const APInt &first, const APInt &second) {
bool didOverflow;
auto res = first.sadd_ov(second, didOverflow);
addOverflowed |= didOverflow;
return res;
};
newTensor = applyElementWise<APInt, APInt>(lhsValues, rhsValues,
resultType, &computeIntAdd);
resultType, intAdd);
if (addOverflowed) {
addOp->emitWarning(
"Addition did overflow. The results are unspecified.");
}
} else {
assert(isa<FloatType>(lhsElemType) && isa<FloatType>(rhsElemType) &&
isa<FloatType>(resultType.getElementType()));
newTensor = applyElementWise<APFloat, APFloat>(
lhsValues, rhsValues, resultType, &computeFloatAdd);
auto floatAdd = [](const APFloat &first, const APFloat &second) {
return first + second;
};
newTensor = applyElementWise<APFloat, APFloat>(lhsValues, rhsValues,
resultType, floatAdd);
}
rewriter.replaceOpWithNewOp<ConstOp>(addOp, newTensor.getType(), newTensor);

Expand Down
5 changes: 4 additions & 1 deletion mlir/test/Dialect/Tosa/constant-add-opt.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func.func @add_fold_int() -> tensor<4xi32> {

// CHECK-LABEL: @add_fold_int_overflow
func.func @add_fold_int_overflow() -> tensor<4xi32> {
// CHECK: [[RES:]] ={{.*}}tosa.const{{.*}}2147483647, 2147483647, -2147483648, -2147483648
// Don't expect any specific results for the overflowing addition, just
// expect that it is folded.
// CHECK: [[RES:]] ={{.*}}tosa.const
// CHECK-NOT: tosa.add
// CHECK: return [[RES]]
%0 = "tosa.const"() {value =
Expand All @@ -86,6 +88,7 @@ func.func @add_fold_int_overflow() -> tensor<4xi32> {
dense<[1, 10, -1, -30]> :
tensor<4xi32>
} : () -> tensor<4xi32>
// expected-warning@below {{Addition did overflow. The results are unspecified.}}
%2 = "tosa.add"(%0, %1) : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi32>
return %2 : tensor<4xi32>
}
Expand Down