Skip to content

[CIR][MLIR] Fix overloaded virtual warnings #1439

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

andykaylor
Copy link
Collaborator

There is a difference in behavior between clang and gcc that was leading to a very large number of -Woverloaded-virtual warnings when compiling with gcc.

The difference in behavior is that gcc warns for "partial" virtual overloads. That is, when a base class contains multiple virtual functions with the same name and a derived class overrides at least one but not all of them, gcc issues a warning saying that the non-overridden variations were hidden. This diagnostic is only reported if the -Woverloaded-virtual is explicitly specified on the command line, which it is by the clang project.

The standard way to suppress this warning, indicating that the behavior was intentional (which it was in all cases I'm fixing here) is to add a "using" statement for the overloaded symbol in the derived class. However, there was a secondary problem that this couldn't be done in the ClangIR classes because of a "using" clause in the base class that was marked as "private".

This change updates a few files in the MLIR, both to allow subclasses to add their own "using" statements by making the base class "using" statement protected, and also fixes a large number of places where the derived class "using" statement was needed in MLIR dialects, in addition to adding such statements where needed in the ClangIR code.

The MLIR changes will likely be made upstream, but I am adding them here in the clangir incubator as an proof-of-concept to show what will be needed in downstream projects.

There is a difference in behavior between clang and gcc that was leading to
a very large number of -Woverloaded-virtual warnings when compiling with
gcc.

The difference in behavior is that gcc warns for "partial" virtual overloads.
That is, when a base class contains multiple virtual functions with the same
name and a derived class overrides at least one but not all of them, gcc
issues a warning saying that the non-overridden variations were hidden. This
diagnostic is only reported if the -Woverloaded-virtual is explicitly
specified on the command line, which it is by the clang project.

The standard way to suppress this warning, indicating that the behavior was
intentional (which it was in all cases I'm fixing here) is to add a
"using" statement for the overloaded symbol in the derived class. However,
there was a secondary problem that this couldn't be done in the ClangIR
classes because of a "using" clause in the base class that was marked as
"private".

This change updates a few files in the MLIR, both to allow subclasses to add
their own "using" statements by making the base class "using" statement
protected, and also fixes a large number of places where the derived class
"using" statement was needed in MLIR dialects, in addition to adding such
statements where needed in the ClangIR code.

The MLIR changes will likely be made upstream, but I am adding them here in the
clangir incubator as an proof-of-concept to show what will be needed in
downstream projects.
@bcardosolopes
Copy link
Member

Can we split this PR int one for MLIR and another one for CIR? But approach LGTM!

@@ -13,6 +13,7 @@

#include "mlir/Conversion/VectorToSCF/VectorToSCF.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.h"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure why this happened. I think my editor did it when I wasn't looking.

@andykaylor
Copy link
Collaborator Author

Can we split this PR int one for MLIR and another one for CIR? But approach LGTM!

I can do that, but I'm not sure I understand the benefit of doing so in the incubator. I think that if the MLIR community doesn't want to make this change upstream we'll need to do something different in the CIR code (like maybe adding -Wno-overloaded-virtual to our build files), so there's a pretty tight dependency between the changes. That said, I'm happy to split them up if you see a reason to do so.

Will splitting them up making rebasing easier?

@bcardosolopes
Copy link
Member

bcardosolopes commented Mar 5, 2025

Will splitting them up making rebasing easier?

Potentially yes, should have mentioned in the other comment! That's the solely the reason

@bcardosolopes
Copy link
Member

I think that if the MLIR community doesn't want to make this change upstream we'll need to do something different in the CIR code (like maybe adding -Wno-overloaded-virtual to our build files

No problem in having a custom solution if they don't want this!

@@ -1415,6 +1416,7 @@ struct DownscaleSizeOneWindowed2DConvolution final
PatternRewriter &rewriter) const override {
return returningMatchAndRewrite(convOp, rewriter);
}
using OpRewritePattern<Conv2DOp>::matchAndRewrite;
Copy link

Choose a reason for hiding this comment

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

Wouldn't it be better to have these protected like in all other places?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Honestly, I think we at least don't care that the other variants are hidden, and don't necessarily want them here public or protected. They're public in the base class, I believe, but I don't suppose we want them to be called, so probably protected is better.

Or maybe this is an argument for just adding -Wno-overloaded-virtual to the ClangIR build, since apart from the warning the current behavior is what we want.

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe this is an argument for just adding -Wno-overloaded-virtual to the ClangIR build, since apart from the warning the current behavior is what we want.

Seems like it might be the less intrusive change for the moment

@matthias-springer
Copy link
Member

matthias-springer commented Mar 5, 2025

This approach requires changes to many patterns: you have to add using everywhere.

I have something different in mind that I would like to try first: removing the match and rewrite functions from the pattern classes. The vast majority of patterns uses a combined matchAndRewrite, so factoring out match and rewrite could clean up the code base a bit and maybe solve the warnings here to some degree.

Can you check if this improves the situation? llvm/llvm-project#129861

@@ -688,8 +688,9 @@ class OpConversionPattern : public ConversionPattern {
return matchAndRewrite(op, OpAdaptor(oneToOneOperands, adaptor), rewriter);
}

private:
protected:
Copy link
Member

Choose a reason for hiding this comment

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

The using here is only to silence warnings. I think it won't be a problem to turn it into protected.

@andykaylor
Copy link
Collaborator Author

This approach requires changes to many patterns: you have to add using everywhere.

I have something different in mind that I would like to try first: removing the match and rewrite functions from the pattern classes. The vast majority of patterns uses a combined matchAndRewrite, so factoring out match and rewrite could clean up the code base a bit and maybe solve the warnings here to some degree.

Can you check if this improves the situation? llvm/llvm-project#129861

I said more on Discourse, but the short answer is that your change does not fix these warnings and still requires adding using in many places.

@bcardosolopes
Copy link
Member

Should we close this one?

@andykaylor
Copy link
Collaborator Author

Abandoned in favor of #1440

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

Successfully merging this pull request may close these issues.

4 participants