Skip to content

Commit 7b87161

Browse files
committed
SwiftCC: Perform physical layout when computing coercion types
We need to take type alignment padding into account whe computing physical layouts. The layout must be compatible with the input layout, offsets are defined in terms of offsets within a packed struct which are computed in terms of the alloc size of a type. Usingthe store size we would insert padding for the following type for example: struct { int3 v; long long l; } __attribute((packed)) On x86-64 int3 is padded to int4 alignment. The swiftcc type would be <{ <3 x float>, [4 x i8], i64 }> which is not compatible with <{ <3 x float>, i64 }>. The latter has i64 at offset 16 and the former at offset 20. rdar://32618125 llvm-svn: 305956
1 parent 0509238 commit 7b87161

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

clang/lib/CodeGen/SwiftCallingConv.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ static CharUnits getTypeStoreSize(CodeGenModule &CGM, llvm::Type *type) {
5757
return CharUnits::fromQuantity(CGM.getDataLayout().getTypeStoreSize(type));
5858
}
5959

60+
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type) {
61+
return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(type));
62+
}
63+
6064
void SwiftAggLowering::addTypedData(QualType type, CharUnits begin) {
6165
// Deal with various aggregate types as special cases:
6266

@@ -542,7 +546,9 @@ SwiftAggLowering::getCoerceAndExpandTypes() const {
542546
packed = true;
543547

544548
elts.push_back(entry.Type);
545-
lastEnd = entry.End;
549+
550+
lastEnd = entry.Begin + getTypeAllocSize(CGM, entry.Type);
551+
assert(entry.End <= lastEnd);
546552
}
547553

548554
// We don't need to adjust 'packed' to deal with possible tail padding

clang/test/CodeGen/64bit-swiftcall.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -target-cpu core2 -emit-llvm -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -target-cpu core2 -emit-llvm -o - %s | FileCheck %s --check-prefix=X86-64
23
// RUN: %clang_cc1 -triple arm64-apple-ios9 -target-cpu cyclone -emit-llvm -o - %s | FileCheck %s
34
// RUN: %clang_cc1 -triple arm64-apple-ios9 -target-cpu cyclone -emit-llvm -o - %s | FileCheck %s --check-prefix=ARM64
45

@@ -1014,3 +1015,20 @@ typedef struct {
10141015
TEST(struct_v1f3)
10151016
// ARM64-LABEL: define swiftcc { <2 x float>, float } @return_struct_v1f3()
10161017
// ARM64-LABEL: define swiftcc void @take_struct_v1f3(<2 x float>, float)
1018+
1019+
typedef struct {
1020+
int3 vect;
1021+
unsigned long long val;
1022+
} __attribute__((packed)) padded_alloc_size_vector;
1023+
TEST(padded_alloc_size_vector)
1024+
// X86-64-LABEL: take_padded_alloc_size_vector(<3 x i32>, i64)
1025+
// X86-64-NOT: [4 x i8]
1026+
// x86-64: ret void
1027+
1028+
typedef union {
1029+
float f1;
1030+
float3 fv2;
1031+
} union_hom_fp_partial2;
1032+
TEST(union_hom_fp_partial2)
1033+
// X86-64-LABEL: take_union_hom_fp_partial2(i64, float)
1034+
// ARM64-LABEL: take_union_hom_fp_partial2(i64, float)

0 commit comments

Comments
 (0)