[clang] [X86_32] fix weird edge case in vaarg. (PR #86388)

Longsheng Mou via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 23 03:16:39 PDT 2024


https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/86388

>From 8752d9019851bd231f1777d20391af60f0b4365c Mon Sep 17 00:00:00 2001
From: Longsheng Mou <moulongsheng at huawei.com>
Date: Sat, 23 Mar 2024 17:53:58 +0800
Subject: [PATCH] [X86_32] fix 0 sized struct case in vaarg.

struct SuperEmpty { struct{ int a[0];} b;};
Such 0 sized structs in c++ mode can not be ignored in i386 for
that c++ fields are never empty.But when EmitVAArg, its size is
0, so that va_list not increase.Maybe we can use direct in this
case.
---
 clang/lib/CodeGen/Targets/X86.cpp | 3 +++
 clang/test/CodeGenCXX/regparm.cpp | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/Targets/X86.cpp b/clang/lib/CodeGen/Targets/X86.cpp
index 1ec0f159ebcb8a..8e6e7c17452048 100644
--- a/clang/lib/CodeGen/Targets/X86.cpp
+++ b/clang/lib/CodeGen/Targets/X86.cpp
@@ -805,6 +805,9 @@ ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, CCState &State,
     if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
       return ABIArgInfo::getIgnore();
 
+    if (TI.Width == 0 && getContext().getLangOpts().CPlusPlus)
+      return ABIArgInfo::getDirect();
+
     llvm::LLVMContext &LLVMContext = getVMContext();
     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
     bool NeedsPadding = false;
diff --git a/clang/test/CodeGenCXX/regparm.cpp b/clang/test/CodeGenCXX/regparm.cpp
index 1fd471c2d0727b..559702a84c99e1 100644
--- a/clang/test/CodeGenCXX/regparm.cpp
+++ b/clang/test/CodeGenCXX/regparm.cpp
@@ -32,7 +32,7 @@ struct S3 {
   } a;
 };
 __attribute((regparm(2))) void foo4(S3 a, int b);
-// CHECK: declare void @_Z4foo42S3i(ptr noundef byval(%struct.S3) align 4, i32 inreg noundef)
+// CHECK: declare void @_Z4foo42S3i(%struct.anon, i32 inreg noundef)
 void bar3(S3 a, int b) {
   foo4(a, b);
 }



More information about the cfe-commits mailing list