https://github.com/hstk30-hw created https://github.com/llvm/llvm-project/pull/246813
Fixes #246256 >From 4fe954fcaef19d7eded5e1f2866821b664ad999e Mon Sep 17 00:00:00 2001 From: hanwei <[email protected]> Date: Sat, 28 Nov 2026 08:45:15 +0000 Subject: [PATCH] [AArch64] Fix illegal vectors argument(v2i8) to correct type on big-endian Fixes #246256. --- clang/lib/CodeGen/Targets/AArch64.cpp | 9 ++- .../CodeGen/AArch64/abi-classify-arg-types.c | 5 +- clang/test/CodeGen/AArch64/varargs.c | 55 +++++++++++++++++++ llvm/lib/ABI/Targets/AArch64.cpp | 13 ++++- llvm/unittests/ABI/AArch64TargetInfoTest.cpp | 20 +++++++ 5 files changed, 98 insertions(+), 4 deletions(-) diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp index e493f717af581d..1b4eef5e53afa1 100644 --- a/clang/lib/CodeGen/Targets/AArch64.cpp +++ b/clang/lib/CodeGen/Targets/AArch64.cpp @@ -321,7 +321,14 @@ ABIArgInfo AArch64ABIInfo::coerceIllegalVector(QualType Ty, unsigned &NSRN, return ABIArgInfo::getDirect(ResType); } if (Size <= 32) { - llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); + // AAPCS64 passes integral types in the least significant bits of the + // register, independent of the endianness (rule C.9). Widening the vector + // to a larger integer requires coercion through memory, which would leave + // the value in the most significant bits on big-endian, so coerce to an + // integer of exactly the vector's size instead: that is a bitcast. + llvm::Type *ResType = getDataLayout().isBigEndian() + ? llvm::IntegerType::get(getVMContext(), Size) + : llvm::Type::getInt32Ty(getVMContext()); return ABIArgInfo::getDirect(ResType); } if (Size == 64) { diff --git a/clang/test/CodeGen/AArch64/abi-classify-arg-types.c b/clang/test/CodeGen/AArch64/abi-classify-arg-types.c index 55e4b2a807997a..e38f52c79cb6ab 100644 --- a/clang/test/CodeGen/AArch64/abi-classify-arg-types.c +++ b/clang/test/CodeGen/AArch64/abi-classify-arg-types.c @@ -4,8 +4,8 @@ // RUN: %clang_cc1 -triple arm64_32-apple-ios7.0 -target-abi darwinpcs -fenable-matrix -fexperimental-max-bitint-width=1024 -fexperimental-abi-lowering -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DARWIN,LONG32,NOHFAALIGN,HUGEVEC,NOANDROID,ILP32 --implicit-check-not="not yet implemented" // RUN: %clang_cc1 -triple aarch64-linux-gnu -fenable-matrix -fexperimental-max-bitint-width=1024 -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,NOANDROID,GPR64,LE // RUN: %clang_cc1 -triple aarch64-linux-gnu -fenable-matrix -fexperimental-max-bitint-width=1024 -fexperimental-abi-lowering -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,NOANDROID,GPR64,LE --implicit-check-not="not yet implemented" -// RUN: %clang_cc1 -triple aarch64_be-linux-gnu -fenable-matrix -fexperimental-max-bitint-width=1024 -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,NOANDROID,GPR64,BE -// RUN: %clang_cc1 -triple aarch64_be-linux-gnu -fenable-matrix -fexperimental-max-bitint-width=1024 -fexperimental-abi-lowering -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,NOANDROID,GPR64,BE --implicit-check-not="not yet implemented" +// RUN: %clang_cc1 -triple aarch64_be-linux-gnu -fenable-matrix -fexperimental-max-bitint-width=1024 -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,GPR64,BE +// RUN: %clang_cc1 -triple aarch64_be-linux-gnu -fenable-matrix -fexperimental-max-bitint-width=1024 -fexperimental-abi-lowering -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,GPR64,BE --implicit-check-not="not yet implemented" // RUN: %clang_cc1 -triple aarch64-linux-android -fenable-matrix -fexperimental-max-bitint-width=1024 -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,ANDROID,GPR64,LE // RUN: %clang_cc1 -triple aarch64-linux-android -fenable-matrix -fexperimental-max-bitint-width=1024 -fexperimental-abi-lowering -emit-llvm -o - %s 2>&1 | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG64,AAPCS64,NOHUGEVEC,ANDROID,GPR64,LE --implicit-check-not="not yet implemented" // RUN: %clang_cc1 -triple aarch64-pc-windows-msvc -fenable-matrix -fexperimental-max-bitint-width=1024 -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,AAPCS,LONG32,NOHFAALIGN,NOHUGEVEC,NOANDROID,GPR64,WIN64,LE @@ -280,6 +280,7 @@ void arg_v16i8(v16i8 v) {} typedef char v2i8 __attribute__((vector_size(2))); void arg_v2i8(v2i8 v) {} // ANDROID: define{{.*}} void @arg_v2i8(i16 noundef %{{.*}}) +// BE: define{{.*}} void @arg_v2i8(i16 noundef %{{.*}}) // NOANDROID: define{{.*}} void @arg_v2i8(i32{{.*}} %{{.*}}) typedef char v3i8 __attribute__((vector_size(3))); diff --git a/clang/test/CodeGen/AArch64/varargs.c b/clang/test/CodeGen/AArch64/varargs.c index 10d445c13ef96c..1cc7f5ef1bc73f 100644 --- a/clang/test/CodeGen/AArch64/varargs.c +++ b/clang/test/CodeGen/AArch64/varargs.c @@ -837,3 +837,58 @@ empty empty_record_test(void) { // CHECK-NEXT: ret void return va_arg(the_list, empty); } + +// An illegal vector is coerced to an integer for parameter passing. AAPCS64 +// rule C.9 passes integral types in the least significant bits of the +// register, so on big-endian the coercion must use an integer of exactly the +// vector's size rather than i32, which would leave the value in the most +// significant bits. +typedef unsigned char v2u8 __attribute__((vector_size(2))); + +void pass_v2u8(v2u8 v) { +// CHECK-LABEL: define{{.*}} void @pass_v2u8( +// CHECK-LE-SAME: i32 %{{.+}}) +// CHECK-BE-SAME: i16 noundef %{{.+}}) +} + +v2u8 simple_v2u8(void) { +// CHECK-LABEL: define{{.*}} <2 x i8> @simple_v2u8 + return va_arg(the_list, v2u8); +// CHECK: [[GR_OFFS:%.+]] = load i32, ptr getelementptr inbounds nuw (i8, ptr @the_list, i64 24) +// CHECK: [[EARLY_ONSTACK:%.+]] = icmp sge i32 [[GR_OFFS]], 0 +// CHECK: br i1 [[EARLY_ONSTACK]], label %[[VAARG_ON_STACK:.+]], label %[[VAARG_MAYBE_REG:.+]] + +// CHECK: [[VAARG_MAYBE_REG]] +// CHECK: [[NEW_REG_OFFS:%.+]] = add i32 [[GR_OFFS]], 8 +// CHECK: store i32 [[NEW_REG_OFFS]], ptr getelementptr inbounds nuw (i8, ptr @the_list, i64 24) +// CHECK: [[INREG:%.+]] = icmp sle i32 [[NEW_REG_OFFS]], 0 +// CHECK: br i1 [[INREG]], label %[[VAARG_IN_REG:.+]], label %[[VAARG_ON_STACK]] + +// CHECK: [[VAARG_IN_REG]] +// CHECK: [[REG_TOP:%.+]] = load ptr, ptr getelementptr inbounds nuw (i8, ptr @the_list, i64 8) +// CHECK: [[REG_ADDR:%.+]] = getelementptr inbounds i8, ptr [[REG_TOP]], i32 [[GR_OFFS]] +// CHECK-BE: [[REG_ADDR_ALIGNED:%.+]] = getelementptr inbounds i8, ptr [[REG_ADDR]], i64 6 +// CHECK: br label %[[VAARG_END:.+]] + +// CHECK: [[VAARG_ON_STACK]] +// CHECK: [[STACK:%.+]] = load ptr, ptr @the_list +// CHECK: [[NEW_STACK:%.+]] = getelementptr inbounds i8, ptr [[STACK]], i64 8 +// CHECK: store ptr [[NEW_STACK]], ptr @the_list +// CHECK-BE: [[STACK_ALIGNED:%.+]] = getelementptr inbounds i8, ptr [[STACK]], i64 6 +// CHECK: br label %[[VAARG_END]] + +// CHECK: [[VAARG_END]] +// CHECK-LE: [[ADDR:%.+]] = phi ptr [ [[REG_ADDR]], %[[VAARG_IN_REG]] ], [ [[STACK]], %[[VAARG_ON_STACK]] ] +// CHECK-BE: [[ADDR:%.+]] = phi ptr [ [[REG_ADDR_ALIGNED]], %[[VAARG_IN_REG]] ], [ [[STACK_ALIGNED]], %[[VAARG_ON_STACK]] ] +// CHECK: [[RESULT:%.+]] = load <2 x i8>, ptr [[ADDR]] +// CHECK: ret <2 x i8> [[RESULT]] +} + +void variadic_v2u8(int n, ...); + +void call_v2u8_varargs(void) { +// CHECK-LABEL: define{{.*}} void @call_v2u8_varargs() + variadic_v2u8(1, (v2u8){1, 2}); +// CHECK-LE: call void (i32, ...) @variadic_v2u8(i32 noundef 1, i32 +// CHECK-BE: call void (i32, ...) @variadic_v2u8(i32 noundef 1, i16 noundef +} diff --git a/llvm/lib/ABI/Targets/AArch64.cpp b/llvm/lib/ABI/Targets/AArch64.cpp index c49b66a76511ea..cf4c5f5274797e 100644 --- a/llvm/lib/ABI/Targets/AArch64.cpp +++ b/llvm/lib/ABI/Targets/AArch64.cpp @@ -431,8 +431,19 @@ ArgInfo AArch64TargetInfo::coerceIllegalVector(const VectorType *VT, return ArgInfo::getDirect(ResType); } const Type *I32 = TB.getIntegerType(32, llvm::Align(4), /*Signed=*/false); - if (Size <= 32) + if (Size <= 32) { + // AAPCS64 passes integral types in the least significant bits of the + // register, independent of the endianness (rule C.9). Widening the vector + // to a larger integer requires coercion through memory, which would leave + // the value in the most significant bits on big-endian, so coerce to an + // integer of exactly the vector's size instead: that is a bitcast. + if (Opts.IsBigEndian && Size < 32) { + assert(Size >= 8 && isPowerOf2_64(Size) && "unexpected size"); + return ArgInfo::getDirect( + TB.getIntegerType(Size, llvm::Align(Size / 8), /*Signed=*/false)); + } return ArgInfo::getDirect(I32); + } if (Size == 64) { NSRN = std::min(NSRN + 1, 8u); return ArgInfo::getDirect( diff --git a/llvm/unittests/ABI/AArch64TargetInfoTest.cpp b/llvm/unittests/ABI/AArch64TargetInfoTest.cpp index 2c7920272332bb..4a98059b5968c3 100644 --- a/llvm/unittests/ABI/AArch64TargetInfoTest.cpp +++ b/llvm/unittests/ABI/AArch64TargetInfoTest.cpp @@ -537,6 +537,26 @@ TEST_F(AArch64TargetInfoTest, ClassifyArgumentIllegalVectorAndroid) { expectDirectCoercedInteger(FI->getArgInfo(0).Info, 32); } +// On big-endian, integral types are passed in the least significant bits of +// the register (AAPCS64 rule C.9), so small illegal vectors are coerced to an +// integer of exactly their size rather than i32. +TEST_F(AArch64TargetInfoTest, ClassifyArgumentIllegalVectorBigEndian) { + AArch64ABIOptions Opts(AArch64ABIKind::AAPCS); + Opts.IsBigEndian = true; + std::unique_ptr<TargetInfo> TI = createAArch64TargetInfo(TB, Opts); + + std::unique_ptr<FunctionInfo> FI = + FunctionInfo::create(llvm::CallingConv::C, Void, {V2I8}); + TI->computeInfo(*FI); + expectDirectCoercedInteger(FI->getArgInfo(0).Info, 16); + + for (const ABIType *ArgTy : {V3I8, V4I8}) { + FI = FunctionInfo::create(llvm::CallingConv::C, Void, {ArgTy}); + TI->computeInfo(*FI); + expectDirectCoercedInteger(FI->getArgInfo(0).Info, 32); + } +} + // arm64_32 MachO treats vectors larger than 32 bits as legal, including // sizes that other AArch64 ABIs pass indirectly. Non-power-of-2 element // counts are still illegal. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
