https://github.com/mugiwaraluffy56 created
https://github.com/llvm/llvm-project/pull/179456
## Summary
- Use `CreateMemTemp` instead of `CreateIRTemp` for return value allocas
- Ensures proper memory type for _BitInt types that have different IR and
memory representations
- For _BitInt(121), the IR type is i121 but memory type is i128, fixing the
coercion to {i64, i64}
Fixes #179448
## Test plan
- Added test case for _BitInt(121) return value in ext-int.c
>From 15da3fca96e4e3a475a89ae67cc0c90d808ad521 Mon Sep 17 00:00:00 2001
From: puneeth_aditya_5656 <[email protected]>
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 | 4 +++-
clang/test/CodeGen/ext-int.c | 9 +++++++++
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp
b/clang/lib/CodeGen/CodeGenFunction.cpp
index fbae2433ec278..8519ff1dd7efd 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1248,7 +1248,9 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
QualType RetTy,
ReturnValue = Address(Addr, ConvertType(RetTy),
CGM.getNaturalTypeAlignment(RetTy), KnownNonNull);
} else {
- ReturnValue = CreateIRTemp(RetTy, "retval");
+ // Use CreateMemTemp to ensure proper memory representation for types
+ // like _BitInt that may have different IR and memory types.
+ ReturnValue = CreateMemTemp(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
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits