Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][transform] Overhaul RegionBranchOpInterface implementations. #111408

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ingomueller-net
Copy link
Contributor

@ingomueller-net ingomueller-net commented Oct 7, 2024

This PR adds the ReturnLike trait to transform.yield. This is required in the one-shot bufferization pass since the merging of #110322, which analyses any FunctionOpInterface and expects them to have a ReturnLike terminator.

If we give transform.yield the ReturnLike trait, then the checks made by RegionBranchOpInterface behave differently. More concretely, the check that the operands and results passed across the control flow edges from the parent op to its regions and back are equal (or compatible). Which results are passed back from regions to the parent op depend on the terminator and/or whether it is ReturnLike.

This commit radically changes the implementations of the alternatives, foreach, and sequence ops, which are the ops that use yield as their terminator. In fact, all of these ops only ever pass control from the parent op to the region and from there back to the parent op. In particular and unlike scf.for, transform.foreach does not pass control from one iteration of its body to the next directly; it rather passes the control back to the parent op, which then passes it back to the body for the next iteration. That can be seen by the fact that the body always gets arguments of the same type as the operands of the parent op (and none of the yielded types) and the types that are yielded correspond exactly to the result types of the parent op.

It is unclear why the previous implementation worked. Clearly, the type checks have not been executed because the terminator was not ReturnLike, so the mismatching types passed unnoticed. I suppose that there simply is/was no other use case of using the RegionBranchOpInterface of the affected ops yet.

This PR adds the `ReturnLike` trait to `transform.yield`. This is
required in the one-shot bufferization pass since the merging of
 llvm#110332, which analyses any `FunctionOpInterface` and expects them to
have a `ReturnLike` terminator.

Signed-off-by: Ingo Müller <ingomueller@google.com>
@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2024

@llvm/pr-subscribers-mlir

Author: Ingo Müller (ingomueller-net)

Changes

This PR adds the ReturnLike trait to transform.yield. This is required in the one-shot bufferization pass since the merging of #110332, which analyses any FunctionOpInterface and expects them to have a ReturnLike terminator.


Full diff: https://github.com/llvm/llvm-project/pull/111408.diff

1 Files Affected:

  • (modified) mlir/include/mlir/Dialect/Transform/IR/TransformOps.td (+2-1)
diff --git a/mlir/include/mlir/Dialect/Transform/IR/TransformOps.td b/mlir/include/mlir/Dialect/Transform/IR/TransformOps.td
index b946fc8875860b..d3933cad920a3f 100644
--- a/mlir/include/mlir/Dialect/Transform/IR/TransformOps.td
+++ b/mlir/include/mlir/Dialect/Transform/IR/TransformOps.td
@@ -1358,7 +1358,8 @@ def VerifyOp : TransformDialectOp<"verify",
 }
 
 def YieldOp : TransformDialectOp<"yield",
