[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 07:48:34 PST 2026
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/195618
>From 9496e593f931395f34e49468a79507006f3420a8 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 whose layout
matches mlir::LLVM::IntegerOverflowFlags, so lowering casts it directly
and keeps both flags. 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.
That leaves cir::OverflowBehavior only describing nsw/nuw, so the builder
takes cir::OverflowFlags instead.
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 | 69 ++---------
.../include/clang/CIR/Dialect/IR/CIRAttrs.td | 18 +++
clang/include/clang/CIR/Dialect/IR/CIROps.td | 111 +++++++++++-------
clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 9 +-
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 8 +-
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 12 +-
.../lib/CIR/CodeGen/CIRGenFixedPointBuilder.h | 54 +++++----
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 10 +-
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 22 ----
.../Dialect/Transforms/CIRCanonicalize.cpp | 12 +-
.../TargetLowering/LowerItaniumCXXABI.cpp | 32 ++---
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 70 +++++------
clang/test/CIR/CodeGen/fixed-point-arith.cpp | 52 +++++++-
.../CodeGenBuiltins/builtins-elementwise.c | 24 ++--
clang/test/CIR/IR/invalid-overflow-flags.cir | 27 +++++
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 | 66 +++++++++++
19 files changed, 416 insertions(+), 250 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..908de844471c95 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,46 @@ 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::nsw);
}
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::nuw);
}
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::nsw);
}
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::nuw);
}
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::nsw);
}
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::nuw);
}
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..f8fafa7c5659ae 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -1135,6 +1135,24 @@ def CIR_FastMathFlags : CIR_I32BitEnum<
def CIR_FastMathFlagsAttr : CIR_EnumAttr<CIR_FastMathFlags, "fastmath">;
+//===----------------------------------------------------------------------===//
+// OverflowFlags
+//===----------------------------------------------------------------------===//
+
+// The bit layout matches mlir::LLVM::IntegerOverflowFlags so that lowering can
+// cast between the two. The flags are written as space-separated keywords, with
+// `nuw` listed first so they print in LLVM IR order, e.g. `nuw nsw`.
+def CIR_OFnone : I32BitEnumCaseNone<"none">;
+def CIR_OFnsw : I32BitEnumCaseBit<"nsw", 0>;
+def CIR_OFnuw : I32BitEnumCaseBit<"nuw", 1>;
+
+def CIR_OverflowFlags : CIR_I32BitEnum<
+ "OverflowFlags", "integer overflow flags", [
+ CIR_OFnone, CIR_OFnuw, CIR_OFnsw
+]> {
+ 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..8b6e76a6360ab8 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2831,27 +2831,19 @@ 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.
+// Base class for integer binary ops that support the nsw/nuw overflow flags.
class CIR_BinaryOpWithOverflowFlags<string mnemonic, Type type,
list<Trait> traits = []>
: CIR_BinaryOp<mnemonic, type, !listconcat([Pure], traits)>
{
- let arguments = (ins
- type:$lhs, type:$rhs,
- UnitProp:$no_signed_wrap,
- UnitProp:$no_unsigned_wrap,
- UnitProp:$saturated
- );
+ let arguments = !con(commonArgs, (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 +2858,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 +2887,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 : !u32i
%3 = cir.sub %va, %vb : !cir.vector<4 x !s32i>
%4 = cir.sub %ma, %mb : !cir.matrix<3 x 3 x !s32i>
```
@@ -2919,39 +2908,73 @@ 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
+//===----------------------------------------------------------------------===//
- 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 representable range of the element type instead of
+ wrapping. Whether the clamp is signed or unsigned follows the signedness
+ of the element type. Vector and matrix operands are added elementwise.
+ Both operands and the result must have the same type.
+
+ Example:
+
+ ```
+ %0 = cir.add.sat %a, %b : !s32i
+ %1 = cir.add.sat %va, %vb : !cir.vector<4 x !u8i>
+ ```
+ }];
+}
+
+//===----------------------------------------------------------------------===//
+// SubSatOp
+//===----------------------------------------------------------------------===//
+
+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 representable range of the element type instead of
+ wrapping. Whether the clamp is signed or unsigned follows the signedness
+ of the element type. Vector and matrix operands are subtracted
+ elementwise. Both operands and the result must have the same type.
+
+ 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..2751dc51d01bbf 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -2096,8 +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.
+ // cir.add.sat/cir.sub.sat do not accept !cir.bool elements, so a
+ // bool-element saturating add/sub is not representable with them.
// 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().
@@ -2114,9 +2114,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);
+ ? cir::AddSatOp::create(builder, loc, op0, op1).getResult()
+ : cir::SubSatOp::create(builder, loc, op0, op1).getResult();
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..2e6ff17fc47b35 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::nsw;
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..35a7770fe1420b 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::nsw;
+ 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::nuw);
}
return numElts;
diff --git a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
index 3ea67b830198a0..2b1db364fa4677 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
@@ -134,13 +134,10 @@ 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()
+ ? createSaturating<cir::AddSatOp>(wideLhs, wideRhs, commonSema)
+ : builder.createAdd(loc, wideLhs, wideRhs);
return createFixedToFixed(result, commonSema,
lhsSema.getCommonSemantics(rhsSema));
@@ -155,22 +152,10 @@ 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()
+ ? createSaturating<cir::SubSatOp>(wideLhs, wideRhs, commonSema)
+ : builder.createSub(loc, wideLhs, wideRhs);
return createFixedToFixed(result, commonSema,
lhsSema.getCommonSemantics(rhsSema));
@@ -392,6 +377,29 @@ struct CIRGenFixedPointBuilder {
c.getScale(), c.isSigned(), c.isSaturated(), bothPadded);
}
+ /// Emits a saturating add or sub of operands in \p sema. With unsigned
+ /// padding, the padding bit keeps the value below the signed maximum, so it
+ /// saturates as a signed value, as llvm::FixedPointBuilder does; a difference
+ /// below 0 is then clamped while the value is still signed.
+ template <typename SatOp>
+ mlir::Value createSaturating(mlir::Value lhs, mlir::Value rhs,
+ const llvm::FixedPointSemantics &sema) {
+ if (!sema.hasUnsignedPadding())
+ return SatOp::create(builder, loc, lhs, rhs);
+
+ mlir::Type signedTy = builder.getSIntNTy(sema.getWidth());
+ mlir::Value signedLhs = builder.createIntCast(lhs, signedTy);
+ mlir::Value signedRhs = builder.createIntCast(rhs, signedTy);
+ mlir::Value result = SatOp::create(builder, loc, signedLhs, signedRhs);
+ if constexpr (std::is_same_v<SatOp, cir::SubSatOp>) {
+ mlir::Value zero = builder.getNullValue(signedTy, loc);
+ mlir::Value ltZero =
+ builder.createCompare(loc, cir::CmpOpKind::lt, result, zero);
+ result = builder.createSelect(loc, ltZero, zero, 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..48dc4bb8c81c25 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -1617,9 +1617,8 @@ CIRGenFunction::emitArrayLength(const clang::ArrayType *origArrayType,
// If we had any VLA dimensions, factor them in.
if (numVLAElements)
- numElements =
- builder.createMul(numVLAElements.getLoc(), numVLAElements, numElements,
- cir::OverflowBehavior::NoUnsignedWrap);
+ numElements = builder.createMul(numVLAElements.getLoc(), numVLAElements,
+ numElements, cir::OverflowFlags::nuw);
return numElements;
}
@@ -1673,9 +1672,8 @@ CIRGenFunction::getVLASize(const VariableArrayType *type) {
// It's undefined behavior if this wraps around, so mark it that way.
// FIXME: Teach -fsanitize=undefined to trap this.
- numElements =
- builder.createMul(numElements.getLoc(), numElements, vlaSize,
- cir::OverflowBehavior::NoUnsignedWrap);
+ numElements = builder.createMul(numElements.getLoc(), numElements,
+ vlaSize, cir::OverflowFlags::nuw);
}
} 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/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
index 31fbdcecc44141..675e4464542a5c 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
@@ -71,12 +71,12 @@ void CIRCanonicalizePass::runOnOperation() {
// Many operations are here to perform a manual `fold` in
// applyOpPatternsGreedily.
if (isa<BrOp, BrCondOp, BuiltinIntCastOp, CastOp, ScopeOp, SwitchOp,
- SelectOp, IncOp, DecOp, MinusOp, FNegOp, NotOp, AddOp, MulOp, AndOp,
- OrOp, XorOp, MaxOp, MinOp, ComplexCreateOp, ComplexImagOp,
- ComplexRealOp, VecCmpOp, VecCreateOp, VecExtractOp, VecShuffleOp,
- VecShuffleDynamicOp, VecTernaryOp, BitClrsbOp, BitClzOp, BitCtzOp,
- BitFfsOp, BitParityOp, BitPopcountOp, BitReverseOp, ByteSwapOp,
- RotateOp, ConstantOp, CleanupScopeOp>(op))
+ SelectOp, IncOp, DecOp, MinusOp, FNegOp, NotOp, AddOp, AddSatOp,
+ SubSatOp, MulOp, AndOp, OrOp, XorOp, MaxOp, MinOp, ComplexCreateOp,
+ ComplexImagOp, ComplexRealOp, VecCmpOp, VecCreateOp, VecExtractOp,
+ VecShuffleOp, VecShuffleDynamicOp, VecTernaryOp, BitClrsbOp,
+ BitClzOp, BitCtzOp, BitFfsOp, BitParityOp, BitPopcountOp,
+ BitReverseOp, ByteSwapOp, RotateOp, ConstantOp, CleanupScopeOp>(op))
ops.push_back(op);
});
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
index f5179f38cfc1e4..59d737c61ef361 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::nsw);
+ else
+ adjustedPtr = cir::AddOp::create(builder, loc, loweredSrc, offsetValue,
+ cir::OverflowFlags::nsw);
return cir::SelectOp::create(builder, loc, ty, isNull, loweredSrc,
adjustedPtr);
@@ -528,17 +525,12 @@ 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::nsw);
+ else
+ adjustedAdjField = cir::AddOp::create(builder, op->getLoc(), adjField,
+ offsetValue, cir::OverflowFlags::nsw);
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..0df709e9459ba2 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -51,6 +51,7 @@
#include "clang/CIR/MissingFeatures.h"
#include "clang/CIR/Passes.h"
#include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/Module.h"
@@ -3783,50 +3784,36 @@ 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;
-}
+static_assert(
+ llvm::to_underlying(cir::OverflowFlags::nsw) ==
+ llvm::to_underlying(mlir::LLVM::IntegerOverflowFlags::nsw) &&
+ llvm::to_underlying(cir::OverflowFlags::nuw) ==
+ llvm::to_underlying(mlir::LLVM::IntegerOverflowFlags::nuw),
+ "cir::OverflowFlags must match mlir::LLVM::IntegerOverflowFlags");
-/// 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>
+/// Lower an integer op carrying overflow flags to its LLVM counterpart.
+template <typename LLVMOp, 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));
+lowerOverflowBinaryOp(CIROp op, mlir::Value lhs, mlir::Value rhs,
+ mlir::ConversionPatternRewriter &rewriter) {
+ rewriter.replaceOpWithNewOp<LLVMOp>(
+ op, lhs, rhs,
+ static_cast<mlir::LLVM::IntegerOverflowFlags>(op.getOverflowFlags()));
return mlir::success();
}
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);
+ return lowerOverflowBinaryOp<mlir::LLVM::AddOp>(op, adaptor.getLhs(),
+ adaptor.getRhs(), rewriter);
}
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);
+ return lowerOverflowBinaryOp<mlir::LLVM::SubOp>(op, adaptor.getLhs(),
+ adaptor.getRhs(), rewriter);
}
mlir::LogicalResult CIRToLLVMMulOpLowering::matchAndRewrite(
@@ -3835,12 +3822,11 @@ mlir::LogicalResult CIRToLLVMMulOpLowering::matchAndRewrite(
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));
- return mlir::success();
+ return lowerOverflowBinaryOp<mlir::LLVM::MulOp>(op, adaptor.getLhs(),
+ adaptor.getRhs(), rewriter);
}
-/// 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 +3841,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..d9397050a87951
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-overflow-flags.cir
@@ -0,0 +1,27 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+!s32i = !cir.int<s, 32>
+
+cir.func @add_sat_with_flag(%a: !s32i, %b: !s32i) {
+ // expected-error at +1 {{expected SSA operand}}
+ %0 = cir.add.sat nsw %a, %b : !s32i
+ cir.return
+}
+
+// -----
+
+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..fc714b885e6eea
--- /dev/null
+++ b/clang/test/CIR/Lowering/overflow-flags.cir
@@ -0,0 +1,66 @@
+// 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>
+!u32i = !cir.int<u, 32>
+
+// Both overflow flags must survive lowering together.
+
+module attributes {cir.triple = "x86_64-unknown-linux-gnu"} {
+ 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 @sub(%a: !u32i, %b: !u32i) -> !u32i {
+ %0 = cir.sub nuw nsw %a, %b : !u32i
+ cir.return %0 : !u32i
+ }
+ // MLIR-LABEL: llvm.func @sub
+ // MLIR: llvm.sub %{{.*}}, %{{.*}} overflow<nsw, nuw> : i32
+ // LLVM-LABEL: define {{.*}}@sub(
+ // LLVM: sub 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>
+
+ cir.func @matrix_mul(%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.mul nuw nsw %a, %b : !cir.matrix<2 x 3 x !s32i>
+ cir.return %0 : !cir.matrix<2 x 3 x !s32i>
+ }
+ // MLIR-LABEL: llvm.func @matrix_mul
+ // MLIR: llvm.mul %{{.*}}, %{{.*}} overflow<nsw, nuw> : vector<6xi32>
+ // LLVM-LABEL: define {{.*}}@matrix_mul(
+ // LLVM: mul nuw nsw <6 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