[clang] [clang] Fix __builtin_mul_overflow for big _BitInts (PR #145497)
Mariya Podchishchaeva via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 24 04:14:06 PDT 2025
https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/145497
For long enough _BitInt types we use different types for memory, storing-loading and 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
>From 8397b9438b6bdf9b63ac9a925ab374acf3e2fdd5 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Tue, 24 Jun 2025 04:07:56 -0700
Subject: [PATCH] [clang] Fix __builtin_mul_overflow for big _BitInts
For long enough _BitInt types we use different types for
memory, storing-loading and operations. Makes sure it is correct for
__builtin_mul_overflow cases.
Fixes https://github.com/llvm/llvm-project/issues/144771
---
clang/lib/CodeGen/CGBuiltin.cpp | 2 +-
clang/test/CodeGen/builtins-overflow.c | 12 ++++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
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