[Mlir-commits] [mlir] [mlir][arith] Check overflow when reassociating integer constants (PR #233155)
Matthias Springer
llvmlistbot at llvm.org
Sat Oct 17 18:41:42 PDT 2026
https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/233155
>From c77b3fd019a274d15bfce0a6d4504c02a4744a01 Mon Sep 17 00:00:00 2001
From: Matthias Springer <me at m-sp.org>
Date: Sun, 18 Oct 2026 01:27:38 +0000
Subject: [PATCH] [mlir][arith] Check overflow when reassociating integer
constants
Folding (x + 100 nsw) + 100 nsw to x + (-56) nsw in i8 introduces
poison for inputs such as x = -100 that were previously defined.
Check signed and unsigned overflow when folding constants in the
add/sub/mul reassociation patterns. Retain each common nsw/nuw flag
only when its constant arithmetic does not overflow. Keep index
behavior unchanged for a separate fix.
Add regression coverage for invalid and preserved overflow flags.
---
.../Dialect/Arith/IR/ArithCanonicalization.td | 35 ++-
mlir/lib/Dialect/Arith/IR/ArithOps.cpp | 38 ++-
mlir/test/Dialect/Arith/canonicalize.mlir | 269 ++++++++++++++++++
3 files changed, 331 insertions(+), 11 deletions(-)
diff --git a/mlir/lib/Dialect/Arith/IR/ArithCanonicalization.td b/mlir/lib/Dialect/Arith/IR/ArithCanonicalization.td
index 2615bcca23e35c..c22156832962c4 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithCanonicalization.td
+++ b/mlir/lib/Dialect/Arith/IR/ArithCanonicalization.td
@@ -42,9 +42,20 @@ def OrIntAttrs : NativeCodeCall<"orIntegerAttrs($_builder, $0, $1, $2)">;
// Bitwise-xor two integer attributes and create a new one with the result.
def XorIntAttrs : NativeCodeCall<"xorIntegerAttrs($_builder, $0, $1, $2)">;
-// Merge overflow flags from 2 ops, selecting the most conservative combination.
+// Intersect overflow flags. Callers must ensure the common flags remain valid
+// after rewriting.
def MergeOverflow : NativeCodeCall<"mergeOverflowFlags($0, $1)">;
+// For fixed-width integers, retain common flags only if the constant arithmetic
+// does not overflow in their respective signedness. Pass constants in folding
+// order.
+class MergeOverflowForReassociation<string op> : NativeCodeCall<
+ "mergeOverflowFlagsForReassociation($0, $1, $2, $3, " #
+ "&APInt::s" # op # "_ov, &APInt::u" # op # "_ov)">;
+def MergeOverflowForAdd : MergeOverflowForReassociation<"add">;
+def MergeOverflowForSub : MergeOverflowForReassociation<"sub">;
+def MergeOverflowForMul : MergeOverflowForReassociation<"mul">;
+
// Default overflow flag (all wraparounds allowed).
defvar DefOverflow = ConstantEnumCase<Arith_IntegerOverflowAttr, "none">;
@@ -69,13 +80,17 @@ def IsEqualityCmpIPredicate :
// addi is commutative and will be canonicalized to have its constants appear
// as the second operand.
+// Constant reassociation preserves the modular result, but combining the
+// constants may overflow even when the original operations do not. Check the
+// constant arithmetic before retaining common overflow flags.
+
// addi(addi(x, c0), c1) -> addi(x, c0 + c1)
def AddIAddConstant :
Pat<(Arith_AddIOp:$res
(Arith_AddIOp $x, (ConstantLikeMatcher APIntAttr:$c0), $ovf1),
(ConstantLikeMatcher APIntAttr:$c1), $ovf2),
(Arith_AddIOp $x, (Arith_ConstantOp (AddIntAttrs $res, $c0, $c1)),
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForAdd $ovf1, $ovf2, $c0, $c1))>;
// addi(subi(x, c0), c1) -> addi(x, c1 - c0)
def AddISubConstantRHS :
@@ -83,7 +98,7 @@ def AddISubConstantRHS :
(Arith_SubIOp $x, (ConstantLikeMatcher APIntAttr:$c0), $ovf1),
(ConstantLikeMatcher APIntAttr:$c1), $ovf2),
(Arith_AddIOp $x, (Arith_ConstantOp (SubIntAttrs $res, $c1, $c0)),
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForSub $ovf1, $ovf2, $c1, $c0))>;
// addi(subi(c0, x), c1) -> subi(c0 + c1, x)
def AddISubConstantLHS :
@@ -91,7 +106,7 @@ def AddISubConstantLHS :
(Arith_SubIOp (ConstantLikeMatcher APIntAttr:$c0), $x, $ovf1),
(ConstantLikeMatcher APIntAttr:$c1), $ovf2),
(Arith_SubIOp (Arith_ConstantOp (AddIntAttrs $res, $c0, $c1)), $x,
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForAdd $ovf1, $ovf2, $c0, $c1))>;
def IsScalarOrSplatNegativeOne :
Constraint<And<[
@@ -120,7 +135,7 @@ def MulIMulIConstant :
(Arith_MulIOp $x, (ConstantLikeMatcher APIntAttr:$c0), $ovf1),
(ConstantLikeMatcher APIntAttr:$c1), $ovf2),
(Arith_MulIOp $x, (Arith_ConstantOp (MulIntAttrs $res, $c0, $c1)),
- (MergeOverflow $ovf1, $ovf2)),
+ (MergeOverflowForMul $ovf1, $ovf2, $c0, $c1)),
[(IntAttrTypeMatchesResult $res, $c0),
(IntAttrTypeMatchesResult $res, $c1)]>;
@@ -164,7 +179,7 @@ def SubILHSAddConstant :
(ConstantLikeMatcher APIntAttr:$c1),
(Arith_AddIOp $x, (ConstantLikeMatcher APIntAttr:$c0), $ovf1), $ovf2),
(Arith_SubIOp (Arith_ConstantOp (SubIntAttrs $res, $c1, $c0)), $x,
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForSub $ovf1, $ovf2, $c1, $c0))>;
// subi(subi(x, c0), c1) -> subi(x, c0 + c1)
def SubIRHSSubConstantRHS :
@@ -172,7 +187,7 @@ def SubIRHSSubConstantRHS :
(Arith_SubIOp $x, (ConstantLikeMatcher APIntAttr:$c0), $ovf1),
(ConstantLikeMatcher APIntAttr:$c1), $ovf2),
(Arith_SubIOp $x, (Arith_ConstantOp (AddIntAttrs $res, $c0, $c1)),
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForAdd $ovf1, $ovf2, $c0, $c1))>;
// subi(subi(c0, x), c1) -> subi(c0 - c1, x)
def SubIRHSSubConstantLHS :
@@ -180,7 +195,7 @@ def SubIRHSSubConstantLHS :
(Arith_SubIOp (ConstantLikeMatcher APIntAttr:$c0), $x, $ovf1),
(ConstantLikeMatcher APIntAttr:$c1), $ovf2),
(Arith_SubIOp (Arith_ConstantOp (SubIntAttrs $res, $c0, $c1)), $x,
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForSub $ovf1, $ovf2, $c0, $c1))>;
// subi(c1, subi(x, c0)) -> subi(c0 + c1, x)
def SubILHSSubConstantRHS :
@@ -188,7 +203,7 @@ def SubILHSSubConstantRHS :
(ConstantLikeMatcher APIntAttr:$c1),
(Arith_SubIOp $x, (ConstantLikeMatcher APIntAttr:$c0), $ovf1), $ovf2),
(Arith_SubIOp (Arith_ConstantOp (AddIntAttrs $res, $c0, $c1)), $x,
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForAdd $ovf1, $ovf2, $c0, $c1))>;
// subi(c1, subi(c0, x)) -> addi(x, c1 - c0)
def SubILHSSubConstantLHS :
@@ -196,7 +211,7 @@ def SubILHSSubConstantLHS :
(ConstantLikeMatcher APIntAttr:$c1),
(Arith_SubIOp (ConstantLikeMatcher APIntAttr:$c0), $x, $ovf1), $ovf2),
(Arith_AddIOp $x, (Arith_ConstantOp (SubIntAttrs $res, $c1, $c0)),
- (MergeOverflow $ovf1, $ovf2))>;
+ (MergeOverflowForSub $ovf1, $ovf2, $c1, $c0))>;
// subi(subi(a, b), a) -> subi(0, b)
def SubISubILHSRHSLHS :
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 40fcfa5c458819..ce8851cdea9974 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -83,7 +83,8 @@ static IntegerAttr xorIntegerAttrs(PatternRewriter &builder, Value res,
return applyToIntegerAttrs(builder, res, lhs, rhs, std::bit_xor<APInt>());
}
-// Merge overflow flags from 2 ops, selecting the most conservative combination.
+// Intersect overflow flags. Callers must ensure the common flags remain valid
+// after rewriting.
static IntegerOverflowFlagsAttr
mergeOverflowFlags(IntegerOverflowFlagsAttr val1,
IntegerOverflowFlagsAttr val2) {
@@ -91,6 +92,41 @@ mergeOverflowFlags(IntegerOverflowFlagsAttr val1,
val1.getValue() & val2.getValue());
}
+using OverflowCheckedBinaryOp = APInt (APInt::*)(const APInt &, bool &) const;
+
+// For fixed-width integers, if combining the constants does not overflow, the
+// reassociated expression has the same mathematical result as the original
+// expression. Common source flags then rule out overflow in the new operation.
+static IntegerOverflowFlagsAttr mergeOverflowFlagsForReassociation(
+ IntegerOverflowFlagsAttr val1, IntegerOverflowFlagsAttr val2, Attribute lhs,
+ Attribute rhs, OverflowCheckedBinaryOp signedOp,
+ OverflowCheckedBinaryOp unsignedOp) {
+ IntegerOverflowFlags flags = val1.getValue() & val2.getValue();
+ if (flags == IntegerOverflowFlags::none)
+ return IntegerOverflowFlagsAttr::get(val1.getContext(), flags);
+
+ auto lhsAttr = llvm::cast<IntegerAttr>(lhs);
+ auto rhsAttr = llvm::cast<IntegerAttr>(rhs);
+ // TODO: Handle index separately; its storage width is not its target width.
+ if (isa<IndexType>(lhsAttr.getType()))
+ return IntegerOverflowFlagsAttr::get(val1.getContext(), flags);
+
+ const APInt &lhsValue = lhsAttr.getValue();
+ const APInt &rhsValue = rhsAttr.getValue();
+ bool overflow;
+ if (bitEnumContainsAny(flags, IntegerOverflowFlags::nsw)) {
+ (void)(lhsValue.*signedOp)(rhsValue, overflow);
+ if (overflow)
+ flags = bitEnumClear(flags, IntegerOverflowFlags::nsw);
+ }
+ if (bitEnumContainsAny(flags, IntegerOverflowFlags::nuw)) {
+ (void)(lhsValue.*unsignedOp)(rhsValue, overflow);
+ if (overflow)
+ flags = bitEnumClear(flags, IntegerOverflowFlags::nuw);
+ }
+ return IntegerOverflowFlagsAttr::get(val1.getContext(), flags);
+}
+
/// Invert an integer comparison predicate.
arith::CmpIPredicate arith::invertPredicate(arith::CmpIPredicate pred) {
switch (pred) {
diff --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index 0da65a4a0f4028..5a376e29176a08 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -2252,6 +2252,275 @@ func.func @tripleMulLargeInt(%arg0: i256) -> i256 {
return %3 : i256
}
+// At x = -100, the source computes 0, then 100, without signed overflow. The
+// folded constant is -56, so retaining nsw would make x + (-56) poison.
+// CHECK-LABEL: func.func @reassoc_add_add_nsw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.addi %[[X]], %[[C]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_add_add_nsw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %a = arith.addi %x, %c100 overflow<nsw> : i8
+ %r = arith.addi %a, %c100 overflow<nsw> : i8
+ return %r : i8
+}
+
+// At x = 100, the source returns 1. The folded constant is unsigned 157,
+// so retaining nuw would make the new addition poison.
+// CHECK-LABEL: func.func @reassoc_add_sub_rhs_nuw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -99 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.addi %[[X]], %[[C]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_add_sub_rhs_nuw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %c1 = arith.constant 1 : i8
+ %a = arith.subi %x, %c100 overflow<nuw> : i8
+ %r = arith.addi %a, %c1 overflow<nuw> : i8
+ return %r : i8
+}
+
+// At x = 50, the source returns -6 and satisfies both flags. The folded
+// constant is 44, so retaining nuw would make 44 - x poison. Signed constant
+// addition does not overflow, so nsw is retained independently.
+// CHECK-LABEL: func.func @reassoc_add_sub_lhs_flags
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant 44 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[C]], %[[X]] overflow<nsw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_add_sub_lhs_flags(%x: i8) -> i8 {
+ %cm56 = arith.constant -56 : i8
+ %c100 = arith.constant 100 : i8
+ %a = arith.subi %cm56, %x overflow<nsw, nuw> : i8
+ %r = arith.addi %a, %c100 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// At x = -1, the source returns -128. The folded constant is -128, so
+// retaining nsw would make the new multiplication poison.
+// CHECK-LABEL: func.func @reassoc_mul_mul_nsw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -128 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.muli %[[X]], %[[C]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_mul_mul_nsw(%x: i8) -> i8 {
+ %c2 = arith.constant 2 : i8
+ %c64 = arith.constant 64 : i8
+ %a = arith.muli %x, %c2 overflow<nsw> : i8
+ %r = arith.muli %a, %c64 overflow<nsw> : i8
+ return %r : i8
+}
+
+// At x = 100, the source returns 100. The folded constant is -56, so
+// retaining nsw would make -56 - x poison.
+// CHECK-LABEL: func.func @reassoc_sub_add_lhs_nsw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[C]], %[[X]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_add_lhs_nsw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %cm100 = arith.constant -100 : i8
+ %a = arith.addi %x, %cm100 overflow<nsw> : i8
+ %r = arith.subi %c100, %a overflow<nsw> : i8
+ return %r : i8
+}
+
+// At x = 100, the source returns -100. The folded constant is -56, so
+// retaining nsw would make x - (-56) poison.
+// CHECK-LABEL: func.func @reassoc_sub_sub_rhs_rhs_nsw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[X]], %[[C]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_sub_rhs_rhs_nsw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %a = arith.subi %x, %c100 overflow<nsw> : i8
+ %r = arith.subi %a, %c100 overflow<nsw> : i8
+ return %r : i8
+}
+
+// At x = 100, the source returns 100. The folded constant is -56, so
+// retaining nsw would make -56 - x poison.
+// CHECK-LABEL: func.func @reassoc_sub_sub_rhs_lhs_nsw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[C]], %[[X]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_sub_rhs_lhs_nsw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %cm100 = arith.constant -100 : i8
+ %a = arith.subi %c100, %x overflow<nsw> : i8
+ %r = arith.subi %a, %cm100 overflow<nsw> : i8
+ return %r : i8
+}
+
+// At x = 100, the source returns 100. The folded constant is -56, so
+// retaining nsw would make -56 - x poison.
+// CHECK-LABEL: func.func @reassoc_sub_sub_lhs_rhs_nsw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[C]], %[[X]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_sub_lhs_rhs_nsw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %a = arith.subi %x, %c100 overflow<nsw> : i8
+ %r = arith.subi %c100, %a overflow<nsw> : i8
+ return %r : i8
+}
+
+// At x = 100, the source returns 1. The folded constant is unsigned 157,
+// so retaining nuw would make the new addition poison.
+// CHECK-LABEL: func.func @reassoc_sub_sub_lhs_lhs_nuw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -99 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.addi %[[X]], %[[C]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_sub_lhs_lhs_nuw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %c1 = arith.constant 1 : i8
+ %a = arith.subi %c100, %x overflow<nuw> : i8
+ %r = arith.subi %c1, %a overflow<nuw> : i8
+ return %r : i8
+}
+
+// Both common flags survive when the constant sum fits in both interpretations.
+// CHECK-LABEL: func.func @reassoc_add_constants_keep_both
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant 59 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.addi %[[X]], %[[C]] overflow<nsw, nuw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_add_constants_keep_both(%x: i8) -> i8 {
+ %c17 = arith.constant 17 : i8
+ %c42 = arith.constant 42 : i8
+ %a = arith.addi %x, %c17 overflow<nsw, nuw> : i8
+ %r = arith.addi %a, %c42 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// 100 + 100 overflows signed arithmetic but fits unsigned arithmetic. At
+// x = 100, the source returns 100 with both flags satisfied; the replacement
+// must drop nsw while preserving nuw.
+// CHECK-LABEL: func.func @reassoc_add_constants_keep_nuw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[C]], %[[X]] overflow<nuw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_add_constants_keep_nuw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %a = arith.subi %c100, %x overflow<nsw, nuw> : i8
+ %r = arith.addi %a, %c100 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// -100 + -100 overflows in both interpretations. At x = -100, the source
+// returns -100 with both flags satisfied, but the replacement's 56 - x
+// overflows in both interpretations. Both flags must be dropped.
+// CHECK-LABEL: func.func @reassoc_add_constants_drop_both
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant 56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[C]], %[[X]] : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_add_constants_drop_both(%x: i8) -> i8 {
+ %cm100 = arith.constant -100 : i8
+ %a = arith.subi %cm100, %x overflow<nsw, nuw> : i8
+ %r = arith.addi %a, %cm100 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// The constant subtraction is c1 - c0, not c0 - c1. 1 - 100 fits signed
+// arithmetic but underflows unsigned arithmetic. At x = 100, the source and
+// the replacement with nsw return 1; retaining nuw would introduce poison.
+// CHECK-LABEL: func.func @reassoc_sub_constants_keep_nsw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -99 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.addi %[[X]], %[[C]] overflow<nsw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_constants_keep_nsw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %c1 = arith.constant 1 : i8
+ %a = arith.subi %x, %c100 overflow<nsw, nuw> : i8
+ %r = arith.addi %a, %c1 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// -100 - 100 overflows signed arithmetic, while unsigned 156 - 100 fits.
+// At x = 100, the source returns -100 with both flags satisfied; the new
+// addition must drop nsw while preserving nuw.
+// CHECK-LABEL: func.func @reassoc_sub_constants_keep_nuw
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant 56 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.addi %[[X]], %[[C]] overflow<nuw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_constants_keep_nuw(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %cm100 = arith.constant -100 : i8
+ %a = arith.subi %x, %c100 overflow<nsw, nuw> : i8
+ %r = arith.addi %a, %cm100 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// This pattern combines c0 - c1, the opposite order from the previous two
+// patterns. 100 - 40 fits in both interpretations, so both flags survive.
+// CHECK-LABEL: func.func @reassoc_sub_constants_forward_order
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant 60 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.subi %[[C]], %[[X]] overflow<nsw, nuw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_sub_constants_forward_order(%x: i8) -> i8 {
+ %c100 = arith.constant 100 : i8
+ %c40 = arith.constant 40 : i8
+ %a = arith.subi %c100, %x overflow<nsw, nuw> : i8
+ %r = arith.subi %a, %c40 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// A product that fits in both interpretations preserves both common flags.
+// CHECK-LABEL: func.func @reassoc_mul_constants_keep_both
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant 21 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.muli %[[X]], %[[C]] overflow<nsw, nuw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_mul_constants_keep_both(%x: i8) -> i8 {
+ %c3 = arith.constant 3 : i8
+ %c7 = arith.constant 7 : i8
+ %a = arith.muli %x, %c3 overflow<nsw, nuw> : i8
+ %r = arith.muli %a, %c7 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// The negative signed boundary is representable: -64 * 2 is -128, unlike
+// the overflowing positive 2 * 64 in the original multiplication regression.
+// The unsigned constant product 192 * 2 overflows, so only nuw is removed.
+// CHECK-LABEL: func.func @reassoc_mul_constants_signed_min
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant -128 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.muli %[[X]], %[[C]] overflow<nsw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_mul_constants_signed_min(%x: i8) -> i8 {
+ %cm64 = arith.constant -64 : i8
+ %c2 = arith.constant 2 : i8
+ %a = arith.muli %x, %cm64 overflow<nsw, nuw> : i8
+ %r = arith.muli %a, %c2 overflow<nsw, nuw> : i8
+ return %r : i8
+}
+
+// Fitting constants only justify flags shared by both source operations.
+// Preserve nsw without incorrectly copying the first multiplication's nuw.
+// CHECK-LABEL: func.func @reassoc_mul_constants_intersect_flags
+// CHECK-SAME: (%[[X:.*]]: i8)
+// CHECK-NEXT: %[[C:.*]] = arith.constant 6 : i8
+// CHECK-NEXT: %[[R:.*]] = arith.muli %[[X]], %[[C]] overflow<nsw> : i8
+// CHECK-NEXT: return %[[R]] : i8
+func.func @reassoc_mul_constants_intersect_flags(%x: i8) -> i8 {
+ %c2 = arith.constant 2 : i8
+ %c3 = arith.constant 3 : i8
+ %a = arith.muli %x, %c2 overflow<nsw, nuw> : i8
+ %r = arith.muli %a, %c3 overflow<nsw> : i8
+ return %r : i8
+}
+
// CHECK-LABEL: @addiMuliToSubiRhsI32
// CHECK-SAME: (%[[ARG0:.+]]: i32, %[[ARG1:.+]]: i32)
// CHECK: %[[SUB:.+]] = arith.subi %[[ARG0]], %[[ARG1]] : i32
More information about the Mlir-commits
mailing list