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

Missed dead branch elimination due to PHI analysis

Open AAAA-I opened this issue 8 months ago • 3 comments

The following reduced IR is derived from https://github.com/darktable-org/darktable/blob/db70f8981b98587057e9ee328c4e0804b3cbe30d/src/iop/Permutohedral.h#L710

Godbolt: https://godbolt.org/z/oWxPE37sT

In the following reduced IR, Block 6 is a dead branch. Although %.not has two incoming values (%3 and true), when it takes the value %3 (from %1), the corresponding PHI value %.06 is 0, making %exitcond.not evaluate to false, and thus control cannot reach block 4. Therefore, block 4 is only reachable via %._crit_edge, along which %.not is always true. As a result, the branch br i1 %.not in block 4 is effectively unconditional, and the successor label %6 is a dead branch.

define void @PermutohedralLattice<3, 2>::blur() const(i32 %0) local_unnamed_addr {
  %2 = tail call ptr @HashTablePermutohedral<3, 2>::getValues() const()
  %3 = icmp eq ptr %2, null
  br label %.preheader

.preheader:
  %.06 = phi i32 [ 0, %1 ], [ %0, %._crit_edge ]
  %.not = phi i1 [ %3, %1 ], [ true, %._crit_edge ]
  br label %.lr.ph

4:
  br i1 %.not, label %common.ret, label %6

._crit_edge:
  %exitcond.not = icmp eq i32 %.06, 1
  br i1 %exitcond.not, label %4, label %.preheader

.lr.ph:
  %5 = tail call i64 @HashTablePermutohedral<3, 2>::size() const()
  %.not7 = icmp eq i64 %5, 0
  br i1 %.not7, label %._crit_edge, label %.lr.ph

common.ret:
  ret void

6:
  %7 = tail call i64 @HashTablePermutohedral<3, 2>::size() const()
  br label %common.ret
}

AAAA-I avatar Mar 25 '26 10:03 AAAA-I

I’d like to work on this.

I reproduced the issue on current main. From a quick investigation, this looks like a missed JumpThreading opportunity around PHI-related predecessor-edge reasoning, rather than just a missing pass in the default pipeline.

Lane0218 avatar Mar 25 '26 15:03 Lane0218

I’d like to work on this.

I reproduced the issue on current main. From a quick investigation, this looks like a missed JumpThreading opportunity around PHI-related predecessor-edge reasoning, rather than just a missing pass in the default pipeline.

Thanks for picking this up!

AAAA-I avatar Mar 26 '26 14:03 AAAA-I

I’ve opened a fix for this here: #190184.

It teaches JumpThreading to infer constants from correlated PHIs on predecessor edges, which is enough to fold the dead branch in the reduced reproducer.

Lane0218 avatar Apr 02 '26 14:04 Lane0218