Issue 239211
Summary [SelectionDAG] `llvm.frexp` with an `i64` exponent returns uninitialized upper 32 bits
Labels new issue
Assignees
Reporter lin334141-cell
    # [SelectionDAG] `llvm.frexp` with an `i64` exponent returns uninitialized upper 32 bits

## Summary

In `llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp`, `expandMultipleResultFPLibCall` expands `ISD::FFREXP` through the standard C libcalls `frexpf` (for `f32`) and `frexp` (for `f64`). 

The second parameter of `frexp` is an `int *` (4 bytes). However, when the intrinsic declares an `i64` exponent (e.g. `llvm.frexp.f32.i64`), `expandMultipleResultFPLibCall` allocates an 8-byte stack slot based on the **declared** result type, passes its address to `frexp` as `int *`, and then loads the full **64-bit** result back from that slot.

Because `frexp` only writes the low 32 bits (`int`), the upper 32 bits of the loaded `i64` exponent are uninitialized stack garbage:

```asm
; Generated AArch64 assembly:
add  x0, sp, #8      ; pass address of slot as int*
bl   _frexpf         ; writes 4 bytes
ldr  x0, [sp, #8]    ; loads 8 bytes — upper 32 bits are uninitialized garbage!
```

This defect affects both scalar forms (`llvm.frexp.f32.i64`, `llvm.frexp.f64.i64`) and vector forms whose exponents do not undergo splitting/widening (e.g. `llvm.frexp.v2f32.v2i64`).

## Minimal Reproducer

```llvm
; frexp-i64.ll
target triple = "arm64-apple-macos"

declare { <2 x float>, <2 x i64> } @llvm.frexp.v2f32.v2i64(<2 x float>)
declare { float, i64 } @llvm.frexp.f32.i64(float)
declare i32 @printf(ptr, ...)

@pf = private unnamed_addr constant [27 x i8] c"v2f32_64: e0=%lld e1=%lld\0A\00"
@ps = private unnamed_addr constant [38 x i8] c"scalar f32.i64: exp=%lld (want -148)\0A\00"

define <2 x i64> @test_vector(<2 x float> %x) {
  %r = call { <2 x float>, <2 x i64> } @llvm.frexp.v2f32.v2i64(<2 x float> %x)
  %e = extractvalue { <2 x float>, <2 x i64> } %r, 1
  ret <2 x i64> %e
}

define i64 @test_scalar(float %f) {
  %r = call { float, i64 } @llvm.frexp.f32.i64(float %f)
  %e = extractvalue { float, i64 } %r, 1
  ret i64 %e
}

define i32 @main() {
entry:
  ; lane0: 2^39 (expected exp = 40)
  ; lane1: 2^-149 (smallest denormal, expected exp = -148)
  %vin = bitcast <2 x i32> <i32 1392508928, i32 1> to <2 x float>
  %e2 = call <2 x i64> @test_vector(<2 x float> %vin)
  %e20 = extractelement <2 x i64> %e2, i32 0
  %e21 = extractelement <2 x i64> %e2, i32 1
  call i32 (ptr, ...) @printf(ptr @pf, i64 %e20, i64 %e21)

  %f_denorm = bitcast i32 1 to float
  %s1 = call i64 @test_scalar(float %f_denorm)
  call i32 (ptr, ...) @printf(ptr @ps, i64 %s1)
  ret i32 0
}
```

### Direct Assembly Inspection

```bash
llc -O2 frexp-i64.ll -o -
```

In the compiled assembly for `@test_scalar`, the mismatch between 4-byte store and 8-byte load is directly visible:
```asm
_test_scalar:
    ...
    add x0, sp, #8
    bl  _frexpf
    ldr x0, [sp, #8]    ; <-- 8-byte load from 4-byte write
    ...
```

### Execution Results

Compile and run:
```bash
clang frexp-i64.ll -o run && ./run
```

#### Expected Behavior
```text
v2f32_64: e0=40 e1=-148
scalar f32.i64: exp=-148 (want -148)
```

#### Actual Behavior
```text
v2f32_64: e0=4294967336 e1=4294967148
scalar f32.i64: exp=8589934444 (want -148)
```
* `4294967336` = `0x1_00000028` (`2^32 + 40`)
* `4294967148` = `0x1_00000000 - 148`
* `8589934444` = `0x2_00000000 - 148`

The lower 32 bits contain the correct exponent values, while the upper 32 bits leak uninitialized stack memory.

## Suggested Fix

In `TargetLowering::expandMultipleResultFPLibCall` (`llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp`):
* Allocate the temporary stack slot using the libcall's parameter type (`MVT::i32` for `int *`).
* Load `MVT::i32` from the slot after the libcall returns.
* If the intrinsic's declared result type is larger (e.g., `MVT::i64`), explicitly sign-extend the loaded 32-bit value using `DAG.getNode(ISD::SIGN_EXTEND, ..., DeclaredVT, LoadedVal)`.

## Related Issues
- #224127 / PR #225886 and PR #224173 address vector widening/splitting derivations for `frexp`, but only for `i32` exponents. They do not address the libcall stack temporary width mismatch for `i64` exponents.

## Tested Versions
- LLVM `main` at commit `ca566d552f7a9e507388cdba9539dd6e36c94bef` (`llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp`)
- LLVM `24.0.0git` (`67005b724c917e1bdca50c82ecaa1b21a69244ed`, macOS arm64)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to