Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 1 | //===- CGSCCPassManagerTest.cpp -------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/CGSCCPassManager.h" |
| 11 | #include "llvm/Analysis/LazyCallGraph.h" |
| 12 | #include "llvm/AsmParser/Parser.h" |
| 13 | #include "llvm/IR/Function.h" |
| 14 | #include "llvm/IR/InstIterator.h" |
| 15 | #include "llvm/IR/LLVMContext.h" |
| 16 | #include "llvm/IR/Module.h" |
| 17 | #include "llvm/IR/PassManager.h" |
| 18 | #include "llvm/Support/SourceMgr.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class TestModuleAnalysis { |
| 26 | public: |
| 27 | struct Result { |
| 28 | Result(int Count) : FunctionCount(Count) {} |
| 29 | int FunctionCount; |
| 30 | }; |
| 31 | |
| 32 | static void *ID() { return (void *)&PassID; } |
| 33 | static StringRef name() { return "TestModuleAnalysis"; } |
| 34 | |
| 35 | TestModuleAnalysis(int &Runs) : Runs(Runs) {} |
| 36 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 37 | Result run(Module &M, ModuleAnalysisManager &AM) { |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 38 | ++Runs; |
| 39 | return Result(M.size()); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | static char PassID; |
| 44 | |
| 45 | int &Runs; |
| 46 | }; |
| 47 | |
| 48 | char TestModuleAnalysis::PassID; |
| 49 | |
| 50 | class TestSCCAnalysis { |
| 51 | public: |
| 52 | struct Result { |
| 53 | Result(int Count) : FunctionCount(Count) {} |
| 54 | int FunctionCount; |
| 55 | }; |
| 56 | |
| 57 | static void *ID() { return (void *)&PassID; } |
| 58 | static StringRef name() { return "TestSCCAnalysis"; } |
| 59 | |
| 60 | TestSCCAnalysis(int &Runs) : Runs(Runs) {} |
| 61 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 62 | Result run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &) { |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 63 | ++Runs; |
| 64 | return Result(C.size()); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | static char PassID; |
| 69 | |
| 70 | int &Runs; |
| 71 | }; |
| 72 | |
| 73 | char TestSCCAnalysis::PassID; |
| 74 | |
| 75 | class TestFunctionAnalysis { |
| 76 | public: |
| 77 | struct Result { |
| 78 | Result(int Count) : InstructionCount(Count) {} |
| 79 | int InstructionCount; |
| 80 | }; |
| 81 | |
| 82 | static void *ID() { return (void *)&PassID; } |
| 83 | static StringRef name() { return "TestFunctionAnalysis"; } |
| 84 | |
| 85 | TestFunctionAnalysis(int &Runs) : Runs(Runs) {} |
| 86 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 87 | Result run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 88 | ++Runs; |
| 89 | int Count = 0; |
| 90 | for (Instruction &I : instructions(F)) { |
| 91 | (void)I; |
| 92 | ++Count; |
| 93 | } |
| 94 | return Result(Count); |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | static char PassID; |
| 99 | |
| 100 | int &Runs; |
| 101 | }; |
| 102 | |
| 103 | char TestFunctionAnalysis::PassID; |
| 104 | |
Chandler Carruth | c5d211e | 2016-02-23 10:47:57 +0000 | [diff] [blame] | 105 | class TestImmutableFunctionAnalysis { |
| 106 | public: |
| 107 | struct Result { |
| 108 | bool invalidate(Function &, const PreservedAnalyses &) { return false; } |
| 109 | }; |
| 110 | |
| 111 | static void *ID() { return (void *)&PassID; } |
| 112 | static StringRef name() { return "TestImmutableFunctionAnalysis"; } |
| 113 | |
| 114 | TestImmutableFunctionAnalysis(int &Runs) : Runs(Runs) {} |
| 115 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 116 | Result run(Function &F, FunctionAnalysisManager &AM) { |
Chandler Carruth | c5d211e | 2016-02-23 10:47:57 +0000 | [diff] [blame] | 117 | ++Runs; |
| 118 | return Result(); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | static char PassID; |
| 123 | |
| 124 | int &Runs; |
| 125 | }; |
| 126 | |
| 127 | char TestImmutableFunctionAnalysis::PassID; |
| 128 | |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 129 | struct LambdaSCCPass : public PassInfoMixin<LambdaSCCPass> { |
| 130 | template <typename T> LambdaSCCPass(T &&Arg) : Func(std::forward<T>(Arg)) {} |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 131 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 132 | PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, |
| 133 | LazyCallGraph &CG, CGSCCUpdateResult &UR) { |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 134 | return Func(C, AM, CG, UR); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 137 | std::function<PreservedAnalyses(LazyCallGraph::SCC &, CGSCCAnalysisManager &, |
| 138 | LazyCallGraph &, CGSCCUpdateResult &)> |
| 139 | Func; |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 142 | struct LambdaFunctionPass : public PassInfoMixin<LambdaFunctionPass> { |
| 143 | template <typename T> LambdaFunctionPass(T &&Arg) : Func(std::forward<T>(Arg)) {} |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 144 | |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 145 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) { |
| 146 | return Func(F, AM); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 149 | std::function<PreservedAnalyses(Function &, FunctionAnalysisManager &)> Func; |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Chandler Carruth | 6c138ce | 2016-06-28 00:38:42 +0000 | [diff] [blame] | 152 | std::unique_ptr<Module> parseIR(const char *IR) { |
| 153 | // We just use a static context here. This is never called from multiple |
| 154 | // threads so it is harmless no matter how it is implemented. We just need |
| 155 | // the context to outlive the module which it does. |
| 156 | static LLVMContext C; |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 157 | SMDiagnostic Err; |
| 158 | return parseAssemblyString(IR, Err, C); |
| 159 | } |
| 160 | |
Chandler Carruth | 6c138ce | 2016-06-28 00:38:42 +0000 | [diff] [blame] | 161 | TEST(CGSCCPassManagerTest, Basic) { |
| 162 | auto M = parseIR("define void @f() {\n" |
| 163 | "entry:\n" |
| 164 | " call void @g()\n" |
| 165 | " call void @h1()\n" |
| 166 | " ret void\n" |
| 167 | "}\n" |
| 168 | "define void @g() {\n" |
| 169 | "entry:\n" |
| 170 | " call void @g()\n" |
| 171 | " call void @x()\n" |
| 172 | " ret void\n" |
| 173 | "}\n" |
| 174 | "define void @h1() {\n" |
| 175 | "entry:\n" |
| 176 | " call void @h2()\n" |
| 177 | " ret void\n" |
| 178 | "}\n" |
| 179 | "define void @h2() {\n" |
| 180 | "entry:\n" |
| 181 | " call void @h3()\n" |
| 182 | " call void @x()\n" |
| 183 | " ret void\n" |
| 184 | "}\n" |
| 185 | "define void @h3() {\n" |
| 186 | "entry:\n" |
| 187 | " call void @h1()\n" |
| 188 | " ret void\n" |
| 189 | "}\n" |
| 190 | "define void @x() {\n" |
| 191 | "entry:\n" |
| 192 | " ret void\n" |
| 193 | "}\n"); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 194 | FunctionAnalysisManager FAM(/*DebugLogging*/ true); |
| 195 | int FunctionAnalysisRuns = 0; |
| 196 | FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); }); |
Chandler Carruth | c5d211e | 2016-02-23 10:47:57 +0000 | [diff] [blame] | 197 | int ImmutableFunctionAnalysisRuns = 0; |
| 198 | FAM.registerPass([&] { |
| 199 | return TestImmutableFunctionAnalysis(ImmutableFunctionAnalysisRuns); |
| 200 | }); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 201 | |
| 202 | CGSCCAnalysisManager CGAM(/*DebugLogging*/ true); |
| 203 | int SCCAnalysisRuns = 0; |
| 204 | CGAM.registerPass([&] { return TestSCCAnalysis(SCCAnalysisRuns); }); |
| 205 | |
| 206 | ModuleAnalysisManager MAM(/*DebugLogging*/ true); |
| 207 | int ModuleAnalysisRuns = 0; |
| 208 | MAM.registerPass([&] { return LazyCallGraphAnalysis(); }); |
| 209 | MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); }); |
| 210 | |
| 211 | MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); |
| 212 | MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); }); |
| 213 | CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); }); |
| 214 | CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); }); |
| 215 | FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); }); |
| 216 | FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); |
| 217 | |
| 218 | ModulePassManager MPM(/*DebugLogging*/ true); |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 219 | MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>()); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 220 | |
| 221 | CGSCCPassManager CGPM1(/*DebugLogging*/ true); |
| 222 | int SCCPassRunCount1 = 0; |
| 223 | int AnalyzedInstrCount1 = 0; |
| 224 | int AnalyzedSCCFunctionCount1 = 0; |
| 225 | int AnalyzedModuleFunctionCount1 = 0; |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 226 | CGPM1.addPass( |
| 227 | LambdaSCCPass([&](LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, |
| 228 | LazyCallGraph &CG, CGSCCUpdateResult &UR) { |
| 229 | ++SCCPassRunCount1; |
| 230 | |
| 231 | const ModuleAnalysisManager &MAM = |
| 232 | AM.getResult<ModuleAnalysisManagerCGSCCProxy>(C, CG).getManager(); |
| 233 | FunctionAnalysisManager &FAM = |
| 234 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); |
| 235 | if (TestModuleAnalysis::Result *TMA = |
| 236 | MAM.getCachedResult<TestModuleAnalysis>( |
| 237 | *C.begin()->getFunction().getParent())) |
| 238 | AnalyzedModuleFunctionCount1 += TMA->FunctionCount; |
| 239 | |
| 240 | TestSCCAnalysis::Result &AR = AM.getResult<TestSCCAnalysis>(C, CG); |
| 241 | AnalyzedSCCFunctionCount1 += AR.FunctionCount; |
| 242 | for (LazyCallGraph::Node &N : C) { |
| 243 | TestFunctionAnalysis::Result &FAR = |
| 244 | FAM.getResult<TestFunctionAnalysis>(N.getFunction()); |
| 245 | AnalyzedInstrCount1 += FAR.InstructionCount; |
| 246 | |
| 247 | // Just ensure we get the immutable results. |
| 248 | (void)FAM.getResult<TestImmutableFunctionAnalysis>(N.getFunction()); |
| 249 | } |
| 250 | |
| 251 | return PreservedAnalyses::all(); |
| 252 | })); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 253 | |
| 254 | FunctionPassManager FPM1(/*DebugLogging*/ true); |
| 255 | int FunctionPassRunCount1 = 0; |
Chandler Carruth | d4e80a9 | 2016-09-02 01:08:04 +0000 | [diff] [blame] | 256 | FPM1.addPass(LambdaFunctionPass([&](Function &, FunctionAnalysisManager &) { |
| 257 | ++FunctionPassRunCount1; |
| 258 | return PreservedAnalyses::all(); |
| 259 | })); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 260 | CGPM1.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM1))); |
| 261 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM1))); |
| 262 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 263 | MPM.run(*M, MAM); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 264 | |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 265 | EXPECT_EQ(1, ModuleAnalysisRuns); |
| 266 | EXPECT_EQ(4, SCCAnalysisRuns); |
| 267 | EXPECT_EQ(6, FunctionAnalysisRuns); |
Chandler Carruth | c5d211e | 2016-02-23 10:47:57 +0000 | [diff] [blame] | 268 | EXPECT_EQ(6, ImmutableFunctionAnalysisRuns); |
Chandler Carruth | 7431992 | 2016-02-23 10:02:02 +0000 | [diff] [blame] | 269 | |
| 270 | EXPECT_EQ(4, SCCPassRunCount1); |
| 271 | EXPECT_EQ(14, AnalyzedInstrCount1); |
| 272 | EXPECT_EQ(6, AnalyzedSCCFunctionCount1); |
| 273 | EXPECT_EQ(4 * 6, AnalyzedModuleFunctionCount1); |
| 274 | } |
| 275 | |
| 276 | } |