Skip to content

Commit c140442

Browse files
Fix bug in visitDivExpr, visitMulExpr and visitModExpr
Whenever the result of a div or mod affine expression is a constant expression, place the value in the constant index of the flattened expression instead of adding it as a local expression.
1 parent 529662a commit c140442

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

mlir/include/mlir/IR/AffineExprVisitor.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@ class SimpleAffineExprFlattener
418418
AffineExpr localExpr);
419419

420420
private:
421+
/// Flatten `expr` and it to `result`. If `expr` is dimension, symbol or
422+
/// constant, we add it to appropriate index in `result`. Otherwise we add it
423+
/// in local variable section.
424+
LogicalResult addExprToFlattenedList(ArrayRef<int64_t> lhs,
425+
ArrayRef<int64_t> rhs,
426+
SmallVectorImpl<int64_t> &result,
427+
AffineExpr expr);
428+
421429
/// Adds `localExpr`, which may be mod, ceildiv, floordiv or mod expression
422430
/// representing the affine expression corresponding to the quantifier
423431
/// introduced as the local variable corresponding to `localExpr`. If the

mlir/lib/IR/AffineExpr.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,10 +1177,9 @@ static AffineExpr getSemiAffineExprFromFlatForm(ArrayRef<int64_t> flatExprs,
11771177
if (flatExprs[numDims + numSymbols + it.index()] == 0)
11781178
continue;
11791179
AffineExpr expr = it.value();
1180-
auto binaryExpr = dyn_cast<AffineBinaryOpExpr>(expr);
1181-
if (!binaryExpr)
1182-
continue;
1183-
1180+
// A Local expression cannot be a dimension, symbol or a constant -- it
1181+
// should be a binary op expression.
1182+
auto binaryExpr = cast<AffineBinaryOpExpr>(expr);
11841183
AffineExpr lhs = binaryExpr.getLHS();
11851184
AffineExpr rhs = binaryExpr.getRHS();
11861185
if (!((isa<AffineDimExpr>(lhs) || isa<AffineSymbolExpr>(lhs)) &&
@@ -1274,6 +1273,27 @@ SimpleAffineExprFlattener::SimpleAffineExprFlattener(unsigned numDims,
12741273
operandExprStack.reserve(8);
12751274
}
12761275

1276+
LogicalResult SimpleAffineExprFlattener::addExprToFlattenedList(
1277+
ArrayRef<int64_t> lhs, ArrayRef<int64_t> rhs,
1278+
SmallVectorImpl<int64_t> &result, AffineExpr expr) {
1279+
if (auto constExpr = dyn_cast<AffineConstantExpr>(expr)) {
1280+
std::fill(result.begin(), result.end(), 0);
1281+
result[getConstantIndex()] = constExpr.getValue();
1282+
return success();
1283+
}
1284+
if (auto dimExpr = dyn_cast<AffineDimExpr>(expr)) {
1285+
std::fill(result.begin(), result.end(), 0);
1286+
result[getDimStartIndex() + dimExpr.getPosition()] = 1;
1287+
return success();
1288+
}
1289+
if (auto symExpr = dyn_cast<AffineSymbolExpr>(expr)) {
1290+
std::fill(result.begin(), result.end(), 0);
1291+
result[getSymbolStartIndex() + symExpr.getPosition()] = 1;
1292+
return success();
1293+
}
1294+
return addLocalVariableSemiAffine(lhs, rhs, expr, result, result.size());
1295+
}
1296+
12771297
// In pure affine t = expr * c, we multiply each coefficient of lhs with c.
12781298
//
12791299
// In case of semi affine multiplication expressions, t = expr * symbolic_expr,
@@ -1295,7 +1315,8 @@ LogicalResult SimpleAffineExprFlattener::visitMulExpr(AffineBinaryOpExpr expr) {
12951315
localExprs, context);
12961316
AffineExpr b = getAffineExprFromFlatForm(rhs, numDims, numSymbols,
12971317
localExprs, context);
1298-
return addLocalVariableSemiAffine(mulLhs, rhs, a * b, lhs, lhs.size());
1318+
AffineExpr mulExpr = a * b;
1319+
return addExprToFlattenedList(mulLhs, rhs, lhs, mulExpr);
12991320
}
13001321

13011322
// Get the RHS constant.
@@ -1348,7 +1369,7 @@ LogicalResult SimpleAffineExprFlattener::visitModExpr(AffineBinaryOpExpr expr) {
13481369
AffineExpr divisorExpr = getAffineExprFromFlatForm(rhs, numDims, numSymbols,
13491370
localExprs, context);
13501371
AffineExpr modExpr = dividendExpr % divisorExpr;
1351-
return addLocalVariableSemiAffine(modLhs, rhs, modExpr, lhs, lhs.size());
1372+
return addExprToFlattenedList(modLhs, rhs, lhs, modExpr);
13521373
}
13531374

13541375
int64_t rhsConst = rhs[getConstantIndex()];
@@ -1482,7 +1503,7 @@ LogicalResult SimpleAffineExprFlattener::visitDivExpr(AffineBinaryOpExpr expr,
14821503
AffineExpr b = getAffineExprFromFlatForm(rhs, numDims, numSymbols,
14831504
localExprs, context);
14841505
AffineExpr divExpr = isCeil ? a.ceilDiv(b) : a.floorDiv(b);
1485-
return addLocalVariableSemiAffine(divLhs, rhs, divExpr, lhs, lhs.size());
1506+
return addExprToFlattenedList(divLhs, rhs, lhs, divExpr);
14861507
}
14871508

14881509
// This is a pure affine expr; the RHS is a positive constant.

mlir/test/Dialect/Affine/simplify-structures.mlir

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,16 +595,33 @@ func.func @semiaffine_modulo_dim(%arg0: index, %arg1: index, %arg2: index) -> in
595595

596596
// -----
597597

598-
// CHECK-LABEL: func @semiaffine_simplification_floordiv_and_ceildiv_const
599-
func.func @semiaffine_simplification_floordiv_and_ceildiv_const(%arg0: tensor<?xf32>) -> (index, index) {
598+
// CHECK-DAG: #[[$MAP:.*]] = affine_map<()[s0] -> (13 mod s0)>
599+
// CHECK-DAG: #[[$MAP1:.*]] = affine_map<(d0) -> (d0 * 2)>
600+
// CHECK-LABEL: semiaffine_simplification_local_expr_folded_into_non_binary_expr
601+
func.func @semiaffine_simplification_local_expr_folded_into_non_binary_expr(%arg0: memref<?x?xf32>) -> (index, index, index, index) {
600602
%c0 = arith.constant 0 : index
601603
%c1 = arith.constant 1 : index
604+
%c4 = arith.constant 4 : index
602605
%c13 = arith.constant 13 : index
603-
%dim = tensor.dim %arg0, %c0 : tensor<?xf32>
606+
// CHECK: %[[DIM:.*]] = memref.dim
607+
%dim = memref.dim %arg0, %c0 : memref<?x?xf32>
608+
// CHECK-DAG: %[[C6:.*]] = arith.constant 6 : index
609+
// CHECK-DAG: %[[C7:.*]] = arith.constant 7 : index
604610
%a = affine.apply affine_map<()[s0, s1, s2] -> (s0 floordiv (s1 + (-s1 + 2) * (-s1 + s1 * s2 + 1)))>()[%c13, %dim, %c1]
605611
%b = affine.apply affine_map<()[s0, s1, s2] -> (s0 ceildiv (s1 + (-s1 + 2) * (-s1 + s1 * s2 + 1)))>()[%c13, %dim, %c1]
606-
// CHECK: %[[C6:.*]] = arith.constant 6 : index
607-
// CHECK-NEXT: %[[C7:.*]] = arith.constant 7 : index
608-
// CHECK-NEXT: return %[[C6]], %[[C7]]
609-
return %a, %b : index, index
612+
// CHECK: %[[VAL0:.*]] = affine.apply #[[$MAP]]()[%[[DIM]]]
613+
%c = affine.apply affine_map<()[s0, s1, s2, s3] -> (s0 mod (s1 + (-s1 + s3) * (-s1 + s1 * s2 + 1)))>()[%c13, %dim, %c1, %dim]
614+
%alloc = memref.alloc() : memref<1xindex>
615+
affine.for %iv = 0 to 1 {
616+
%d = affine.apply affine_map<(d0)[s1, s2] -> ((d0 - s1 + s1 * s2) * (s1 + (-s1 + 2) * (-s1 + s1 * s2 + 1)))>(%iv)[%dim, %c1]
617+
affine.store %d, %alloc[0] : memref<1xindex>
618+
}
619+
// CHECK: affine.for %[[IV:.*]] = 0 to 1 {
620+
// CHECK-NEXT: %[[VAL:.*]] = affine.apply #[[$MAP1]](%[[IV]])
621+
// CHECK-NEXT: affine.store %[[VAL]], %{{.*}}[0] : memref<1xindex>
622+
// CHECK-NEXT: }
623+
// CHECK: %[[VAL1:.*]] = affine.load %{{.*}}[0]
624+
%d = affine.load %alloc[0] : memref<1xindex>
625+
// CHECK: return %[[C6]], %[[C7]], %[[VAL0]], %[[VAL1]]
626+
return %a, %b, %c, %d : index, index, index, index
610627
}

0 commit comments

Comments
 (0)