Skip to content

[mlir][affine] Add AffineScope trait to scf.index_switch #68672

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

Closed
wants to merge 1 commit into from

Conversation

gptsarthak
Copy link
Contributor

Fixes #64287

The code in AffineStructures.cpp:FlatAffineValueConstraints' assert statement checks that the loop bound (i.e. val) isTopLevelValue or isForInductionVar. In this case, %dim = memref.dim %20, %c0 : memref<?x?x6xi1> is neither, since it is inside scf.index_switch, which is not a part of AffineScope. Thus, the assert is triggered.

However, %dim = memref.dim %20, %c0 : memref<?x?x6xi1> is an invariant in the scope of the scf.index_switch. If it was a loop, a invariant code motion could have hoisted it to be outside the scf scope, making it a valid TopLevelValue.

Adding the AffineScope trait to scf.index_switch solves the issue. This defines a new top level scope for symbols, which proves highly practical as it enables a broader range of things to be represented as sequences of affine loop nests.
I even suggest that we add this trait to other SCF operations like scf.for, scf.while as well, as that will be very useful, and should not have any major issues that we cannot solve.

Copy link
Contributor

@bondhugula bondhugula left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 337 to 338
%3 = affine.load %1[%arg0] : memref<?xi1>
affine.store %3, %alloc[%arg0] : memref<?xi1>
Copy link
Contributor

Choose a reason for hiding this comment

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

Indent with two spaces.

Copy link
Contributor Author

@gptsarthak gptsarthak Oct 11, 2023

Choose a reason for hiding this comment

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

Fixed. Thank you for reviewing my first patch in affine!
Do you think we can add this trait to scf.if and some other scf operations too? That will be very useful as it defines a new top level scope for symbols. Or we can just do that when an issue arises.

Adding the `AffineScope` trait to `scf.index_switch` solves llvm#64287. This defines a new top level scope for symbols, which proves highly practical as it enables a broader range of things to be represented as sequences of affine loop nests.
I suggest that we add this trait to other SCF operations like `scf.for`, `scf.while` as well, as that will be very useful, and should not have any major issues that we cannot solve.
@bondhugula
Copy link
Contributor

This isn't actually the right fix to #64287 - the analysis methods needed to properly return status. Fixed here: #129476

@bondhugula bondhugula closed this Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mlir] affine-parallelize pass crashed with assertion failure "non-terminal symbol / loop IV expected".
3 participants