|
| 1 | +//===- Standard pass instrumentations handling ----------------*- C++ -*--===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +/// \file |
| 10 | +/// |
| 11 | +/// This file defines IR-printing pass instrumentation callbacks as well as |
| 12 | +/// StandardInstrumentations class that manages standard pass instrumentations. |
| 13 | +/// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "llvm/Passes/StandardInstrumentations.h" |
| 17 | +#include "llvm/Analysis/CallGraphSCCPass.h" |
| 18 | +#include "llvm/Analysis/LazyCallGraph.h" |
| 19 | +#include "llvm/Analysis/LoopInfo.h" |
| 20 | +#include "llvm/IR/Function.h" |
| 21 | +#include "llvm/IR/IRPrintingPasses.h" |
| 22 | +#include "llvm/IR/Module.h" |
| 23 | +#include "llvm/IR/PassInstrumentation.h" |
| 24 | +#include "llvm/Support/Debug.h" |
| 25 | +#include "llvm/Support/FormatVariadic.h" |
| 26 | +#include "llvm/Support/raw_ostream.h" |
| 27 | + |
| 28 | +using namespace llvm; |
| 29 | + |
| 30 | +namespace { |
| 31 | +namespace PrintIR { |
| 32 | + |
| 33 | +//===----------------------------------------------------------------------===// |
| 34 | +// IR-printing instrumentation |
| 35 | +//===----------------------------------------------------------------------===// |
| 36 | + |
| 37 | +/// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into |
| 38 | +/// llvm::Any and does actual print job. |
| 39 | +void unwrapAndPrint(StringRef Banner, Any IR) { |
| 40 | + if (any_isa<const CallGraphSCC *>(IR) || |
| 41 | + any_isa<const LazyCallGraph::SCC *>(IR)) |
| 42 | + return; |
| 43 | + |
| 44 | + SmallString<40> Extra{"\n"}; |
| 45 | + const Module *M = nullptr; |
| 46 | + if (any_isa<const Module *>(IR)) { |
| 47 | + M = any_cast<const Module *>(IR); |
| 48 | + } else if (any_isa<const Function *>(IR)) { |
| 49 | + const Function *F = any_cast<const Function *>(IR); |
| 50 | + if (!llvm::isFunctionInPrintList(F->getName())) |
| 51 | + return; |
| 52 | + if (!llvm::forcePrintModuleIR()) { |
| 53 | + dbgs() << Banner << Extra << static_cast<const Value &>(*F); |
| 54 | + return; |
| 55 | + } |
| 56 | + M = F->getParent(); |
| 57 | + Extra = formatv(" (function: {0})\n", F->getName()); |
| 58 | + } else if (any_isa<const Loop *>(IR)) { |
| 59 | + const Loop *L = any_cast<const Loop *>(IR); |
| 60 | + const Function *F = L->getHeader()->getParent(); |
| 61 | + if (!isFunctionInPrintList(F->getName())) |
| 62 | + return; |
| 63 | + if (!llvm::forcePrintModuleIR()) { |
| 64 | + llvm::printLoop(const_cast<Loop &>(*L), dbgs(), Banner); |
| 65 | + return; |
| 66 | + } |
| 67 | + M = F->getParent(); |
| 68 | + { |
| 69 | + std::string LoopName; |
| 70 | + raw_string_ostream ss(LoopName); |
| 71 | + L->getHeader()->printAsOperand(ss, false); |
| 72 | + Extra = formatv(" (loop: {0})\n", ss.str()); |
| 73 | + } |
| 74 | + } |
| 75 | + if (M) { |
| 76 | + dbgs() << Banner << Extra; |
| 77 | + M->print(dbgs(), nullptr, false); |
| 78 | + } else { |
| 79 | + llvm_unreachable("Unknown wrapped IR type"); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +bool printBeforePass(StringRef PassID, Any IR) { |
| 84 | + if (!llvm::shouldPrintBeforePass(PassID)) |
| 85 | + return true; |
| 86 | + |
| 87 | + if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<")) |
| 88 | + return true; |
| 89 | + |
| 90 | + SmallString<20> Banner = formatv("*** IR Dump Before {0} ***", PassID); |
| 91 | + unwrapAndPrint(Banner, IR); |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +void printAfterPass(StringRef PassID, Any IR) { |
| 96 | + if (!llvm::shouldPrintAfterPass(PassID)) |
| 97 | + return; |
| 98 | + |
| 99 | + if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<")) |
| 100 | + return; |
| 101 | + |
| 102 | + SmallString<20> Banner = formatv("*** IR Dump After {0} ***", PassID); |
| 103 | + unwrapAndPrint(Banner, IR); |
| 104 | + return; |
| 105 | +} |
| 106 | +} // namespace PrintIR |
| 107 | +} // namespace |
| 108 | + |
| 109 | +void StandardInstrumentations::registerCallbacks( |
| 110 | + PassInstrumentationCallbacks &PIC) { |
| 111 | + if (llvm::shouldPrintBeforePass()) |
| 112 | + PIC.registerBeforePassCallback(PrintIR::printBeforePass); |
| 113 | + if (llvm::shouldPrintAfterPass()) |
| 114 | + PIC.registerAfterPassCallback(PrintIR::printAfterPass); |
| 115 | +} |
0 commit comments