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

[Bug][Utils] cloneLoopWithPreheader did not update LoopInfo correctly

Open CongzheUalberta opened this issue 7 months ago • 4 comments

The way to reproduce the bug: inside the function cloneLoopWithPreheader() in llvm/lib/Transforms/Utils/CloneFunction.cpp, put a call to LI->verify(*DT) at the very beginning and another call to LI->verify(*DT) at the very end of the function. Then run check-llvm-transforms, 53 tests would fail including LoopBoundSplit, LoopDistribute, LoopFlatten, LoopLoadElim and LoopVersioning.

If we only put one call to LI->verify(*DT) at the very beginning of cloneLoopWithPreheader(), then all tests could pass. This means LI is valid when we entered the function.

Therefore, all 53 test failures above were due to failing the LI.verify(DT) at the very end of cloneLoopWithPreheader(). Hence cloneLoopWithPreheader() breaks LI whereas it is supposed to keep LI updated correctly (see the code comment for cloneLoopWithPreheader() below).

/// Clones a loop \p OrigLoop.  Returns the loop and the blocks in \p
/// Blocks.
///
/// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
/// \p LoopDomBB.  Insert the new blocks before block specified in \p Before.
/// Note: Only innermost loops are supported.
LLVM_ABI Loop *cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
                                      Loop *OrigLoop, ValueToValueMapTy &VMap,
                                      const Twine &NameSuffix, LoopInfo *LI,
                                      DominatorTree *DT,
                                      SmallVectorImpl<BasicBlock *> &Blocks);

CongzheUalberta avatar Apr 10 '26 17:04 CongzheUalberta

I think this is mostly a documentation issue; the intended usage of the function is that the user fixes the CFG. Granted, the API is a little awkward.

efriedma-quic avatar Jul 10 '26 00:07 efriedma-quic

I think this is mostly a documentation issue; the intended usage of the function is that the user fixes the CFG. Granted, the API is a little awkward.

Thanks for the comment. I can create a PR that updates the comments for the function, or I can just close the issue for now. Which would you prefer?

CongzheUalberta avatar Jul 10 '26 14:07 CongzheUalberta

I think it's worth updating the comment. I don't really care if we keep the issue open.

efriedma-quic avatar Jul 10 '26 16:07 efriedma-quic

I think it's worth updating the comment. I don't really care if we keep the issue open.

Posted https://github.com/llvm/llvm-project/pull/208817 for it. I'd appreciate it if you could take a look.

CongzheUalberta avatar Jul 10 '26 19:07 CongzheUalberta