[mlir][affine] Add AffineScope
trait to scf.index_switch
#68672
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 insidescf.index_switch
, which is not a part ofAffineScope
. Thus, the assert is triggered.However,
%dim = memref.dim %20, %c0 : memref<?x?x6xi1>
is an invariant in the scope of thescf.index_switch
. If it was a loop, a invariant code motion could have hoisted it to be outside thescf
scope, making it a validTopLevelValue
.Adding the
AffineScope
trait toscf.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.