blob: 7014436c290b904f6a8323677a68b7e69e780411 [file] [log] [blame]
Fedor Sergeev6a9c2f42019-03-15 22:15:23 +00001//===- unittests/IR/TimePassesTest.cpp - TimePassesHandler 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 <gtest/gtest.h>
10#include <llvm/ADT/SmallString.h>
11#include <llvm/IR/LLVMContext.h>
12#include <llvm/IR/Module.h>
13#include <llvm/IR/PassInstrumentation.h>
14#include <llvm/IR/PassManager.h>
15#include <llvm/IR/PassTimingInfo.h>
16#include <llvm/Support/raw_ostream.h>
17
18using namespace llvm;
19
20namespace {
21
22class MyPass1 : public PassInfoMixin<MyPass1> {};
23class MyPass2 : public PassInfoMixin<MyPass2> {};
24
25TEST(TimePassesTest, CustomOut) {
26 PassInstrumentationCallbacks PIC;
27 PassInstrumentation PI(&PIC);
28
29 LLVMContext Context;
30 Module M("TestModule", Context);
31 MyPass1 Pass1;
32 MyPass2 Pass2;
33
34 SmallString<0> TimePassesStr;
35 raw_svector_ostream ReportStream(TimePassesStr);
36
37 // Setup time-passes handler and redirect output to the stream.
38 std::unique_ptr<TimePassesHandler> TimePasses =
39 llvm::make_unique<TimePassesHandler>(true);
40 TimePasses->setOutStream(ReportStream);
41 TimePasses->registerCallbacks(PIC);
42
43 // Running some passes to trigger the timers.
44 PI.runBeforePass(Pass1, M);
45 PI.runBeforePass(Pass2, M);
46 PI.runAfterPass(Pass2, M);
47 PI.runAfterPass(Pass1, M);
48
49 // Generating report.
50 TimePasses->print();
51
52 // There should be Pass1 and Pass2 in the report
53 EXPECT_FALSE(TimePassesStr.empty());
54 EXPECT_TRUE(TimePassesStr.str().contains("report"));
55 EXPECT_TRUE(TimePassesStr.str().contains("Pass1"));
56 EXPECT_TRUE(TimePassesStr.str().contains("Pass2"));
57
58 // Clear and generate report again.
59 TimePassesStr.clear();
60 TimePasses->print();
61 // Since we did not run any passes since last print, report should be empty.
62 EXPECT_TRUE(TimePassesStr.empty());
63
64 // Now run just a single pass to populate timers again.
65 PI.runBeforePass(Pass2, M);
66 PI.runAfterPass(Pass2, M);
67
68 // Generate report by deleting the handler.
69 TimePasses.reset();
70
71 // There should be Pass2 in this report and no Pass1.
72 EXPECT_FALSE(TimePassesStr.str().empty());
73 EXPECT_TRUE(TimePassesStr.str().contains("report"));
74 EXPECT_FALSE(TimePassesStr.str().contains("Pass1"));
75 EXPECT_TRUE(TimePassesStr.str().contains("Pass2"));
76}
77
78} // end anonymous namespace