Skip to content

Commit f5c4f27

Browse files
authored
[analyzer] Add -ftime-trace scopes for region-store bindings and removeDead (#125884)
From investigation of a few slow analysis cases, I discovered that `RegionStoreManager::bind*` and `ExprEngine::removeDead` are often the slowest actions. This change adds explicit scope to the time trace generated by `-ftime-trace` to enable easy diagnostics of the cases when these functions are the slowdown culprits. -- CPP-6109
1 parent 3041dd5 commit f5c4f27

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

clang/lib/StaticAnalyzer/Core/ExprEngine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "llvm/Support/ErrorHandling.h"
7575
#include "llvm/Support/GraphWriter.h"
7676
#include "llvm/Support/SaveAndRestore.h"
77+
#include "llvm/Support/TimeProfiler.h"
7778
#include "llvm/Support/raw_ostream.h"
7879
#include <cassert>
7980
#include <cstdint>
@@ -1031,6 +1032,7 @@ void ExprEngine::removeDead(ExplodedNode *Pred, ExplodedNodeSet &Out,
10311032
const LocationContext *LC,
10321033
const Stmt *DiagnosticStmt,
10331034
ProgramPoint::Kind K) {
1035+
llvm::TimeTraceScope TimeScope("ExprEngine::removeDead");
10341036
assert((K == ProgramPoint::PreStmtPurgeDeadSymbolsKind ||
10351037
ReferenceStmt == nullptr || isa<ReturnStmt>(ReferenceStmt))
10361038
&& "PostStmt is not generally supported by the SymbolReaper yet");

clang/lib/StaticAnalyzer/Core/RegionStore.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
3030
#include "llvm/ADT/ImmutableMap.h"
3131
#include "llvm/ADT/STLExtras.h"
32+
#include "llvm/Support/TimeProfiler.h"
3233
#include "llvm/Support/raw_ostream.h"
3334
#include <optional>
3435
#include <utility>
@@ -112,6 +113,13 @@ class BindingKey {
112113

113114
LLVM_DUMP_METHOD void dump() const;
114115
};
116+
117+
std::string locDescr(Loc L) {
118+
std::string S;
119+
llvm::raw_string_ostream OS(S);
120+
L.dumpToStream(OS);
121+
return OS.str();
122+
}
115123
} // end anonymous namespace
116124

117125
BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
@@ -2408,6 +2416,8 @@ StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
24082416

24092417
RegionBindingsRef
24102418
RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
2419+
llvm::TimeTraceScope TimeScope("RegionStoreManager::bind",
2420+
[&L]() { return locDescr(L); });
24112421
// We only care about region locations.
24122422
auto MemRegVal = L.getAs<loc::MemRegionVal>();
24132423
if (!MemRegVal)
@@ -2514,6 +2524,8 @@ RegionBindingsRef
25142524
RegionStoreManager::bindArray(RegionBindingsConstRef B,
25152525
const TypedValueRegion* R,
25162526
SVal Init) {
2527+
llvm::TimeTraceScope TimeScope("RegionStoreManager::bindArray",
2528+
[R]() { return R->getDescriptiveName(); });
25172529

25182530
const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
25192531
QualType ElementTy = AT->getElementType();
@@ -2578,6 +2590,8 @@ RegionStoreManager::bindArray(RegionBindingsConstRef B,
25782590
RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B,
25792591
const TypedValueRegion* R,
25802592
SVal V) {
2593+
llvm::TimeTraceScope TimeScope("RegionStoreManager::bindVector",
2594+
[R]() { return R->getDescriptiveName(); });
25812595
QualType T = R->getValueType();
25822596
const VectorType *VT = T->castAs<VectorType>(); // Use castAs for typedefs.
25832597

@@ -2700,6 +2714,8 @@ std::optional<RegionBindingsRef> RegionStoreManager::tryBindSmallStruct(
27002714
RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B,
27012715
const TypedValueRegion *R,
27022716
SVal V) {
2717+
llvm::TimeTraceScope TimeScope("RegionStoreManager::bindStruct",
2718+
[R]() { return R->getDescriptiveName(); });
27032719
QualType T = R->getValueType();
27042720
assert(T->isStructureOrClassType());
27052721

@@ -2818,6 +2834,8 @@ RegionBindingsRef
28182834
RegionStoreManager::bindAggregate(RegionBindingsConstRef B,
28192835
const TypedRegion *R,
28202836
SVal Val) {
2837+
llvm::TimeTraceScope TimeScope("RegionStoreManager::bindAggregate",
2838+
[R]() { return R->getDescriptiveName(); });
28212839
// Remove the old bindings, using 'R' as the root of all regions
28222840
// we will invalidate. Then add the new binding.
28232841
return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core %s -ftime-trace=%t.raw.json -ftime-trace-granularity=0 -verify
2+
// RUN: %python -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4))' < %t.raw.json > %t.formatted.json
3+
// RUN: FileCheck --input-file=%t.formatted.json --check-prefix=CHECK %s
4+
5+
// CHECK: "name": "RegionStoreManager::bindArray",
6+
// CHECK-NEXT: "args": {
7+
//
8+
// The below does not necessarily follow immediately,
9+
// depending on what parts of the array are initialized first.
10+
//
11+
// CHECK: "detail": "'arr[0][1]'"
12+
// CHECK-NEXT: }
13+
//
14+
// CHECK: "detail": "'arr[0]'"
15+
// CHECK-NEXT: }
16+
//
17+
// CHECK: "detail": "'arr'"
18+
// CHECK-NEXT: }
19+
20+
int f() {
21+
int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
22+
return arr[1][0][1];
23+
}
24+
// expected-no-diagnostics
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core %s -ftime-trace=%t.raw.json -ftime-trace-granularity=0 -verify
2+
// RUN: %python -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4))' < %t.raw.json > %t.formatted.json
3+
// RUN: FileCheck --input-file=%t.formatted.json --check-prefix=CHECK %s
4+
5+
// The trace file is rather large, but it should contain at least one scope for removeDead:
6+
//
7+
// CHECK: "name": "ExprEngine::removeDead"
8+
9+
bool coin();
10+
int f() {
11+
int x = 0;
12+
int y = 0;
13+
while (coin()) {
14+
x = 1;
15+
}
16+
return x / y; // expected-warning{{Division by zero}}
17+
}

0 commit comments

Comments
 (0)