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
14using namespace llvm;
15
16namespace {
17
18static std::unique_ptr<ModuleSummaryIndex> makeLLVMIndex(const char *Summary) {
19 SMDiagnostic Err;
20 std::unique_ptr<ModuleSummaryIndex> Index =
21 parseSummaryIndexAssemblyString(AsmString: Summary, Err);
22 if (!Index)
23 Err.print(ProgName: "ModuleSummaryIndexTest", S&: errs());
24 return Index;
25}
26
27TEST(ModuleSummaryIndexTest, MemProfSummaryPrinting) {
28 std::unique_ptr<ModuleSummaryIndex> Index = makeLLVMIndex(Summary: 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>(Val: Index->getGlobalValueSummary(/*guid=*/ValueGUID: 25));
40 for (auto &CI : CallsiteSummary->callsites())
41 OS << "\n" << CI;
42
43 auto *AllocSummary =
44 cast<FunctionSummary>(Val: Index->getGlobalValueSummary(/*guid=*/ValueGUID: 23));
45 for (auto &AI : AllocSummary->allocs())
46 OS << "\n" << AI;
47
48 OS.flush();
49 EXPECT_EQ(Data, R"(
50Callee: 23 Clones: 0 StackIds: 2, 3
51Callee: 23 Clones: 0 StackIds: 2, 4
52Versions: 0 MIB:
53 AllocType 1 StackIds: 0, 1, 2, 3
54 AllocType 2 StackIds: 0, 1, 2, 4
55)");
56}
57} // end anonymous namespace
58

source code of llvm/unittests/IR/ModuleSummaryIndexTest.cpp