Skip to content

[analyzer] Add time-trace scopes for high-level analyzer steps #125508

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

Merged
merged 17 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions clang/docs/analyzer/developer-docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Contents:
developer-docs/InitializerLists
developer-docs/nullability
developer-docs/RegionStore
developer-docs/PerformanceInvestigation
38 changes: 38 additions & 0 deletions clang/docs/analyzer/developer-docs/PerformanceInvestigation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=========================
Performance Investigation
=========================

Multiple factors contribute to the time it takes to analyze a file with Clang Static Analyzer.
A translation unit contains multiple entry points, each of which take multiple steps to analyze.

You can add the ``-ftime-trace=file.json`` option to break down the analysis time into individual entry points and steps within each entry point.
You can explore the generated JSON file in a Chromium browser using the ``chrome://tracing`` URL,
or using `speedscope <https://speedscope.app>`_.
Once you narrow down to specific analysis steps you are interested in, you can more effectively employ heavier profilers,
such as `Perf <https://perfwiki.github.io/main/>`_ and `Callgrind <https://valgrind.org/docs/manual/cl-manual.html>`_.

Each analysis step has a time scope in the trace, corresponds to processing of an exploded node, and is designated with a ``ProgramPoint``.
If the ``ProgramPoint`` is associated with a location, you can see it on the scope metadata label.

.. image:: ../images/speedscope.png

On the speedscope screenshot above, under the first time ruler is the birds-eye view of the entire trace that spans a little over 8 seconds.
Under the second ruler (focused on the 4.27s time point) you can see a narrowed-down portion.
The second box (of light-green color) that spans entire screen (and actually extends beyond it) corresponds to the analysis of ``get_global_options()`` entry point that is defined in the "options.cc" file on line 303646
(the line number is huge because the file was preprocessed).
Below it, you can find multiple sub-scopes each corresponding to processing of a single exploded node.

1. A ``PostStmt`` for some statement on line 2337. This step spent most of the time in the ``ExprEngine::removeDead`` call.
2. (Selected): another ``PostStmt`` for a return statement
3. A ``BlockEdge``, and so on...

In addition to the ``-ftime-trace`` option, you can use ``-ftime-trace-granularity`` to fine-tune the time trace.

- ``-ftime-trace-granularity=NN`` dumps only time scopes that are longer than NN microseconds.
- ``-ftime-trace-verbose`` enables some additional dumps in the frontend related to template instantiations.
At the moment, it has no effect on the traces from the static analyzer.

Note: Both Chrome-tracing and speedscope tools might struggle with time traces above 100 MB in size.
Luckily, in most cases the default max-steps boundary of 225 000 produces the traces of approximately that size
for a single entry point.
You can use ``-analyze-function=get_global_options`` together with ``-ftime-trace`` to narrow down analysis to a specific entry point.
Binary file added clang/docs/analyzer/images/speedscope.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions clang/include/clang/Analysis/ProgramPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class ProgramPoint {
LoopExitKind,
EpsilonKind};

static StringRef getProgramPointKindName(Kind K);
std::optional<SourceLocation> getSourceLocation() const;

private:
const void *Data1;
llvm::PointerIntPair<const void *, 2, unsigned> Data2;
Expand Down
115 changes: 115 additions & 0 deletions clang/lib/Analysis/ProgramPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,121 @@ LLVM_DUMP_METHOD void ProgramPoint::dump() const {
return printJson(llvm::errs());
}

