This is an archive of the discontinued LLVM Phabricator instance.

[mlir][docs] Update/Add documentation for MLIRs Pattern Rewrite infrastructure
ClosedPublic

Authored by rriddle on Aug 4 2020, 4:50 PM.

Details

Summary

This infrastructure has evolved a lot over the course of MLIRs lifetime, and has never truly been documented outside of rationale or proposals. This revision aims to document the infrastructure and user facing API, with the rationale specific portions moved to the Rationale folder and updated.

Depends On D85167

Diff Detail

Event Timeline

rriddle created this revision.Aug 4 2020, 4:50 PM
rriddle requested review of this revision.Aug 4 2020, 4:50 PM

I tried to touch on most of the important aspects, but let me know if I missed something. Thanks.

Thanks very much for this finally - this was long overdue. I skimmed through it - I think it's documenting most of the things. But I think it's also important to have a few lines where appropriate on what one can't do inside a pattern rewrite: in fact, this is typically what new users get wrong and source of many questions.

  1. IR shouldn't be mutated outside of the rewriter methods described here. An op for example can't be erased directly with its erase method.
  1. A replacement of uses or any other update to operands outside of the rewriter methods if performed will take effect but such a replacement will not be visible to the pattern rewriter and the op may not be reprocessed by the driver.

It would be good to factor these in somewhere if not already there.

flaub added a subscriber: flaub.Aug 5 2020, 11:16 AM
rriddle updated this revision to Diff 283758.Aug 6 2020, 3:57 PM

Address comments

ftynse added a comment.Aug 7 2020, 7:10 AM

I don't know how much of the rationale is reused from the previous version, so feel free to ignore comments about the parts you carried over.

mlir/docs/PatternRewriter.md
30

Nit: unmatched rparen

54

Maybe add something like: "the combined matchAndRewrite is useful to keep information that is not trivially recomputable between the matching and the rewriting phase"

62

[Not for the documentation]: can't we rather make the constructor RewritePattern a template that takes the op class and calls TemplateTy::getOperationName() itself?

105

Add that in-place updates are only allowed on the root operation? (Or has that been relaxed already?)

219

Nit: warning: unused variable `result'

234

Consider mentioning that dialect conversion supports type conversion

mlir/docs/Rationale/RationaleGenericDAGRewriter.md
17–18

This sounds a bit strange to me, especially the "replacing with another set", even though I understand what you want to say.

19–20

I'd consider using putting examples that are friendlier to "old-school" compiler people first, like peephole optimizations, canonicalization and inst-combine. I'm not even sure TF-related things should be mentioned in the generic document.

22

"MLIR graph" isn't a well-defined term AFAIK. I'd put just "optimization algorithms for IR at multiple levels".

28

Add "an affine loop"? Just to avoid the jump from TFLite to LLVM IR....

29

s/CUDA/PTX

32–33

The last clause does not seem to relate to the rest of the sentence.

38

"tile patterns" sounds wrong here. We don't necessarily want to tile the input graph with patterns, and we don't have a driver that does that.

41

performs

47

s/computation/replacement? or application?

53

I'm a bit confused by the "here are a few" passage, followed by "the most similar design...". It feels like there isn't going to be a list of related systems after all.

56

Nit: the comma looks unnecessary here

59

At the end of what?

64

drop "given"

67

Is this section intended to review related work, or to demonstrate how existing pattern-matching constructs can be expressed in MLIR?

83

Nit: can we reference a more recent clang version? :)

87

The text hasn't yet mentioned that source-to-source optimizers are tree-based.

93

This subsection does not explain how AST rewriting can be implemented in MLIR, unlike the previous section for constant folding.

112

I'd drop C++, it's a general OOP thing.

142

Nit: expand OTOH

148

Nit: consider pinning a specific revision rather than master, otherwise the line number will get quickly out-of-sync

170

Consider using the last name, or both first and last :)

186

Nit: let's use github links consistently

209

It would be great to specify why

211

We don't have runtime patterns in MLIR either, do we?

219

It would be great to have a section that explains how MLIR's approach addresses all of these shortcomings, point by point.

229

Which "these algorithms"?

230–232

Examples here sound too specific for folks who don't know how TF works inside (I would assume most compiler folks don't).

235

s/lowest cost/highest benefit. Although the optimization problems are isomorphic, let's not introduce confusing terminology.

238

Is it always locally optimal?

241

"DAG tile" again, it hasn't been defined. Googling for "dag tile" finds the current version of MLIR doc as one of the first results...

242

Nit: I think whoever wants to touch the internals of a compiler knows what NP-complete stands for. I'm also not 100% sure our variant of graph coverage problem is NP-complete (i.e., can be reduced to 3-SAT) or NP-hard. I'd use the latter to be safe unless there's a citation.

250

"very very"

260

This went from TF operations to "not apply for all hardware" really fast... I'd just stick with pattern being usable at all abstraction levels of MLIR, including target-independent and target-specific

262

Arguably, it is solvable, but there are multiple possible solutions with different trade-offs.

265

What "this project" ?

279

Let's not mention PHI nodes, MLIR doesn't have them and it may be confusing

284

Let's stick with "benefits".

bondhugula added inline comments.Aug 8 2020, 3:26 PM
mlir/docs/PatternRewriter.md
239

Somehow, this doesn't bring out the scheme that benefit comes into play while choosing among multiple available patterns for the *same* op.

mlir/docs/Rationale/RationaleGenericDAGRewriter.md
224

levels that it -> levels is that it

Nit: infra -> infrastructure

bondhugula added inline comments.Aug 8 2020, 3:31 PM
mlir/docs/PatternRewriter.md
30

Missing opening parenthesis.

I don't know how much of the rationale is reused from the previous version, so feel free to ignore comments about the parts you carried over.

Ah no worries, thanks for the comments. I was a bit hesitant at first, but now I'd rather just clean the doc up than let it be a relic and not valid for the current state of the project.

rriddle updated this revision to Diff 284483.Aug 10 2020, 12:54 PM
rriddle marked 45 inline comments as done.

Resolve comments

mlir/docs/PatternRewriter.md
62

You can't explicitly specify a template for a constructor, so there would be no way to achieve that.

105

You can in-place modify any operation you can call replaceOp/eraseOp on.

mlir/docs/Rationale/RationaleGenericDAGRewriter.md
38

DAG tiling is common phrasing in instruction selection literature, added a definition here.

211

We will soon (via PDL).

219

Is it fine to do this is a followup?

229

Which "these algorithms"?

241

Added a definition on first use in the introduction

ftynse accepted this revision.Aug 11 2020, 4:39 AM

Thanks, this looks great!

mlir/docs/Rationale/RationaleGenericDAGRewriter.md
219

Sure!

This revision is now accepted and ready to land.Aug 11 2020, 4:39 AM
This revision was landed with ongoing or failed builds.Aug 13 2020, 12:06 PM
This revision was automatically updated to reflect the committed changes.
mlir/docs/DialectConversion.md