[clang] [CIR] Guard full-width shift in CIRGenFixedPointBuilder (PR #244504)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 19 21:07:35 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jaeyong Lee (npmania)
<details>
<summary>Changes</summary>
Casting unsigned _Fract to an integer shifts by its full width, since scale == width.
Follows https://github.com/llvm/llvm-project/pull/239969.
---
Full diff: https://github.com/llvm/llvm-project/pull/244504.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h (+6-1)
- (modified) clang/test/CIR/CodeGen/fixed-point-conversions.cpp (+10-14)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
index 71854ff872406..e8b03ba3e9679 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFixedPointBuilder.h
@@ -295,7 +295,12 @@ struct CIRGenFixedPointBuilder {
mlir::Value rounded = builder.createAdd(loc, result, lowBits);
result = builder.createSelect(loc, isNegative, rounded, result);
}
- result = builder.createShiftRight(loc, result, srcScale - dstScale);
+ unsigned shift = srcScale - dstScale;
+ assert(shift <= resultWidth && "Shift cannot exceed result width");
+ if (shift < resultWidth)
+ result = builder.createShiftRight(loc, result, shift);
+ else
+ result = builder.getNullValue(result.getType(), loc);
}
cir::IntType dstIntTy = getIntType(dstSema);
diff --git a/clang/test/CIR/CodeGen/fixed-point-conversions.cpp b/clang/test/CIR/CodeGen/fixed-point-conversions.cpp
index 62ba6c55877bd..1060a15b29013 100644
--- a/clang/test/CIR/CodeGen/fixed-point-conversions.cpp
+++ b/clang/test/CIR/CodeGen/fixed-point-conversions.cpp
@@ -650,17 +650,15 @@ int accum_to_int(_Accum a) {
// CIR-LABEL: cir.func{{.*}} @ufract_to_int
// CIR: %[[ARG:.*]] = cir.alloca "f" align(2) init : !cir.ptr<!u16i>
// CIR: %[[LOAD_ARG:.*]] = cir.load align(2) %[[ARG]] : !cir.ptr<!u16i>, !u16i
-// CIR-NEXT: %[[SHIFT_AMOUNT:.*]] = cir.const #cir.int<16> : !u16i
-// CIR-NEXT: %[[SHIFT:.*]] = cir.shift(right, %[[LOAD_ARG]] : !u16i, %[[SHIFT_AMOUNT]] : !u16i) -> !u16i
-// CIR-NEXT: %[[CAST:.*]] = cir.cast integral %[[SHIFT]] : !u16i -> !s32i
-// CIR-NEXT: cir.store %[[CAST]], %[[RET:.*]] : !s32i, !cir.ptr<!s32i>
+// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-NEXT: cir.store %[[ZERO]], %[[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: @ufract_to_int
// LLVM: %[[LOAD_ARG:.*]] = load i16, ptr %{{.*}}, align 2
-// LLVMCIR: %[[SHIFT:.*]] = lshr i16 %[[LOAD_ARG]], 16
-// LLVMCIR: %[[CAST:.*]] = zext i16 %[[SHIFT]] to i32
-// LLVMCIR: ret i32 %{{.*}}
+// LLVMCIR: store i32 0, ptr %[[RET_ADDR:.*]], align 4
+// LLVMCIR: %[[RET:.*]] = load i32, ptr %[[RET_ADDR]], align 4
+// LLVMCIR: ret i32 %[[RET]]
// OGCG: ret i32 0
int ufract_to_int(unsigned _Fract f) {
return f;
@@ -787,17 +785,15 @@ unsigned int accum_to_uint(_Accum a) {
// CIR-LABEL: cir.func{{.*}} @ufract_to_uint
// CIR: %[[ARG:.*]] = cir.alloca "f" align(2) init : !cir.ptr<!u16i>
// CIR: %[[LOAD_ARG:.*]] = cir.load align(2) %[[ARG]] : !cir.ptr<!u16i>, !u16i
-// CIR-NEXT: %[[SHIFT_AMOUNT:.*]] = cir.const #cir.int<16> : !u16i
-// CIR-NEXT: %[[SHIFT:.*]] = cir.shift(right, %[[LOAD_ARG]] : !u16i, %[[SHIFT_AMOUNT]] : !u16i) -> !u16i
-// CIR-NEXT: %[[CAST:.*]] = cir.cast integral %[[SHIFT]] : !u16i -> !u32i
-// CIR-NEXT: cir.store %[[CAST]], %[[RET:.*]] : !u32i, !cir.ptr<!u32i>
+// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !u32i
+// CIR-NEXT: cir.store %[[ZERO]], %[[RET:.*]] : !u32i, !cir.ptr<!u32i>
// CIR-NEXT: %[[LOAD_RET:.*]] = cir.load %[[RET]] : !cir.ptr<!u32i>, !u32i
// CIR-NEXT: cir.return %[[LOAD_RET]] : !u32i
// LLVM-LABEL: @ufract_to_uint
// LLVM: %[[LOAD_ARG:.*]] = load i16, ptr %{{.*}}, align 2
-// LLVMCIR: %[[SHIFT:.*]] = lshr i16 %[[LOAD_ARG]], 16
-// LLVMCIR: %[[CAST:.*]] = zext i16 %[[SHIFT]] to i32
-// LLVMCIR: ret i32 %{{.*}}
+// LLVMCIR: store i32 0, ptr %[[RET_ADDR:.*]], align 4
+// LLVMCIR: %[[RET:.*]] = load i32, ptr %[[RET_ADDR]], align 4
+// LLVMCIR: ret i32 %[[RET]]
// OGCG: ret i32 0
unsigned int ufract_to_uint(unsigned _Fract f) {
return f;
``````````
</details>
https://github.com/llvm/llvm-project/pull/244504
More information about the cfe-commits
mailing list