This is an archive of the discontinued LLVM Phabricator instance.

Add a document on side effects & speculation in MLIR
ClosedPublic

Authored by sanjoy on Oct 19 2022, 10:22 PM.

Diff Detail

Event Timeline

sanjoy created this revision.Oct 19 2022, 10:22 PM
sanjoy requested review of this revision.Oct 19 2022, 10:22 PM
sanjoy updated this revision to Diff 469116.Oct 19 2022, 10:36 PM
  • Add note about Pure

This is really great Sanjoy. Thanks! I have a few comments.

mlir/docs/Rationale/SideEffectsAndSpeculation.md
20

can we introduce an appropriate generalization of the notion of "heap" here? It seems overly specific to talk about "heap"/"stack" in a document about MLIR broadly.

23

Is there an appropriate generalization of this that includes graph regions as well? cc @mehdi_amini

mravishankar accepted this revision.Oct 20 2022, 11:45 AM

Thanks LGTM, but please wait for other folks to weigh in.

This revision is now accepted and ready to land.Oct 20 2022, 11:45 AM
kuhar added inline comments.Oct 20 2022, 12:37 PM
mlir/docs/Rationale/SideEffectsAndSpeculation.md
21

How about something like this:

  • These operations are not defined on certain inputs. We do not define what happens when such illegal inputs are passed, and instead say that behavior is undefined and can assume it does not happen.
  • In practice, in such cases these ops may be considered unreachable or do anything from producing garbage results to crashing the program or corrupting memory.
  • Examples of operations with well-known UB include division by zero or loading from an already freed pointer.

Can you format the markdown to wrap on 80 lines? (ideally just run mdformat on it)

Also we should link to this doc from other places (like the SideEffect interface doc).

mlir/docs/Rationale/SideEffectsAndSpeculation.md
20

We should likely add something like "or any other global state for a program, usually matching some HW (modifying memory or other resources)". Keeping "heap" isn't a big deal in a "rationale" document I think (it's not a spec).

21

I like the suggestion here. In particular "can assume it does not happen" is important to mention.

In practice, in such cases these ops may be considered unreachable

That can be misunderstood because the condition is really dynamic in general, the "can assume it does not happen" should be enough though?

22

More broadly speaking: any operation that may not terminate (infinite loop is a specific construct that won't terminate).

23

I am not sure: without control-flow (a graph region...) it gets weird to have non-local control flow!

sanjoy updated this revision to Diff 469427.Oct 20 2022, 5:31 PM
sanjoy marked 5 inline comments as done.
  • reformat
  • address review comments

Also we should link to this doc from other places (like the SideEffect interface doc).

I don't see a doc, do you mean the .td file?

mlir/docs/Rationale/SideEffectsAndSpeculation.md
20

Generalized the language here a bit, PTAL.

21

Agreed -- folks might read "unreachable" to mean "statically unreachable" which isn't correct.

23

I don't think any of this makes sense generically in a graph region.

For instance, an graph region could be modeling a "sea of nodes"-style IR where all memory effects are explicitly sequenced via an explicit MemorySSA representation. Or it could be represent something that's not even an IR in the traditional sense.

So for now I've added a disclaimer that this doc only applies to CFG regions. LMK if I should remove this.

sanjoy updated this revision to Diff 469431.Oct 20 2022, 5:36 PM
  • Add disclaimer about graph regions

+1 for revising the name "pure" :-)

kuhar accepted this revision.Oct 20 2022, 8:18 PM

Overview + categorization LGTM

mehdi_amini accepted this revision.Oct 20 2022, 8:43 PM

LG, but please wait for @rriddle to take a look as well

jpienaar added inline comments.Oct 21 2022, 9:44 AM
mlir/docs/Rationale/SideEffectsAndSpeculation.md
35

Micro nit: "E.g.,"

37

Ha good example for %s case :)

43

Micro nit: Only one space after period.

45

And here there is no differentiator between where the op produces value/has side effects or not? E.g., in the scf.while case, given it has recursive side effect, it can still be removed in some cases today even I believe. [But Mehdi has good point above, this is a rationale doc not a spec]

84

So, for clarification, if your op expects rank-3 inputs but gets a rank-4 input then you have an invalid module/function. The op has undefined behavior there, where your program as a whole is wrong. (effectively all ops which allow dynamic shapes need this and mostly only speculated in static shape case).

rriddle accepted this revision.Oct 24 2022, 2:39 AM

This LGTM. I would still love for us to have a non-rationale/user facing doc that details how/when/etc. to use things.

mlir/docs/Rationale/SideEffectsAndSpeculation.md
33

Does the resource have to be mutable?

49
sanjoy marked 2 inline comments as done.Oct 24 2022, 9:34 PM
sanjoy added inline comments.
mlir/docs/Rationale/SideEffectsAndSpeculation.md
33

I think so -- reading from immutable resources seems pure. Under the assumption that freeing memory counts as mutating a resource.

LMK if I missed something.

43

Looks like mdformat fixed all such instances.

45

Not sure I totally understood your concern, but I added a note that operations may exhibit multiple kinds of side effects. Let me know if I misunderstood.

84

invalid module/function.

I'd rather frame this as the entire _execution_ is invalid.

sanjoy marked an inline comment as done.Oct 24 2022, 10:02 PM
This revision was automatically updated to reflect the committed changes.