Skip to content

Commit e2db10a

Browse files
Merge pull request #9450 from adrian-prantl/138144624-rebranch
More aggressively deduplicate global warnings based on contents. (#1
2 parents 2b4412e + ae03401 commit e2db10a

File tree

10 files changed

+277
-134
lines changed

10 files changed

+277
-134
lines changed

lldb/include/lldb/Core/Module.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "llvm/ADT/DenseSet.h"
3232
#include "llvm/ADT/STLFunctionalExtras.h"
33+
#include "llvm/ADT/StableHashing.h"
3334
#include "llvm/ADT/StringRef.h"
3435
#include <optional>
3536
#include "llvm/Support/Chrono.h"
@@ -1076,11 +1077,14 @@ class Module : public std::enable_shared_from_this<Module>,
10761077
/// time for the symbol tables can be aggregated here.
10771078
StatsDuration m_symtab_index_time;
10781079

1079-
std::once_flag m_optimization_warning;
1080-
std::once_flag m_language_warning;
10811080
#ifdef LLDB_ENABLE_SWIFT
10821081
std::once_flag m_toolchain_mismatch_warning;
10831082
#endif
1083+
/// A set of hashes of all warnings and errors, to avoid reporting them
1084+
/// multiple times to the same Debugger.
1085+
llvm::DenseMap<llvm::stable_hash, std::unique_ptr<std::once_flag>>
1086+
m_shown_diagnostics;
1087+
std::recursive_mutex m_diagnostic_mutex;
10841088

10851089
void SymbolIndicesToSymbolContextList(Symtab *symtab,
10861090
std::vector<uint32_t> &symbol_indexes,
@@ -1112,6 +1116,7 @@ class Module : public std::enable_shared_from_this<Module>,
11121116
LazyBool m_is_swift_cxx_interop_enabled = eLazyBoolCalculate;
11131117
LazyBool m_is_embedded_swift = eLazyBoolCalculate;
11141118
#endif
1119+
std::once_flag *GetDiagnosticOnceFlag(llvm::StringRef msg);
11151120
};
11161121

11171122
} // namespace lldb_private

lldb/source/Core/Module.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,8 @@ void Module::ReportWarningOptimization(
11271127
ss << file_name
11281128
<< " was compiled with optimization - stepping may behave "
11291129
"oddly; variables may not be available.";
1130-
Debugger::ReportWarning(std::string(ss.GetString()), debugger_id,
1131-
&m_optimization_warning);
1130+
llvm::StringRef msg = ss.GetString();
1131+
Debugger::ReportWarning(msg.str(), debugger_id, GetDiagnosticOnceFlag(msg));
11321132
}
11331133

11341134
void Module::ReportWarningUnsupportedLanguage(
@@ -1138,8 +1138,8 @@ void Module::ReportWarningUnsupportedLanguage(
11381138
<< Language::GetNameForLanguageType(language)
11391139
<< "\". "
11401140
"Inspection of frame variables will be limited.";
1141-
Debugger::ReportWarning(std::string(ss.GetString()), debugger_id,
1142-
&m_language_warning);
1141+
llvm::StringRef msg = ss.GetString();
1142+
Debugger::ReportWarning(msg.str(), debugger_id, GetDiagnosticOnceFlag(msg));
11431143
}
11441144

11451145
#ifdef LLDB_ENABLE_SWIFT
@@ -1257,20 +1257,29 @@ void Module::ReportErrorIfModifyDetected(
12571257
}
12581258
}
12591259

1260+
std::once_flag *Module::GetDiagnosticOnceFlag(llvm::StringRef msg) {
1261+
std::lock_guard<std::recursive_mutex> guard(m_diagnostic_mutex);
1262+
auto &once_ptr = m_shown_diagnostics[llvm::stable_hash_name(msg)];
1263+
if (!once_ptr)
1264+
once_ptr = std::make_unique<std::once_flag>();
1265+
return once_ptr.get();
1266+
}
1267+
12601268
void Module::ReportError(const llvm::formatv_object_base &payload) {
12611269
StreamString strm;
12621270
GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelBrief);
1263-
strm.PutChar(' ');
1264-
strm.PutCString(payload.str());
1265-
Debugger::ReportError(strm.GetString().str());
1271+
std::string msg = payload.str();
1272+
strm << ' ' << msg;
1273+
Debugger::ReportError(strm.GetString().str(), {}, GetDiagnosticOnceFlag(msg));
12661274
}
12671275

