Bug type: crash
Component: LoopUnrollAndJamPass
Bug location: llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp: LoopUnrollAndJamPass::run
tryToUnrollAndJamLoop
Issue title: LoopUnrollAndJamPass crash: GenericLoopInfo.h:146: [...] Assertion `!isInvalid() && "Loop not in a valid state!"' failed.
Labels: crash
llvm:transforms
generated by fuzzer
Reported by: mikaelholmen

llvm commit: 6805d7e8aa5f

Reproduce with:
```opt -passes="loop(invalidate<all>,loop-unroll-and-jam,loop-unroll-and-jam)" bbi-103085.ll -o /dev/null -S -unroll-and-jam-count=4 -mcpu=cortex-a55 -mtriple=aarch64```

Result:
```
opt: ../include/llvm/Support/GenericLoopInfo.h:146: const std::vector<LoopT *> &llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::getSubLoops() const [BlockT = llvm::BasicBlock, LoopT = llvm::Loop]: Assertion `!isInvalid() && "Loop not in a valid state!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: build-all/bin/opt -passes=loop(invalidate<all>,loop-unroll-and-jam,loop-unroll-and-jam) bbi-103085.ll -o /dev/null -S -unroll-and-jam-count=4 -mcpu=cortex-a55 -mtriple=aarch64
1.	Running pass "function(loop(invalidate<all>,loop-unroll-and-jam,loop-unroll-and-jam))" on module "bbi-103085.ll"
2.	Running pass "loop(invalidate<all>,loop-unroll-and-jam,loop-unroll-and-jam)" on function "test_it"
 #0 0x0000564cc1e14ac6 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (build-all/bin/opt+0x45ceac6)
 #1 0x0000564cc1e1250e llvm::sys::RunSignalHandlers() (build-all/bin/opt+0x45cc50e)
 #2 0x0000564cc1e15379 SignalHandler(int) Signals.cpp:0:0
 #3 0x00007fafbeb0bd10 __restore_rt (/lib64/libpthread.so.0+0x12d10)
 #4 0x00007fafbc4ab52f raise (/lib64/libc.so.6+0x4e52f)
 #5 0x00007fafbc47ee65 abort (/lib64/libc.so.6+0x21e65)
 #6 0x00007fafbc47ed39 _nl_load_domain.cold.0 (/lib64/libc.so.6+0x21d39)
 #7 0x00007fafbc4a3e86 (/lib64/libc.so.6+0x46e86)
 #8 0x0000564cc274b6cb void llvm::appendReversedLoopsToWorklist<llvm::iterator_range<std::reverse_iterator<llvm::Loop* const*>>>(llvm::iterator_range<std::reverse_iterator<llvm::Loop* const*>>&&, llvm::SmallPriorityWorklist<llvm::Loop*, 4u>&) (build-all/bin/opt+0x4f056cb)
 #9 0x0000564cc274b42e void llvm::appendLoopsToWorklist<llvm::ArrayRef<llvm::Loop*>&>(llvm::ArrayRef<llvm::Loop*>&, llvm::SmallPriorityWorklist<llvm::Loop*, 4u>&) (build-all/bin/opt+0x4f0542e)
#10 0x0000564cc381dfce llvm::LoopUnrollAndJamPass::run(llvm::LoopNest&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) (build-all/bin/opt+0x5fd7fce)
#11 0x0000564cc328d01d llvm::detail::PassModel<llvm::LoopNest, llvm::LoopUnrollAndJamPass, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::run(llvm::LoopNest&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) PassBuilderPipelines.cpp:0:0
#12 0x0000564cc34ff5ed std::optional<llvm::PreservedAnalyses> llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::runSinglePass<llvm::LoopNest, std::unique_ptr<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>, std::default_delete<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>>>>(llvm::LoopNest&, std::unique_ptr<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>, std::default_delete<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>>>&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&, llvm::PassInstrumentation&) (build-all/bin/opt+0x5cb95ed)
#13 0x0000564cc34fe8e5 llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::runWithLoopNestPasses(llvm::Loop&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) (build-all/bin/opt+0x5cb88e5)
#14 0x0000564cc34fe480 llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::run(llvm::Loop&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) (build-all/bin/opt+0x5cb8480)
#15 0x0000564cc327a8bd llvm::detail::PassModel<llvm::Loop, llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::run(llvm::Loop&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) PassBuilderPipelines.cpp:0:0
#16 0x0000564cc35001a1 llvm::FunctionToLoopPassAdaptor::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (build-all/bin/opt+0x5cba1a1)
#17 0x0000564cc327eedd llvm::detail::PassModel<llvm::Function, llvm::FunctionToLoopPassAdaptor, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) PassBuilderPipelines.cpp:0:0
#18 0x0000564cc2035a27 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (build-all/bin/opt+0x47efa27)
#19 0x0000564cc32846cd llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) PassBuilderPipelines.cpp:0:0
#20 0x0000564cc203a5fe llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (build-all/bin/opt+0x47f45fe)
#21 0x0000564cc327c19d llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) PassBuilderPipelines.cpp:0:0
#22 0x0000564cc2034717 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (build-all/bin/opt+0x47ee717)
#23 0x0000564cc320468c llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (build-all/bin/opt+0x59be68c)
#24 0x0000564cc1dd7712 optMain (build-all/bin/opt+0x4591712)
#25 0x00007fafbc4977e5 __libc_start_main (/lib64/libc.so.6+0x3a7e5)
#26 0x0000564cc1dd532e _start (build-all/bin/opt+0x458f32e)
Abort
```