StringRef ProgramPoint::getProgramPointKindName(Kind K) {
switch (K) {
case BlockEdgeKind:
return "BlockEdge";
case BlockEntranceKind:
return "BlockEntrance";
case BlockExitKind:
return "BlockExit";
case PreStmtKind:
return "PreStmt";
case PreStmtPurgeDeadSymbolsKind:
return "PreStmtPurgeDeadSymbols";
case PostStmtPurgeDeadSymbolsKind:
return "PostStmtPurgeDeadSymbols";
case PostStmtKind:
return "PostStmt";
case PreLoadKind:
return "PreLoad";
case PostLoadKind:
return "PostLoad";
case PreStoreKind:
return "PreStore";
case PostStoreKind:
return "PostStore";
case PostConditionKind:
return "PostCondition";
case PostLValueKind:
return "PostLValue";
case PostAllocatorCallKind:
return "PostAllocatorCall";
case PostInitializerKind:
return "PostInitializer";
case CallEnterKind:
return "CallEnter";
case CallExitBeginKind:
return "CallExitBegin";
case CallExitEndKind:
return "CallExitEnd";
case FunctionExitKind:
return "FunctionExit";
case PreImplicitCallKind:
return "PreImplicitCall";
case PostImplicitCallKind:
return "PostImplicitCall";
case LoopExitKind:
return "LoopExit";
case EpsilonKind:
return "Epsilon";
}
llvm_unreachable("Unknown ProgramPoint kind");
}

std::optional<SourceLocation> ProgramPoint::getSourceLocation() const {
switch (getKind()) {
case BlockEdgeKind:
// If needed, the source and or destination beginning can be used to get
// source location.
return std::nullopt;
case BlockEntranceKind:
// If needed, first statement of the block can be used.
return std::nullopt;
case BlockExitKind:
if (const auto *B = castAs<BlockExit>().getBlock()) {
if (const auto *T = B->getTerminatorStmt()) {
return T->getBeginLoc();
}
}
return std::nullopt;
case PreStmtKind:
case PreStmtPurgeDeadSymbolsKind:
case PostStmtPurgeDeadSymbolsKind:
case PostStmtKind:
case PreLoadKind:
case PostLoadKind:
case PreStoreKind:
case PostStoreKind:
case PostConditionKind:
case PostLValueKind:
case PostAllocatorCallKind:
if (const Stmt *S = castAs<StmtPoint>().getStmt())
return S->getBeginLoc();
return std::nullopt;
case PostInitializerKind:
if (const auto *Init = castAs<PostInitializer>().getInitializer())
return Init->getSourceLocation();
return std::nullopt;
case CallEnterKind:
if (const Stmt *S = castAs<CallEnter>().getCallExpr())
return S->getBeginLoc();
return std::nullopt;
case CallExitBeginKind:
if (const Stmt *S = castAs<CallExitBegin>().getReturnStmt())
return S->getBeginLoc();
return std::nullopt;
case CallExitEndKind:
return std::nullopt;
case FunctionExitKind:
if (const auto *B = castAs<FunctionExitPoint>().getBlock();
B && B->getTerminatorStmt())
return B->getTerminatorStmt()->getBeginLoc();
return std::nullopt;
case PreImplicitCallKind:
return castAs<ImplicitCallPoint>().getLocation();
case PostImplicitCallKind:
return castAs<ImplicitCallPoint>().getLocation();
case LoopExitKind:
if (const Stmt *S = castAs<LoopExit>().getLoopStmt())
return S->getBeginLoc();
return std::nullopt;
case EpsilonKind:
return std::nullopt;
}
llvm_unreachable("Unknown ProgramPoint kind");
}

void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
const ASTContext &Context =
getLocationContext()->getAnalysisDeclContext()->getASTContext();
Expand Down
36 changes: 34 additions & 2 deletions clang/lib/StaticAnalyzer/Core/BugReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -287,6 +288,34 @@ class PathDiagnosticBuilder : public BugReporterContext {
const PathSensitiveBugReport *getBugReport() const { return R; }
};

std::string timeTraceName(const BugReportEquivClass &EQ) {
if (!llvm::timeTraceProfilerEnabled())
return "";
const auto &BugReports = EQ.getReports();
if (BugReports.empty())
return "Empty Equivalence Class";
const BugReport *R = BugReports.front().get();
const auto &BT = R->getBugType();
return ("Flushing EQC " + BT.getDescription()).str();
}

