xref: /llvm-project/llvm/unittests/IR/ModuleSummaryIndexTest.cpp (revision 459a82e6890ff41e30d486f36c8c7ec22628bb7a)
1 //===- ModuleSummaryIndexTest.cpp - ModuleSummaryIndex Unit Tests-============//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/IR/ModuleSummaryIndex.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/Support/SourceMgr.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
16 namespace {
17 
18 static std::unique_ptr<ModuleSummaryIndex> makeLLVMIndex(const char *Summary) {
19   SMDiagnostic Err;
20   std::unique_ptr<ModuleSummaryIndex> Index =
21       parseSummaryIndexAssemblyString(Summary, Err);
22   if (!Index)
23     Err.print("ModuleSummaryIndexTest", errs());
24   return Index;
25 }
26 
27 TEST(ModuleSummaryIndexTest, MemProfSummaryPrinting) {
28   std::unique_ptr<ModuleSummaryIndex> Index = makeLLVMIndex(R"Summary(
29 ^0 = module: (path: "test.o", hash: (0, 0, 0, 0, 0))
30 ^1 = gv: (guid: 23, summaries: (function: (module: ^0, flags: (linkage: external), insts: 2, allocs: ((versions: (none), memProf: ((type: notcold, stackIds: (1, 2, 3, 4)), (type: cold, stackIds: (1, 2, 3, 5))))))))
31 ^2 = gv: (guid: 25, summaries: (function: (module: ^0, flags: (linkage: external), insts: 22, calls: ((callee: ^1)), callsites: ((callee: ^1, clones: (0), stackIds: (3, 4)), (callee: ^1, clones: (0), stackIds: (3, 5))))))
32 )Summary");
33 
34   std::string Data;
35   raw_string_ostream OS(Data);
36 
37   ASSERT_NE(Index, nullptr);
38   auto *CallsiteSummary =
39       cast<FunctionSummary>(Index->getGlobalValueSummary(/*guid=*/25));
40   for (auto &CI : CallsiteSummary->callsites())
41     OS << "\n" << CI;
42 
43   auto *AllocSummary =
44       cast<FunctionSummary>(Index->getGlobalValueSummary(/*guid=*/23));
45   for (auto &AI : AllocSummary->allocs())
46     OS << "\n" << AI;
47 
48   EXPECT_EQ(Data, R"(
49 Callee: 23 Clones: 0 StackIds: 2, 3
50 Callee: 23 Clones: 0 StackIds: 2, 4
51 Versions: 0 MIB:
52 		AllocType 1 StackIds: 0, 1, 2, 3
53 		AllocType 2 StackIds: 0, 1, 2, 4
54 )");
55 }
56 } // end anonymous namespace
57