[clang] ad87d95 - [clang] Fix __builtin_mul_overflow for big _BitInts (#145497)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 25 01:57:52 PDT 2025


Author: Mariya Podchishchaeva
Date: 2025-06-25T10:57:48+02:00
New Revision: ad87d951c94f045389b2e424c2d2a1505c593b85

URL: https://github.com/llvm/llvm-project/commit/ad87d951c94f045389b2e424c2d2a1505c593b85
DIFF: https://github.com/llvm/llvm-project/commit/ad87d951c94f045389b2e424c2d2a1505c593b85.diff

LOG: [clang] Fix __builtin_mul_overflow for big _BitInts (#145497)

For long enough _BitInt types we use different types for memory,
storing-loading and other operations. Makes sure it is correct for mixed
sign __builtin_mul_overflow cases. Using pointer element type as a
result type doesn't work, because it will be "in-memory" type which is
usually bigger than "operations" type and that caused crashes because
clang was trying to emit trunc to a bigger type.

Fixes https://github.com/llvm/llvm-project/issues/144771

Added: 
    

Modified: 
    clang/lib/CodeGen/CGBuiltin.cpp
    clang/test/CodeGen/builtins-overflow.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2c011a9519860..2a8722221f24b 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -2356,7 +2356,7 @@ EmitCheckedMixedSignMultiply(CodeGenFunction &CGF, const clang::Expr *Op1,
   llvm::Type *OpTy = Signed->getType();
   llvm::Value *Zero = llvm::Constant::getNullValue(OpTy);
   Address ResultPtr = CGF.EmitPointerWithAlignment(ResultArg);
-  llvm::Type *ResTy = ResultPtr.getElementType();
+  llvm::Type *ResTy = CGF.getTypes().ConvertType(ResultQTy);
   unsigned OpWidth = std::max(Op1Info.Width, Op2Info.Width);
 
   // Take the absolute value of the signed operand.

diff  --git a/clang/test/CodeGen/builtins-overflow.c b/clang/test/CodeGen/builtins-overflow.c
index 7c524723f76e8..0e04191b9e2ac 100644
--- a/clang/test/CodeGen/builtins-overflow.c
+++ b/clang/test/CodeGen/builtins-overflow.c
@@ -604,3 +604,15 @@ long long test_mixed_sign_mul_overflow_extend_unsigned(int x, unsigned y) {
     return LongLongErrorCode;
   return result;
 }
+
+_BitInt(65) test_mixed_sign_mul_overflow_bitint(unsigned _BitInt(65) y, _BitInt(119) a) {
+// CHECK: call { i119, i1 } @llvm.umul.with.overflow.i119
+// CHECK: select i1 %{{.*}}, i119 %{{.*}}, i119 %{{.*}}
+// CHECK: trunc i119
+// CHECK: zext i65
+// CHECK: store
+  unsigned _BitInt(65) result;
+  if (__builtin_mul_overflow(a, y, &result))
+    return LongLongErrorCode;
+  return result;
+}


        


More information about the cfe-commits mailing list