[PowerPC] ppc64le ELFv2: preceding double parameter incorrectly shadows a GPR slot for a following i32 parameter
On powerpc64le-unknown-linux-gnu, when a function's parameter list has a floating-point argument immediately followed by an integer argument, LLVM delivers the integer argument to the callee in the wrong register (r4 instead of r3) — a genuine miscompile, not just a suboptimal-codegen issue.
Diagnosed via numba/numba#8489 (https://github.com/numba/numba/issues/8489): math.ldexp/math.frexp return wrong results (inf) exclusively on ppc64le, open since 2022, still reproducing (confirmed against this same LLVM 22.1.8). Numba's JIT (via llvmlite) emits IR calling small C helpers with exactly this (double, int) shape — a pattern any LLVM-IR-emitting JIT can hit.
Minimal reproducer
target triple = "powerpc64le-unknown-linux-gnu"
declare double @foo(double, i32)
define double @caller(double %x, i32 %e) {
entry:
%result = call double @foo(double %x, i32 %e)
ret double %result
}
Expected (ELFv2 ABI: a non-variadic FP argument passed in f1 does not consume a GPR slot): %e should reach foo in r3. Observed: %e reaches foo in r4 at both optimization levels
- llvmlite/LLVM 22.1.8
A possible patch could look like:
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -4598,9 +4608,28 @@
if (CallConv != CallingConv::Fast) {
ComputeArgOffset();
- /* Compute GPR index associated with argument offset. */
- GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
- GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
+ bool ArgConsumesFPRVROnly =
...
+ if (!ArgConsumesFPRVROnly) {
+ GPRShadowOffset = alignTo(GPRShadowOffset, Alignment);
+ /* Compute GPR index associated with argument offset. */
+ GPR_idx = (GPRShadowOffset - LinkageSize) / PtrByteSize;
+ GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
...
@@ -6272,6 +6301,11 @@
...
If this looks acceptable, we can follow-up with a PR from our local LLVM build
@llvm/issue-subscribers-backend-powerpc
Author: Memento "RC" Mori (MementoRC)
Diagnosed via numba/numba#8489 (https://github.com/numba/numba/issues/8489): math.ldexp/math.frexp return wrong results (inf) exclusively on ppc64le, open since 2022, still reproducing (confirmed against this same LLVM 22.1.8). Numba's JIT (via llvmlite) emits IR calling small C helpers with exactly this (double, int) shape — a pattern any LLVM-IR-emitting JIT can hit.
Minimal reproducer
target triple = "powerpc64le-unknown-linux-gnu"
declare double @<!-- -->foo(double, i32)
define double @<!-- -->caller(double %x, i32 %e) {
entry:
%result = call double @<!-- -->foo(double %x, i32 %e)
ret double %result
}
Expected (ELFv2 ABI: a non-variadic FP argument passed in f1 does not consume a GPR slot): %e should reach foo in r3. Observed: %e reaches foo in r4 at both optimization levels
- llvmlite/LLVM 22.1.8
A possible patch could look like:
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -4598,9 +4608,28 @@
if (CallConv != CallingConv::Fast) {
ComputeArgOffset();
- /* Compute GPR index associated with argument offset. */
- GPR_idx = (ArgOffset - LinkageSize) / PtrByteSize;
- GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
+ bool ArgConsumesFPRVROnly =
...
+ if (!ArgConsumesFPRVROnly) {
+ GPRShadowOffset = alignTo(GPRShadowOffset, Alignment);
+ /* Compute GPR index associated with argument offset. */
+ GPR_idx = (GPRShadowOffset - LinkageSize) / PtrByteSize;
+ GPR_idx = std::min(GPR_idx, Num_GPR_Regs);
...
@@ -6272,6 +6301,11 @@
...
If this looks acceptable, we can follow-up with a PR from our local LLVM build
Expected (ELFv2 ABI: a non-variadic FP argument passed in f1 does not consume a GPR slot): %e should reach foo in r3. Observed: %e reaches foo in r4 at both optimization levels
The latest version of the PPC ELF V2 ABI is available here. In the outline of the argument passing (section 2.2.4.1) it describes how to allocate the registers for different C types. gr is the next available gpr, and its initialized as INITIALIZE: If the function return type requires a storage buffer, set gr = 4; else set gr = 3. We don't have any extra storage requirement for a double return value so we start at r3.
The algorithm for float arguments is:
float:
// float is passed in one FPR.
// double is passed in one FPR.
// IBM EXTENDED PRECISION is passed in the next two FPRs.
// IEEE BINARY 128 QUADRUPLE PRECISION is passed in one VR.
// _Decimal32 is passed in the lower half of one FPR.
// _Decimal64 is passed in one FPR.
// _Decimal128 is passed in an even-odd FPR pair, skipping an FPR if necessary.
if (register_type_used (type (argument)) == vr)
// Assumes == vr is true for IEEE BINARY 128 QUADRUPLE PRECISION.
goto use_vr;
fr += align_pad(fr,type(argument))
// Assumes align_pad = 8 for _Decimal128 if fr is odd; otherwise = 0.
if fr > 14
goto mem_argument
n_fregs = n_fregs_for_type(argument)
// Assumes n_fregs_for_type == 2 for IBM EXTENDED PRECISION
// or _Decimal128, == 1 for float, double, _Decimal32 or _Decimal64.
pass(FPR,fr,argument)
fr += n_fregs
gr += size_in_DW(argument)
break;
with the relevant part being gr += size_in_DW(argument) which means even though the float argument is passed in an fpr we still skip a gpr for it. You can see other powerpc64 compilers also exhibit the same behaviour. For example compiling
int callee(float f, int i) {
return i;
}
with gcc produces the assembly
callee:
.LFB0:
.cfi_startproc
mr 3,4
blr
which move the argument i from register r4 into r3 to use as the return value.
Numba's JIT (via llvmlite) emits IR calling small C helpers with exactly this (double, int) shape — a pattern any LLVM-IR-emitting JIT can hit.
Does the code that sets up the call into the LLVM jited code put the arguments in the right registers? Did you think the integer argument was supposed to go into r3 because thats what the setup code is doing?
@mandlebug Thank you for your time and pointers to our erroneous hypothesis, we followed it to a numba "fix" for the wrong reasons.
It turns out that the ppc64le inf in numba#8489 issue seems to be a missing signext parameter attribute on numba's extern declaration of the exponent, not a register or ordering defect. We no longer think that there is anything to fix with LLVM
I am a conda-forge maintainer, not a numba expert, I dug into the issue when adding more test coverage to numba for the PR on a workaround and only realized the wrong hypothesis a few hours ago
Thank you for your time