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

    <tr>
        <th>Summary</th>
        <td>
            Inconsistent results and crash when using --int-range-optimizations
        </td>
    </tr>

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

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

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

<pre>
    I have the MLIR program:

[test.mlir.txt](https://github.com/llvm/llvm-project/files/14217577/test.mlir.txt)

When I run the following command on the above MLIR program, I will get inconsistent results over multiple runs:
command:
```
mlir-opt --int-range-optimizations --canonicalize --convert-arith-to-llvm test.mlir
```
possible result 1:
```
module {
  func.func @func1() {
    %0 = llvm.mlir.constant(3743431372 : i64) : i64
    %1 = llvm.mlir.constant(true) : i1
    vector.print %1 : i1
    vector.print %0 : i64
    return
  }
}
```
possible result 2:
```
module {
  func.func @func1() {
    %0 = llvm.mlir.constant(3743431372 : i64) : i64
    %1 = llvm.mlir.constant(false) : i1
    vector.print %1 : i1
 vector.print %0 : i64
    return
  }
}
```


I've tried to reduce the program, but after reducing it, the error then changed into crash.
The reduced program:
test.mlir:
```
module {
 func.func @func1() {
    %false = arith.constant false
    %false_0 = arith.constant false
    // These two constant values can just be declared as both true or both false and cannot be removed

    %c2_i16 = arith.constant 2 : i16
    %7 = arith.addi %c2_i16, %c2_i16 : i16
 %31 = math.absi %c2_i16 : i16
    // This operation can be changed to math.cttz or math.ctlz, but then the frequency of causing crashes will change from always to sometimes

    %32 = arith.cmpi sge, %7, %31 : i16
 //This comparison attribute can be any type supported in the arith.cmpi operation

    vector.print %32 : i1
    return
 }
}
```

The above MLIR program will cause a crash when using the following command:

```
mlir-opt --int-range-optimizations test.mlir
```

And the crash backtrace is:

```
mlir-opt: /data/tmp/v0209/llvm-project/llvm/lib/Support/APInt.cpp:291: int llvm::APInt::compareSigned(const llvm::APInt&) const: Assertion `BitWidth == RHS.BitWidth && "Bit widths must be same for comparison"' 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/v0209/llvm-project/build/bin/mlir-opt --int-range-optimizations test.mlir
 #0 0x000055bb113871df llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x17141df)
 #1 0x000055bb11384234 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f8b1d00e420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f8b1cadb00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f8b1caba859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007f8b1caba729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007f8b1cacbfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x000055bb11412f2d llvm::APInt::compareSigned(llvm::APInt const&) const (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x179ff2d)
 #8 0x000055bb116d428f handleSge(mlir::ConstantIntRanges, mlir::ConstantIntRanges) IntRangeOptimizations.cpp:0:0
 #9 0x000055bb116d547c (anonymous namespace)::ConvertCmpOp::matchAndRewrite(mlir::arith::CmpIOp, mlir::PatternRewriter&) const IntRangeOptimizations.cpp:0:0
#10 0x000055bb17049288 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<mlir::LogicalResult (mlir::Pattern const&)>) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x73d6288)
#11 0x000055bb142bb0ef (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#12 0x000055bb142bdf8b mlir::applyPatternsAndFoldGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x464af8b)
#13 0x000055bb116d6bfe (anonymous namespace)::IntRangeOptimizationsPass::runOnOperation() IntRangeOptimizations.cpp:0:0
#14 0x000055bb142075ae mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x45945ae)
#15 0x000055bb14207a8a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x4594a8a)
#16 0x000055bb14208244 mlir::PassManager::run(mlir::Operation*) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x4595244)
#17 0x000055bb141f9003 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#18 0x000055bb141fa584 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
#19 0x000055bb141fa734 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
#20 0x000055bb142f70e4 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) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x46840e4)
#21 0x000055bb141f2b97 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x457fb97)
#22 0x000055bb141fa881 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x4587881)
#23 0x000055bb141fad06 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x4587d06)
#24 0x000055bb112bc58b main (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x164958b)
#25 0x00007f8b1cabc083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#26 0x000055bb11356dee _start (/data/tmp/v0209/llvm-project/build/bin/mlir-opt+0x16e3dee)
Aborted (core dumped)
```

I'm not sure if there is any problem in the arith compare operations, or maybe in the --int-range-optimizations pass that caused the above errors.
My git version is f60826917aff102450a470dee85208fd578685c4.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWt1u4zawfhrlhohBUf8XuXCS9WmABgmSBXppUOLIZkuRKkkl6336A1KyLTlO4rRGT3sWG1sSh8OZb34pmhrDVxLgKkiug-T2gnZ2rfTVK5WrjZKr30N6USq2ubpDa_oCyK4B3f9694RarV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