Skip to content

Commit

Permalink
Improve integer divisibilty inference for util.assume.int with 0 cases (
Browse files Browse the repository at this point in the history
iree-org#19254)

If one of the possible values for a util.assume.int is a constant 0,
don't bail out of the GCD computation even if that constant doesn't have
a `udiv` term.

This is because 0 is divisible by any integer.
  • Loading branch information
krzysz00 authored Nov 22, 2024
1 parent 38d8d0a commit 7850ea9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion compiler/src/iree/compiler/Dialect/Util/IR/UtilOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,14 +1184,26 @@ AssumeIntOp::getUnionedUnsignedRange(unsigned operandIndex) {
umaxCount == assumptions.size() ? umaxUnion : std::nullopt);
}

static bool isConstantZero(IntAssumptionAttr assumption) {
std::optional<uint64_t> umin = assumption.getUmin();
std::optional<uint64_t> umax = assumption.getUmax();
if (!umin || !umax)
return false;
return *umin == 0 && *umax == 0;
}

std::optional<uint64_t>
AssumeIntOp::getUnionedUnsignedDivisor(unsigned operandIndex) {
auto assumptions = getOperandAssumptions(operandIndex);
std::optional<uint64_t> divisorUnion;
for (auto assumption : assumptions) {
auto divisor = assumption.getUdiv();
if (!divisor)
if (!divisor) {
// Constant zero is divisible by anything
if (isConstantZero(assumption))
continue;
return std::nullopt;
}
if (divisorUnion)
divisorUnion = std::gcd(*divisor, *divisorUnion);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ util.func @divui_divisibility(%arg0 : index) -> (index, index) {
// CHECK: %[[V:.+]] = arith.divui
// CHECK: %[[REM:.+]] = arith.remui %[[V]], %[[C32]]
// CHECK: return %[[C0]], %[[REM]]

// -----

util.func @everything_divides_zero(%arg0 : index) -> index {
%c32 = arith.constant 32 : index
%0 = util.assume.int %arg0[<umin = 0, umax = 0>, <umin = 64, umax = 64, udiv = 64>] : index
%1 = arith.remui %0, %c32 : index
util.return %1 : index
}
// CHECK-LABEL: @everything_divides_zero
// CHECK-DAG: %[[C0:.+]] = arith.constant 0 : index
// CHECK: return %[[C0]]

0 comments on commit 7850ea9

Please sign in to comment.