Fix MLIR's floordiv, ceildiv, and mod for constant inputs (for negative lhs's)
- introduce mlir::{floorDiv, ceilDiv, mod} for constant inputs in
mlir/Support/MathExtras.h
- consistently use these everywhere in IR, Analysis, and Transforms.
PiperOrigin-RevId: 215580677
diff --git a/lib/Analysis/LoopAnalysis.cpp b/lib/Analysis/LoopAnalysis.cpp
index f4e607e..1549182 100644
--- a/lib/Analysis/LoopAnalysis.cpp
+++ b/lib/Analysis/LoopAnalysis.cpp
@@ -25,6 +25,7 @@
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/Statements.h"
+#include "mlir/Support/MathExtras.h"
using mlir::AffineExpr;
@@ -74,10 +75,8 @@
if (loopSpan < 0)
return 0;
- return AffineConstantExpr::get(
- static_cast<uint64_t>(loopSpan % step == 0 ? loopSpan / step
- : loopSpan / step + 1),
- context);
+ return AffineConstantExpr::get(static_cast<uint64_t>(ceilDiv(loopSpan, step)),
+ context);
}
/// Returns the trip count of the loop if it's a constant, None otherwise. This