[clang] [clang][PowerPC] Take two FPRs for a long double va_arg on 32-bit SVR4 (PR #246994)
Matt Turner via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 08:40:40 PST 2026
https://github.com/mattst88 created https://github.com/llvm/llvm-project/pull/246994
An IBM double-double long double is passed in two consecutive FPRs, which
need not start at an even register, or in a doubleword-aligned slot of the
overflow area. va_arg counted it as one FPR and aligned its overflow slot to
16, so a variadic function read the wrong value for every long double after
the first: summing 1.0L, 2.0L and 3.0L gave 5.
Use two FPRs, send the value to the overflow area once seven FPRs are used,
and align its overflow slot to 8. GCC's va_arg does the same, and both
compilers already pass long double arguments this way.
Assisted-by: Claude Code
>From fff19ff9e26a3327618711f1e92bb081e339c490 Mon Sep 17 00:00:00 2001
From: Matt Turner <mattst88 at gmail.com>
Date: Wed, 11 Nov 2026 17:50:06 -0500
Subject: [PATCH] [clang][PowerPC] Take two FPRs for a long double va_arg on
32-bit SVR4
An IBM double-double long double is passed in two consecutive FPRs, which
need not start at an even register, or in a doubleword-aligned slot of the
overflow area. va_arg counted it as one FPR and aligned its overflow slot to
16, so a variadic function read the wrong value for every long double after
the first: summing 1.0L, 2.0L and 3.0L gave 5.
Use two FPRs, send the value to the overflow area once seven FPRs are used,
and align its overflow slot to 8. GCC's va_arg does the same, and both
compilers already pass long double arguments this way.
Assisted-by: Claude Code
---
clang/lib/CodeGen/Targets/PPC.cpp | 17 +++++++---
.../PowerPC/ppc32-varargs-long-double.c | 33 +++++++++++++++++++
2 files changed, 46 insertions(+), 4 deletions(-)
create mode 100644 clang/test/CodeGen/PowerPC/ppc32-varargs-long-double.c
diff --git a/clang/lib/CodeGen/Targets/PPC.cpp b/clang/lib/CodeGen/Targets/PPC.cpp
index de296680ce6af2..08a3dada03d557 100644
--- a/clang/lib/CodeGen/Targets/PPC.cpp
+++ b/clang/lib/CodeGen/Targets/PPC.cpp
@@ -500,6 +500,13 @@ RValue PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
bool isInt = !Ty->isFloatingType();
bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
+ bool isIBMLongDouble =
+ Ty->isRealFloatingType() && &getContext().getFloatTypeSemantics(Ty) ==
+ &llvm::APFloat::PPCDoubleDouble();
+ // With hard float, an IBM double-double long double takes two consecutive
+ // FPRs, which need not start at an even register, or a doubleword-aligned
+ // overflow slot.
+ bool isPPCF128 = !IsSoftFloatABI && isIBMLongDouble;
// All aggregates are passed indirectly? That doesn't seem consistent
// with the argument-lowering code.
@@ -523,8 +530,9 @@ RValue PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
}
+ unsigned RegLimit = OverflowLimit - (isPPCF128 ? 1 : 0);
llvm::Value *CC =
- Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
+ Builder.CreateICmpULT(NumRegs, Builder.getInt8(RegLimit), "cond");
llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
@@ -563,9 +571,8 @@ RValue PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
// Increase the used-register count.
- NumRegs =
- Builder.CreateAdd(NumRegs,
- Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
+ bool TwoRegs = isI64 || (isF64 && IsSoftFloatABI) || isPPCF128;
+ NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(TwoRegs ? 2 : 1));
Builder.CreateStore(NumRegs, NumRegsAddr);
CGF.EmitBranch(Cont);
@@ -595,6 +602,8 @@ RValue PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
OverflowAreaAlign);
// Round up address of argument to alignment
CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
+ if (isPPCF128)
+ Align = CharUnits::fromQuantity(8);
if (Align > OverflowAreaAlign) {
llvm::Value *Ptr = OverflowArea.emitRawPointer(CGF);
OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
diff --git a/clang/test/CodeGen/PowerPC/ppc32-varargs-long-double.c b/clang/test/CodeGen/PowerPC/ppc32-varargs-long-double.c
new file mode 100644
index 00000000000000..689009e6ad15ac
--- /dev/null
+++ b/clang/test/CodeGen/PowerPC/ppc32-varargs-long-double.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -triple powerpc-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple powerpc-unknown-linux-gnu -target-feature +spe -emit-llvm -o - %s | FileCheck %s --check-prefix=SPE
+
+// An IBM double-double long double takes two consecutive FPRs, starting at
+// any register, so it goes to the overflow area once seven FPRs are used. In
+// the overflow area its slot is aligned to 8, not to its 16-byte alignment.
+
+#include <stdarg.h>
+
+// CHECK-LABEL: define{{.*}} ppc_fp128 @get(
+// CHECK: %fpr = getelementptr inbounds nuw %struct.__va_list_tag, ptr %{{.*}}, i32 0, i32 1
+// CHECK: %numUsedRegs = load i8, ptr %fpr
+// CHECK-NEXT: %cond = icmp ult i8 %numUsedRegs, 7
+// CHECK: using_regs:
+// CHECK: mul i8 %numUsedRegs, 8
+// CHECK: add i8 %numUsedRegs, 2
+// CHECK: using_overflow:
+// CHECK-NEXT: store i8 8, ptr %fpr
+// CHECK: getelementptr inbounds i8, ptr %argp.cur, i32 7
+// CHECK-NEXT: %argp.cur.aligned = call ptr @llvm.ptrmask.p0.i32(ptr %{{.*}}, i32 -8)
+// CHECK-NEXT: getelementptr inbounds i8, ptr %argp.cur.aligned, i32 16
+
+// SPE-LABEL: define{{.*}} @get(
+// SPE: %gpr = getelementptr inbounds nuw %struct.__va_list_tag, ptr %{{.*}}, i32 0, i32 0
+// SPE-NOT: %fpr
+// SPE: icmp ult i8 %{{.*}}, 8
+long double get(va_list ap) { return va_arg(ap, long double); }
+
+// A double still takes one FPR.
+// CHECK-LABEL: define{{.*}} double @getd(
+// CHECK: %cond = icmp ult i8 %numUsedRegs, 8
+// CHECK: add i8 %numUsedRegs, 1
+double getd(va_list ap) { return va_arg(ap, double); }
More information about the cfe-commits
mailing list