Issue 239409
Summary [DFAJumpThreading] Wrong code: threading a path decided by a later state definition redirects a path carrying an earlier one
Labels new issue
Assignees
Reporter trevp
    
(written by AI!):

The following IR returns the wrong value after `opt -passes=dfa-jump-threading` alone.

```llvm
target triple = "x86_64-unknown-linux-gnu"

@fmt = private constant [11 x i8] c"result=%d\0A\00"
declare i32 @printf(ptr, ...)

define i32 @f(i1 %a, i1 %c) {
entry:
  br label %header

header:
  br i1 %a, label %def1, label %set

set:
  br label %def1

def1:
  %s1 = phi i32 [ 0, %header ], [ -1, %set ]
  br label %def2

def2:
  %s2 = phi i32 [ 0, %def1 ]
  br i1 %c, label %via, label %join

via:
  br label %join

join:
  %st = phi i32 [ %s2, %def2 ], [ %s1, %via ]
  switch i32 %st, label %header [
    i32 -1, label %none
    i32 0, label %found
  ]

none:
  ret i32 0

found:
  ret i32 1
}

define i32 @main() {
  %r = call i32 @f(i1 false, i1 true)
  call i32 (ptr, ...) @printf(ptr @fmt, i32 %r)
  ret i32 0
}
```

`@f(false, true)` must return 0:
- `%a = false` goes through `%set`, so `%s1 = -1`;
- `%c = true` goes through `%via`, so `%st = %s1 = -1`;
- the switch then takes `%none`.

```
$ llc -O0 -relocation-model=pic -filetype=obj repro.ll -o a.o && cc a.o -o a && ./a
result=0
$ opt -S -passes=dfa-jump-threading repro.ll -o after.ll
$ llc -O0 -relocation-model=pic -filetype=obj after.ll -o b.o && cc b.o -o b && ./b
result=1
```

`llubi` gives 0 before the pass and 1 after, and reports no undefined behaviour.

**Versions.** Reproduced with `opt -passes=dfa-jump-threading` alone, on x86_64 Linux:
- trunk: `opt` 24.0.0git on Compiler Explorer, release and assertions builds, on 2026-10-29. No
  assertion fires. `DFAJumpThreading.cpp` has not changed on main since 2026-10-23;
- 23.1.1 (`23.1.1-rust-1.99.0-stable`);
- 22.1.6 and 22.1.2 (`-rust-dev` builds);
- 18.1.3 (Ubuntu).

So the bug has been latent since at least LLVM 18. It became visible when the pass was enabled by
default (#169183): LLVM 22-based compilers produce correct code by default and are wrong only with
`-enable-dfa-jump-thread=true`.

It was found through rustc, which runs the pass by default at `-O2` since its LLVM 23 upgrade.

**Workaround:** `-enable-dfa-jump-thread=false`.

Relevant part of the output (23.1.1):

```llvm
def1.jt4294967295:
  %s1.jt4294967295 = phi i32 [ -1, %set ]
  br label %def2.jt0            ; the clone for state -1 jumps into the state-0 copy
...
join.jt0:
  %st.jt0 = phi i32 [ %s2.jt0, %def2.jt0 ], [ %s11, %via.jt0 ]
  br label %found               ; unconditional, although %s11 can be -1 here
```

### Analysis

Two consecutive blocks each define the switch state: `%def1` (`%s1`, 0 or -1), then `%def2` (`%s2`,
always 0). Among the paths the pass finds are these two:
- `set → def1 → def2 → via → join`, with state -1;
- `def1 → def2 → join`, with state 0, decided by `%def2`.

Both share the edge `def1 → def2`. When the second path is threaded, every copy of `%def1` is
redirected to `%def2.jt0`, including the copy made for state -1. But on `%def2.jt0`'s `via`
continuation the switch still reads `%s1`, not `%s2`. `join.jt0` branches straight to `%found`, and
`%def2.jt4294967295` is left with no predecessors.

LLVM 18 gets it wrong more simply: it clones `def2`, `via` and `join` for state 0, and `join.jt0`
receives `%s1` through `via.jt0`.

### Possibly related

- #211090 (open, no fix on main as of 2026-11-05). Its diagnosis, "it is incorrect to assume the
  switch condition is 0 on all the paths past set_next -> determine", seems to fit here. In both, a
  block that defines the state as 0 (`determine` there, `%def2` here) is treated as deciding the
  state on all of its continuations, including ones where the switch still reads an earlier
  definition. That may be the same root cause, but the two cases differ:
  - #211090's further-reduced IR (godbolt.org/z/77PhTddY5) is handled correctly by `opt` 18.1.3,
    while this IR is miscompiled there. Both are miscompiled by 22.1.2, 22.1.6 and 23.1.1. (The
    22.1.x results may bear on the tentative bisection to #195512 in #211090, since release/22.x
    does not contain #195512.)
  - #211090 has a back edge into the deciding block; this IR does not.

  I'm filing this separately for those reasons. Please close it as a duplicate if the fix for
  #211090 also fixes it.
- #195088, fixed by #195512 (included in 23.1.1). #195512 skips blocks that define several state
  phis. Here each definition is in its own block, so that check does not apply. Moving both phis
  into `%def1` makes 23.1.1 correct, but LLVM 18 still miscompiles that variant.

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

Reply via email to