Skip to content

[IRGen] Fix alignment for imported C types in CVW #73405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/IRGen/TypeLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,10 @@ AlignedGroupEntry::fixedAlignment(IRGenModule &IGM) const {
if (_fixedAlignment.has_value())
return *_fixedAlignment;

if (fixedTypeInfo) {
return *(_fixedAlignment = (*fixedTypeInfo)->getFixedAlignment());
}

Alignment currentAlignment = Alignment(
std::max((Alignment::int_type)1, minimumAlignment));
for (auto *entry : entries) {
Expand Down
8 changes: 8 additions & 0 deletions test/Interpreter/Inputs/CTypes/CTypes.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#ifndef SWIFT_TEST_CTYPES_H
#define SWIFT_TEST_CTYPES_H

#include <stdint.h>

struct BigAlignment {
_Alignas(16) float foo[4];
char b;
};

#pragma pack(push, 4)
struct UnderAligned {
int64_t bar;
};
#pragma pack(pop)

#endif
10 changes: 10 additions & 0 deletions test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public struct CTypeAligned {
}
}

public struct CTypeUnderAligned {
let w: Int32 = 0
let x: UnderAligned? = UnderAligned()
let y: SimpleClass

public init(_ y: SimpleClass) {
self.y = y
}
}

public struct GenericStruct<T> {
let x: Int = 0
let y: T
Expand Down
43 changes: 43 additions & 0 deletions test/Interpreter/layout_string_witnesses_static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,49 @@ func testCTypeAligned() {

testCTypeAligned()

func testCTypeUnderAligned() {
let ptr = UnsafeMutablePointer<CTypeUnderAligned>.allocate(capacity: 1)

// initWithCopy
do {
let x = CTypeUnderAligned(SimpleClass(x: 23))
testInit(ptr, to: x)
}

// assignWithTake
do {
let y = CTypeUnderAligned(SimpleClass(x: 1))

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
testAssign(ptr, from: y)
}

// assignWithCopy
do {
var z = CTypeUnderAligned(SimpleClass(x: 5))

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
testAssignCopy(ptr, from: &z)
}

// CHECK-NEXT: Before deinit
print("Before deinit")

// destroy
// CHECK-NEXT: SimpleClass deinitialized!
testDestroy(ptr)

ptr.deallocate()
}

testCTypeUnderAligned()

#if os(macOS)
func testObjc() {
let ptr = UnsafeMutablePointer<ObjcWrapper>.allocate(capacity: 1)
Expand Down