Skip to content

Adding TiedOpInterface and wiring it through the Flow dialect. - #5016

Merged
benvanik merged 1 commit into
mainfrom
benvanik-flow-tie
Mar 11, 2021
Merged

benvanik merged 1 commit into
mainfrom
benvanik-flow-tie

Conversation

@benvanik

@benvanik benvanik commented Mar 8, 2021 •

Copy link
Copy Markdown
Collaborator

This allows for results of operations to be tied back to their operands in storage but not in time. This allows for in-place operations to be defined on tensors that carry enough metadata to be able to correctly form streams, materialize HAL interfaces, and allocate buffers.

Example:

%t = flow.dispatch @foo[...](%input) : (tensor<4xf32>) -> %input

This syntax also combines with the shape-carrying op interface to make it possible to also indicate that an input and a result share type and shape information:

%t = flow.dispatch @foo[...](%input) : (tensor<?xf32>{%dim}) -> %input

which is effectively:

%t = flow.dispatch @foo[...](%input) : (tensor<?xf32>{%dim}) -> tensor<?xf32>{%dim}

but with the extra bit that result 0 is tied to operand 0.

Here the result %t of the dispatch aliases the storage for %input, making %input a read-write/mutable binding in the resulting HAL executable. %t is a distinct SSA value from %input, though, and represents the value of the storage backing %input after the dispatch has completed. By keeping the SSA use-def chains correct
with respect to time they are still meaningful for analysis and nothing at this level (and the beginning of the HAL transformations) needs to perform alias analysis, while still giving us all of the information required to induce aliasing during later allocation passes.

This stands in contrast to other approaches that induce aliasing much earlier and solves tricky cases like in-place updates to tensors that are reused throughout the program. It's always valid in this form to expand a use-after-in-place-update to a clone + update:

%t0 = flow.dispatch ... () : () -> tensor<4xf32>
%t1 = flow.dispatch ... (%t0) : (tensor<4xf32>) -> %t0
// HAZARD: reusing %t0 after in-place update:
%t2 = flow.dispatch ... (%t0) : (tensor<4xf32>) -> tensor<4xf32>

->

%t0 = flow.dispatch ... () : () -> tensor<4xf32>
%t0_clone = flow.tensor.clone %t0 : tensor<4xf32>
%t1 = flow.dispatch ... (%t0) : (tensor<4xf32>) -> %t0
// Safe: now using the captured value of %t0:
%t2 = flow.dispatch ... (%t0_clone) : (tensor<4xf32>) -> tensor<4xf32>

These clones are inserted automatically as required and eliminated when not needed. Because the IR is valid both before and after the transformation it enables iterative optimization and code motion such as during stream formation. Depending on how streams are scheduled as they go down into the HAL some of the clones may not be needed and making these kinds of determinations is trivial as all of the information required to track liveness of both captured snapshots of tensors and their backing storage is available.

Continuing the pattern of raising compiler concepts up to higher level IR this is similar to virtual registers and register renaming, where each SSA value represents a virtual register and the in-place operations are a form of renaming to avoid data hazards.

NOTE: flow.tensor.update ties its operands now but the flow.dispatch.workgroups construction from linalg ops is not yet doing so. That's for future @MaheshRavishankar & co follow-up as it likely requires a new linalg op interface/tie detection algorithm.

@benvanik benvanik added the compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) label Mar 8, 2021
@google-cla google-cla Bot added the cla: yes label Mar 8, 2021
@benvanik
benvanik force-pushed the benvanik-flow-tie branch 2 times, most recently from 40e4c4a to 9645a75 Compare March 8, 2021 16:56
@benvanik
benvanik force-pushed the benvanik-flow branch 4 times, most recently from 088fea8 to 3ad7762 Compare March 8, 2021 18:55
@benvanik
benvanik force-pushed the benvanik-flow-tie branch 4 times, most recently from 975f12a to f57db03 Compare March 9, 2021 02:08
@benvanik
benvanik requested a review from silvasean March 9, 2021 15:27
@benvanik

benvanik commented Mar 9, 2021

Copy link
Copy Markdown
Collaborator Author

This is paired with #4881 and adds a new TiedOpInterface to IREEInterfaces.td. A few ops in FlowOps.td now implement the interface and gained new/upgraded printers/parsers.

Almost everything else besides the new interface and a few folders in FlowOpFolders.cpp is plumbing, lit test updates, or renamings. The largest change is that the !flow.dispatch.input and !flow.dispatch.output types were combined into !flow.dispatch.tensor that now specifies whether it's readonly, readwrite, or writeonly. This allows the flow.dispatch.workgroups and related ops to correctly verify loads/stores and as things are lowered into the HAL to correctly materialize the hal.interface.binding for each I/O. ConvertStreamOps.cpp was hacked around to reuse allocations for the in-place operations - that'll all be changing next as part of buffer allocation and only the minimal was done there to verify this new interface works and that it could start being used by Mahesh immediately for in-place things like padding.

