Lines Matching defs:IR

10 /// This file defines IR-printing pass instrumentation callbacks as well as
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/InstIterator.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/PassInstrumentation.h"
31 #include "llvm/IR/PassManager.h"
32 #include "llvm/IR/PrintPasses.h"
33 #include "llvm/IR/StructuralHash.h"
34 #include "llvm/IR/Verifier.h"
107 // Options to print the IR that was being processed when a pass crashes.
110 cl::desc("Print the last form of the IR before crash to a file"),
115 cl::desc("Print the last form of the IR before crash (use -print-on-crash-path to dump to a file)"),
120 cl::desc("Print IR to path when opt-bisect-limit is reached"), cl::Hidden);
128 cl::desc("Print IR before the pass with this number as "
133 cl::desc("Print IR after the pass with this number as "
138 cl::desc("If specified, IR printed using the "
148 template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
149 const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
155 // An option for specifying an executable that will be called with the IR
157 // the initial IR as it enters the pipeline. The executable will be passed
158 // the name of a temporary file containing the IR and the PassID. This may
159 // be used, for example, to call llc on the IR and run a test to determine
160 // which pass makes a change that changes the functioning of the IR.
164 cl::desc("exe called with module IR after each pass that "
167 /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
169 const Module *unwrapModule(Any IR, bool Force = false) {
170 if (const auto *M = unwrapIR<Module>(IR))
173 if (const auto *F = unwrapIR<Function>(IR)) {
180 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) {
191 if (const auto *L = unwrapIR<Loop>(IR)) {
198 if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
204 llvm_unreachable("Unknown IR unit");
245 std::string getIRName(Any IR) {
246 if (unwrapIR<Module>(IR))
249 if (const auto *F = unwrapIR<Function>(IR))
252 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
255 if (const auto *L = unwrapIR<Loop>(IR))
259 if (const auto *MF = unwrapIR<MachineFunction>(IR))
262 llvm_unreachable("Unknown wrapped IR type");
281 bool shouldPrintIR(Any IR) {
282 if (const auto *M = unwrapIR<Module>(IR))
285 if (const auto *F = unwrapIR<Function>(IR))
288 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
291 if (const auto *L = unwrapIR<Loop>(IR))
294 if (const auto *MF = unwrapIR<MachineFunction>(IR))
296 llvm_unreachable("Unknown wrapped IR type");
299 /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
301 void unwrapAndPrint(raw_ostream &OS, Any IR) {
302 if (!shouldPrintIR(IR))
306 auto *M = unwrapModule(IR);
312 if (const auto *M = unwrapIR<Module>(IR)) {
317 if (const auto *F = unwrapIR<Function>(IR)) {
322 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) {
327 if (const auto *L = unwrapIR<Loop>(IR)) {
332 if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
336 llvm_unreachable("Unknown wrapped IR type");
363 // Return the module when that is the appropriate level of comparison for \p IR.
364 const Module *getModuleForComparison(Any IR) {
365 if (const auto *M = unwrapIR<Module>(IR))
367 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
376 // Return true when this is a pass on IR for which printing
378 bool isInteresting(Any IR, StringRef PassID, StringRef PassName) {
381 if (const auto *F = unwrapIR<Function>(IR))
393 void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID,
395 // Is this the initial IR?
399 handleInitialIR(IR);
403 // are not given the IR so it cannot be determined whether the pass was for
407 if (!isInteresting(IR, PassID, PassName))
410 // Save the IR representation on the stack.
412 generateIRRepresentation(IR, PassID, Data);
416 void ChangeReporter<T>::handleIRAfterPass(Any IR, StringRef PassID,
420 std::string Name = getIRName(IR);
425 } else if (!isInteresting(IR, PassID, PassName)) {
433 generateIRRepresentation(IR, PassID, After);
435 // Was there a change in IR?
440 handleAfter(PassID, Name, Before, After, IR);
451 // get the IR in the call. Also, the output is just alternate
461 PIC.registerBeforeNonSkippedPassCallback([&PIC, this](StringRef P, Any IR) {
462 saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P));
466 [&PIC, this](StringRef P, Any IR, const PreservedAnalyses &) {
467 handleIRAfterPass(IR, P, PIC.getPassNameForClassName(P));
479 template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) {
482 auto *M = unwrapModule(IR, /*Force=*/true);
484 Out << "*** IR Dump At Start ***\n";
490 Out << formatv("*** IR Dump After {0} on {1} omitted because no change ***\n",
496 Out << formatv("*** IR Pass {0} invalidated ***\n", PassID);
503 formatv("*** IR Dump After {0} on {1} filtered out ***\n", PassID, Name);
509 Out << formatv("*** IR Pass {0} on {1} ignored ***\n", PassID, Name);
520 void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
523 unwrapAndPrint(OS, IR);
530 // Report the IR before the changes when requested.
532 Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n"
538 Out << "*** IR Deleted After " << PassID << " on " << Name << " ***\n";
542 Out << "*** IR Dump After " << PassID << " on " << Name << " ***\n" << After;
578 void IRChangedTester::handleInitialIR(Any IR) {
582 generateIRRepresentation(IR, "Initial IR", S);
583 handleIR(S, "Initial IR");
696 template <typename T> void IRComparer<T>::analyzeIR(Any IR, IRDataT<T> &Data) {
697 if (const Module *M = getModuleForComparison(IR)) {
704 if (const auto *F = unwrapIR<Function>(IR)) {
709 if (const auto *L = unwrapIR<Loop>(IR)) {
715 if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
720 llvm_unreachable("Unknown IR unit");
758 static SmallString<32> getIRFileDisplayName(Any IR) {
761 const Module *M = unwrapModule(IR, /*Force=*/true);
766 if (unwrapIR<Module>(IR)) {
768 } else if (const auto *F = unwrapIR<Function>(IR)) {
773 } else if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) {
777 } else if (const auto *L = unwrapIR<Loop>(IR)) {
781 } else if (const auto *MF = unwrapIR<MachineFunction>(IR)) {
787 llvm_unreachable("Unknown wrapped IR type");
793 Any IR) {
796 "The flag -ir-dump-directory must be passed to dump IR to files");
803 FilenameStream << getIRFileDisplayName(IR);
823 StringRef PassID, Any IR, std::string &DumpIRFilename) {
824 const Module *M = unwrapModule(IR);
826 PassRunDescriptor(M, DumpIRFilename, getIRName(IR), PassID));
856 void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
865 DumpIRFilename = fetchDumpFilename(PassID, IR);
872 pushPassRunDescriptor(PassID, IR, DumpIRFilename);
874 if (!shouldPrintIR(IR))
881 << " on " << getIRName(IR) << "\n";
884 pushPassRunDescriptor(PassID, IR, DumpIRFilename);
890 Stream << "; *** IR Dump Before ";
893 Stream << PassID << " on " << getIRName(IR) << " ***\n";
894 unwrapAndPrint(Stream, IR);
907 void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
917 if (!shouldPrintIR(IR) ||
922 Stream << "; *** IR Dump After ";
926 unwrapAndPrint(Stream, IR);
961 Banner = formatv("; *** IR Dump After {0} on {1} (invalidated) ***", PassID,
1030 [this](StringRef P, Any IR) { this->printBeforePass(P, IR); });
1034 [this](StringRef P, Any IR, const PreservedAnalyses &) {
1035 this->printAfterPass(P, IR);
1047 [this](StringRef P, Any IR) { return this->shouldRun(P, IR); });
1050 bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
1052 if (const auto *F = unwrapIR<Function>(IR))
1054 else if (const auto *L = unwrapIR<Loop>(IR))
1056 else if (const auto *MF = unwrapIR<MachineFunction>(IR))
1060 errs() << "Skipping pass " << PassID << " on " << getIRName(IR)
1066 bool OptPassGateInstrumentation::shouldRun(StringRef PassName, Any IR) {
1071 Context.getOptPassGate().shouldRunPass(PassName, getIRName(IR));
1073 // FIXME: print IR if limit is higher than number of opt-bisect
1076 const Module *M = unwrapModule(IR, /*Force=*/true);
1093 PIC.registerShouldRunOptionalPassCallback([this](StringRef PassName, Any IR) {
1094 return this->shouldRun(PassName, IR);
1118 Any IR) {
1122 print() << "Skipping pass: " << PassID << " on " << getIRName(IR) << "\n";
1125 StringRef PassID, Any IR) {
1130 OS << "Running pass: " << PassID << " on " << getIRName(IR);
1131 if (const auto *F = unwrapIR<Function>(IR)) {
1137 } else if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR)) {
1148 [this, SpecialPasses](StringRef PassID, Any IR,
1156 [this, SpecialPasses](StringRef PassID, Any IR) {
1164 PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) {
1165 print() << "Running analysis: " << PassID << " on " << getIRName(IR)
1170 [this](StringRef PassID, Any IR) { Indent -= 2; });
1171 PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) {
1172 print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR)
1350 static SmallVector<Function *, 1> GetFunctions(Any IR) {
1353 if (const auto *MaybeF = unwrapIR<Function>(IR)) {
1355 } else if (const auto *MaybeM = unwrapIR<Module>(IR)) {
1369 StringRef P, Any IR) mutable {
1376 *const_cast<Module *>(unwrapModule(IR, /*Force=*/true)))
1385 for (Function *F : GetFunctions(IR)) {
1391 if (const auto *MPtr = unwrapIR<Module>(IR)) {
1406 PIC.registerAfterPassCallback([this, &MAM](StringRef P, Any IR,
1418 *const_cast<Module *>(unwrapModule(IR, /*Force=*/true)))
1421 for (Function *F : GetFunctions(IR)) {
1450 if (const auto *MPtr = unwrapIR<Module>(IR)) {
1466 [this, MAM](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
1469 const auto *F = unwrapIR<Function>(IR);
1471 if (const auto *L = unwrapIR<Loop>(IR))
1484 const auto *M = unwrapIR<Module>(IR);
1486 if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
1500 if (auto *MF = unwrapIR<MachineFunction>(IR)) {
1524 void InLineChangePrinter::generateIRRepresentation(Any IR,
1527 IRComparer<EmptyData>::analyzeIR(IR, D);
1533 Any IR) {
1535 formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
1538 .compare(getModuleForComparison(IR),
1554 Out << "\n*** IR for function " << Name << " ***\n";
1584 [this](StringRef P, Any IR) { this->runBeforePass(P, IR); });
1586 [this](StringRef P, Any IR, const PreservedAnalyses &) {
1594 [this](StringRef P, Any IR) { this->runBeforePass(P, IR); });
1596 [this](StringRef P, Any IR) { this->runAfterPass(); }, true);
1599 void TimeProfilingPassesHandler::runBeforePass(StringRef PassID, Any IR) {
1600 timeTraceProfilerBegin(PassID, getIRName(IR));
1990 // Handle each basic block in the before IR.
2007 // Handle each basic block in the after IR
2013 // This only exists in the after IR. Create the node.
2297 void DotCfgChangeReporter::handleInitialIR(Any IR) {
2300 << "Initial IR (by function)</button>\n"
2303 // Create representation of IR
2305 IRComparer<DCData>::analyzeIR(IR, Data);
2309 .compare(getModuleForComparison(IR),
2313 handleFunctionCompare("", " ", "Initial IR", "", InModule,
2321 void DotCfgChangeReporter::generateIRRepresentation(Any IR, StringRef PassID,
2323 IRComparer<DCData>::analyzeIR(IR, Data);
2337 const IRDataT<DCData> &After, Any IR) {
2340 .compare(getModuleForComparison(IR),
2512 [&PIC, this](StringRef PassID, Any IR) {
2515 OS << formatv("*** Dump of {0}IR Before Last Pass {1}",
2517 if (!isInteresting(IR, PassID, PIC.getPassNameForClassName(PassID))) {
2522 unwrapAndPrint(OS, IR);