[clang] [clang][CodeGen] Fix _BitInt return value miscompile (PR #179456)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 4 04:57:35 PST 2026
https://github.com/mugiwaraluffy56 updated https://github.com/llvm/llvm-project/pull/179456
>From dca58d0ddf24a91b9c5b3a0cc7c147fda2df3e7f Mon Sep 17 00:00:00 2001
From: puneeth_aditya_5656 <myakampuneeth at gmail.com>
Date: Tue, 3 Feb 2026 18:59:10 +0530
Subject: [PATCH] [clang][CodeGen] Fix _BitInt return value miscompile
Use CreateMemTemp instead of CreateIRTemp for the return value alloca
to ensure proper memory representation for types like _BitInt that have
different IR and memory types.
For _BitInt(121), ConvertType returns i121 but ConvertTypeForMem returns
i128. When the return value is coerced to {i64, i64} for the ABI, storing
i121 and loading {i64, i64} would leave undefined bits in the result.
Using the memory type (i128) ensures all 128 bits are properly initialized.
Fixes #179448
---
clang/lib/CodeGen/CodeGenFunction.cpp | 8 +++++++-
clang/test/CodeGen/ext-int.c | 9 +++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index fbae2433ec278..c578a8ae422a7 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1248,7 +1248,13 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
ReturnValue = Address(Addr, ConvertType(RetTy),
CGM.getNaturalTypeAlignment(RetTy), KnownNonNull);
} else {
- ReturnValue = CreateIRTemp(RetTy, "retval");
+ // Use CreateMemTemp for types where IR and memory representations differ
+ // (e.g., _BitInt(121) has IR type i121 but memory type i128).
+ // This ensures proper coercion when the return value is converted for ABI.
+ if (ConvertType(RetTy) != ConvertTypeForMem(RetTy))
+ ReturnValue = CreateMemTemp(RetTy, "retval");
+ else
+ ReturnValue = CreateIRTemp(RetTy, "retval");
// Tell the epilog emitter to autorelease the result. We do this
// now so that various specialized functions can suppress it
diff --git a/clang/test/CodeGen/ext-int.c b/clang/test/CodeGen/ext-int.c
index a12b11adbf00d..319bbe86558e8 100644
--- a/clang/test/CodeGen/ext-int.c
+++ b/clang/test/CodeGen/ext-int.c
@@ -252,4 +252,13 @@ void bitField() {
// LIN64: store i64 %bf.set4, ptr %s1, align 8
}
+// GH#179448: Ensure the return value alloca uses the memory type (i128)
+// instead of the IR type (i121) to avoid miscompilation when coercing
+// the return value to {i64, i64}.
+// LIN64: define {{.*}}{ i64, i64 } @returnBitInt121(i64 {{.*}}, i64 {{.*}})
+// LIN64: %retval = alloca i128, align 8
+_BitInt(121) returnBitInt121(_BitInt(121) a) {
+ return a;
+}
+
#endif
More information about the cfe-commits
mailing list