-
Notifications
You must be signed in to change notification settings - Fork 145
[CIR][Lowering] Introduce HoistAllocasPass #887
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we invent a new pass for this, or just include it in the FlattenCFG
pass?
EDIT: thinking more about this, iterating the hoisting until it reaches the parent basic block will actually involve teaching all different OpRewritePattern in FlattenCFG into it. What I suggest instead is to apply the same logic you are doing here to a specific
I think this is what @Lancern is originally referring to. |
466e5b7
to
797dfaf
Compare
Done. |
797dfaf
to
a520b95
Compare
Update: I try to match A side effect change of this is, the MLIR may try to remove the trivial dead alloca op. So we have to update some tests. e.g., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I try to match AllocaOp instead of FuncOp in the new version.
This is the wrong way to implement this transformation. The pattern rewriting framework requires that you match on the root operation and all transformations should be limited within the root operation. This means if you match on alloca
you can only modify / replace / erase the matched alloca
operations, but you cannot touch other parts of the function.
// It is cheaper to call `mlir::Operation::moveBefore` than using rewriter. | ||
// So we prefer to manually here. | ||
mlir::Operation *insertPoint = &*entryBlock.begin(); | ||
allocaOp->moveBefore(insertPoint); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a rewrite pattern, all IR mutations must be performed through the rewriter, or it may crash. See https://mlir.llvm.org/docs/PatternRewriter/#restrictions :
Within the rewrite section of a pattern, the following constraints apply:
- All IR mutations, including creation, must be performed by the given PatternRewriter. This class provides hooks for performing all of the possible mutations that may take place within a pattern. For example, this means that an operation should not be erased via its erase method. To erase an operation, the appropriate PatternRewriter hook (in this case eraseOp) should be used instead.
- [...]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://mlir.llvm.org/getting_started/Debugging/#detecting-invalid-api-usage mentions -DMLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS=ON
, which might be useful for development? I haven't tried it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wished we had a clang tidy thing for preventing us to use this idiom in rewriters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://mlir.llvm.org/getting_started/Debugging/#detecting-invalid-api-usage mentions
-DMLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS=ON
, which might be useful for development? I haven't tried it though.
Great idea. Maybe we can enable this for github testing, so we catch those in PRs. Now that Nathan disabled unrelated tests perhaps it won't feel like it's taking too much longer.
Wow, this is interesting. Do you have the verifiers ON or OFF? Wonder how long it would take without verifiers (e.g. by using |
Nice catch! |
That's awesome! Eagerly awaiting for the PR merged! There are many tests fail in the llvm-test-suite due to variables declared in loops.
And does NOT fail if the variables are declared outside of the loop. |
a520b95
to
a3f7a21
Compare
Yes, I have this ON without verifiers. I believe the root cause is that pattern rewritten process is applied iteratively until a fixed point. The reproduce case is |
Update: due to the above long time compilation issue, I prefer to move this pass out of FlattenCIR pass into a standalone pass. If I understand correctly, the previous suggestion to move this to FlattenCIR is majorly for style and I feel the current one is not bad. |
Thanks a bunch, this should be enough! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Close llvm#883. See the above issue for details
Close llvm#883. See the above issue for details
Close #883. See the above issue for details
Close #883. See the above issue for details
Close #883. See the above issue for details