Repository navigation
Adding TiedOpInterface and wiring it through the Flow dialect. - #5016
Conversation
40e4c4a to
9645a75
Compare
088fea8 to
3ad7762
Compare
975f12a to
f57db03
Compare
|
This is paired with #4881 and adds a new TiedOpInterface to Almost everything else besides the new interface and a few folders in |
|
Yes please! |
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 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
left a comment
There was a problem hiding this comment.
A drive-by nit. Will let Mahesh do the detailed review.
MaheshRavishankar
left a comment
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Ok, maybe I am mistaken on what this is intended on. I thought it was an "optional" argument with a default value.
There was a problem hiding this comment.
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))
893a9f1 to
d34bdc2
Compare
f57db03 to
f198dd8
Compare
f198dd8 to
3808aef
Compare
1ddd1d6 to
b7d1bb7
Compare
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.
b7d1bb7 to
c4125df
Compare
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:
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:
which is effectively:
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:
->
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.