https://github.com/npmania updated https://github.com/llvm/llvm-project/pull/239969
>From f131cfea960a0ff4f09d57cc0674359dfc8c96e7 Mon Sep 17 00:00:00 2001 From: Jaeyong Lee <[email protected]> Date: Sat, 7 Nov 2026 22:42:47 +0900 Subject: [PATCH 1/2] [IR][Clang] Guard full-width shift in FixedPointBuilder --- .../CIR/CodeGen/fixed-point-conversions.cpp | 18 ++++--- clang/test/Frontend/fixed_point_conversions.c | 54 +++++++++++++++++++ llvm/include/llvm/IR/FixedPointBuilder.h | 10 ++-- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/clang/test/CIR/CodeGen/fixed-point-conversions.cpp b/clang/test/CIR/CodeGen/fixed-point-conversions.cpp index fa7c6a28dfc6db..e681049d34b7e5 100644 --- a/clang/test/CIR/CodeGen/fixed-point-conversions.cpp +++ b/clang/test/CIR/CodeGen/fixed-point-conversions.cpp @@ -1,9 +1,9 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffixed-point -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -ffixed-point -fclangir -emit-llvm %s -o %t-cir.ll -// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM +// 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 +// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG extern "C" { @@ -658,9 +658,10 @@ int accum_to_int(_Accum a) { // CIR-NEXT: cir.return %[[LOAD_RET]] : !s32i // LLVM-LABEL: @ufract_to_int // LLVM: %[[LOAD_ARG:.*]] = load i16, ptr %{{.*}}, align 2 -// LLVM: %[[SHIFT:.*]] = lshr i16 %[[LOAD_ARG]], 16 -// LLVM: %[[CAST:.*]] = zext i16 %[[SHIFT]] to i32 -// LLVM: ret i32 %{{.*}} +// LLVMCIR: %[[SHIFT:.*]] = lshr i16 %[[LOAD_ARG]], 16 +// LLVMCIR: %[[CAST:.*]] = zext i16 %[[SHIFT]] to i32 +// LLVMCIR: ret i32 %{{.*}} +// OGCG: ret i32 0 int ufract_to_int(unsigned _Fract f) { return f; } @@ -794,9 +795,10 @@ unsigned int accum_to_uint(_Accum a) { // CIR-NEXT: cir.return %[[LOAD_RET]] : !u32i // LLVM-LABEL: @ufract_to_uint // LLVM: %[[LOAD_ARG:.*]] = load i16, ptr %{{.*}}, align 2 -// LLVM: %[[SHIFT:.*]] = lshr i16 %[[LOAD_ARG]], 16 -// LLVM: %[[CAST:.*]] = zext i16 %[[SHIFT]] to i32 -// LLVM: ret i32 %{{.*}} +// LLVMCIR: %[[SHIFT:.*]] = lshr i16 %[[LOAD_ARG]], 16 +// LLVMCIR: %[[CAST:.*]] = zext i16 %[[SHIFT]] to i32 +// LLVMCIR: ret i32 %{{.*}} +// OGCG: ret i32 0 unsigned int ufract_to_uint(unsigned _Fract f) { return f; } diff --git a/clang/test/Frontend/fixed_point_conversions.c b/clang/test/Frontend/fixed_point_conversions.c index 61c5c1354d3287..d48851ad62b160 100644 --- a/clang/test/Frontend/fixed_point_conversions.c +++ b/clang/test/Frontend/fixed_point_conversions.c @@ -19,7 +19,9 @@ unsigned long _Accum ula; short _Fract sf; _Fract f; long _Fract lf; +unsigned short _Fract usf; unsigned _Fract uf; +unsigned long _Fract ulf; _Sat short _Accum sat_sa; _Sat _Accum sat_a; @@ -540,6 +542,58 @@ void fix_int2(void) { i = usa; } +// SIGNED-LABEL: @fix_int3( +// SIGNED-NEXT: entry: +// SIGNED-NEXT: [[TMP0:%.*]] = load i8, ptr @usf, align 1 +// SIGNED-NEXT: store i32 0, ptr @i, align 4 +// SIGNED-NEXT: ret void +// +// UNSIGNED-LABEL: @fix_int3( +// UNSIGNED-NEXT: entry: +// UNSIGNED-NEXT: [[TMP0:%.*]] = load i8, ptr @usf, align 1 +// UNSIGNED-NEXT: [[DOWNSCALE:%.*]] = lshr i8 [[TMP0]], 7 +// UNSIGNED-NEXT: [[RESIZE:%.*]] = zext i8 [[DOWNSCALE]] to i32 +// UNSIGNED-NEXT: store i32 [[RESIZE]], ptr @i, align 4 +// UNSIGNED-NEXT: ret void +// +void fix_int3(void) { + i = usf; +} + +// SIGNED-LABEL: @fix_int4( +// SIGNED-NEXT: entry: +// SIGNED-NEXT: [[TMP0:%.*]] = load i16, ptr @uf, align 2 +// SIGNED-NEXT: store i32 0, ptr @i, align 4 +// SIGNED-NEXT: ret void +// +// UNSIGNED-LABEL: @fix_int4( +// UNSIGNED-NEXT: entry: +// UNSIGNED-NEXT: [[TMP0:%.*]] = load i16, ptr @uf, align 2 +// UNSIGNED-NEXT: [[DOWNSCALE:%.*]] = lshr i16 [[TMP0]], 15 +// UNSIGNED-NEXT: [[RESIZE:%.*]] = zext i16 [[DOWNSCALE]] to i32 +// UNSIGNED-NEXT: store i32 [[RESIZE]], ptr @i, align 4 +// UNSIGNED-NEXT: ret void +// +void fix_int4(void) { + i = uf; +} + +// SIGNED-LABEL: @fix_int5( +// SIGNED-NEXT: entry: +// SIGNED-NEXT: [[TMP0:%.*]] = load i32, ptr @ulf, align 4 +// SIGNED-NEXT: store i32 0, ptr @i, align 4 +// SIGNED-NEXT: ret void +// +// UNSIGNED-LABEL: @fix_int5( +// UNSIGNED-NEXT: entry: +// UNSIGNED-NEXT: [[TMP0:%.*]] = load i32, ptr @ulf, align 4 +// UNSIGNED-NEXT: [[DOWNSCALE:%.*]] = lshr i32 [[TMP0]], 31 +// UNSIGNED-NEXT: store i32 [[DOWNSCALE]], ptr @i, align 4 +// UNSIGNED-NEXT: ret void +// +void fix_int5(void) { + i = ulf; +} // CHECK-LABEL: @int_fix1( // CHECK-NEXT: entry: diff --git a/llvm/include/llvm/IR/FixedPointBuilder.h b/llvm/include/llvm/IR/FixedPointBuilder.h index 1a22dd6b609367..ae2e41b154241b 100644 --- a/llvm/include/llvm/IR/FixedPointBuilder.h +++ b/llvm/include/llvm/IR/FixedPointBuilder.h @@ -60,9 +60,13 @@ template <class IRBuilderTy> class FixedPointBuilder { Result = B.CreateSelect(IsNegative, Rounded, Result); } - Result = SrcIsSigned - ? B.CreateAShr(Result, SrcScale - DstScale, "downscale") - : B.CreateLShr(Result, SrcScale - DstScale, "downscale"); + unsigned Shift = SrcScale - DstScale; + if (Shift < ResultWidth) { + Result = SrcIsSigned ? B.CreateAShr(Result, Shift, "downscale") + : B.CreateLShr(Result, Shift, "downscale"); + } else { + Result = Constant::getNullValue(Result->getType()); + } } if (!DstSema.isSaturated()) { >From 5ffede85a511f59d6560003e6cd838fda5b9e9d0 Mon Sep 17 00:00:00 2001 From: Jaeyong Lee <[email protected]> Date: Tue, 10 Nov 2026 09:50:48 +0900 Subject: [PATCH 2/2] Assert Shift <= ResultWidth in FixedPointBuilder --- llvm/include/llvm/IR/FixedPointBuilder.h | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm/include/llvm/IR/FixedPointBuilder.h b/llvm/include/llvm/IR/FixedPointBuilder.h index ae2e41b154241b..0fd4e5a4435bde 100644 --- a/llvm/include/llvm/IR/FixedPointBuilder.h +++ b/llvm/include/llvm/IR/FixedPointBuilder.h @@ -61,6 +61,7 @@ template <class IRBuilderTy> class FixedPointBuilder { } unsigned Shift = SrcScale - DstScale; + assert(Shift <= ResultWidth && "Shift cannot exceed result width"); if (Shift < ResultWidth) { Result = SrcIsSigned ? B.CreateAShr(Result, Shift, "downscale") : B.CreateLShr(Result, Shift, "downscale"); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