@benvanik
benvanik marked this pull request as ready for review March 9, 2021 15:32
@nicolasvasilache

Copy link
Copy Markdown
Contributor

Yes please!
What are the plans to slice this up and send upstream?
If you don't know where: Linalg :)
Linalg can then use it and remove some baked-in assumptions.
I can also very much use this in the sandbox bufferization.

@benvanik

benvanik commented Mar 9, 2021

Copy link
Copy Markdown
Collaborator Author

What are the plans to slice this up and send upstream?

No immediate plans from me as I wanted to kick the tires a bit but I put the interface and supporting utils in a standalone place so that it'd be easy to migrate if desired. I'm sure there would be some improvements as it moved upstream so I wanted to decouple the code as much as possible. The nice thing is that composing interfaces is pretty nice at this boundary because there is only one time that the linalg->IREE jump is needed (during the flow.dispatch.workgroups formation) and if linalg had a similar interface it'd be just a few lines of code to handle.

Maybe we can gather feedback from you/Mahesh on what you would want in linalg and figure out if this is the kind of thing you want or if there's something simpler that could be used in linalg because some of this is already somewhat defined in there (with outs and such) :)

@silvasean silvasean 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.

A drive-by nit. Will let Mahesh do the detailed review.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

First set of reviews. Nothing major. Mostly trying to understand this.

Sets the operand index tied to the given result index, if any.
}],
"void", "setTiedResultOperandIndex",
(ins "unsigned":$resultIndex, "::llvm::Optional<unsigned>":$operandIndex),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why use llvm::Optional<unsigned> instead of just unsigned and setting a default value for the operand. I think there is a way to specify that in ODS.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not sure I follow - the optional is because you can either set an operand index that the result is tied to or clear it and indicate that the result is not tied to anything. What in ODS are you referring to?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ok, maybe I am mistaken on what this is intended on. I thought it was an "optional" argument with a default value.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

ah no - just a way to clear things that's paired with getTiedResultOperandIndex that also returns an Optional - that way you can do opB.setTiedResultOperandIndex(0, opA.getTiedResultOperandIndex(0))

@benvanik
benvanik force-pushed the benvanik-flow branch 5 times, most recently from 893a9f1 to d34bdc2 Compare March 11, 2021 16:27
@benvanik
benvanik force-pushed the benvanik-flow-tie branch from f57db03 to f198dd8 Compare March 11, 2021 17:32
@benvanik
benvanik force-pushed the benvanik-flow-tie branch from f198dd8 to 3808aef Compare March 11, 2021 18:33
Base automatically changed from benvanik-flow to main March 11, 2021 18:33
@benvanik
benvanik force-pushed the benvanik-flow-tie branch 5 times, most recently from 1ddd1d6 to b7d1bb7 Compare March 11, 2021 19:23
This allows for results of operations to be tied back to their
operands in storage but not in time. This allows for in-place
operations to be defined on tensors that carry enough metadata
to be able to correctly form streams, materialize HAL
interfaces, and allocate buffers.

Example:
```mlir
%t = flow.dispatch @foo[...](%input) : (tensor<4xf32>) -> %input
```

This syntax also combines with the shape-carrying op interface
to make it possible to also indicate that an input and a result
share type and shape information:
```mlir
%t = flow.dispatch @foo[...](%input) : (tensor<?xf32>{%dim}) -> %input
```
which is effectively:
```mlir
%t = flow.dispatch @foo[...](%input) : (tensor<?xf32>{%dim}) -> tensor<?xf32>{%dim}
```
but with the extra bit that result 0 is tied to operand 0.

Here the result %t of the dispatch aliases the storage for %input,
making %input a read-write/mutable binding in the resulting HAL
executable. %t is a distinct SSA value from %input, though, and
represents the value of the storage backing %input after the
dispatch has completed. By keeping the SSA use-def chains correct
with respect to time they are still meaningful for analysi2As and
nothing at this level (and the beginning of the HAL transformations)
needs to perform alias analysis, while still giving us all of the
information required to induce aliasing during later allocation
passes.
@benvanik
benvanik force-pushed the benvanik-flow-tie branch from b7d1bb7 to c4125df Compare March 11, 2021 19:33
@benvanik
benvanik merged commit 54749ce into main Mar 11, 2021
@benvanik
benvanik deleted the benvanik-flow-tie branch March 11, 2021 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants