llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[InstCombine] Fixpoint not reached: one-use trunc fold in foldICmpAndConstConst unblocked late

Open arie2026 opened this issue 3 weeks ago • 3 comments

instcombine (default verify-fixpoint) needs a second iteration. In iteration 1, icmp eq (and (trunc W), 1), 0 is visited while the trunc still has a second user, so the one-use fold to icmp eq (and W, 1), 0 is blocked. That second user is folded away later in the same iteration, but the icmp is not revisited.

Reproducer (opt -passes=instcombine, also reproduces on f2808fc79 and 5e457cda8d5a):

define i1 @f(i32 %x) {
  %ctlz = call i32 @llvm.ctlz.i32(i32 %x, i1 false)
  %sub = sub i32 32, %ctlz
  %t = trunc i32 %sub to i8
  %a = and i8 %t, 1
  %c1 = icmp eq i8 %a, 0
  %c2 = icmp eq i8 %t, 0
  %r = or i1 %c1, %c2
  ret i1 %r
}

declare i32 @llvm.ctlz.i32(i32, i1)
$ opt -passes=instcombine -disable-output test.ll
LLVM ERROR: Instruction Combining on f did not reach a fixpoint after 1 iterations. Use 'instcombine<no-verify-fixpoint>' or function attribute 'instcombine-no-verify-fixpoint' to suppress this error.

After one iteration (instcombine<no-verify-fixpoint>):

  %ctlz = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %x, i1 false)
  %1 = trunc nuw nsw i32 %ctlz to i8
  %a = and i8 %1, 1
  %c1 = icmp eq i8 %a, 0
  %c2 = icmp eq i32 %x, 0
  %r = or i1 %c1, %c2
  ret i1 %r

A second iteration gives:

  %ctlz = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %x, i1 false)
  %a1 = and i32 %ctlz, 1
  %c1 = icmp eq i32 %a1, 0
  ret i1 %c1

Expected: a fixpoint after one iteration (the single-iteration result should equal the second one).

Notes: the blocked fold is icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1' in foldICmpAndConstConst (m_OneUse(m_Trunc(...))). The trunc's other user icmp eq i8 %t, 0 is rewritten to icmp eq i32 %x, 0 after that. instcombine<max-iterations=2> succeeds. Low priority, like other fixpoint reports.

Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were assisted by an LLM; verified the reproducer on 9dce18f0e.

Assisted-by: Claude (Anthropic)

arie2026 avatar Nov 11 '26 20:11 arie2026

@nikic: Please elaborate.

EugeneZelenko avatar Nov 11 '26 20:11 EugeneZelenko

Fixpoint iteration failures are not bugs.

This is explicitly indicated by the use of reportFatalUsageError() and produces a corresponding error message with a graceful exit instead of abort.

nikic avatar Nov 11 '26 20:11 nikic

Fixpoint iteration failures are not bugs.

This is explicitly indicated by the use of reportFatalUsageError() and produces a corresponding error message with a graceful exit instead of abort.

Thanks for the explanation. I wasn't aware that fixpoint failures are reported via reportFatalUsageError by design. I will be more careful when posting an issue next time.

arie2026 avatar Nov 11 '26 21:11 arie2026