x86: Correctly pass larger structs/types in registers with -Zregparm by sulix · Pull Request #147628 · rust-lang/rust (original) (raw)

Fixes #145694. I think this should be the last compiler fix needed to unblock 32-bit x86 support for Rust-for-linux (Rust-for-Linux/linux#78)

Tracking issue for -Zregparm: #131749

The -Zregparm option for 32-bit x86 modifies the calling convention to pass a number of arguments in registers instead of on the stack. (This is primarily used by the Linux kernel.)

Currently, rustc will only pass primitive integer types in registers, which seems to match the gcc documentation[1], which says that arguments "of integral type" are passed as registers. This also matches the fastcall and vectorcall conventions on windows.

However, it seems that any type — most particularly structs — should be passed as a register if possible. This matches what clang and gcc do, and so avoids an ABI mismatch when linking with C code (which, after all, is the whole point of supporting -Zregparm).

Note that I haven't tested this particularly thoroughly outside the Linux kernel usecase, and in particular haven't tried to implement any 'assembly-llvm' tests, nor test for regressions against fastcall/vectorcall.