[clang] [Mips] Fix big-endian va_arg of vectors smaller than a slot (PR #247572)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 02:08:35 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-mips
Author: Brad Smith (brad0)
<details>
<summary>Changes</summary>
Vectors are passed like aggregates on MIPS, so on big-endian a vector
smaller than an argument slot is at the start of its slot, the same as
GCC. va_arg right-adjusted it instead and read the padding. On O32 this
broke vectors under 4 bytes, and on N32/N64 vectors under 8 bytes.
Fixes #<!-- -->247461
---
Full diff: https://github.com/llvm/llvm-project/pull/247572.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/Targets/Mips.cpp (+6)
- (modified) clang/test/CodeGen/mips-varargs.c (+53)
``````````diff
diff --git a/clang/lib/CodeGen/Targets/Mips.cpp b/clang/lib/CodeGen/Targets/Mips.cpp
index 4ad1a56b92a39..a8da13401aa86 100644
--- a/clang/lib/CodeGen/Targets/Mips.cpp
+++ b/clang/lib/CodeGen/Targets/Mips.cpp
@@ -481,6 +481,12 @@ RValue MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
// MinABIStackAlignInBytes is the size of argument slots on the stack.
CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
+ // Vectors are passed like aggregates, so on big-endian targets a vector
+ // smaller than a slot is at the start of its slot rather than the end.
+ // Treat it as filling the slot so it is not right-adjusted.
+ if (Ty->isVectorType() && TyInfo.Width < ArgSlotSize)
+ TyInfo.Width = ArgSlotSize;
+
RValue Res = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, TyInfo,
ArgSlotSize, /*AllowHigherAlign*/ true, Slot);
diff --git a/clang/test/CodeGen/mips-varargs.c b/clang/test/CodeGen/mips-varargs.c
index 0c22b43600dac..3ad42a548b6cf 100644
--- a/clang/test/CodeGen/mips-varargs.c
+++ b/clang/test/CodeGen/mips-varargs.c
@@ -8,6 +8,8 @@
#include <stdarg.h>
typedef int v4i32 __attribute__ ((__vector_size__ (16)));
+typedef char v2i8 __attribute__ ((__vector_size__ (2)));
+typedef int v1i32 __attribute__ ((__vector_size__ (4)));
int test_i32(char *fmt, ...) {
va_list va;
@@ -149,3 +151,54 @@ int test_v4i32(char *fmt, ...) {
// ALL: [[VECEXT:%.+]] = extractelement <4 x i32> {{.*}}, i32 0
// ALL: ret i32 [[VECEXT]]
// ALL: }
+
+// Vectors smaller than an argument slot are passed like aggregates, at the
+// start of the slot, so they must not be right-adjusted on big-endian targets.
+
+int test_v2i8(char *fmt, ...) {
+ va_list va;
+
+ va_start(va, fmt);
+ v2i8 v = va_arg(va, v2i8);
+ va_end(va);
+
+ return v[1];
+}
+
+// ALL-LABEL: define{{.*}} i32 @test_v2i8(ptr{{.*}} %fmt, ...)
+//
+// ALL: %va = alloca ptr, align [[$PTRALIGN]]
+// ALL: [[V:%.+]] = alloca <2 x i8>, align 2
+// ALL: call void @llvm.va_start.p0(ptr %va)
+// ALL: [[AP_CUR:%.+]] = load ptr, ptr %va, align [[$PTRALIGN]]
+// ALL: [[AP_NEXT:%.+]] = getelementptr inbounds i8, ptr [[AP_CUR]], [[$INTPTR_T]] [[$CHUNKSIZE]]
+// ALL: store ptr [[AP_NEXT]], ptr %va, align [[$PTRALIGN]]
+// ALL: [[ARG:%.+]] = load <2 x i8>, ptr [[AP_CUR]], align 2
+// ALL: store <2 x i8> [[ARG]], ptr [[V]], align 2
+//
+// ALL: call void @llvm.va_end.p0(ptr %va)
+// ALL: }
+
+int test_v1i32(char *fmt, ...) {
+ va_list va;
+
+ va_start(va, fmt);
+ v1i32 v = va_arg(va, v1i32);
+ va_end(va);
+
+ return v[0];
+}
+
+// ALL-LABEL: define{{.*}} i32 @test_v1i32(ptr{{.*}} %fmt, ...)
+//
+// ALL: %va = alloca ptr, align [[$PTRALIGN]]
+// ALL: [[V:%.+]] = alloca <1 x i32>, align 4
+// ALL: call void @llvm.va_start.p0(ptr %va)
+// ALL: [[AP_CUR:%.+]] = load ptr, ptr %va, align [[$PTRALIGN]]
+// ALL: [[AP_NEXT:%.+]] = getelementptr inbounds i8, ptr [[AP_CUR]], [[$INTPTR_T]] [[$CHUNKSIZE]]
+// ALL: store ptr [[AP_NEXT]], ptr %va, align [[$PTRALIGN]]
+// ALL: [[ARG:%.+]] = load <1 x i32>, ptr [[AP_CUR]], align 4
+// ALL: store <1 x i32> [[ARG]], ptr [[V]], align 4
+//
+// ALL: call void @llvm.va_end.p0(ptr %va)
+// ALL: }
``````````
</details>
https://github.com/llvm/llvm-project/pull/247572
More information about the cfe-commits
mailing list