llvm / Polygeist

C/C++ frontend for MLIR. Also features polyhedral optimizations, parallel optimizations, and more!
https://polygeist.llvm.org
Other
462 stars 105 forks source link

[bug] `AffineApplyNormalizer` assert Error #421

Open sBobHuang opened 1 month ago

sBobHuang commented 1 month ago

When running my code using cgeist with the --raise-scf-to-affine option, I encountered the following error:

cgeist: ../lib/polygeist/Passes/AffineCFG.cpp:534: AffineApplyNormalizer::AffineApplyNormalizer(mlir::AffineMap, llvm::ArrayRef, mlir::PatternRewriter&, mlir::DominanceInfo&): Assertion `isValidSymbolInt(t, false)' failed. PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.

Use the following minimal code example (saved as test.c) that reproduces the error,run the code using cgeist test.c --raise-scf-to-affine --function=* -S --memref-fullrank -o test.mlir

void nested_loop_using_variable_as_step(int vali[20][30]) {
  int stepx = vali[0][0];
  for (int x = 0; x < 20; x++) {
    for (int y = 1; y < 30; y += stepx) {
      vali[x][y] = 1;
    }
  }
}
sBobHuang commented 1 month ago

Upon analyzing the process flow, the error appears to involve the operation "arith.divui," which is created at lib/polygeist/Passes/RaiseToAffine.cpp:114. This operation subsequently encounters a problem during evaluation at lib/polygeist/Passes/AffineCFG.cpp:319.

sBobHuang commented 1 month ago

I think lib/polygeist/Passes/AffineCFG.cpp:215 fix involves a lambda function attempting to promote the current value to a top level to satisfy the condition “It is defined at the top level of 'region' or is its argument.” If promotion is not possible, the function returns nullptr. In this example, the condition for fix at lib/polygeist/Passes/AffineCFG.cpp:326 is not satisfied, but after applying fix, promotion is possible. Therefore, I modified the condition at line lib/polygeist/Passes/AffineCFG.cpp:335 to include DivUIOp, which resolved the issue.