StandardInstrumentations.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. //===- Standard pass instrumentations handling ----------------*- C++ -*--===//
  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. /// \file
  9. ///
  10. /// This file defines IR-printing pass instrumentation callbacks as well as
  11. /// StandardInstrumentations class that manages standard pass instrumentations.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Passes/StandardInstrumentations.h"
  15. #include "llvm/ADT/Any.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Analysis/CallGraphSCCPass.h"
  18. #include "llvm/Analysis/LazyCallGraph.h"
  19. #include "llvm/Analysis/LoopInfo.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/Function.h"
  22. #include "llvm/IR/LegacyPassManager.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IR/PassInstrumentation.h"
  25. #include "llvm/IR/PassManager.h"
  26. #include "llvm/IR/PrintPasses.h"
  27. #include "llvm/IR/Verifier.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/CrashRecoveryContext.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/Error.h"
  32. #include "llvm/Support/FormatVariadic.h"
  33. #include "llvm/Support/GraphWriter.h"
  34. #include "llvm/Support/MemoryBuffer.h"
  35. #include "llvm/Support/Program.h"
  36. #include "llvm/Support/Regex.h"
  37. #include "llvm/Support/Signals.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include <unordered_map>
  40. #include <unordered_set>
  41. #include <utility>
  42. #include <vector>
  43. using namespace llvm;
  44. cl::opt<bool> PreservedCFGCheckerInstrumentation::VerifyPreservedCFG(
  45. "verify-cfg-preserved", cl::Hidden,
  46. #ifdef NDEBUG
  47. cl::init(false)
  48. #else
  49. cl::init(true)
  50. #endif
  51. );
  52. // An option that supports the -print-changed option. See
  53. // the description for -print-changed for an explanation of the use
  54. // of this option. Note that this option has no effect without -print-changed.
  55. static cl::opt<bool>
  56. PrintChangedBefore("print-before-changed",
  57. cl::desc("Print before passes that change them"),
  58. cl::init(false), cl::Hidden);
  59. // An option for specifying the dot used by
  60. // print-changed=[dot-cfg | dot-cfg-quiet]
  61. static cl::opt<std::string>
  62. DotBinary("print-changed-dot-path", cl::Hidden, cl::init("dot"),
  63. cl::desc("system dot used by change reporters"));
  64. // An option that determines the colour used for elements that are only
  65. // in the before part. Must be a colour named in appendix J of
  66. // https://graphviz.org/pdf/dotguide.pdf
  67. static cl::opt<std::string>
  68. BeforeColour("dot-cfg-before-color",
  69. cl::desc("Color for dot-cfg before elements"), cl::Hidden,
  70. cl::init("red"));
  71. // An option that determines the colour used for elements that are only
  72. // in the after part. Must be a colour named in appendix J of
  73. // https://graphviz.org/pdf/dotguide.pdf
  74. static cl::opt<std::string>
  75. AfterColour("dot-cfg-after-color",
  76. cl::desc("Color for dot-cfg after elements"), cl::Hidden,
  77. cl::init("forestgreen"));
  78. // An option that determines the colour used for elements that are in both
  79. // the before and after parts. Must be a colour named in appendix J of
  80. // https://graphviz.org/pdf/dotguide.pdf
  81. static cl::opt<std::string>
  82. CommonColour("dot-cfg-common-color",
  83. cl::desc("Color for dot-cfg common elements"), cl::Hidden,
  84. cl::init("black"));
  85. // An option that determines where the generated website file (named
  86. // passes.html) and the associated pdf files (named diff_*.pdf) are saved.
  87. static cl::opt<std::string> DotCfgDir(
  88. "dot-cfg-dir",
  89. cl::desc("Generate dot files into specified directory for changed IRs"),
  90. cl::Hidden, cl::init("./"));
  91. // An option to print the IR that was being processed when a pass crashes.
  92. static cl::opt<bool>
  93. PrintCrashIR("print-on-crash",
  94. cl::desc("Print the last form of the IR before crash"),
  95. cl::Hidden);
  96. static cl::opt<std::string> OptBisectPrintIRPath(
  97. "opt-bisect-print-ir-path",
  98. cl::desc("Print IR to path when opt-bisect-limit is reached"), cl::Hidden);
  99. namespace {
  100. // An option for specifying an executable that will be called with the IR
  101. // everytime it changes in the opt pipeline. It will also be called on
  102. // the initial IR as it enters the pipeline. The executable will be passed
  103. // the name of a temporary file containing the IR and the PassID. This may
  104. // be used, for example, to call llc on the IR and run a test to determine
  105. // which pass makes a change that changes the functioning of the IR.
  106. // The usual modifier options work as expected.
  107. static cl::opt<std::string>
  108. TestChanged("exec-on-ir-change", cl::Hidden, cl::init(""),
  109. cl::desc("exe called with module IR after each pass that "
  110. "changes it"));
  111. /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
  112. /// certain global filters. Will never return nullptr if \p Force is true.
  113. const Module *unwrapModule(Any IR, bool Force = false) {
  114. if (const auto **M = any_cast<const Module *>(&IR))
  115. return *M;
  116. if (const auto **F = any_cast<const Function *>(&IR)) {
  117. if (!Force && !isFunctionInPrintList((*F)->getName()))
  118. return nullptr;
  119. return (*F)->getParent();
  120. }
  121. if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
  122. for (const LazyCallGraph::Node &N : **C) {
  123. const Function &F = N.getFunction();
  124. if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) {
  125. return F.getParent();
  126. }
  127. }
  128. assert(!Force && "Expected a module");
  129. return nullptr;
  130. }
  131. if (const auto **L = any_cast<const Loop *>(&IR)) {
  132. const Function *F = (*L)->getHeader()->getParent();
  133. if (!Force && !isFunctionInPrintList(F->getName()))
  134. return nullptr;
  135. return F->getParent();
  136. }
  137. llvm_unreachable("Unknown IR unit");
  138. }
  139. void printIR(raw_ostream &OS, const Function *F) {
  140. if (!isFunctionInPrintList(F->getName()))
  141. return;
  142. OS << *F;
  143. }
  144. void printIR(raw_ostream &OS, const Module *M) {
  145. if (isFunctionInPrintList("*") || forcePrintModuleIR()) {
  146. M->print(OS, nullptr);
  147. } else {
  148. for (const auto &F : M->functions()) {
  149. printIR(OS, &F);
  150. }
  151. }
  152. }
  153. void printIR(raw_ostream &OS, const LazyCallGraph::SCC *C) {
  154. for (const LazyCallGraph::Node &N : *C) {
  155. const Function &F = N.getFunction();
  156. if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
  157. F.print(OS);
  158. }
  159. }
  160. }
  161. void printIR(raw_ostream &OS, const Loop *L) {
  162. const Function *F = L->getHeader()->getParent();
  163. if (!isFunctionInPrintList(F->getName()))
  164. return;
  165. printLoop(const_cast<Loop &>(*L), OS);
  166. }
  167. std::string getIRName(Any IR) {
  168. if (any_cast<const Module *>(&IR))
  169. return "[module]";
  170. if (const auto **F = any_cast<const Function *>(&IR))
  171. return (*F)->getName().str();
  172. if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
  173. return (*C)->getName();
  174. if (const auto **L = any_cast<const Loop *>(&IR))
  175. return (*L)->getName().str();
  176. llvm_unreachable("Unknown wrapped IR type");
  177. }
  178. bool moduleContainsFilterPrintFunc(const Module &M) {
  179. return any_of(M.functions(),
  180. [](const Function &F) {
  181. return isFunctionInPrintList(F.getName());
  182. }) ||
  183. isFunctionInPrintList("*");
  184. }
  185. bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
  186. return any_of(C,
  187. [](const LazyCallGraph::Node &N) {
  188. return isFunctionInPrintList(N.getName());
  189. }) ||
  190. isFunctionInPrintList("*");
  191. }
  192. bool shouldPrintIR(Any IR) {
  193. if (const auto **M = any_cast<const Module *>(&IR))
  194. return moduleContainsFilterPrintFunc(**M);
  195. if (const auto **F = any_cast<const Function *>(&IR))
  196. return isFunctionInPrintList((*F)->getName());
  197. if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
  198. return sccContainsFilterPrintFunc(**C);
  199. if (const auto **L = any_cast<const Loop *>(&IR))
  200. return isFunctionInPrintList((*L)->getHeader()->getParent()->getName());
  201. llvm_unreachable("Unknown wrapped IR type");
  202. }
  203. /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
  204. /// Any and does actual print job.
  205. void unwrapAndPrint(raw_ostream &OS, Any IR) {
  206. if (!shouldPrintIR(IR))
  207. return;
  208. if (forcePrintModuleIR()) {
  209. auto *M = unwrapModule(IR);
  210. assert(M && "should have unwrapped module");
  211. printIR(OS, M);
  212. return;
  213. }
  214. if (const auto **M = any_cast<const Module *>(&IR)) {
  215. printIR(OS, *M);
  216. return;
  217. }
  218. if (const auto **F = any_cast<const Function *>(&IR)) {
  219. printIR(OS, *F);
  220. return;
  221. }
  222. if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
  223. printIR(OS, *C);
  224. return;
  225. }
  226. if (const auto **L = any_cast<const Loop *>(&IR)) {
  227. printIR(OS, *L);
  228. return;
  229. }
  230. llvm_unreachable("Unknown wrapped IR type");
  231. }
  232. // Return true when this is a pass for which changes should be ignored
  233. bool isIgnored(StringRef PassID) {
  234. return isSpecialPass(PassID,
  235. {"PassManager", "PassAdaptor", "AnalysisManagerProxy",
  236. "DevirtSCCRepeatedPass", "ModuleInlinerWrapperPass"});
  237. }
  238. std::string makeHTMLReady(StringRef SR) {
  239. std::string S;
  240. while (true) {
  241. StringRef Clean =
  242. SR.take_until([](char C) { return C == '<' || C == '>'; });
  243. S.append(Clean.str());
  244. SR = SR.drop_front(Clean.size());
  245. if (SR.size() == 0)
  246. return S;
  247. S.append(SR[0] == '<' ? "&lt;" : "&gt;");
  248. SR = SR.drop_front();
  249. }
  250. llvm_unreachable("problems converting string to HTML");
  251. }
  252. // Return the module when that is the appropriate level of comparison for \p IR.
  253. const Module *getModuleForComparison(Any IR) {
  254. if (const auto **M = any_cast<const Module *>(&IR))
  255. return *M;
  256. if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
  257. return (*C)
  258. ->begin()
  259. ->getFunction()
  260. .getParent();
  261. return nullptr;
  262. }
  263. bool isInterestingFunction(const Function &F) {
  264. return isFunctionInPrintList(F.getName());
  265. }
  266. // Return true when this is a pass on IR for which printing
  267. // of changes is desired.
  268. bool isInteresting(Any IR, StringRef PassID, StringRef PassName) {
  269. if (isIgnored(PassID) || !isPassInPrintList(PassName))
  270. return false;
  271. if (const auto **F = any_cast<const Function *>(&IR))
  272. return isInterestingFunction(**F);
  273. return true;
  274. }
  275. } // namespace
  276. template <typename T> ChangeReporter<T>::~ChangeReporter() {
  277. assert(BeforeStack.empty() && "Problem with Change Printer stack.");
  278. }
  279. template <typename T>
  280. void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID,
  281. StringRef PassName) {
  282. // Is this the initial IR?
  283. if (InitialIR) {
  284. InitialIR = false;
  285. if (VerboseMode)
  286. handleInitialIR(IR);
  287. }
  288. // Always need to place something on the stack because invalidated passes
  289. // are not given the IR so it cannot be determined whether the pass was for
  290. // something that was filtered out.
  291. BeforeStack.emplace_back();
  292. if (!isInteresting(IR, PassID, PassName))
  293. return;
  294. // Save the IR representation on the stack.
  295. T &Data = BeforeStack.back();
  296. generateIRRepresentation(IR, PassID, Data);
  297. }
  298. template <typename T>
  299. void ChangeReporter<T>::handleIRAfterPass(Any IR, StringRef PassID,
  300. StringRef PassName) {
  301. assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
  302. std::string Name = getIRName(IR);
  303. if (isIgnored(PassID)) {
  304. if (VerboseMode)
  305. handleIgnored(PassID, Name);
  306. } else if (!isInteresting(IR, PassID, PassName)) {
  307. if (VerboseMode)
  308. handleFiltered(PassID, Name);
  309. } else {
  310. // Get the before rep from the stack
  311. T &Before = BeforeStack.back();
  312. // Create the after rep
  313. T After;
  314. generateIRRepresentation(IR, PassID, After);
  315. // Was there a change in IR?
  316. if (Before == After) {
  317. if (VerboseMode)
  318. omitAfter(PassID, Name);
  319. } else
  320. handleAfter(PassID, Name, Before, After, IR);
  321. }
  322. BeforeStack.pop_back();
  323. }
  324. template <typename T>
  325. void ChangeReporter<T>::handleInvalidatedPass(StringRef PassID) {
  326. assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
  327. // Always flag it as invalidated as we cannot determine when
  328. // a pass for a filtered function is invalidated since we do not
  329. // get the IR in the call. Also, the output is just alternate
  330. // forms of the banner anyway.
  331. if (VerboseMode)
  332. handleInvalidated(PassID);
  333. BeforeStack.pop_back();
  334. }
  335. template <typename T>
  336. void ChangeReporter<T>::registerRequiredCallbacks(
  337. PassInstrumentationCallbacks &PIC) {
  338. PIC.registerBeforeNonSkippedPassCallback([&PIC, this](StringRef P, Any IR) {
  339. saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P));
  340. });
  341. PIC.registerAfterPassCallback(
  342. [&PIC, this](StringRef P, Any IR, const PreservedAnalyses &) {
  343. handleIRAfterPass(IR, P, PIC.getPassNameForClassName(P));
  344. });
  345. PIC.registerAfterPassInvalidatedCallback(
  346. [this](StringRef P, const PreservedAnalyses &) {
  347. handleInvalidatedPass(P);
  348. });
  349. }
  350. template <typename T>
  351. TextChangeReporter<T>::TextChangeReporter(bool Verbose)
  352. : ChangeReporter<T>(Verbose), Out(dbgs()) {}
  353. template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) {
  354. // Always print the module.
  355. // Unwrap and print directly to avoid filtering problems in general routines.
  356. auto *M = unwrapModule(IR, /*Force=*/true);
  357. assert(M && "Expected module to be unwrapped when forced.");
  358. Out << "*** IR Dump At Start ***\n";
  359. M->print(Out, nullptr);
  360. }
  361. template <typename T>
  362. void TextChangeReporter<T>::omitAfter(StringRef PassID, std::string &Name) {
  363. Out << formatv("*** IR Dump After {0} on {1} omitted because no change ***\n",
  364. PassID, Name);
  365. }
  366. template <typename T>
  367. void TextChangeReporter<T>::handleInvalidated(StringRef PassID) {
  368. Out << formatv("*** IR Pass {0} invalidated ***\n", PassID);
  369. }
  370. template <typename T>
  371. void TextChangeReporter<T>::handleFiltered(StringRef PassID,
  372. std::string &Name) {
  373. SmallString<20> Banner =
  374. formatv("*** IR Dump After {0} on {1} filtered out ***\n", PassID, Name);
  375. Out << Banner;
  376. }
  377. template <typename T>
  378. void TextChangeReporter<T>::handleIgnored(StringRef PassID, std::string &Name) {
  379. Out << formatv("*** IR Pass {0} on {1} ignored ***\n", PassID, Name);
  380. }
  381. IRChangedPrinter::~IRChangedPrinter() = default;
  382. void IRChangedPrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
  383. if (PrintChanged == ChangePrinter::Verbose ||
  384. PrintChanged == ChangePrinter::Quiet)
  385. TextChangeReporter<std::string>::registerRequiredCallbacks(PIC);
  386. }
  387. void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
  388. std::string &Output) {
  389. raw_string_ostream OS(Output);
  390. unwrapAndPrint(OS, IR);
  391. OS.str();
  392. }
  393. void IRChangedPrinter::handleAfter(StringRef PassID, std::string &Name,
  394. const std::string &Before,
  395. const std::string &After, Any) {
  396. // Report the IR before the changes when requested.
  397. if (PrintChangedBefore)
  398. Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n"
  399. << Before;
  400. // We might not get anything to print if we only want to print a specific
  401. // function but it gets deleted.
  402. if (After.empty()) {
  403. Out << "*** IR Deleted After " << PassID << " on " << Name << " ***\n";
  404. return;
  405. }
  406. Out << "*** IR Dump After " << PassID << " on " << Name << " ***\n" << After;
  407. }
  408. IRChangedTester::~IRChangedTester() {}
  409. void IRChangedTester::registerCallbacks(PassInstrumentationCallbacks &PIC) {
  410. if (TestChanged != "")
  411. TextChangeReporter<std::string>::registerRequiredCallbacks(PIC);
  412. }
  413. void IRChangedTester::handleIR(const std::string &S, StringRef PassID) {
  414. // Store the body into a temporary file
  415. static SmallVector<int> FD{-1};
  416. SmallVector<StringRef> SR{S};
  417. static SmallVector<std::string> FileName{""};
  418. if (auto Err = prepareTempFiles(FD, SR, FileName)) {
  419. dbgs() << "Unable to create temporary file.";
  420. return;
  421. }
  422. static ErrorOr<std::string> Exe = sys::findProgramByName(TestChanged);
  423. if (!Exe) {
  424. dbgs() << "Unable to find test-changed executable.";
  425. return;
  426. }
  427. StringRef Args[] = {TestChanged, FileName[0], PassID};
  428. int Result = sys::ExecuteAndWait(*Exe, Args);
  429. if (Result < 0) {
  430. dbgs() << "Error executing test-changed executable.";
  431. return;
  432. }
  433. if (auto Err = cleanUpTempFiles(FileName))
  434. dbgs() << "Unable to remove temporary file.";
  435. }
  436. void IRChangedTester::handleInitialIR(Any IR) {
  437. // Always test the initial module.
  438. // Unwrap and print directly to avoid filtering problems in general routines.
  439. std::string S;
  440. generateIRRepresentation(IR, "Initial IR", S);
  441. handleIR(S, "Initial IR");
  442. }
  443. void IRChangedTester::omitAfter(StringRef PassID, std::string &Name) {}
  444. void IRChangedTester::handleInvalidated(StringRef PassID) {}
  445. void IRChangedTester::handleFiltered(StringRef PassID, std::string &Name) {}
  446. void IRChangedTester::handleIgnored(StringRef PassID, std::string &Name) {}
  447. void IRChangedTester::handleAfter(StringRef PassID, std::string &Name,
  448. const std::string &Before,
  449. const std::string &After, Any) {
  450. handleIR(After, PassID);
  451. }
  452. template <typename T>
  453. void OrderedChangedData<T>::report(
  454. const OrderedChangedData &Before, const OrderedChangedData &After,
  455. function_ref<void(const T *, const T *)> HandlePair) {
  456. const auto &BFD = Before.getData();
  457. const auto &AFD = After.getData();
  458. std::vector<std::string>::const_iterator BI = Before.getOrder().begin();
  459. std::vector<std::string>::const_iterator BE = Before.getOrder().end();
  460. std::vector<std::string>::const_iterator AI = After.getOrder().begin();
  461. std::vector<std::string>::const_iterator AE = After.getOrder().end();
  462. auto HandlePotentiallyRemovedData = [&](std::string S) {
  463. // The order in LLVM may have changed so check if still exists.
  464. if (!AFD.count(S)) {
  465. // This has been removed.
  466. HandlePair(&BFD.find(*BI)->getValue(), nullptr);
  467. }
  468. };
  469. auto HandleNewData = [&](std::vector<const T *> &Q) {
  470. // Print out any queued up new sections
  471. for (const T *NBI : Q)
  472. HandlePair(nullptr, NBI);
  473. Q.clear();
  474. };
  475. // Print out the data in the after order, with before ones interspersed
  476. // appropriately (ie, somewhere near where they were in the before list).
  477. // Start at the beginning of both lists. Loop through the
  478. // after list. If an element is common, then advance in the before list
  479. // reporting the removed ones until the common one is reached. Report any
  480. // queued up new ones and then report the common one. If an element is not
  481. // common, then enqueue it for reporting. When the after list is exhausted,
  482. // loop through the before list, reporting any removed ones. Finally,
  483. // report the rest of the enqueued new ones.
  484. std::vector<const T *> NewDataQueue;
  485. while (AI != AE) {
  486. if (!BFD.count(*AI)) {
  487. // This section is new so place it in the queue. This will cause it
  488. // to be reported after deleted sections.
  489. NewDataQueue.emplace_back(&AFD.find(*AI)->getValue());
  490. ++AI;
  491. continue;
  492. }
  493. // This section is in both; advance and print out any before-only
  494. // until we get to it.
  495. // It's possible that this section has moved to be later than before. This
  496. // will mess up printing most blocks side by side, but it's a rare case and
  497. // it's better than crashing.
  498. while (BI != BE && *BI != *AI) {
  499. HandlePotentiallyRemovedData(*BI);
  500. ++BI;
  501. }
  502. // Report any new sections that were queued up and waiting.
  503. HandleNewData(NewDataQueue);
  504. const T &AData = AFD.find(*AI)->getValue();
  505. const T &BData = BFD.find(*AI)->getValue();
  506. HandlePair(&BData, &AData);
  507. if (BI != BE)
  508. ++BI;
  509. ++AI;
  510. }
  511. // Check any remaining before sections to see if they have been removed
  512. while (BI != BE) {
  513. HandlePotentiallyRemovedData(*BI);
  514. ++BI;
  515. }
  516. HandleNewData(NewDataQueue);
  517. }
  518. template <typename T>
  519. void IRComparer<T>::compare(
  520. bool CompareModule,
  521. std::function<void(bool InModule, unsigned Minor,
  522. const FuncDataT<T> &Before, const FuncDataT<T> &After)>
  523. CompareFunc) {
  524. if (!CompareModule) {
  525. // Just handle the single function.
  526. assert(Before.getData().size() == 1 && After.getData().size() == 1 &&
  527. "Expected only one function.");
  528. CompareFunc(false, 0, Before.getData().begin()->getValue(),
  529. After.getData().begin()->getValue());
  530. return;
  531. }
  532. unsigned Minor = 0;
  533. FuncDataT<T> Missing("");
  534. IRDataT<T>::report(Before, After,
  535. [&](const FuncDataT<T> *B, const FuncDataT<T> *A) {
  536. assert((B || A) && "Both functions cannot be missing.");
  537. if (!B)
  538. B = &Missing;
  539. else if (!A)
  540. A = &Missing;
  541. CompareFunc(true, Minor++, *B, *A);
  542. });
  543. }
  544. template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) {
  545. if (const Module *M = getModuleForComparison(IR)) {
  546. // Create data for each existing/interesting function in the module.
  547. for (const Function &F : *M)
  548. generateFunctionData(Data, F);
  549. return;
  550. }
  551. const Function **FPtr = any_cast<const Function *>(&IR);
  552. const Function *F = FPtr ? *FPtr : nullptr;
  553. if (!F) {
  554. const Loop **L = any_cast<const Loop *>(&IR);
  555. assert(L && "Unknown IR unit.");
  556. F = (*L)->getHeader()->getParent();
  557. }
  558. assert(F && "Unknown IR unit.");
  559. generateFunctionData(Data, *F);
  560. }
  561. template <typename T>
  562. bool IRComparer<T>::generateFunctionData(IRDataT<T> &Data, const Function &F) {
  563. if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
  564. FuncDataT<T> FD(F.getEntryBlock().getName().str());
  565. int I = 0;
  566. for (const auto &B : F) {
  567. std::string BBName = B.getName().str();
  568. if (BBName.empty()) {
  569. BBName = formatv("{0}", I);
  570. ++I;
  571. }
  572. FD.getOrder().emplace_back(BBName);
  573. FD.getData().insert({BBName, B});
  574. }
  575. Data.getOrder().emplace_back(F.getName());
  576. Data.getData().insert({F.getName(), FD});
  577. return true;
  578. }
  579. return false;
  580. }
  581. PrintIRInstrumentation::~PrintIRInstrumentation() {
  582. assert(ModuleDescStack.empty() && "ModuleDescStack is not empty at exit");
  583. }
  584. void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) {
  585. const Module *M = unwrapModule(IR);
  586. ModuleDescStack.emplace_back(M, getIRName(IR), PassID);
  587. }
  588. PrintIRInstrumentation::PrintModuleDesc
  589. PrintIRInstrumentation::popModuleDesc(StringRef PassID) {
  590. assert(!ModuleDescStack.empty() && "empty ModuleDescStack");
  591. PrintModuleDesc ModuleDesc = ModuleDescStack.pop_back_val();
  592. assert(std::get<2>(ModuleDesc).equals(PassID) && "malformed ModuleDescStack");
  593. return ModuleDesc;
  594. }
  595. void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
  596. if (isIgnored(PassID))
  597. return;
  598. // Saving Module for AfterPassInvalidated operations.
  599. // Note: here we rely on a fact that we do not change modules while
  600. // traversing the pipeline, so the latest captured module is good
  601. // for all print operations that has not happen yet.
  602. if (shouldPrintAfterPass(PassID))
  603. pushModuleDesc(PassID, IR);
  604. if (!shouldPrintBeforePass(PassID))
  605. return;
  606. if (!shouldPrintIR(IR))
  607. return;
  608. dbgs() << "*** IR Dump Before " << PassID << " on " << getIRName(IR)
  609. << " ***\n";
  610. unwrapAndPrint(dbgs(), IR);
  611. }
  612. void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
  613. if (isIgnored(PassID))
  614. return;
  615. if (!shouldPrintAfterPass(PassID))
  616. return;
  617. const Module *M;
  618. std::string IRName;
  619. StringRef StoredPassID;
  620. std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
  621. assert(StoredPassID == PassID && "mismatched PassID");
  622. if (!shouldPrintIR(IR))
  623. return;
  624. dbgs() << "*** IR Dump After " << PassID << " on " << IRName << " ***\n";
  625. unwrapAndPrint(dbgs(), IR);
  626. }
  627. void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
  628. StringRef PassName = PIC->getPassNameForClassName(PassID);
  629. if (!shouldPrintAfterPass(PassName))
  630. return;
  631. if (isIgnored(PassID))
  632. return;
  633. const Module *M;
  634. std::string IRName;
  635. StringRef StoredPassID;
  636. std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
  637. assert(StoredPassID == PassID && "mismatched PassID");
  638. // Additional filtering (e.g. -filter-print-func) can lead to module
  639. // printing being skipped.
  640. if (!M)
  641. return;
  642. SmallString<20> Banner =
  643. formatv("*** IR Dump After {0} on {1} (invalidated) ***", PassID, IRName);
  644. dbgs() << Banner << "\n";
  645. printIR(dbgs(), M);
  646. }
  647. bool PrintIRInstrumentation::shouldPrintBeforePass(StringRef PassID) {
  648. if (shouldPrintBeforeAll())
  649. return true;
  650. StringRef PassName = PIC->getPassNameForClassName(PassID);
  651. return is_contained(printBeforePasses(), PassName);
  652. }
  653. bool PrintIRInstrumentation::shouldPrintAfterPass(StringRef PassID) {
  654. if (shouldPrintAfterAll())
  655. return true;
  656. StringRef PassName = PIC->getPassNameForClassName(PassID);
  657. return is_contained(printAfterPasses(), PassName);
  658. }
  659. void PrintIRInstrumentation::registerCallbacks(
  660. PassInstrumentationCallbacks &PIC) {
  661. this->PIC = &PIC;
  662. // BeforePass callback is not just for printing, it also saves a Module
  663. // for later use in AfterPassInvalidated.
  664. if (shouldPrintBeforeSomePass() || shouldPrintAfterSomePass())
  665. PIC.registerBeforeNonSkippedPassCallback(
  666. [this](StringRef P, Any IR) { this->printBeforePass(P, IR); });
  667. if (shouldPrintAfterSomePass()) {
  668. PIC.registerAfterPassCallback(
  669. [this](StringRef P, Any IR, const PreservedAnalyses &) {
  670. this->printAfterPass(P, IR);
  671. });
  672. PIC.registerAfterPassInvalidatedCallback(
  673. [this](StringRef P, const PreservedAnalyses &) {
  674. this->printAfterPassInvalidated(P);
  675. });
  676. }
  677. }
  678. void OptNoneInstrumentation::registerCallbacks(
  679. PassInstrumentationCallbacks &PIC) {
  680. PIC.registerShouldRunOptionalPassCallback(
  681. [this](StringRef P, Any IR) { return this->shouldRun(P, IR); });
  682. }
  683. bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
  684. const Function **FPtr = any_cast<const Function *>(&IR);
  685. const Function *F = FPtr ? *FPtr : nullptr;
  686. if (!F) {
  687. if (const auto **L = any_cast<const Loop *>(&IR))
  688. F = (*L)->getHeader()->getParent();
  689. }
  690. bool ShouldRun = !(F && F->hasOptNone());
  691. if (!ShouldRun && DebugLogging) {
  692. errs() << "Skipping pass " << PassID << " on " << F->getName()
  693. << " due to optnone attribute\n";
  694. }
  695. return ShouldRun;
  696. }
  697. bool OptPassGateInstrumentation::shouldRun(StringRef PassName, Any IR) {
  698. if (isIgnored(PassName))
  699. return true;
  700. bool ShouldRun =
  701. Context.getOptPassGate().shouldRunPass(PassName, getIRName(IR));
  702. if (!ShouldRun && !this->HasWrittenIR && !OptBisectPrintIRPath.empty()) {
  703. // FIXME: print IR if limit is higher than number of opt-bisect
  704. // invocations
  705. this->HasWrittenIR = true;
  706. const Module *M = unwrapModule(IR, /*Force=*/true);
  707. assert((M && &M->getContext() == &Context) && "Missing/Mismatching Module");
  708. std::error_code EC;
  709. raw_fd_ostream OS(OptBisectPrintIRPath, EC);
  710. if (EC)
  711. report_fatal_error(errorCodeToError(EC));
  712. M->print(OS, nullptr);
  713. }
  714. return ShouldRun;
  715. }
  716. void OptPassGateInstrumentation::registerCallbacks(
  717. PassInstrumentationCallbacks &PIC) {
  718. OptPassGate &PassGate = Context.getOptPassGate();
  719. if (!PassGate.isEnabled())
  720. return;
  721. PIC.registerShouldRunOptionalPassCallback([this](StringRef PassName, Any IR) {
  722. return this->shouldRun(PassName, IR);
  723. });
  724. }
  725. raw_ostream &PrintPassInstrumentation::print() {
  726. if (Opts.Indent) {
  727. assert(Indent >= 0);
  728. dbgs().indent(Indent);
  729. }
  730. return dbgs();
  731. }
  732. void PrintPassInstrumentation::registerCallbacks(
  733. PassInstrumentationCallbacks &PIC) {
  734. if (!Enabled)
  735. return;
  736. std::vector<StringRef> SpecialPasses;
  737. if (!Opts.Verbose) {
  738. SpecialPasses.emplace_back("PassManager");
  739. SpecialPasses.emplace_back("PassAdaptor");
  740. }
  741. PIC.registerBeforeSkippedPassCallback([this, SpecialPasses](StringRef PassID,
  742. Any IR) {
  743. assert(!isSpecialPass(PassID, SpecialPasses) &&
  744. "Unexpectedly skipping special pass");
  745. print() << "Skipping pass: " << PassID << " on " << getIRName(IR) << "\n";
  746. });
  747. PIC.registerBeforeNonSkippedPassCallback([this, SpecialPasses](
  748. StringRef PassID, Any IR) {
  749. if (isSpecialPass(PassID, SpecialPasses))
  750. return;
  751. auto &OS = print();
  752. OS << "Running pass: " << PassID << " on " << getIRName(IR);
  753. if (const auto **F = any_cast<const Function *>(&IR)) {
  754. unsigned Count = (*F)->getInstructionCount();
  755. OS << " (" << Count << " instruction";
  756. if (Count != 1)
  757. OS << 's';
  758. OS << ')';
  759. } else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) {
  760. int Count = (*C)->size();
  761. OS << " (" << Count << " node";
  762. if (Count != 1)
  763. OS << 's';
  764. OS << ')';
  765. }
  766. OS << "\n";
  767. Indent += 2;
  768. });
  769. PIC.registerAfterPassCallback(
  770. [this, SpecialPasses](StringRef PassID, Any IR,
  771. const PreservedAnalyses &) {
  772. if (isSpecialPass(PassID, SpecialPasses))
  773. return;
  774. Indent -= 2;
  775. });
  776. PIC.registerAfterPassInvalidatedCallback(
  777. [this, SpecialPasses](StringRef PassID, Any IR) {
  778. if (isSpecialPass(PassID, SpecialPasses))
  779. return;
  780. Indent -= 2;
  781. });
  782. if (!Opts.SkipAnalyses) {
  783. PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) {
  784. print() << "Running analysis: " << PassID << " on " << getIRName(IR)
  785. << "\n";
  786. Indent += 2;
  787. });
  788. PIC.registerAfterAnalysisCallback(
  789. [this](StringRef PassID, Any IR) { Indent -= 2; });
  790. PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) {
  791. print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR)
  792. << "\n";
  793. });
  794. PIC.registerAnalysesClearedCallback([this](StringRef IRName) {
  795. print() << "Clearing all analysis results for: " << IRName << "\n";
  796. });
  797. }
  798. }
  799. PreservedCFGCheckerInstrumentation::CFG::CFG(const Function *F,
  800. bool TrackBBLifetime) {
  801. if (TrackBBLifetime)
  802. BBGuards = DenseMap<intptr_t, BBGuard>(F->size());
  803. for (const auto &BB : *F) {
  804. if (BBGuards)
  805. BBGuards->try_emplace(intptr_t(&BB), &BB);
  806. for (const auto *Succ : successors(&BB)) {
  807. Graph[&BB][Succ]++;
  808. if (BBGuards)
  809. BBGuards->try_emplace(intptr_t(Succ), Succ);
  810. }
  811. }
  812. }
  813. static void printBBName(raw_ostream &out, const BasicBlock *BB) {
  814. if (BB->hasName()) {
  815. out << BB->getName() << "<" << BB << ">";
  816. return;
  817. }
  818. if (!BB->getParent()) {
  819. out << "unnamed_removed<" << BB << ">";
  820. return;
  821. }
  822. if (BB->isEntryBlock()) {
  823. out << "entry"
  824. << "<" << BB << ">";
  825. return;
  826. }
  827. unsigned FuncOrderBlockNum = 0;
  828. for (auto &FuncBB : *BB->getParent()) {
  829. if (&FuncBB == BB)
  830. break;
  831. FuncOrderBlockNum++;
  832. }
  833. out << "unnamed_" << FuncOrderBlockNum << "<" << BB << ">";
  834. }
  835. void PreservedCFGCheckerInstrumentation::CFG::printDiff(raw_ostream &out,
  836. const CFG &Before,
  837. const CFG &After) {
  838. assert(!After.isPoisoned());
  839. if (Before.isPoisoned()) {
  840. out << "Some blocks were deleted\n";
  841. return;
  842. }
  843. // Find and print graph differences.
  844. if (Before.Graph.size() != After.Graph.size())
  845. out << "Different number of non-leaf basic blocks: before="
  846. << Before.Graph.size() << ", after=" << After.Graph.size() << "\n";
  847. for (auto &BB : Before.Graph) {
  848. auto BA = After.Graph.find(BB.first);
  849. if (BA == After.Graph.end()) {
  850. out << "Non-leaf block ";
  851. printBBName(out, BB.first);
  852. out << " is removed (" << BB.second.size() << " successors)\n";
  853. }
  854. }
  855. for (auto &BA : After.Graph) {
  856. auto BB = Before.Graph.find(BA.first);
  857. if (BB == Before.Graph.end()) {
  858. out << "Non-leaf block ";
  859. printBBName(out, BA.first);
  860. out << " is added (" << BA.second.size() << " successors)\n";
  861. continue;
  862. }
  863. if (BB->second == BA.second)
  864. continue;
  865. out << "Different successors of block ";
  866. printBBName(out, BA.first);
  867. out << " (unordered):\n";
  868. out << "- before (" << BB->second.size() << "): ";
  869. for (auto &SuccB : BB->second) {
  870. printBBName(out, SuccB.first);
  871. if (SuccB.second != 1)
  872. out << "(" << SuccB.second << "), ";
  873. else
  874. out << ", ";
  875. }
  876. out << "\n";
  877. out << "- after (" << BA.second.size() << "): ";
  878. for (auto &SuccA : BA.second) {
  879. printBBName(out, SuccA.first);
  880. if (SuccA.second != 1)
  881. out << "(" << SuccA.second << "), ";
  882. else
  883. out << ", ";
  884. }
  885. out << "\n";
  886. }
  887. }
  888. // PreservedCFGCheckerInstrumentation uses PreservedCFGCheckerAnalysis to check
  889. // passes, that reported they kept CFG analyses up-to-date, did not actually
  890. // change CFG. This check is done as follows. Before every functional pass in
  891. // BeforeNonSkippedPassCallback a CFG snapshot (an instance of
  892. // PreservedCFGCheckerInstrumentation::CFG) is requested from
  893. // FunctionAnalysisManager as a result of PreservedCFGCheckerAnalysis. When the
  894. // functional pass finishes and reports that CFGAnalyses or AllAnalyses are
  895. // up-to-date then the cached result of PreservedCFGCheckerAnalysis (if
  896. // available) is checked to be equal to a freshly created CFG snapshot.
  897. struct PreservedCFGCheckerAnalysis
  898. : public AnalysisInfoMixin<PreservedCFGCheckerAnalysis> {
  899. friend AnalysisInfoMixin<PreservedCFGCheckerAnalysis>;
  900. static AnalysisKey Key;
  901. public:
  902. /// Provide the result type for this analysis pass.
  903. using Result = PreservedCFGCheckerInstrumentation::CFG;
  904. /// Run the analysis pass over a function and produce CFG.
  905. Result run(Function &F, FunctionAnalysisManager &FAM) {
  906. return Result(&F, /* TrackBBLifetime */ true);
  907. }
  908. };
  909. AnalysisKey PreservedCFGCheckerAnalysis::Key;
  910. bool PreservedCFGCheckerInstrumentation::CFG::invalidate(
  911. Function &F, const PreservedAnalyses &PA,
  912. FunctionAnalysisManager::Invalidator &) {
  913. auto PAC = PA.getChecker<PreservedCFGCheckerAnalysis>();
  914. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
  915. PAC.preservedSet<CFGAnalyses>());
  916. }
  917. void PreservedCFGCheckerInstrumentation::registerCallbacks(
  918. PassInstrumentationCallbacks &PIC, FunctionAnalysisManager &FAM) {
  919. if (!VerifyPreservedCFG)
  920. return;
  921. FAM.registerPass([&] { return PreservedCFGCheckerAnalysis(); });
  922. auto checkCFG = [](StringRef Pass, StringRef FuncName, const CFG &GraphBefore,
  923. const CFG &GraphAfter) {
  924. if (GraphAfter == GraphBefore)
  925. return;
  926. dbgs() << "Error: " << Pass
  927. << " does not invalidate CFG analyses but CFG changes detected in "
  928. "function @"
  929. << FuncName << ":\n";
  930. CFG::printDiff(dbgs(), GraphBefore, GraphAfter);
  931. report_fatal_error(Twine("CFG unexpectedly changed by ", Pass));
  932. };
  933. PIC.registerBeforeNonSkippedPassCallback(
  934. [this, &FAM](StringRef P, Any IR) {
  935. #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
  936. assert(&PassStack.emplace_back(P));
  937. #endif
  938. (void)this;
  939. const auto **F = any_cast<const Function *>(&IR);
  940. if (!F)
  941. return;
  942. // Make sure a fresh CFG snapshot is available before the pass.
  943. FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(*F));
  944. });
  945. PIC.registerAfterPassInvalidatedCallback(
  946. [this](StringRef P, const PreservedAnalyses &PassPA) {
  947. #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
  948. assert(PassStack.pop_back_val() == P &&
  949. "Before and After callbacks must correspond");
  950. #endif
  951. (void)this;
  952. });
  953. PIC.registerAfterPassCallback([this, &FAM,
  954. checkCFG](StringRef P, Any IR,
  955. const PreservedAnalyses &PassPA) {
  956. #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
  957. assert(PassStack.pop_back_val() == P &&
  958. "Before and After callbacks must correspond");
  959. #endif
  960. (void)this;
  961. const auto **F = any_cast<const Function *>(&IR);
  962. if (!F)
  963. return;
  964. if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() &&
  965. !PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>())
  966. return;
  967. if (auto *GraphBefore = FAM.getCachedResult<PreservedCFGCheckerAnalysis>(
  968. *const_cast<Function *>(*F)))
  969. checkCFG(P, (*F)->getName(), *GraphBefore,
  970. CFG(*F, /* TrackBBLifetime */ false));
  971. });
  972. }
  973. void VerifyInstrumentation::registerCallbacks(
  974. PassInstrumentationCallbacks &PIC) {
  975. PIC.registerAfterPassCallback(
  976. [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
  977. if (isIgnored(P) || P == "VerifierPass")
  978. return;
  979. const Function **FPtr = any_cast<const Function *>(&IR);
  980. const Function *F = FPtr ? *FPtr : nullptr;
  981. if (!F) {
  982. if (const auto **L = any_cast<const Loop *>(&IR))
  983. F = (*L)->getHeader()->getParent();
  984. }
  985. if (F) {
  986. if (DebugLogging)
  987. dbgs() << "Verifying function " << F->getName() << "\n";
  988. if (verifyFunction(*F, &errs()))
  989. report_fatal_error("Broken function found, compilation aborted!");
  990. } else {
  991. const Module **MPtr = any_cast<const Module *>(&IR);
  992. const Module *M = MPtr ? *MPtr : nullptr;
  993. if (!M) {
  994. if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR))
  995. M = (*C)->begin()->getFunction().getParent();
  996. }
  997. if (M) {
  998. if (DebugLogging)
  999. dbgs() << "Verifying module " << M->getName() << "\n";
  1000. if (verifyModule(*M, &errs()))
  1001. report_fatal_error("Broken module found, compilation aborted!");
  1002. }
  1003. }
  1004. });
  1005. }
  1006. InLineChangePrinter::~InLineChangePrinter() = default;
  1007. void InLineChangePrinter::generateIRRepresentation(Any IR,
  1008. StringRef PassID,
  1009. IRDataT<EmptyData> &D) {
  1010. IRComparer<EmptyData>::analyzeIR(IR, D);
  1011. }
  1012. void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name,
  1013. const IRDataT<EmptyData> &Before,
  1014. const IRDataT<EmptyData> &After,
  1015. Any IR) {
  1016. SmallString<20> Banner =
  1017. formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
  1018. Out << Banner;
  1019. IRComparer<EmptyData>(Before, After)
  1020. .compare(getModuleForComparison(IR),
  1021. [&](bool InModule, unsigned Minor,
  1022. const FuncDataT<EmptyData> &Before,
  1023. const FuncDataT<EmptyData> &After) -> void {
  1024. handleFunctionCompare(Name, "", PassID, " on ", InModule,
  1025. Minor, Before, After);
  1026. });
  1027. Out << "\n";
  1028. }
  1029. void InLineChangePrinter::handleFunctionCompare(
  1030. StringRef Name, StringRef Prefix, StringRef PassID, StringRef Divider,
  1031. bool InModule, unsigned Minor, const FuncDataT<EmptyData> &Before,
  1032. const FuncDataT<EmptyData> &After) {
  1033. // Print a banner when this is being shown in the context of a module
  1034. if (InModule)
  1035. Out << "\n*** IR for function " << Name << " ***\n";
  1036. FuncDataT<EmptyData>::report(
  1037. Before, After,
  1038. [&](const BlockDataT<EmptyData> *B, const BlockDataT<EmptyData> *A) {
  1039. StringRef BStr = B ? B->getBody() : "\n";
  1040. StringRef AStr = A ? A->getBody() : "\n";
  1041. const std::string Removed =
  1042. UseColour ? "\033[31m-%l\033[0m\n" : "-%l\n";
  1043. const std::string Added = UseColour ? "\033[32m+%l\033[0m\n" : "+%l\n";
  1044. const std::string NoChange = " %l\n";
  1045. Out << doSystemDiff(BStr, AStr, Removed, Added, NoChange);
  1046. });
  1047. }
  1048. void InLineChangePrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
  1049. if (PrintChanged == ChangePrinter::DiffVerbose ||
  1050. PrintChanged == ChangePrinter::DiffQuiet ||
  1051. PrintChanged == ChangePrinter::ColourDiffVerbose ||
  1052. PrintChanged == ChangePrinter::ColourDiffQuiet)
  1053. TextChangeReporter<IRDataT<EmptyData>>::registerRequiredCallbacks(PIC);
  1054. }
  1055. TimeProfilingPassesHandler::TimeProfilingPassesHandler() {}
  1056. void TimeProfilingPassesHandler::registerCallbacks(
  1057. PassInstrumentationCallbacks &PIC) {
  1058. if (!getTimeTraceProfilerInstance())
  1059. return;
  1060. PIC.registerBeforeNonSkippedPassCallback(
  1061. [this](StringRef P, Any IR) { this->runBeforePass(P, IR); });
  1062. PIC.registerAfterPassCallback(
  1063. [this](StringRef P, Any IR, const PreservedAnalyses &) {
  1064. this->runAfterPass();
  1065. },
  1066. true);
  1067. PIC.registerAfterPassInvalidatedCallback(
  1068. [this](StringRef P, const PreservedAnalyses &) { this->runAfterPass(); },
  1069. true);
  1070. PIC.registerBeforeAnalysisCallback(
  1071. [this](StringRef P, Any IR) { this->runBeforePass(P, IR); });
  1072. PIC.registerAfterAnalysisCallback(
  1073. [this](StringRef P, Any IR) { this->runAfterPass(); }, true);
  1074. }
  1075. void TimeProfilingPassesHandler::runBeforePass(StringRef PassID, Any IR) {
  1076. timeTraceProfilerBegin(PassID, getIRName(IR));
  1077. }
  1078. void TimeProfilingPassesHandler::runAfterPass() { timeTraceProfilerEnd(); }
  1079. namespace {
  1080. class DisplayNode;
  1081. class DotCfgDiffDisplayGraph;
  1082. // Base class for a node or edge in the dot-cfg-changes graph.
  1083. class DisplayElement {
  1084. public:
  1085. // Is this in before, after, or both?
  1086. StringRef getColour() const { return Colour; }
  1087. protected:
  1088. DisplayElement(StringRef Colour) : Colour(Colour) {}
  1089. const StringRef Colour;
  1090. };
  1091. // An edge representing a transition between basic blocks in the
  1092. // dot-cfg-changes graph.
  1093. class DisplayEdge : public DisplayElement {
  1094. public:
  1095. DisplayEdge(std::string Value, DisplayNode &Node, StringRef Colour)
  1096. : DisplayElement(Colour), Value(Value), Node(Node) {}
  1097. // The value on which the transition is made.
  1098. std::string getValue() const { return Value; }
  1099. // The node (representing a basic block) reached by this transition.
  1100. const DisplayNode &getDestinationNode() const { return Node; }
  1101. protected:
  1102. std::string Value;
  1103. const DisplayNode &Node;
  1104. };
  1105. // A node in the dot-cfg-changes graph which represents a basic block.
  1106. class DisplayNode : public DisplayElement {
  1107. public:
  1108. // \p C is the content for the node, \p T indicates the colour for the
  1109. // outline of the node
  1110. DisplayNode(std::string Content, StringRef Colour)
  1111. : DisplayElement(Colour), Content(Content) {}
  1112. // Iterator to the child nodes. Required by GraphWriter.
  1113. using ChildIterator = std::unordered_set<DisplayNode *>::const_iterator;
  1114. ChildIterator children_begin() const { return Children.cbegin(); }
  1115. ChildIterator children_end() const { return Children.cend(); }
  1116. // Iterator for the edges. Required by GraphWriter.
  1117. using EdgeIterator = std::vector<DisplayEdge *>::const_iterator;
  1118. EdgeIterator edges_begin() const { return EdgePtrs.cbegin(); }
  1119. EdgeIterator edges_end() const { return EdgePtrs.cend(); }
  1120. // Create an edge to \p Node on value \p Value, with colour \p Colour.
  1121. void createEdge(StringRef Value, DisplayNode &Node, StringRef Colour);
  1122. // Return the content of this node.
  1123. std::string getContent() const { return Content; }
  1124. // Return the edge to node \p S.
  1125. const DisplayEdge &getEdge(const DisplayNode &To) const {
  1126. assert(EdgeMap.find(&To) != EdgeMap.end() && "Expected to find edge.");
  1127. return *EdgeMap.find(&To)->second;
  1128. }
  1129. // Return the value for the transition to basic block \p S.
  1130. // Required by GraphWriter.
  1131. std::string getEdgeSourceLabel(const DisplayNode &Sink) const {
  1132. return getEdge(Sink).getValue();
  1133. }
  1134. void createEdgeMap();
  1135. protected:
  1136. const std::string Content;
  1137. // Place to collect all of the edges. Once they are all in the vector,
  1138. // the vector will not reallocate so then we can use pointers to them,
  1139. // which are required by the graph writing routines.
  1140. std::vector<DisplayEdge> Edges;
  1141. std::vector<DisplayEdge *> EdgePtrs;
  1142. std::unordered_set<DisplayNode *> Children;
  1143. std::unordered_map<const DisplayNode *, const DisplayEdge *> EdgeMap;
  1144. // Safeguard adding of edges.
  1145. bool AllEdgesCreated = false;
  1146. };
  1147. // Class representing a difference display (corresponds to a pdf file).
  1148. class DotCfgDiffDisplayGraph {
  1149. public:
  1150. DotCfgDiffDisplayGraph(std::string Name) : GraphName(Name) {}
  1151. // Generate the file into \p DotFile.
  1152. void generateDotFile(StringRef DotFile);
  1153. // Iterator to the nodes. Required by GraphWriter.
  1154. using NodeIterator = std::vector<DisplayNode *>::const_iterator;
  1155. NodeIterator nodes_begin() const {
  1156. assert(NodeGenerationComplete && "Unexpected children iterator creation");
  1157. return NodePtrs.cbegin();
  1158. }
  1159. NodeIterator nodes_end() const {
  1160. assert(NodeGenerationComplete && "Unexpected children iterator creation");
  1161. return NodePtrs.cend();
  1162. }
  1163. // Record the index of the entry node. At this point, we can build up
  1164. // vectors of pointers that are required by the graph routines.
  1165. void setEntryNode(unsigned N) {
  1166. // At this point, there will be no new nodes.
  1167. assert(!NodeGenerationComplete && "Unexpected node creation");
  1168. NodeGenerationComplete = true;
  1169. for (auto &N : Nodes)
  1170. NodePtrs.emplace_back(&N);
  1171. EntryNode = NodePtrs[N];
  1172. }
  1173. // Create a node.
  1174. void createNode(std::string C, StringRef Colour) {
  1175. assert(!NodeGenerationComplete && "Unexpected node creation");
  1176. Nodes.emplace_back(C, Colour);
  1177. }
  1178. // Return the node at index \p N to avoid problems with vectors reallocating.
  1179. DisplayNode &getNode(unsigned N) {
  1180. assert(N < Nodes.size() && "Node is out of bounds");
  1181. return Nodes[N];
  1182. }
  1183. unsigned size() const {
  1184. assert(NodeGenerationComplete && "Unexpected children iterator creation");
  1185. return Nodes.size();
  1186. }
  1187. // Return the name of the graph. Required by GraphWriter.
  1188. std::string getGraphName() const { return GraphName; }
  1189. // Return the string representing the differences for basic block \p Node.
  1190. // Required by GraphWriter.
  1191. std::string getNodeLabel(const DisplayNode &Node) const {
  1192. return Node.getContent();
  1193. }
  1194. // Return a string with colour information for Dot. Required by GraphWriter.
  1195. std::string getNodeAttributes(const DisplayNode &Node) const {
  1196. return attribute(Node.getColour());
  1197. }
  1198. // Return a string with colour information for Dot. Required by GraphWriter.
  1199. std::string getEdgeColorAttr(const DisplayNode &From,
  1200. const DisplayNode &To) const {
  1201. return attribute(From.getEdge(To).getColour());
  1202. }
  1203. // Get the starting basic block. Required by GraphWriter.
  1204. DisplayNode *getEntryNode() const {
  1205. assert(NodeGenerationComplete && "Unexpected children iterator creation");
  1206. return EntryNode;
  1207. }
  1208. protected:
  1209. // Return the string containing the colour to use as a Dot attribute.
  1210. std::string attribute(StringRef Colour) const {
  1211. return "color=" + Colour.str();
  1212. }
  1213. bool NodeGenerationComplete = false;
  1214. const std::string GraphName;
  1215. std::vector<DisplayNode> Nodes;
  1216. std::vector<DisplayNode *> NodePtrs;
  1217. DisplayNode *EntryNode = nullptr;
  1218. };
  1219. void DisplayNode::createEdge(StringRef Value, DisplayNode &Node,
  1220. StringRef Colour) {
  1221. assert(!AllEdgesCreated && "Expected to be able to still create edges.");
  1222. Edges.emplace_back(Value.str(), Node, Colour);
  1223. Children.insert(&Node);
  1224. }
  1225. void DisplayNode::createEdgeMap() {
  1226. // No more edges will be added so we can now use pointers to the edges
  1227. // as the vector will not grow and reallocate.
  1228. AllEdgesCreated = true;
  1229. for (auto &E : Edges)
  1230. EdgeMap.insert({&E.getDestinationNode(), &E});
  1231. }
  1232. class DotCfgDiffNode;
  1233. class DotCfgDiff;
  1234. // A class representing a basic block in the Dot difference graph.
  1235. class DotCfgDiffNode {
  1236. public:
  1237. DotCfgDiffNode() = delete;
  1238. // Create a node in Dot difference graph \p G representing the basic block
  1239. // represented by \p BD with colour \p Colour (where it exists).
  1240. DotCfgDiffNode(DotCfgDiff &G, unsigned N, const BlockDataT<DCData> &BD,
  1241. StringRef Colour)
  1242. : Graph(G), N(N), Data{&BD, nullptr}, Colour(Colour) {}
  1243. DotCfgDiffNode(const DotCfgDiffNode &DN)
  1244. : Graph(DN.Graph), N(DN.N), Data{DN.Data[0], DN.Data[1]},
  1245. Colour(DN.Colour), EdgesMap(DN.EdgesMap), Children(DN.Children),
  1246. Edges(DN.Edges) {}
  1247. unsigned getIndex() const { return N; }
  1248. // The label of the basic block
  1249. StringRef getLabel() const {
  1250. assert(Data[0] && "Expected Data[0] to be set.");
  1251. return Data[0]->getLabel();
  1252. }
  1253. // Return the colour for this block
  1254. StringRef getColour() const { return Colour; }
  1255. // Change this basic block from being only in before to being common.
  1256. // Save the pointer to \p Other.
  1257. void setCommon(const BlockDataT<DCData> &Other) {
  1258. assert(!Data[1] && "Expected only one block datum");
  1259. Data[1] = &Other;
  1260. Colour = CommonColour;
  1261. }
  1262. // Add an edge to \p E of colour {\p Value, \p Colour}.
  1263. void addEdge(unsigned E, StringRef Value, StringRef Colour) {
  1264. // This is a new edge or it is an edge being made common.
  1265. assert((EdgesMap.count(E) == 0 || Colour == CommonColour) &&
  1266. "Unexpected edge count and color.");
  1267. EdgesMap[E] = {Value.str(), Colour};
  1268. }
  1269. // Record the children and create edges.
  1270. void finalize(DotCfgDiff &G);
  1271. // Return the colour of the edge to node \p S.
  1272. StringRef getEdgeColour(const unsigned S) const {
  1273. assert(EdgesMap.count(S) == 1 && "Expected to find edge.");
  1274. return EdgesMap.at(S).second;
  1275. }
  1276. // Return the string representing the basic block.
  1277. std::string getBodyContent() const;
  1278. void createDisplayEdges(DotCfgDiffDisplayGraph &Graph, unsigned DisplayNode,
  1279. std::map<const unsigned, unsigned> &NodeMap) const;
  1280. protected:
  1281. DotCfgDiff &Graph;
  1282. const unsigned N;
  1283. const BlockDataT<DCData> *Data[2];
  1284. StringRef Colour;
  1285. std::map<const unsigned, std::pair<std::string, StringRef>> EdgesMap;
  1286. std::vector<unsigned> Children;
  1287. std::vector<unsigned> Edges;
  1288. };
  1289. // Class representing the difference graph between two functions.
  1290. class DotCfgDiff {
  1291. public:
  1292. // \p Title is the title given to the graph. \p EntryNodeName is the
  1293. // entry node for the function. \p Before and \p After are the before
  1294. // after versions of the function, respectively. \p Dir is the directory
  1295. // in which to store the results.
  1296. DotCfgDiff(StringRef Title, const FuncDataT<DCData> &Before,
  1297. const FuncDataT<DCData> &After);
  1298. DotCfgDiff(const DotCfgDiff &) = delete;
  1299. DotCfgDiff &operator=(const DotCfgDiff &) = delete;
  1300. DotCfgDiffDisplayGraph createDisplayGraph(StringRef Title,
  1301. StringRef EntryNodeName);
  1302. // Return a string consisting of the labels for the \p Source and \p Sink.
  1303. // The combination allows distinguishing changing transitions on the
  1304. // same value (ie, a transition went to X before and goes to Y after).
  1305. // Required by GraphWriter.
  1306. StringRef getEdgeSourceLabel(const unsigned &Source,
  1307. const unsigned &Sink) const {
  1308. std::string S =
  1309. getNode(Source).getLabel().str() + " " + getNode(Sink).getLabel().str();
  1310. assert(EdgeLabels.count(S) == 1 && "Expected to find edge label.");
  1311. return EdgeLabels.find(S)->getValue();
  1312. }
  1313. // Return the number of basic blocks (nodes). Required by GraphWriter.
  1314. unsigned size() const { return Nodes.size(); }
  1315. const DotCfgDiffNode &getNode(unsigned N) const {
  1316. assert(N < Nodes.size() && "Unexpected index for node reference");
  1317. return Nodes[N];
  1318. }
  1319. protected:
  1320. // Return the string surrounded by HTML to make it the appropriate colour.
  1321. std::string colourize(std::string S, StringRef Colour) const;
  1322. void createNode(StringRef Label, const BlockDataT<DCData> &BD, StringRef C) {
  1323. unsigned Pos = Nodes.size();
  1324. Nodes.emplace_back(*this, Pos, BD, C);
  1325. NodePosition.insert({Label, Pos});
  1326. }
  1327. // TODO Nodes should probably be a StringMap<DotCfgDiffNode> after the
  1328. // display graph is separated out, which would remove the need for
  1329. // NodePosition.
  1330. std::vector<DotCfgDiffNode> Nodes;
  1331. StringMap<unsigned> NodePosition;
  1332. const std::string GraphName;
  1333. StringMap<std::string> EdgeLabels;
  1334. };
  1335. std::string DotCfgDiffNode::getBodyContent() const {
  1336. if (Colour == CommonColour) {
  1337. assert(Data[1] && "Expected Data[1] to be set.");
  1338. StringRef SR[2];
  1339. for (unsigned I = 0; I < 2; ++I) {
  1340. SR[I] = Data[I]->getBody();
  1341. // drop initial '\n' if present
  1342. if (SR[I][0] == '\n')
  1343. SR[I] = SR[I].drop_front();
  1344. // drop predecessors as they can be big and are redundant
  1345. SR[I] = SR[I].drop_until([](char C) { return C == '\n'; }).drop_front();
  1346. }
  1347. SmallString<80> OldLineFormat = formatv(
  1348. "<FONT COLOR=\"{0}\">%l</FONT><BR align=\"left\"/>", BeforeColour);
  1349. SmallString<80> NewLineFormat = formatv(
  1350. "<FONT COLOR=\"{0}\">%l</FONT><BR align=\"left\"/>", AfterColour);
  1351. SmallString<80> UnchangedLineFormat = formatv(
  1352. "<FONT COLOR=\"{0}\">%l</FONT><BR align=\"left\"/>", CommonColour);
  1353. std::string Diff = Data[0]->getLabel().str();
  1354. Diff += ":\n<BR align=\"left\"/>" +
  1355. doSystemDiff(makeHTMLReady(SR[0]), makeHTMLReady(SR[1]),
  1356. OldLineFormat, NewLineFormat, UnchangedLineFormat);
  1357. // Diff adds in some empty colour changes which are not valid HTML
  1358. // so remove them. Colours are all lowercase alpha characters (as
  1359. // listed in https://graphviz.org/pdf/dotguide.pdf).
  1360. Regex R("<FONT COLOR=\"\\w+\"></FONT>");
  1361. while (true) {
  1362. std::string Error;
  1363. std::string S = R.sub("", Diff, &Error);
  1364. if (Error != "")
  1365. return Error;
  1366. if (S == Diff)
  1367. return Diff;
  1368. Diff = S;
  1369. }
  1370. llvm_unreachable("Should not get here");
  1371. }
  1372. // Put node out in the appropriate colour.
  1373. assert(!Data[1] && "Data[1] is set unexpectedly.");
  1374. std::string Body = makeHTMLReady(Data[0]->getBody());
  1375. const StringRef BS = Body;
  1376. StringRef BS1 = BS;
  1377. // Drop leading newline, if present.
  1378. if (BS.front() == '\n')
  1379. BS1 = BS1.drop_front(1);
  1380. // Get label.
  1381. StringRef Label = BS1.take_until([](char C) { return C == ':'; });
  1382. // drop predecessors as they can be big and are redundant
  1383. BS1 = BS1.drop_until([](char C) { return C == '\n'; }).drop_front();
  1384. std::string S = "<FONT COLOR=\"" + Colour.str() + "\">" + Label.str() + ":";
  1385. // align each line to the left.
  1386. while (BS1.size()) {
  1387. S.append("<BR align=\"left\"/>");
  1388. StringRef Line = BS1.take_until([](char C) { return C == '\n'; });
  1389. S.append(Line.str());
  1390. BS1 = BS1.drop_front(Line.size() + 1);
  1391. }
  1392. S.append("<BR align=\"left\"/></FONT>");
  1393. return S;
  1394. }
  1395. std::string DotCfgDiff::colourize(std::string S, StringRef Colour) const {
  1396. if (S.length() == 0)
  1397. return S;
  1398. return "<FONT COLOR=\"" + Colour.str() + "\">" + S + "</FONT>";
  1399. }
  1400. DotCfgDiff::DotCfgDiff(StringRef Title, const FuncDataT<DCData> &Before,
  1401. const FuncDataT<DCData> &After)
  1402. : GraphName(Title.str()) {
  1403. StringMap<StringRef> EdgesMap;
  1404. // Handle each basic block in the before IR.
  1405. for (auto &B : Before.getData()) {
  1406. StringRef Label = B.getKey();
  1407. const BlockDataT<DCData> &BD = B.getValue();
  1408. createNode(Label, BD, BeforeColour);
  1409. // Create transitions with names made up of the from block label, the value
  1410. // on which the transition is made and the to block label.
  1411. for (StringMap<std::string>::const_iterator Sink = BD.getData().begin(),
  1412. E = BD.getData().end();
  1413. Sink != E; ++Sink) {
  1414. std::string Key = (Label + " " + Sink->getKey().str()).str() + " " +
  1415. BD.getData().getSuccessorLabel(Sink->getKey()).str();
  1416. EdgesMap.insert({Key, BeforeColour});
  1417. }
  1418. }
  1419. // Handle each basic block in the after IR
  1420. for (auto &A : After.getData()) {
  1421. StringRef Label = A.getKey();
  1422. const BlockDataT<DCData> &BD = A.getValue();
  1423. unsigned C = NodePosition.count(Label);
  1424. if (C == 0)
  1425. // This only exists in the after IR. Create the node.
  1426. createNode(Label, BD, AfterColour);
  1427. else {
  1428. assert(C == 1 && "Unexpected multiple nodes.");
  1429. Nodes[NodePosition[Label]].setCommon(BD);
  1430. }
  1431. // Add in the edges between the nodes (as common or only in after).
  1432. for (StringMap<std::string>::const_iterator Sink = BD.getData().begin(),
  1433. E = BD.getData().end();
  1434. Sink != E; ++Sink) {
  1435. std::string Key = (Label + " " + Sink->getKey().str()).str() + " " +
  1436. BD.getData().getSuccessorLabel(Sink->getKey()).str();
  1437. unsigned C = EdgesMap.count(Key);
  1438. if (C == 0)
  1439. EdgesMap.insert({Key, AfterColour});
  1440. else {
  1441. EdgesMap[Key] = CommonColour;
  1442. }
  1443. }
  1444. }
  1445. // Now go through the map of edges and add them to the node.
  1446. for (auto &E : EdgesMap) {
  1447. // Extract the source, sink and value from the edge key.
  1448. StringRef S = E.getKey();
  1449. auto SP1 = S.rsplit(' ');
  1450. auto &SourceSink = SP1.first;
  1451. auto SP2 = SourceSink.split(' ');
  1452. StringRef Source = SP2.first;
  1453. StringRef Sink = SP2.second;
  1454. StringRef Value = SP1.second;
  1455. assert(NodePosition.count(Source) == 1 && "Expected to find node.");
  1456. DotCfgDiffNode &SourceNode = Nodes[NodePosition[Source]];
  1457. assert(NodePosition.count(Sink) == 1 && "Expected to find node.");
  1458. unsigned SinkNode = NodePosition[Sink];
  1459. StringRef Colour = E.second;
  1460. // Look for an edge from Source to Sink
  1461. if (EdgeLabels.count(SourceSink) == 0)
  1462. EdgeLabels.insert({SourceSink, colourize(Value.str(), Colour)});
  1463. else {
  1464. StringRef V = EdgeLabels.find(SourceSink)->getValue();
  1465. std::string NV = colourize(V.str() + " " + Value.str(), Colour);
  1466. Colour = CommonColour;
  1467. EdgeLabels[SourceSink] = NV;
  1468. }
  1469. SourceNode.addEdge(SinkNode, Value, Colour);
  1470. }
  1471. for (auto &I : Nodes)
  1472. I.finalize(*this);
  1473. }
  1474. DotCfgDiffDisplayGraph DotCfgDiff::createDisplayGraph(StringRef Title,
  1475. StringRef EntryNodeName) {
  1476. assert(NodePosition.count(EntryNodeName) == 1 &&
  1477. "Expected to find entry block in map.");
  1478. unsigned Entry = NodePosition[EntryNodeName];
  1479. assert(Entry < Nodes.size() && "Expected to find entry node");
  1480. DotCfgDiffDisplayGraph G(Title.str());
  1481. std::map<const unsigned, unsigned> NodeMap;
  1482. int EntryIndex = -1;
  1483. unsigned Index = 0;
  1484. for (auto &I : Nodes) {
  1485. if (I.getIndex() == Entry)
  1486. EntryIndex = Index;
  1487. G.createNode(I.getBodyContent(), I.getColour());
  1488. NodeMap.insert({I.getIndex(), Index++});
  1489. }
  1490. assert(EntryIndex >= 0 && "Expected entry node index to be set.");
  1491. G.setEntryNode(EntryIndex);
  1492. for (auto &I : NodeMap) {
  1493. unsigned SourceNode = I.first;
  1494. unsigned DisplayNode = I.second;
  1495. getNode(SourceNode).createDisplayEdges(G, DisplayNode, NodeMap);
  1496. }
  1497. return G;
  1498. }
  1499. void DotCfgDiffNode::createDisplayEdges(
  1500. DotCfgDiffDisplayGraph &DisplayGraph, unsigned DisplayNodeIndex,
  1501. std::map<const unsigned, unsigned> &NodeMap) const {
  1502. DisplayNode &SourceDisplayNode = DisplayGraph.getNode(DisplayNodeIndex);
  1503. for (auto I : Edges) {
  1504. unsigned SinkNodeIndex = I;
  1505. StringRef Colour = getEdgeColour(SinkNodeIndex);
  1506. const DotCfgDiffNode *SinkNode = &Graph.getNode(SinkNodeIndex);
  1507. StringRef Label = Graph.getEdgeSourceLabel(getIndex(), SinkNodeIndex);
  1508. DisplayNode &SinkDisplayNode = DisplayGraph.getNode(SinkNode->getIndex());
  1509. SourceDisplayNode.createEdge(Label, SinkDisplayNode, Colour);
  1510. }
  1511. SourceDisplayNode.createEdgeMap();
  1512. }
  1513. void DotCfgDiffNode::finalize(DotCfgDiff &G) {
  1514. for (auto E : EdgesMap) {
  1515. Children.emplace_back(E.first);
  1516. Edges.emplace_back(E.first);
  1517. }
  1518. }
  1519. } // namespace
  1520. namespace llvm {
  1521. template <> struct GraphTraits<DotCfgDiffDisplayGraph *> {
  1522. using NodeRef = const DisplayNode *;
  1523. using ChildIteratorType = DisplayNode::ChildIterator;
  1524. using nodes_iterator = DotCfgDiffDisplayGraph::NodeIterator;
  1525. using EdgeRef = const DisplayEdge *;
  1526. using ChildEdgeIterator = DisplayNode::EdgeIterator;
  1527. static NodeRef getEntryNode(const DotCfgDiffDisplayGraph *G) {
  1528. return G->getEntryNode();
  1529. }
  1530. static ChildIteratorType child_begin(NodeRef N) {
  1531. return N->children_begin();
  1532. }
  1533. static ChildIteratorType child_end(NodeRef N) { return N->children_end(); }
  1534. static nodes_iterator nodes_begin(const DotCfgDiffDisplayGraph *G) {
  1535. return G->nodes_begin();
  1536. }
  1537. static nodes_iterator nodes_end(const DotCfgDiffDisplayGraph *G) {
  1538. return G->nodes_end();
  1539. }
  1540. static ChildEdgeIterator child_edge_begin(NodeRef N) {
  1541. return N->edges_begin();
  1542. }
  1543. static ChildEdgeIterator child_edge_end(NodeRef N) { return N->edges_end(); }
  1544. static NodeRef edge_dest(EdgeRef E) { return &E->getDestinationNode(); }
  1545. static unsigned size(const DotCfgDiffDisplayGraph *G) { return G->size(); }
  1546. };
  1547. template <>
  1548. struct DOTGraphTraits<DotCfgDiffDisplayGraph *> : public DefaultDOTGraphTraits {
  1549. explicit DOTGraphTraits(bool Simple = false)
  1550. : DefaultDOTGraphTraits(Simple) {}
  1551. static bool renderNodesUsingHTML() { return true; }
  1552. static std::string getGraphName(const DotCfgDiffDisplayGraph *DiffData) {
  1553. return DiffData->getGraphName();
  1554. }
  1555. static std::string
  1556. getGraphProperties(const DotCfgDiffDisplayGraph *DiffData) {
  1557. return "\tsize=\"190, 190\";\n";
  1558. }
  1559. static std::string getNodeLabel(const DisplayNode *Node,
  1560. const DotCfgDiffDisplayGraph *DiffData) {
  1561. return DiffData->getNodeLabel(*Node);
  1562. }
  1563. static std::string getNodeAttributes(const DisplayNode *Node,
  1564. const DotCfgDiffDisplayGraph *DiffData) {
  1565. return DiffData->getNodeAttributes(*Node);
  1566. }
  1567. static std::string getEdgeSourceLabel(const DisplayNode *From,
  1568. DisplayNode::ChildIterator &To) {
  1569. return From->getEdgeSourceLabel(**To);
  1570. }
  1571. static std::string getEdgeAttributes(const DisplayNode *From,
  1572. DisplayNode::ChildIterator &To,
  1573. const DotCfgDiffDisplayGraph *DiffData) {
  1574. return DiffData->getEdgeColorAttr(*From, **To);
  1575. }
  1576. };
  1577. } // namespace llvm
  1578. namespace {
  1579. void DotCfgDiffDisplayGraph::generateDotFile(StringRef DotFile) {
  1580. std::error_code EC;
  1581. raw_fd_ostream OutStream(DotFile, EC);
  1582. if (EC) {
  1583. errs() << "Error: " << EC.message() << "\n";
  1584. return;
  1585. }
  1586. WriteGraph(OutStream, this, false);
  1587. OutStream.flush();
  1588. OutStream.close();
  1589. }
  1590. } // namespace
  1591. namespace llvm {
  1592. DCData::DCData(const BasicBlock &B) {
  1593. // Build up transition labels.
  1594. const Instruction *Term = B.getTerminator();
  1595. if (const BranchInst *Br = dyn_cast<const BranchInst>(Term))
  1596. if (Br->isUnconditional())
  1597. addSuccessorLabel(Br->getSuccessor(0)->getName().str(), "");
  1598. else {
  1599. addSuccessorLabel(Br->getSuccessor(0)->getName().str(), "true");
  1600. addSuccessorLabel(Br->getSuccessor(1)->getName().str(), "false");
  1601. }
  1602. else if (const SwitchInst *Sw = dyn_cast<const SwitchInst>(Term)) {
  1603. addSuccessorLabel(Sw->case_default()->getCaseSuccessor()->getName().str(),
  1604. "default");
  1605. for (auto &C : Sw->cases()) {
  1606. assert(C.getCaseValue() && "Expected to find case value.");
  1607. SmallString<20> Value = formatv("{0}", C.getCaseValue()->getSExtValue());
  1608. addSuccessorLabel(C.getCaseSuccessor()->getName().str(), Value);
  1609. }
  1610. } else
  1611. for (const_succ_iterator I = succ_begin(&B), E = succ_end(&B); I != E; ++I)
  1612. addSuccessorLabel((*I)->getName().str(), "");
  1613. }
  1614. DotCfgChangeReporter::DotCfgChangeReporter(bool Verbose)
  1615. : ChangeReporter<IRDataT<DCData>>(Verbose) {}
  1616. void DotCfgChangeReporter::handleFunctionCompare(
  1617. StringRef Name, StringRef Prefix, StringRef PassID, StringRef Divider,
  1618. bool InModule, unsigned Minor, const FuncDataT<DCData> &Before,
  1619. const FuncDataT<DCData> &After) {
  1620. assert(HTML && "Expected outstream to be set");
  1621. SmallString<8> Extender;
  1622. SmallString<8> Number;
  1623. // Handle numbering and file names.
  1624. if (InModule) {
  1625. Extender = formatv("{0}_{1}", N, Minor);
  1626. Number = formatv("{0}.{1}", N, Minor);
  1627. } else {
  1628. Extender = formatv("{0}", N);
  1629. Number = formatv("{0}", N);
  1630. }
  1631. // Create a temporary file name for the dot file.
  1632. SmallVector<char, 128> SV;
  1633. sys::fs::createUniquePath("cfgdot-%%%%%%.dot", SV, true);
  1634. std::string DotFile = Twine(SV).str();
  1635. SmallString<20> PDFFileName = formatv("diff_{0}.pdf", Extender);
  1636. SmallString<200> Text;
  1637. Text = formatv("{0}.{1}{2}{3}{4}", Number, Prefix, makeHTMLReady(PassID),
  1638. Divider, Name);
  1639. DotCfgDiff Diff(Text, Before, After);
  1640. std::string EntryBlockName = After.getEntryBlockName();
  1641. // Use the before entry block if the after entry block was removed.
  1642. if (EntryBlockName == "")
  1643. EntryBlockName = Before.getEntryBlockName();
  1644. assert(EntryBlockName != "" && "Expected to find entry block");
  1645. DotCfgDiffDisplayGraph DG = Diff.createDisplayGraph(Text, EntryBlockName);
  1646. DG.generateDotFile(DotFile);
  1647. *HTML << genHTML(Text, DotFile, PDFFileName);
  1648. std::error_code EC = sys::fs::remove(DotFile);
  1649. if (EC)
  1650. errs() << "Error: " << EC.message() << "\n";
  1651. }
  1652. std::string DotCfgChangeReporter::genHTML(StringRef Text, StringRef DotFile,
  1653. StringRef PDFFileName) {
  1654. SmallString<20> PDFFile = formatv("{0}/{1}", DotCfgDir, PDFFileName);
  1655. // Create the PDF file.
  1656. static ErrorOr<std::string> DotExe = sys::findProgramByName(DotBinary);
  1657. if (!DotExe)
  1658. return "Unable to find dot executable.";
  1659. StringRef Args[] = {DotBinary, "-Tpdf", "-o", PDFFile, DotFile};
  1660. int Result = sys::ExecuteAndWait(*DotExe, Args, std::nullopt);
  1661. if (Result < 0)
  1662. return "Error executing system dot.";
  1663. // Create the HTML tag refering to the PDF file.
  1664. SmallString<200> S = formatv(
  1665. " <a href=\"{0}\" target=\"_blank\">{1}</a><br/>\n", PDFFileName, Text);
  1666. return S.c_str();
  1667. }
  1668. void DotCfgChangeReporter::handleInitialIR(Any IR) {
  1669. assert(HTML && "Expected outstream to be set");
  1670. *HTML << "<button type=\"button\" class=\"collapsible\">0. "
  1671. << "Initial IR (by function)</button>\n"
  1672. << "<div class=\"content\">\n"
  1673. << " <p>\n";
  1674. // Create representation of IR
  1675. IRDataT<DCData> Data;
  1676. IRComparer<DCData>::analyzeIR(IR, Data);
  1677. // Now compare it against itself, which will have everything the
  1678. // same and will generate the files.
  1679. IRComparer<DCData>(Data, Data)
  1680. .compare(getModuleForComparison(IR),
  1681. [&](bool InModule, unsigned Minor,
  1682. const FuncDataT<DCData> &Before,
  1683. const FuncDataT<DCData> &After) -> void {
  1684. handleFunctionCompare("", " ", "Initial IR", "", InModule,
  1685. Minor, Before, After);
  1686. });
  1687. *HTML << " </p>\n"
  1688. << "</div><br/>\n";
  1689. ++N;
  1690. }
  1691. void DotCfgChangeReporter::generateIRRepresentation(Any IR, StringRef PassID,
  1692. IRDataT<DCData> &Data) {
  1693. IRComparer<DCData>::analyzeIR(IR, Data);
  1694. }
  1695. void DotCfgChangeReporter::omitAfter(StringRef PassID, std::string &Name) {
  1696. assert(HTML && "Expected outstream to be set");
  1697. SmallString<20> Banner =
  1698. formatv(" <a>{0}. Pass {1} on {2} omitted because no change</a><br/>\n",
  1699. N, makeHTMLReady(PassID), Name);
  1700. *HTML << Banner;
  1701. ++N;
  1702. }
  1703. void DotCfgChangeReporter::handleAfter(StringRef PassID, std::string &Name,
  1704. const IRDataT<DCData> &Before,
  1705. const IRDataT<DCData> &After, Any IR) {
  1706. assert(HTML && "Expected outstream to be set");
  1707. IRComparer<DCData>(Before, After)
  1708. .compare(getModuleForComparison(IR),
  1709. [&](bool InModule, unsigned Minor,
  1710. const FuncDataT<DCData> &Before,
  1711. const FuncDataT<DCData> &After) -> void {
  1712. handleFunctionCompare(Name, " Pass ", PassID, " on ", InModule,
  1713. Minor, Before, After);
  1714. });
  1715. *HTML << " </p></div>\n";
  1716. ++N;
  1717. }
  1718. void DotCfgChangeReporter::handleInvalidated(StringRef PassID) {
  1719. assert(HTML && "Expected outstream to be set");
  1720. SmallString<20> Banner =
  1721. formatv(" <a>{0}. {1} invalidated</a><br/>\n", N, makeHTMLReady(PassID));
  1722. *HTML << Banner;
  1723. ++N;
  1724. }
  1725. void DotCfgChangeReporter::handleFiltered(StringRef PassID, std::string &Name) {
  1726. assert(HTML && "Expected outstream to be set");
  1727. SmallString<20> Banner =
  1728. formatv(" <a>{0}. Pass {1} on {2} filtered out</a><br/>\n", N,
  1729. makeHTMLReady(PassID), Name);
  1730. *HTML << Banner;
  1731. ++N;
  1732. }
  1733. void DotCfgChangeReporter::handleIgnored(StringRef PassID, std::string &Name) {
  1734. assert(HTML && "Expected outstream to be set");
  1735. SmallString<20> Banner = formatv(" <a>{0}. {1} on {2} ignored</a><br/>\n", N,
  1736. makeHTMLReady(PassID), Name);
  1737. *HTML << Banner;
  1738. ++N;
  1739. }
  1740. bool DotCfgChangeReporter::initializeHTML() {
  1741. std::error_code EC;
  1742. HTML = std::make_unique<raw_fd_ostream>(DotCfgDir + "/passes.html", EC);
  1743. if (EC) {
  1744. HTML = nullptr;
  1745. return false;
  1746. }
  1747. *HTML << "<!doctype html>"
  1748. << "<html>"
  1749. << "<head>"
  1750. << "<style>.collapsible { "
  1751. << "background-color: #777;"
  1752. << " color: white;"
  1753. << " cursor: pointer;"
  1754. << " padding: 18px;"
  1755. << " width: 100%;"
  1756. << " border: none;"
  1757. << " text-align: left;"
  1758. << " outline: none;"
  1759. << " font-size: 15px;"
  1760. << "} .active, .collapsible:hover {"
  1761. << " background-color: #555;"
  1762. << "} .content {"
  1763. << " padding: 0 18px;"
  1764. << " display: none;"
  1765. << " overflow: hidden;"
  1766. << " background-color: #f1f1f1;"
  1767. << "}"
  1768. << "</style>"
  1769. << "<title>passes.html</title>"
  1770. << "</head>\n"
  1771. << "<body>";
  1772. return true;
  1773. }
  1774. DotCfgChangeReporter::~DotCfgChangeReporter() {
  1775. if (!HTML)
  1776. return;
  1777. *HTML
  1778. << "<script>var coll = document.getElementsByClassName(\"collapsible\");"
  1779. << "var i;"
  1780. << "for (i = 0; i < coll.length; i++) {"
  1781. << "coll[i].addEventListener(\"click\", function() {"
  1782. << " this.classList.toggle(\"active\");"
  1783. << " var content = this.nextElementSibling;"
  1784. << " if (content.style.display === \"block\"){"
  1785. << " content.style.display = \"none\";"
  1786. << " }"
  1787. << " else {"
  1788. << " content.style.display= \"block\";"
  1789. << " }"
  1790. << " });"
  1791. << " }"
  1792. << "</script>"
  1793. << "</body>"
  1794. << "</html>\n";
  1795. HTML->flush();
  1796. HTML->close();
  1797. }
  1798. void DotCfgChangeReporter::registerCallbacks(
  1799. PassInstrumentationCallbacks &PIC) {
  1800. if (PrintChanged == ChangePrinter::DotCfgVerbose ||
  1801. PrintChanged == ChangePrinter::DotCfgQuiet) {
  1802. SmallString<128> OutputDir;
  1803. sys::fs::expand_tilde(DotCfgDir, OutputDir);
  1804. sys::fs::make_absolute(OutputDir);
  1805. assert(!OutputDir.empty() && "expected output dir to be non-empty");
  1806. DotCfgDir = OutputDir.c_str();
  1807. if (initializeHTML()) {
  1808. ChangeReporter<IRDataT<DCData>>::registerRequiredCallbacks(PIC);
  1809. return;
  1810. }
  1811. dbgs() << "Unable to open output stream for -cfg-dot-changed\n";
  1812. }
  1813. }
  1814. StandardInstrumentations::StandardInstrumentations(
  1815. LLVMContext &Context, bool DebugLogging, bool VerifyEach,
  1816. PrintPassOptions PrintPassOpts)
  1817. : PrintPass(DebugLogging, PrintPassOpts),
  1818. OptNone(DebugLogging),
  1819. OptPassGate(Context),
  1820. PrintChangedIR(PrintChanged == ChangePrinter::Verbose),
  1821. PrintChangedDiff(PrintChanged == ChangePrinter::DiffVerbose ||
  1822. PrintChanged == ChangePrinter::ColourDiffVerbose,
  1823. PrintChanged == ChangePrinter::ColourDiffVerbose ||
  1824. PrintChanged == ChangePrinter::ColourDiffQuiet),
  1825. WebsiteChangeReporter(PrintChanged == ChangePrinter::DotCfgVerbose),
  1826. Verify(DebugLogging), VerifyEach(VerifyEach) {}
  1827. PrintCrashIRInstrumentation *PrintCrashIRInstrumentation::CrashReporter =
  1828. nullptr;
  1829. void PrintCrashIRInstrumentation::reportCrashIR() { dbgs() << SavedIR; }
  1830. void PrintCrashIRInstrumentation::SignalHandler(void *) {
  1831. // Called by signal handlers so do not lock here
  1832. // Is the PrintCrashIRInstrumentation still alive?
  1833. if (!CrashReporter)
  1834. return;
  1835. assert(PrintCrashIR && "Did not expect to get here without option set.");
  1836. CrashReporter->reportCrashIR();
  1837. }
  1838. PrintCrashIRInstrumentation::~PrintCrashIRInstrumentation() {
  1839. if (!CrashReporter)
  1840. return;
  1841. assert(PrintCrashIR && "Did not expect to get here without option set.");
  1842. CrashReporter = nullptr;
  1843. }
  1844. void PrintCrashIRInstrumentation::registerCallbacks(
  1845. PassInstrumentationCallbacks &PIC) {
  1846. if (!PrintCrashIR || CrashReporter)
  1847. return;
  1848. sys::AddSignalHandler(SignalHandler, nullptr);
  1849. CrashReporter = this;
  1850. PIC.registerBeforeNonSkippedPassCallback([&PIC, this](StringRef PassID,
  1851. Any IR) {
  1852. SavedIR.clear();
  1853. raw_string_ostream OS(SavedIR);
  1854. OS << formatv("*** Dump of {0}IR Before Last Pass {1}",
  1855. llvm::forcePrintModuleIR() ? "Module " : "", PassID);
  1856. if (!isInteresting(IR, PassID, PIC.getPassNameForClassName(PassID))) {
  1857. OS << " Filtered Out ***\n";
  1858. return;
  1859. }
  1860. OS << " Started ***\n";
  1861. unwrapAndPrint(OS, IR);
  1862. });
  1863. }
  1864. void StandardInstrumentations::registerCallbacks(
  1865. PassInstrumentationCallbacks &PIC, FunctionAnalysisManager *FAM) {
  1866. PrintIR.registerCallbacks(PIC);
  1867. PrintPass.registerCallbacks(PIC);
  1868. TimePasses.registerCallbacks(PIC);
  1869. OptNone.registerCallbacks(PIC);
  1870. OptPassGate.registerCallbacks(PIC);
  1871. if (FAM)
  1872. PreservedCFGChecker.registerCallbacks(PIC, *FAM);
  1873. PrintChangedIR.registerCallbacks(PIC);
  1874. PseudoProbeVerification.registerCallbacks(PIC);
  1875. if (VerifyEach)
  1876. Verify.registerCallbacks(PIC);
  1877. PrintChangedDiff.registerCallbacks(PIC);
  1878. WebsiteChangeReporter.registerCallbacks(PIC);
  1879. ChangeTester.registerCallbacks(PIC);
  1880. PrintCrashIR.registerCallbacks(PIC);
  1881. // TimeProfiling records the pass running time cost.
  1882. // Its 'BeforePassCallback' can be appended at the tail of all the
  1883. // BeforeCallbacks by calling `registerCallbacks` in the end.
  1884. // Its 'AfterPassCallback' is put at the front of all the
  1885. // AfterCallbacks by its `registerCallbacks`. This is necessary
  1886. // to ensure that other callbacks are not included in the timings.
  1887. TimeProfilingPasses.registerCallbacks(PIC);
  1888. }
  1889. template class ChangeReporter<std::string>;
  1890. template class TextChangeReporter<std::string>;
  1891. template class BlockDataT<EmptyData>;
  1892. template class FuncDataT<EmptyData>;
  1893. template class IRDataT<EmptyData>;
  1894. template class ChangeReporter<IRDataT<EmptyData>>;
  1895. template class TextChangeReporter<IRDataT<EmptyData>>;
  1896. template class IRComparer<EmptyData>;
  1897. } // namespace llvm