12681276
void Module::ReportWarning(const llvm::formatv_object_base &payload) {
12691277
StreamString strm;
12701278
GetDescription(strm.AsRawOstream(), lldb::eDescriptionLevelFull);
1271-
strm.PutChar(' ');
1272-
strm.PutCString(payload.str());
1273-
Debugger::ReportWarning(std::string(strm.GetString()));
1279+
std::string msg = payload.str();
1280+
strm << ' ' << msg;
1281+
Debugger::ReportWarning(strm.GetString().str(), {},
1282+
GetDiagnosticOnceFlag(msg));
12741283
}
12751284

12761285
void Module::LogMessage(Log *log, const llvm::formatv_object_base &payload) {

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,13 +2088,15 @@ void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
20882088
Status error = ModuleList::GetSharedModule(dwo_module_spec, module_sp,
20892089
nullptr, nullptr, nullptr);
20902090
if (!module_sp) {
2091+
// ReportWarning also rate-limits based on the warning string,
2092+
// but in a -gmodules build, each object file has a similar DAG
2093+
// of module dependencies that would all be listed here.
20912094
GetObjectFile()->GetModule()->ReportWarning(
2092-
"{0:x16}: unable to locate module needed for external types: "
2093-
"{1}\nerror: {2}\nDebugging will be degraded due to missing "
2094-
"types. Rebuilding the project will regenerate the needed "
2095-
"module files.",
2096-
die.GetOffset(), dwo_module_spec.GetFileSpec().GetPath().c_str(),
2097-
error.AsCString("unknown error"));
2095+
"{0}", error.AsCString("unknown error"));
2096+
GetObjectFile()->GetModule()->ReportWarning(
2097+
"Unable to locate module needed for external types.\n"
2098+
"Debugging will be degraded due to missing types. Rebuilding the "
2099+
"project will regenerate the needed module files.");
20982100
continue;
20992101
}
21002102

@@ -2114,12 +2116,11 @@ void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
21142116

21152117
if (dwo_id != dwo_dwo_id) {
21162118
GetObjectFile()->GetModule()->ReportWarning(
2117-
"{0:x16}: Module {1} is out-of-date (hash mismatch). Type "
2118-
"information "
2119-
"from this module may be incomplete or inconsistent with the rest of "
2120-
"the program. Rebuilding the project will regenerate the needed "
2121-
"module files.",
2122-
die.GetOffset(), dwo_module_spec.GetFileSpec().GetPath().c_str());
2119+
"Module {0} is out-of-date (hash mismatch).\n"
2120+
"Type information from this module may be incomplete or inconsistent "
2121+
"with the rest of the program. Rebuilding the project will "
2122+
"regenerate the needed module files.",
2123+
dwo_module_spec.GetFileSpec().GetPath());
21232124
}
21242125
}
21252126
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# REQUIRES: system-darwin
2+
# Test the rate-limiting of module not found warnings.
3+
# RUN: rm -rf %t
4+
# RUN: mkdir -p %t
5+
6+
# RUN: echo 'module "C" { header "c.h" }' >%t/module.modulemap
7+
# RUN: echo 'struct c {};' >>%t/c.h
8+
# RUN: echo '@import C;' >%t/a.m
9+
# RUN: echo 'struct a { struct c c; } a;' >>%t/a.m
10+
# RUN: echo '@import C;' >%t/b.m
11+
# RUN: echo 'struct b { struct c c; } b;' >>%t/b.m
12+
# RUN: echo 'int main() {}' >>%t/b.m
13+
14+
# RUN: %clang_host -fmodules -Xclang -fmodules-cache-path=%t/cache -I%t -g -gmodules %t/a.m -o %t/a.o -c
15+
# RUN: %clang_host -fmodules -Xclang -fmodules-cache-path=%t/cache -I%t -g -gmodules %t/b.m -o %t/b.o -c
16+
# RUN: %clang_host %t/a.o %t/b.o -o %t/a.out
17+
# RUN: rm -rf %t/cache
18+
# RUN: %lldb %t/a.out -o "b main" -o run -o "p a" -o "p b" -o q 2>&1 | FileCheck %s
19+
# CHECK: {{[ab]}}.o{{.*}}/cache/{{.*}}/C-{{.*}}.pcm' does not exist
20+
# CHECK-NOT: /cache/{{.*}}/C-{.*}.pcm' does not exist
21+
# CHECK: {{[ab]}}.o{{.*}}/cache/{{.*}}/C-{{.*}}.pcm' does not exist
22+
# CHECK-NOT: /cache/{{.*}}/C-{.*}.pcm' does not exist

