[clang] [CIR] Store nsw/nuw as an OverflowFlags bit enum and split out saturated add/sub (PR #195618)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 13:50:20 PST 2026


https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/195618

>From b1cc685e16d242bac62dd0b25ef206cd504f23ce Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Tue, 27 Oct 2026 12:52:51 +0000
Subject: [PATCH] [CIR] Store nsw/nuw as an OverflowFlags bit enum and split
 out saturated add/sub

cir.add, cir.sub and cir.mul carried nsw and nuw as two separate unit
properties, and lowering mapped them to LLVM with an early return that
picked nuw over nsw, so an op with both flags lowered to `overflow<nuw>`
and lost nsw. cir.add and cir.sub also carried a `sat` flag that changed
the operation's result rather than adding a poison condition.

Store the flags as one OverflowFlags bit-enum property, mapped flag by
flag onto mlir::LLVM::IntegerOverflowFlags in lowering so both survive.
The property uses the default EnumProp syntax with a space separator,
printed as `nuw`, `nsw` or `nuw nsw` as in LLVM IR, so no custom parser
or printer is needed. cir.mul now shares the add/sub base class and so
becomes Pure.

Saturating arithmetic moves to new cir.add.sat and cir.sub.sat ops, which
lower to the signed or unsigned saturation intrinsics by element type.
cir::OverflowBehavior and its operators go away; createAdd, createSub and
createMul take cir::OverflowFlags.

Saturating fixed-point add/sub on padded unsigned types now saturate as
signed values and clamp subtraction at 0 while signed, as
llvm::FixedPointBuilder does. Before, CIR picked the unsigned intrinsics
and its `< 0` clamp could never fire.

Assisted-by: Claude Code (Claude Opus 5.5).
---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h      |  79 ++++---------
 .../include/clang/CIR/Dialect/IR/CIRAttrs.td  |  16 +++
 clang/include/clang/CIR/Dialect/IR/CIROps.td  | 106 ++++++++++--------
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp       |  12 +-
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp          |   8 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp    |  12 +-
 .../lib/CIR/CodeGen/CIRGenFixedPointBuilder.h |  70 +++++++-----
 clang/lib/CIR/CodeGen/CIRGenFunction.cpp      |   4 +-
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |  22 ----
 .../TargetLowering/LowerItaniumCXXABI.cpp     |  34 +++---
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp |  73 ++++++------
 clang/test/CIR/CodeGen/fixed-point-arith.cpp  |  52 ++++++++-
 .../CodeGenBuiltins/builtins-elementwise.c    |  24 ++--
 clang/test/CIR/IR/invalid-overflow-flags.cir  |  17 +++
 clang/test/CIR/IR/overflow-flags.cir          |  62 ++++++++++
 clang/test/CIR/Lowering/binop-signed-int.cir  |   4 +-
 .../test/CIR/Lowering/binop-unsigned-int.cir  |   4 +-
 clang/test/CIR/Lowering/overflow-flags.cir    |  47 ++++++++
 18 files changed, 393 insertions(+), 253 deletions(-)
 create mode 100644 clang/test/CIR/IR/invalid-overflow-flags.cir
 create mode 100644 clang/test/CIR/IR/overflow-flags.cir
 create mode 100644 clang/test/CIR/Lowering/overflow-flags.cir

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 489ed57695d299..d558f7dbfcd867 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -16,7 +16,6 @@
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
 #include "clang/CIR/Dialect/IR/CIRTypes.h"
 #include "clang/CIR/MissingFeatures.h"
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/IR/FPEnv.h"
 #include "llvm/Support/ErrorHandling.h"
 
@@ -29,39 +28,6 @@
 
 namespace cir {
 
-enum class OverflowBehavior {
-  None = 0,
-  NoSignedWrap = 1 << 0,
-  NoUnsignedWrap = 1 << 1,
-  Saturated = 1 << 2,
-};
-
-constexpr OverflowBehavior operator|(OverflowBehavior a, OverflowBehavior b) {
-  return static_cast<OverflowBehavior>(llvm::to_underlying(a) |
-                                       llvm::to_underlying(b));
-}
-
-constexpr OverflowBehavior operator&(OverflowBehavior a, OverflowBehavior b) {
-  return static_cast<OverflowBehavior>(llvm::to_underlying(a) &
-                                       llvm::to_underlying(b));
-}
-
-constexpr OverflowBehavior &operator|=(OverflowBehavior &a,
-                                       OverflowBehavior b) {
-  a = a | b;
-  return a;
-}
-
-constexpr OverflowBehavior &operator&=(OverflowBehavior &a,
-                                       OverflowBehavior b) {
-  a = a & b;
-  return a;
-}
-
-constexpr bool testFlag(OverflowBehavior ob, OverflowBehavior flag) {
-  return (ob & flag) != OverflowBehavior::None;
-}
-
 class CIRBaseBuilderTy : public mlir::OpBuilder {
 
 public:
@@ -854,57 +820,56 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
   }
 
   mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
-                        OverflowBehavior ob = OverflowBehavior::None) {
-    auto op = cir::MulOp::create(*this, loc, lhs, rhs);
-    op.setNoUnsignedWrap(testFlag(ob, OverflowBehavior::NoUnsignedWrap));
-    op.setNoSignedWrap(testFlag(ob, OverflowBehavior::NoSignedWrap));
-    return op;
+                        OverflowFlags flags = OverflowFlags::None) {
+    return cir::MulOp::create(*this, loc, lhs, rhs, flags);
   }
   mlir::Value createNSWMul(mlir::Location loc, mlir::Value lhs,
                            mlir::Value rhs) {
-    return createMul(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
+    return createMul(loc, lhs, rhs, OverflowFlags::NoSignedWrap);
   }
   mlir::Value createNUWAMul(mlir::Location loc, mlir::Value lhs,
                             mlir::Value rhs) {
-    return createMul(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
+    return createMul(loc, lhs, rhs, OverflowFlags::NoUnsignedWrap);
+  }
+
+  mlir::Value createAddSat(mlir::Location loc, mlir::Value lhs,
+                           mlir::Value rhs) {
+    return cir::AddSatOp::create(*this, loc, lhs, rhs);
+  }
+
+  mlir::Value createSubSat(mlir::Location loc, mlir::Value lhs,
+                           mlir::Value rhs) {
+    return cir::SubSatOp::create(*this, loc, lhs, rhs);
   }
 
   mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
-                        OverflowBehavior ob = OverflowBehavior::None) {
-    auto op = cir::SubOp::create(*this, loc, lhs, rhs);
-    op.setNoUnsignedWrap(testFlag(ob, OverflowBehavior::NoUnsignedWrap));
-    op.setNoSignedWrap(testFlag(ob, OverflowBehavior::NoSignedWrap));
-    op.setSaturated(testFlag(ob, OverflowBehavior::Saturated));
-    return op;
+                        OverflowFlags flags = OverflowFlags::None) {
+    return cir::SubOp::create(*this, loc, lhs, rhs, flags);
   }
 
   mlir::Value createNSWSub(mlir::Location loc, mlir::Value lhs,
                            mlir::Value rhs) {
-    return createSub(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
+    return createSub(loc, lhs, rhs, OverflowFlags::NoSignedWrap);
   }
 
   mlir::Value createNUWSub(mlir::Location loc, mlir::Value lhs,
                            mlir::Value rhs) {
-    return createSub(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
+    return createSub(loc, lhs, rhs, OverflowFlags::NoUnsignedWrap);
   }
 
   mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
-                        OverflowBehavior ob = OverflowBehavior::None) {
-    auto op = cir::AddOp::create(*this, loc, lhs, rhs);
-    op.setNoUnsignedWrap(testFlag(ob, OverflowBehavior::NoUnsignedWrap));
-    op.setNoSignedWrap(testFlag(ob, OverflowBehavior::NoSignedWrap));
-    op.setSaturated(testFlag(ob, OverflowBehavior::Saturated));
-    return op;
+                        OverflowFlags flags = OverflowFlags::None) {
+    return cir::AddOp::create(*this, loc, lhs, rhs, flags);
   }
 
   mlir::Value createNSWAdd(mlir::Location loc, mlir::Value lhs,
                            mlir::Value rhs) {
-    return createAdd(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
+    return createAdd(loc, lhs, rhs, OverflowFlags::NoSignedWrap);
   }
 
   mlir::Value createNUWAdd(mlir::Location loc, mlir::Value lhs,
                            mlir::Value rhs) {
-    return createAdd(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
+    return createAdd(loc, lhs, rhs, OverflowFlags::NoUnsignedWrap);
   }
 
   mlir::Value createDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index 4be903d337675f..dbc1269a37ced0 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -1135,6 +1135,22 @@ def CIR_FastMathFlags : CIR_I32BitEnum<
 
 def CIR_FastMathFlagsAttr : CIR_EnumAttr<CIR_FastMathFlags, "fastmath">;
 
+//===----------------------------------------------------------------------===//
+// OverflowFlags
+//===----------------------------------------------------------------------===//
+
+// `nuw` is listed first so the flags print in LLVM IR order, `nuw nsw`.
+def CIR_OFNone : I32BitEnumCaseNone<"None", "none">;
+def CIR_OFNoSignedWrap : I32BitEnumCaseBit<"NoSignedWrap", 0, "nsw">;
+def CIR_OFNoUnsignedWrap : I32BitEnumCaseBit<"NoUnsignedWrap", 1, "nuw">;
+
+def CIR_OverflowFlags : CIR_I32BitEnum<
+    "OverflowFlags", "integer overflow flags", [
+  CIR_OFNone, CIR_OFNoUnsignedWrap, CIR_OFNoSignedWrap
+]> {
+  let separator = " ";
+}
+
 //===----------------------------------------------------------------------===//
 // GlobalViewAttr
 //===----------------------------------------------------------------------===//
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 4ffde906be3937..1332ce92b441da 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2831,27 +2831,20 @@ class CIR_BinaryOp<string mnemonic, Type type, list<Trait> traits = [],
   }];
 }
 
-// Base class for binary ops that support integer overflow flags (nsw/nuw)
-// and saturated arithmetic.
 class CIR_BinaryOpWithOverflowFlags<string mnemonic, Type type,
                                     list<Trait> traits = []>
-    : CIR_BinaryOp<mnemonic, type, !listconcat([Pure], traits)>
+    : CIR_BinaryOp<mnemonic, type, traits>
 {
-  let arguments = (ins
-    type:$lhs, type:$rhs,
-    UnitProp:$no_signed_wrap,
-    UnitProp:$no_unsigned_wrap,
-    UnitProp:$saturated
+  let append traits = [Pure];
+
+  let append arguments = (ins
+    DefaultValuedProp<EnumProp<CIR_OverflowFlags>,
+                      "cir::OverflowFlags::None">:$overflow_flags
   );
 
   let assemblyFormat = [{
-    (`nsw` $no_signed_wrap^)?
-    (`nuw` $no_unsigned_wrap^)?
-    (`sat` $saturated^)?
-    $lhs `,` $rhs `:` type($lhs) attr-dict
+    ($overflow_flags^)? $lhs `,` $rhs `:` type($lhs) attr-dict
   }];
-
-  let hasVerifier = 1;
 }
 
 //===----------------------------------------------------------------------===//
@@ -2866,21 +2859,19 @@ def CIR_AddOp
     operands and the result must have the same integer, vector-of-integer or
     matrix-of-integer type.
 
-    The optional `nsw` (no signed wrap) and `nuw` (no unsigned wrap) unit
-    attributes indicate that the result is poison if signed or unsigned
-    overflow occurs, respectively. The optional `sat` (saturated) attribute
-    clamps the result to the type's representable range instead of wrapping.
-    The `nsw`/`nuw` flags and `sat` are mutually exclusive.
+    The optional `nuw` (no unsigned wrap) and `nsw` (no signed wrap) flags
+    indicate that the result is poison if unsigned or signed overflow occurs,
+    respectively; both may be given as `nuw nsw`. Saturating addition is
+    `cir.add.sat`.
 
     Example:
 
     ```
     %0 = cir.add %a, %b : !s32i
     %1 = cir.add nsw %a, %b : !s32i
-    %2 = cir.add nuw %a, %b : !u32i
-    %3 = cir.add sat %a, %b : !s32i
-    %4 = cir.add %va, %vb : !cir.vector<4 x !s32i>
-    %5 = cir.add %ma, %mb : !cir.matrix<3 x 3 x !s32i>
+    %2 = cir.add nuw nsw %a, %b : !s32i
+    %3 = cir.add %va, %vb : !cir.vector<4 x !s32i>
+    %4 = cir.add %ma, %mb : !cir.matrix<3 x 3 x !s32i>
     ```
   }];
 }
