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

[LoopInterchange] Assertion on phi nodes

Open scui-ibm opened this issue 4 months ago • 3 comments

Reduced test case t.ll:

target datalayout = "E-m:a-Fi64-i64:64-i128:128-p:64:64-n32:64-f64:32:64-S128-v256:256:256-v512:512:512"
target triple = "powerpc64-ibm-aix7.2.0.0"

%_elem_type_of_t = type <{ i32 }>

; Function Attrs: nofree norecurse nosync nounwind memory(write, argmem: readwrite, inaccessiblemem: none, target_mem: none)
define noundef i64 @main(ptr nofree readonly captures(none) %a, ptr nofree readonly captures(none) %s_ix_dim_0_1192, ptr nofree readonly captures(none) %x_rvo_based_addr_1395) local_unnamed_addr #0 {
entry:
  %t2 = alloca [600 x %_elem_type_of_t], align 4
  %t_rvo_based_addr_1484 = getelementptr i8, ptr %t2, i64 -84
  br label %_loop_1_do_

_loop_1_do_:                                      ; preds = %if_merge.end, %entry
  %i_1.41 = phi i64 [ %_loop_1_update_loop_ix, %if_merge.split ], [ 1, %entry ]
  %_ix_x_len1493 = shl nuw nsw i64 %i_1.41, 2
  %invariant.gep = getelementptr i8, ptr %t_rvo_based_addr_1484, i64 %_ix_x_len1493
  %_ix_x_len1234 = mul nuw nsw i64 %i_1.41, 160
  %s_ix_dim_0_1235 = getelementptr i8, ptr null, i64 %_ix_x_len1234
  br label %_loop_2_do_

_loop_2_do_:                                      ; preds = %if_merge, %_loop_1_do_
  %indvars.iv = phi i64 [ 17, %_loop_1_do_ ], [ %indvars.iv.next.pre-phi, %if_merge ]
  %0 = shl nuw i64 %indvars.iv, 1
  %s_ix_dim_1_1197 = getelementptr i8, ptr %s_ix_dim_0_1192, i64 %0
  %_val_s_1198 = load i32, ptr %s_ix_dim_1_1197, align 4
  %_leq_tmp1199 = icmp sgt i32 %_val_s_1198, 0
  br i1 %_leq_tmp1199, label %if_then, label %if_else

if_then:                                          ; preds = %_loop_2_do_
  store i32 poison, ptr %s_ix_dim_0_1235, align 4
  %.pre = add nsw i64 %indvars.iv, -1
  br label %if_merge

if_else:                                          ; preds = %_loop_2_do_
  %1 = mul nuw i64 %indvars.iv, 80
  %2 = getelementptr i8, ptr %invariant.gep, i64 %1
  %gep = getelementptr i8, ptr %2, i64 1040
  %3 = add nsw i64 %indvars.iv, -1
  %_ix_x_len1499 = mul nuw nsw i64 %3, 80
  %t_ix_dim_0_1500 = getelementptr i8, ptr %t_rvo_based_addr_1484, i64 %_ix_x_len1499
  %_val_t_ = load i32, ptr %t_ix_dim_0_1500, align 4
  %_val_t_1517 = load i32, ptr %a, align 4
  %_add_tmp1518 = or i32 %_val_t_1517, %_val_t_
  store i32 %_add_tmp1518, ptr %gep, align 4
  br label %if_merge

if_merge:                                         ; preds = %if_else, %if_then
  %indvars.iv.next.pre-phi = phi i64 [ %3, %if_else ], [ %.pre, %if_then ]
  %_geq_tmp = icmp samesign ugt i64 %indvars.iv, 5
  br i1 %_geq_tmp, label %_loop_2_do_, label %if_merge.split

if_merge.split:                                   ; preds = %if_merge
  %_loop_1_update_loop_ix = add nuw nsw i64 %i_1.41, 1
  %exitcond.not = icmp eq i64 %_loop_1_update_loop_ix, 9
  br i1 %exitcond.not, label %exit, label %_loop_1_do_

exit:                                             ; preds = %if_merge.split
  ret i64 0
}

attributes #0 = { nofree norecurse nosync nounwind memory(write, argmem: readwrite, inaccessiblemem: none, target_mem: none) }

Run: opt -passes='loop-interchange' t.ll

Assertion:

