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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] `scf-for-loop-canonicalization` pass crashed with assertion failure "RHS constant has to be positive"
        </td>
    </tr>

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

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

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

<pre>
    MLIR built at commit  b4c54b20
Reproduced with:  
`mlir-opt  --scf-for-loop-canonicalization temp.mlir`

temp.mlir:
``` milr 
#map21 = affine_map<(d0, d1, d2) -> (d0, d1 - d2)>
module { 
  func.func nested @func2()  {
 %c1503765534_i32 = arith.constant 1503765534 : i32
    %cst_2 = arith.constant 0x4D638DD7 : f32
    %cst_4 = arith.constant 2.13858022E+9 : f32
    %c589637867_i32 = arith.constant 589637867 : i32
    %c0 = arith.constant 0 : index
    %c6 = arith.constant 6 : index
    %c9 = arith.constant 9 : index 
    %29 = index.divu %c9, %c6
    %89 = arith.shrui %c1503765534_i32, %c589637867_i32 : i32
    %90 = index.shru %c9, %c6
    %c0_31 = arith.constant 0 : index
    %c-1_32 = arith.constant -1 : index
    %134 = arith.muli %c9, %c-1_32 : index
 scf.parallel (%arg0) = (%c9) to (%90) step (%134) {
      %140 = affine.min #map21(%134, %90, %arg0)
      scf.parallel (%arg1) = (%c0_31) to (%140) step (%c9) {
        %154 = arith.remf %cst_4, %cst_2 : f32
 scf.yield
      }
      scf.yield
    } 
    return  
 }
}
```
 
