https://github.com/fmeum created
https://github.com/llvm/llvm-project/pull/244424
ASTReader::PrintStats computed the number of read types and declarations as the
number of all entries of TypesLoaded and DeclsLoaded minus the null entries of
their materialized pages. Since these became PagedVectors, the entries of pages
that were never materialized were counted as read, so -print-stats reported
almost all declarations of a PCH or module as read, even for an empty
translation unit.
Count the non-null entries of the materialized pages instead.
I ran into this while profiling explicit header module builds, where
`-print-stats` claimed that every translation unit read all declarations of
every imported module. The new test fails without the fix ("2000/2000
declarations read").
Assisted-by: Claude
>From cd3626556cf45848ed8dda8a2fcc869ab4cb24a1 Mon Sep 17 00:00:00 2001
From: Fabian Meumertzheim <[email protected]>
Date: Thu, 19 Nov 2026 23:40:50 +0100
Subject: [PATCH] [clang][Serialization] Don't count unread declarations in
-print-stats
ASTReader::PrintStats computed the number of read types and
declarations as the number of all entries of TypesLoaded and
DeclsLoaded minus the null entries of their materialized pages. Since
these became PagedVectors, the entries of pages that were never
materialized were counted as read, so -print-stats reported almost all
declarations of a PCH or module as read, even for an empty translation
unit.
Count the non-null entries of the materialized pages instead.
Assisted-by: Claude
---
clang/lib/Serialization/ASTReader.cpp | 11 +++++-----
clang/test/PCH/print-stats.c | 30 +++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 5 deletions(-)
create mode 100644 clang/test/PCH/print-stats.c
diff --git a/clang/lib/Serialization/ASTReader.cpp
b/clang/lib/Serialization/ASTReader.cpp
index db04c06f3cee73..5f0ea28a60da5c 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -9225,11 +9225,12 @@ void ASTReader::StartTranslationUnit(ASTConsumer
*Consumer) {
void ASTReader::PrintStats() {
std::fprintf(stderr, "*** AST File Statistics:\n");
- unsigned NumTypesLoaded =
- TypesLoaded.size() - llvm::count(TypesLoaded.materialized(), QualType());
- unsigned NumDeclsLoaded =
- DeclsLoaded.size() -
- llvm::count(DeclsLoaded.materialized(), (Decl *)nullptr);
+ // Only count the elements of materialized pages, the others haven't been
+ // read yet.
+ unsigned NumTypesLoaded = llvm::count_if(
+ TypesLoaded.materialized(), [](QualType T) { return !T.isNull(); });
+ unsigned NumDeclsLoaded = llvm::count_if(
+ DeclsLoaded.materialized(), [](Decl *D) { return D != nullptr; });
unsigned NumIdentifiersLoaded =
IdentifiersLoaded.size() -
llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
diff --git a/clang/test/PCH/print-stats.c b/clang/test/PCH/print-stats.c
new file mode 100644
index 00000000000000..418386e22316aa
--- /dev/null
+++ b/clang/test/PCH/print-stats.c
@@ -0,0 +1,30 @@
+// Declarations and types of a PCH that aren't used must not be counted as
+// read, even if they are in pages of the vectors that haven't been
+// materialized.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -include-pch %t \
+// RUN: -fsyntax-only -print-stats %s 2>&1 | FileCheck %s
+
+// CHECK: *** AST File Statistics:
+// CHECK: {{^ [0-9]{1,2}/[0-9]{4} declarations read}}
+
+#ifndef HEADER
+#define HEADER
+
+#define D1(x) void f##x(void);
+#define D10(x) D1(x##0) D1(x##1) D1(x##2) D1(x##3) D1(x##4) \
+ D1(x##5) D1(x##6) D1(x##7) D1(x##8) D1(x##9)
+#define D100(x) D10(x##0) D10(x##1) D10(x##2) D10(x##3) D10(x##4) \
+ D10(x##5) D10(x##6) D10(x##7) D10(x##8) D10(x##9)
+#define D1000(x) D100(x##0) D100(x##1) D100(x##2) D100(x##3) D100(x##4) \
+ D100(x##5) D100(x##6) D100(x##7) D100(x##8) D100(x##9)
+
+D1000(1)
+D1000(2)
+
+#else
+
+void use(void);
+
+#endif
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits