[Bug][Utils] cloneLoopWithPreheader did not update LoopInfo correctly
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);
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.
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?
I think it's worth updating the comment. I don't really care if we keep the issue open.
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.