|
| 1 | +//===- MemProfUseTest.cpp - MemProf use 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/AsmParser/Parser.h" |
| 10 | +#include "llvm/IR/LLVMContext.h" |
| 11 | +#include "llvm/IR/Module.h" |
| 12 | +#include "llvm/ProfileData/MemProf.h" |
| 13 | +#include "llvm/Support/SourceMgr.h" |
| 14 | +#include "llvm/Transforms/Instrumentation/MemProfiler.h" |
| 15 | + |
| 16 | +#include "gmock/gmock.h" |
| 17 | +#include "gtest/gtest.h" |
| 18 | + |
| 19 | +namespace { |
| 20 | +using namespace llvm; |
| 21 | +using namespace llvm::memprof; |
| 22 | +using testing::FieldsAre; |
| 23 | +using testing::Pair; |
| 24 | +using testing::SizeIs; |
| 25 | + |
| 26 | +TEST(MemProf, ExtractDirectCallsFromIR) { |
| 27 | + // The following IR is generated from: |
| 28 | + // |
| 29 | + // void f1(); |
| 30 | + // void f2(); |
| 31 | + // void f3(); |
| 32 | + // |
| 33 | + // void foo() { |
| 34 | + // f1(); |
| 35 | + // f2(); f3(); |
| 36 | + // } |
| 37 | + StringRef IR = R"IR( |
| 38 | +define dso_local void @_Z3foov() !dbg !10 { |
| 39 | +entry: |
| 40 | + call void @_Z2f1v(), !dbg !13 |
| 41 | + call void @_Z2f2v(), !dbg !14 |
| 42 | + call void @_Z2f3v(), !dbg !15 |
| 43 | + ret void, !dbg !16 |
| 44 | +} |
| 45 | +
|
| 46 | +declare !dbg !17 void @_Z2f1v() |
| 47 | +
|
| 48 | +declare !dbg !18 void @_Z2f2v() |
| 49 | +
|
| 50 | +declare !dbg !19 void @_Z2f3v() |
| 51 | +
|
| 52 | +!llvm.dbg.cu = !{!0} |
| 53 | +!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8} |
| 54 | +!llvm.ident = !{!9} |
| 55 | +
|
| 56 | +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, debugInfoForProfiling: true, nameTableKind: None) |
| 57 | +!1 = !DIFile(filename: "foobar.cc", directory: "/") |
| 58 | +!2 = !{i32 7, !"Dwarf Version", i32 5} |
| 59 | +!3 = !{i32 2, !"Debug Info Version", i32 3} |
| 60 | +!4 = !{i32 1, !"wchar_size", i32 4} |
| 61 | +!5 = !{i32 1, !"MemProfProfileFilename", !"memprof.profraw"} |
| 62 | +!6 = !{i32 8, !"PIC Level", i32 2} |
| 63 | +!7 = !{i32 7, !"PIE Level", i32 2} |
| 64 | +!8 = !{i32 7, !"uwtable", i32 2} |
| 65 | +!9 = !{!"clang"} |
| 66 | +!10 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 5, type: !11, scopeLine: 5, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0) |
| 67 | +!11 = !DISubroutineType(types: !12) |
| 68 | +!12 = !{} |
| 69 | +!13 = !DILocation(line: 6, column: 3, scope: !10) |
| 70 | +!14 = !DILocation(line: 7, column: 3, scope: !10) |
| 71 | +!15 = !DILocation(line: 7, column: 9, scope: !10) |
| 72 | +!16 = !DILocation(line: 8, column: 1, scope: !10) |
| 73 | +!17 = !DISubprogram(name: "f1", linkageName: "_Z2f1v", scope: !1, file: !1, line: 1, type: !11, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) |
| 74 | +!18 = !DISubprogram(name: "f2", linkageName: "_Z2f2v", scope: !1, file: !1, line: 2, type: !11, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) |
| 75 | +!19 = !DISubprogram(name: "f3", linkageName: "_Z2f3v", scope: !1, file: !1, line: 3, type: !11, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) |
| 76 | +)IR"; |
| 77 | + |
| 78 | + LLVMContext Ctx; |
| 79 | + SMDiagnostic Err; |
| 80 | + std::unique_ptr<Module> M = parseAssemblyString(IR, Err, Ctx); |
| 81 | + ASSERT_TRUE(M); |
| 82 | + |
| 83 | + auto Calls = extractCallsFromIR(*M); |
| 84 | + |
| 85 | + // Expect exactly one caller. |
| 86 | + ASSERT_THAT(Calls, SizeIs(1)); |
| 87 | + |
| 88 | + auto It = Calls.begin(); |
| 89 | + ASSERT_NE(It, Calls.end()); |
| 90 | + |
| 91 | + const auto &[CallerGUID, CallSites] = *It; |
| 92 | + EXPECT_EQ(CallerGUID, IndexedMemProfRecord::getGUID("_Z3foov")); |
| 93 | + ASSERT_THAT(CallSites, SizeIs(3)); |
| 94 | + |
| 95 | + // Verify that call sites show up in the ascending order of their source |
| 96 | + // locations. |
| 97 | + EXPECT_THAT(CallSites[0], |
| 98 | + Pair(FieldsAre(1U, 3U), IndexedMemProfRecord::getGUID("_Z2f1v"))); |
| 99 | + EXPECT_THAT(CallSites[1], |
| 100 | + Pair(FieldsAre(2U, 3U), IndexedMemProfRecord::getGUID("_Z2f2v"))); |
| 101 | + EXPECT_THAT(CallSites[2], |
| 102 | + Pair(FieldsAre(2U, 9U), IndexedMemProfRecord::getGUID("_Z2f3v"))); |
| 103 | +} |
| 104 | +} // namespace |
0 commit comments