Skip to content

Commit 651e5ae

Browse files
committed
[MS] Fix passing aligned records by value in some cases
It's not exactly clear what the meaning of TypeInfo::AlignRequirement is, so go directly to the ASTRecordLayout for records and check the required alignment there. Compare that number with the stack alignment value of 4. This fixes cases when the alignment attribute does not appear directly on the record [1], or when the attribute on the record is underaligned [2]. [1]: `struct Foo { int __declspec(align(16)) x; };` [2]: `struct __declspec(align(1)) Bar { int x; };` Fixes https://llvm.org/pr63257 Differential Revision: https://reviews.llvm.org/D152752
1 parent a5cd198 commit 651e5ae

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,9 +1893,21 @@ ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
18931893
llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
18941894

18951895
// Pass over-aligned aggregates on Windows indirectly. This behavior was
1896-
// added in MSVC 2015.
1897-
if (IsWin32StructABI && TI.isAlignRequired() && TI.Align > 32)
1898-
return getIndirectResult(Ty, /*ByVal=*/false, State);
1896+
// added in MSVC 2015. Use the required alignment from the record layout,
1897+
// since that may be less than the regular type alignment, and types with
1898+
// required alignment of less than 4 bytes are not passed indirectly.
1899+
if (IsWin32StructABI) {
1900+
unsigned AlignInBits = 0;
1901+
if (RT) {
1902+
const ASTRecordLayout &Layout =
1903+
getContext().getASTRecordLayout(RT->getDecl());
1904+
AlignInBits = getContext().toBits(Layout.getRequiredAlignment());
1905+
} else if (TI.isAlignRequired()) {
1906+
AlignInBits = TI.Align;
1907+
}
1908+
if (AlignInBits > 32)
1909+
return getIndirectResult(Ty, /*ByVal=*/false, State);
1910+
}
18991911

19001912
// Expand small (<= 128-bit) record types when we know that the stack layout
19011913
// of those arguments will match the struct. This is important because the

clang/test/CodeGen/X86/x86_32-arguments-win32.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -w -triple i386-pc-win32 -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -fms-extensions -w -triple i386-pc-win32 -emit-llvm -o - %s | FileCheck %s
22

33
// CHECK-LABEL: define dso_local i64 @f1_1()
44
// CHECK-LABEL: define dso_local void @f1_2(i32 %a0.0, i32 %a0.1)
@@ -90,3 +90,41 @@ void __fastcall fastcall_indirect_vec(__m128 x, __m128 y, __m128 z, __m128 w, in
9090
gv128 = x + y + z + w + q;
9191
}
9292
// CHECK-LABEL: define dso_local x86_fastcallcc void @"\01@fastcall_indirect_vec@84"(<4 x float> inreg noundef %x, <4 x float> inreg noundef %y, <4 x float> inreg noundef %z, ptr inreg noundef %0, i32 inreg noundef %edx, ptr noundef %1)
93+
94+
struct __declspec(align(1)) Align1 { unsigned long long x; };
95+
struct __declspec(align(4)) Align4 { unsigned long long x; };
96+
struct __declspec(align(8)) Align8 { unsigned long long x; };
97+
void receive_align1(struct Align1 o);
98+
void receive_align4(struct Align4 o);
99+
void receive_align8(struct Align8 o);
100+
void pass_underaligned_record() {
101+
struct Align1 a1;
102+
receive_align1(a1);
103+
struct Align4 a4;
104+
receive_align4(a4);
105+
struct Align8 a8;
106+
receive_align8(a8);
107+
}
108+
// CHECK-LABEL: define dso_local void @pass_underaligned_record()
109+
// CHECK: call void @receive_align1(i64 {{[^,)]*}})
110+
// CHECK: call void @receive_align4(i64 {{[^,)]*}})
111+
// CHECK: call void @receive_align8(ptr {{[^,)]*}})
112+
113+
struct FieldAlign1 { unsigned long long __declspec(align(1)) x; };
114+
struct FieldAlign4 { unsigned long long __declspec(align(4)) x; };
115+
struct FieldAlign8 { unsigned long long __declspec(align(8)) x; };
116+
void receive_falign1(struct FieldAlign1 o);
117+
void receive_falign4(struct FieldAlign4 o);
118+
void receive_falign8(struct FieldAlign8 o);
119+
void pass_underaligned_record_field() {
120+
struct FieldAlign1 a1;
121+
receive_falign1(a1);
122+
struct FieldAlign4 a4;
123+
receive_falign4(a4);
124+
struct FieldAlign8 a8;
125+
receive_falign8(a8);
126+
}
127+
// CHECK-LABEL: define dso_local void @pass_underaligned_record_field()
128+
// CHECK: call void @receive_falign1(i64 {{[^,)]*}})
129+
// CHECK: call void @receive_falign4(i64 {{[^,)]*}})
130+
// CHECK: call void @receive_falign8(ptr {{[^,)]*}})

0 commit comments

Comments
 (0)