- Notifications
You must be signed in to change notification settings - Fork14.5k
[clang-tidy] Add MLIR check for old op builder usage.#149148
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
jpienaar wants to merge9 commits intollvm:mainChoose a base branch fromjpienaar:clangtidy-mlir
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+240 −0
Open
Changes from1 commit
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
b404f53
[clang-tidy] Add MLIR check for old op builder usage.
jpienaar34b2a3a
Move into LLVM module as this seems better spot given lack of other p…
jpienaardc85637
Fix formatting
jpienaara74232f
Address review comments
jpienaar4fe574a
Address missed review comments
jpienaara25b8af
Fix wrong type
jpienaar9a86c0f
Address clang-tidy warnings except in mlir_op_builder.cpp which is fo…
jpienaar46ca80b
Update clang-tools-extra/clang-tidy/llvm/MLIROpBuilderCheck.cpp
jpienaar01f5fbb
Renamings & extra tests
jpienaarFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
[clang-tidy] Add MLIR check for old op builder usage.
Moving towards new create method invocation, add check to flag old usage.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitb404f5390ac5684c7452e69f6fe209e5215f8929
There are no files selected for viewing
2 changes: 2 additions & 0 deletionsclang-tools-extra/clang-tidy/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletionsclang-tools-extra/clang-tidy/ClangTidyForceLinker.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletionsclang-tools-extra/clang-tidy/mlir/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
FrontendOpenMP | ||
Support | ||
) | ||
add_clang_library(clangTidyMLIRModule STATIC | ||
MLIRTidyModule.cpp | ||
OpBuilderCheck.cpp | ||
LINK_LIBS | ||
clangTidy | ||
clangTidyReadabilityModule | ||
clangTidyUtils | ||
clangTransformer | ||
DEPENDS | ||
omp_gen | ||
ClangDriverOptions | ||
) | ||
clang_target_link_libraries(clangTidyMLIRModule | ||
PRIVATE | ||
clangAST | ||
clangASTMatchers | ||
clangBasic | ||
clangLex | ||
clangTooling | ||
) |
39 changes: 39 additions & 0 deletionsclang-tools-extra/clang-tidy/mlir/MLIRTidyModule.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===--- MLIRTidyModule.cpp - clang-tidy ----------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "../ClangTidy.h" | ||
#include "../ClangTidyModule.h" | ||
#include "../ClangTidyModuleRegistry.h" | ||
#include "OpBuilderCheck.h" | ||
namespace clang::tidy { | ||
namespace mlir_check { | ||
class MLIRModule : public ClangTidyModule { | ||
public: | ||
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { | ||
CheckFactories.registerCheck<OpBuilderCheck>("mlir-op-builder"); | ||
} | ||
ClangTidyOptions getModuleOptions() override { | ||
ClangTidyOptions Options; | ||
return Options; | ||
} | ||
}; | ||
// Register the ModuleModule using this statically initialized variable. | ||
static ClangTidyModuleRegistry::Add<MLIRModule> X("mlir-module", | ||
"Adds MLIR lint checks."); | ||
} // namespace mlir_check | ||
// This anchor is used to force the linker to link in the generated object file | ||
// and thus register the MLIRModule. | ||
volatile int MLIRModuleAnchorSource = 0; // NOLINT(misc-use-internal-linkage) | ||
} // namespace clang::tidy |
102 changes: 102 additions & 0 deletionsclang-tools-extra/clang-tidy/mlir/OpBuilderCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//===--- OpBuilderCheck.cpp - clang-tidy ----------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "OpBuilderCheck.h" | ||
#include "clang/ASTMatchers/ASTMatchers.h" | ||
#include "clang/Basic/LLVM.h" | ||
#include "clang/Lex/Lexer.h" | ||
#include "clang/Tooling/Transformer/RangeSelector.h" | ||
#include "clang/Tooling/Transformer/RewriteRule.h" | ||
#include "clang/Tooling/Transformer/SourceCode.h" | ||
#include "clang/Tooling/Transformer/Stencil.h" | ||
#include "llvm/Support/Error.h" | ||
namespace clang::tidy::mlir_check { | ||
namespace { | ||
using namespace ::clang::ast_matchers; // NOLINT: Too many names. | ||
using namespace ::clang::transformer; // NOLINT: Too many names. | ||
class TypeAsWrittenStencil : public StencilInterface { | ||
public: | ||
explicit TypeAsWrittenStencil(std::string S) : id(std::move(S)) {} | ||
std::string toString() const override { | ||
return (llvm::Twine("TypeAsWritten(\"") + id + "\")").str(); | ||
} | ||
llvm::Error eval(const MatchFinder::MatchResult &match, | ||
std::string *result) const override { | ||
auto n = node(id)(match); | ||
if (!n) | ||
return n.takeError(); | ||
auto srcRange = n->getAsRange(); | ||
if (srcRange.isInvalid()) { | ||
return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument, | ||
"srcRange is invalid"); | ||
} | ||
auto range = n->getTokenRange(srcRange); | ||
auto nextToken = [&](std::optional<Token> token) { | ||
if (!token) | ||
return token; | ||
return clang::Lexer::findNextToken(token->getLocation(), | ||
*match.SourceManager, | ||
match.Context->getLangOpts()); | ||
}; | ||
auto lessToken = clang::Lexer::findNextToken( | ||
range.getBegin(), *match.SourceManager, match.Context->getLangOpts()); | ||
while (lessToken && lessToken->getKind() != clang::tok::less) { | ||
lessToken = nextToken(lessToken); | ||
} | ||
if (!lessToken) { | ||
return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument, | ||
"missing '<' token"); | ||
} | ||
std::optional<Token> endToken = nextToken(lessToken); | ||
for (auto greaterToken = nextToken(endToken); | ||
greaterToken && greaterToken->getKind() != clang::tok::greater; | ||
greaterToken = nextToken(greaterToken)) { | ||
endToken = greaterToken; | ||
} | ||
if (!endToken) { | ||
return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument, | ||
"missing '>' token"); | ||
} | ||
*result += clang::tooling::getText( | ||
CharSourceRange::getTokenRange(lessToken->getEndLoc(), | ||
endToken->getLastLoc()), | ||
*match.Context); | ||
return llvm::Error::success(); | ||
} | ||
std::string id; | ||
}; | ||
Stencil typeAsWritten(StringRef Id) { | ||
// Using this instead of `describe` so that we get the exact same spelling. | ||
return std::make_shared<TypeAsWrittenStencil>(std::string(Id)); | ||
} | ||
RewriteRuleWith<std::string> OpBuilderCheckRule() { | ||
return makeRule( | ||
cxxMemberCallExpr( | ||
on(expr(hasType( | ||
cxxRecordDecl(isSameOrDerivedFrom("::mlir::OpBuilder")))) | ||
.bind("builder")), | ||
callee(cxxMethodDecl(hasTemplateArgument(0, templateArgument()))), | ||
callee(cxxMethodDecl(hasName("create")))) | ||
.bind("call"), | ||
changeTo(cat(typeAsWritten("call"), "::create(", node("builder"), ", ", | ||
callArgs("call"), ")")), | ||
cat("Use OpType::create(builder, ...) instead of " | ||
"builder.create<OpType>(...)")); | ||
} | ||
} // namespace | ||
OpBuilderCheck::OpBuilderCheck(StringRef Name, ClangTidyContext *Context) | ||
: TransformerClangTidyCheck(OpBuilderCheckRule(), Name, Context) {} | ||
} // namespace clang::tidy::mlir_check |
21 changes: 21 additions & 0 deletionsclang-tools-extra/clang-tidy/mlir/OpBuilderCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MLIR_OPBUILDERCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MLIR_OPBUILDERCHECK_H | ||
#include "../utils/TransformerClangTidyCheck.h" | ||
namespace clang::tidy::mlir_check { | ||
/// Checks for uses of `OpBuilder::create<T>` and suggests using `T::create` | ||
/// instead. | ||
class OpBuilderCheck : public utils::TransformerClangTidyCheck { | ||
public: | ||
OpBuilderCheck(StringRef Name, ClangTidyContext *Context); | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return getLangOpts().CPlusPlus; | ||
} | ||
}; | ||
} // namespace clang::tidy::mlir_check | ||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MLIR_OPBUILDERCHECK_H |
5 changes: 5 additions & 0 deletionsclang-tools-extra/docs/ReleaseNotes.rst
jpienaar marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -96,6 +96,11 @@ Improvements to clang-tidy | ||
New checks | ||
^^^^^^^^^^ | ||
- New :doc:`mlir-op-builder | ||
<clang-tidy/checks/mlir/op-builder>` check. | ||
Flags usage of old OpBuilder format. | ||
jpienaar marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
New check aliases | ||
^^^^^^^^^^^^^^^^^ | ||
2 changes: 2 additions & 0 deletionsclang-tools-extra/docs/clang-tidy/checks/list.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletionsclang-tools-extra/docs/clang-tidy/checks/mlir/op-builder.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.. title:: clang-tidy - mlir-op-builder | ||
mlir-op-builder | ||
=============== | ||
Flags usage of old form of invoking create on `OpBuilder` and suggesting new | ||
form. | ||
Example | ||
------- | ||
.. code-block:: c++ | ||
builder.create<FooOp>(builder.getUnknownLoc(), "baz"); | ||
Transforms to: | ||
.. code-block:: c++ | ||
FooOp::create(builder, builder.getUnknownLoc(), "baz"); | ||
51 changes: 51 additions & 0 deletionsclang-tools-extra/test/clang-tidy/checkers/mlir/op_builder.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// RUN: %check_clang_tidy --match-partial-fixes %s mlir-op-builder %t | ||
namespace mlir { | ||
class Location {}; | ||
class OpBuilder { | ||
public: | ||
template <typename OpTy, typename... Args> | ||
OpTy create(Location location, Args &&...args) { | ||
return OpTy(args...); | ||
} | ||
Location getUnknownLoc() { return Location(); } | ||
}; | ||
class ImplicitLocOpBuilder : public OpBuilder { | ||
public: | ||
template <typename OpTy, typename... Args> | ||
OpTy create(Args &&...args) { | ||
return OpTy(args...); | ||
} | ||
}; | ||
struct ModuleOp { | ||
ModuleOp() {} | ||
static ModuleOp create(OpBuilder &builder, Location location) { | ||
return ModuleOp(); | ||
} | ||
}; | ||
struct NamedOp { | ||
NamedOp(const char* name) {} | ||
static NamedOp create(OpBuilder &builder, Location location, const char* name) { | ||
return NamedOp(name); | ||
} | ||
}; | ||
} // namespace mlir | ||
void f() { | ||
mlir::OpBuilder builder; | ||
// CHECK-MESSAGES: :[[@LINE+2]]:3: warning: Use OpType::create(builder, ...) instead of builder.create<OpType>(...) [mlir-op-builder] | ||
// CHECK-FIXES: mlir:: ModuleOp::create(builder, builder.getUnknownLoc()) | ||
builder.create<mlir:: ModuleOp>(builder.getUnknownLoc()); | ||
using mlir::NamedOp; | ||
// CHECK-MESSAGES: :[[@LINE+2]]:3: warning: Use OpType::create(builder, ...) instead of builder.create<OpType>(...) [mlir-op-builder] | ||
// CHECK-FIXES: NamedOp::create(builder, builder.getUnknownLoc(), "baz") | ||
builder.create<NamedOp>(builder.getUnknownLoc(), "baz"); | ||
mlir::ImplicitLocOpBuilder ib; | ||
// Note: extra space in the case where there is no other arguments. Could be | ||
// improved, but also clang-format will do that just post. | ||
// CHECK-MESSAGES: :[[@LINE+2]]:3: warning: Use OpType::create(builder, ...) instead of builder.create<OpType>(...) [mlir-op-builder] | ||
// CHECK-FIXES: mlir::ModuleOp::create(ib ) | ||
ib.create<mlir::ModuleOp>(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.