[clang] [clang][PowerPC] Take a vector va_arg from the overflow area on 32-bit SVR4 (PR #246993)
Matt Turner via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 08:40:35 PST 2026
https://github.com/mattst88 created https://github.com/llvm/llvm-project/pull/246993
GCC and the backend pass a 16-byte AltiVec vector to a variadic function
in the overflow area, aligned to 16, without using a GPR. va_arg instead
read a GPR's word from the register save area while any GPR was left,
and counted it as used, so the vector and every integer after it came
out wrong, even when the caller was built by clang.
Assisted-by: Claude Code
>From 0b29ef620c5f88c608143696690f1713875e24bc Mon Sep 17 00:00:00 2001
From: Matt Turner <mattst88 at gmail.com>
Date: Wed, 11 Nov 2026 18:13:03 -0500
Subject: [PATCH] [clang][PowerPC] Take a vector va_arg from the overflow area
on 32-bit SVR4
GCC and the backend pass a 16-byte AltiVec vector to a variadic function
in the overflow area, aligned to 16, without using a GPR. va_arg instead
read a GPR's word from the register save area while any GPR was left,
and counted it as used, so the vector and every integer after it came
out wrong, even when the caller was built by clang.
Assisted-by: Claude Code
---
clang/lib/CodeGen/Targets/PPC.cpp | 9 +++++++++
.../test/CodeGen/PowerPC/ppc32-vaarg-vector.c | 19 +++++++++++++++++++
2 files changed, 28 insertions(+)
create mode 100644 clang/test/CodeGen/PowerPC/ppc32-vaarg-vector.c
diff --git a/clang/lib/CodeGen/Targets/PPC.cpp b/clang/lib/CodeGen/Targets/PPC.cpp
index de296680ce6af2..acd3eb44e5ddc7 100644
--- a/clang/lib/CodeGen/Targets/PPC.cpp
+++ b/clang/lib/CodeGen/Targets/PPC.cpp
@@ -482,6 +482,15 @@ RValue PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
/*AllowHigherAlign=*/true, Slot);
}
+ // A 16-byte vector is always passed in the overflow area, aligned to 16,
+ // and leaves the GPRs for later arguments.
+ if (Ty->isVectorType() && getContext().getTypeSize(Ty) == 128)
+ return emitVoidPtrVAArg(CGF, CGF.Builder.CreateStructGEP(VAList, 3), Ty,
+ /*IsIndirect=*/false,
+ getContext().getTypeInfoInChars(Ty),
+ CharUnits::fromQuantity(4),
+ /*AllowHigherAlign=*/true, Slot);
+
const unsigned OverflowLimit = 8;
if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
// TODO: Implement this. For now ignore.
diff --git a/clang/test/CodeGen/PowerPC/ppc32-vaarg-vector.c b/clang/test/CodeGen/PowerPC/ppc32-vaarg-vector.c
new file mode 100644
index 00000000000000..92b71197aea8ca
--- /dev/null
+++ b/clang/test/CodeGen/PowerPC/ppc32-vaarg-vector.c
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -triple powerpc-unknown-linux-gnu -target-feature +altivec -emit-llvm -o - %s | FileCheck %s
+
+// An AltiVec vector argument to a variadic function goes in the overflow
+// area, aligned to 16, and does not use up a GPR; an int after it is still
+// passed in a GPR. va_arg must not touch the GPR count.
+
+typedef __builtin_va_list va_list;
+
+// CHECK-LABEL: define{{.*}} <4 x i32> @v(
+// CHECK-NOT: %gpr
+// CHECK-NOT: numUsedRegs
+// CHECK: %[[P:.+]] = getelementptr inbounds nuw %struct.__va_list_tag, ptr %{{.+}}, i32 0, i32 3
+// CHECK-NEXT: %argp.cur = load ptr, ptr %[[P]], align 4
+// CHECK-NEXT: %[[R:.+]] = getelementptr inbounds i8, ptr %argp.cur, i32 15
+// CHECK-NEXT: %argp.cur.aligned = call ptr @llvm.ptrmask.p0.i32(ptr %[[R]], i32 -16)
+// CHECK-NEXT: %argp.next = getelementptr inbounds i8, ptr %argp.cur.aligned, i32 16
+// CHECK-NEXT: store ptr %argp.next, ptr %[[P]], align 4
+// CHECK-NEXT: load <4 x i32>, ptr %argp.cur.aligned, align 16
+vector int v(va_list ap) { return __builtin_va_arg(ap, vector int); }
More information about the cfe-commits
mailing list