@@ -2897,18 +2888,17 @@ def CIR_SubOp
     operands and the result must have the same integer, vector-of-integer or
     matrix-of-integer type.
 
-    The optional `nsw` (no signed wrap) and `nuw` (no unsigned wrap) unit
-    attributes indicate that the result is poison if signed or unsigned
-    overflow occurs, respectively. The optional `sat` (saturated) attribute
-    clamps the result to the type's representable range. The `nsw`/`nuw`
-    flags and `sat` are mutually exclusive.
+    The optional `nuw` (no unsigned wrap) and `nsw` (no signed wrap) flags
+    indicate that the result is poison if unsigned or signed overflow occurs,
+    respectively; both may be given as `nuw nsw`. Saturating subtraction is
+    `cir.sub.sat`.
 
     Example:
 
     ```
     %0 = cir.sub %a, %b : !s32i
     %1 = cir.sub nsw %a, %b : !s32i
-    %2 = cir.sub sat %a, %b : !s32i
+    %2 = cir.sub nuw nsw %a, %b : !s32i
     %3 = cir.sub %va, %vb : !cir.vector<4 x !s32i>
     %4 = cir.sub %ma, %mb : !cir.matrix<3 x 3 x !s32i>
     ```
@@ -2919,39 +2909,67 @@ def CIR_SubOp
 // MulOp
 //===----------------------------------------------------------------------===//
 
