x86_64 opt-level=0: load through a pointer made from f64 bits uses the XMM register as the address base

Abierto
#165,994 8 comentarios 1 reacción 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
22/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp, rust
Área
compilers

Línea de trabajo

The fix belongs in X86FastISel::X86SelectAddress in llvm/lib/Target/X86/X86FastISel.cpp, where the BitCast case recurses without checking the operand type, unlike the IntToPtr case just below it. Reproduce first with the given rustc --emit=asm command plus -Cllvm-args=-verify-machineinstrs to confirm the FR64 base register, then rebuild LLVM and recheck the assembly. Done looks like movq %xmm0, %rax followed by movl (%rax), %eax, a clean verifier run, and a codegen test under LLVM's X86 tests.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

A-floating-point A-LLVM C-bug I-miscompile I-prioritize O-x86_64 S-found-in-wild T-compiler

On x86_64 at opt-level = 0, a pointer built from the bits of an f64 is loaded through the wrong register. LLVM's FastISel folds the bitcast double to i64 and the inttoptr into the load address and uses the XMM register as the base register. In the object file that register number is the general purpose register with the same number (xmm0 becomes rax), so the load reads through whatever rax holds.

Code
#[no_mangle]
#[allow(unnecessary_transmutes)]
pub unsafe fn read_through_f64(x: f64) -> u32 {
    let p = std::mem::transmute::<f64, usize>(x) as *const u32;
    *p
}
rustc --crate-type=lib --target x86_64-unknown-linux-gnu -Copt-level=0 -Cdebug-assertions=off --emit=asm m.rs

I expected movq %xmm0, %rax followed by movl (%rax), %eax. Instead:

read_through_f64:
	movl	(%xmm0), %eax
	retq

The machine verifier agrees (-Cllvm-args=-verify-machineinstrs):

*** Bad machine code: Illegal virtual register for instruction ***
- instruction: %3:gr32 = MOV32rm %1:fr64, 1, $noreg, 0, $noreg :: (load (s32) from %ir.3)
Expected a GR64 register, but got a FR64 register

The IR is fine:

define i32 @read_through_f64(double %x) unnamed_addr #0 {
start:
  %_3 = bitcast double %x to i64
  %p = inttoptr i64 %_3 to ptr
  %_0 = load i32, ptr %p, align 4
  ret i32 %_0
}

Same output for x86_64-pc-windows-msvc and x86_64-apple-darwin. -Cllvm-args=-fast-isel=false gives correct code. With debug assertions on, the alignment check splits the block and hides it; that is why the repro turns them off.

Where it bites

This crashes real code: wasmi 2.0.0 keeps an instance pointer in an f64 on x86_64, and a generic #[inline] path that gets instantiated in an opt-level = 0 crate (optimized dependency, unoptimized embedder) faults on Windows with rax holding a string length. See https://github.com/wasmi-labs/wasmi/issues/2061.

Cause

X86FastISel::X86SelectAddress in llvm/lib/Target/X86/X86FastISel.cpp looks past every BitCast, without checking that the operand is an integer or pointer:

  case Instruction::BitCast:
    // Look past bitcasts.
    return X86SelectAddress(U->getOperand(0), AM);

So it reaches the double argument and uses its FR64 register as the base. The IntToPtr case right below already checks the operand type. This is still the code on LLVM main today.

Version
rustc 1.94.0 (4a4ef493e 2026-03-02), LLVM 21.1.8

Also reported on 1.95.0 and 1.100.0 (via the wasmi issue).

Lenguaje dominante
Rust
Estrellas
122k
Forks
16.7k
Merge medio
2 d 16 h
PR fusionados (30 d)
515

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/rust

Todos los issues de rust-lang/rust

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.