https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128956
--- Comment #2 from liu zhenyu <lzy20000419 at outlook dot com> --- (In reply to Drea Pinski from comment #1) > This might be a problem on the caller side. Someone needs to go read the abi > to check which side is correct. Thanks for looking. I checked the ABI text, the caller side and clang; summary below. The N32/N64 ABI only says "Any other struct or union results of at most 128 bits are returned in $2 (first 64 bits) and $3 (remainder, if necessary)" (MIPSpro N32 ABI Handbook, ch. 2: https://techpubs.jurassic.nl/library/manuals/2000/007-2816-005/sgi_html/ch02.html). The extension rule in the same chapter is stated for integers only: "32-bit integer (int) parameters are always sign-extended when passed in registers, whether of signed or unsigned type." For aggregates it describes the register as the memory image of the struct ("struct parameters are always left-justified ... the struct is laid out in the save area memory exactly as if it were allocated normally"). So for a 4-byte struct returned on little-endian the ABI does not say what bits 63..32 of $2 hold; read strictly, a caller should not rely on them. LLVM reads it that way: RetCC_MipsN promotes inreg aggregate returns with CCPromoteToType<i64>, i.e. any-extend (https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/Mips/MipsCallingConv.td#L220-L224), and clang's caller re-extends after the call: jalr $25 sll $2,$2,0 GCC's caller instead models the result as (reg:SI 2) (expand dump: "(set (reg:SI 2 $2) (call ..."), i.e. as an already sign-extended SImode value: as_long() in the testcase returns $2 unchanged, and "return get (p).b;" becomes a bare "sra $2,$2,16" after the call. GCC's callee does not match its own caller, though. For struct { int x; } (SImode) it returns "lw $2,0($4)", sign-extended, and clang's callee also hands back a sign-extended value (lwl/lwr, or sll/or). Only the BLKmode path differs: copy_blkmode_to_reg builds the value in DImode and then takes (subreg:SI (reg:DI 201) 0) with no truncation, so $2 ends up zero-extended (move $2,$0; dins $2,$3,0,16; dins $2,$3,16,16). Interop test on qemu-mips64el (trunk bfd6e895d9ec vs clang 19.1.7, -O1 -march=mips64r2 -mabi=64, callee and caller in separate files): gcc callee + gcc caller fails; gcc callee + clang caller, clang callee + gcc caller and clang + clang all pass. So it only breaks when both sides are GCC. Big-endian is not affected: there the struct is returned in the upper half (mips_return_in_msb) and the caller extracts it with dext. My reading: the ABI leaves the upper half unspecified, so strictly the caller-side assumption is the part the ABI does not promise. In practice, though, every other producer I checked (GCC's own SImode path, clang) returns a sign-extended value, and GCC is inconsistent with itself either way. The callee-side change in the description (convert_to_mode instead of gen_lowpart in copy_blkmode_to_reg, which adds "sll $2,$2,0") is the one I tested; it also keeps new callees working with callers already compiled by GCC. A caller-side change would also guard against callees that leave the upper half undefined, but I have not written or tested one.