-def CIR_MulOp : CIR_BinaryOp<"mul", CIR_AnyIntOrVecOrMatrixOfIntType> {
+def CIR_MulOp
+    : CIR_BinaryOpWithOverflowFlags<"mul", CIR_AnyIntOrVecOrMatrixOfIntType> {
   let summary = "Integer multiplication";
   let description = [{
     The `cir.mul` operation performs multiplication on integer operands. Both
     operands and the result must have the same integer or vector-of-integer
     or matrix-of-integer type.
 
-    The optional `nsw` (no signed wrap) and `nuw` (no unsigned wrap) unit
-    attributes indicate that the result is poison if signed or unsigned
-    overflow occurs, respectively.
+    The optional `nuw` (no unsigned wrap) and `nsw` (no signed wrap) flags
+    indicate that the result is poison if unsigned or signed overflow occurs,
+    respectively; both may be given as `nuw nsw`.
 
     Example:
 
     ```
     %0 = cir.mul %a, %b : !s32i
     %1 = cir.mul nsw %a, %b : !s32i
-    %2 = cir.mul nuw %a, %b : !u32i
+    %2 = cir.mul nuw nsw %a, %b : !u32i
     %3 = cir.mul %va, %vb : !cir.vector<4 x !s32i>
     %4 = cir.mul %ma, %mb : !cir.matrix<3 x 3 x !s32i>
     ```
   }];
+}
 
-  let arguments = (ins
-    CIR_AnyIntOrVecOrMatrixOfIntType:$lhs,
-    CIR_AnyIntOrVecOrMatrixOfIntType:$rhs,
-    UnitProp:$no_signed_wrap,
-    UnitProp:$no_unsigned_wrap
-  );
+//===----------------------------------------------------------------------===//
+// AddSatOp / SubSatOp
+//===----------------------------------------------------------------------===//
 
-  let assemblyFormat = [{
-    (`nsw` $no_signed_wrap^)?
-    (`nuw` $no_unsigned_wrap^)?
-    $lhs `,` $rhs `:` type($lhs) attr-dict
+def CIR_AddSatOp
+    : CIR_BinaryOp<"add.sat", CIR_AnyIntOrVecOrMatrixOfIntType, [Pure]> {
+  let summary = "Saturating integer addition";
+  let description = [{
+    The `cir.add.sat` operation adds its integer operands and clamps the
+    result to the range of the element type instead of wrapping. The clamp is
+    signed or unsigned according to the element type; vectors and matrices
+    saturate elementwise.
+
+    Example:
+
+    ```
+    %0 = cir.add.sat %a, %b : !s32i
+    %1 = cir.add.sat %va, %vb : !cir.vector<4 x !u8i>
+    ```
+  }];
+}
+
+def CIR_SubSatOp
+    : CIR_BinaryOp<"sub.sat", CIR_AnyIntOrVecOrMatrixOfIntType, [Pure]> {
+  let summary = "Saturating integer subtraction";
+  let description = [{
+    The `cir.sub.sat` operation subtracts its integer operands and clamps the
+    result to the range of the element type instead of wrapping. The clamp is
+    signed or unsigned according to the element type; vectors and matrices
+    saturate elementwise.
+
+    Example:
+
+    ```
+    %0 = cir.sub.sat %a, %b : !u32i
+    %1 = cir.sub.sat %va, %vb : !cir.vector<4 x !s16i>
+    ```
   }];
 }
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 55941a8480cf1d..93a69df7fc4016 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -2096,11 +2096,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
     return errorBuiltinNYI(*this, e, builtinID);
   case Builtin::BI__builtin_elementwise_add_sat:
   case Builtin::BI__builtin_elementwise_sub_sat: {
-    // cir.add/cir.sub do not model i1 arithmetic, so a bool-element
-    // saturating add/sub is not representable through the saturated op.
-    // Bail before emitScalarExpr: an ext-vector-of-bool operand would
-    // otherwise hit the NYI bool-vector load, which returns a null value
-    // and would crash op0.getType().
+    // The saturating ops have no !cir.bool form, and emitScalarExpr on a bool
+    // vector hits the NYI bool-vector load and returns null, so bail first.
     QualType argTy = e->getArg(0)->getType();
     if (argTy->isBooleanType() || argTy->isExtVectorBoolType()) {
       cgm.errorNYI(e->getSourceRange(),
@@ -2114,9 +2111,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
            "elementwise saturating add/sub requires integer operands");
     mlir::Value val =
         builtinIDIfNoAsmLabel == Builtin::BI__builtin_elementwise_add_sat
-            ? builder.createAdd(loc, op0, op1, cir::OverflowBehavior::Saturated)
-            : builder.createSub(loc, op0, op1,
-                                cir::OverflowBehavior::Saturated);
+            ? builder.createAddSat(loc, op0, op1)
+            : builder.createSubSat(loc, op0, op1);
     return RValue::get(val);
   }
   case Builtin::BI__builtin_elementwise_max: {
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 0675268bea0477..9877178d51ec60 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -1568,11 +1568,11 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
     // GEP indexes are signed, and scaling an index isn't permitted to
     // signed-overflow, so we use the same semantics for our explicit
     // multiply.  We suppress this if overflow is not undefined behavior.
-    OverflowBehavior overflowBehavior = getLangOpts().PointerOverflowDefined
-                                            ? OverflowBehavior::None
-                                            : OverflowBehavior::NoSignedWrap;
+    cir::OverflowFlags overflowFlags = getLangOpts().PointerOverflowDefined
+                                           ? cir::OverflowFlags::None
+                                           : cir::OverflowFlags::NoSignedWrap;
     idx = builder.createMul(cgm.getLoc(e->getExprLoc()), idx, numElements,
-                            overflowBehavior);
+                            overflowFlags);
 
     addr = emitArraySubscriptPtr(*this, cgm.getLoc(e->getBeginLoc()),
                                  cgm.getLoc(e->getEndLoc()), addr, e->getType(),
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index ed15c81b878458..665f17469eec94 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -2067,12 +2067,10 @@ static mlir::Value emitPointerArithmetic(CIRGenFunction &cgf,
     // GEP indexes are signed, and scaling an index isn't permitted to
     // signed-overflow, so we use the same semantics for our explicit
     // multiply.  We suppress this if overflow is not undefined behavior.
-    cir::OverflowBehavior overflowBehavior =
-        cgf.getLangOpts().PointerOverflowDefined
-            ? cir::OverflowBehavior::None
-            : cir::OverflowBehavior::NoSignedWrap;
-    index =
-        cgf.getBuilder().createMul(loc, index, numElements, overflowBehavior);
+    cir::OverflowFlags overflowFlags = cgf.getLangOpts().PointerOverflowDefined
+                                           ? cir::OverflowFlags::None
+                                           : cir::OverflowFlags::NoSignedWrap;
+    index = cgf.getBuilder().createMul(loc, index, numElements, overflowFlags);
     assert(!cir::MissingFeatures::sanitizers());
     return cir::PtrStrideOp::create(cgf.getBuilder(), loc, pointer.getType(),
                                     pointer, index);
@@ -3257,7 +3255,7 @@ mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
               builder.getConstAPInt(numElts.getLoc(), numElts.getType(),
                                     cgf.cgm.getSize(eltSize).getValue());
           return builder.createMul(loc, eltSizeValue, numElts,
-                                   cir::OverflowBehavior::NoUnsignedWrap);
+                                   cir::OverflowFlags::NoUnsignedWrap);
         }
 
         return numElts;
diff --git a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
index 3ea67b830198a0..97cf78157b8267 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
@@ -83,12 +83,8 @@ struct CIRGenFixedPointBuilder {
 
     // When saturating unsigned-with-padding using signed operations, we may
     // get negative values. Emit an extra clamp to zero.
-    if (dstSema.isSaturated() && dstSema.hasUnsignedPadding()) {
-      mlir::Value zero = builder.getNullValue(result.getType(), loc);
-      mlir::Value isNeg =
-          builder.createCompare(loc, cir::CmpOpKind::lt, result, zero);
-      result = builder.createSelect(loc, isNeg, zero, result);
-    }
+    if (dstSema.isSaturated() && dstSema.hasUnsignedPadding())
+      result = clampNegativeToZero(result);
 
     return result;
   }
@@ -134,13 +130,11 @@ struct CIRGenFixedPointBuilder {
     mlir::Value wideLhs = createFixedToFixed(lhs, lhsSema, commonSema);
     mlir::Value wideRhs = createFixedToFixed(rhs, rhsSema, commonSema);
 
-    mlir::Value result;
-    if (commonSema.isSaturated()) {
-      result = builder.createAdd(loc, wideLhs, wideRhs,
-                                 cir::OverflowBehavior::Saturated);
-    } else {
-      result = builder.createAdd(loc, wideLhs, wideRhs);
-    }
+    mlir::Value result =
+        commonSema.isSaturated()
+            ? createSaturatingAddSub(wideLhs, wideRhs, commonSema,
+                                     /*isSub=*/false)
+            : builder.createAdd(loc, wideLhs, wideRhs);
 
     return createFixedToFixed(result, commonSema,
                               lhsSema.getCommonSemantics(rhsSema));
@@ -155,22 +149,11 @@ struct CIRGenFixedPointBuilder {
     mlir::Value wideLhs = createFixedToFixed(lhs, lhsSema, commonSema);
     mlir::Value wideRhs = createFixedToFixed(rhs, rhsSema, commonSema);
 
-    mlir::Value result;
-    if (commonSema.isSaturated()) {
-      result = builder.createSub(loc, wideLhs, wideRhs,
-                                 cir::OverflowBehavior::Saturated);
-    } else {
-      result = builder.createSub(loc, wideLhs, wideRhs);
-    }
-
-    // Subtraction can end up below 0 for padded unsigned operations, so emit
-    // an extra clamp in that case.
-    if (commonSema.isSaturated() && commonSema.hasUnsignedPadding()) {
-      mlir::Value zero = builder.getNullValue(result.getType(), loc);
-      mlir::Value ltZero =
-          builder.createCompare(loc, cir::CmpOpKind::lt, result, zero);
-      result = builder.createSelect(loc, ltZero, zero, result);
-    }
+    mlir::Value result =
+        commonSema.isSaturated()
+            ? createSaturatingAddSub(wideLhs, wideRhs, commonSema,
+                                     /*isSub=*/true)
+            : builder.createSub(loc, wideLhs, wideRhs);
 
     return createFixedToFixed(result, commonSema,
                               lhsSema.getCommonSemantics(rhsSema));
@@ -392,6 +375,35 @@ struct CIRGenFixedPointBuilder {
         c.getScale(), c.isSigned(), c.isSaturated(), bothPadded);
   }
 
+  /// Padded unsigned semantics compute as signed, as llvm::FixedPointBuilder
+  /// does: the padding bit keeps values at or below the signed maximum. CIR
+  /// integers carry signedness, so operands are cast to this type and back.
+  cir::IntType getOpIntType(const llvm::FixedPointSemantics &sema) {
+    return cir::IntType::get(builder.getContext(), sema.getWidth(),
+                             sema.isSigned() || sema.hasUnsignedPadding());
+  }
+
+  /// A padded unsigned result computed as signed may have gone negative.
+  mlir::Value clampNegativeToZero(mlir::Value value) {
+    mlir::Value zero = builder.getNullValue(value.getType(), loc);
+    mlir::Value isNeg =
+        builder.createCompare(loc, cir::CmpOpKind::lt, value, zero);
+    return builder.createSelect(loc, isNeg, zero, value);
+  }
+
+  mlir::Value createSaturatingAddSub(mlir::Value lhs, mlir::Value rhs,
+                                     const llvm::FixedPointSemantics &sema,
+                                     bool isSub) {
+    cir::IntType opTy = getOpIntType(sema);
+    mlir::Value opLhs = builder.createIntCast(lhs, opTy);
+    mlir::Value opRhs = builder.createIntCast(rhs, opTy);
+    mlir::Value result = isSub ? builder.createSubSat(loc, opLhs, opRhs)
+                               : builder.createAddSat(loc, opLhs, opRhs);
+    if (isSub && sema.hasUnsignedPadding())
+      result = clampNegativeToZero(result);
+    return builder.createIntCast(result, lhs.getType());
+  }
+
   CIRGenBuilderTy &builder;
   mlir::Location loc;
 };
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 4fa4585b08d1ef..310be35ed82de2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -1619,7 +1619,7 @@ CIRGenFunction::emitArrayLength(const clang::ArrayType *origArrayType,
   if (numVLAElements)
     numElements =
         builder.createMul(numVLAElements.getLoc(), numVLAElements, numElements,
-                          cir::OverflowBehavior::NoUnsignedWrap);
+                          cir::OverflowFlags::NoUnsignedWrap);
 
   return numElements;
 }
@@ -1675,7 +1675,7 @@ CIRGenFunction::getVLASize(const VariableArrayType *type) {
 
       numElements =
           builder.createMul(numElements.getLoc(), numElements, vlaSize,
-                            cir::OverflowBehavior::NoUnsignedWrap);
+                            cir::OverflowFlags::NoUnsignedWrap);
     }
   } while ((type = getContext().getAsVariableArrayType(elementType)));
 
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index f38161648b3fcd..84cc8554380644 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -3429,28 +3429,6 @@ mlir::LogicalResult cir::FuncOp::verify() {
   return success();
 }
 
-//===----------------------------------------------------------------------===//
-// AddOp / SubOp
-//===----------------------------------------------------------------------===//
-
-// The integer-only type constraint on these ops makes the nsw/nuw/sat flag
-// type checks unnecessary. Only the mutual-exclusivity between nsw/nuw and
-// sat needs to be verified.
-
-LogicalResult cir::AddOp::verify() {
-  if (getSaturated() && (getNoSignedWrap() || getNoUnsignedWrap()))
-    return emitOpError()
-           << "the nsw/nuw flags and the saturated flag are mutually exclusive";
-  return mlir::success();
-}
-
-LogicalResult cir::SubOp::verify() {
-  if (getSaturated() && (getNoSignedWrap() || getNoUnsignedWrap()))
-    return emitOpError()
-           << "the nsw/nuw flags and the saturated flag are mutually exclusive";
-  return mlir::success();
-}
-
 //===----------------------------------------------------------------------===//
 // TernaryOp
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
index f5179f38cfc1e4..5cdf5bb75e5910 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
@@ -478,15 +478,12 @@ static mlir::Value lowerDataMemberCast(mlir::Operation *op,
 
   cir::ConstantOp offsetValue = getConstantInt(offset);
   mlir::Value adjustedPtr;
-  if (isDerivedToBase) {
-    auto subOp = cir::SubOp::create(builder, loc, ty, loweredSrc, offsetValue);
-    subOp.setNoSignedWrap(true);
-    adjustedPtr = subOp;
-  } else {
-    auto addOp = cir::AddOp::create(builder, loc, ty, loweredSrc, offsetValue);
-    addOp.setNoSignedWrap(true);
-    adjustedPtr = addOp;
-  }
+  if (isDerivedToBase)
+    adjustedPtr = cir::SubOp::create(builder, loc, loweredSrc, offsetValue,
+                                     cir::OverflowFlags::NoSignedWrap);
+  else
+    adjustedPtr = cir::AddOp::create(builder, loc, loweredSrc, offsetValue,
+                                     cir::OverflowFlags::NoSignedWrap);
 
   return cir::SelectOp::create(builder, loc, ty, isNull, loweredSrc,
                                adjustedPtr);
@@ -528,17 +525,14 @@ static mlir::Value lowerMethodCast(mlir::Operation *op, mlir::Value loweredSrc,
   auto offsetValue = cir::ConstantOp::create(
       builder, op->getLoc(), cir::IntAttr::get(ptrdiffCIRTy, offset));
   mlir::Value adjustedAdjField;
-  if (isDerivedToBase) {
-    auto subOp = cir::SubOp::create(builder, op->getLoc(), ptrdiffCIRTy,
-                                    adjField, offsetValue);
-    subOp.setNoSignedWrap(true);
-    adjustedAdjField = subOp;
-  } else {
-    auto addOp = cir::AddOp::create(builder, op->getLoc(), ptrdiffCIRTy,
-                                    adjField, offsetValue);
-    addOp.setNoSignedWrap(true);
-    adjustedAdjField = addOp;
-  }
+  if (isDerivedToBase)
+    adjustedAdjField =
+        cir::SubOp::create(builder, op->getLoc(), adjField, offsetValue,
+                           cir::OverflowFlags::NoSignedWrap);
+  else
+    adjustedAdjField =
+        cir::AddOp::create(builder, op->getLoc(), adjField, offsetValue,
+                           cir::OverflowFlags::NoSignedWrap);
 
   return cir::InsertMemberOp::create(builder, op->getLoc(), loweredSrc, 1,
                                      adjustedAdjField);
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index a48dbd04de84ac..ad77744616c87e 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -3783,64 +3783,45 @@ static bool isIntTypeUnsigned(mlir::Type type) {
 // Binary Op Lowering
 //===----------------------------------------------------------------------===//
 
-template <typename BinOp>
-static mlir::LLVM::IntegerOverflowFlags intOverflowFlag(BinOp op) {
-  if (op.getNoUnsignedWrap())
-    return mlir::LLVM::IntegerOverflowFlags::nuw;
-  if (op.getNoSignedWrap())
-    return mlir::LLVM::IntegerOverflowFlags::nsw;
-  return mlir::LLVM::IntegerOverflowFlags::none;
-}
-
-/// Lower an arithmetic op that supports saturation, overflow flags, and an FP
-/// Lower an integer Add/Sub op that may use saturating-arithmetic semantics.
-template <typename UIntSatOp, typename SIntSatOp, typename IntOp,
-          typename CIROp>
-static mlir::LogicalResult
-lowerSaturatableArithOp(CIROp op, mlir::Value lhs, mlir::Value rhs,
-                        mlir::ConversionPatternRewriter &rewriter) {
-  const mlir::Type eltType = elementTypeIfVectorOrMatrix(op.getRhs().getType());
-  assert(cir::isIntOrBoolType(eltType) &&
-         "saturatable arith op expects integer operand types");
-  if (op.getSaturated()) {
-    if (isIntTypeUnsigned(eltType))
-      rewriter.replaceOpWithNewOp<UIntSatOp>(op, lhs, rhs);
-    else
-      rewriter.replaceOpWithNewOp<SIntSatOp>(op, lhs, rhs);
-    return mlir::success();
-  }
-  rewriter.replaceOpWithNewOp<IntOp>(op, lhs, rhs, intOverflowFlag(op));
-  return mlir::success();
+static mlir::LLVM::IntegerOverflowFlags
+convertOverflowFlags(cir::OverflowFlags flags) {
+  mlir::LLVM::IntegerOverflowFlags result =
+      mlir::LLVM::IntegerOverflowFlags::none;
+  if (bitEnumContainsAny(flags, cir::OverflowFlags::NoSignedWrap))
+    result = result | mlir::LLVM::IntegerOverflowFlags::nsw;
+  if (bitEnumContainsAny(flags, cir::OverflowFlags::NoUnsignedWrap))
+    result = result | mlir::LLVM::IntegerOverflowFlags::nuw;
+  return result;
 }
 
 mlir::LogicalResult CIRToLLVMAddOpLowering::matchAndRewrite(
     cir::AddOp op, OpAdaptor adaptor,
     mlir::ConversionPatternRewriter &rewriter) const {
-  return lowerSaturatableArithOp<mlir::LLVM::UAddSat, mlir::LLVM::SAddSat,
-                                 mlir::LLVM::AddOp>(op, adaptor.getLhs(),
-                                                    adaptor.getRhs(), rewriter);
+  rewriter.replaceOpWithNewOp<mlir::LLVM::AddOp>(
+      op, adaptor.getLhs(), adaptor.getRhs(),
+      convertOverflowFlags(op.getOverflowFlags()));
+  return mlir::success();
 }
 
 mlir::LogicalResult CIRToLLVMSubOpLowering::matchAndRewrite(
     cir::SubOp op, OpAdaptor adaptor,
     mlir::ConversionPatternRewriter &rewriter) const {
-  return lowerSaturatableArithOp<mlir::LLVM::USubSat, mlir::LLVM::SSubSat,
-                                 mlir::LLVM::SubOp>(op, adaptor.getLhs(),
-                                                    adaptor.getRhs(), rewriter);
+  rewriter.replaceOpWithNewOp<mlir::LLVM::SubOp>(
+      op, adaptor.getLhs(), adaptor.getRhs(),
+      convertOverflowFlags(op.getOverflowFlags()));
+  return mlir::success();
 }
 
 mlir::LogicalResult CIRToLLVMMulOpLowering::matchAndRewrite(
     cir::MulOp op, OpAdaptor adaptor,
     mlir::ConversionPatternRewriter &rewriter) const {
-  assert(cir::isIntOrBoolType(
-             elementTypeIfVectorOrMatrix(op.getRhs().getType())) &&
-         "cir.mul expects integer operand types");
   rewriter.replaceOpWithNewOp<mlir::LLVM::MulOp>(
-      op, adaptor.getLhs(), adaptor.getRhs(), intOverflowFlag(op));
+      op, adaptor.getLhs(), adaptor.getRhs(),
+      convertOverflowFlags(op.getOverflowFlags()));
   return mlir::success();
 }
 
-/// Lower an integer Div/Rem op to its signed or unsigned LLVM counterpart.
+/// Lower an integer op to its signed or unsigned LLVM counterpart.
 template <typename UIntOp, typename SIntOp, typename CIROp>
 static mlir::LogicalResult
 lowerIntBinaryOp(CIROp op, mlir::Value lhs, mlir::Value rhs,
@@ -3855,6 +3836,20 @@ lowerIntBinaryOp(CIROp op, mlir::Value lhs, mlir::Value rhs,
   return mlir::success();
 }
 
+mlir::LogicalResult CIRToLLVMAddSatOpLowering::matchAndRewrite(
+    cir::AddSatOp op, OpAdaptor adaptor,
+    mlir::ConversionPatternRewriter &rewriter) const {
+  return lowerIntBinaryOp<mlir::LLVM::UAddSat, mlir::LLVM::SAddSat>(
+      op, adaptor.getLhs(), adaptor.getRhs(), rewriter);
+}
+
+mlir::LogicalResult CIRToLLVMSubSatOpLowering::matchAndRewrite(
+    cir::SubSatOp op, OpAdaptor adaptor,
+    mlir::ConversionPatternRewriter &rewriter) const {
+  return lowerIntBinaryOp<mlir::LLVM::USubSat, mlir::LLVM::SSubSat>(
+      op, adaptor.getLhs(), adaptor.getRhs(), rewriter);
+}
+
 mlir::LogicalResult CIRToLLVMDivOpLowering::matchAndRewrite(
     cir::DivOp op, OpAdaptor adaptor,
     mlir::ConversionPatternRewriter &rewriter) const {
diff --git a/clang/test/CIR/CodeGen/fixed-point-arith.cpp b/clang/test/CIR/CodeGen/fixed-point-arith.cpp
index 1e1f11860dd806..485c2fab71c453 100644
--- a/clang/test/CIR/CodeGen/fixed-point-arith.cpp
+++ b/clang/test/CIR/CodeGen/fixed-point-arith.cpp
@@ -4,6 +4,12 @@
 // RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVMCIR
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffixed-point -emit-llvm %s -o %t.ll
 // RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM,OGCG
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffixed-point -fpadding-on-unsigned-fixed-point -fclangir -emit-cir %s -o %t-pad.cir
+// RUN: FileCheck --input-file=%t-pad.cir %s --check-prefix=PAD-CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffixed-point -fpadding-on-unsigned-fixed-point -fclangir -emit-llvm %s -o %t-pad-cir.ll
+// RUN: FileCheck --input-file=%t-pad-cir.ll %s --check-prefix=PAD-LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffixed-point -fpadding-on-unsigned-fixed-point -emit-llvm %s -o %t-pad.ll
+// RUN: FileCheck --input-file=%t-pad.ll %s --check-prefix=PAD-LLVM
 
 extern "C" {
 // CIR-LABEL: cir.func {{.*}}@add(
@@ -538,7 +544,7 @@ _Fract dec2(_Fract a) {
 // CIR-NEXT: %[[B:.*]] = cir.alloca "b" align(4) init : !cir.ptr<!s32i>
 // CIR:      %[[LOAD_A:.*]] = cir.load align(4) %[[A]] : !cir.ptr<!s32i>, !s32i
 // CIR-NEXT: %[[LOAD_B:.*]] = cir.load align(4) %[[B]] : !cir.ptr<!s32i>, !s32i
-// CIR-NEXT: %[[ADD:.*]] = cir.add sat %[[LOAD_A]], %[[LOAD_B]] : !s32i
+// CIR-NEXT: %[[ADD:.*]] = cir.add.sat %[[LOAD_A]], %[[LOAD_B]] : !s32i
 // CIR-NEXT: cir.store %[[ADD]], %[[RET]] : !s32i, !cir.ptr<!s32i>
 
 // LLVM-LABEL: define {{.*}}i32 @satAdd(
@@ -559,7 +565,7 @@ _Sat _Accum satAdd(_Sat _Accum a, _Accum b) {
 // CIR-NEXT: %[[B:.*]] = cir.alloca "b" align(4) init : !cir.ptr<!s32i>
 // CIR:      %[[LOAD_A:.*]] = cir.load align(4) %[[A]] : !cir.ptr<!s32i>, !s32i
 // CIR-NEXT: %[[LOAD_B:.*]] = cir.load align(4) %[[B]] : !cir.ptr<!s32i>, !s32i
-// CIR-NEXT: %[[SUB:.*]] = cir.sub sat %[[LOAD_A]], %[[LOAD_B]] : !s32i
+// CIR-NEXT: %[[SUB:.*]] = cir.sub.sat %[[LOAD_A]], %[[LOAD_B]] : !s32i
 // CIR-NEXT: cir.store %[[SUB]], %[[RET]] : !s32i, !cir.ptr<!s32i>
 
 // LLVM-LABEL: define {{.*}}i32 @satSub(
@@ -717,7 +723,7 @@ _Sat _Fract satShr2(_Sat _Fract f, int i) {
 // CIR-NEXT: %[[CMP:.*]] = cir.cmp lt %[[SELECT]], %[[MIN]] : !cir.int<s, 47>
 // CIR-NEXT: %[[SELECT2:.*]] = cir.select if %[[CMP]] then %[[MIN]] else %[[SELECT]] : (!cir.bool, !cir.int<s, 47>, !cir.int<s, 47>) -> !cir.int<s, 47>
 // CIR-NEXT: %[[TRUNC:.*]] = cir.cast integral %[[SELECT2]] : !cir.int<s, 47> -> !s32i
-// CIR-NEXT: %[[SUB:.*]] = cir.sub sat %[[LOAD_A]], %[[TRUNC]] : !s32i
+// CIR-NEXT: %[[SUB:.*]] = cir.sub.sat %[[LOAD_A]], %[[TRUNC]] : !s32i
 // CIR-NEXT: cir.store align(4) %[[SUB]], %[[A]] : !s32i, !cir.ptr<!s32i>
 // CIR-NEXT: cir.store %[[SUB]], %[[RET]] : !s32i, !cir.ptr<!s32i>
 
@@ -746,7 +752,7 @@ _Sat _Accum satInc(_Sat _Accum a) {
 // CIR-NEXT: %[[CMP:.*]] = cir.cmp lt %[[SELECT]], %[[MIN]] : !cir.int<s, 31>
 // CIR-NEXT: %[[SELECT2:.*]] = cir.select if %[[CMP]] then %[[MIN]] else %[[SELECT]] : (!cir.bool, !cir.int<s, 31>, !cir.int<s, 31>) -> !cir.int<s, 31>
 // CIR-NEXT: %[[TRUNC:.*]] = cir.cast integral %[[SELECT2]] : !cir.int<s, 31> -> !s16i
-// CIR-NEXT: %[[ADD:.*]] = cir.add sat %[[LOAD_F]], %[[TRUNC]] : !s16i
+// CIR-NEXT: %[[ADD:.*]] = cir.add.sat %[[LOAD_F]], %[[TRUNC]] : !s16i
 // CIR-NEXT: cir.store align(2) %[[ADD]], %[[F]] : !s16i, !cir.ptr<!s16i>
 // CIR-NEXT: cir.store %[[LOAD_F]], %[[RET]] : !s16i, !cir.ptr<!s16i>
 
@@ -776,7 +782,7 @@ _Sat _Fract satDec(_Sat _Fract f) {
 // CIR-NEXT: %[[CMP:.*]] = cir.cmp lt %[[SELECT]], %[[ZERO]] : !s32i
 // CIR-NEXT: %[[SELECT2:.*]] = cir.select if %[[CMP]] then %[[ZERO]] else %[[SELECT]] : (!cir.bool, !s32i, !s32i) -> !s32i
 // CIR-NEXT: %[[TRUNC:.*]] = cir.cast integral %[[SELECT2]] : !s32i -> !u16i
-// CIR-NEXT: %[[ADD:.*]] = cir.add sat %[[LOAD_A]], %[[TRUNC]] : !u16i
+// CIR-NEXT: %[[ADD:.*]] = cir.add.sat %[[LOAD_A]], %[[TRUNC]] : !u16i
 // CIR-NEXT: cir.store align(2) %[[ADD]], %[[A]] : !u16i, !cir.ptr<!u16i>
 // CIR-NEXT: cir.store %[[ADD]], %[[RET]] : !u16i, !cir.ptr<!u16i>
 
@@ -790,4 +796,40 @@ _Sat _Fract satDec(_Sat _Fract f) {
 _Sat unsigned _Fract satIncUFract(_Sat unsigned _Fract a) {
   return ++a;
 }
+
+// With unsigned padding, saturating add/sub saturate as signed values like
+// classic codegen does, and subtraction clamps at 0 while still signed.
+
+// PAD-CIR-LABEL: cir.func {{.*}}@padSatAddUAccum(
+// PAD-CIR:      %[[LHS:.*]] = cir.cast integral %{{.*}} : !u32i -> !s32i
+// PAD-CIR-NEXT: %[[RHS:.*]] = cir.cast integral %{{.*}} : !u32i -> !s32i
+// PAD-CIR-NEXT: %[[SUM:.*]] = cir.add.sat %[[LHS]], %[[RHS]] : !s32i
+// PAD-CIR-NEXT: %{{.*}} = cir.cast integral %[[SUM]] : !s32i -> !u32i
+
+// PAD-LLVM-LABEL: define {{.*}}i32 @padSatAddUAccum(
+// PAD-LLVM:      %[[SUM:.*]] = call i32 @llvm.sadd.sat.i32(i32 %{{.*}}, i32 %{{.*}})
+// PAD-LLVM-NEXT: %{{.*}} = trunc i32 %[[SUM]] to i31
+_Sat unsigned _Accum padSatAddUAccum(_Sat unsigned _Accum a,
+                                     _Sat unsigned _Accum b) {
+  return a + b;
+}
+
+// PAD-CIR-LABEL: cir.func {{.*}}@padSatSubUAccum(
+// PAD-CIR:      %[[LHS:.*]] = cir.cast integral %{{.*}} : !u32i -> !s32i
+// PAD-CIR-NEXT: %[[RHS:.*]] = cir.cast integral %{{.*}} : !u32i -> !s32i
+// PAD-CIR-NEXT: %[[DIFF:.*]] = cir.sub.sat %[[LHS]], %[[RHS]] : !s32i
+// PAD-CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+// PAD-CIR-NEXT: %[[NEG:.*]] = cir.cmp lt %[[DIFF]], %[[ZERO]] : !s32i
+// PAD-CIR-NEXT: %[[CLAMP:.*]] = cir.select if %[[NEG]] then %[[ZERO]] else %[[DIFF]]
+// PAD-CIR-NEXT: %{{.*}} = cir.cast integral %[[CLAMP]] : !s32i -> !u32i
+
+// PAD-LLVM-LABEL: define {{.*}}i32 @padSatSubUAccum(
+// PAD-LLVM:      %[[DIFF:.*]] = call i32 @llvm.ssub.sat.i32(i32 %{{.*}}, i32 %{{.*}})
+// PAD-LLVM-NEXT: %[[NEG:.*]] = icmp slt i32 %[[DIFF]], 0
+// PAD-LLVM-NEXT: %[[CLAMP:.*]] = select i1 %[[NEG]], i32 0, i32 %[[DIFF]]
+// PAD-LLVM-NEXT: %{{.*}} = trunc i32 %[[CLAMP]] to i31
+_Sat unsigned _Accum padSatSubUAccum(_Sat unsigned _Accum a,
+                                     _Sat unsigned _Accum b) {
+  return a - b;
+}
 }
diff --git a/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c b/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c
index 2a4377d47d7950..61a14a44174915 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c
@@ -672,27 +672,27 @@ void test_builtin_elementwise_add_sat(int i1, int i2, unsigned u1, unsigned u2,
   // CIR-LABEL: test_builtin_elementwise_add_sat
   // LLVM-LABEL: test_builtin_elementwise_add_sat
 
-  // CIR: cir.add sat %{{.*}}, %{{.*}} : !s32i
+  // CIR: cir.add.sat %{{.*}}, %{{.*}} : !s32i
   // LLVM: call i32 @llvm.sadd.sat.i32(i32 %{{.*}}, i32 %{{.*}})
   i1 = __builtin_elementwise_add_sat(i1, i2);
 
-  // CIR: cir.add sat %{{.*}}, %{{.*}} : !u32i
+  // CIR: cir.add.sat %{{.*}}, %{{.*}} : !u32i
   // LLVM: call i32 @llvm.uadd.sat.i32(i32 %{{.*}}, i32 %{{.*}})
   u1 = __builtin_elementwise_add_sat(u1, u2);
 
-  // CIR: cir.add sat %{{.*}}, %{{.*}} : !s16i
+  // CIR: cir.add.sat %{{.*}}, %{{.*}} : !s16i
   // LLVM: call i16 @llvm.sadd.sat.i16(i16 %{{.*}}, i16 %{{.*}})
   s1 = __builtin_elementwise_add_sat(s1, s2);
 
-  // CIR: cir.add sat %{{.*}}, %{{.*}} : !cir.vector<4 x !s32i>
+  // CIR: cir.add.sat %{{.*}}, %{{.*}} : !cir.vector<4 x !s32i>
   // LLVM: call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> %{{.*}}, <4 x i32> %{{.*}})
   vi1 = __builtin_elementwise_add_sat(vi1, vi2);
 
-  // CIR: cir.add sat %{{.*}}, %{{.*}} : !cir.vector<4 x !u32i>
+  // CIR: cir.add.sat %{{.*}}, %{{.*}} : !cir.vector<4 x !u32i>
   // LLVM: call <4 x i32> @llvm.uadd.sat.v4i32(<4 x i32> %{{.*}}, <4 x i32> %{{.*}})
   vu1 = __builtin_elementwise_add_sat(vu1, vu2);
 
-  // CIR: cir.add sat %{{.*}}, %{{.*}} : !cir.vector<8 x !s16i>
+  // CIR: cir.add.sat %{{.*}}, %{{.*}} : !cir.vector<8 x !s16i>
   // LLVM: call <8 x i16> @llvm.sadd.sat.v8i16(<8 x i16> %{{.*}}, <8 x i16> %{{.*}})
   vs1 = __builtin_elementwise_add_sat(vs1, vs2);
 }
@@ -704,27 +704,27 @@ void test_builtin_elementwise_sub_sat(int i1, int i2, unsigned u1, unsigned u2,
   // CIR-LABEL: test_builtin_elementwise_sub_sat
   // LLVM-LABEL: test_builtin_elementwise_sub_sat
 
-  // CIR: cir.sub sat %{{.*}}, %{{.*}} : !s32i
+  // CIR: cir.sub.sat %{{.*}}, %{{.*}} : !s32i
   // LLVM: call i32 @llvm.ssub.sat.i32(i32 %{{.*}}, i32 %{{.*}})
   i1 = __builtin_elementwise_sub_sat(i1, i2);
 
-  // CIR: cir.sub sat %{{.*}}, %{{.*}} : !u32i
+  // CIR: cir.sub.sat %{{.*}}, %{{.*}} : !u32i
   // LLVM: call i32 @llvm.usub.sat.i32(i32 %{{.*}}, i32 %{{.*}})
   u1 = __builtin_elementwise_sub_sat(u1, u2);
 
-  // CIR: cir.sub sat %{{.*}}, %{{.*}} : !s16i
+  // CIR: cir.sub.sat %{{.*}}, %{{.*}} : !s16i
   // LLVM: call i16 @llvm.ssub.sat.i16(i16 %{{.*}}, i16 %{{.*}})
   s1 = __builtin_elementwise_sub_sat(s1, s2);
 
-  // CIR: cir.sub sat %{{.*}}, %{{.*}} : !cir.vector<4 x !s32i>
+  // CIR: cir.sub.sat %{{.*}}, %{{.*}} : !cir.vector<4 x !s32i>
   // LLVM: call <4 x i32> @llvm.ssub.sat.v4i32(<4 x i32> %{{.*}}, <4 x i32> %{{.*}})
   vi1 = __builtin_elementwise_sub_sat(vi1, vi2);
 
-  // CIR: cir.sub sat %{{.*}}, %{{.*}} : !cir.vector<4 x !u32i>
+  // CIR: cir.sub.sat %{{.*}}, %{{.*}} : !cir.vector<4 x !u32i>
   // LLVM: call <4 x i32> @llvm.usub.sat.v4i32(<4 x i32> %{{.*}}, <4 x i32> %{{.*}})
   vu1 = __builtin_elementwise_sub_sat(vu1, vu2);
 
-  // CIR: cir.sub sat %{{.*}}, %{{.*}} : !cir.vector<8 x !s16i>
+  // CIR: cir.sub.sat %{{.*}}, %{{.*}} : !cir.vector<8 x !s16i>
   // LLVM: call <8 x i16> @llvm.ssub.sat.v8i16(<8 x i16> %{{.*}}, <8 x i16> %{{.*}})
   vs1 = __builtin_elementwise_sub_sat(vs1, vs2);
 }
diff --git a/clang/test/CIR/IR/invalid-overflow-flags.cir b/clang/test/CIR/IR/invalid-overflow-flags.cir
new file mode 100644
index 00000000000000..a896378f872c0a
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-overflow-flags.cir
@@ -0,0 +1,17 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+cir.func @sub_sat_bool(%a: !cir.bool, %b: !cir.bool) {
+  // expected-error at +1 {{'cir.sub.sat' op operand #0 must be}}
+  %0 = cir.sub.sat %a, %b : !cir.bool
+  cir.return
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+cir.func @unknown_bit(%a: !s32i, %b: !s32i) {
+  // expected-error at +1 {{property 'overflow_flags' failed to satisfy constraint: integer overflow flags}}
+  %0 = "cir.add"(%a, %b) <{overflow_flags = 4 : i32}> : (!s32i, !s32i) -> !s32i
+  cir.return
+}
diff --git a/clang/test/CIR/IR/overflow-flags.cir b/clang/test/CIR/IR/overflow-flags.cir
new file mode 100644
index 00000000000000..2f0ea9d1410747
--- /dev/null
+++ b/clang/test/CIR/IR/overflow-flags.cir
@@ -0,0 +1,62 @@
+// RUN: cir-opt %s --verify-roundtrip | FileCheck %s
+// RUN: cir-opt %s -mlir-print-op-generic | FileCheck %s --check-prefix=GENERIC
+
+!s32i = !cir.int<s, 32>
+!u32i = !cir.int<u, 32>
+
+module {
+  // CHECK-LABEL: cir.func @add
+  cir.func @add(%a: !s32i, %b: !s32i) {
+    // CHECK: cir.add %{{.*}}, %{{.*}} : !s32i
+    %0 = cir.add %a, %b : !s32i
+    // CHECK: cir.add nsw %{{.*}}, %{{.*}} : !s32i
+    %1 = cir.add nsw %a, %b : !s32i
+    // CHECK: cir.add nuw %{{.*}}, %{{.*}} : !s32i
+    %2 = cir.add nuw %a, %b : !s32i
+    // CHECK: cir.add nuw nsw %{{.*}}, %{{.*}} : !s32i
+    %3 = cir.add nuw nsw %a, %b : !s32i
+    // CHECK: cir.add nuw nsw %{{.*}}, %{{.*}} : !s32i
+    %4 = cir.add nsw nuw %a, %b : !s32i
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @sub
+  cir.func @sub(%a: !u32i, %b: !u32i) {
+    // CHECK: cir.sub nuw %{{.*}}, %{{.*}} : !u32i
+    %0 = cir.sub nuw %a, %b : !u32i
+    // CHECK: cir.sub nuw nsw %{{.*}}, %{{.*}} : !u32i
+    %1 = cir.sub nsw nuw %a, %b : !u32i
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @mul
+  cir.func @mul(%a: !s32i, %b: !s32i) {
+    // CHECK: cir.mul %{{.*}}, %{{.*}} : !s32i
+    %0 = cir.mul %a, %b : !s32i
+    // CHECK: cir.mul nsw %{{.*}}, %{{.*}} : !s32i
+    %1 = cir.mul nsw %a, %b : !s32i
+    // CHECK: cir.mul nuw nsw %{{.*}}, %{{.*}} : !s32i
+    %2 = cir.mul nuw nsw %a, %b : !s32i
+    cir.return
+  }
+
+  // CHECK-LABEL: cir.func @sat
+  cir.func @sat(%a: !s32i, %b: !s32i, %va: !cir.vector<4 x !u32i>,
+                %vb: !cir.vector<4 x !u32i>) {
+    // CHECK: cir.add.sat %{{.*}}, %{{.*}} : !s32i
+    %0 = cir.add.sat %a, %b : !s32i
+    // CHECK: cir.sub.sat %{{.*}}, %{{.*}} : !s32i
+    %1 = cir.sub.sat %a, %b : !s32i
+    // CHECK: cir.add.sat %{{.*}}, %{{.*}} : !cir.vector<4 x !u32i>
+    %2 = cir.add.sat %va, %vb : !cir.vector<4 x !u32i>
+    cir.return
+  }
+}
+
+// The flags are stored as a single bit-enum property.
+// GENERIC-LABEL: sym_name = "add"
+// GENERIC: "cir.add"(%{{.*}}, %{{.*}}) <{overflow_flags = 0 : i32}>
+// GENERIC: "cir.add"(%{{.*}}, %{{.*}}) <{overflow_flags = 1 : i32}>
+// GENERIC: "cir.add"(%{{.*}}, %{{.*}}) <{overflow_flags = 2 : i32}>
+// GENERIC: "cir.add"(%{{.*}}, %{{.*}}) <{overflow_flags = 3 : i32}>
+// GENERIC: "cir.add"(%{{.*}}, %{{.*}}) <{overflow_flags = 3 : i32}>
diff --git a/clang/test/CIR/Lowering/binop-signed-int.cir b/clang/test/CIR/Lowering/binop-signed-int.cir
index b10cfd982f26a7..497c64e392a45c 100644
--- a/clang/test/CIR/Lowering/binop-signed-int.cir
+++ b/clang/test/CIR/Lowering/binop-signed-int.cir
@@ -48,9 +48,9 @@ module {
     %33 = cir.load %1 : !cir.ptr<!s32i>, !s32i
     %34 = cir.or %32, %33 : !s32i
     // CHECK: = llvm.or
-    %35 = cir.add sat %32, %33: !s32i
+    %35 = cir.add.sat %32, %33: !s32i
     // CHECK: = llvm.intr.sadd.sat{{.*}}(i32, i32) -> i32
-    %36 = cir.sub sat %32, %33: !s32i
+    %36 = cir.sub.sat %32, %33: !s32i
     // CHECK: = llvm.intr.ssub.sat{{.*}}(i32, i32) -> i32 
     cir.store %34, %2 : !s32i, !cir.ptr<!s32i>
     %37 = cir.max %32, %33 : !s32i
diff --git a/clang/test/CIR/Lowering/binop-unsigned-int.cir b/clang/test/CIR/Lowering/binop-unsigned-int.cir
index 2dce7404676165..eedb33a75dd05f 100644
--- a/clang/test/CIR/Lowering/binop-unsigned-int.cir
+++ b/clang/test/CIR/Lowering/binop-unsigned-int.cir
@@ -41,8 +41,8 @@ module {
     %33 = cir.load %1 : !cir.ptr<!u32i>, !u32i
     %34 = cir.or %32, %33 : !u32i
     cir.store %34, %2 : !u32i, !cir.ptr<!u32i>
-    %35 = cir.add sat %32, %33: !u32i
-    %36 = cir.sub sat %32, %33: !u32i  
+    %35 = cir.add.sat %32, %33: !u32i
+    %36 = cir.sub.sat %32, %33: !u32i  
     %37 = cir.max %32, %33 : !u32i
     %38 = cir.min %32, %33 : !u32i
     cir.return
diff --git a/clang/test/CIR/Lowering/overflow-flags.cir b/clang/test/CIR/Lowering/overflow-flags.cir
new file mode 100644
index 00000000000000..93261b2aa2f28a
--- /dev/null
+++ b/clang/test/CIR/Lowering/overflow-flags.cir
@@ -0,0 +1,47 @@
+// RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR
+// RUN: cir-translate %s -cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering | FileCheck %s -check-prefix=LLVM
+
+!s32i = !cir.int<s, 32>
+
+// Both overflow flags must survive lowering together.
+
+module {
+  cir.func @add(%a: !s32i, %b: !s32i) -> !s32i {
+    %0 = cir.add nuw nsw %a, %b : !s32i
+    cir.return %0 : !s32i
+  }
+  // MLIR-LABEL: llvm.func @add
+  // MLIR: llvm.add %{{.*}}, %{{.*}} overflow<nsw, nuw> : i32
+  // LLVM-LABEL: define {{.*}}@add(
+  // LLVM: add nuw nsw i32
+
+  cir.func @mul(%a: !s32i, %b: !s32i) -> !s32i {
+    %0 = cir.mul nuw nsw %a, %b : !s32i
+    cir.return %0 : !s32i
+  }
+  // MLIR-LABEL: llvm.func @mul
+  // MLIR: llvm.mul %{{.*}}, %{{.*}} overflow<nsw, nuw> : i32
+  // LLVM-LABEL: define {{.*}}@mul(
+  // LLVM: mul nuw nsw i32
+
+  cir.func @vec_add(%a: !cir.vector<4 x !s32i>, %b: !cir.vector<4 x !s32i>) -> !cir.vector<4 x !s32i> {
+    %0 = cir.add nuw nsw %a, %b : !cir.vector<4 x !s32i>
+    cir.return %0 : !cir.vector<4 x !s32i>
+  }
+  // MLIR-LABEL: llvm.func @vec_add
+  // MLIR: llvm.add %{{.*}}, %{{.*}} overflow<nsw, nuw> : vector<4xi32>
+  // LLVM-LABEL: define {{.*}}@vec_add(
+  // LLVM: add nuw nsw <4 x i32>
+
+  // Saturating ops pick the signed or unsigned intrinsic from the element
+  // type, elementwise for matrices.
+
+  cir.func @matrix_sub_sat(%a: !cir.matrix<2 x 3 x !s32i>, %b: !cir.matrix<2 x 3 x !s32i>) -> !cir.matrix<2 x 3 x !s32i> {
+    %0 = cir.sub.sat %a, %b : !cir.matrix<2 x 3 x !s32i>
+    cir.return %0 : !cir.matrix<2 x 3 x !s32i>
+  }
+  // MLIR-LABEL: llvm.func @matrix_sub_sat
+  // MLIR: llvm.intr.ssub.sat(%{{.*}}, %{{.*}}) : (vector<6xi32>, vector<6xi32>) -> vector<6xi32>
+  // LLVM-LABEL: define {{.*}}@matrix_sub_sat(
+  // LLVM: call <6 x i32> @llvm.ssub.sat.v6i32(
+}



More information about the cfe-commits mailing list