@@ -356,8 +356,9 @@ unsigned AffineDimExpr::getPosition() const {
356
356
// /`exprKind` is floordiv and `expr` is also a binary expression of a floordiv
357
357
// / operation, then the commutative property can be used otherwise, the floordiv
358
358
// / operation is not divisible. The same argument holds for ceildiv operation.
359
- static bool isDivisibleBySymbol (AffineExpr expr, unsigned symbolPos,
360
- AffineExprKind opKind) {
359
+ static bool canSimplifyDivisionBySymbol (AffineExpr expr, unsigned symbolPos,
360
+ AffineExprKind opKind,
361
+ bool fromMul = false ) {
361
362
// The argument `opKind` can either be Modulo, Floordiv or Ceildiv only.
362
363
assert ((opKind == AffineExprKind::Mod || opKind == AffineExprKind::FloorDiv ||
363
364
opKind == AffineExprKind::CeilDiv) &&
@@ -372,8 +373,9 @@ static bool isDivisibleBySymbol(AffineExpr expr, unsigned symbolPos,
372
373
// Checks divisibility by the given symbol for both operands.
373
374
case AffineExprKind::Add: {
374
375
AffineBinaryOpExpr binaryExpr = cast<AffineBinaryOpExpr>(expr);
375
- return isDivisibleBySymbol (binaryExpr.getLHS (), symbolPos, opKind) &&
376
- isDivisibleBySymbol (binaryExpr.getRHS (), symbolPos, opKind);
376
+ return canSimplifyDivisionBySymbol (binaryExpr.getLHS (), symbolPos,
377
+ opKind) &&
378
+ canSimplifyDivisionBySymbol (binaryExpr.getRHS (), symbolPos, opKind);
377
379
}
378
380
// Checks divisibility by the given symbol for both operands. Consider the
379
381
// expression `(((s1*s0) floordiv w) mod ((s1 * s2) floordiv p)) floordiv s1`,
@@ -382,31 +384,38 @@ static bool isDivisibleBySymbol(AffineExpr expr, unsigned symbolPos,
382
384
// `AffineExprKind::Mod` for this reason.
383
385
case AffineExprKind::Mod: {
384
386
AffineBinaryOpExpr binaryExpr = cast<AffineBinaryOpExpr>(expr);
385
- return isDivisibleBySymbol (binaryExpr.getLHS (), symbolPos,
386
- AffineExprKind::Mod) &&
387
- isDivisibleBySymbol (binaryExpr.getRHS (), symbolPos,
388
- AffineExprKind::Mod);
387
+ return canSimplifyDivisionBySymbol (binaryExpr.getLHS (), symbolPos,
388
+ AffineExprKind::Mod) &&
389
+ canSimplifyDivisionBySymbol (binaryExpr.getRHS (), symbolPos,
390
+ AffineExprKind::Mod);
389
391
}
390
392
// Checks if any of the operand divisible by the given symbol.
391
393
case AffineExprKind::Mul: {
392
394
AffineBinaryOpExpr binaryExpr = cast<AffineBinaryOpExpr>(expr);
393
- return isDivisibleBySymbol (binaryExpr.getLHS (), symbolPos, opKind) ||
394
- isDivisibleBySymbol (binaryExpr.getRHS (), symbolPos, opKind);
395
+ return canSimplifyDivisionBySymbol (binaryExpr.getLHS (), symbolPos, opKind,
396
+ true ) ||
397
+ canSimplifyDivisionBySymbol (binaryExpr.getRHS (), symbolPos, opKind,
398
+ true );
395
399
}
396
400
// Floordiv and ceildiv are divisible by the given symbol when the first
397
401
// operand is divisible, and the affine expression kind of the argument expr
398
402
// is same as the argument `opKind`. This can be inferred from commutative
399
403
// property of floordiv and ceildiv operations and are as follow:
400
404
// (exp1 floordiv exp2) floordiv exp3 = (exp1 floordiv exp3) floordiv exp2
401
405
// (exp1 ceildiv exp2) ceildiv exp3 = (exp1 ceildiv exp3) ceildiv expr2
402
- // It will fail if operations are not same. For example:
403
- // (exps1 ceildiv exp2) floordiv exp3 can not be simplified.
406
+ // It will fail 1.if operations are not same. For example:
407
+ // (exps1 ceildiv exp2) floordiv exp3 can not be simplified. 2.if there is a
408
+ // multiplication operation in the expression. For example:
409
+ // (exps1 ceildiv exp2) mul exp3 ceildiv exp4 can not be simplified.
404
410
case AffineExprKind::FloorDiv:
405
411
case AffineExprKind::CeilDiv: {
406
412
AffineBinaryOpExpr binaryExpr = cast<AffineBinaryOpExpr>(expr);
407
413
if (opKind != expr.getKind ())
408
414
return false ;
409
- return isDivisibleBySymbol (binaryExpr.getLHS (), symbolPos, expr.getKind ());
415
+ if (fromMul)
416
+ return false ;
417
+ return canSimplifyDivisionBySymbol (binaryExpr.getLHS (), symbolPos,
418
+ expr.getKind ());
410
419
}
411
420
}
412
421
llvm_unreachable (" Unknown AffineExpr" );
@@ -448,7 +457,7 @@ static AffineExpr symbolicDivide(AffineExpr expr, unsigned symbolPos,
448
457
// Dividing any of the operand by the given symbol.
449
458
case AffineExprKind::Mul: {
450
459
AffineBinaryOpExpr binaryExpr = cast<AffineBinaryOpExpr>(expr);
451
- if (!isDivisibleBySymbol (binaryExpr.getLHS (), symbolPos, opKind))
460
+ if (!canSimplifyDivisionBySymbol (binaryExpr.getLHS (), symbolPos, opKind))
452
461
return binaryExpr.getLHS () *
453
462
symbolicDivide (binaryExpr.getRHS (), symbolPos, opKind);
454
463
return symbolicDivide (binaryExpr.getLHS (), symbolPos, opKind) *
@@ -583,7 +592,8 @@ static AffineExpr simplifySemiAffine(AffineExpr expr, unsigned numDims,
583
592
if (!symbolExpr)
584
593
return getAffineBinaryOpExpr (expr.getKind (), sLHS , sRHS );
585
594
unsigned symbolPos = symbolExpr.getPosition ();
586
- if (!isDivisibleBySymbol (binaryExpr.getLHS (), symbolPos, expr.getKind ()))
595
+ if (!canSimplifyDivisionBySymbol (binaryExpr.getLHS (), symbolPos,
596
+ expr.getKind ()))
587
597
return getAffineBinaryOpExpr (expr.getKind (), sLHS , sRHS );
588
598
if (expr.getKind () == AffineExprKind::Mod)
589
599
return getAffineConstantExpr (0 , expr.getContext ());
0 commit comments