[Mlir-commits] [mlir] Lower affine modulo by powers of two using bitwise AND (PR #146311)
Uday Bondhugula
llvmlistbot at llvm.org
Mon Oct 6 04:21:40 PDT 2025
bondhugula wrote:
> According to https://mlir.llvm.org/docs/Dialects/Affine/#affinefor-affineaffineforop: mod is the modulo operation: since its second argument is always positive, its results are always positive in our usage.
>
> `mod` gets conservatively lowered into remsi, cmp, add, select. `remsi` produces negative result for negative LHS, so the power-of-2 optimization to a single `and` can not be applied. To optimize power-of-2 for `remsi` would require right shift, left shift, sub, and, select.
>
> I guess we could implement different canonicalizers for `arith.remsi` and `arith.remui`, but optimizing directly on mod seems a cleaner solution to me, if the power-of-2 optimization is considered necessary...
You are right - this is accurate. The result of an affine mod expression is guaranteed to be positive. So, exploiting this info will lead to a single `andi` and ensure that the information isn't lost while going through a rem operation.
https://github.com/llvm/llvm-project/pull/146311
More information about the Mlir-commits
mailing list