<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61371>61371</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] `-affine-data-copy-generate` triggers Assertion Failure `(isTopLevelValue(val) || isAffineForInductionVar(val)) && "non-terminal symbol / loop IV expected"
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          BealZephyr
      </td>
    </tr>
</table>

<pre>
    Test on commit: https://github.com/llvm/llvm-project/commit/3b4cb1e96c645bb833fe710856479c31383859bb
The same crash occurred in [LLVM 16](https://github.com/llvm/llvm-project/tree/release/16.x)
steps to reproduce:
```
mlir-opt -affine-data-copy-generate test.mlir
```
test case:
```
#map16 = affine_map<(d0, d1, d2) -> (d0 * 40 + d1 * 8 + d2 * 2)>
#map17 = affine_map<(d0, d1, d2) -> (d0 * 40 + d1 * 8 + d2 * 2 + 2)>
module {
  func.func @main(%85:memref<2x5x4x2xi64>) {
    affine.for %arg0 = 0 to 2 {
 affine.parallel (%arg1) = (0) to (5) {
        affine.parallel (%arg2) = (0) to (4) {
          affine.for %arg3 = #map16(%arg0, %arg1, %arg2) to #map17(%arg0, %arg1, %arg2) {
            %105 = affine.load %85[((%arg3 floordiv 2) floordiv 4) floordiv 5, ((%arg3 floordiv 2) floordiv 4) mod 5, (%arg3 floordiv 2) mod 4, %arg3 mod 2] : memref<2x5x4x2xi64>
 }
        }
      }
    }
    return
  }
}
```
crash trace:
```
mlir-opt: /home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/mlir/lib/Dialect/Affine/Analysis/AffineStructures.cpp:580: void mlir::FlatAffineValueConstraints::addInductionVarOrTerminalSymbol(mlir::Value): Assertion `(isTopLevelValue(val) || isAffineForInductionVar(val)) && "non-terminal symbol / loop IV expected"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt -affine-data-copy-generate test.mlir
 #0 0x0000563ceffb7f1a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x221f1a)
 #1 0x0000563ceffb541c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f9459116980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #3 0x00007f945820ee87 raise /build/glibc-CVJwZb/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #4 0x00007f94582107f1 abort /build/glibc-CVJwZb/glibc-2.27/stdlib/abort.c:81:0
 #5 0x00007f94582003fa __assert_fail_base /build/glibc-CVJwZb/glibc-2.27/assert/assert.c:89:0
 #6 0x00007f9458200472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
 #7 0x0000563cf1803fd4 mlir::FlatAffineValueConstraints::addInductionVarOrTerminalSymbol(mlir::Value) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1a6dfd4)
 #8 0x0000563cf180367f mlir::FlatAffineValueConstraints::addBound(mlir::presburger::IntegerRelation::BoundType, unsigned int, mlir::AffineMap, mlir::ValueRange) (.constprop.290) AffineStructures.cpp:0:0
 #9 0x0000563cf18038d8 mlir::FlatAffineValueConstraints::addAffineForOpDomain(mlir::AffineForOp) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1a6d8d8)
#10 0x0000563cf1819203 mlir::MemRefRegion::compute(mlir::Operation*, unsigned int, mlir::ComputationSliceState const*, bool) (.constprop.398) Utils.cpp:0:0
#11 0x0000563cf181a264 getMemoryFootprintBytes(mlir::Block&, llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Operation, true, false, void>, false, false>, llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Operation, true, false, void>, false, false>, int)::'lambda'(mlir::Operation*)::operator()(mlir::Operation*) const Utils.cpp:0:0
#12 0x0000563cf180e30e mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (.constprop.393) Utils.cpp:0:0
#13 0x0000563cf180e30e mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (.constprop.393) Utils.cpp:0:0
#14 0x0000563cf180e30e mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (.constprop.393) Utils.cpp:0:0
#15 0x0000563cf181a766 getMemoryFootprintBytes(mlir::Block&, llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Operation, true, false, void>, false, false>, llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Operation, true, false, void>, false, false>, int) (.isra.384) Utils.cpp:0:0
#16 0x0000563cf181afd4 mlir::getMemoryFootprintBytes(mlir::AffineForOp, int) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1a84fd4)
#17 0x0000563cf005449d (anonymous namespace)::AffineDataCopyGeneration::runOnBlock(mlir::Block*, llvm::DenseSet<mlir::Operation*, llvm::DenseMapInfo<mlir::Operation*, void>>&) AffineDataCopyGeneration.cpp:0:0
#18 0x0000563cf0054e6a (anonymous namespace)::AffineDataCopyGeneration::runOnOperation() AffineDataCopyGeneration.cpp:0:0
#19 0x0000563cf186f759 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1ad9759)
#20 0x0000563cf186fa7a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1ad9a7a)
#21 0x0000563cf186fdf8 mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::'lambda'(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&)::operator()(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&) const Pass.cpp:0:0
#22 0x0000563cf186ef35 mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1ad8f35)
#23 0x0000563cf186f6ca mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1ad96ca)
#24 0x0000563cf18705b3 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (.constprop.495) Pass.cpp:0:0
#25 0x0000563cf1870c9d mlir::PassManager::run(mlir::Operation*) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1adac9d)
#26 0x0000563cf1863ca3 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#27 0x0000563cf18643d9 processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, llvm::ThreadPool*) MlirOptMain.cpp:0:0
#28 0x0000563cf1864701 mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#29 0x0000563cf1929380 mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, bool, bool) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1b93380)
#30 0x0000563cf18613e3 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1acb3e3)
#31 0x0000563cf1864ae8 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&, bool) (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1aceae8)
#32 0x0000563ceff84330 main (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x1ee330)
#33 0x00007f94581f1c87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#34 0x0000563ceff96fca _start (/home/verify/llvm-project-3b4cb1e96c645bb833fe710856479c31383859bb/build/bin/mlir-opt+0x200fca)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsOl9P5Djyn8a8WLQSO38feGia6d-P0yAQcHPSvrQc2wm-ceKc7TD0tz_ZSTdJaJhmxbJ7q5Fm6Ngu1_8qVyUmxoiq4fwMxOcgvjghnX1Q-uycE_kbbx-2-qRQbHt2z42FqoFU1bWwAC_hg7WtAXgJ0BqgdSXsQ1csqKoBWkv5uPs5bbX6N6cWoPWwFa1xEdEi5HlCkyguigzjkqdhkMVJlOYUhzjDWZwXBQguQLC8f-DQkJpDqol5gIrSTmvOoGggiM-_fv12BcMExBcAZe9myWrOAVprLjkx7ilMFk8A5T1lY3lroFVQ81Yr1lHucPslkATDPz-spdCnqrXwlJSlaPgpI5acUtVuTyvecE0sh5Ybu3CABzG4VUgdE6-QAAjXpA0TCPAF7KlsatICvAIoYwFAK8hC_xcBlMNTgL9AvwIBWsLI_ZxDFvpR1g-QHzhwgL9MqKQfT8UPJrRqxTrJIUjP-zGEZdfQhfsDQRTURDQAZQDFWQzwsua15iXAK_QUP0VP6EkkkUOF8jEGODC9KJWGAMVEV4GXJXB2RCPQAa4lmkjJJexJEV2FHiW-cDOBe7bKPcYvKI2ovcSCDmOJDmI5wDUeNvdG36P1FtjzuXtEewK99Y4BP8AEdOthEI-Mv5CKMNjbID73eLMdg6VUSjPx6K36PIomo7gne-y-WrHRlkPwDiJ6lgX7CQTiC-iS0mte0ksK0oupzLOJyXAy0Nx2utmN90vPD9Ng7VOV1eTnKcNxDdD6QdUu_zxyLcrtLFGdHp0w0donGLSWwg0uBJF9olt6a7qHhsitEWY_d2d1R22nuVnQtgV4GWeB4-lRCQY9NrwEeLmWxPYbvhHZ8ZVqjNVENNb064Sxy4Z11ArVfCP6Wt9zXYuGyLttXSgJUPaMy2PwmWAJl8Zw7TZ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