Skip to content

Commit

Permalink
[clang-query] add basic profiling on matching each ASTs (llvm#114806)
Browse files Browse the repository at this point in the history
We've found that basic profiling could help improving/optimizing when
developing clang-tidy checks.

This PR adds an extra command
```
set enable-profile (true|false)   Set whether to enable matcher profiling.
```
which enables profiling queries on each file.

Sample output:

```
$ cat test.cql
set enable-profile true
m binaryOperator(isExpansionInMainFile())

$ cat test.c
int test(int i, int j) {
  return i + j;
}

$ clang-query --track-memory -f test.cql test.c --

Match llvm#1:

{{.*}}/test.c:2:10: note: "root" binds here
    2 |   return i + j;
      |          ^~~~~
1 match.
===-------------------------------------------------------------------------===
                         clang-query matcher profiling
===-------------------------------------------------------------------------===
  Total Execution Time: 0.0000 seconds (0.0000 wall clock)

   ---User Time---   --System Time--   --User+System--   ---Wall Time---  ---Mem---  --- Name ---
   0.0000 (100.0%)   0.0000 (100.0%)   0.0000 (100.0%)   0.0000 (100.0%)        224  {{.*}}/test.c
   0.0000 (100.0%)   0.0000 (100.0%)   0.0000 (100.0%)   0.0000 (100.0%)        224  Total
```
  • Loading branch information
danix800 authored and PhilippRados committed Nov 6, 2024
1 parent 132be5b commit 1ac8a68
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
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

0 comments on commit 1ac8a68

Please sign in to comment.