trace:
```console
Assertion failed: (rhsConst > 0 && "RHS constant has to be positive"), function visitDivExpr, file AffineExpr.cpp, line 1328.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: mlir-opt --scf-for-loop-canonicalization temp.mlir
 #0 0x0000000102c60548 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/workspace/build/bin/mlir-opt+0x1002f8548)
 #1 0x0000000102c5e7e4 llvm::sys::RunSignalHandlers() (/workspace/build/bin/mlir-opt+0x1002f67e4)
 #2 0x0000000102c60bf4 SignalHandler(int) (/workspace/build/bin/mlir-opt+0x1002f8bf4)
 #3 0x00000001a46cd4c4 (/usr/lib/system/libsystem_platform.dylib+0x1803414c4)
 #4 0x00000001a46b5ee0 (/usr/lib/system/libsystem_pthread.dylib+0x180329ee0)
 #5 0x00000001a45f0340 (/usr/lib/system/libsystem_c.dylib+0x180264340)
 #6 0x00000001a45ef754 (/usr/lib/system/libsystem_c.dylib+0x180263754)
 #7 0x000000010724de2c mlir::SimpleAffineExprFlattener::visitDivExpr(mlir::AffineBinaryOpExpr, bool) (.cold.3) (/workspace/build/bin/mlir-opt+0x1048e5e2c)
 #8 0x0000000105367418 mlir::SimpleAffineExprFlattener::visitDivExpr(mlir::AffineBinaryOpExpr, bool) (/workspace/build/bin/mlir-opt+0x1029ff418)
 #9 0x0000000102ceffb0 mlir::AffineExprVisitor<mlir::SimpleAffineExprFlattener, void>::walkPostOrder(mlir::AffineExpr) (/workspace/build/bin/mlir-opt+0x100387fb0)
#10 0x0000000102cefec0 mlir::AffineExprVisitor<mlir::SimpleAffineExprFlattener, void>::walkPostOrder(mlir::AffineExpr) (/workspace/build/bin/mlir-opt+0x100387ec0)
#11 0x0000000102cefe58 mlir::AffineExprVisitor<mlir::SimpleAffineExprFlattener, void>::walkPostOrder(mlir::AffineExpr) (/workspace/build/bin/mlir-opt+0x100387e58)
#12 0x00000001052b0584 getFlattenedAffineExprs(llvm::ArrayRef<mlir::AffineExpr>, unsigned int, unsigned int, std::__1::vector<llvm::SmallVector<long long, 8u>, std::__1::allocator<llvm::SmallVector<long long, 8u>>>*, mlir::FlatLinearConstraints*) (/workspace/build/bin/mlir-opt+0x102948584)
#13 0x00000001052b0810 mlir::getFlattenedAffineExprs(mlir::AffineMap, std::__1::vector<llvm::SmallVector<long long, 8u>, std::__1::allocator<llvm::SmallVector<long long, 8u>>>*, mlir::FlatLinearConstraints*) (/workspace/build/bin/mlir-opt+0x102948810)
#14 0x00000001052b0f20 mlir::FlatLinearConstraints::flattenAlignedMapAndMergeLocals(mlir::AffineMap, std::__1::vector<llvm::SmallVector<long long, 8u>, std::__1::allocator<llvm::SmallVector<long long, 8u>>>*) (/workspace/build/bin/mlir-opt+0x102948f20)
#15 0x00000001052b3158 mlir::FlatLinearConstraints::addBound(mlir::presburger::BoundType, unsigned int, mlir::AffineMap, bool) (/workspace/build/bin/mlir-opt+0x10294b158)
#16 0x00000001041684c4 mlir::scf::addLoopRangeConstraints(mlir::affine::FlatAffineValueConstraints&, mlir::Value, mlir::OpFoldResult, mlir::OpFoldResult, mlir::OpFoldResult) (/workspace/build/bin/mlir-opt+0x1018004c4)
#17 0x00000001041686cc mlir::scf::canonicalizeMinMaxOpInLoop(mlir::RewriterBase&, mlir::Operation*, llvm::function_ref<mlir::LogicalResult (mlir::Value, mlir::OpFoldResult&, mlir::OpFoldResult&, mlir::OpFoldResult&)>) (/workspace/build/bin/mlir-opt+0x1018006cc)
#18 0x0000000106903b74 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&)>)::$_2::operator()() const (/workspace/build/bin/mlir-opt+0x103f9bb74)
#19 0x0000000106900e70 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&)>) (/workspace/build/bin/mlir-opt+0x103f98e70)
#20 0x00000001053492b4 (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() (/workspace/build/bin/mlir-opt+0x1029e12b4)
#21 0x0000000105346ae4 mlir::applyPatternsAndFoldGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (/workspace/build/bin/mlir-opt+0x1029deae4)
#22 0x000000010414d550 (anonymous namespace)::SCFForLoopCanonicalization::runOnOperation() (/workspace/build/bin/mlir-opt+0x1017e5550)
#23 0x00000001052e9e34 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/workspace/build/bin/mlir-opt+0x102981e34)
#24 0x00000001052ea524 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/workspace/build/bin/mlir-opt+0x102982524)
#25 0x00000001052ec3bc mlir::PassManager::run(mlir::Operation*) (/workspace/build/bin/mlir-opt+0x1029843bc)
#26 0x00000001052e4808 performActions(llvm::raw_ostream&, std::__1::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) (/workspace/build/bin/mlir-opt+0x10297c808)
#27 0x00000001052e3d30 mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::$_1>(long, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) (/workspace/build/bin/mlir-opt+0x10297bd30)
#28 0x0000000105362be0 mlir::splitAndProcessBuffer(std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, bool, bool) (/workspace/build/bin/mlir-opt+0x1029fabe0)
#29 0x00000001052de6e4 mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/workspace/build/bin/mlir-opt+0x1029766e4)
#30 0x00000001052deb54 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/workspace/build/bin/mlir-opt+0x102976b54)
#31 0x000000010296b58c main (/workspace/build/bin/mlir-opt+0x10000358c)
#32 0x000000010dfcd088 

``` 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWtlu4zjWfhrlhohBUYuli1w4i_8uoIIESaP_S4Mij2xOUaKGpFJxP_2AWmJRdqoSz6DRPT2FgmNLh2f5zkqJ1BixrQGuguQ6SG4vaGt3Sl_dKClVo7RtDb4oFN9f3X_98oSKVkiLqEVMVZWwCBUxS-KC4ADfBnj1BI1WvGXA0Xdhd0G0Qqi_E6S4kkJfqsYidHlpWHlZKn0plWouGa1VLRiV4ndqhaqRhapZOPIgHRj3n4fr0eqNbf8fVULqURaJKtqQEAXRLaJlKWrYVLQJopuAZBwH5AbxsPskAcnRZRDdockddNnfCKK7nl-leCsBBcvrQQBCZVuzhftANRgLHAUxdj9JQDLH0xEPtAFJWJjgaJkmSRRvRER6vbSwuwVTtbG0tuhAgRxqIiKjKNRxMHZzch1-jW_TKLu9XXbryhPr4lPryCKMsiTDhNwF5Do_vTjJ8jRaZunyPa3fCE4rjU9q3NPWHF596vQUdfoedX6KOj9QI4-c9OTdnQUXL23Pwzm8E-0RZ1PeZqdbccKJ49I5Rscw5Hgi27H7kWyGN1H4Kdguw81p51yG7ywJo2lIVK0UvkYjR3-tYeWioZpKCRJ1YZ5QvcXBAG1_xTHJkVXDz7y7bSw0w4UwirsFb9mBRp1iPEnXRSVq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