This patch collects operations that have users in a for loop and uses them when loop invariant operations are detected and hoisted.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
187–188 | This choice of name definedOps appears to be confusing: there is no such thing as ops being defined. It would be good rename this and add a comment while on this (since you are adding to definedOps below) to make this readable. | |
197 | Nit: op.getNumResults() > 0 if you prefer. |
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
187–188 | I have renamed it to opsWithUsers |
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
197 | Just because the op has results doesn't mean those results have users. You can do a check in O(1) time to determine if the results have no uses (use_empty()). This will make the check more powerful and you'll still be able to hoist those that have results but are ultimately unused. |
Thank you for bringing up this issue in LICM, it is an important one. I have a few comments on the fix:
- Inherent issue is definedOps ( going to be renamed to opsWithUsers) is being populated only under some conditions, and does not actually do the job of collecting all ops that define an SSA value in that block.
- Even after this fix, only AffineForOps will be added to the list, but there are other cases like AffineDma... ops, AffineIfOps that are actually NOT code invariant etc. which will still cause similar problems.
So, I suggest that you can implement a generic fix for this inherent issue, where any op that defines an SSA value can be added to the above list, and this is decoupled from the checks like 'isOpLoopInvariant`. This will make the check more robust, and relevant test cases can also be added.
Minor suggestion.
mlir/test/Dialect/Affine/affine-loop-invariant-code-motion.mlir | ||
---|---|---|
620 | Minor suggestion: It is better to create a minimal test case that captures the intended check. A similar test case without vector_load and vector.reduction can be created to improve readability. |
Rework based on comments
- Separate the part that identifies operations that have users and use it in the later phase.
Thanks @vinayaka-polymage for the very useful feedback. Hadnt realized the issue might affect other operations such as Affineif etc. I have reworked the code and also addes two tests.
@sumesh13 This new generic fix looks good, it handles more cases that were previously not handled. Thanks for this. I have added some comments related to code organization, please check them. Logic looks good to me.
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
48 | It is better to keep this local like before, see comment below. | |
190 | This is a per AffineForOp var, and can be kept local here like before. | |
201 | Populating into definedOps (opsWithUsers) can happen here. No need to walk the affine for, because source of an operand for any op, can only come before it. So, if (op->getNumResults() > 0 && !op->use_empty()) opsWithUsers.insert(op); can be placed here. |
@sumesh13 Thanks for addressing all the comments, the change looks good now. Can you please check these last two comments before I approve this ?
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
81–82 | Don't need this now, right ? This line can be deleted, and comment be moved to the location where it is getting populated. | |
226–227 | This section now does not have any change, so please make sure that there is no diff here. |
Please change the commit title to a more relevant one, I am approving this.
@bondhugula Could you please take a final pass at this?
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
81–82 | Actually, I take my above comment back. This function is being used in ifOp hoisting as well, and with the current implementation it will be needed. Refactoring that can be a separate change in itself. |
@vinayaka-polymage Just noticed that you accepted a previous revision with comment on handling of ifops. Considering ifops, would you think it might be helpful to just collect all the ops like I was doing in a previous revision ?
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
81–82 | Thanks ...that was one reason I was collecting ops separately. Would you think that might be preferable ? |
Adding back a change from previous revision that was approved and some minor cleanup.
Gentle reminder..also pls suggest if it would be better to collect all the opsWithUsers separately as i was doing in a previous revision. For nested ifs, looks like otherwise you may not be collecting all the opsWithUsers by just collecting at the top level and leaves.
mlir/lib/Dialect/Affine/Transforms/AffineLoopInvariantCodeMotion.cpp | ||
---|---|---|
199 | I think just !op.use_empty() should be sufficient. |
@bondhugula , @vinayaka-polymage thanks for the review. @sgrechanik Can you pls help land this
It is better to keep this local like before, see comment below.