opt: llvm/lib/Transforms/Scalar/LoopInterchange.cpp:2147: auto (anonymous namespace)::LoopInterchangeTransform::transform(ArrayRef<Instruction *>, ArrayRef<Instruction *>)::(anonymous class)::operator()() const: Assertion `!isa<PHINode>(WorkList[i]) && "MoveInstructions does not support PHI nodes"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: opt -passes=loop-interchange t.ll
1. Running pass "function(loop(loop-interchange))" on module "t.ll"
2. Running pass "loop(loop-interchange)" on function "main"
#0 0x00000c5104b6dc60 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/scui/llvm/scui-ibm/build/bin/opt+0x4d3dc60)
 #1 0x00000c5104b6e524 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #2 0x00000c5104b69a54 llvm::sys::RunSignalHandlers() (/home/scui/llvm/scui-ibm/build/bin/opt+0x4d39a54)
 #3 0x00000c5104b6f16c SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #4 0x00006492e76d0444 (linux-vdso64.so.1+0x444)
 #5 0x00006492e6ab55fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x00006492e6ab55fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #7 0x00006492e6ab55fc pthread_kill ./nptl/pthread_kill.c:89:10
 #8 0x00006492e6a4bd5c raise ./signal/../sysdeps/posix/raise.c:26:13
 #9 0x00006492e6a2645c abort ./stdlib/abort.c:79:7
#10 0x00006492e6a3d824 __assert_fail_base ./assert/assert.c:96:3
#11 0x00006492e6a3d8c4 __assert_fail ./assert/assert.c:105:3
#12 0x00000c5106fb1944 (anonymous namespace)::LoopInterchangeTransform::transform(llvm::ArrayRef<llvm::Instruction*>, llvm::ArrayRef<llvm::Instruction*>)::$_0::operator()() const LoopInterchange.cpp:0:0
#13 0x00000c5106fa50e4 (anonymous namespace)::LoopInterchange::processLoop(llvm::SmallVectorImpl<llvm::Loop*>&, unsigned int, unsigned int, std::__1::vector<std::__1::vector<char, std::__1::allocator<char>>, std::__1::allocator<std::__1::vector<char, std::__1::allocator<char>>>>&, (anonymous namespace)::CacheCostManager&) LoopInterchange.cpp:0:0

scui-ibm avatar Jul 16 '26 14:07 scui-ibm

The failure is due to recent changes on LoopInterchange. If https://github.com/llvm/llvm-project/pull/202863 is reverted, the assertion is different but still related to the handling of phis:

PHINode should have one entry for each predecessor of its parent basic block!
  %7 = phi i64 [ %5, %if_else ], [ %4, %if_then ]
Instruction does not dominate all uses!
  %5 = add nsw i64 %indvars.iv, -1
  %7 = phi i64 [ %5, %if_else ], [ %4, %if_then ]
Instruction does not dominate all uses!
  %4 = add nsw i64 %indvars.iv, -1
  %7 = phi i64 [ %5, %if_else ], [ %4, %if_then ]
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: opt -passes=loop-interchange t.ll
1.	Running pass "verify" on module "t.ll"
 #0 0x000000b619e8dac0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (opt+0x4d3dac0)

scui-ibm avatar Jul 16 '26 14:07 scui-ibm

Sorry for the late response. I'll take a look.

kasuga-fj avatar Jul 24 '26 16:07 kasuga-fj

Minimized reproducer (assisted-by AI)

define void @f(ptr %A) {
entry:
  br label %outer.header

outer.header:
  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
  br label %inner.header

inner.header:
  %j = phi i64 [ 0, %outer.header ], [ %j.next.phi, %inner.latch ]
  %j.next = add i64 %j, 1
  %gep = getelementptr [256 x i8], ptr %A, i64 %j, i64 %i
  store i8 0, ptr %gep
  br label %inner.latch

inner.latch:
  %j.next.phi = phi i64 [ %j.next, %inner.header ]
  %ec.j = icmp eq i64 %j.next, 256
  br i1 %ec.j, label %outer.latch, label %inner.header

outer.latch:
  %i.next = add i64 %i, 1
  %ec.i = icmp eq i64 %i.next, 256
  br i1 %ec.i, label %exit, label %outer.header

exit:
  ret void
}

kasuga-fj avatar Jul 24 '26 17:07 kasuga-fj