Skip to content

Commit d7736a0

Browse files
committed
fixup! [Analysis] Add DebugInfoCache analysis
1 parent 7d38fe3 commit d7736a0

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

llvm/include/llvm/Analysis/DebugInfoCache.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ namespace llvm {
2727
/// DICompileUnit-level debug info. See an example usage in CoroSplit.
2828
class DebugInfoCache {
2929
public:
30-
using DIFinderCache = SmallDenseMap<const DICompileUnit *, DebugInfoFinder>;
31-
DIFinderCache Result;
30+
SmallDenseMap<const DICompileUnit *, DebugInfoFinder> Result;
3231

3332
DebugInfoCache(const Module &M);
3433

llvm/lib/Analysis/DebugInfoCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ DebugInfoFinder processCompileUnit(DICompileUnit *CU) {
2626
} // namespace
2727

2828
DebugInfoCache::DebugInfoCache(const Module &M) {
29-
for (const auto CU : M.debug_compile_units()) {
29+
for (auto *CU : M.debug_compile_units()) {
3030
auto DIFinder = processCompileUnit(CU);
31-
Result[CU] = std::move(DIFinder);
31+
Result.insert_or_assign(CU, std::move(DIFinder));
3232
}
3333
}
3434

llvm/unittests/Analysis/DebugInfoCacheTest.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Analysis/DebugInfoCache.h"
10+
#include "llvm/ADT/STLExtras.h"
1011
#include "llvm/AsmParser/Parser.h"
1112
#include "llvm/IR/Module.h"
1213
#include "llvm/IR/Verifier.h"
@@ -17,25 +18,46 @@
1718
namespace llvm {
1819
namespace {
1920

20-
// Forward declare the assembly
21+
// Forward declare the IR string
2122
extern StringRef MultiCUModule;
2223

23-
const DICompileUnit *findCU(const Module &M, StringRef FileName) {
24-
for (const auto CU : M.debug_compile_units()) {
25-
if (CU->getFilename() == FileName)
26-
return CU;
27-
}
24+
DICompileUnit *findCU(const Module &M, StringRef FileName) {
25+
auto CUs = M.debug_compile_units();
26+
auto Matching = llvm::find_if(
27+
CUs, [&](auto *CU) { return CU->getFilename() == FileName; });
28+
return Matching != CUs.end() ? *Matching : nullptr;
29+
}
30+
31+
void checkEqualDI(const DebugInfoFinder &DIFinder1,
32+
const DebugInfoFinder &DIFinder2) {
33+
EXPECT_TRUE(
34+
llvm::equal(DIFinder1.compile_units(), DIFinder2.compile_units()));
35+
EXPECT_TRUE(llvm::equal(DIFinder1.types(), DIFinder2.types()));
36+
EXPECT_TRUE(llvm::equal(DIFinder1.subprograms(), DIFinder2.subprograms()));
37+
EXPECT_TRUE(llvm::equal(DIFinder1.scopes(), DIFinder2.scopes()));
38+
}
39+
40+
void checkCachedDISameAsFromScratch(llvm::Module &M, const DebugInfoCache &DIC,
41+
StringRef CUName) {
42+
auto *CU = findCU(M, CUName);
43+
EXPECT_NE(CU, nullptr);
44+
45+
auto CachedDIFinder = DIC.Result.find(CU);
46+
EXPECT_NE(CachedDIFinder, DIC.Result.end());
2847

29-
return nullptr;
48+
DebugInfoFinder ExpectedDIFinder;
49+
ExpectedDIFinder.processCompileUnit(CU);
50+
51+
checkEqualDI(CachedDIFinder->getSecond(), ExpectedDIFinder);
3052
}
3153

3254
class DebugInfoCacheTest : public testing::Test {
3355
protected:
3456
LLVMContext C;
3557

36-
std::unique_ptr<Module> makeModule(StringRef Assembly) {
58+
std::unique_ptr<Module> makeModule(StringRef IR) {
3759
SMDiagnostic Err;
38-
auto M = parseAssemblyString(Assembly, Err, C);
60+
auto M = parseAssemblyString(IR, Err, C);
3961
if (!M)
4062
Err.print("DebugInfoCacheTest", errs());
4163

@@ -55,27 +77,8 @@ TEST_F(DebugInfoCacheTest, TestMultiCU) {
5577
DebugInfoCache DIC{*M};
5678
EXPECT_EQ(DIC.Result.size(), 2u);
5779

58-
auto *File1CU = findCU(*M, "file1.cpp");
59-
EXPECT_NE(File1CU, nullptr);
60-
61-
auto File1DIFinder = DIC.Result.find(File1CU);
62-
EXPECT_NE(File1DIFinder, DIC.Result.end());
63-
64-
EXPECT_EQ(File1DIFinder->getSecond().compile_unit_count(), 1u);
65-
EXPECT_EQ(File1DIFinder->getSecond().type_count(), 6u);
66-
EXPECT_EQ(File1DIFinder->getSecond().subprogram_count(), 0u);
67-
EXPECT_EQ(File1DIFinder->getSecond().scope_count(), 1u);
68-
69-
auto *File2CU = findCU(*M, "file2.cpp");
70-
EXPECT_NE(File1CU, nullptr);
71-
72-
auto File2DIFinder = DIC.Result.find(File2CU);
73-
EXPECT_NE(File2DIFinder, DIC.Result.end());
74-
75-
EXPECT_EQ(File2DIFinder->getSecond().compile_unit_count(), 1u);
76-
EXPECT_EQ(File2DIFinder->getSecond().type_count(), 2u);
77-
EXPECT_EQ(File2DIFinder->getSecond().subprogram_count(), 0u);
78-
EXPECT_EQ(File2DIFinder->getSecond().scope_count(), 2u);
80+
checkCachedDISameAsFromScratch(*M, DIC, "file1.cpp");
81+
checkCachedDISameAsFromScratch(*M, DIC, "file2.cpp");
7982
}
8083

8184
/* Generated roughly by

0 commit comments

Comments
 (0)