Lines Matching full:ir

10 /// This file defines IR-printing pass instrumentation callbacks as well as
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/PassInstrumentation.h"
30 #include "llvm/IR/PassManager.h"
31 #include "llvm/IR/PrintPasses.h"
32 #include "llvm/IR/StructuralHash.h"
33 #include "llvm/IR/Verifier.h"
105 // Options to print the IR that was being processed when a pass crashes.
108 cl::desc("Print the last form of the IR before crash to a file"),
113 …cl::desc("Print the last form of the IR before crash (use -print-on-crash-path to dump to a file)"…
117 "opt-bisect-print-ir-path",
118 cl::desc("Print IR to path when opt-bisect-limit is reached"), cl::Hidden);
126 cl::desc("Print IR before the pass with this number as "
131 cl::desc("Print IR after the pass with this number as "
135 "ir-dump-directory",
136 cl::desc("If specified, IR printed using the "
141 template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) { in unwrapIR() argument
142 const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR); in unwrapIR()
148 // An option for specifying an executable that will be called with the IR
150 // the initial IR as it enters the pipeline. The executable will be passed
151 // the name of a temporary file containing the IR and the PassID. This may
152 // be used, for example, to call llc on the IR and run a test to determine
153 // which pass makes a change that changes the functioning of the IR.
156 TestChanged("exec-on-ir-change", cl::Hidden, cl::init(""),
157 cl::desc("exe called with module IR after each pass that "
160 /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
162 const Module *unwrapModule(Any IR, bool Force = false) { in unwrapModule() argument
163 if (const auto *M = unwrapIR<Module>(IR)) in unwrapModule()
166 if (const auto *F = unwrapIR<Function>(IR)) { in unwrapModule()
173 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) { in unwrapModule()
184 if (const auto *L = unwrapIR<Loop>(IR)) { in unwrapModule()
191 if (const auto *MF = unwrapIR<MachineFunction>(IR)) { in unwrapModule()
197 llvm_unreachable("Unknown IR unit"); in unwrapModule()
238 std::string getIRName(Any IR) { in getIRName() argument
239 if (unwrapIR<Module>(IR)) in getIRName()
242 if (const auto *F = unwrapIR<Function>(IR)) in getIRName()
245 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) in getIRName()
248 if (const auto *L = unwrapIR<Loop>(IR)) in getIRName()
252 if (const auto *MF = unwrapIR<MachineFunction>(IR)) in getIRName()
255 llvm_unreachable("Unknown wrapped IR type"); in getIRName()
274 bool shouldPrintIR(Any IR) { in shouldPrintIR() argument
275 if (const auto *M = unwrapIR<Module>(IR)) in shouldPrintIR()
278 if (const auto *F = unwrapIR<Function>(IR)) in shouldPrintIR()
281 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) in shouldPrintIR()
284 if (const auto *L = unwrapIR<Loop>(IR)) in shouldPrintIR()
287 if (const auto *MF = unwrapIR<MachineFunction>(IR)) in shouldPrintIR()
289 llvm_unreachable("Unknown wrapped IR type"); in shouldPrintIR()
292 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
294 void unwrapAndPrint(raw_ostream &OS, Any IR) { in unwrapAndPrint() argument
295 if (!shouldPrintIR(IR)) in unwrapAndPrint()
299 auto *M = unwrapModule(IR); in unwrapAndPrint()
305 if (const auto *M = unwrapIR<Module>(IR)) { in unwrapAndPrint()
310 if (const auto *F = unwrapIR<Function>(IR)) { in unwrapAndPrint()
315 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) { in unwrapAndPrint()
320 if (const auto *L = unwrapIR<Loop>(IR)) { in unwrapAndPrint()
325 if (const auto *MF = unwrapIR<MachineFunction>(IR)) { in unwrapAndPrint()
329 llvm_unreachable("Unknown wrapped IR type"); in unwrapAndPrint()
356 // Return the module when that is the appropriate level of comparison for \p IR.
357 const Module *getModuleForComparison(Any IR) { in getModuleForComparison() argument
358 if (const auto *M = unwrapIR<Module>(IR)) in getModuleForComparison()
360 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) in getModuleForComparison()
369 // Return true when this is a pass on IR for which printing
371 bool isInteresting(Any IR, StringRef PassID, StringRef PassName) { in isInteresting() argument
374 if (const auto *F = unwrapIR<Function>(IR)) in isInteresting()
386 void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID, in saveIRBeforePass() argument
388 // Is this the initial IR? in saveIRBeforePass()
392 handleInitialIR(IR); in saveIRBeforePass()
396 // are not given the IR so it cannot be determined whether the pass was for in saveIRBeforePass()
400 if (!isInteresting(IR, PassID, PassName)) in saveIRBeforePass()
403 // Save the IR representation on the stack. in saveIRBeforePass()
405 generateIRRepresentation(IR, PassID, Data); in saveIRBeforePass()
409 void ChangeReporter<T>::handleIRAfterPass(Any IR, StringRef PassID, in handleIRAfterPass() argument
413 std::string Name = getIRName(IR); in handleIRAfterPass()
418 } else if (!isInteresting(IR, PassID, PassName)) { in handleIRAfterPass()
426 generateIRRepresentation(IR, PassID, After); in handleIRAfterPass()
428 // Was there a change in IR? in handleIRAfterPass()
433 handleAfter(PassID, Name, Before, After, IR); in handleIRAfterPass()
444 // get the IR in the call. Also, the output is just alternate in handleInvalidatedPass()
454 PIC.registerBeforeNonSkippedPassCallback([&PIC, this](StringRef P, Any IR) { in registerRequiredCallbacks() argument
455 saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P)); in registerRequiredCallbacks()
459 [&PIC, this](StringRef P, Any IR, const PreservedAnalyses &) { in registerRequiredCallbacks() argument
460 handleIRAfterPass(IR, P, PIC.getPassNameForClassName(P)); in registerRequiredCallbacks()
472 template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) { in handleInitialIR() argument
475 auto *M = unwrapModule(IR, /*Force=*/true); in handleInitialIR()
477 Out << "*** IR Dump At Start ***\n"; in handleInitialIR()
483 Out << formatv("*** IR Dump After {0} on {1} omitted because no change ***\n", in omitAfter()
489 Out << formatv("*** IR Pass {0} invalidated ***\n", PassID); in handleInvalidated()
496 formatv("*** IR Dump After {0} on {1} filtered out ***\n", PassID, Name); in handleFiltered()
502 Out << formatv("*** IR Pass {0} on {1} ignored ***\n", PassID, Name); in handleIgnored()
513 void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID, in generateIRRepresentation() argument
516 unwrapAndPrint(OS, IR); in generateIRRepresentation()
523 // Report the IR before the changes when requested. in handleAfter()
525 Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n" in handleAfter()
531 Out << "*** IR Deleted After " << PassID << " on " << Name << " ***\n"; in handleAfter()
535 Out << "*** IR Dump After " << PassID << " on " << Name << " ***\n" << After; in handleAfter()
571 void IRChangedTester::handleInitialIR(Any IR) { in handleInitialIR() argument
575 generateIRRepresentation(IR, "Initial IR", S); in handleInitialIR()
576 handleIR(S, "Initial IR"); in handleInitialIR()
689 template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) { in analyzeIR() argument
690 if (const Module *M = getModuleForComparison(IR)) { in analyzeIR()
697 if (const auto *F = unwrapIR<Function>(IR)) { in analyzeIR()
702 if (const auto *L = unwrapIR<Loop>(IR)) { in analyzeIR()
708 if (const auto *MF = unwrapIR<MachineFunction>(IR)) { in analyzeIR()
713 llvm_unreachable("Unknown IR unit"); in analyzeIR()
751 static SmallString<32> getIRFileDisplayName(Any IR) { in getIRFileDisplayName() argument
754 const Module *M = unwrapModule(IR); in getIRFileDisplayName()
758 if (unwrapIR<Module>(IR)) { in getIRFileDisplayName()
760 } else if (const auto *F = unwrapIR<Function>(IR)) { in getIRFileDisplayName()
765 } else if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) { in getIRFileDisplayName()
769 } else if (const auto *L = unwrapIR<Loop>(IR)) { in getIRFileDisplayName()
773 } else if (const auto *MF = unwrapIR<MachineFunction>(IR)) { in getIRFileDisplayName()
780 llvm_unreachable("Unknown wrapped IR type"); in getIRFileDisplayName()
786 Any IR) { in fetchDumpFilename() argument
789 "The flag -ir-dump-directory must be passed to dump IR to files"); in fetchDumpFilename()
796 FilenameStream << getIRFileDisplayName(IR); in fetchDumpFilename()
816 StringRef PassID, Any IR, std::string &DumpIRFilename) { in pushPassRunDescriptor() argument
817 const Module *M = unwrapModule(IR); in pushPassRunDescriptor()
819 PassRunDescriptor(M, DumpIRFilename, getIRName(IR), PassID)); in pushPassRunDescriptor()
838 " to support -ir-dump-directory: " + EC.message()); in prepareDumpIRFileDescriptor()
845 " to support -ir-dump-directory: " + EC.message()); in prepareDumpIRFileDescriptor()
849 void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) { in printBeforePass() argument
858 DumpIRFilename = fetchDumpFilename(PassID, IR); in printBeforePass()
865 pushPassRunDescriptor(PassID, IR, DumpIRFilename); in printBeforePass()
867 if (!shouldPrintIR(IR)) in printBeforePass()
874 << " on " << getIRName(IR) << "\n"; in printBeforePass()
877 pushPassRunDescriptor(PassID, IR, DumpIRFilename); in printBeforePass()
883 Stream << "; *** IR Dump Before "; in printBeforePass()
886 Stream << PassID << " on " << getIRName(IR) << " ***\n"; in printBeforePass()
887 unwrapAndPrint(Stream, IR); in printBeforePass()
900 void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) { in printAfterPass() argument
910 if (!shouldPrintIR(IR) || in printAfterPass()
915 Stream << "; *** IR Dump After "; in printAfterPass()
919 unwrapAndPrint(Stream, IR); in printAfterPass()
954 Banner = formatv("; *** IR Dump After {0} on {1} (invalidated) ***", PassID, in printAfterPassInvalidated()
1023 [this](StringRef P, Any IR) { this->printBeforePass(P, IR); }); in registerCallbacks() argument
1027 [this](StringRef P, Any IR, const PreservedAnalyses &) { in registerCallbacks() argument
1028 this->printAfterPass(P, IR); in registerCallbacks()
1040 [this](StringRef P, Any IR) { return this->shouldRun(P, IR); }); in registerCallbacks() argument
1043 bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) { in shouldRun() argument
1044 const auto *F = unwrapIR<Function>(IR); in shouldRun()
1046 if (const auto *L = unwrapIR<Loop>(IR)) in shouldRun()
1057 bool OptPassGateInstrumentation::shouldRun(StringRef PassName, Any IR) { in shouldRun() argument
1062 Context.getOptPassGate().shouldRunPass(PassName, getIRName(IR)); in shouldRun()
1064 // FIXME: print IR if limit is higher than number of opt-bisect in shouldRun()
1067 const Module *M = unwrapModule(IR, /*Force=*/true); in shouldRun()
1084 PIC.registerShouldRunOptionalPassCallback([this](StringRef PassName, Any IR) { in registerCallbacks() argument
1085 return this->shouldRun(PassName, IR); in registerCallbacks()
1109 Any IR) { in registerCallbacks() argument
1113 print() << "Skipping pass: " << PassID << " on " << getIRName(IR) << "\n"; in registerCallbacks()
1116 StringRef PassID, Any IR) { in registerCallbacks() argument
1121 OS << "Running pass: " << PassID << " on " << getIRName(IR); in registerCallbacks()
1122 if (const auto *F = unwrapIR<Function>(IR)) { in registerCallbacks()
1128 } else if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) { in registerCallbacks()
1139 [this, SpecialPasses](StringRef PassID, Any IR, in registerCallbacks()
1147 [this, SpecialPasses](StringRef PassID, Any IR) { in registerCallbacks() argument
1155 PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) { in registerCallbacks() argument
1156 print() << "Running analysis: " << PassID << " on " << getIRName(IR) in registerCallbacks()
1161 [this](StringRef PassID, Any IR) { Indent -= 2; }); in registerCallbacks() argument
1162 PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) { in registerCallbacks() argument
1163 print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR) in registerCallbacks()
1341 static SmallVector<Function *, 1> GetFunctions(Any IR) { in GetFunctions() argument
1344 if (const auto *MaybeF = unwrapIR<Function>(IR)) { in GetFunctions()
1346 } else if (const auto *MaybeM = unwrapIR<Module>(IR)) { in GetFunctions()
1360 StringRef P, Any IR) mutable { in registerCallbacks() argument
1367 *const_cast<Module *>(unwrapModule(IR, /*Force=*/true))) in registerCallbacks()
1376 for (Function *F : GetFunctions(IR)) { in registerCallbacks()
1382 if (const auto *MPtr = unwrapIR<Module>(IR)) { in registerCallbacks()
1397 PIC.registerAfterPassCallback([this, &MAM](StringRef P, Any IR, in registerCallbacks()
1409 *const_cast<Module *>(unwrapModule(IR, /*Force=*/true))) in registerCallbacks()
1412 for (Function *F : GetFunctions(IR)) { in registerCallbacks()
1441 if (const auto *MPtr = unwrapIR<Module>(IR)) { in registerCallbacks()
1457 [this, MAM](StringRef P, Any IR, const PreservedAnalyses &PassPA) { in registerCallbacks() argument
1460 const auto *F = unwrapIR<Function>(IR); in registerCallbacks()
1462 if (const auto *L = unwrapIR<Loop>(IR)) in registerCallbacks()
1475 const auto *M = unwrapIR<Module>(IR); in registerCallbacks()
1477 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) in registerCallbacks()
1491 if (auto *MF = unwrapIR<MachineFunction>(IR)) { in registerCallbacks()
1515 void InLineChangePrinter::generateIRRepresentation(Any IR, in generateIRRepresentation() argument
1518 IRComparer<EmptyData>::analyzeIR(IR, D); in generateIRRepresentation()
1524 Any IR) { in handleAfter() argument
1526 formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name); in handleAfter()
1529 .compare(getModuleForComparison(IR), in handleAfter()
1545 Out << "\n*** IR for function " << Name << " ***\n"; in handleFunctionCompare()
1575 [this](StringRef P, Any IR) { this->runBeforePass(P, IR); }); in registerCallbacks() argument
1577 [this](StringRef P, Any IR, const PreservedAnalyses &) { in registerCallbacks() argument
1585 [this](StringRef P, Any IR) { this->runBeforePass(P, IR); }); in registerCallbacks() argument
1587 [this](StringRef P, Any IR) { this->runAfterPass(); }, true); in registerCallbacks() argument
1590 void TimeProfilingPassesHandler::runBeforePass(StringRef PassID, Any IR) { in runBeforePass() argument
1591 timeTraceProfilerBegin(PassID, getIRName(IR)); in runBeforePass()
1981 // Handle each basic block in the before IR. in DotCfgDiff()
1998 // Handle each basic block in the after IR in DotCfgDiff()
2004 // This only exists in the after IR. Create the node. in DotCfgDiff()
2287 void DotCfgChangeReporter::handleInitialIR(Any IR) { in handleInitialIR() argument
2290 << "Initial IR (by function)</button>\n" in handleInitialIR()
2293 // Create representation of IR in handleInitialIR()
2295 IRComparer<DCData>::analyzeIR(IR, Data); in handleInitialIR()
2299 .compare(getModuleForComparison(IR), in handleInitialIR()
2303 handleFunctionCompare("", " ", "Initial IR", "", InModule, in handleInitialIR()
2311 void DotCfgChangeReporter::generateIRRepresentation(Any IR, StringRef PassID, in generateIRRepresentation() argument
2313 IRComparer<DCData>::analyzeIR(IR, Data); in generateIRRepresentation()
2327 const IRDataT<DCData> &After, Any IR) { in handleAfter() argument
2330 .compare(getModuleForComparison(IR), in handleAfter()
2502 [&PIC, this](StringRef PassID, Any IR) { in registerCallbacks() argument
2505 OS << formatv("*** Dump of {0}IR Before Last Pass {1}", in registerCallbacks()
2507 if (!isInteresting(IR, PassID, PIC.getPassNameForClassName(PassID))) { in registerCallbacks()
2512 unwrapAndPrint(OS, IR); in registerCallbacks()