[clang] [CIR] Match classic codegen for saturating fixed-point negation and padded conversions (PR #236702)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 00:29:53 PST 2026
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/236702
>From ceb84f801e4bb159908c668b60de002d6522ead4 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Thu, 29 Oct 2026 09:42:43 +0000
Subject: [PATCH] [CIR] Match classic codegen for saturating fixed-point
negation and padded conversions
Three CIR fixed-point paths diverged from classic codegen. Unary minus on a
saturating fixed-point type emitted a plain subtraction instead of a
saturating 0 - x. With -fpadding-on-unsigned-fixed-point, float to unsigned
fixed-point conversion produced the fptosi.sat result in an unsigned CIR
type, so its clamp to 0 compared as unsigned and never fired; it now
converts and clamps in a signed type, like llvm::FixedPointBuilder, and
casts to the unsigned type afterwards. A saturating fixed-point conversion
that kept the width but changed signedness returned a value of the source
CIR type, which broke the cir.cmp verifier and needed a pointer bitcast to
store; it now casts to the destination type.
Assisted-by: Claude Code (Claude Opus 5.5).
---
clang/lib/CIR/CodeGen/CIRGenBuilder.h | 8 ++
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 8 ++
.../lib/CIR/CodeGen/CIRGenFixedPointBuilder.h | 44 +++++-----
clang/test/CIR/CodeGen/fixed-point-arith.cpp | 86 +++++++++++++++++++
.../CIR/CodeGen/fixed-point-conversions.cpp | 8 +-
5 files changed, 130 insertions(+), 24 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 9edfeae3d6324f..33feccefb80732 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -917,6 +917,14 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
std::forward<Operands>(op)..., fastmath)
.getResult();
}
+
+ /// Converts \p src to \p dstTy, saturating at its bounds. The signedness of
+ /// \p dstTy selects between fptosi.sat and fptoui.sat.
+ mlir::Value createFPToIntSat(mlir::Location loc, mlir::Value src,
+ cir::IntType dstTy) {
+ return emitIntrinsicCallOp(
+ loc, dstTy.isSigned() ? "fptosi.sat" : "fptoui.sat", dstTy, src);
+ }
};
} // namespace clang::CIRGen
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 514788e01faa13..411c31a2ee2360 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -887,6 +887,14 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
if (cir::isFPOrVectorOfFPType(operand.getType()))
return builder.createOrFold<cir::FNegOp>(loc, operand);
+ // Saturated negation must saturate (-min for signed, any nonzero value for
+ // unsigned), as in classic codegen.
+ if (e->getType()->isSaturatedFixedPointType()) {
+ CIRGenFixedPointBuilder fpBuilder(builder, loc);
+ return fpBuilder.createNeg(
+ operand, cgf.getContext().getFixedPointSemantics(e->getType()));
+ }
+
// TODO(cir): We might have to change this to support overflow trapping.
// Classic codegen routes unary minus through emitSub to ensure
// that the overflow behavior is handled correctly.
diff --git a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
index 97cf78157b8267..71854ff8724069 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
@@ -49,8 +49,6 @@ struct CIRGenFixedPointBuilder {
mlir::Value createFloatingToFixed(mlir::Value src,
const llvm::FixedPointSemantics &dstSema) {
-
- bool useSigned = dstSema.isSigned() || dstSema.hasUnsignedPadding();
mlir::Value result = src;
mlir::Type opTy = getAccommodatingFloatType(src.getType(), dstSema);
@@ -70,23 +68,17 @@ struct CIRGenFixedPointBuilder {
cir::ConstantOp fpConst = builder.getConstFP(loc, opTy, scaleVal);
result = builder.createFMul(loc, result, fpConst);
- cir::IntType resultTy = cir::IntType::get(
- builder.getContext(), dstSema.getWidth(), dstSema.isSigned());
-
+ cir::IntType opIntTy = getOpIntType(dstSema);
if (dstSema.isSaturated()) {
- result = builder.emitIntrinsicCallOp(
- loc, useSigned ? "fptosi.sat" : "fptoui.sat", resultTy, result);
+ result = builder.createFPToIntSat(loc, result, opIntTy);
+ if (dstSema.hasUnsignedPadding())
+ result = clampNegativeToZero(result);
} else {
- result = builder.createCast(loc, cir::CastKind::float_to_int, result,
- resultTy);
+ result =
+ builder.createCast(loc, cir::CastKind::float_to_int, result, opIntTy);
}
- // When saturating unsigned-with-padding using signed operations, we may
- // get negative values. Emit an extra clamp to zero.
- if (dstSema.isSaturated() && dstSema.hasUnsignedPadding())
- result = clampNegativeToZero(result);
-
- return result;
+ return builder.createIntCast(result, getIntType(dstSema));
}
mlir::Value createFixedToInteger(mlir::Value src,
@@ -159,6 +151,13 @@ struct CIRGenFixedPointBuilder {
lhsSema.getCommonSemantics(rhsSema));
}
+ /// Negates \p value as 0 - value, so that saturating types saturate.
+ mlir::Value createNeg(mlir::Value value,
+ const llvm::FixedPointSemantics &sema) {
+ return createSub(builder.getNullValue(value.getType(), loc), sema, value,
+ sema);
+ }
+
mlir::Value createMul(mlir::Value lhs,
const llvm::FixedPointSemantics &lhsSema,
mlir::Value rhs,
@@ -299,8 +298,7 @@ struct CIRGenFixedPointBuilder {
result = builder.createShiftRight(loc, result, srcScale - dstScale);
}
- cir::IntType dstIntTy =
- cir::IntType::get(builder.getContext(), dstWidth, dstSema.isSigned());
+ cir::IntType dstIntTy = getIntType(dstSema);
if (!dstSema.isSaturated()) {
// Resize.
@@ -344,9 +342,9 @@ struct CIRGenFixedPointBuilder {
result = builder.createSelect(loc, tooLow, min, result);
}
- // Resize the integer part to get the final destination size.
- if (resultWidth != dstWidth)
- result = builder.createIntCast(result, dstIntTy);
+ // Unlike LLVM integers, CIR integers carry signedness, so the cast is
+ // needed even when only the signedness differs.
+ result = builder.createIntCast(result, dstIntTy);
}
return result;
}
@@ -375,6 +373,12 @@ struct CIRGenFixedPointBuilder {
c.getScale(), c.isSigned(), c.isSaturated(), bothPadded);
}
+ /// The CIR integer type that holds values of \p sema.
+ cir::IntType getIntType(const llvm::FixedPointSemantics &sema) {
+ return cir::IntType::get(builder.getContext(), sema.getWidth(),
+ sema.isSigned());
+ }
+
/// 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.
diff --git a/clang/test/CIR/CodeGen/fixed-point-arith.cpp b/clang/test/CIR/CodeGen/fixed-point-arith.cpp
index 485c2fab71c453..1408f0be26fffe 100644
--- a/clang/test/CIR/CodeGen/fixed-point-arith.cpp
+++ b/clang/test/CIR/CodeGen/fixed-point-arith.cpp
@@ -832,4 +832,90 @@ _Sat unsigned _Accum padSatSubUAccum(_Sat unsigned _Accum a,
_Sat unsigned _Accum b) {
return a - b;
}
+
+// Unary minus on a saturating type is a saturating 0 - x, like classic codegen.
+
+// CIR-LABEL: cir.func {{.*}}@satNeg(
+// CIR: %[[LOAD_A:.*]] = cir.load align(2) %{{.*}} : !cir.ptr<!s16i>, !s16i
+// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s16i
+// CIR-NEXT: %{{.*}} = cir.sub.sat %[[ZERO]], %[[LOAD_A]] : !s16i
+
+// LLVM-LABEL: define {{.*}}i16 @satNeg(
+// LLVM: %[[LOAD_A:.*]] = load i16, ptr %{{.*}}, align 2
+// LLVM-NEXT: %{{.*}} = call i16 @llvm.ssub.sat.i16(i16 0, i16 %[[LOAD_A]])
+_Sat short _Accum satNeg(_Sat short _Accum a) {
+ return -a;
+}
+
+// CIR-LABEL: cir.func {{.*}}@satNegU(
+// CIR: %[[LOAD_A:.*]] = cir.load align(2) %{{.*}} : !cir.ptr<!u16i>, !u16i
+// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !u16i
+// CIR-NEXT: %{{.*}} = cir.sub.sat %[[ZERO]], %[[LOAD_A]] : !u16i
+
+// LLVM-LABEL: define {{.*}}i16 @satNegU(
+// LLVM: %[[LOAD_A:.*]] = load i16, ptr %{{.*}}, align 2
+// LLVM-NEXT: %{{.*}} = call i16 @llvm.usub.sat.i16(i16 0, i16 %[[LOAD_A]])
+
+// PAD-CIR-LABEL: cir.func {{.*}}@satNegU(
+// PAD-CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s16i
+// PAD-CIR-NEXT: %[[A:.*]] = cir.cast integral %{{.*}} : !u16i -> !s16i
+// PAD-CIR-NEXT: %[[DIFF:.*]] = cir.sub.sat %[[ZERO]], %[[A]] : !s16i
+// PAD-CIR-NEXT: %[[ZERO2:.*]] = cir.const #cir.int<0> : !s16i
+// PAD-CIR-NEXT: %[[LT:.*]] = cir.cmp lt %[[DIFF]], %[[ZERO2]] : !s16i
+// PAD-CIR-NEXT: %{{.*}} = cir.select if %[[LT]] then %[[ZERO2]] else %[[DIFF]]
+
+// PAD-LLVM-LABEL: define {{.*}}i16 @satNegU(
+// PAD-LLVM: %[[DIFF:.*]] = call i16 @llvm.ssub.sat.i16(i16 0, i16 %{{.*}})
+// PAD-LLVM-NEXT: %[[LT:.*]] = icmp slt i16 %[[DIFF]], 0
+// PAD-LLVM-NEXT: %{{.*}} = select i1 %[[LT]], i16 0, i16 %[[DIFF]]
+_Sat unsigned short _Accum satNegU(_Sat unsigned short _Accum a) {
+ return -a;
+}
+
+// With unsigned padding, float to fixed-point conversion is signed, and the
+// saturating form clamps negative values to 0.
+
+// PAD-CIR-LABEL: cir.func {{.*}}@padFloatToSatU(
+// PAD-CIR: %[[CONV:.*]] = cir.call_llvm_intrinsic "fptosi.sat" %{{.*}} : (!cir.float) -> !s16i
+// PAD-CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s16i
+// PAD-CIR-NEXT: %[[LT:.*]] = cir.cmp lt %[[CONV]], %[[ZERO]] : !s16i
+// PAD-CIR-NEXT: %[[CLAMP:.*]] = cir.select if %[[LT]] then %[[ZERO]] else %[[CONV]]
+// PAD-CIR-NEXT: %{{.*}} = cir.cast integral %[[CLAMP]] : !s16i -> !u16i
+
+// PAD-LLVM-LABEL: define {{.*}}i16 @padFloatToSatU(
+// PAD-LLVM: %[[CONV:.*]] = call i16 @llvm.fptosi.sat.i16.f32(float %{{.*}})
+// PAD-LLVM-NEXT: %[[LT:.*]] = icmp slt i16 %[[CONV]], 0
+// PAD-LLVM-NEXT: %{{.*}} = select i1 %[[LT]], i16 0, i16 %[[CONV]]
+_Sat unsigned short _Accum padFloatToSatU(float f) {
+ return f;
+}
+
+// PAD-CIR-LABEL: cir.func {{.*}}@padFloatToU(
+// PAD-CIR: %[[CONV:.*]] = cir.cast float_to_int %{{.*}} : !cir.float -> !s16i
+// PAD-CIR-NEXT: %{{.*}} = cir.cast integral %[[CONV]] : !s16i -> !u16i
+
+// PAD-LLVM-LABEL: define {{.*}}i16 @padFloatToU(
+// PAD-LLVM: %{{.*}} = fptosi float %{{.*}} to i16
+unsigned short _Accum padFloatToU(float f) {
+ return f;
+}
+
+// A saturating conversion that only changes signedness still yields a value of
+// the destination type.
+
+// PAD-CIR-LABEL: cir.func {{.*}}@padSatSignedToU(
+// PAD-CIR: %[[LT:.*]] = cir.cmp lt %[[A:.*]], %[[ZERO:.*]] : !s16i
+// PAD-CIR-NEXT: %[[CLAMP:.*]] = cir.select if %[[LT]] then %[[ZERO]] else %[[A]]
+// PAD-CIR-NEXT: %[[CONV:.*]] = cir.cast integral %[[CLAMP]] : !s16i -> !u16i
+// PAD-CIR-NEXT: %[[B:.*]] = cir.load align(2) %{{.*}} : !cir.ptr<!u16i>, !u16i
+// PAD-CIR-NEXT: %{{.*}} = cir.cmp lt %[[CONV]], %[[B]] : !u16i
+
+// PAD-LLVM-LABEL: define {{.*}}i1 @padSatSignedToU(
+// PAD-LLVM: %[[LT:.*]] = icmp slt i16 %[[A:.*]], 0
+// PAD-LLVM-NEXT: %[[CLAMP:.*]] = select i1 %[[LT]], i16 0, i16 %[[A]]
+// PAD-LLVM-NEXT: %[[B:.*]] = load i16, ptr %{{.*}}, align 2
+// PAD-LLVM-NEXT: %{{.*}} = icmp ult i16 %[[CLAMP]], %[[B]]
+bool padSatSignedToU(_Sat short _Accum a, _Sat unsigned short _Accum b) {
+ return (_Sat unsigned short _Accum)a < b;
+}
}
diff --git a/clang/test/CIR/CodeGen/fixed-point-conversions.cpp b/clang/test/CIR/CodeGen/fixed-point-conversions.cpp
index fa7c6a28dfc6db..b3bae52f66ab07 100644
--- a/clang/test/CIR/CodeGen/fixed-point-conversions.cpp
+++ b/clang/test/CIR/CodeGen/fixed-point-conversions.cpp
@@ -346,8 +346,8 @@ unsigned _Accum bool_to_uaccum(bool i) {
// CIR-NEXT: %[[MAX:.*]] = cir.const #cir.int<32767> : !u16i
// CIR-NEXT: %[[GT_CMP:.*]] = cir.cmp gt %[[SHIFT]], %[[MAX]] : !u16i
// CIR-NEXT: %[[MAX_SEL:.*]] = cir.select if %[[GT_CMP]] then %[[MAX]] else %[[SHIFT]] : (!cir.bool, !u16i, !u16i) -> !u16i
-// CIR-NEXT: %[[RET_BITCAST:.*]] = cir.cast bitcast %[[RET:.*]] : !cir.ptr<!s16i> -> !cir.ptr<!u16i>
-// CIR-NEXT: cir.store %[[MAX_SEL]], %[[RET_BITCAST]] : !u16i, !cir.ptr<!u16i>
+// CIR-NEXT: %[[RES:.*]] = cir.cast integral %[[MAX_SEL]] : !u16i -> !s16i
+// CIR-NEXT: cir.store %[[RES]], %[[RET:.*]] : !s16i, !cir.ptr<!s16i>
// CIR-NEXT: %[[LOAD_RET:.*]] = cir.load %[[RET]] : !cir.ptr<!s16i>, !s16i
// CIR-NEXT: cir.return %[[LOAD_RET]] : !s16i
// LLVM-LABEL: @bool_to_sat_fract
@@ -368,8 +368,8 @@ _Sat _Fract bool_to_sat_fract(bool i) {
// CIR-NEXT: %[[CAST:.*]] = cir.cast integral %[[BOOL_CAST]] : !cir.int<u, 1> -> !u32i
// CIR-NEXT: %[[SHIFT_AMOUNT:.*]] = cir.const #cir.int<15> : !u32i
// CIR-NEXT: %[[SHIFT:.*]] = cir.shift(left, %[[CAST]] : !u32i, %[[SHIFT_AMOUNT]] : !u32i) -> !u32i
-// CIR-NEXT: %[[RET_BITCAST:.*]] = cir.cast bitcast %[[RET:.*]] : !cir.ptr<!s32i> -> !cir.ptr<!u32i>
-// CIR-NEXT: cir.store %[[SHIFT]], %[[RET_BITCAST]] : !u32i, !cir.ptr<!u32i>
+// CIR-NEXT: %[[RES:.*]] = cir.cast integral %[[SHIFT]] : !u32i -> !s32i
+// CIR-NEXT: cir.store %[[RES]], %[[RET:.*]] : !s32i, !cir.ptr<!s32i>
// CIR-NEXT: %[[LOAD_RET:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i
// CIR-NEXT: cir.return %[[LOAD_RET]] : !s32i
// LLVM-LABEL: @bool_to_sat_accum
More information about the cfe-commits
mailing list