@@ -26,14 +26,6 @@ struct TosaFoldConstantAdd : public OpRewritePattern<AddOp> {
26
26
27
27
using OpRewritePattern::OpRewritePattern;
28
28
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
-
37
29
LogicalResult matchAndRewrite (AddOp addOp,
38
30
PatternRewriter &rewriter) const override {
39
31
auto leftOp = addOp.getInput1 ();
@@ -76,13 +68,27 @@ struct TosaFoldConstantAdd : public OpRewritePattern<AddOp> {
76
68
if (isa<IntegerType>(lhsElemType)) {
77
69
assert (isa<IntegerType>(rhsElemType) &&
78
70
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
+ };
79
78
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
+ }
81
84
} else {
82
85
assert (isa<FloatType>(lhsElemType) && isa<FloatType>(rhsElemType) &&
83
86
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);
86
92
}
87
93
rewriter.replaceOpWithNewOp <ConstOp>(addOp, newTensor.getType (), newTensor);
88
94
0 commit comments