Skip to content

Commit 6fdaea5

Browse files
authored
Merge pull request #73405 from drexin/wip-127279770
[IRGen] Fix alignment for imported C types in CVW
2 parents 1096a05 + c1d732a commit 6fdaea5

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

lib/IRGen/TypeLayout.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,10 @@ AlignedGroupEntry::fixedAlignment(IRGenModule &IGM) const {
16551655
if (_fixedAlignment.has_value())
16561656
return *_fixedAlignment;
16571657

1658+
if (fixedTypeInfo) {
1659+
return *(_fixedAlignment = (*fixedTypeInfo)->getFixedAlignment());
1660+
}
1661+
16581662
Alignment currentAlignment = Alignment(
16591663
std::max((Alignment::int_type)1, minimumAlignment));
16601664
for (auto *entry : entries) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#ifndef SWIFT_TEST_CTYPES_H
22
#define SWIFT_TEST_CTYPES_H
33

4+
#include <stdint.h>
5+
46
struct BigAlignment {
57
_Alignas(16) float foo[4];
68
char b;
79
};
810

11+
#pragma pack(push, 4)
12+
struct UnderAligned {
13+
int64_t bar;
14+
};
15+
#pragma pack(pop)
16+
917
#endif

test/Interpreter/Inputs/layout_string_witnesses_types.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public struct CTypeAligned {
4949
}
5050
}
5151

52+
public struct CTypeUnderAligned {
53+
let w: Int32 = 0
54+
let x: UnderAligned? = UnderAligned()
55+
let y: SimpleClass
56+
57+
public init(_ y: SimpleClass) {
58+
self.y = y
59+
}
60+
}
61+
5262
public struct GenericStruct<T> {
5363
let x: Int = 0
5464
let y: T

test/Interpreter/layout_string_witnesses_static.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,49 @@ func testCTypeAligned() {
12231223

12241224
testCTypeAligned()
12251225

1226+
func testCTypeUnderAligned() {
1227+
let ptr = UnsafeMutablePointer<CTypeUnderAligned>.allocate(capacity: 1)
1228+
1229+
// initWithCopy
1230+
do {
1231+
let x = CTypeUnderAligned(SimpleClass(x: 23))
1232+
testInit(ptr, to: x)
1233+
}
1234+
1235+
// assignWithTake
1236+
do {
1237+
let y = CTypeUnderAligned(SimpleClass(x: 1))
1238+
1239+
// CHECK-NEXT: Before deinit
1240+
print("Before deinit")
1241+
1242+
// CHECK-NEXT: SimpleClass deinitialized!
1243+
testAssign(ptr, from: y)
1244+
}
1245+
1246+
// assignWithCopy
1247+
do {
1248+
var z = CTypeUnderAligned(SimpleClass(x: 5))
1249+
1250+
// CHECK-NEXT: Before deinit
1251+
print("Before deinit")
1252+
1253+
// CHECK-NEXT: SimpleClass deinitialized!
1254+
testAssignCopy(ptr, from: &z)
1255+
}
1256+
1257+
// CHECK-NEXT: Before deinit
1258+
print("Before deinit")
1259+
1260+
// destroy
1261+
// CHECK-NEXT: SimpleClass deinitialized!
1262+
testDestroy(ptr)
1263+
1264+
ptr.deallocate()
1265+
}
1266+
1267+
testCTypeUnderAligned()
1268+
12261269
#if os(macOS)
12271270
func testObjc() {
12281271
let ptr = UnsafeMutablePointer<ObjcWrapper>.allocate(capacity: 1)

0 commit comments

Comments
 (0)