This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Fold away divs and mods in affine ops with operand info
ClosedPublic

Authored by bondhugula on Feb 6 2023, 7:24 PM.

Details

Summary

Fold away divs and mods in affine maps exploiting operand info during
canonicalization. This simplifies affine map applications such as the ones
below:

// Simple ones.
affine.for %i = 0 to 32 {
  affine.load %A[%i floordiv 32]
  affine.load %A[%i mod 32]
  affine.load %A[2 * %i floordiv 64]
  affine.load %A[(%i mod 16) floordiv 16]
  ...
}

// Others.
 affine.for %i = -8 to 32 {
   // Will be simplified %A[0].
   affine.store %cst, %A[2 + (%i - 96) floordiv 64] : memref<64xf32>
}

Diff Detail

Event Timeline

bondhugula created this revision.Feb 6 2023, 7:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 6 2023, 7:24 PM
bondhugula requested review of this revision.Feb 6 2023, 7:24 PM
springerm accepted this revision.Feb 9 2023, 12:54 AM
springerm added inline comments.
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
719

Would exprs with rhs = 0 not already be rejected by the verifier?

719

nit: You could reduce the amount of nesting here and below:

if (!rhsConst || rhsConst.getValue() < 1)
  return std::nullopt;
744

lb mod c

804–812

nit:

if (...)
  return constExpr.getValue()
return getBoundForExpr ...
This revision is now accepted and ready to land.Feb 9 2023, 12:54 AM
bondhugula marked an inline comment as done.Feb 10 2023, 12:04 AM

Thanks for the review!

mlir/lib/Dialect/Affine/IR/AffineOps.cpp
719

Expressions with rhs <= 0 aren't and can't be rejected by the verifier in general -- because they could appear in dead code. They are just treated as undefined behavior the same way as division by zero. Eg: it's possible that one gets IR like this after some simplifications and rewrites at the end of a pass:

affine.if (#identity_set (%c0) == 0 )
  ...
else 
  // 0 got propagated to the RHS.
  .. floordiv 0
}

The else region will eventually go away but we have let these undefined things pass.

bondhugula marked 4 inline comments as done.

Address review comments.

This revision was landed with ongoing or failed builds.Feb 10 2023, 12:11 AM
This revision was automatically updated to reflect the committed changes.