llvm::TimeTraceMetadata timeTraceMetadata(const BugReportEquivClass &EQ) {
// Must be called only when constructing non-bogus TimeTraceScope
assert(llvm::timeTraceProfilerEnabled());

const auto &BugReports = EQ.getReports();
if (BugReports.empty())
return {};
const BugReport *R = BugReports.front().get();
const auto &BT = R->getBugType();
auto Loc = R->getLocation().asLocation();
std::string File = "";
if (const auto *Entry = Loc.getFileEntry())
File = Entry->tryGetRealPathName().str();
return {BT.getCheckerName().str(), std::move(File),
static_cast<int>(Loc.getLineNumber())};
}

} // namespace

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2892,6 +2921,7 @@ std::optional<PathDiagnosticBuilder> PathDiagnosticBuilder::findValidReport(

if (R->isValid()) {
if (Reporter.getAnalyzerOptions().ShouldCrosscheckWithZ3) {
llvm::TimeTraceScope TCS{"Crosscheck with Z3"};
// If crosscheck is enabled, remove all visitors, add the refutation
// visitor and check again
R->clearVisitors();
Expand Down Expand Up @@ -3119,8 +3149,10 @@ BugReport *PathSensitiveBugReporter::findReportInEquivalenceClass(
return exampleReport;
}

void BugReporter::FlushReport(BugReportEquivClass& EQ) {
SmallVector<BugReport*, 10> bugReports;
void BugReporter::FlushReport(BugReportEquivClass &EQ) {
llvm::TimeTraceScope TCS{timeTraceName(EQ),
[&EQ]() { return timeTraceMetadata(EQ); }};
SmallVector<BugReport *, 10> bugReports;
BugReport *report = findReportInEquivalenceClass(EQ, bugReports);
if (!report)
return;
Expand Down
28 changes: 28 additions & 0 deletions clang/lib/StaticAnalyzer/Core/BugSuppression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "clang/StaticAnalyzer/Core/BugReporter/BugSuppression.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/TimeProfiler.h"

using namespace clang;
using namespace ento;
Expand Down Expand Up @@ -119,6 +121,29 @@ class CacheInitializer : public DynamicRecursiveASTVisitor {
Ranges &Result;
};

std::string timeScopeName(const Decl *DeclWithIssue) {
if (!llvm::timeTraceProfilerEnabled())
return "";
return llvm::formatv(
"BugSuppression::isSuppressed init suppressions cache for {0}",
DeclWithIssue->getDeclKindName())
.str();
}

llvm::TimeTraceMetadata getDeclTimeTraceMetadata(const Decl *DeclWithIssue) {
assert(DeclWithIssue);
assert(llvm::timeTraceProfilerEnabled());
std::string Name = "<noname>";
if (const auto *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
Name = ND->getNameAsString();
}
const auto &SM = DeclWithIssue->getASTContext().getSourceManager();
auto Line = SM.getPresumedLineNumber(DeclWithIssue->getBeginLoc());
auto Fname = SM.getFilename(DeclWithIssue->getBeginLoc());
return llvm::TimeTraceMetadata{std::move(Name), Fname.str(),
static_cast<int>(Line)};
}

} // end anonymous namespace

// TODO: Introduce stable IDs for checkers and check for those here
Expand Down Expand Up @@ -177,6 +202,9 @@ bool BugSuppression::isSuppressed(const PathDiagnosticLocation &Location,
std::make_pair(DeclWithIssue, CachedRanges{}));
Ranges &SuppressionRanges = InsertionResult.first->second;
if (InsertionResult.second) {
llvm::TimeTraceScope TimeScope(
timeScopeName(DeclWithIssue),
[DeclWithIssue]() { return getDeclTimeTraceMetadata(DeclWithIssue); });
// We haven't checked this declaration for suppressions yet!
CacheInitializer::initialize(DeclWithIssue, SuppressionRanges);
}
Expand Down
Loading