2018-09-24 18:08:15 +02:00
|
|
|
//===- Standard pass instrumentations handling ----------------*- C++ -*--===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-09-24 18:08:15 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file defines IR-printing pass instrumentation callbacks as well as
|
|
|
|
/// StandardInstrumentations class that manages standard pass instrumentations.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Passes/StandardInstrumentations.h"
|
2020-07-21 18:48:22 +02:00
|
|
|
#include "llvm/ADT/Any.h"
|
2018-12-21 12:49:05 +01:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2018-09-24 18:08:15 +02:00
|
|
|
#include "llvm/Analysis/CallGraphSCCPass.h"
|
|
|
|
#include "llvm/Analysis/LazyCallGraph.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/PassInstrumentation.h"
|
2020-07-21 18:48:22 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-09-24 18:08:15 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2020-07-29 02:08:24 +02:00
|
|
|
#include <vector>
|
2018-09-24 18:08:15 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-07-21 18:48:22 +02:00
|
|
|
// TODO: remove once all required passes are marked as such.
|
|
|
|
static cl::opt<bool>
|
|
|
|
EnableOptnone("enable-npm-optnone", cl::init(false),
|
|
|
|
cl::desc("Enable skipping optional passes optnone functions "
|
|
|
|
"under new pass manager"));
|
|
|
|
|
2020-07-29 02:08:24 +02:00
|
|
|
// FIXME: Change `-debug-pass-manager` from boolean to enum type. Similar to
|
|
|
|
// `-debug-pass` in legacy PM.
|
|
|
|
static cl::opt<bool>
|
|
|
|
DebugPMVerbose("debug-pass-manager-verbose", cl::Hidden, cl::init(false),
|
|
|
|
cl::desc("Print all pass management debugging information. "
|
|
|
|
"`-debug-pass-manager` must also be specified"));
|
|
|
|
|
2018-09-24 18:08:15 +02:00
|
|
|
namespace {
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
/// Extracting Module out of \p IR unit. Also fills a textual description
|
|
|
|
/// of \p IR for use in header when printing.
|
|
|
|
Optional<std::pair<const Module *, std::string>> unwrapModule(Any IR) {
|
|
|
|
if (any_isa<const Module *>(IR))
|
|
|
|
return std::make_pair(any_cast<const Module *>(IR), std::string());
|
2018-09-24 18:08:15 +02:00
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
if (any_isa<const Function *>(IR)) {
|
2018-09-24 18:08:15 +02:00
|
|
|
const Function *F = any_cast<const Function *>(IR);
|
|
|
|
if (!llvm::isFunctionInPrintList(F->getName()))
|
2018-12-21 12:49:05 +01:00
|
|
|
return None;
|
|
|
|
const Module *M = F->getParent();
|
|
|
|
return std::make_pair(M, formatv(" (function: {0})", F->getName()).str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const LazyCallGraph::SCC *>(IR)) {
|
2018-10-15 12:46:35 +02:00
|
|
|
const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
|
|
|
|
for (const LazyCallGraph::Node &N : *C) {
|
|
|
|
const Function &F = N.getFunction();
|
|
|
|
if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
|
2018-12-21 12:49:05 +01:00
|
|
|
const Module *M = F.getParent();
|
|
|
|
return std::make_pair(M, formatv(" (scc: {0})", C->getName()).str());
|
2018-10-15 12:46:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 12:49:05 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Loop *>(IR)) {
|
2018-09-24 18:08:15 +02:00
|
|
|
const Loop *L = any_cast<const Loop *>(IR);
|
|
|
|
const Function *F = L->getHeader()->getParent();
|
|
|
|
if (!isFunctionInPrintList(F->getName()))
|
2018-12-21 12:49:05 +01:00
|
|
|
return None;
|
|
|
|
const Module *M = F->getParent();
|
|
|
|
std::string LoopName;
|
|
|
|
raw_string_ostream ss(LoopName);
|
|
|
|
L->getHeader()->printAsOperand(ss, false);
|
|
|
|
return std::make_pair(M, formatv(" (loop: {0})", ss.str()).str());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown IR unit");
|
|
|
|
}
|
|
|
|
|
2020-08-18 18:05:20 +02:00
|
|
|
void printIR(raw_ostream &OS, const Function *F, StringRef Banner,
|
|
|
|
StringRef Extra = StringRef(), bool Brief = false) {
|
2020-07-29 02:08:24 +02:00
|
|
|
if (Brief) {
|
2020-08-18 18:05:20 +02:00
|
|
|
OS << F->getName() << '\n';
|
2020-07-29 02:08:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
if (!llvm::isFunctionInPrintList(F->getName()))
|
|
|
|
return;
|
2020-08-18 18:05:20 +02:00
|
|
|
OS << Banner << Extra << "\n" << static_cast<const Value &>(*F);
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
2020-02-24 00:13:27 +01:00
|
|
|
|
2020-08-18 18:05:20 +02:00
|
|
|
void printIR(raw_ostream &OS, const Module *M, StringRef Banner,
|
|
|
|
StringRef Extra = StringRef(), bool Brief = false) {
|
2020-07-29 02:08:24 +02:00
|
|
|
if (Brief) {
|
2020-08-18 18:05:20 +02:00
|
|
|
OS << M->getName() << '\n';
|
2020-07-29 02:08:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-24 00:13:27 +01:00
|
|
|
if (llvm::isFunctionInPrintList("*") || llvm::forcePrintModuleIR()) {
|
2020-08-18 18:05:20 +02:00
|
|
|
OS << Banner << Extra << "\n";
|
|
|
|
M->print(OS, nullptr, false);
|
2020-02-24 00:13:27 +01:00
|
|
|
} else {
|
|
|
|
for (const auto &F : M->functions()) {
|
2020-08-18 18:05:20 +02:00
|
|
|
printIR(OS, &F, Banner, Extra);
|
2020-02-24 00:13:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 18:05:20 +02:00
|
|
|
void printIR(raw_ostream &OS, const LazyCallGraph::SCC *C, StringRef Banner,
|
2020-07-29 02:08:24 +02:00
|
|
|
StringRef Extra = StringRef(), bool Brief = false) {
|
|
|
|
if (Brief) {
|
2020-08-18 18:05:20 +02:00
|
|
|
OS << *C << '\n';
|
2020-07-29 02:08:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
bool BannerPrinted = false;
|
|
|
|
for (const LazyCallGraph::Node &N : *C) {
|
|
|
|
const Function &F = N.getFunction();
|
|
|
|
if (!F.isDeclaration() && llvm::isFunctionInPrintList(F.getName())) {
|
|
|
|
if (!BannerPrinted) {
|
2020-08-18 18:05:20 +02:00
|
|
|
OS << Banner << Extra << "\n";
|
2018-12-21 12:49:05 +01:00
|
|
|
BannerPrinted = true;
|
|
|
|
}
|
2020-08-18 18:05:20 +02:00
|
|
|
F.print(OS);
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
2020-07-29 02:08:24 +02:00
|
|
|
|
2020-08-18 18:05:20 +02:00
|
|
|
void printIR(raw_ostream &OS, const Loop *L, StringRef Banner,
|
|
|
|
bool Brief = false) {
|
2020-07-29 02:08:24 +02:00
|
|
|
if (Brief) {
|
2020-08-18 18:05:20 +02:00
|
|
|
OS << *L;
|
2020-07-29 02:08:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
const Function *F = L->getHeader()->getParent();
|
|
|
|
if (!llvm::isFunctionInPrintList(F->getName()))
|
|
|
|
return;
|
2020-08-18 18:05:20 +02:00
|
|
|
llvm::printLoop(const_cast<Loop &>(*L), OS, std::string(Banner));
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into
|
|
|
|
/// llvm::Any and does actual print job.
|
2020-08-18 18:05:20 +02:00
|
|
|
void unwrapAndPrint(raw_ostream &OS, Any IR, StringRef Banner,
|
|
|
|
bool ForceModule = false, bool Brief = false) {
|
2018-12-21 12:49:05 +01:00
|
|
|
if (ForceModule) {
|
|
|
|
if (auto UnwrappedModule = unwrapModule(IR))
|
2020-08-18 18:05:20 +02:00
|
|
|
printIR(OS, UnwrappedModule->first, Banner, UnwrappedModule->second);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
2018-12-21 12:49:05 +01:00
|
|
|
|
|
|
|
if (any_isa<const Module *>(IR)) {
|
|
|
|
const Module *M = any_cast<const Module *>(IR);
|
|
|
|
assert(M && "module should be valid for printing");
|
2020-08-18 18:05:20 +02:00
|
|
|
printIR(OS, M, Banner, "", Brief);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Function *>(IR)) {
|
|
|
|
const Function *F = any_cast<const Function *>(IR);
|
|
|
|
assert(F && "function should be valid for printing");
|
2020-08-18 18:05:20 +02:00
|
|
|
printIR(OS, F, Banner, "", Brief);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const LazyCallGraph::SCC *>(IR)) {
|
|
|
|
const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
|
|
|
|
assert(C && "scc should be valid for printing");
|
2020-01-28 20:23:46 +01:00
|
|
|
std::string Extra = std::string(formatv(" (scc: {0})", C->getName()));
|
2020-08-18 18:05:20 +02:00
|
|
|
printIR(OS, C, Banner, Extra, Brief);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Loop *>(IR)) {
|
|
|
|
const Loop *L = any_cast<const Loop *>(IR);
|
|
|
|
assert(L && "Loop should be valid for printing");
|
2020-08-18 18:05:20 +02:00
|
|
|
printIR(OS, L, Banner, Brief);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown wrapped IR type");
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
} // namespace
|
2018-09-24 18:08:15 +02:00
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
PrintIRInstrumentation::~PrintIRInstrumentation() {
|
|
|
|
assert(ModuleDescStack.empty() && "ModuleDescStack is not empty at exit");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) {
|
|
|
|
assert(StoreModuleDesc);
|
|
|
|
const Module *M = nullptr;
|
|
|
|
std::string Extra;
|
|
|
|
if (auto UnwrappedModule = unwrapModule(IR))
|
|
|
|
std::tie(M, Extra) = UnwrappedModule.getValue();
|
|
|
|
ModuleDescStack.emplace_back(M, Extra, PassID);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintIRInstrumentation::PrintModuleDesc
|
|
|
|
PrintIRInstrumentation::popModuleDesc(StringRef PassID) {
|
|
|
|
assert(!ModuleDescStack.empty() && "empty ModuleDescStack");
|
|
|
|
PrintModuleDesc ModuleDesc = ModuleDescStack.pop_back_val();
|
|
|
|
assert(std::get<2>(ModuleDesc).equals(PassID) && "malformed ModuleDescStack");
|
|
|
|
return ModuleDesc;
|
|
|
|
}
|
|
|
|
|
2020-07-29 01:31:46 +02:00
|
|
|
void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) {
|
2018-09-24 18:08:15 +02:00
|
|
|
if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<"))
|
2020-07-29 01:31:46 +02:00
|
|
|
return;
|
2018-09-24 18:08:15 +02:00
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
// Saving Module for AfterPassInvalidated operations.
|
|
|
|
// Note: here we rely on a fact that we do not change modules while
|
|
|
|
// traversing the pipeline, so the latest captured module is good
|
|
|
|
// for all print operations that has not happen yet.
|
|
|
|
if (StoreModuleDesc && llvm::shouldPrintAfterPass(PassID))
|
|
|
|
pushModuleDesc(PassID, IR);
|
|
|
|
|
|
|
|
if (!llvm::shouldPrintBeforePass(PassID))
|
2020-07-29 01:31:46 +02:00
|
|
|
return;
|
2018-12-21 12:49:05 +01:00
|
|
|
|
2018-09-24 18:08:15 +02:00
|
|
|
SmallString<20> Banner = formatv("*** IR Dump Before {0} ***", PassID);
|
2020-08-18 18:05:20 +02:00
|
|
|
unwrapAndPrint(dbgs(), IR, Banner, llvm::forcePrintModuleIR());
|
2020-07-29 01:31:46 +02:00
|
|
|
return;
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
|
|
|
|
if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<"))
|
|
|
|
return;
|
|
|
|
|
2018-09-24 18:08:15 +02:00
|
|
|
if (!llvm::shouldPrintAfterPass(PassID))
|
|
|
|
return;
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
if (StoreModuleDesc)
|
|
|
|
popModuleDesc(PassID);
|
|
|
|
|
|
|
|
SmallString<20> Banner = formatv("*** IR Dump After {0} ***", PassID);
|
2020-08-18 18:05:20 +02:00
|
|
|
unwrapAndPrint(dbgs(), IR, Banner, llvm::forcePrintModuleIR());
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
|
|
|
|
if (!StoreModuleDesc || !llvm::shouldPrintAfterPass(PassID))
|
|
|
|
return;
|
|
|
|
|
2018-09-24 18:08:15 +02:00
|
|
|
if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<"))
|
|
|
|
return;
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
const Module *M;
|
|
|
|
std::string Extra;
|
|
|
|
StringRef StoredPassID;
|
|
|
|
std::tie(M, Extra, StoredPassID) = popModuleDesc(PassID);
|
|
|
|
// Additional filtering (e.g. -filter-print-func) can lead to module
|
|
|
|
// printing being skipped.
|
|
|
|
if (!M)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallString<20> Banner =
|
|
|
|
formatv("*** IR Dump After {0} *** invalidated: ", PassID);
|
2020-08-18 18:05:20 +02:00
|
|
|
printIR(dbgs(), M, Banner, Extra);
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintIRInstrumentation::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
|
|
|
// BeforePass callback is not just for printing, it also saves a Module
|
|
|
|
// for later use in AfterPassInvalidated.
|
|
|
|
StoreModuleDesc = llvm::forcePrintModuleIR() && llvm::shouldPrintAfterPass();
|
|
|
|
if (llvm::shouldPrintBeforePass() || StoreModuleDesc)
|
2020-07-29 01:31:46 +02:00
|
|
|
PIC.registerBeforeNonSkippedPassCallback(
|
|
|
|
[this](StringRef P, Any IR) { this->printBeforePass(P, IR); });
|
2018-12-21 12:49:05 +01:00
|
|
|
|
|
|
|
if (llvm::shouldPrintAfterPass()) {
|
|
|
|
PIC.registerAfterPassCallback(
|
2020-08-21 10:52:26 +02:00
|
|
|
[this](StringRef P, Any IR, const PreservedAnalyses &) {
|
|
|
|
this->printAfterPass(P, IR);
|
|
|
|
});
|
2018-12-21 12:49:05 +01:00
|
|
|
PIC.registerAfterPassInvalidatedCallback(
|
2020-08-21 10:52:26 +02:00
|
|
|
[this](StringRef P, const PreservedAnalyses &) {
|
|
|
|
this->printAfterPassInvalidated(P);
|
|
|
|
});
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 18:48:22 +02:00
|
|
|
void OptNoneInstrumentation::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
|
|
|
PIC.registerBeforePassCallback(
|
|
|
|
[this](StringRef P, Any IR) { return this->skip(P, IR); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptNoneInstrumentation::skip(StringRef PassID, Any IR) {
|
|
|
|
if (!EnableOptnone)
|
|
|
|
return true;
|
|
|
|
const Function *F = nullptr;
|
|
|
|
if (any_isa<const Function *>(IR)) {
|
|
|
|
F = any_cast<const Function *>(IR);
|
|
|
|
} else if (any_isa<const Loop *>(IR)) {
|
|
|
|
F = any_cast<const Loop *>(IR)->getHeader()->getParent();
|
|
|
|
}
|
2020-08-07 04:03:09 +02:00
|
|
|
return !(F && F->hasOptNone());
|
2020-07-21 18:48:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 02:08:24 +02:00
|
|
|
void PrintPassInstrumentation::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
|
|
|
if (!DebugLogging)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<StringRef> SpecialPasses = {"PassManager"};
|
|
|
|
if (!DebugPMVerbose)
|
|
|
|
SpecialPasses.emplace_back("PassAdaptor");
|
|
|
|
|
2020-08-07 04:03:09 +02:00
|
|
|
PIC.registerBeforeSkippedPassCallback(
|
|
|
|
[SpecialPasses](StringRef PassID, Any IR) {
|
|
|
|
assert(!isSpecialPass(PassID, SpecialPasses) &&
|
|
|
|
"Unexpectedly skipping special pass");
|
|
|
|
|
|
|
|
dbgs() << "Skipping pass: " << PassID << " on ";
|
2020-08-18 18:05:20 +02:00
|
|
|
unwrapAndPrint(dbgs(), IR, "", false, true);
|
2020-08-07 04:03:09 +02:00
|
|
|
});
|
|
|
|
|
2020-07-29 02:08:24 +02:00
|
|
|
PIC.registerBeforeNonSkippedPassCallback(
|
|
|
|
[SpecialPasses](StringRef PassID, Any IR) {
|
|
|
|
if (isSpecialPass(PassID, SpecialPasses))
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbgs() << "Running pass: " << PassID << " on ";
|
2020-08-18 18:05:20 +02:00
|
|
|
unwrapAndPrint(dbgs(), IR, "", false, true);
|
2020-07-29 02:08:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
PIC.registerBeforeAnalysisCallback([](StringRef PassID, Any IR) {
|
|
|
|
dbgs() << "Running analysis: " << PassID << " on ";
|
2020-08-18 18:05:20 +02:00
|
|
|
unwrapAndPrint(dbgs(), IR, "", false, true);
|
2020-07-29 02:08:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:08:15 +02:00
|
|
|
void StandardInstrumentations::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
2018-12-21 12:49:05 +01:00
|
|
|
PrintIR.registerCallbacks(PIC);
|
2020-07-29 02:08:24 +02:00
|
|
|
PrintPass.registerCallbacks(PIC);
|
2018-10-06 00:32:01 +02:00
|
|
|
TimePasses.registerCallbacks(PIC);
|
2020-07-21 18:48:22 +02:00
|
|
|
OptNone.registerCallbacks(PIC);
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|