Skip to content

Rework LinalgBufferize pass. - #5539

Merged
MaheshRavishankar merged 6 commits into
iree-org:mainfrom
MaheshRavishankar:adapt_bufferize
Apr 22, 2021
Merged

MaheshRavishankar merged 6 commits into
iree-org:mainfrom
MaheshRavishankar:adapt_bufferize

Conversation

@MaheshRavishankar

@MaheshRavishankar MaheshRavishankar commented Apr 20, 2021 •

Copy link
Copy Markdown
Collaborator

The bufferization pass on the linalg on tensors path was an arbitrary
set of methods put in place to connect linalg on tensors to the post
tile+distribute codegeneration on CPU and GPU. This PR cleans up the
logic to better handle the tensor -> memref conversion.
In particular

  1. Adds an analysis step to figure out which tensors can be mapped to
    the same memref object. This also provides information of what
    tensors are eventually mapped to the output of the dispatch
    region. In such cases, the output is used to compute the values in
    place reducing the need for copies and intermediate allocations.
  2. Use this analysis step to convert operations from tensors to memrefs.
  3. Use the readonly, readwrite and writeonly attributes to avoid
    copies when possible.

All of this uses logic similar to the Tied operands/results approach
that the bufferization in the sandbox uses. Indeed this pass might
eventually subsumed by a bufferization pass in the sandbox and to be
upstreamed. Till that happens, this rewrite gives something that
is similar in spirit.

Fixes #4734
Fixes #5013

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

Might be better to just treat this as a new pass. Its significantly different from the old pass (cause the old version was just too brittle and almost at end-of-life.

@MaheshRavishankar
MaheshRavishankar marked this pull request as ready for review April 21, 2021 04:48

@ThomasRaoux ThomasRaoux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added few more nits. This doesn't support SCF ops right? Does that mean it won't work with our current flow? Is the idea to rely on tiled_loop op?

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

Added few more nits. This doesn't support SCF ops right? Does that mean it won't work with our current flow? Is the idea to rely on tiled_loop op?

There is nothing to do for scf.for ops that dont return anything. For scf.for ops that do return something, this is similar to handling subtensor_insert. You can "tie" the init and the return values. Then you can tie the tensors of the arguments to the internal tensors of the region. So should be extensible to tiled_loop as well.

@ThomasRaoux ThomasRaoux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Do the comment line 15 to 25 still apply? Those seem out of date (github doesn't let me comment on it since it wasn't modified...)

MaheshRavishankar added 5 commits April 22, 2021 09:15
The bufferization pass on the linalg on tensors path was an arbitrary
set of methods put in place to connect linalg on tensors to the post
tile+distribute codegeneration on CPU and GPU. This PR cleans up the
logic to better handle the tensor -> memref conversion.
In particular

1) Adds an analysis step to figure out which tensors can be mapped to
   the same memref object. This also provides information of what
   tensors are eventually mapped to the output of the dispatch
   region. In such cases, the output is used to compute the values in
   place reducing the need for copies and intermediate allocations.
2) Use this analysis step to convert operations from tensors to memrefs.
3) Use the `readonly`, `readwrite` and `writeonly` attributes to avoid
   copies when possible.

All of this uses logic similar to the Tied operands/results approach
that the bufferization in the sandbox uses. Indeed this pass might
eventually subsumed by a bufferization pass in the sandbox and to be
upstreamed. Till that happens, this rewrite gives something that
is similar in spirit.

@asaadaldien asaadaldien left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Lets run the benchmarks this to make sure no extra alloc + copies are inserted.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

LGTM! Lets run the benchmarks this to make sure no extra alloc + copies are inserted.

I checked the bufferization manually to see there no allocs and unnecessary copies. Plus these seem on par https://buildkite.com/iree/iree-android-benchmark/builds/2341

@antiagainst antiagainst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Nice refactoring, Mahesh! Much better.

I just have a few nits.

@MaheshRavishankar
MaheshRavishankar merged commit 245bb9f into iree-org:main Apr 22, 2021
@MaheshRavishankar
MaheshRavishankar deleted the adapt_bufferize branch April 22, 2021 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move LinalgBufferizePass to use patterns Bufferization of vector ops failing with PR #4688

5 participants