Skip to content

Commit

Permalink
[MLIR] Fix bug in simplify affine map with operands
Browse files Browse the repository at this point in the history
Fix bug in simplify affine map with operands utility; the wrong LHS and
RHS were being used in some cases post simplification. While on this,
also handle a corner case of undefined expressions.

Differential Revision: https://reviews.llvm.org/D138584
  • Loading branch information
bondhugula committed Nov 23, 2022
1 parent 6eee66d commit e3b02be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mlir/lib/Dialect/Affine/IR/AffineOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ static void simplifyExprAndOperands(AffineExpr &expr,
return;

// Simplify the child expressions first.
auto lhs = binExpr.getLHS();
auto rhs = binExpr.getRHS();
AffineExpr lhs = binExpr.getLHS();
AffineExpr rhs = binExpr.getRHS();
simplifyExprAndOperands(lhs, operands);
simplifyExprAndOperands(rhs, operands);
expr = getAffineBinaryOpExpr(binExpr.getKind(), lhs, rhs);
Expand All @@ -691,11 +691,18 @@ static void simplifyExprAndOperands(AffineExpr &expr,
return;
}

// The `lhs` and `rhs` may be different post construction of simplified expr.
lhs = binExpr.getLHS();
rhs = binExpr.getRHS();
auto rhsConst = rhs.dyn_cast<AffineConstantExpr>();
if (!rhsConst)
return;

int64_t rhsConstVal = rhsConst.getValue();
// Undefined exprsessions aren't touched; IR can still be valid with them.
if (rhsConstVal == 0)
return;

AffineExpr quotientTimesDiv, rem;
int64_t divisor;

Expand Down
12 changes: 12 additions & 0 deletions mlir/test/Dialect/Affine/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,8 @@ module {

// Simplification of maps exploiting operand info.

// CHECK: #[[$MAP_SIMPLER:.*]] = affine_map<(d0, d1) -> (((d0 + d1) mod 458313) floordiv 227)>

// CHECK-LABEL: func @simplify_with_operands
func.func @simplify_with_operands(%N: index, %A: memref<?x32xf32>) {
// CHECK-NEXT: affine.for %[[I:.*]] = 0 to %{{.*}}
Expand Down Expand Up @@ -1186,5 +1188,15 @@ func.func @simplify_with_operands(%N: index, %A: memref<?x32xf32>) {
"test.foo"(%x) : (f32) -> ()
}

affine.for %arg0 = 0 to %N step 128 {
affine.for %arg4 = 0 to 32 step 32 {
affine.for %arg5 = 0 to 128 {
// CHECK: affine.apply #[[$MAP_SIMPLER]]
%x = affine.apply affine_map<(d0, d1, d2) -> (((d0 + d2) mod 458313) floordiv 227 + d1 floordiv 256)>(%arg0, %arg4, %arg5)
"test.foo"(%x) : (index) -> ()
}
}
}

return
}

0 comments on commit e3b02be

Please sign in to comment.