https://github.com/mugiwaraluffy56 updated https://github.com/llvm/llvm-project/pull/179456
>From 6bac61c7257f58de90e8e44bfdad8e44e41840e1 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 | 10 +++++++++- clang/test/CodeGen/ext-int.c | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index fbae2433ec278..b208fe0ecc550 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1248,7 +1248,15 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, ReturnValue = Address(Addr, ConvertType(RetTy), CGM.getNaturalTypeAlignment(RetTy), KnownNonNull); } else { - ReturnValue = CreateIRTemp(RetTy, "retval"); + // For CoerceAndExpand returns where IR and memory types differ + // (e.g., _BitInt(121) has IR type i121 but memory type i128), + // use CreateMemTemp so the alloca has the memory type. This ensures + // proper coercion when the epilog reads it as the coerced type. + if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::CoerceAndExpand && + 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
