Skip to content
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

[clang-query] add basic profiling on matching each ASTs #114806

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions clang-tools-extra/clang-query/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
" set bind-root (true|false) "
"Set whether to bind the root matcher to \"root\".\n"
" set print-matcher (true|false) "
"Set whether to print the current matcher,\n"
"Set whether to print the current matcher.\n"
" set enable-profile (true|false) "
"Set whether to enable matcher profiling.\n"
" set traversal <kind> "
"Set traversal kind of clang-query session. Available kinds are:\n"
" AsIs "
Expand Down Expand Up @@ -82,27 +84,53 @@ namespace {

struct CollectBoundNodes : MatchFinder::MatchCallback {
std::vector<BoundNodes> &Bindings;
CollectBoundNodes(std::vector<BoundNodes> &Bindings) : Bindings(Bindings) {}
StringRef Unit;
CollectBoundNodes(std::vector<BoundNodes> &Bindings, StringRef Unit)
: Bindings(Bindings), Unit(Unit) {}
void run(const MatchFinder::MatchResult &Result) override {
Bindings.push_back(Result.Nodes);
}
StringRef getID() const override { return Unit; }
};

struct QueryProfiler {
llvm::StringMap<llvm::TimeRecord> Records;

~QueryProfiler() {
llvm::TimerGroup TG("clang-query", "clang-query matcher profiling",
Records);
TG.print(llvm::errs());
llvm::errs().flush();
}
};

} // namespace

bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
unsigned MatchCount = 0;

std::optional<QueryProfiler> Profiler;
if (QS.EnableProfile)
Profiler.emplace();

for (auto &AST : QS.ASTs) {
MatchFinder Finder;
ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
std::optional<llvm::StringMap<llvm::TimeRecord>> Records;
if (QS.EnableProfile) {
Records.emplace();
FinderOptions.CheckProfiling.emplace(*Records);
}

MatchFinder Finder(FinderOptions);
std::vector<BoundNodes> Matches;
DynTypedMatcher MaybeBoundMatcher = Matcher;
if (QS.BindRoot) {
std::optional<DynTypedMatcher> M = Matcher.tryBind("root");
if (M)
MaybeBoundMatcher = *M;
}
CollectBoundNodes Collect(Matches);
StringRef OrigSrcName = AST->getOriginalSourceFileName();
CollectBoundNodes Collect(Matches, OrigSrcName);
if (!Finder.addDynamicMatcher(MaybeBoundMatcher, &Collect)) {
OS << "Not a valid top-level matcher.\n";
return false;
Expand All @@ -111,6 +139,8 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
ASTContext &Ctx = AST->getASTContext();
Ctx.getParentMapContext().setTraversalKind(QS.TK);
Finder.matchAST(Ctx);
if (QS.EnableProfile)
Profiler->Records[OrigSrcName] += (*Records)[OrigSrcName];

if (QS.PrintMatcher) {
SmallVector<StringRef, 4> Lines;
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clang-query/QueryParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ enum ParsedQueryVariable {
PQV_Output,
PQV_BindRoot,
PQV_PrintMatcher,
PQV_EnableProfile,
PQV_Traversal
};

Expand Down Expand Up @@ -285,6 +286,7 @@ QueryRef QueryParser::doParse() {
.Case("output", PQV_Output)
.Case("bind-root", PQV_BindRoot)
.Case("print-matcher", PQV_PrintMatcher)
.Case("enable-profile", PQV_EnableProfile)
.Case("traversal", PQV_Traversal)
.Default(PQV_Invalid);
if (VarStr.empty())
Expand All @@ -303,6 +305,9 @@ QueryRef QueryParser::doParse() {
case PQV_PrintMatcher:
Q = parseSetBool(&QuerySession::PrintMatcher);
break;
case PQV_EnableProfile:
Q = parseSetBool(&QuerySession::EnableProfile);
break;
case PQV_Traversal:
Q = parseSetTraversalKind(&QuerySession::TK);
break;
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clang-query/QuerySession.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class QuerySession {
QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs)
: ASTs(ASTs), PrintOutput(false), DiagOutput(true),
DetailedASTOutput(false), BindRoot(true), PrintMatcher(false),
Terminate(false), TK(TK_AsIs) {}
EnableProfile(false), Terminate(false), TK(TK_AsIs) {}

llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs;

Expand All @@ -36,6 +36,7 @@ class QuerySession {

bool BindRoot;
bool PrintMatcher;
bool EnableProfile;
bool Terminate;

TraversalKind TK;
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Improvements to clang-doc
Improvements to clang-query
---------------------------

The improvements are...
- Added `set enable-profile true/false` command for basic matcher profiling.

Improvements to clang-tidy
--------------------------
Expand Down
Loading