[clang] [X86_64] fix empty structure vaarg in c++ (PR #77907)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 4 22:37:32 PST 2024
https://github.com/hstk30-hw updated https://github.com/llvm/llvm-project/pull/77907
>From aa02306347026829c621a637124fae900de18cbc Mon Sep 17 00:00:00 2001
From: Longsheng Mou <moulongsheng at huawei.com>
Date: Fri, 12 Jan 2024 18:24:08 +0800
Subject: [PATCH] [X86_64] fix empty structure vaarg in c++
SizeInBytes of empty structure is 0 in C, while 1 in C++.
And empty structure argument of the function is ignored
in X86_64 backend.As a result, the value of variable
arguments in C++ is incorrect.So we should just make a
temporary and return its address.
---
clang/lib/CodeGen/Targets/X86.cpp | 10 ++++++++++
clang/test/CodeGenCXX/x86_64-vaarg.cpp | 12 ++++++++++++
2 files changed, 22 insertions(+)
create mode 100644 clang/test/CodeGenCXX/x86_64-vaarg.cpp
diff --git a/clang/lib/CodeGen/Targets/X86.cpp b/clang/lib/CodeGen/Targets/X86.cpp
index d053f41ab168f5..2baa5eb98347c8 100644
--- a/clang/lib/CodeGen/Targets/X86.cpp
+++ b/clang/lib/CodeGen/Targets/X86.cpp
@@ -3014,6 +3014,16 @@ Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
/*isNamedArg*/false);
+ // Empty records are ignored for parameter passing purposes.
+ if (AI.isIgnore()) {
+ CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
+ VAListAddr = CGF.Builder.CreateStructGEP(VAListAddr, 2);
+ llvm::Value *Load = CGF.Builder.CreateLoad(VAListAddr);
+ llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, 0);
+ Load = CGF.Builder.CreateGEP(CGF.Int8Ty, Load, Offset);
+ return Address(Load, CGF.ConvertTypeForMem(Ty), Align);
+ }
+
// AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
// in the registers. If not go to step 7.
if (!neededInt && !neededSSE)
diff --git a/clang/test/CodeGenCXX/x86_64-vaarg.cpp b/clang/test/CodeGenCXX/x86_64-vaarg.cpp
new file mode 100644
index 00000000000000..8c00fc153519dc
--- /dev/null
+++ b/clang/test/CodeGenCXX/x86_64-vaarg.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+typedef struct { struct {} a; } empty;
+
+// CHECK-LABEL: define{{.*}} void @_Z17empty_record_testv()
+empty empty_record_test(void) {
+// CHECK: [[ADDR0:%[a-z._0-9]+]] = getelementptr inbounds %struct.__va_list_tag, ptr %arraydecay, i32 0, i32 2
+// CHECK-NEXT: [[ADDR1:%[a-z._0-9]+]] = load ptr, ptr [[ADDR0]], align 8
+// CHECK-NEXT: [[ADDR2:%[a-z._0-9]+]] = getelementptr i8, ptr [[ADDR1]], i32 0
+ __builtin_va_list list;
+ return __builtin_va_arg(list, empty);
+}
More information about the cfe-commits
mailing list