-    [Terminator, DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
+    [Terminator, ReturnLike,
+     DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
   let summary = "Yields operation handles from a transform IR region";
   let description = [{
     This terminator operation yields operation handles from regions of the

@ingomueller-net
Copy link
Contributor Author

One could also argue that the one-shot bufferization is using the interface wrong: AFAIU, FunctionOpInterface does not prescribe the ops implementing it to have ReturnLike terminators, so the pass should not assume this kind of terminators.

Copy link
Member

@matthias-springer matthias-springer left a comment

Choose a reason for hiding this comment

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

Irrespective of the bufferization issue, this change makes sense to me. yield ops in other dialects typically implement the ReturnLike trait, e.g., scf.yield.

If we give `transform.yield` the `ReturnLike` trait, then the checks
made by `RegionBranchOpInterface` behave differently. More concretely,
the check that the operands and results passed across the control flow
edges from the parent op to its regions and back are equal (or
compatible). Which results are passed back from regions to the parent op
depend on the terminator and/or whether it is `ReturnLike`.

This commit radically changes the implementations of the `alternatives`,
`foreach`, and `sequence` ops, which are the ops that use `yield` as
their terminator. In fact, all of these ops only ever pass control from
the parent op to the region and from there back to the parent op. In
particular and unlike `scf.for`, `transform.foreach` does *not* pass
control from one iteration of its body to the next directly; it rather
passes the control back to the parent op, which then passes it back to
the body for the next iteration. That can be seen by the fact that the
body always gets arguments of the same type as the operands of the
parent op (and none of the yielded types) and the types that are yielded
correspond exactly to the result types of the parent op.

It is unclear why the previous implementation worked. Clearly, the type
checks have not been executed because the terminator was not
`ReturnLike`, so the mismatching types passed unnoticed. I suppose that
there simply is/was no other use case of using the
`RegionBranchOpInterface` of the affected ops yet.

Signed-off-by: Ingo Müller <ingomueller@google.com>
@ingomueller-net ingomueller-net changed the title [mlir][transform] Make yield a ReturnLike op. [mlir][transform] Overhaul RegionBranchOpInterface implementations. Oct 10, 2024
@ingomueller-net
Copy link
Contributor Author

I just pushed a new commit (hopefully) fixing the failed tests. That commit increases the magnitude of the commit significantly, so I'd like another full review before proceeding.

The new commit changes the behavior of the implementations of RegionBranchOpInterface quite radically, to the point where I wonder whether the previous implementations were at all functional. I couldn't reconstruct what they were trying to achieve but they did and do not fit my mental model of what the ops do. See the commit message for details.

@@ -104,16 +104,8 @@ transform::AlternativesOp::getEntrySuccessorOperands(RegionBranchPoint point) {

void transform::AlternativesOp::getSuccessorRegions(
RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &regions) {
for (Region &alternative : llvm::drop_begin(
Copy link
Member

Choose a reason for hiding this comment

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

Removing this does not seem right. When a region fails, the op jumps to the next region.

Copy link
Member

Choose a reason for hiding this comment

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

In particular and unlike scf.for, transform.foreach does not pass control from one iteration of its body to the next directly; it rather passes the control back to the parent op, which then passes it back to the body for the next iteration. That can be seen by the fact that the body always gets arguments of the same type as the operands of the parent op (and none of the yielded types) and the types that are yielded correspond exactly to the result types of the parent op.

I don't follow this part. Where is the type mismatch here?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I guess you are saying that the successor operands do not match with the block arguments of the next region.

Copy link
Member

Choose a reason for hiding this comment

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

I think the RegionBranchOpInterface does not support the use case here. When you branch back to the parent op, there is no way to go back into the op a second time.

    A "region successor" indicates the target of a branch. It can indicate
    either a region of this op or this op. In the former case, the region
    successor is a region pointer and a range of block arguments to which the
    "successor operands" are forwarded to. In the latter case, the control flow
    leaves this op and the region successor is a range of results of this op to
    which the successor operands are forwarded to.

This sentence talks about results of the op, so if we would allow going back into the op, you could set the same results multiple times.

    In the latter case, the control flow
    leaves this op and the region successor is a range of results of this op to
    which the successor operands are forwarded to.

I think we should clarify this in the documentation of RegionBranchOpInterface. (We could also allow it but most transformations on top of RegionBranchOpInterface probably assume that you cannot go back into the op.)

Do we have to implement the RegionBranchOpInterface on these transform ops at all?

Copy link
Contributor Author

@ingomueller-net ingomueller-net Oct 10, 2024

Choose a reason for hiding this comment

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

In sequence, I think the sentence matches, no? When control reaches yield, the operands of yield are forwarded to the parent op, they become the result of the op, and "control flow leaves [the] op," right?

What doesn't quite fit for foreach and alternatives is the "control flow leaves [the] op" part. One could argue that the "region successor is a [part of or a potential] range of results of [the] op," but then the control flow does not leave the op; instead it may go to the next region or re-enter the body. Plus there are rules about how to combine the different results yielded by different regions/iterations (though the types always match).

Copy link
Member

Choose a reason for hiding this comment

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

In practice, what's the difference between "no arguments are passed" and "it isn't specified which arguments are passed"? The former does not imply that the region must have 0 arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe that "it isn't specified which arguments are passed" cannot be expressed currently. An empty argument list is always interpreted as "no arguments are passed."

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's correct. But I am wondering if it makes any difference for analyses and transformations.

If we a region has an argument, but the RegionBranchOpInterface says that no arguments are forwarded to that region: what does that mean? It means that we have no idea where the data for that block argument is coming from. Maybe the op itself produces it.

Or maybe the value was actually forwarded from another region but we did not account for it in the RegionBranchOpInterface (and the terminator interface).

Does it matter which one is the case for an analysis that checks the RegionBranchOpInterface? It has to be conservative around such cases anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we a region has an argument, but the RegionBranchOpInterface says that no arguments are forwarded to that region: what does that mean?

Are you sure that that can even exist? I think I ran into failed type check that seem to check exactly that while working on the last commit...

Copy link
Member

Choose a reason for hiding this comment

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

I think scf.forall is an example.

ingomueller-net added a commit to ingomueller-net/llvm-project that referenced this pull request Oct 14, 2024
This PR revisits the traits and interfaces of the `alternatives`,
`foreach`, and `yield`ops. First, it adds the `ReturnLike` trait to
`transform.yield`. This is required in the one-shot bufferization pass
since the merging of llvm#110332, which analyses any `FunctionOpInterface`
and expects them to have a `ReturnLike` terminator.

This uncovered problems with `alternatives` and `foreach`, which
implemented the `BranchRegionOpInterface`. That interface verifies that
the operands and results passed across the control flow edges from the
parent op to its regions and back are equal (or compatible).
Which results are passed back from regions to the parent op depend on
the terminator and/or whether it is `ReturnLike`. Making `yield` a
`ReturnLike` op, those checks fail without other changes.

We explored and rejected the possibility to fix the implementation of
`RegionBranchOpInterface` of the two concerned ops in llvm#111408. The
problem is that the interface expects the result passed from a region to
the parent op to *be* the result of the op, whereas in the two ops they
*contribute* to the result: in `alternatives` only the operand yielded
from one of the regions becomes the result whereas the others are
ignored, and in `foreach` the operands from all iterations are fused
into a new value that becomes the result of the op.

Signed-off-by: Ingo Müller <ingomueller@google.com>
Copy link
Member

@ftynse ftynse left a comment

Choose a reason for hiding this comment

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

Sorry, I missed this. This may depend on what we understand by ReturnLike, but transform.yield is likely not that. In particular, ReturnLike implies one-to-one mapping of the yielded value to the result of the parent operation or the arguments of the next region in dataflow analyses, which is not the case for transform.yield used in alternatives, or foreach.

@ingomueller-net
Copy link
Contributor Author

One could also argue that the one-shot bufferization is using the interface wrong: AFAIU, FunctionOpInterface does not prescribe the ops implementing it to have ReturnLike terminators, so the pass should not assume this kind of terminators.

Thanks, @ftynse, for helping us decide. I think that my earlier statement (quoted here) is then correct: one cannot assume that FunctionOpInterface ops have ReturnLike terminators, so that change was not valid. That also corresponds to @ftynse's comment in #112615.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants