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