Issue 244583
Summary [fuzzer-found][GlobalISel] p2i_to_i2p combine folds `inttoptr(ptrtoint p to i16)` to `p`: the truncation to the narrow integer is lost
Labels backend:RISC-V, miscompilation, llvm:globalisel, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

IR-only reproducer (clang emits pointer-sized ptrtoint/inttoptr plus a separate trunc/zext). Run on x86-64 with `llc -O2 -mtriple=x86_64-linux-gnu -relocation-model=pic -global-isel repro.ll -o repro.s && clang repro.s -o repro && ./repro`. `main` prints `1` if `g(&buf) == (&buf & 0xffff)`, then bits 16..47 of the result (expected `0`).

```llvm
; [GlobalISel] p2i_to_i2p folds inttoptr(ptrtoint p to i16) to p: the truncation to
; 16 bits (and inttoptr's zero-extension back) is lost.
; Godbolt: compiler "llc (trunk)" with -O2 -mtriple=riscv64 -global-isel
;   buggy:   g: ret (returns p unchanged)
;   correct: g: slli a0,a0,48; srli a0,a0,48; ret   (p & 0xffff; what SelectionDAG emits)
; Also aarch64 -global-isel (just `ret` instead of `and x0, x0, #0xffff`) and
; x86_64 -global-isel (`movq %rdi, %rax`).
@buf = global [4 x i8] c"abcd", align 4
@fmt = private constant [4 x i8] c"%d\0A\00"

define ptr @g(ptr %p) noinline {
  %i = ptrtoint ptr %p to i16
  %q = inttoptr i16 %i to ptr
  ret ptr %q
}

declare i32 @printf(ptr, ...)

define i32 @main() {
  %r = call ptr @g(ptr @buf)
  %ri = ptrtoint ptr %r to i64
  %b = ptrtoint ptr @buf to i64
  %e = and i64 %b, 65535
  %ok = icmp eq i64 %ri, %e
  %o = zext i1 %ok to i32
  call i32 (ptr, ...) @printf(ptr @fmt, i32 %o)
  %hi = lshr i64 %ri, 16
  %h = trunc i64 %hi to i32
  call i32 (ptr, ...) @printf(ptr @fmt, i32 %h)
  ret i32 0
}
```

Resulted in this output:
```
$ ./repro # built with -global-isel
0
1440809423
$ ./repro            # built without -global-isel (SelectionDAG)
1
0
```

| build | expected | actual |
|-------|----------|--------|
| -global-isel | 1, 0 | 0, 1440809423 (the high pointer bits survive) |
| SelectionDAG | 1, 0 | 1, 0 |

Code for `g` with riscv64 `-O2 -global-isel` is a bare `ret`. SelectionDAG emits `slli a0, a0, 48; srli a0, a0, 48; ret`. x86-64 `-global-isel` gives `movq %rdi, %rax; retq`.

Trunk: https://godbolt.org/z/3q7hEWExq (llc trunk, `-O2 -mtriple=riscv64 -global-isel`; reproduces as of 2026-10-22, also on aarch64)

Root cause: The `p2i_to_i2p` rule in llvm/include/llvm/Target/GlobalISel/Combine.td (part of `all_combines`) rewrites `G_INTTOPTR(G_PTRTOINT x)` to `x`. Its only check is that the source and destination pointer types are equal. It never checks that the intermediate integer is pointer-sized. `ptrtoint ptr %p to i16` keeps only the low 16 bits and `inttoptr i16` zero-extends them, so the correct result is `p & 0xffff`. The sibling rules `i2p_to_p2i_*` do handle the sizes. The older C++ matcher had the same gap, so the bug is long-standing.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to