I have a question about compiler warnings that are being generated when I build LLVM with ClangIR enabled. I don’t think the warnings I’m asking about are specific to the ClangIR code, but they may not have been seen elsewhere because they only appear if you compile using GCC and explicitly specify the -Woverloaded-virtual option, which clang does.
The summary is that when -Woverloaded-virtual is used, GCC issues warnings about virtual functions that are hidden by declaring a function of the same name with a different signature in a derived class. Clang doesn’t give a warning about this. Here’s a small example showing the difference between GCC and clang behavior without bringing in MLIR:
https://godbolt.org/z/46edeKM5T
Now for the MLIR-specific part of this…
In ClangIR, we have a class CIRToLLVMGlobalOpLowering which derives from mlir::OpConversionPattern<cir::GlobalOp> and defines a method with this signature:
mlir::LogicalResult
matchAndRewrite(cir::GlobalOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override;
When I compile this with GCC, I get a number of warnings similar to the following:
warning: 'llvm::LogicalResult mlir::OpConversionPattern<SourceOp>::matchAndRewrite(mlir::Operation*,
llvm::ArrayRef<mlir::Value>, mlir::ConversionPatternRewriter&) const [with SourceOp = cir::GlobalOp]'
was hidden [-Woverloaded-virtual]
647 | matchAndRewrite(Operation *op, ArrayRef<Value> operands,
| ^~~~~~~~~~~~~~~
It took me a while to understand exactly what’s happening. GCC is issuing a warning because mlir::OpConversionPattern<cir::GlobalOp> defines four virtual functions named matchAndRewrite (two of which are deprecated and marked final) and we’re only overriding one of them.
@erichkeane suggested adding a using mlir::OpConversionPattern<cir::GlobalOp>::matchAndRewrite; statement, but this produces an error because the mlir::OpConversionPattern<cir::GlobalOp> has a similar using statement for its base class, ConversionPattern, and that statement is declared private.
So, basically, I don’t think I can change the ClangIR code in a way that avoids this warning other than by doing something to suppress the warning. That is, I can’t fix the problem it’s reporting, only silence it. The “fix” – I think – would be to modify the MLIR base classes to use some kind impl pattern so the functions I’m overloading wouldn’t have a name conflict with functions that the class I’m deriving from is overloading, but that feels like it could have an unacceptable impact on downstream code.
To be clear, I don’t think there’s currently a real problem related to what GCC is warning about. I don’t expect anyone to intend to call the “hidden” functions. My only goal here is to get to a warning-free state.
There’s some discussion about GCC’s behavior here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=20423
There’s some discussion about removing the -Woverloaded-virtual from clang builds (and why they decided not to) here:
D82617 Disable GCC’s -Woverloaded-virtual, which has false positives.
Has anyone else seen this? Any suggestions for the best way to handle it?