https://github.com/koachan updated https://github.com/llvm/llvm-project/pull/217065
>From 09db49897e5ddb41474be49e93b41fa348acf323 Mon Sep 17 00:00:00 2001 From: Koakuma <[email protected]> Date: Tue, 18 Aug 2026 22:48:28 +0700 Subject: [PATCH 1/3] [clang][SPARC] Match zero-size struct lowering with GCC's Zero-sized struct arguments are lowered in a different way to other types: - If there are still free integer registers avaliable, it's lowered as if it's a normal i64, that is, it will occupy an i64 register, together with a corresponding shadow space in an f64 register and the stack. - After that, the arguments will occupy a f64 register starting from %d12, but *not* allocate a shadow space in the stack. - After the f64 register slots are exhausted, the arguments will not be allocated at all. To do this the clang frontend lowers ZSS arguments as `i64 inreg`s, and the backend allocates `i64 inreg` arguments following the scheme above. Fixes https://github.com/llvm/llvm-project/issues/213561. --- clang/lib/CodeGen/Targets/Sparc.cpp | 12 ++-- clang/test/CodeGen/Sparc/sparcv9-abi.c | 13 ++-- llvm/lib/Target/Sparc/SparcISelLowering.cpp | 71 +++++++++++++++++---- llvm/test/CodeGen/SPARC/inreg-i64-v9.ll | 47 ++++++++++++++ 4 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 llvm/test/CodeGen/SPARC/inreg-i64-v9.ll diff --git a/clang/lib/CodeGen/Targets/Sparc.cpp b/clang/lib/CodeGen/Targets/Sparc.cpp index fcd2086964c488..c8166e63ba7905 100644 --- a/clang/lib/CodeGen/Targets/Sparc.cpp +++ b/clang/lib/CodeGen/Targets/Sparc.cpp @@ -306,6 +306,11 @@ class SparcV9ABIInfo : public ABIInfo { break; } } + + // Zero-sized structs gets the inreg flag; they are treated specially. + unsigned Size = DL.getTypeSizeInBits(StrTy).getKnownMinValue(); + if (!Size) + InReg = true; } // Check if Ty is a usable substitute for the coercion type. @@ -420,12 +425,11 @@ ABIArgInfo SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit, CoerceBuilder CB(VMContext, getDataLayout(), IsVarArg); CB.addStruct(0, StrTy); + // All structs, even empty ones, should take up a register argument slot, // so pin the minimum struct size to one bit. - CB.pad(llvm::alignTo( - std::max(CB.DL.getTypeSizeInBits(StrTy).getKnownMinValue(), uint64_t(1)), - 64)); - RegOffset += PaddingSlots + CB.Size / 64; + uint64_t ActualSize = CB.DL.getTypeSizeInBits(StrTy).getKnownMinValue(); + CB.pad(llvm::alignTo(std::max(ActualSize, uint64_t(1)), 64)); // Try to use the original type for coercion. llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); diff --git a/clang/test/CodeGen/Sparc/sparcv9-abi.c b/clang/test/CodeGen/Sparc/sparcv9-abi.c index 46556b7abfcab5..c37181d9ee168c 100644 --- a/clang/test/CodeGen/Sparc/sparcv9-abi.c +++ b/clang/test/CodeGen/Sparc/sparcv9-abi.c @@ -21,7 +21,7 @@ char f_int_4(char x) { return x; } // CHECK-LABEL: define{{.*}} fp128 @f_ld(fp128 noundef %x) long double f_ld(long double x) { return x; } -// Zero-sized structs reserves an argument register slot if passed directly. +// Zero-sized structs will be coerced into `i64 inreg`s. struct empty {}; struct emptyarr { struct empty a[10]; }; @@ -31,12 +31,17 @@ struct align16_int { _Alignas(16) int x; }; struct align16_mixed { _Alignas(16) int x; double y; }; struct align16_longdouble { long double x; }; -// CHECK-LABEL: define{{.*}} i64 @f_empty(i64 %x.coerce) +// CHECK-LABEL: define{{.*}} i64 @f_empty(i64 inreg %x.coerce) struct empty f_empty(struct empty x) { return x; } -// CHECK-LABEL: define{{.*}} i64 @f_emptyarr(i64 %x.coerce) +// CHECK-LABEL: define{{.*}} i64 @f_emptyarr(i64 inreg %x.coerce) struct empty f_emptyarr(struct emptyarr x) { return x.a[0]; } +// CHECK-LABEL: define{{.*}} i64 @f_emptystackarg(i64 inreg %a0.coerce, i64 inreg %a1.coerce, i64 inreg %a2.coerce, i64 inreg %a3.coerce, i64 inreg %a4.coerce, i64 inreg %a5.coerce, i64 inreg %a6.coerce, i64 inreg %a7.coerce) +struct empty f_emptystackarg(struct empty a0, struct empty a1, struct empty a2, struct empty a3, struct empty a4, struct empty a5, struct empty a6, struct empty a7) { + return a7; +} + // CHECK-LABEL: define{{.*}} void @f_aligncaller(i64 %a.coerce0, i64 %a.coerce1) // CHECK-LABEL: declare{{.*}} void @f_aligncallee(i32 noundef signext, i64, i64, i64) void f_aligncallee(int pad, struct align16_int a); @@ -98,7 +103,7 @@ long f_emptyvar(unsigned count, ...) { } // If the zero-sized struct is contained in a non-zero-sized struct, -// though, it doesn't reserve any registers. +// though, it disappears completely. struct emptymixed { struct empty a; long b; }; struct emptyflex { unsigned count; struct empty data[10]; }; diff --git a/llvm/lib/Target/Sparc/SparcISelLowering.cpp b/llvm/lib/Target/Sparc/SparcISelLowering.cpp index 4ab47cceb48529..03746475937fcf 100644 --- a/llvm/lib/Target/Sparc/SparcISelLowering.cpp +++ b/llvm/lib/Target/Sparc/SparcISelLowering.cpp @@ -44,6 +44,20 @@ using namespace llvm; // Calling Convention Implementation //===----------------------------------------------------------------------===// +static const MCPhysReg ArgIs[] = {SP::I0, SP::I1, SP::I2, + SP::I3, SP::I4, SP::I5}; + +static const MCPhysReg ArgFs[] = { + SP::F1, SP::F3, SP::F5, SP::F7, SP::F9, SP::F11, SP::F13, SP::F15, + SP::F17, SP::F19, SP::F21, SP::F23, SP::F25, SP::F27, SP::F29, SP::F31}; + +static const MCPhysReg ArgDs[] = { + SP::D0, SP::D1, SP::D2, SP::D3, SP::D4, SP::D5, SP::D6, SP::D7, + SP::D8, SP::D9, SP::D10, SP::D11, SP::D12, SP::D13, SP::D14, SP::D15}; + +static const MCPhysReg ArgQs[] = {SP::Q0, SP::Q1, SP::Q2, SP::Q3, + SP::Q4, SP::Q5, SP::Q6, SP::Q7}; + static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT, MVT &LocVT, CCValAssign::LocInfo &LocInfo, ISD::ArgFlagsTy &ArgFlags, CCState &State) @@ -114,25 +128,60 @@ static bool Analyze_CC_Sparc64_Full(bool IsReturn, unsigned &ValNo, MVT &ValVT, || LocVT.getSizeInBits() == 64) && "Can't handle non-64 bits locations"); - // Stack space is allocated for all arguments starting from [%fp+BIAS+128]. + // Zero-sized struct arguments are lowered as `i64 inreg`s, and here they get + // a special treatment: + // - If there are still free integer registers avaliable, it's lowered as if + // it's a normal i64. + // - After that, the arguments will occupy a f64 register starting from %d12 + // (SP::D6 in LLVM parlance), but *not* occupy a stack slot. + // - After the f64 register slots are exhausted, the arguments will not be + // allocated at all. + // This is to match GCC's lowering behavior. + // See also: + // https://github.com/llvm/llvm-project/issues/213561#issuecomment-5377850094 + if (!IsReturn && LocVT == MVT::i64 && ArgFlags.isInReg() && + State.isAllocated(SP::I5)) { + Register Reg = State.AllocateReg(ArgDs); + if (Reg) + State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); + return true; + } + + // Stack space is allocated for all non-inreg arguments and the first six + // i64 inreg arguments starting from [%fp+BIAS+128]. unsigned size = (LocVT == MVT::f128) ? 16 : 8; Align alignment = (LocVT == MVT::f128 || ArgFlags.isSplit()) ? Align(16) : Align(8); unsigned Offset = State.AllocateStack(size, alignment); - unsigned Reg = 0; + Register Reg = 0; - if (LocVT == MVT::i64 && Offset < 6*8) + if (LocVT == MVT::i64) { // Promote integers to %i0-%i5. - Reg = SP::I0 + Offset/8; - else if (LocVT == MVT::f64 && Offset < 16*8) - // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15). - Reg = SP::D0 + Offset/8; - else if (LocVT == MVT::f32 && Offset < 16*8) + if (ArgFlags.isSplit() && + ((State.getFirstUnallocated(ArgIs) - SP::I0) % 2 != 0)) { + // We are receiving a split i128, pad so we have an even-numbered + // integer register. + State.AllocateReg(ArgIs); + State.AllocateReg(ArgDs); // Shadow allocation. + } + + Reg = State.AllocateReg(ArgIs); + State.AllocateReg(ArgDs); // Shadow allocation. + } else if (LocVT == MVT::f32) { // Promote floats to %f1, %f3, ... - Reg = SP::F1 + Offset/4; - else if (LocVT == MVT::f128 && Offset < 16*8) + Reg = State.AllocateReg(ArgFs); + State.AllocateReg(ArgIs); // Shadow allocation. + } else if (LocVT == MVT::f64) { + // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15). + Reg = State.AllocateReg(ArgDs); + State.AllocateReg(ArgIs); // Shadow allocation. + } else if (LocVT == MVT::f128) { // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7). - Reg = SP::Q0 + Offset/16; + Reg = State.AllocateReg(ArgQs); + // Shadow allocation. + for (int i = 0; i < 2; i++) + State.AllocateReg(ArgIs); + } // Promote to register when possible, otherwise use the stack slot. if (Reg) { diff --git a/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll b/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll new file mode 100644 index 00000000000000..6dcf3ca0b14836 --- /dev/null +++ b/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll @@ -0,0 +1,47 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc < %s -mtriple=sparcv9 | FileCheck %s -check-prefix=SPARC64 + +;; `i64 inreg`s get a special treatment when lowered + +declare void @callee1(i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i8 noundef zeroext) + +define void @caller1() nounwind { +; SPARC64-LABEL: caller1: +; SPARC64: ! %bb.0: ! %entry +; SPARC64-NEXT: save %sp, -192, %sp +; SPARC64-NEXT: mov 90, %i0 +; SPARC64-NEXT: call callee1 +; SPARC64-NEXT: stx %i0, [%sp+2223] +; SPARC64-NEXT: ret +; SPARC64-NEXT: restore +entry: + tail call void @callee1(i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i8 noundef zeroext 90) + ret void +} + +declare void @callee2(i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i8 noundef zeroext) + +define void @caller2() nounwind { +; SPARC64-LABEL: caller2: +; SPARC64: ! %bb.0: ! %entry +; SPARC64-NEXT: save %sp, -192, %sp +; SPARC64-NEXT: mov 90, %i0 +; SPARC64-NEXT: call callee2 +; SPARC64-NEXT: stx %i0, [%sp+2223] +; SPARC64-NEXT: ret +; SPARC64-NEXT: restore +entry: + tail call void @callee2(i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i8 noundef zeroext 90) + ret void +} + + +define noundef float @floatret(i64 inreg %.coerce, i64 inreg %.coerce1, i64 inreg %.coerce2, i64 inreg %.coerce3, i64 inreg %.coerce4, i64 inreg %.coerce5, i64 inreg %.coerce6, float noundef returned %v) nounwind { +; SPARC64-LABEL: floatret: +; SPARC64: ! %bb.0: ! %entry +; SPARC64-NEXT: fmovs %f15, %f0 +; SPARC64-NEXT: retl +; SPARC64-NEXT: nop +entry: + ret float %v +} >From 363e4fe075d7f99bdf82f426eb473941616b8fea Mon Sep 17 00:00:00 2001 From: Koakuma <[email protected]> Date: Sun, 15 Nov 2026 09:38:16 +0700 Subject: [PATCH 2/3] Remove undef --- llvm/test/CodeGen/SPARC/inreg-i64-v9.ll | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll b/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll index 6dcf3ca0b14836..1bc0a6ad7bef0e 100644 --- a/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll +++ b/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll @@ -3,7 +3,7 @@ ;; `i64 inreg`s get a special treatment when lowered -declare void @callee1(i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i8 noundef zeroext) +declare void @callee1(i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i8 zeroext) define void @caller1() nounwind { ; SPARC64-LABEL: caller1: @@ -15,11 +15,11 @@ define void @caller1() nounwind { ; SPARC64-NEXT: ret ; SPARC64-NEXT: restore entry: - tail call void @callee1(i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i8 noundef zeroext 90) + tail call void @callee1(i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i8 zeroext 90) ret void } -declare void @callee2(i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i8 noundef zeroext) +declare void @callee2(i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i64 inreg, i8 zeroext) define void @caller2() nounwind { ; SPARC64-LABEL: caller2: @@ -31,12 +31,12 @@ define void @caller2() nounwind { ; SPARC64-NEXT: ret ; SPARC64-NEXT: restore entry: - tail call void @callee2(i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i64 inreg undef, i8 noundef zeroext 90) + tail call void @callee2(i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i64 inreg poison, i8 zeroext 90) ret void } -define noundef float @floatret(i64 inreg %.coerce, i64 inreg %.coerce1, i64 inreg %.coerce2, i64 inreg %.coerce3, i64 inreg %.coerce4, i64 inreg %.coerce5, i64 inreg %.coerce6, float noundef returned %v) nounwind { +define nopoison float @floatret(i64 inreg %.coerce, i64 inreg %.coerce1, i64 inreg %.coerce2, i64 inreg %.coerce3, i64 inreg %.coerce4, i64 inreg %.coerce5, i64 inreg %.coerce6, float nopoison returned %v) nounwind { ; SPARC64-LABEL: floatret: ; SPARC64: ! %bb.0: ! %entry ; SPARC64-NEXT: fmovs %f15, %f0 >From 67398eb5684ae9a5820bd1f0287c375045d0aa62 Mon Sep 17 00:00:00 2001 From: Koakuma <[email protected]> Date: Sun, 15 Nov 2026 12:06:30 +0700 Subject: [PATCH 3/3] Fix typo --- llvm/test/CodeGen/SPARC/inreg-i64-v9.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll b/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll index 1bc0a6ad7bef0e..101acd332114bf 100644 --- a/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll +++ b/llvm/test/CodeGen/SPARC/inreg-i64-v9.ll @@ -36,7 +36,7 @@ entry: } -define nopoison float @floatret(i64 inreg %.coerce, i64 inreg %.coerce1, i64 inreg %.coerce2, i64 inreg %.coerce3, i64 inreg %.coerce4, i64 inreg %.coerce5, i64 inreg %.coerce6, float nopoison returned %v) nounwind { +define float @floatret(i64 inreg %.coerce, i64 inreg %.coerce1, i64 inreg %.coerce2, i64 inreg %.coerce3, i64 inreg %.coerce4, i64 inreg %.coerce5, i64 inreg %.coerce6, float %v) nounwind { ; SPARC64-LABEL: floatret: ; SPARC64: ! %bb.0: ! %entry ; SPARC64-NEXT: fmovs %f15, %f0 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
