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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Crash when using --arith-unsigned-when-equivalent
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

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

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

<pre>
    I have the following MLIR program:
test.mlir:
```
func.func @func1() {
  %c1 = arith.constant 1 : index
  %127 = scf.for %arg0 = %c1 to %c1 step %c1 iter_args(%arg1 = %c1) -> (index) {
    %138 = index.floordivs %arg0, %arg0
    scf.yield %c1 : index
  }
 return
}
```
The above MLIR program will cause a crash when using the following command:
```
mlir-opt --arith-unsigned-when-equivalent test.mlir
```
And the crash backtrace is:
```
mlir-opt: /data/tmp/v0605/llvm-project/llvm/lib/Support/APInt.cpp:1548: llvm::APInt llvm::APInt::udiv(const llvm::APInt&) const: Assertion `RHS.U.VAL != 0 && "Divide by zero?"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /data/tmp/v0605/llvm-project/build/bin/mlir-opt --arith-unsigned-when-equivalent test.mlir
 #0 0x000055c890751cef llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x14b6cef)
 #1 0x000055c89074ed44 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f94ec05e420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f94ebb2b00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f94ebb0a859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007f94ebb0a729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007f94ebb1bfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x000055c8907e1151 llvm::APInt::udiv(llvm::APInt const&) const (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x1546151)
 #8 0x000055c8907e159e llvm::APInt::sdiv(llvm::APInt const&) const (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x154659e)
 #9 0x000055c8907e36eb llvm::APInt::sdiv_ov(llvm::APInt const&, bool&) const (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x15486eb)
#10 0x000055c895be3049 std::optional<llvm::APInt> llvm::function_ref<std::optional<llvm::APInt> (llvm::APInt const&, llvm::APInt const&)>::callback_fn<inferDivSRange(mlir::ConstantIntRanges const&, mlir::ConstantIntRanges const&, llvm::function_ref<std::optional<llvm::APInt> (llvm::APInt const&, llvm::APInt const&, llvm::APInt const&)>)::'lambda'(llvm::APInt const&, llvm::APInt const&)>(long, llvm::APInt const&, llvm::APInt const&) InferIntRangeCommon.cpp:0:0
#11 0x000055c895be54e4 minMaxBy(llvm::function_ref<std::optional<llvm::APInt> (llvm::APInt const&, llvm::APInt const&)>, llvm::ArrayRef<llvm::APInt>, llvm::ArrayRef<llvm::APInt>, bool) (.constprop.1) InferIntRangeCommon.cpp:0:0
#12 0x000055c895be5880 inferDivSRange(mlir::ConstantIntRanges const&, mlir::ConstantIntRanges const&, llvm::function_ref<std::optional<llvm::APInt> (llvm::APInt const&, llvm::APInt const&, llvm::APInt const&)>) InferIntRangeCommon.cpp:0:0
#13 0x000055c895be8008 mlir::intrange::inferFloorDivS(llvm::ArrayRef<mlir::ConstantIntRanges>) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x694d008)
#14 0x000055c890fd1c7a std::_Function_handler<mlir::ConstantIntRanges (llvm::ArrayRef<mlir::ConstantIntRanges>), mlir::ConstantIntRanges (*)(llvm::ArrayRef<mlir::ConstantIntRanges>)>::_M_invoke(std::_Any_data const&, llvm::ArrayRef<mlir::ConstantIntRanges>&&) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x1d36c7a)
#15 0x000055c895bea8ae mlir::intrange::inferIndexOp(std::function<mlir::ConstantIntRanges (llvm::ArrayRef<mlir::ConstantIntRanges>)> const&, llvm::ArrayRef<mlir::ConstantIntRanges>, mlir::intrange::CmpMode) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x694f8ae)
#16 0x000055c890fd2efb mlir::index::FloorDivSOp::inferResultRanges(llvm::ArrayRef<mlir::ConstantIntRanges>, llvm::function_ref<void (mlir::Value, mlir::ConstantIntRanges const&)>) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x1d37efb)
#17 0x000055c890fbf37a mlir::detail::InferIntRangeInterfaceInterfaceTraits::Model<mlir::index::FloorDivSOp>::inferResultRanges(mlir::detail::InferIntRangeInterfaceInterfaceTraits::Concept const*, mlir::Operation*, llvm::ArrayRef<mlir::ConstantIntRanges>, llvm::function_ref<void (mlir::Value, mlir::ConstantIntRanges const&)>) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x1d2437a)
#18 0x000055c89689285e mlir::intrange::detail::defaultInferResultRanges(mlir::InferIntRangeInterface, llvm::ArrayRef<mlir::IntegerValueRange>, llvm::function_ref<void (mlir::Value, mlir::IntegerValueRange const&)>) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x75f785e)
#19 0x000055c8936dc283 mlir::dataflow::IntegerRangeAnalysis::visitOperation(mlir::Operation*, llvm::ArrayRef<mlir::dataflow::IntegerValueRangeLattice const*>, llvm::ArrayRef<mlir::dataflow::IntegerValueRangeLattice*>) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x4441283)
#20 0x000055c8936e64ce mlir::dataflow::AbstractSparseForwardDataFlowAnalysis::visitOperation(mlir::Operation*) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x444b4ce)
#21 0x000055c8936e699b mlir::dataflow::AbstractSparseForwardDataFlowAnalysis::visit(mlir::ProgramPoint) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x444b99b)
#22 0x000055c8936902fd mlir::DataFlowSolver::initializeAndRun(mlir::Operation*) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x43f52fd)
#23 0x000055c890a94cce (anonymous namespace)::ArithUnsignedWhenEquivalentPass::runOnOperation() UnsignedWhenEquivalent.cpp:0:0
#24 0x000055c8937026f1 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x44676f1)
#25 0x000055c893702bda mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x4467bda)
#26 0x000055c893703394 mlir::PassManager::run(mlir::Operation*) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x4468394)
#27 0x000055c8936f481b performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#28 0x000055c8936f51d4 processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, llvm::ThreadPoolInterface*) MlirOptMain.cpp:0:0
#29 0x000055c8936f5384 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
#30 0x000055c8937fdcfe 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&, llvm::StringRef, llvm::StringRef) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x4562cfe)
#31 0x000055c8936ecf84 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x4451f84)
#32 0x000055c8936f54dc mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x445a4dc)
#33 0x000055c8936f59a6 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x445a9a6)
#34 0x000055c89066c8cb main (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x13d18cb)
#35 0x00007f94ebb0c083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#36 0x000055c890720ffe _start (/data/tmp/v0605/llvm-project/build/bin/mlir-opt+0x1485ffe)
```
My git version is dd82fd4744397e0510c8204f1a6031441e21858e.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWt1u4zawfhrlhohBkfq9yIXXidEAGyRItj2XBiWSNluZVEnKifv0B5RkW1TixJv1tj09wG4sS6PhfN8MhzM0iTFiKRm7CuIvQXx9QRq7UvrqmcjlVsnl7yG5KBTdXt2CFdkwYFcMcFVV6lnIJbj7evsIaq2W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