Skip to content

Commit 6b1341e

Browse files
committed
[PowerPC] [Clang] Fix alignment of 128-bit float types
According to ELF v2 ABI, both IEEE 128-bit and IBM extended floating point variables should be quad-word (16 bytes) aligned. Previously, only vector types are considered aligned as quad-word on PowerPC. This patch will fix incorrectness of IEEE 128-bit float argument in va_arg cases. Reviewed By: rjmccall Differential Revision: https://reviews.llvm.org/D91596
1 parent 4d8dede commit 6b1341e

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎clang/lib/CodeGen/TargetInfo.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5051,6 +5051,11 @@ CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
50515051
return CharUnits::fromQuantity(16);
50525052
} else if (Ty->isVectorType()) {
50535053
return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
5054+
} else if (Ty->isRealFloatingType() && getContext().getTypeSize(Ty) == 128) {
5055+
// IEEE 128-bit floating numbers are also stored in vector registers.
5056+
// And both IEEE quad-precision and IBM extended double (ppc_fp128) should
5057+
// be quad-word aligned.
5058+
return CharUnits::fromQuantity(16);
50545059
}
50555060

50565061
// For single-element float/vector structs, we consider the whole type
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu -emit-llvm \
2+
// RUN: -target-cpu pwr9 -target-feature +float128 -mabi=ieeelongdouble \
3+
// RUN: -o - %s | FileCheck %s -check-prefix=IEEE
4+
// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu -emit-llvm \
5+
// RUN: -target-cpu pwr9 -target-feature +float128 \
6+
// RUN: -o - %s | FileCheck %s -check-prefix=IBM
7+
8+
#include <stdarg.h>
9+
10+
// IEEE-LABEL: define fp128 @f128(i32 signext %n, ...)
11+
// IEEE: call void @llvm.va_start(i8* %{{[0-9a-zA-Z_.]+}})
12+
// IEEE: %[[P1:[0-9a-zA-Z_.]+]] = add i64 %{{[0-9a-zA-Z_.]+}}, 15
13+
// IEEE: %[[P2:[0-9a-zA-Z_.]+]] = and i64 %[[P1]], -16
14+
// IEEE: %[[P3:[0-9a-zA-Z_.]+]] = inttoptr i64 %[[P2]] to i8*
15+
// IEEE: %[[P4:[0-9a-zA-Z_.]+]] = bitcast i8* %[[P3]] to fp128*
16+
// IEEE: %{{[0-9a-zA-Z_.]+}} = load fp128, fp128* %[[P4]], align 16
17+
// IEEE: call void @llvm.va_end(i8* %{{[0-9a-zA-Z_.]+}})
18+
__float128 f128(int n, ...) {
19+
va_list ap;
20+
va_start(ap, n);
21+
__float128 x = va_arg(ap, __float128);
22+
va_end(ap);
23+
return x;
24+
}
25+
26+
// IEEE-LABEL: define fp128 @long_double(i32 signext %n, ...)
27+
// IEEE: call void @llvm.va_start(i8* %{{[0-9a-zA-Z_.]+}})
28+
// IEEE: %[[P1:[0-9a-zA-Z_.]+]] = add i64 %{{[0-9a-zA-Z_.]+}}, 15
29+
// IEEE: %[[P2:[0-9a-zA-Z_.]+]] = and i64 %[[P1]], -16
30+
// IEEE: %[[P3:[0-9a-zA-Z_.]+]] = inttoptr i64 %[[P2]] to i8*
31+
// IEEE: %[[P4:[0-9a-zA-Z_.]+]] = bitcast i8* %[[P3]] to fp128*
32+
// IEEE: %{{[0-9a-zA-Z_.]+}} = load fp128, fp128* %[[P4]], align 16
33+
// IEEE: call void @llvm.va_end(i8* %{{[0-9a-zA-Z_.]+}})
34+
35+
// IBM-LABEL: define ppc_fp128 @long_double(i32 signext %n, ...)
36+
// IBM: call void @llvm.va_start(i8* %{{[0-9a-zA-Z_.]+}})
37+
// IBM: %[[P1:[0-9a-zA-Z_.]+]] = add i64 %{{[0-9a-zA-Z_.]+}}, 15
38+
// IBM: %[[P2:[0-9a-zA-Z_.]+]] = and i64 %[[P1]], -16
39+
// IBM: %[[P3:[0-9a-zA-Z_.]+]] = inttoptr i64 %[[P2]] to i8*
40+
// IBM: %[[P4:[0-9a-zA-Z_.]+]] = bitcast i8* %[[P3]] to ppc_fp128*
41+
// IBM: %{{[0-9a-zA-Z_.]+}} = load ppc_fp128, ppc_fp128* %[[P4]], align 16
42+
// IBM: call void @llvm.va_end(i8* %{{[0-9a-zA-Z_.]+}})
43+
long double long_double(int n, ...) {
44+
va_list ap;
45+
va_start(ap, n);
46+
long double x = va_arg(ap, long double);
47+
va_end(ap);
48+
return x;
49+
}

0 commit comments

Comments
 (0)