[llvm-branch-commits] [clang] [CIR] Match classic codegen for saturating fixed-point negation and padded conversions (PR #236702)

Henrich Lauko via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Nov 5 07:56:08 PST 2026


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

>From 85f92afebe9c8d060cd38b935d91bb03f89c1077 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 used fptoui and compared against 0 as unsigned, so the
clamp to 0 never fired; it now converts and clamps as signed, like
llvm::FixedPointBuilder. 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 (Opus 5.5).
---
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp    | 14 +++
 .../lib/CIR/CodeGen/CIRGenFixedPointBuilder.h | 18 ++--
 clang/test/CIR/CodeGen/fixed-point-arith.cpp  | 86 +++++++++++++++++++
 .../CIR/CodeGen/fixed-point-conversions.cpp   |  8 +-
 4 files changed, 116 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 35a7770fe1420b..90e944bd70f56e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -887,6 +887,20 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
     if (cir::isFPOrVectorOfFPType(operand.getType()))
       return builder.createOrFold<cir::FNegOp>(loc, operand);
 
+    // Emit saturating fixed-point negation as a saturating 0 - x, like classic
+    // codegen does.
+    if (e->getType()->isSaturatedFixedPointType()) {
+      BinOpInfo ops;
+      ops.lhs = builder.getNullValue(operand.getType(), loc);
+      ops.rhs = operand;
+      ops.loc = e->getSourceRange();
+      ops.fullType = ops.compType = e->getType();
+      ops.opcode = BO_Sub;
+      ops.fpFeatures = e->getFPFeaturesInEffect(cgf.getLangOpts());
+      ops.e = e;
+      return emitFixedPointBinOp(ops);
+    }
+
     // 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 2b1db364fa4677..187189ea24658e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
@@ -70,8 +70,11 @@ 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());
+    // Convert through a signed type when useSigned is set, so that padded
+    // unsigned destinations get a signed conversion and clamp like classic
+    // codegen.
+    cir::IntType resultTy =
+        cir::IntType::get(builder.getContext(), dstSema.getWidth(), useSigned);
 
     if (dstSema.isSaturated()) {
       result = builder.emitIntrinsicCallOp(
@@ -90,7 +93,9 @@ struct CIRGenFixedPointBuilder {
       result = builder.createSelect(loc, isNeg, zero, result);
     }
 
-    return result;
+    return builder.createIntCast(result, cir::IntType::get(builder.getContext(),
+                                                           dstSema.getWidth(),
+                                                           dstSema.isSigned()));
   }
 
   mlir::Value createFixedToInteger(mlir::Value src,
@@ -346,9 +351,10 @@ 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);
+      // Resize the integer part to get the final destination size. Unlike
+      // LLVM integers, CIR integers carry signedness, so also cast when only
+      // the signedness differs.
+      result = builder.createIntCast(result, dstIntTy);
     }
     return result;
   }
diff --git a/clang/test/CIR/CodeGen/fixed-point-arith.cpp b/clang/test/CIR/CodeGen/fixed-point-arith.cpp
index 485c2fab71c453..561efa4d3e35b2 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: %[[NEG:.*]] = 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: %[[NEG:.*]] = 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 llvm-branch-commits mailing list