PassManagerTest.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. //===- llvm/unittest/IR/PassManager.cpp - PassManager 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. #include "llvm/IR/PassManager.h"
  9. #include "llvm/AsmParser/Parser.h"
  10. #include "llvm/IR/Function.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "llvm/IR/Module.h"
  13. #include "llvm/Support/SourceMgr.h"
  14. #include "gtest/gtest.h"
  15. using namespace llvm;
  16. namespace {
  17. class TestFunctionAnalysis : public AnalysisInfoMixin<TestFunctionAnalysis> {
  18. public:
  19. struct Result {
  20. Result(int Count) : InstructionCount(Count) {}
  21. int InstructionCount;
  22. };
  23. TestFunctionAnalysis(int &Runs) : Runs(Runs) {}
  24. /// Run the analysis pass over the function and return a result.
  25. Result run(Function &F, FunctionAnalysisManager &AM) {
  26. ++Runs;
  27. int Count = 0;
  28. for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
  29. for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
  30. ++II)
  31. ++Count;
  32. return Result(Count);
  33. }
  34. private:
  35. friend AnalysisInfoMixin<TestFunctionAnalysis>;
  36. static AnalysisKey Key;
  37. int &Runs;
  38. };
  39. AnalysisKey TestFunctionAnalysis::Key;
  40. class TestModuleAnalysis : public AnalysisInfoMixin<TestModuleAnalysis> {
  41. public:
  42. struct Result {
  43. Result(int Count) : FunctionCount(Count) {}
  44. int FunctionCount;
  45. };
  46. TestModuleAnalysis(int &Runs) : Runs(Runs) {}
  47. Result run(Module &M, ModuleAnalysisManager &AM) {
  48. ++Runs;
  49. int Count = 0;
  50. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  51. ++Count;
  52. return Result(Count);
  53. }
  54. private:
  55. friend AnalysisInfoMixin<TestModuleAnalysis>;
  56. static AnalysisKey Key;
  57. int &Runs;
  58. };
  59. AnalysisKey TestModuleAnalysis::Key;
  60. struct TestModulePass : PassInfoMixin<TestModulePass> {
  61. TestModulePass(int &RunCount) : RunCount(RunCount) {}
  62. PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
  63. ++RunCount;
  64. return PreservedAnalyses::none();
  65. }
  66. int &RunCount;
  67. };
  68. struct TestPreservingModulePass : PassInfoMixin<TestPreservingModulePass> {
  69. PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
  70. return PreservedAnalyses::all();
  71. }
  72. };
  73. struct TestFunctionPass : PassInfoMixin<TestFunctionPass> {
  74. TestFunctionPass(int &RunCount, int &AnalyzedInstrCount,
  75. int &AnalyzedFunctionCount,
  76. bool OnlyUseCachedResults = false)
  77. : RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount),
  78. AnalyzedFunctionCount(AnalyzedFunctionCount),
  79. OnlyUseCachedResults(OnlyUseCachedResults) {}
  80. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
  81. ++RunCount;
  82. const ModuleAnalysisManager &MAM =
  83. AM.getResult<ModuleAnalysisManagerFunctionProxy>(F).getManager();
  84. if (TestModuleAnalysis::Result *TMA =
  85. MAM.getCachedResult<TestModuleAnalysis>(*F.getParent()))
  86. AnalyzedFunctionCount += TMA->FunctionCount;
  87. if (OnlyUseCachedResults) {
  88. // Hack to force the use of the cached interface.
  89. if (TestFunctionAnalysis::Result *AR =
  90. AM.getCachedResult<TestFunctionAnalysis>(F))
  91. AnalyzedInstrCount += AR->InstructionCount;
  92. } else {
  93. // Typical path just runs the analysis as needed.
  94. TestFunctionAnalysis::Result &AR = AM.getResult<TestFunctionAnalysis>(F);
  95. AnalyzedInstrCount += AR.InstructionCount;
  96. }
  97. return PreservedAnalyses::all();
  98. }
  99. int &RunCount;
  100. int &AnalyzedInstrCount;
  101. int &AnalyzedFunctionCount;
  102. bool OnlyUseCachedResults;
  103. };
  104. // A test function pass that invalidates all function analyses for a function
  105. // with a specific name.
  106. struct TestInvalidationFunctionPass
  107. : PassInfoMixin<TestInvalidationFunctionPass> {
  108. TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {}
  109. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
  110. return F.getName() == Name ? PreservedAnalyses::none()
  111. : PreservedAnalyses::all();
  112. }
  113. StringRef Name;
  114. };
  115. std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) {
  116. SMDiagnostic Err;
  117. return parseAssemblyString(IR, Err, Context);
  118. }
  119. class PassManagerTest : public ::testing::Test {
  120. protected:
  121. LLVMContext Context;
  122. std::unique_ptr<Module> M;
  123. public:
  124. PassManagerTest()
  125. : M(parseIR(Context, "define void @f() {\n"
  126. "entry:\n"
  127. " call void @g()\n"
  128. " call void @h()\n"
  129. " ret void\n"
  130. "}\n"
  131. "define void @g() {\n"
  132. " ret void\n"
  133. "}\n"
  134. "define void @h() {\n"
  135. " ret void\n"
  136. "}\n")) {}
  137. };
  138. TEST(PreservedAnalysesTest, Basic) {
  139. PreservedAnalyses PA1 = PreservedAnalyses();
  140. {
  141. auto PAC = PA1.getChecker<TestFunctionAnalysis>();
  142. EXPECT_FALSE(PAC.preserved());
  143. EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
  144. }
  145. {
  146. auto PAC = PA1.getChecker<TestModuleAnalysis>();
  147. EXPECT_FALSE(PAC.preserved());
  148. EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Module>>());
  149. }
  150. auto PA2 = PreservedAnalyses::none();
  151. {
  152. auto PAC = PA2.getChecker<TestFunctionAnalysis>();
  153. EXPECT_FALSE(PAC.preserved());
  154. EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
  155. }
  156. auto PA3 = PreservedAnalyses::all();
  157. {
  158. auto PAC = PA3.getChecker<TestFunctionAnalysis>();
  159. EXPECT_TRUE(PAC.preserved());
  160. EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>());
  161. }
  162. PreservedAnalyses PA4 = PA1;
  163. {
  164. auto PAC = PA4.getChecker<TestFunctionAnalysis>();
  165. EXPECT_FALSE(PAC.preserved());
  166. EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
  167. }
  168. PA4 = PA3;
  169. {
  170. auto PAC = PA4.getChecker<TestFunctionAnalysis>();
  171. EXPECT_TRUE(PAC.preserved());
  172. EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>());
  173. }
  174. PA4 = std::move(PA2);
  175. {
  176. auto PAC = PA4.getChecker<TestFunctionAnalysis>();
  177. EXPECT_FALSE(PAC.preserved());
  178. EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Function>>());
  179. }
  180. auto PA5 = PreservedAnalyses::allInSet<AllAnalysesOn<Function>>();
  181. {
  182. auto PAC = PA5.getChecker<TestFunctionAnalysis>();
  183. EXPECT_FALSE(PAC.preserved());
  184. EXPECT_TRUE(PAC.preservedSet<AllAnalysesOn<Function>>());
  185. EXPECT_FALSE(PAC.preservedSet<AllAnalysesOn<Module>>());
  186. }
  187. }
  188. TEST(PreservedAnalysesTest, Preserve) {
  189. auto PA = PreservedAnalyses::none();
  190. PA.preserve<TestFunctionAnalysis>();
  191. EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved());
  192. EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>().preserved());
  193. PA.preserve<TestModuleAnalysis>();
  194. EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved());
  195. EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>().preserved());
  196. // Redundant calls are fine.
  197. PA.preserve<TestFunctionAnalysis>();
  198. EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>().preserved());
  199. EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>().preserved());
  200. }
  201. TEST(PreservedAnalysesTest, PreserveSets) {
  202. auto PA = PreservedAnalyses::none();
  203. PA.preserveSet<AllAnalysesOn<Function>>();
  204. EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
  205. .preservedSet<AllAnalysesOn<Function>>());
  206. EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>()
  207. .preservedSet<AllAnalysesOn<Module>>());
  208. PA.preserveSet<AllAnalysesOn<Module>>();
  209. EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
  210. .preservedSet<AllAnalysesOn<Function>>());
  211. EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>()
  212. .preservedSet<AllAnalysesOn<Module>>());
  213. // Mixing is fine.
  214. PA.preserve<TestFunctionAnalysis>();
  215. EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
  216. .preservedSet<AllAnalysesOn<Function>>());
  217. EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>()
  218. .preservedSet<AllAnalysesOn<Module>>());
  219. // Redundant calls are fine.
  220. PA.preserveSet<AllAnalysesOn<Module>>();
  221. EXPECT_TRUE(PA.getChecker<TestFunctionAnalysis>()
  222. .preservedSet<AllAnalysesOn<Function>>());
  223. EXPECT_TRUE(PA.getChecker<TestModuleAnalysis>()
  224. .preservedSet<AllAnalysesOn<Module>>());
  225. }
  226. TEST(PreservedAnalysisTest, Intersect) {
  227. // Setup the initial sets.
  228. auto PA1 = PreservedAnalyses::none();
  229. PA1.preserve<TestFunctionAnalysis>();
  230. PA1.preserveSet<AllAnalysesOn<Module>>();
  231. auto PA2 = PreservedAnalyses::none();
  232. PA2.preserve<TestFunctionAnalysis>();
  233. PA2.preserveSet<AllAnalysesOn<Function>>();
  234. PA2.preserve<TestModuleAnalysis>();
  235. PA2.preserveSet<AllAnalysesOn<Module>>();
  236. auto PA3 = PreservedAnalyses::none();
  237. PA3.preserve<TestModuleAnalysis>();
  238. PA3.preserveSet<AllAnalysesOn<Function>>();
  239. // Self intersection is a no-op.
  240. auto Intersected = PA1;
  241. Intersected.intersect(PA1);
  242. EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  243. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  244. .preservedSet<AllAnalysesOn<Function>>());
  245. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  246. EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
  247. .preservedSet<AllAnalysesOn<Module>>());
  248. // Intersecting with all is a no-op.
  249. Intersected.intersect(PreservedAnalyses::all());
  250. EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  251. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  252. .preservedSet<AllAnalysesOn<Function>>());
  253. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  254. EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
  255. .preservedSet<AllAnalysesOn<Module>>());
  256. // Intersecting a narrow set with a more broad set is the narrow set.
  257. Intersected.intersect(PA2);
  258. EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  259. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  260. .preservedSet<AllAnalysesOn<Function>>());
  261. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  262. EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
  263. .preservedSet<AllAnalysesOn<Module>>());
  264. // Intersecting a broad set with a more narrow set is the narrow set.
  265. Intersected = PA2;
  266. Intersected.intersect(PA1);
  267. EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  268. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  269. .preservedSet<AllAnalysesOn<Function>>());
  270. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  271. EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
  272. .preservedSet<AllAnalysesOn<Module>>());
  273. // Intersecting with empty clears.
  274. Intersected.intersect(PreservedAnalyses::none());
  275. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  276. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  277. .preservedSet<AllAnalysesOn<Function>>());
  278. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  279. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>()
  280. .preservedSet<AllAnalysesOn<Module>>());
  281. // Intersecting non-overlapping clears.
  282. Intersected = PA1;
  283. Intersected.intersect(PA3);
  284. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  285. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  286. .preservedSet<AllAnalysesOn<Function>>());
  287. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  288. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>()
  289. .preservedSet<AllAnalysesOn<Module>>());
  290. // Intersecting with moves works in when there is storage on both sides.
  291. Intersected = PA1;
  292. auto Tmp = PA2;
  293. Intersected.intersect(std::move(Tmp));
  294. EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  295. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  296. .preservedSet<AllAnalysesOn<Function>>());
  297. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  298. EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
  299. .preservedSet<AllAnalysesOn<Module>>());
  300. // Intersecting with move works for incoming all and existing all.
  301. auto Tmp2 = PreservedAnalyses::all();
  302. Intersected.intersect(std::move(Tmp2));
  303. EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  304. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  305. .preservedSet<AllAnalysesOn<Function>>());
  306. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  307. EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
  308. .preservedSet<AllAnalysesOn<Module>>());
  309. Intersected = PreservedAnalyses::all();
  310. auto Tmp3 = PA1;
  311. Intersected.intersect(std::move(Tmp3));
  312. EXPECT_TRUE(Intersected.getChecker<TestFunctionAnalysis>().preserved());
  313. EXPECT_FALSE(Intersected.getChecker<TestFunctionAnalysis>()
  314. .preservedSet<AllAnalysesOn<Function>>());
  315. EXPECT_FALSE(Intersected.getChecker<TestModuleAnalysis>().preserved());
  316. EXPECT_TRUE(Intersected.getChecker<TestModuleAnalysis>()
  317. .preservedSet<AllAnalysesOn<Module>>());
  318. }
  319. TEST(PreservedAnalysisTest, Abandon) {
  320. auto PA = PreservedAnalyses::none();
  321. // We can abandon things after they are preserved.
  322. PA.preserve<TestFunctionAnalysis>();
  323. PA.abandon<TestFunctionAnalysis>();
  324. EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>().preserved());
  325. // Repeated is fine, and abandoning if they were never preserved is fine.
  326. PA.abandon<TestFunctionAnalysis>();
  327. EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>().preserved());
  328. PA.abandon<TestModuleAnalysis>();
  329. EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>().preserved());
  330. // Even if the sets are preserved, the abandoned analyses' checker won't
  331. // return true for those sets.
  332. PA.preserveSet<AllAnalysesOn<Function>>();
  333. PA.preserveSet<AllAnalysesOn<Module>>();
  334. EXPECT_FALSE(PA.getChecker<TestFunctionAnalysis>()
  335. .preservedSet<AllAnalysesOn<Function>>());
  336. EXPECT_FALSE(PA.getChecker<TestModuleAnalysis>()
  337. .preservedSet<AllAnalysesOn<Module>>());
  338. // But an arbitrary (opaque) analysis will still observe the sets as
  339. // preserved. This also checks that we can use an explicit ID rather than
  340. // a type.
  341. AnalysisKey FakeKey, *FakeID = &FakeKey;
  342. EXPECT_TRUE(PA.getChecker(FakeID).preservedSet<AllAnalysesOn<Function>>());
  343. EXPECT_TRUE(PA.getChecker(FakeID).preservedSet<AllAnalysesOn<Module>>());
  344. }
  345. TEST_F(PassManagerTest, Basic) {
  346. FunctionAnalysisManager FAM(/*DebugLogging*/ true);
  347. int FunctionAnalysisRuns = 0;
  348. FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); });
  349. ModuleAnalysisManager MAM(/*DebugLogging*/ true);
  350. int ModuleAnalysisRuns = 0;
  351. MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); });
  352. MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
  353. FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
  354. MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
  355. FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
  356. ModulePassManager MPM;
  357. // Count the runs over a Function.
  358. int FunctionPassRunCount1 = 0;
  359. int AnalyzedInstrCount1 = 0;
  360. int AnalyzedFunctionCount1 = 0;
  361. {
  362. // Pointless scoped copy to test move assignment.
  363. ModulePassManager NestedMPM(/*DebugLogging*/ true);
  364. FunctionPassManager FPM;
  365. {
  366. // Pointless scope to test move assignment.
  367. FunctionPassManager NestedFPM(/*DebugLogging*/ true);
  368. NestedFPM.addPass(TestFunctionPass(
  369. FunctionPassRunCount1, AnalyzedInstrCount1, AnalyzedFunctionCount1));
  370. FPM = std::move(NestedFPM);
  371. }
  372. NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  373. MPM = std::move(NestedMPM);
  374. }
  375. // Count the runs over a module.
  376. int ModulePassRunCount = 0;
  377. MPM.addPass(TestModulePass(ModulePassRunCount));
  378. // Count the runs over a Function in a separate manager.
  379. int FunctionPassRunCount2 = 0;
  380. int AnalyzedInstrCount2 = 0;
  381. int AnalyzedFunctionCount2 = 0;
  382. {
  383. FunctionPassManager FPM(/*DebugLogging*/ true);
  384. FPM.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2,
  385. AnalyzedFunctionCount2));
  386. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  387. }
  388. // A third function pass manager but with only preserving intervening passes
  389. // and with a function pass that invalidates exactly one analysis.
  390. MPM.addPass(TestPreservingModulePass());
  391. int FunctionPassRunCount3 = 0;
  392. int AnalyzedInstrCount3 = 0;
  393. int AnalyzedFunctionCount3 = 0;
  394. {
  395. FunctionPassManager FPM(/*DebugLogging*/ true);
  396. FPM.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3,
  397. AnalyzedFunctionCount3));
  398. FPM.addPass(TestInvalidationFunctionPass("f"));
  399. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  400. }
  401. // A fourth function pass manager but with only preserving intervening
  402. // passes but triggering the module analysis.
  403. MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>());
  404. int FunctionPassRunCount4 = 0;
  405. int AnalyzedInstrCount4 = 0;
  406. int AnalyzedFunctionCount4 = 0;
  407. {
  408. FunctionPassManager FPM;
  409. FPM.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4,
  410. AnalyzedFunctionCount4));
  411. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  412. }
  413. // A fifth function pass manager which invalidates one function first but
  414. // uses only cached results.
  415. int FunctionPassRunCount5 = 0;
  416. int AnalyzedInstrCount5 = 0;
  417. int AnalyzedFunctionCount5 = 0;
  418. {
  419. FunctionPassManager FPM(/*DebugLogging*/ true);
  420. FPM.addPass(TestInvalidationFunctionPass("f"));
  421. FPM.addPass(TestFunctionPass(FunctionPassRunCount5, AnalyzedInstrCount5,
  422. AnalyzedFunctionCount5,
  423. /*OnlyUseCachedResults=*/true));
  424. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  425. }
  426. MPM.run(*M, MAM);
  427. // Validate module pass counters.
  428. EXPECT_EQ(1, ModulePassRunCount);
  429. // Validate all function pass counter sets are the same.
  430. EXPECT_EQ(3, FunctionPassRunCount1);
  431. EXPECT_EQ(5, AnalyzedInstrCount1);
  432. EXPECT_EQ(0, AnalyzedFunctionCount1);
  433. EXPECT_EQ(3, FunctionPassRunCount2);
  434. EXPECT_EQ(5, AnalyzedInstrCount2);
  435. EXPECT_EQ(0, AnalyzedFunctionCount2);
  436. EXPECT_EQ(3, FunctionPassRunCount3);
  437. EXPECT_EQ(5, AnalyzedInstrCount3);
  438. EXPECT_EQ(0, AnalyzedFunctionCount3);
  439. EXPECT_EQ(3, FunctionPassRunCount4);
  440. EXPECT_EQ(5, AnalyzedInstrCount4);
  441. EXPECT_EQ(9, AnalyzedFunctionCount4);
  442. EXPECT_EQ(3, FunctionPassRunCount5);
  443. EXPECT_EQ(2, AnalyzedInstrCount5); // Only 'g' and 'h' were cached.
  444. EXPECT_EQ(9, AnalyzedFunctionCount5);
  445. // Validate the analysis counters:
  446. // first run over 3 functions, then module pass invalidates
  447. // second run over 3 functions, nothing invalidates
  448. // third run over 0 functions, but 1 function invalidated
  449. // fourth run over 1 function
  450. // fifth run invalidates 1 function first, but runs over 0 functions
  451. EXPECT_EQ(7, FunctionAnalysisRuns);
  452. EXPECT_EQ(1, ModuleAnalysisRuns);
  453. }
  454. // A customized pass manager that passes extra arguments through the
  455. // infrastructure.
  456. typedef AnalysisManager<Function, int> CustomizedAnalysisManager;
  457. typedef PassManager<Function, CustomizedAnalysisManager, int, int &>
  458. CustomizedPassManager;
  459. class CustomizedAnalysis : public AnalysisInfoMixin<CustomizedAnalysis> {
  460. public:
  461. struct Result {
  462. Result(int I) : I(I) {}
  463. int I;
  464. };
  465. Result run(Function &F, CustomizedAnalysisManager &AM, int I) {
  466. return Result(I);
  467. }
  468. private:
  469. friend AnalysisInfoMixin<CustomizedAnalysis>;
  470. static AnalysisKey Key;
  471. };
  472. AnalysisKey CustomizedAnalysis::Key;
  473. struct CustomizedPass : PassInfoMixin<CustomizedPass> {
  474. std::function<void(CustomizedAnalysis::Result &, int &)> Callback;
  475. template <typename CallbackT>
  476. CustomizedPass(CallbackT Callback) : Callback(Callback) {}
  477. PreservedAnalyses run(Function &F, CustomizedAnalysisManager &AM, int I,
  478. int &O) {
  479. Callback(AM.getResult<CustomizedAnalysis>(F, I), O);
  480. return PreservedAnalyses::none();
  481. }
  482. };
  483. TEST_F(PassManagerTest, CustomizedPassManagerArgs) {
  484. CustomizedAnalysisManager AM;
  485. AM.registerPass([&] { return CustomizedAnalysis(); });
  486. PassInstrumentationCallbacks PIC;
  487. AM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
  488. CustomizedPassManager PM;
  489. // Add an instance of the customized pass that just accumulates the input
  490. // after it is round-tripped through the analysis.
  491. int Result = 0;
  492. PM.addPass(
  493. CustomizedPass([](CustomizedAnalysis::Result &R, int &O) { O += R.I; }));
  494. // Run this over every function with the input of 42.
  495. for (Function &F : *M)
  496. PM.run(F, AM, 42, Result);
  497. // And ensure that we accumulated the correct result.
  498. EXPECT_EQ(42 * (int)M->size(), Result);
  499. }
  500. /// A test analysis pass which caches in its result another analysis pass and
  501. /// uses it to serve queries. This requires the result to invalidate itself
  502. /// when its dependency is invalidated.
  503. struct TestIndirectFunctionAnalysis
  504. : public AnalysisInfoMixin<TestIndirectFunctionAnalysis> {
  505. struct Result {
  506. Result(TestFunctionAnalysis::Result &FDep, TestModuleAnalysis::Result &MDep)
  507. : FDep(FDep), MDep(MDep) {}
  508. TestFunctionAnalysis::Result &FDep;
  509. TestModuleAnalysis::Result &MDep;
  510. bool invalidate(Function &F, const PreservedAnalyses &PA,
  511. FunctionAnalysisManager::Invalidator &Inv) {
  512. auto PAC = PA.getChecker<TestIndirectFunctionAnalysis>();
  513. return !(PAC.preserved() ||
  514. PAC.preservedSet<AllAnalysesOn<Function>>()) ||
  515. Inv.invalidate<TestFunctionAnalysis>(F, PA);
  516. }
  517. };
  518. TestIndirectFunctionAnalysis(int &Runs) : Runs(Runs) {}
  519. /// Run the analysis pass over the function and return a result.
  520. Result run(Function &F, FunctionAnalysisManager &AM) {
  521. ++Runs;
  522. auto &FDep = AM.getResult<TestFunctionAnalysis>(F);
  523. auto &Proxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
  524. const ModuleAnalysisManager &MAM = Proxy.getManager();
  525. // For the test, we insist that the module analysis starts off in the
  526. // cache.
  527. auto &MDep = *MAM.getCachedResult<TestModuleAnalysis>(*F.getParent());
  528. // And register the dependency as module analysis dependencies have to be
  529. // pre-registered on the proxy.
  530. Proxy.registerOuterAnalysisInvalidation<TestModuleAnalysis,
  531. TestIndirectFunctionAnalysis>();
  532. return Result(FDep, MDep);
  533. }
  534. private:
  535. friend AnalysisInfoMixin<TestIndirectFunctionAnalysis>;
  536. static AnalysisKey Key;
  537. int &Runs;
  538. };
  539. AnalysisKey TestIndirectFunctionAnalysis::Key;
  540. /// A test analysis pass which chaches in its result the result from the above
  541. /// indirect analysis pass.
  542. ///
  543. /// This allows us to ensure that whenever an analysis pass is invalidated due
  544. /// to dependencies (especially dependencies across IR units that trigger
  545. /// asynchronous invalidation) we correctly detect that this may in turn cause
  546. /// other analysis to be invalidated.
  547. struct TestDoublyIndirectFunctionAnalysis
  548. : public AnalysisInfoMixin<TestDoublyIndirectFunctionAnalysis> {
  549. struct Result {
  550. Result(TestIndirectFunctionAnalysis::Result &IDep) : IDep(IDep) {}
  551. TestIndirectFunctionAnalysis::Result &IDep;
  552. bool invalidate(Function &F, const PreservedAnalyses &PA,
  553. FunctionAnalysisManager::Invalidator &Inv) {
  554. auto PAC = PA.getChecker<TestDoublyIndirectFunctionAnalysis>();
  555. return !(PAC.preserved() ||
  556. PAC.preservedSet<AllAnalysesOn<Function>>()) ||
  557. Inv.invalidate<TestIndirectFunctionAnalysis>(F, PA);
  558. }
  559. };
  560. TestDoublyIndirectFunctionAnalysis(int &Runs) : Runs(Runs) {}
  561. /// Run the analysis pass over the function and return a result.
  562. Result run(Function &F, FunctionAnalysisManager &AM) {
  563. ++Runs;
  564. auto &IDep = AM.getResult<TestIndirectFunctionAnalysis>(F);
  565. return Result(IDep);
  566. }
  567. private:
  568. friend AnalysisInfoMixin<TestDoublyIndirectFunctionAnalysis>;
  569. static AnalysisKey Key;
  570. int &Runs;
  571. };
  572. AnalysisKey TestDoublyIndirectFunctionAnalysis::Key;
  573. struct LambdaPass : public PassInfoMixin<LambdaPass> {
  574. using FuncT = std::function<PreservedAnalyses(Function &, FunctionAnalysisManager &)>;
  575. LambdaPass(FuncT Func) : Func(std::move(Func)) {}
  576. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
  577. return Func(F, AM);
  578. }
  579. FuncT Func;
  580. };
  581. TEST_F(PassManagerTest, IndirectAnalysisInvalidation) {
  582. FunctionAnalysisManager FAM(/*DebugLogging*/ true);
  583. int FunctionAnalysisRuns = 0, ModuleAnalysisRuns = 0,
  584. IndirectAnalysisRuns = 0, DoublyIndirectAnalysisRuns = 0;
  585. FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); });
  586. FAM.registerPass(
  587. [&] { return TestIndirectFunctionAnalysis(IndirectAnalysisRuns); });
  588. FAM.registerPass([&] {
  589. return TestDoublyIndirectFunctionAnalysis(DoublyIndirectAnalysisRuns);
  590. });
  591. ModuleAnalysisManager MAM(/*DebugLogging*/ true);
  592. MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); });
  593. MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
  594. FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
  595. PassInstrumentationCallbacks PIC;
  596. MAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
  597. FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
  598. int InstrCount = 0, FunctionCount = 0;
  599. ModulePassManager MPM(/*DebugLogging*/ true);
  600. FunctionPassManager FPM(/*DebugLogging*/ true);
  601. // First just use the analysis to get the instruction count, and preserve
  602. // everything.
  603. FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
  604. auto &DoublyIndirectResult =
  605. AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
  606. auto &IndirectResult = DoublyIndirectResult.IDep;
  607. InstrCount += IndirectResult.FDep.InstructionCount;
  608. FunctionCount += IndirectResult.MDep.FunctionCount;
  609. return PreservedAnalyses::all();
  610. }));
  611. // Next, invalidate
  612. // - both analyses for "f",
  613. // - just the underlying (indirect) analysis for "g", and
  614. // - just the direct analysis for "h".
  615. FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
  616. auto &DoublyIndirectResult =
  617. AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
  618. auto &IndirectResult = DoublyIndirectResult.IDep;
  619. InstrCount += IndirectResult.FDep.InstructionCount;
  620. FunctionCount += IndirectResult.MDep.FunctionCount;
  621. auto PA = PreservedAnalyses::none();
  622. if (F.getName() == "g")
  623. PA.preserve<TestFunctionAnalysis>();
  624. else if (F.getName() == "h")
  625. PA.preserve<TestIndirectFunctionAnalysis>();
  626. return PA;
  627. }));
  628. // Finally, use the analysis again on each function, forcing re-computation
  629. // for all of them.
  630. FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
  631. auto &DoublyIndirectResult =
  632. AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
  633. auto &IndirectResult = DoublyIndirectResult.IDep;
  634. InstrCount += IndirectResult.FDep.InstructionCount;
  635. FunctionCount += IndirectResult.MDep.FunctionCount;
  636. return PreservedAnalyses::all();
  637. }));
  638. // Create a second function pass manager. This will cause the module-level
  639. // invalidation to occur, which will force yet another invalidation of the
  640. // indirect function-level analysis as the module analysis it depends on gets
  641. // invalidated.
  642. FunctionPassManager FPM2(/*DebugLogging*/ true);
  643. FPM2.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
  644. auto &DoublyIndirectResult =
  645. AM.getResult<TestDoublyIndirectFunctionAnalysis>(F);
  646. auto &IndirectResult = DoublyIndirectResult.IDep;
  647. InstrCount += IndirectResult.FDep.InstructionCount;
  648. FunctionCount += IndirectResult.MDep.FunctionCount;
  649. return PreservedAnalyses::all();
  650. }));
  651. // Add a requires pass to populate the module analysis and then our function
  652. // pass pipeline.
  653. MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>());
  654. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  655. // Now require the module analysis again (it will have been invalidated once)
  656. // and then use it again from a function pass manager.
  657. MPM.addPass(RequireAnalysisPass<TestModuleAnalysis, Module>());
  658. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM2)));
  659. MPM.run(*M, MAM);
  660. // There are generally two possible runs for each of the three functions. But
  661. // for one function, we only invalidate the indirect analysis so the base one
  662. // only gets run five times.
  663. EXPECT_EQ(5, FunctionAnalysisRuns);
  664. // The module analysis pass should be run twice here.
  665. EXPECT_EQ(2, ModuleAnalysisRuns);
  666. // The indirect analysis is invalidated for each function (either directly or
  667. // indirectly) and run twice for each.
  668. EXPECT_EQ(9, IndirectAnalysisRuns);
  669. EXPECT_EQ(9, DoublyIndirectAnalysisRuns);
  670. // There are five instructions in the module and we add the count four
  671. // times.
  672. EXPECT_EQ(5 * 4, InstrCount);
  673. // There are three functions and we count them four times for each of the
  674. // three functions.
  675. EXPECT_EQ(3 * 4 * 3, FunctionCount);
  676. }
  677. }