Skip to content

[Integrate] Make IREE compatible with the new memref.assume_alignment semantic change. - #20913

Merged
hanhanW merged 15 commits into
iree-org:mainfrom
hanhanW:drop-memref-assume-alignment-revert
May 29, 2025
Merged

hanhanW merged 15 commits into
iree-org:mainfrom
hanhanW:drop-memref-assume-alignment-revert

Conversation

@hanhanW

@hanhanW hanhanW commented May 28, 2025 •

Copy link
Copy Markdown
Contributor

This drops a revert: iree-org/llvm-project@435bb50

To make IREE compatible with the new memref.assuem_assignment semantics, the revision moves the op creation from OneShotBufferization to post bufferization passes. It is mainly for reducing the burdens of bufferization analysis. Otherwise, it'd easily generate redundant copies between the original memref and the result of AssumeAlignment op.

Furthermore, there are several missing support when AssumeAlignment op is present in SSA chain.

  • The FlattenMemRefSubspanPass needs to be improved to take the op into account.
  • We are missing a pattern that propagates the memref.dim ops for memref.dim(memref.assume_alignment) cases. It reduces the deps from AssumeAlignment op, so we don't need to add more patterns for it.
  • The chained memref.assume_alignment ops are not folded away. This can be done as a canonicalization pattern. The revision adds a workaround for early-bufferization dispatch, and it will be removed once we implement the pattern.
  • Need to teach SPIRVVectorizeLoadStore pass to take AssumeAlignment ops into account.
  • Need to teach VMVX ResolveBufferDescriptors pass to take AssumeAlignment ops into account. The new pattern is copied from the other one, and a refactoring can be done in the future. For now, we mainly want to drop the revert, and we can easily refactor the code out. So it is a TODO for now.
  • It triggers failures in hoisting vector transfer ops when AssumeAlignment ops are present. We don't know the root cause yet, and it is too complicated and hard to fix it within the revision. Because the implementation is in upstream. See [Integrate] Fail to hoisting vectors when memref.assume_alignment is present #20912 for reproducer.

Closes #20908

Signed-off-by: hanhanW <[email protected]>
@hanhanW

hanhanW commented May 28, 2025 •

Copy link
Copy Markdown
Contributor Author

Note: there are some dump memref.assume_alignment ops that I don't fix in the PR. This is also happening in upstream. Somehow, it accepts the op without capturing the SSA value. If the tests are happy, I think most of them are redundant IR in our codebase. E.g.,

Input:

func.func @foo(%0: memref<10xf32>) {
  memref.assume_alignment %0, 32 : memref<10xf32>
  return
}

Output from iree-opt:

module {
  func.func @foo(%arg0: memref<10xf32>) {
    %assume_align = memref.assume_alignment %arg0, 32 : memref<10xf32>
    return
  }
}

@hanhanW
hanhanW requested a review from benvanik as a code owner May 28, 2025 13:13
@krzysz00

Copy link
Copy Markdown
Contributor

Yeah, an assume_alignment that isn't used anywhere (that example that doesn't capture the SSA value) will get dead-code-eliminated, IIRC

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

Awesome! This was a big switch, thanks @hanhanW

patterns.add<RemoveAssumeAlignOp>(context);
populateCleanMemRefAssumeAlignmentPatterns(patterns);
context->getOrLoadDialect<IREE::Util::UtilDialect>()
->getCanonicalizationPatterns(patterns);

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.

Why do we need this if we just end up folding them subsequently anyway?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question!

This is needed because the Linalg/LinalgExt to loops passes can generate memref.dim ops for ub when there is a dynamic shape. The standard pipeline runs the FoldDimOp pattern to fold memref.dim op away in canonicalization pass when the source is IREE HAL bindings. (Because they implement ShapeAwareOpInterface interface.)

However, it is not happening when the source of memref.dim is memref.assume_alignment, which does not implement the interface. This is why I need to implement the pattern that makes it query the dim size from the source instead; kick in the specific canonicalization patterns in the pre-processing.

To make this type of casting work with ShapeAwareOpInterface, I think we'll need to make the interface methods return std::optional<*> instead. Then they can have an indirect query and the world would be easier. I can file an issue for this and start a quick discussion with Ben there, if you think it is helpful.

(I think the memref.assume_alignment semantic change introduces way more issues than I expected; it does not play well with the folding/hoisting/etc transformations. This is why this big switch is hard. IMO, it is better if upstream can keep the both semantic, like create a new op for the new semantic and deprecate the old one later. My guess is that we may be one of few users that has such broad e2e coverage.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was wrong on this. I implemented the folder in 94d2e31, and I think it is needed for recovering all the memref.assume_alignment status. Currently, few of dispatches disable the support.

I'll revisit this part after I fix all the assuem_alignment issues.

@hanhanW hanhanW changed the title [Integrate] Move memref.assume_alignment creation to post bufferization phase. [Integrate] Make IREE compatible with the new memref.assume_alignment semantic change. May 29, 2025
@hanhanW
hanhanW enabled auto-merge (squash) May 29, 2025 00:20
@hanhanW
hanhanW merged commit cbb7536 into iree-org:main May 29, 2025
@hanhanW
hanhanW deleted the drop-memref-assume-alignment-revert branch May 29, 2025 00:53
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
… semantic change. (#20913)

This drops a revert:
iree-org/llvm-project@435bb50

To make IREE compatible with the new `memref.assuem_assignment`
semantics, the revision moves the op creation from OneShotBufferization
to post bufferization passes. It is mainly for reducing the burdens of
bufferization analysis. Otherwise, it'd easily generate redundant copies
between the original memref and the result of AssumeAlignment op.

Furthermore, there are several missing support when AssumeAlignment op
is present in SSA chain.
- The `FlattenMemRefSubspanPass` needs to be improved to take the op
into account.
- We are missing a pattern that propagates the `memref.dim` ops for
`memref.dim(memref.assume_alignment)` cases. It reduces the deps from
AssumeAlignment op, so we don't need to add more patterns for it.
- The chained memref.assume_alignment ops are not folded away. This can
be done as a canonicalization pattern. The revision adds a workaround
for early-bufferization dispatch, and it will be removed once we
implement the pattern.
- Need to teach SPIRVVectorizeLoadStore pass to take AssumeAlignment ops
into account.
- Need to teach VMVX ResolveBufferDescriptors pass to take
AssumeAlignment ops into account. The new pattern is copied from the
other one, and a refactoring can be done in the future. For now, we
mainly want to drop the revert, and we can easily refactor the code out.
So it is a TODO for now.
- It triggers failures in hoisting vector transfer ops when
AssumeAlignment ops are present. We don't know the root cause yet, and
it is too complicated and hard to fix it within the revision. Because
the implementation is in upstream. See
#20912 for reproducer.

Closes #20908

---------

Signed-off-by: hanhanW <[email protected]>
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.

[MLIR Integrate] Revisit the use of memref.assume_alignment ops in IREE

3 participants