StandardInstrumentations.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StandardInstrumentations.h ------------------------------*- C++ -*--===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. ///
  15. /// This header defines a class that provides bookkeeping for all standard
  16. /// (i.e in-tree) pass instrumentations.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_PASSES_STANDARDINSTRUMENTATIONS_H
  20. #define LLVM_PASSES_STANDARDINSTRUMENTATIONS_H
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/IR/BasicBlock.h"
  25. #include "llvm/IR/OptBisect.h"
  26. #include "llvm/IR/PassTimingInfo.h"
  27. #include "llvm/IR/ValueHandle.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Transforms/IPO/SampleProfileProbe.h"
  30. #include <string>
  31. #include <utility>
  32. namespace llvm {
  33. class Module;
  34. class Function;
  35. class PassInstrumentationCallbacks;
  36. /// Instrumentation to print IR before/after passes.
  37. ///
  38. /// Needs state to be able to print module after pass that invalidates IR unit
  39. /// (typically Loop or SCC).
  40. class PrintIRInstrumentation {
  41. public:
  42. ~PrintIRInstrumentation();
  43. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  44. private:
  45. void printBeforePass(StringRef PassID, Any IR);
  46. void printAfterPass(StringRef PassID, Any IR);
  47. void printAfterPassInvalidated(StringRef PassID);
  48. bool shouldPrintBeforePass(StringRef PassID);
  49. bool shouldPrintAfterPass(StringRef PassID);
  50. using PrintModuleDesc = std::tuple<const Module *, std::string, StringRef>;
  51. void pushModuleDesc(StringRef PassID, Any IR);
  52. PrintModuleDesc popModuleDesc(StringRef PassID);
  53. PassInstrumentationCallbacks *PIC;
  54. /// Stack of Module description, enough to print the module after a given
  55. /// pass.
  56. SmallVector<PrintModuleDesc, 2> ModuleDescStack;
  57. };
  58. class OptNoneInstrumentation {
  59. public:
  60. OptNoneInstrumentation(bool DebugLogging) : DebugLogging(DebugLogging) {}
  61. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  62. private:
  63. bool DebugLogging;
  64. bool shouldRun(StringRef PassID, Any IR);
  65. };
  66. class OptBisectInstrumentation {
  67. public:
  68. OptBisectInstrumentation() = default;
  69. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  70. };
  71. struct PrintPassOptions {
  72. /// Print adaptors and pass managers.
  73. bool Verbose = false;
  74. /// Don't print information for analyses.
  75. bool SkipAnalyses = false;
  76. /// Indent based on hierarchy.
  77. bool Indent = false;
  78. };
  79. // Debug logging for transformation and analysis passes.
  80. class PrintPassInstrumentation {
  81. raw_ostream &print();
  82. public:
  83. PrintPassInstrumentation(bool Enabled, PrintPassOptions Opts)
  84. : Enabled(Enabled), Opts(Opts) {}
  85. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  86. private:
  87. bool Enabled;
  88. PrintPassOptions Opts;
  89. int Indent = 0;
  90. };
  91. class PreservedCFGCheckerInstrumentation {
  92. public:
  93. // Keeps sticky poisoned flag for the given basic block once it has been
  94. // deleted or RAUWed.
  95. struct BBGuard final : public CallbackVH {
  96. BBGuard(const BasicBlock *BB) : CallbackVH(BB) {}
  97. void deleted() override { CallbackVH::deleted(); }
  98. void allUsesReplacedWith(Value *) override { CallbackVH::deleted(); }
  99. bool isPoisoned() const { return !getValPtr(); }
  100. };
  101. // CFG is a map BB -> {(Succ, Multiplicity)}, where BB is a non-leaf basic
  102. // block, {(Succ, Multiplicity)} set of all pairs of the block's successors
  103. // and the multiplicity of the edge (BB->Succ). As the mapped sets are
  104. // unordered the order of successors is not tracked by the CFG. In other words
  105. // this allows basic block successors to be swapped by a pass without
  106. // reporting a CFG change. CFG can be guarded by basic block tracking pointers
  107. // in the Graph (BBGuard). That is if any of the block is deleted or RAUWed
  108. // then the CFG is treated poisoned and no block pointer of the Graph is used.
  109. struct CFG {
  110. Optional<DenseMap<intptr_t, BBGuard>> BBGuards;
  111. DenseMap<const BasicBlock *, DenseMap<const BasicBlock *, unsigned>> Graph;
  112. CFG(const Function *F, bool TrackBBLifetime);
  113. bool operator==(const CFG &G) const {
  114. return !isPoisoned() && !G.isPoisoned() && Graph == G.Graph;
  115. }
  116. bool isPoisoned() const {
  117. return BBGuards &&
  118. std::any_of(BBGuards->begin(), BBGuards->end(),
  119. [](const auto &BB) { return BB.second.isPoisoned(); });
  120. }
  121. static void printDiff(raw_ostream &out, const CFG &Before,
  122. const CFG &After);
  123. bool invalidate(Function &F, const PreservedAnalyses &PA,
  124. FunctionAnalysisManager::Invalidator &);
  125. };
  126. #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
  127. SmallVector<StringRef, 8> PassStack;
  128. #endif
  129. static cl::opt<bool> VerifyPreservedCFG;
  130. void registerCallbacks(PassInstrumentationCallbacks &PIC,
  131. FunctionAnalysisManager &FAM);
  132. };
  133. // Base class for classes that report changes to the IR.
  134. // It presents an interface for such classes and provides calls
  135. // on various events as the new pass manager transforms the IR.
  136. // It also provides filtering of information based on hidden options
  137. // specifying which functions are interesting.
  138. // Calls are made for the following events/queries:
  139. // 1. The initial IR processed.
  140. // 2. To get the representation of the IR (of type \p T).
  141. // 3. When a pass does not change the IR.
  142. // 4. When a pass changes the IR (given both before and after representations
  143. // of type \p T).
  144. // 5. When an IR is invalidated.
  145. // 6. When a pass is run on an IR that is not interesting (based on options).
  146. // 7. When a pass is ignored (pass manager or adapter pass).
  147. // 8. To compare two IR representations (of type \p T).
  148. template <typename IRUnitT> class ChangeReporter {
  149. protected:
  150. ChangeReporter(bool RunInVerboseMode) : VerboseMode(RunInVerboseMode) {}
  151. public:
  152. virtual ~ChangeReporter();
  153. // Determine if this pass/IR is interesting and if so, save the IR
  154. // otherwise it is left on the stack without data.
  155. void saveIRBeforePass(Any IR, StringRef PassID);
  156. // Compare the IR from before the pass after the pass.
  157. void handleIRAfterPass(Any IR, StringRef PassID);
  158. // Handle the situation where a pass is invalidated.
  159. void handleInvalidatedPass(StringRef PassID);
  160. protected:
  161. // Register required callbacks.
  162. void registerRequiredCallbacks(PassInstrumentationCallbacks &PIC);
  163. // Return true when this is a defined function for which printing
  164. // of changes is desired.
  165. bool isInterestingFunction(const Function &F);
  166. // Return true when this is a pass for which printing of changes is desired.
  167. bool isInterestingPass(StringRef PassID);
  168. // Return true when this is a pass on IR for which printing
  169. // of changes is desired.
  170. bool isInteresting(Any IR, StringRef PassID);
  171. // Called on the first IR processed.
  172. virtual void handleInitialIR(Any IR) = 0;
  173. // Called before and after a pass to get the representation of the IR.
  174. virtual void generateIRRepresentation(Any IR, StringRef PassID,
  175. IRUnitT &Output) = 0;
  176. // Called when the pass is not iteresting.
  177. virtual void omitAfter(StringRef PassID, std::string &Name) = 0;
  178. // Called when an interesting IR has changed.
  179. virtual void handleAfter(StringRef PassID, std::string &Name,
  180. const IRUnitT &Before, const IRUnitT &After,
  181. Any) = 0;
  182. // Called when an interesting pass is invalidated.
  183. virtual void handleInvalidated(StringRef PassID) = 0;
  184. // Called when the IR or pass is not interesting.
  185. virtual void handleFiltered(StringRef PassID, std::string &Name) = 0;
  186. // Called when an ignored pass is encountered.
  187. virtual void handleIgnored(StringRef PassID, std::string &Name) = 0;
  188. // Stack of IRs before passes.
  189. std::vector<IRUnitT> BeforeStack;
  190. // Is this the first IR seen?
  191. bool InitialIR = true;
  192. // Run in verbose mode, printing everything?
  193. const bool VerboseMode;
  194. };
  195. // An abstract template base class that handles printing banners and
  196. // reporting when things have not changed or are filtered out.
  197. template <typename IRUnitT>
  198. class TextChangeReporter : public ChangeReporter<IRUnitT> {
  199. protected:
  200. TextChangeReporter(bool Verbose);
  201. // Print a module dump of the first IR that is changed.
  202. void handleInitialIR(Any IR) override;
  203. // Report that the IR was omitted because it did not change.
  204. void omitAfter(StringRef PassID, std::string &Name) override;
  205. // Report that the pass was invalidated.
  206. void handleInvalidated(StringRef PassID) override;
  207. // Report that the IR was filtered out.
  208. void handleFiltered(StringRef PassID, std::string &Name) override;
  209. // Report that the pass was ignored.
  210. void handleIgnored(StringRef PassID, std::string &Name) override;
  211. // Make substitutions in \p S suitable for reporting changes
  212. // after the pass and then print it.
  213. raw_ostream &Out;
  214. };
  215. // A change printer based on the string representation of the IR as created
  216. // by unwrapAndPrint. The string representation is stored in a std::string
  217. // to preserve it as the IR changes in each pass. Note that the banner is
  218. // included in this representation but it is massaged before reporting.
  219. class IRChangedPrinter : public TextChangeReporter<std::string> {
  220. public:
  221. IRChangedPrinter(bool VerboseMode)
  222. : TextChangeReporter<std::string>(VerboseMode) {}
  223. ~IRChangedPrinter() override;
  224. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  225. protected:
  226. // Called before and after a pass to get the representation of the IR.
  227. void generateIRRepresentation(Any IR, StringRef PassID,
  228. std::string &Output) override;
  229. // Called when an interesting IR has changed.
  230. void handleAfter(StringRef PassID, std::string &Name,
  231. const std::string &Before, const std::string &After,
  232. Any) override;
  233. };
  234. // Information that needs to be saved for a basic block in order to compare
  235. // before and after the pass to determine if it was changed by a pass.
  236. template <typename T> class BlockDataT {
  237. public:
  238. BlockDataT(const BasicBlock &B) : Label(B.getName().str()), Data(B) {
  239. raw_string_ostream SS(Body);
  240. B.print(SS, nullptr, true, true);
  241. }
  242. bool operator==(const BlockDataT &That) const { return Body == That.Body; }
  243. bool operator!=(const BlockDataT &That) const { return Body != That.Body; }
  244. // Return the label of the represented basic block.
  245. StringRef getLabel() const { return Label; }
  246. // Return the string representation of the basic block.
  247. StringRef getBody() const { return Body; }
  248. // Return the associated data
  249. const T &getData() const { return Data; }
  250. protected:
  251. std::string Label;
  252. std::string Body;
  253. // Extra data associated with a basic block
  254. T Data;
  255. };
  256. template <typename T> class OrderedChangedData {
  257. public:
  258. // Return the names in the order they were saved
  259. std::vector<std::string> &getOrder() { return Order; }
  260. const std::vector<std::string> &getOrder() const { return Order; }
  261. // Return a map of names to saved representations
  262. StringMap<T> &getData() { return Data; }
  263. const StringMap<T> &getData() const { return Data; }
  264. bool operator==(const OrderedChangedData<T> &That) const {
  265. return Data == That.getData();
  266. }
  267. // Call the lambda \p HandlePair on each corresponding pair of data from
  268. // \p Before and \p After. The order is based on the order in \p After
  269. // with ones that are only in \p Before interspersed based on where they
  270. // occur in \p Before. This is used to present the output in an order
  271. // based on how the data is ordered in LLVM.
  272. static void report(const OrderedChangedData &Before,
  273. const OrderedChangedData &After,
  274. function_ref<void(const T *, const T *)> HandlePair);
  275. protected:
  276. std::vector<std::string> Order;
  277. StringMap<T> Data;
  278. };
  279. // Do not need extra information for patch-style change reporter.
  280. class EmptyData {
  281. public:
  282. EmptyData(const BasicBlock &) {}
  283. };
  284. // The data saved for comparing functions.
  285. template <typename T>
  286. class FuncDataT : public OrderedChangedData<BlockDataT<T>> {
  287. public:
  288. FuncDataT(std::string S) : EntryBlockName(S) {}
  289. // Return the name of the entry block
  290. std::string getEntryBlockName() const { return EntryBlockName; }
  291. protected:
  292. std::string EntryBlockName;
  293. };
  294. // The data saved for comparing IRs.
  295. template <typename T>
  296. class IRDataT : public OrderedChangedData<FuncDataT<T>> {};
  297. // Abstract template base class for a class that compares two IRs. The
  298. // class is created with the 2 IRs to compare and then compare is called.
  299. // The static function analyzeIR is used to build up the IR representation.
  300. template <typename T> class IRComparer {
  301. public:
  302. IRComparer(const IRDataT<T> &Before, const IRDataT<T> &After)
  303. : Before(Before), After(After) {}
  304. // Compare the 2 IRs. \p handleFunctionCompare is called to handle the
  305. // compare of a function. When \p InModule is set,
  306. // this function is being handled as part of comparing a module.
  307. void compare(
  308. bool CompareModule,
  309. std::function<void(bool InModule, unsigned Minor,
  310. const FuncDataT<T> &Before, const FuncDataT<T> &After)>
  311. CompareFunc);
  312. // Analyze \p IR and build the IR representation in \p Data.
  313. static void analyzeIR(Any IR, IRDataT<T> &Data);
  314. protected:
  315. // Generate the data for \p F into \p Data.
  316. static bool generateFunctionData(IRDataT<T> &Data, const Function &F);
  317. const IRDataT<T> &Before;
  318. const IRDataT<T> &After;
  319. };
  320. // A change printer that prints out in-line differences in the basic
  321. // blocks. It uses an InlineComparer to do the comparison so it shows
  322. // the differences prefixed with '-' and '+' for code that is removed
  323. // and added, respectively. Changes to the IR that do not affect basic
  324. // blocks are not reported as having changed the IR. The option
  325. // -print-module-scope does not affect this change reporter.
  326. class InLineChangePrinter : public TextChangeReporter<IRDataT<EmptyData>> {
  327. public:
  328. InLineChangePrinter(bool VerboseMode, bool ColourMode)
  329. : TextChangeReporter<IRDataT<EmptyData>>(VerboseMode),
  330. UseColour(ColourMode) {}
  331. ~InLineChangePrinter() override;
  332. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  333. protected:
  334. // Create a representation of the IR.
  335. virtual void generateIRRepresentation(Any IR, StringRef PassID,
  336. IRDataT<EmptyData> &Output) override;
  337. // Called when an interesting IR has changed.
  338. virtual void handleAfter(StringRef PassID, std::string &Name,
  339. const IRDataT<EmptyData> &Before,
  340. const IRDataT<EmptyData> &After, Any) override;
  341. void handleFunctionCompare(StringRef Name, StringRef Prefix, StringRef PassID,
  342. StringRef Divider, bool InModule, unsigned Minor,
  343. const FuncDataT<EmptyData> &Before,
  344. const FuncDataT<EmptyData> &After);
  345. bool UseColour;
  346. };
  347. class VerifyInstrumentation {
  348. bool DebugLogging;
  349. public:
  350. VerifyInstrumentation(bool DebugLogging) : DebugLogging(DebugLogging) {}
  351. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  352. };
  353. // Class that holds transitions between basic blocks. The transitions
  354. // are contained in a map of values to names of basic blocks.
  355. class DCData {
  356. public:
  357. // Fill the map with the transitions from basic block \p B.
  358. DCData(const BasicBlock &B);
  359. // Return an iterator to the names of the successor blocks.
  360. StringMap<std::string>::const_iterator begin() const {
  361. return Successors.begin();
  362. }
  363. StringMap<std::string>::const_iterator end() const {
  364. return Successors.end();
  365. }
  366. // Return the label of the basic block reached on a transition on \p S.
  367. StringRef getSuccessorLabel(StringRef S) const {
  368. assert(Successors.count(S) == 1 && "Expected to find successor.");
  369. return Successors.find(S)->getValue();
  370. }
  371. protected:
  372. // Add a transition to \p Succ on \p Label
  373. void addSuccessorLabel(StringRef Succ, StringRef Label) {
  374. std::pair<std::string, std::string> SS{Succ.str(), Label.str()};
  375. Successors.insert(SS);
  376. }
  377. StringMap<std::string> Successors;
  378. };
  379. // A change reporter that builds a website with links to pdf files showing
  380. // dot control flow graphs with changed instructions shown in colour.
  381. class DotCfgChangeReporter : public ChangeReporter<IRDataT<DCData>> {
  382. public:
  383. DotCfgChangeReporter(bool Verbose);
  384. ~DotCfgChangeReporter() override;
  385. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  386. protected:
  387. // Initialize the HTML file and output the header.
  388. bool initializeHTML();
  389. // Called on the first IR processed.
  390. void handleInitialIR(Any IR) override;
  391. // Called before and after a pass to get the representation of the IR.
  392. void generateIRRepresentation(Any IR, StringRef PassID,
  393. IRDataT<DCData> &Output) override;
  394. // Called when the pass is not iteresting.
  395. void omitAfter(StringRef PassID, std::string &Name) override;
  396. // Called when an interesting IR has changed.
  397. void handleAfter(StringRef PassID, std::string &Name,
  398. const IRDataT<DCData> &Before, const IRDataT<DCData> &After,
  399. Any) override;
  400. // Called when an interesting pass is invalidated.
  401. void handleInvalidated(StringRef PassID) override;
  402. // Called when the IR or pass is not interesting.
  403. void handleFiltered(StringRef PassID, std::string &Name) override;
  404. // Called when an ignored pass is encountered.
  405. void handleIgnored(StringRef PassID, std::string &Name) override;
  406. // Generate the pdf file into \p Dir / \p PDFFileName using \p DotFile as
  407. // input and return the html <a> tag with \Text as the content.
  408. static std::string genHTML(StringRef Text, StringRef DotFile,
  409. StringRef PDFFileName);
  410. void handleFunctionCompare(StringRef Name, StringRef Prefix, StringRef PassID,
  411. StringRef Divider, bool InModule, unsigned Minor,
  412. const FuncDataT<DCData> &Before,
  413. const FuncDataT<DCData> &After);
  414. unsigned N = 0;
  415. std::unique_ptr<raw_fd_ostream> HTML;
  416. };
  417. /// This class provides an interface to register all the standard pass
  418. /// instrumentations and manages their state (if any).
  419. class StandardInstrumentations {
  420. PrintIRInstrumentation PrintIR;
  421. PrintPassInstrumentation PrintPass;
  422. TimePassesHandler TimePasses;
  423. OptNoneInstrumentation OptNone;
  424. OptBisectInstrumentation OptBisect;
  425. PreservedCFGCheckerInstrumentation PreservedCFGChecker;
  426. IRChangedPrinter PrintChangedIR;
  427. PseudoProbeVerifier PseudoProbeVerification;
  428. InLineChangePrinter PrintChangedDiff;
  429. DotCfgChangeReporter WebsiteChangeReporter;
  430. VerifyInstrumentation Verify;
  431. bool VerifyEach;
  432. public:
  433. StandardInstrumentations(bool DebugLogging, bool VerifyEach = false,
  434. PrintPassOptions PrintPassOpts = PrintPassOptions());
  435. // Register all the standard instrumentation callbacks. If \p FAM is nullptr
  436. // then PreservedCFGChecker is not enabled.
  437. void registerCallbacks(PassInstrumentationCallbacks &PIC,
  438. FunctionAnalysisManager *FAM = nullptr);
  439. TimePassesHandler &getTimePasses() { return TimePasses; }
  440. };
  441. extern template class ChangeReporter<std::string>;
  442. extern template class TextChangeReporter<std::string>;
  443. extern template class BlockDataT<EmptyData>;
  444. extern template class FuncDataT<EmptyData>;
  445. extern template class IRDataT<EmptyData>;
  446. extern template class ChangeReporter<IRDataT<EmptyData>>;
  447. extern template class TextChangeReporter<IRDataT<EmptyData>>;
  448. extern template class IRComparer<EmptyData>;
  449. } // namespace llvm
  450. #endif
  451. #ifdef __GNUC__
  452. #pragma GCC diagnostic pop
  453. #endif