llvm/include/llvm/ADT/StableHashing.h

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,103 +8,63 @@
88
//
99
// This file provides types and functions for computing and combining stable
1010
// hashes. Stable hashes can be useful for hashing across different modules,
11-
// processes, or compiler runs.
11+
// processes, machines, or compiler runs for a specific compiler version. It
12+
// currently employs the xxh3_64bits hashing algorithm. Be aware that this
13+
// implementation may be adjusted or updated as improvements to the compiler are
14+
// made.
1215
//
1316
//===----------------------------------------------------------------------===//
1417

1518
#ifndef LLVM_ADT_STABLEHASHING_H
1619
#define LLVM_ADT_STABLEHASHING_H
1720

1821
#include "llvm/ADT/StringRef.h"
22+
#include "llvm/Support/xxhash.h"
1923

2024
namespace llvm {
2125

2226
/// An opaque object representing a stable hash code. It can be serialized,
2327
/// deserialized, and is stable across processes and executions.
2428
using stable_hash = uint64_t;
2529

26-
// Implementation details
27-
namespace hashing {
28-
namespace detail {
29-
30-
// Stable hashes are based on the 64-bit FNV-1 hash:
31-
// https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function
32-
33-
const uint64_t FNV_PRIME_64 = 1099511628211u;
34-
const uint64_t FNV_OFFSET_64 = 14695981039346656037u;
35-
36-
inline void stable_hash_append(stable_hash &Hash, const char Value) {
37-
Hash = Hash ^ (Value & 0xFF);
38-
Hash = Hash * FNV_PRIME_64;
39-
}
40-
41-
inline void stable_hash_append(stable_hash &Hash, stable_hash Value) {
42-
for (unsigned I = 0; I < 8; ++I) {
43-
stable_hash_append(Hash, static_cast<char>(Value));
44-
Value >>= 8;
45-
}
30+
inline stable_hash stable_hash_combine(ArrayRef<stable_hash> Buffer) {
31+
const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(Buffer.data());
32+
size_t Size = Buffer.size() * sizeof(stable_hash);
33+
return xxh3_64bits(ArrayRef<uint8_t>(Ptr, Size));
4634
}
4735

48-
} // namespace detail
49-
} // namespace hashing
50-
5136
inline stable_hash stable_hash_combine(stable_hash A, stable_hash B) {
52-
stable_hash Hash = hashing::detail::FNV_OFFSET_64;
53-
hashing::detail::stable_hash_append(Hash, A);
54-
hashing::detail::stable_hash_append(Hash, B);
55-
return Hash;
37+
stable_hash Hashes[2] = {A, B};
38+
return stable_hash_combine(Hashes);
5639
}
5740

5841
inline stable_hash stable_hash_combine(stable_hash A, stable_hash B,
5942
stable_hash C) {
60-
stable_hash Hash = hashing::detail::FNV_OFFSET_64;
61-
hashing::detail::stable_hash_append(Hash, A);
62-
hashing::detail::stable_hash_append(Hash, B);
63-
hashing::detail::stable_hash_append(Hash, C);
64-
return Hash;
43+
stable_hash Hashes[3] = {A, B, C};
44+
return stable_hash_combine(Hashes);
6545
}
6646

6747
inline stable_hash stable_hash_combine(stable_hash A, stable_hash B,
6848
stable_hash C, stable_hash D) {
69-
stable_hash Hash = hashing::detail::FNV_OFFSET_64;
70-
hashing::detail::stable_hash_append(Hash, A);
71-
hashing::detail::stable_hash_append(Hash, B);
72-
hashing::detail::stable_hash_append(Hash, C);
73-
hashing::detail::stable_hash_append(Hash, D);
74-
return Hash;
75-
}
76-
77-
/// Compute a stable_hash for a sequence of values.
78-
///
79-
/// This hashes a sequence of values. It produces the same stable_hash as
80-
/// 'stable_hash_combine(a, b, c, ...)', but can run over arbitrary sized
81-
/// sequences and is significantly faster given pointers and types which
82-
/// can be hashed as a sequence of bytes.
83-
template <typename InputIteratorT>
84-
stable_hash stable_hash_combine_range(InputIteratorT First,
85-
InputIteratorT Last) {
86-
stable_hash Hash = hashing::detail::FNV_OFFSET_64;
87-
for (auto I = First; I != Last; ++I)
88-
hashing::detail::stable_hash_append(Hash, *I);
89-
return Hash;
90-
}
91-
92-
inline stable_hash stable_hash_combine_array(const stable_hash *P, size_t C) {
93-
stable_hash Hash = hashing::detail::FNV_OFFSET_64;
94-
for (size_t I = 0; I < C; ++I)
95-
hashing::detail::stable_hash_append(Hash, P[I]);
96-
return Hash;
49+
stable_hash Hashes[4] = {A, B, C, D};
50+
return stable_hash_combine(Hashes);
9751
}
9852

99-
inline stable_hash stable_hash_combine_string(const StringRef &S) {
100-
return stable_hash_combine_range(S.begin(), S.end());
53+
// Removes suffixes introduced by LLVM from the name to enhance stability and
54+
// maintain closeness to the original name across different builds.
55+
inline StringRef get_stable_name(StringRef Name) {
56+
auto [P1, S1] = Name.rsplit(".llvm.");
57+
auto [P2, S2] = P1.rsplit(".__uniq.");
58+
return P2;
10159
}
10260

103-
inline stable_hash stable_hash_combine_string(const char *C) {
104-
stable_hash Hash = hashing::detail::FNV_OFFSET_64;
105-
while (*C)
106-
hashing::detail::stable_hash_append(Hash, *(C++));
107-
return Hash;
61+
// Generates a consistent hash value for a given input name across different
62+
// program executions and environments. This function first converts the input
63+
// name into a stable form using the `get_stable_name` function, and then
64+
// computes a hash of this stable name. For instance, `foo.llvm.1234` would have
65+
// the same hash as `foo.llvm.5678.
66+
inline stable_hash stable_hash_name(StringRef Name) {
67+
return xxh3_64bits(get_stable_name(Name));
10868
}
10969

11070
} // namespace llvm

llvm/lib/CodeGen/MachineOperand.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ hash_code llvm::hash_value(const MachineOperand &MO) {
424424
const uint32_t *RegMask = MO.getRegMask();
425425
std::vector<stable_hash> RegMaskHashes(RegMask, RegMask + RegMaskSize);
426426
return hash_combine(MO.getType(), MO.getTargetFlags(),
427-
stable_hash_combine_array(RegMaskHashes.data(),
428-
RegMaskHashes.size()));
427+
stable_hash_combine(RegMaskHashes));
429428
}
430429

431430
assert(0 && "MachineOperand not associated with any MachineFunction");

0 commit comments

Comments
 (0)