Skip to content

Commit 4461b69

Browse files
authored
[X86_32][C++] fix 0 sized struct case in vaarg. (#86388)
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 just Ignore this kind of arguments, like X86_64 did. Fix #86385.
1 parent c5f1395 commit 4461b69

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

clang/lib/CodeGen/Targets/X86.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, CCState &State,
795795
if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
796796
return ABIArgInfo::getIgnore();
797797

798+
// Ignore 0 sized structs.
799+
if (TI.Width == 0)
800+
return ABIArgInfo::getIgnore();
801+
798802
llvm::LLVMContext &LLVMContext = getVMContext();
799803
llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
800804
bool NeedsPadding = false;

clang/test/CodeGenCXX/regparm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct S3 {
3232
} a;
3333
};
3434
__attribute((regparm(2))) void foo4(S3 a, int b);
35-
// CHECK: declare void @_Z4foo42S3i(ptr noundef byval(%struct.S3) align 4, i32 inreg noundef)
35+
// CHECK: declare void @_Z4foo42S3i(i32 inreg noundef)
3636
void bar3(S3 a, int b) {
3737
foo4(a, b);
3838
}

clang/test/CodeGenCXX/x86_32-vaarg.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,25 @@ empty empty_record_test(int z, ...) {
1818
__builtin_va_start(list, z);
1919
return __builtin_va_arg(list, empty);
2020
}
21+
22+
typedef struct {
23+
struct {
24+
int a[0];
25+
} b;
26+
} SortOfEmpty;
27+
28+
// CHECK-LABEL: @_Z18test_sort_of_emptyiz(
29+
// CHECK-NEXT: entry:
30+
// CHECK-NEXT: [[RESULT_PTR:%.*]] = alloca ptr, align 4
31+
// CHECK-NEXT: [[Z_ADDR:%.*]] = alloca i32, align 4
32+
// CHECK-NEXT: [[LIST:%.*]] = alloca ptr, align 4
33+
// CHECK-NEXT: store ptr [[AGG_RESULT:%.*]], ptr [[RESULT_PTR]], align 4
34+
// CHECK-NEXT: store i32 [[Z:%.*]], ptr [[Z_ADDR]], align 4
35+
// CHECK-NEXT: call void @llvm.va_start.p0(ptr [[LIST]])
36+
// CHECK-NEXT: ret void
37+
//
38+
SortOfEmpty test_sort_of_empty(int z, ...) {
39+
__builtin_va_list list;
40+
__builtin_va_start(list, z);
41+
return __builtin_va_arg(list, SortOfEmpty);
42+
}

0 commit comments

Comments
 (0)