This is an archive of the discontinued LLVM Phabricator instance.

Add affine expr simplification for subtract from self
AbandonedPublic

Authored by bondhugula on Feb 9 2020, 7:26 PM.

Details

Summary
  • Detect "expr + expr * (-1)" and "expr * -1 + expr" as 0.

Diff Detail

Event Timeline

bondhugula created this revision.Feb 9 2020, 7:26 PM
rriddle accepted this revision.Feb 9 2020, 10:39 PM
rriddle added inline comments.
mlir/lib/IR/AffineExpr.cpp
319

It looks like these detection comments are reversed. I may be missing something, but it looks like this code block computes : expr * -1 + expr = 0.

320

It looks like lBinOpExpr is the same as lBin?

330

Why are these wrapped in scopes?

mlir/test/IR/affine-map.mlir
187

Can we not test the added detection directly: d0 + d0 * -1 and d0 * -1 + d0?

This revision is now accepted and ready to land.Feb 9 2020, 10:39 PM
nicolasvasilache requested changes to this revision.Feb 10 2020, 7:20 AM
nicolasvasilache added inline comments.
mlir/lib/IR/AffineExpr.cpp
317

This looks like it could separate concerns better..
Shouldn't this be a single pattern that adds the constants and lets a 0 * expr canonicalization rewrite it to 0?

This revision now requires changes to proceed.Feb 10 2020, 7:20 AM
bondhugula marked an inline comment as done.Feb 10 2020, 8:44 AM
bondhugula added inline comments.
mlir/lib/IR/AffineExpr.cpp
317

We can actually generalize this to "check for constant multipliers if both are binary op expressions, add the constants, and the 0 times expr is already constructed as 0 if that's the case" - but that would make the logic and casing long and complex because expr * 1 is always expr by construction. So, you'd have to check for both expr and expr * const_not_1 on either side -- four different cases. I kept this simple for now but can extend it this way if you prefer.

bondhugula abandoned this revision.Mar 16 2020, 8:56 AM
bondhugula marked 4 inline comments as done.

This has been subsumed by D76233.

mlir/lib/IR/AffineExpr.cpp
317

I've now generalized to simplify c1 * expr + c2 * expr to (c1 + c2) * expr - the logic is more complex, but it's better than having two patches.

319

You are right - the comments were switched. Anyway the commit is now generalized.

330

There was no good reason - perceived readability. Anyway, this is outdated now.

mlir/test/IR/affine-map.mlir
187

d0 - d0 is internally d0 + d0 * -1. So we'll get the same thing.