https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/236702
>From 127f19b914fd94c3ff3c0487d0d38827e3444be9 Mon Sep 17 00:00:00 2001 From: Henrich Lauko <[email protected]> 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/CIRGenExprScalar.cpp | 14 +++ .../lib/CIR/CodeGen/CIRGenFixedPointBuilder.h | 23 +++-- clang/test/CIR/CodeGen/fixed-point-arith.cpp | 86 +++++++++++++++++++ .../CIR/CodeGen/fixed-point-conversions.cpp | 8 +- 4 files changed, 115 insertions(+), 16 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 665f17469eec94..1d32076dc08a10 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); + // Negating the minimum of a saturated type must saturate, so go through + // the fixed-point subtraction like classic codegen. + if (e->getSubExpr()->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 97cf78157b8267..7eacdf0ed20c6d 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,15 +68,14 @@ 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); + loc, opIntTy.isSigned() ? "fptosi.sat" : "fptoui.sat", opIntTy, + 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 @@ -86,7 +83,9 @@ struct CIRGenFixedPointBuilder { if (dstSema.isSaturated() && dstSema.hasUnsignedPadding()) result = clampNegativeToZero(result); - return result; + cir::IntType dstIntTy = cir::IntType::get( + builder.getContext(), dstSema.getWidth(), dstSema.isSigned()); + return builder.createIntCast(result, dstIntTy); } mlir::Value createFixedToInteger(mlir::Value src, @@ -344,9 +343,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; } 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 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
