Skip to content

Commit ad87d95

Browse files
authored
[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 #144771
1 parent ecff028 commit ad87d95

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,7 @@ EmitCheckedMixedSignMultiply(CodeGenFunction &CGF, const clang::Expr *Op1,
23562356
llvm::Type *OpTy = Signed->getType();
23572357
llvm::Value *Zero = llvm::Constant::getNullValue(OpTy);
23582358
Address ResultPtr = CGF.EmitPointerWithAlignment(ResultArg);
2359-
llvm::Type *ResTy = ResultPtr.getElementType();
2359+
llvm::Type *ResTy = CGF.getTypes().ConvertType(ResultQTy);
23602360
unsigned OpWidth = std::max(Op1Info.Width, Op2Info.Width);
23612361

23622362
// Take the absolute value of the signed operand.

clang/test/CodeGen/builtins-overflow.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,15 @@ long long test_mixed_sign_mul_overflow_extend_unsigned(int x, unsigned y) {
604604
return LongLongErrorCode;
605605
return result;
606606
}
607+
608+
_BitInt(65) test_mixed_sign_mul_overflow_bitint(unsigned _BitInt(65) y, _BitInt(119) a) {
609+
// CHECK: call { i119, i1 } @llvm.umul.with.overflow.i119
610+
// CHECK: select i1 %{{.*}}, i119 %{{.*}}, i119 %{{.*}}
611+
// CHECK: trunc i119
612+
// CHECK: zext i65
613+
// CHECK: store
614+
unsigned _BitInt(65) result;
615+
if (__builtin_mul_overflow(a, y, &result))
616+
return LongLongErrorCode;
617+
return result;
618+
}

0 commit comments

Comments
 (0)