[bbi-103085.ll.gz](https://github.com/user-attachments/files/18556221/bbi-103085.ll.gz)

Comments: author: mikaelholmen
body: The opt pipeline used to triggered this is non-standard and generated by a fuzzer, so I'm labelling this with "generated by fuzzer". I don't know if that the intended use. If not, please let me know.
author: davemgreen
body: Do you have the IR reproducer too? Thanks
author: mikaelholmen
body: > Do you have the IR reproducer too? Thanks

Bah, sorry. Attached it to the original post now.
Thanks!
author: mikaelholmen
body: I noticed that if we build opt with ASAN and run the bbi-103085.ll testcase we get
```
==3349467==ERROR: AddressSanitizer: use-after-poison on address 0x521000007a40 at pc 0x55713d205877 bp 0x7ffdd1999e50 sp 0x7ffdd1999e48
READ of size 1 at 0x521000007a40 thread T0
    #0 0x55713d205876 in isInvalid /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/GenericLoopInfo.h:219:12
    #1 0x55713d205876 in getSubLoops /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/GenericLoopInfo.h:146:5
    #2 0x55713d205876 in begin /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/GenericLoopInfo.h:156:35
    #3 0x55713d205876 in void llvm::appendReversedLoopsToWorklist<llvm::iterator_range<std::__1::reverse_iterator<llvm::Loop* const*>>>(llvm::iterator_range<std::__1::reverse_iterator<llvm::Loop* const*>>&&, llvm::SmallPriorityWorklist<llvm::Loop*, 4u>&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Utils/LoopUtils.cpp:1808:34
    #4 0x55713d204ad9 in void llvm::appendLoopsToWorklist<llvm::ArrayRef<llvm::Loop*>&>(llvm::ArrayRef<llvm::Loop*>&, llvm::SmallPriorityWorklist<llvm::Loop*, 4u>&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Utils/LoopUtils.cpp:1820:3
    #5 0x55714083fe1b in tryToUnrollAndJamLoop /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp:436:3
    #6 0x55714083fe1b in llvm::LoopUnrollAndJamPass::run(llvm::LoopNest&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp:460:8
    #7 0x55713f72db11 in llvm::detail::PassModel<llvm::LoopNest, llvm::LoopUnrollAndJamPass, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::run(llvm::LoopNest&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #8 0x55713fec0dd7 in std::__1::optional<llvm::PreservedAnalyses> llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::runSinglePass<llvm::LoopNest, std::__1::unique_ptr<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>, std::__1::default_delete<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>>>>(llvm::LoopNest&, std::__1::unique_ptr<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>, std::__1::default_delete<llvm::detail::PassConcept<llvm::LoopNest, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>>>&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&, llvm::PassInstrumentation&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/Transforms/Scalar/LoopPassManager.h:375:32
    #9 0x55713febe67c in llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::runWithLoopNestPasses(llvm::Loop&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Scalar/LoopPassManager.cpp:112:16
    #10 0x55713febdd61 in llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::run(llvm::Loop&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Scalar/LoopPassManager.cpp:30:32
    #11 0x55713f719901 in llvm::detail::PassModel<llvm::Loop, llvm::PassManager<llvm::Loop, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&>::run(llvm::Loop&, llvm::AnalysisManager<llvm::Loop, llvm::LoopStandardAnalysisResults&>&, llvm::LoopStandardAnalysisResults&, llvm::LPMUpdater&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #12 0x55713fec3348 in llvm::FunctionToLoopPassAdaptor::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Scalar/LoopPassManager.cpp:302:38
    #13 0x55713f722991 in llvm::detail::PassModel<llvm::Function, llvm::FunctionToLoopPassAdaptor, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #14 0x55713ba254c9 in llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerImpl.h:81:38
    #15 0x55713f728871 in llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #16 0x55713ba34d25 in llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /repo/llvm/build-all-bbisdk-asan/../lib/IR/PassManager.cpp:124:38
    #17 0x55713f71d491 in llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #18 0x55713ba218f9 in llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerImpl.h:81:38
    #19 0x55713f5eda5d in llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::__1::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) /repo/llvm/build-all-bbisdk-asan/../tools/opt/NewPMDriver.cpp:552:7
    #20 0x55713b2721b3 in optMain /repo/llvm/build-all-bbisdk-asan/../tools/opt/optdriver.cpp:725:12
    #21 0x7f6c3bae47e4 in __libc_start_main (/lib64/libc.so.6+0x3a7e4) (BuildId: 37e4ac6a7fb96950b0e6bf72d73d94f3296c77eb)
    #22 0x55713b1823ad in _start (/repo/llvm/build-all-bbisdk-asan/bin/opt+0x8cfe3ad)

0x521000007a40 is located 320 bytes inside of 4096-byte region [0x521000007900,0x521000008900)
allocated by thread T0 here:
    #0 0x55713b25a992 in operator new(unsigned long, std::align_val_t, std::nothrow_t const&) asan_new_delete.cpp:113:3
    #1 0x55713b346d4f in llvm::allocate_buffer(unsigned long, unsigned long) /repo/llvm/build-all-bbisdk-asan/../lib/Support/MemAlloc.cpp:16:18
    #2 0x55713b34a30a in Allocate /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/AllocatorBase.h:92:12
    #3 0x55713b34a30a in StartNewSlab /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/Allocator.h:346:42
    #4 0x55713b34a30a in llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 4096ul, 4096ul, 128ul>::AllocateSlow(unsigned long, unsigned long, llvm::Align) /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/Allocator.h:202:5
    #5 0x55713cb12443 in Allocate /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/Allocator.h:178:12
    #6 0x55713cb12443 in Allocate /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/Allocator.h:216:12
    #7 0x55713cb12443 in Allocate /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/AllocatorBase.h:53:43
    #8 0x55713cb12443 in Allocate<llvm::Loop> /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/AllocatorBase.h:76:29
    #9 0x55713cb12443 in llvm::Loop* llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>::AllocateLoop<llvm::BasicBlock*&>(llvm::BasicBlock*&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/GenericLoopInfo.h:571:36
    #10 0x55713cb0ff50 in llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop>::analyze(llvm::DominatorTreeBase<llvm::BasicBlock, false> const&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/GenericLoopInfoImpl.h:594:18
    #11 0x55713cb23dbc in llvm::LoopAnalysis::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../lib/Analysis/LoopInfo.cpp:977:6
    #12 0x55714109e4e7 in llvm::detail::AnalysisPassModel<llvm::Function, llvm::LoopAnalysis, llvm::AnalysisManager<llvm::Function>::Invalidator>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:321:14
    #13 0x55713ba300ab in llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerImpl.h:156:35
    #14 0x55713d1e67b4 in getResult<llvm::LoopAnalysis> /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManager.h:414:9
    #15 0x55713d1e67b4 in llvm::LoopSimplifyPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Utils/LoopSimplify.cpp:825:22
    #16 0x55713f719f11 in llvm::detail::PassModel<llvm::Function, llvm::LoopSimplifyPass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #17 0x55713ba254c9 in llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerImpl.h:81:38
    #18 0x55713fec1dce in llvm::FunctionToLoopPassAdaptor::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../lib/Transforms/Scalar/LoopPassManager.cpp:209:34
    #19 0x55713f722991 in llvm::detail::PassModel<llvm::Function, llvm::FunctionToLoopPassAdaptor, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #20 0x55713ba254c9 in llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerImpl.h:81:38
    #21 0x55713f728871 in llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #22 0x55713ba34d25 in llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /repo/llvm/build-all-bbisdk-asan/../lib/IR/PassManager.cpp:124:38
    #23 0x55713f71d491 in llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerInternal.h:91:17
    #24 0x55713ba218f9 in llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /repo/llvm/build-all-bbisdk-asan/../include/llvm/IR/PassManagerImpl.h:81:38
    #25 0x55713f5eda5d in llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::__1::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) /repo/llvm/build-all-bbisdk-asan/../tools/opt/NewPMDriver.cpp:552:7
    #26 0x55713b2721b3 in optMain /repo/llvm/build-all-bbisdk-asan/../tools/opt/optdriver.cpp:725:12
    #27 0x7f6c3bae47e4 in __libc_start_main (/lib64/libc.so.6+0x3a7e4) (BuildId: 37e4ac6a7fb96950b0e6bf72d73d94f3296c77eb)

SUMMARY: AddressSanitizer: use-after-poison /repo/llvm/build-all-bbisdk-asan/../include/llvm/Support/GenericLoopInfo.h:219:12 in isInvalid
Shadow bytes around the buggy address:
  0x521000007780: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x521000007800: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x521000007880: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x521000007900: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x521000007980: 00 00 00 00 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
=>0x521000007a00: f7 f7 f7 f7 f7 f7 f7 f7[f7]f7 00 00 00 00 00 00
  0x521000007a80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f7 f7
  0x521000007b00: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x521000007b80: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x521000007c00: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x521000007c80: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==3349467==ABORTING
```


Fix patch:
commit 3ef33066bb32a9d9b76d72c6de6a7ae9ff72ddcc
Author: David Green <david.green@arm.com>
Date:   Sat Mar 29 19:21:34 2025 +0000

    [UnrollAndJam] Do not preserve loop nests if a loop was fully unrolled. (#133510)
    
    If UnJ completely unrolls a loop and removes it entirely, the loop
    remains in the current loop nest. If the loop nest gets reused the loops
    will no longer be valid. As there is no way to remove a loop from a
    LoopNest, this patch removes the preserve of the LoopNestAnalysis so
    that it will be regenerated.
    
    Fixes #124518

diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp
index f1d1f3bc1e30..4fe74c7c3bbc 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp
@@ -425,7 +425,7 @@ static bool tryToUnrollAndJamLoop(LoopNest &LN, DominatorTree &DT, LoopInfo &LI,
                                   const TargetTransformInfo &TTI,
                                   AssumptionCache &AC, DependenceInfo &DI,
                                   OptimizationRemarkEmitter &ORE, int OptLevel,
-                                  LPMUpdater &U) {
+                                  LPMUpdater &U, bool &AnyLoopRemoved) {
   bool DidSomething = false;
   ArrayRef<Loop *> Loops = LN.getLoops();
   Loop *OutmostLoop = &LN.getOutermostLoop();
@@ -441,8 +441,11 @@ static bool tryToUnrollAndJamLoop(LoopNest &LN, DominatorTree &DT, LoopInfo &LI,
         tryToUnrollAndJamLoop(L, DT, &LI, SE, TTI, AC, DI, ORE, OptLevel);
     if (Result != LoopUnrollResult::Unmodified)
       DidSomething = true;
-    if (L == OutmostLoop && Result == LoopUnrollResult::FullyUnrolled)
-      U.markLoopAsDeleted(*L, LoopName);
+    if (Result == LoopUnrollResult::FullyUnrolled) {
+      if (L == OutmostLoop)
+        U.markLoopAsDeleted(*L, LoopName);
+      AnyLoopRemoved = true;
+    }
   }
 
   return DidSomething;
@@ -457,11 +460,13 @@ PreservedAnalyses LoopUnrollAndJamPass::run(LoopNest &LN,
   DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
   OptimizationRemarkEmitter ORE(&F);
 
+  bool AnyLoopRemoved = false;
   if (!tryToUnrollAndJamLoop(LN, AR.DT, AR.LI, AR.SE, AR.TTI, AR.AC, DI, ORE,
-                             OptLevel, U))
+                             OptLevel, U, AnyLoopRemoved))
     return PreservedAnalyses::all();
 
   auto PA = getLoopPassPreservedAnalyses();
-  PA.preserve<LoopNestAnalysis>();
+  if (!AnyLoopRemoved)
+    PA.preserve<LoopNestAnalysis>();
   return PA;
 }


Regression tests:
file: llvm/test/Transforms/LoopUnrollAndJam/delete_middle_loop.ll
commands: opt -passes="loop(invalidate<all>,loop-unroll-and-jam,loop-unroll-and-jam)" -allow-unroll-and-jam -unroll-and-jam-count=4 < %s -S
tests: test_name: <module>
test_body: 
; This test completely unrolls the middle loop out of a 3-deep loop nest.

define i16 @test_it() {
;
entry:
  br label %for.cond

for.cond:                                         ; preds = %do.cond, %entry
  br label %do.body2

do.body2:                                         ; preds = %do.cond, %for.cond
  br label %while.cond3

while.cond3:                                      ; preds = %while.cond3, %do.body2
  br i1 true, label %do.cond, label %while.cond3

do.cond:                                          ; preds = %while.cond3
  br i1 true, label %for.cond, label %do.body2
}