-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SCF] add debug label for MergeNestedParallelLoops pattern #135980
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
[SCF] add debug label for MergeNestedParallelLoops pattern #135980
Conversation
Adding debug labels to rewrite patterns gives the canonicalizer the ability to disable specific patterns if undesired. The motivation here is that since there is currently no way to handle attributes on ops that are changed when these patterns are applied, this at least provides a workaround.
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-scf Author: Scott Manley (rscottmanley) ChangesAdding debug labels to rewrite patterns gives the canonicalizer the ability to disable specific patterns if undesired. The motivation here is that since there is currently no way to handle attributes on ops that are changed when these patterns are applied, this at least provides a workaround. Full diff: https://github.com/llvm/llvm-project/pull/135980.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/SCF/IR/SCF.cpp b/mlir/lib/Dialect/SCF/IR/SCF.cpp
index c454f342b4204..04e89105c056a 100644
--- a/mlir/lib/Dialect/SCF/IR/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/IR/SCF.cpp
@@ -3202,9 +3202,9 @@ struct MergeNestedParallelLoops : public OpRewritePattern<ParallelOp> {
void ParallelOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
- results
- .add<ParallelOpSingleOrZeroIterationDimsFolder, MergeNestedParallelLoops>(
- context);
+ results.add<ParallelOpSingleOrZeroIterationDimsFolder>(context);
+ results.addWithLabel<MergeNestedParallelLoops>({"MergeNestedParallelLoops"},
+ context);
}
/// Given the region at `index`, or the parent operation if `index` is None,
|
mlir/lib/Dialect/SCF/IR/SCF.cpp
Outdated
| context); | ||
| results.add<ParallelOpSingleOrZeroIterationDimsFolder>(context); | ||
| results.addWithLabel<MergeNestedParallelLoops>({"MergeNestedParallelLoops"}, | ||
| context); |
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.
Can you add a name on the pattern instead of a label here?
A label to identify a single pattern does not seem the intended purpose of labels.
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.
Okay I agree after I dug around some more. I took my guidance from what I read in include/mlir/Transforms/Passes.h which seems to say you can do it both ways:
/// ... Debug labels must be set explicitly on patterns or when adding
/// them with `RewritePatternSet::addWithLabel`. Debug names may be empty, but
/// patterns created with `RewritePattern::create` have their default debug name
/// set to their type name.
Does this suggest you could have individual pattern names and also a label (in this case, like SCFParallelPatterns) at the same time?
|
updated with |
|
|
No. That appears to happen with I could add it to the |
Actually it doesnt look like works in that case either. |
|
Both |
Yeah, I see what you mean, through RewritePattern::create.. but it's definitely not working. Ill see if I can track it down. If that's the expected behaviour I would much rather have that anyway. |
|
I think I know what's happening.. |
|
I'm not sure there's a simple solution to resolve this universally.. it seems like explicitly specifying the debug name is probably the best way to get what you would reasonably "expect" |
|
even the I would like to proceed with this change if there is no significant objection. |
|
Well, it's called |
I understand and there's no criticism intended - I am just asking that this one be made predictable. There's probably a worthwhile larger conversation/RFC on perhaps requiring a name for patterns so they can be selectively disabled but this feels like a reasonable fix for now? |
|
I have no objections to the patch itself, but it's not very robust. If you really want to prevent nested loops merging I would suggest instead of using custom attributes on This worked good enough for us downstream |
Perhaps - I am probably not going to sign up to add a name to every Pattern that exists. I would maybe say it provides an example for others should they want to do something similar.
Yes, that's exactly what I was trying to avoid doing. To me this just introduces its own set of challenges to workaround. E.g. if you were hoist an operation out of the inner loop, does it/should it go into the region op or into the body of the outer loop? Some existing xforms don't work unless they have the same parent region, etc. |
|
The problem I see here is that you want to attach semantics through the debug name, however here is how they are documented: That means we should be able to strip these out of a release build for example without breaking anything, which you may object to in the future if you depends on this behavior. |
That's a fair point. Like I said earlier, an RFC to require a name is probably the larger fix. I mean, can we change "debugName" to "patternName" and then my commit is acceptable? It seems to me like this is a neat feature for customizing what you want or not want to be canonicalized depending on various needs of your downstream implementation. |
Probably: but this isn't free, it makes all patterns more costly, and the framework heavier in production.
I would argue that the canonical form of the IR isn't something meant to be customized with this kind of fine grain control. |
I agree with everything you're saying from a compiler design perspective.. but at the same time, I don't think this particular pattern is very good candidate for canonicalization and unfortunately feel the need to disable it somehow. For example, loop optimizations that may apply to a |
|
I am closing this PR. Thank you for your input everyone! |
That I could agree with: parallelism is often expressed in a hierarchical way for good reasons, the collapsing done here is losing this information and does not seem like a good canonicalization to me. |
|
Adding debug labels to rewrite patterns gives the canonicalizer the ability to disable specific patterns if undesired.
The motivation here is that since there is currently no way to handle attributes on ops that are changed when these patterns are applied, this at least provides a workaround.