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"
|
2020-12-03 19:59:10 +01:00
|
|
|
#include "llvm/ADT/StringRef.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"
|
2021-04-29 09:29:42 +02:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2018-09-24 18:08:15 +02:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/PassInstrumentation.h"
|
2021-04-06 06:55:18 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2020-12-03 19:59:10 +01:00
|
|
|
#include "llvm/IR/PrintPasses.h"
|
2020-09-22 18:34:46 +02:00
|
|
|
#include "llvm/IR/Verifier.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"
|
2021-02-08 16:09:34 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2018-09-24 18:08:15 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2020-10-01 19:39:02 +02:00
|
|
|
#include <unordered_set>
|
2020-07-29 02:08:24 +02:00
|
|
|
#include <vector>
|
2018-09-24 18:08:15 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-09-11 07:55:24 +02:00
|
|
|
cl::opt<bool> PreservedCFGCheckerInstrumentation::VerifyPreservedCFG(
|
|
|
|
"verify-cfg-preserved", cl::Hidden,
|
|
|
|
#ifdef NDEBUG
|
|
|
|
cl::init(false));
|
|
|
|
#else
|
2021-04-07 03:34:30 +02:00
|
|
|
cl::init(true));
|
2020-09-11 07:55:24 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-01 19:39:02 +02:00
|
|
|
// An option that prints out the IR after passes, similar to
|
|
|
|
// -print-after-all except that it only prints the IR after passes that
|
|
|
|
// change the IR. Those passes that do not make changes to the IR are
|
|
|
|
// reported as not making any changes. In addition, the initial IR is
|
|
|
|
// also reported. Other hidden options affect the output from this
|
|
|
|
// option. -filter-passes will limit the output to the named passes
|
|
|
|
// that actually change the IR and other passes are reported as filtered out.
|
|
|
|
// The specified passes will either be reported as making no changes (with
|
|
|
|
// no IR reported) or the changed IR will be reported. Also, the
|
|
|
|
// -filter-print-funcs and -print-module-scope options will do similar
|
|
|
|
// filtering based on function name, reporting changed IRs as functions(or
|
|
|
|
// modules if -print-module-scope is specified) for a particular function
|
|
|
|
// or indicating that the IR has been filtered out. The extra options
|
|
|
|
// can be combined, allowing only changed IRs for certain passes on certain
|
|
|
|
// functions to be reported in different formats, with the rest being
|
2020-11-12 16:20:46 +01:00
|
|
|
// reported as filtered out. The -print-before-changed option will print
|
2021-01-11 20:11:00 +01:00
|
|
|
// the IR as it was before each pass that changed it. The optional
|
|
|
|
// value of quiet will only report when the IR changes, suppressing
|
2021-02-08 16:09:34 +01:00
|
|
|
// all other messages, including the initial IR. The values "diff" and
|
|
|
|
// "diff-quiet" will present the changes in a form similar to a patch, in
|
|
|
|
// either verbose or quiet mode, respectively. The lines that are removed
|
|
|
|
// and added are prefixed with '-' and '+', respectively. The
|
|
|
|
// -filter-print-funcs and -filter-passes can be used to filter the output.
|
|
|
|
// This reporter relies on the linux diff utility to do comparisons and
|
|
|
|
// insert the prefixes. For systems that do not have the necessary
|
|
|
|
// facilities, the error message will be shown in place of the expected output.
|
|
|
|
//
|
|
|
|
enum class ChangePrinter {
|
|
|
|
NoChangePrinter,
|
|
|
|
PrintChangedVerbose,
|
|
|
|
PrintChangedQuiet,
|
|
|
|
PrintChangedDiffVerbose,
|
2021-03-25 15:32:13 +01:00
|
|
|
PrintChangedDiffQuiet,
|
|
|
|
PrintChangedColourDiffVerbose,
|
|
|
|
PrintChangedColourDiffQuiet
|
2021-02-08 16:09:34 +01:00
|
|
|
};
|
2021-01-11 20:11:00 +01:00
|
|
|
static cl::opt<ChangePrinter> PrintChanged(
|
|
|
|
"print-changed", cl::desc("Print changed IRs"), cl::Hidden,
|
2021-02-08 16:09:34 +01:00
|
|
|
cl::ValueOptional, cl::init(ChangePrinter::NoChangePrinter),
|
2021-03-25 15:32:13 +01:00
|
|
|
cl::values(
|
|
|
|
clEnumValN(ChangePrinter::PrintChangedQuiet, "quiet",
|
|
|
|
"Run in quiet mode"),
|
|
|
|
clEnumValN(ChangePrinter::PrintChangedDiffVerbose, "diff",
|
|
|
|
"Display patch-like changes"),
|
|
|
|
clEnumValN(ChangePrinter::PrintChangedDiffQuiet, "diff-quiet",
|
|
|
|
"Display patch-like changes in quiet mode"),
|
|
|
|
clEnumValN(ChangePrinter::PrintChangedColourDiffVerbose, "cdiff",
|
|
|
|
"Display patch-like changes with color"),
|
|
|
|
clEnumValN(ChangePrinter::PrintChangedColourDiffQuiet, "cdiff-quiet",
|
|
|
|
"Display patch-like changes in quiet mode with color"),
|
|
|
|
// Sentinel value for unspecified option.
|
|
|
|
clEnumValN(ChangePrinter::PrintChangedVerbose, "", "")));
|
2021-01-11 20:11:00 +01:00
|
|
|
|
2020-10-01 19:39:02 +02:00
|
|
|
// An option that supports the -print-changed option. See
|
|
|
|
// the description for -print-changed for an explanation of the use
|
|
|
|
// of this option. Note that this option has no effect without -print-changed.
|
|
|
|
static cl::list<std::string>
|
|
|
|
PrintPassesList("filter-passes", cl::value_desc("pass names"),
|
|
|
|
cl::desc("Only consider IR changes for passes whose names "
|
|
|
|
"match for the print-changed option"),
|
|
|
|
cl::CommaSeparated, cl::Hidden);
|
2020-11-12 16:20:46 +01:00
|
|
|
// An option that supports the -print-changed option. See
|
|
|
|
// the description for -print-changed for an explanation of the use
|
|
|
|
// of this option. Note that this option has no effect without -print-changed.
|
|
|
|
static cl::opt<bool>
|
|
|
|
PrintChangedBefore("print-before-changed",
|
|
|
|
cl::desc("Print before passes that change them"),
|
|
|
|
cl::init(false), cl::Hidden);
|
2020-10-01 19:39:02 +02:00
|
|
|
|
2021-02-08 16:09:34 +01:00
|
|
|
// An option for specifying the diff used by print-changed=[diff | diff-quiet]
|
|
|
|
static cl::opt<std::string>
|
|
|
|
DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"),
|
|
|
|
cl::desc("system diff used by change reporters"));
|
|
|
|
|
2018-09-24 18:08:15 +02:00
|
|
|
namespace {
|
|
|
|
|
2021-02-08 16:09:34 +01:00
|
|
|
// Perform a system based diff between \p Before and \p After, using
|
|
|
|
// \p OldLineFormat, \p NewLineFormat, and \p UnchangedLineFormat
|
|
|
|
// to control the formatting of the output. Return an error message
|
|
|
|
// for any failures instead of the diff.
|
|
|
|
std::string doSystemDiff(StringRef Before, StringRef After,
|
|
|
|
StringRef OldLineFormat, StringRef NewLineFormat,
|
|
|
|
StringRef UnchangedLineFormat) {
|
|
|
|
StringRef SR[2]{Before, After};
|
|
|
|
// Store the 2 bodies into temporary files and call diff on them
|
|
|
|
// to get the body of the node.
|
|
|
|
const unsigned NumFiles = 3;
|
2021-05-27 16:19:13 +02:00
|
|
|
static std::string FileName[NumFiles];
|
|
|
|
static int FD[NumFiles]{-1, -1, -1};
|
2021-02-08 16:09:34 +01:00
|
|
|
for (unsigned I = 0; I < NumFiles; ++I) {
|
|
|
|
if (FD[I] == -1) {
|
|
|
|
SmallVector<char, 200> SV;
|
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::createTemporaryFile("tmpdiff", "txt", FD[I], SV);
|
|
|
|
if (EC)
|
|
|
|
return "Unable to create temporary file.";
|
|
|
|
FileName[I] = Twine(SV).str();
|
|
|
|
}
|
|
|
|
// The third file is used as the result of the diff.
|
|
|
|
if (I == NumFiles - 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::error_code EC = sys::fs::openFileForWrite(FileName[I], FD[I]);
|
|
|
|
if (EC)
|
|
|
|
return "Unable to open temporary file for writing.";
|
|
|
|
|
|
|
|
raw_fd_ostream OutStream(FD[I], /*shouldClose=*/true);
|
|
|
|
if (FD[I] == -1)
|
|
|
|
return "Error opening file for writing.";
|
|
|
|
OutStream << SR[I];
|
|
|
|
}
|
|
|
|
|
|
|
|
static ErrorOr<std::string> DiffExe = sys::findProgramByName(DiffBinary);
|
|
|
|
if (!DiffExe)
|
|
|
|
return "Unable to find diff executable.";
|
|
|
|
|
|
|
|
SmallString<128> OLF = formatv("--old-line-format={0}", OldLineFormat);
|
|
|
|
SmallString<128> NLF = formatv("--new-line-format={0}", NewLineFormat);
|
|
|
|
SmallString<128> ULF =
|
|
|
|
formatv("--unchanged-line-format={0}", UnchangedLineFormat);
|
|
|
|
|
|
|
|
StringRef Args[] = {"-w", "-d", OLF, NLF, ULF, FileName[0], FileName[1]};
|
|
|
|
Optional<StringRef> Redirects[] = {None, StringRef(FileName[2]), None};
|
|
|
|
int Result = sys::ExecuteAndWait(*DiffExe, Args, None, Redirects);
|
|
|
|
if (Result < 0)
|
|
|
|
return "Error executing system diff.";
|
|
|
|
std::string Diff;
|
|
|
|
auto B = MemoryBuffer::getFile(FileName[2]);
|
|
|
|
if (B && *B)
|
|
|
|
Diff = (*B)->getBuffer().str();
|
|
|
|
else
|
|
|
|
return "Unable to read result.";
|
|
|
|
|
|
|
|
// Clean up.
|
|
|
|
for (unsigned I = 0; I < NumFiles; ++I) {
|
|
|
|
std::error_code EC = sys::fs::remove(FileName[I]);
|
|
|
|
if (EC)
|
|
|
|
return "Unable to remove temporary file.";
|
|
|
|
}
|
|
|
|
return Diff;
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
/// Extract Module out of \p IR unit. May return nullptr if \p IR does not match
|
|
|
|
/// certain global filters. Will never return nullptr if \p Force is true.
|
|
|
|
const Module *unwrapModule(Any IR, bool Force = false) {
|
2018-12-21 12:49:05 +01:00
|
|
|
if (any_isa<const Module *>(IR))
|
2021-04-14 23:52:50 +02:00
|
|
|
return any_cast<const Module *>(IR);
|
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);
|
2020-12-03 19:59:10 +01:00
|
|
|
if (!Force && !isFunctionInPrintList(F->getName()))
|
2021-04-14 23:52:50 +02:00
|
|
|
return nullptr;
|
2020-10-01 19:39:02 +02:00
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
return F->getParent();
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2020-10-01 19:39:02 +02:00
|
|
|
if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) {
|
2021-04-14 23:52:50 +02:00
|
|
|
return F.getParent();
|
2018-10-15 12:46:35 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 23:52:50 +02:00
|
|
|
assert(!Force && "Expected a module");
|
|
|
|
return nullptr;
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2020-10-01 19:39:02 +02:00
|
|
|
if (!Force && !isFunctionInPrintList(F->getName()))
|
2021-04-14 23:52:50 +02:00
|
|
|
return nullptr;
|
|
|
|
return F->getParent();
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown IR unit");
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
void printIR(raw_ostream &OS, const Function *F) {
|
2020-12-03 19:59:10 +01:00
|
|
|
if (!isFunctionInPrintList(F->getName()))
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
2021-04-14 23:52:50 +02:00
|
|
|
OS << *F;
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
2020-02-24 00:13:27 +01:00
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
void printIR(raw_ostream &OS, const Module *M,
|
2020-10-01 19:39:02 +02:00
|
|
|
bool ShouldPreserveUseListOrder = false) {
|
2020-12-03 19:59:10 +01:00
|
|
|
if (isFunctionInPrintList("*") || forcePrintModuleIR()) {
|
2020-10-01 19:39:02 +02:00
|
|
|
M->print(OS, nullptr, ShouldPreserveUseListOrder);
|
2020-02-24 00:13:27 +01:00
|
|
|
} else {
|
|
|
|
for (const auto &F : M->functions()) {
|
2021-04-14 23:52:50 +02:00
|
|
|
printIR(OS, &F);
|
2020-02-24 00:13:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
void printIR(raw_ostream &OS, const LazyCallGraph::SCC *C) {
|
2018-12-21 12:49:05 +01:00
|
|
|
for (const LazyCallGraph::Node &N : *C) {
|
|
|
|
const Function &F = N.getFunction();
|
2020-12-03 19:59:10 +01:00
|
|
|
if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
|
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
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
void printIR(raw_ostream &OS, const Loop *L) {
|
2018-12-21 12:49:05 +01:00
|
|
|
const Function *F = L->getHeader()->getParent();
|
2020-12-03 19:59:10 +01:00
|
|
|
if (!isFunctionInPrintList(F->getName()))
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
2021-04-14 23:52:50 +02:00
|
|
|
printLoop(const_cast<Loop &>(*L), OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getIRName(Any IR) {
|
|
|
|
if (any_isa<const Module *>(IR))
|
|
|
|
return "[module]";
|
|
|
|
|
|
|
|
if (any_isa<const Function *>(IR)) {
|
|
|
|
const Function *F = any_cast<const Function *>(IR);
|
|
|
|
return F->getName().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const LazyCallGraph::SCC *>(IR)) {
|
|
|
|
const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
|
|
|
|
return C->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Loop *>(IR)) {
|
|
|
|
const Loop *L = any_cast<const Loop *>(IR);
|
|
|
|
std::string S;
|
|
|
|
raw_string_ostream OS(S);
|
|
|
|
L->print(OS, /*Verbose*/ false, /*PrintNested*/ false);
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown wrapped IR type");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moduleContainsFilterPrintFunc(const Module &M) {
|
|
|
|
return any_of(M.functions(),
|
|
|
|
[](const Function &F) {
|
|
|
|
return isFunctionInPrintList(F.getName());
|
|
|
|
}) ||
|
|
|
|
isFunctionInPrintList("*");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sccContainsFilterPrintFunc(const LazyCallGraph::SCC &C) {
|
|
|
|
return any_of(C,
|
|
|
|
[](const LazyCallGraph::Node &N) {
|
|
|
|
return isFunctionInPrintList(N.getName());
|
|
|
|
}) ||
|
|
|
|
isFunctionInPrintList("*");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldPrintIR(Any IR) {
|
|
|
|
if (any_isa<const Module *>(IR)) {
|
|
|
|
const Module *M = any_cast<const Module *>(IR);
|
|
|
|
return moduleContainsFilterPrintFunc(*M);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Function *>(IR)) {
|
|
|
|
const Function *F = any_cast<const Function *>(IR);
|
|
|
|
return isFunctionInPrintList(F->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const LazyCallGraph::SCC *>(IR)) {
|
|
|
|
const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR);
|
|
|
|
return sccContainsFilterPrintFunc(*C);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Loop *>(IR)) {
|
|
|
|
const Loop *L = any_cast<const Loop *>(IR);
|
|
|
|
return isFunctionInPrintList(L->getHeader()->getParent()->getName());
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown wrapped IR type");
|
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.
|
2021-04-14 23:52:50 +02:00
|
|
|
void unwrapAndPrint(raw_ostream &OS, Any IR,
|
2020-10-01 19:39:02 +02:00
|
|
|
bool ShouldPreserveUseListOrder = false) {
|
2021-04-14 23:52:50 +02:00
|
|
|
if (!shouldPrintIR(IR))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (forcePrintModuleIR()) {
|
|
|
|
auto *M = unwrapModule(IR);
|
|
|
|
assert(M && "should have unwrapped module");
|
|
|
|
printIR(OS, M, ShouldPreserveUseListOrder);
|
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);
|
2021-04-14 23:52:50 +02:00
|
|
|
printIR(OS, M, ShouldPreserveUseListOrder);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Function *>(IR)) {
|
|
|
|
const Function *F = any_cast<const Function *>(IR);
|
2021-04-14 23:52:50 +02:00
|
|
|
printIR(OS, F);
|
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);
|
2021-04-14 23:52:50 +02:00
|
|
|
printIR(OS, C);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (any_isa<const Loop *>(IR)) {
|
|
|
|
const Loop *L = any_cast<const Loop *>(IR);
|
2021-04-14 23:52:50 +02:00
|
|
|
printIR(OS, L);
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown wrapped IR type");
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 19:39:02 +02:00
|
|
|
// Return true when this is a pass for which changes should be ignored
|
2020-09-22 18:34:46 +02:00
|
|
|
bool isIgnored(StringRef PassID) {
|
2020-10-01 19:39:02 +02:00
|
|
|
return isSpecialPass(PassID,
|
2021-04-22 08:51:51 +02:00
|
|
|
{"PassManager", "PassAdaptor", "AnalysisManagerProxy",
|
|
|
|
"DevirtSCCRepeatedPass", "ModuleInlinerWrapperPass"});
|
2020-10-01 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
|
|
|
ChangeReporter<IRUnitT>::~ChangeReporter<IRUnitT>() {
|
|
|
|
assert(BeforeStack.empty() && "Problem with Change Printer stack.");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
|
|
|
bool ChangeReporter<IRUnitT>::isInterestingFunction(const Function &F) {
|
2020-12-03 19:59:10 +01:00
|
|
|
return isFunctionInPrintList(F.getName());
|
2020-10-01 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
template <typename IRUnitT>
|
|
|
|
bool ChangeReporter<IRUnitT>::isInterestingPass(StringRef PassID) {
|
2020-10-01 19:39:02 +02:00
|
|
|
if (isIgnored(PassID))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
static std::unordered_set<std::string> PrintPassNames(PrintPassesList.begin(),
|
|
|
|
PrintPassesList.end());
|
|
|
|
return PrintPassNames.empty() || PrintPassNames.count(PassID.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true when this is a pass on IR for which printing
|
|
|
|
// of changes is desired.
|
2020-11-20 15:43:06 +01:00
|
|
|
template <typename IRUnitT>
|
|
|
|
bool ChangeReporter<IRUnitT>::isInteresting(Any IR, StringRef PassID) {
|
2020-10-01 19:39:02 +02:00
|
|
|
if (!isInterestingPass(PassID))
|
|
|
|
return false;
|
|
|
|
if (any_isa<const Function *>(IR))
|
|
|
|
return isInterestingFunction(*any_cast<const Function *>(IR));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
2020-11-20 15:43:06 +01:00
|
|
|
void ChangeReporter<IRUnitT>::saveIRBeforePass(Any IR, StringRef PassID) {
|
2020-10-01 19:39:02 +02:00
|
|
|
// Always need to place something on the stack because invalidated passes
|
|
|
|
// are not given the IR so it cannot be determined whether the pass was for
|
|
|
|
// something that was filtered out.
|
|
|
|
BeforeStack.emplace_back();
|
|
|
|
|
|
|
|
if (!isInteresting(IR, PassID))
|
|
|
|
return;
|
|
|
|
// Is this the initial IR?
|
|
|
|
if (InitialIR) {
|
|
|
|
InitialIR = false;
|
2021-01-11 20:11:00 +01:00
|
|
|
if (VerboseMode)
|
|
|
|
handleInitialIR(IR);
|
2020-10-01 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the IR representation on the stack.
|
|
|
|
IRUnitT &Data = BeforeStack.back();
|
|
|
|
generateIRRepresentation(IR, PassID, Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
2020-11-20 15:43:06 +01:00
|
|
|
void ChangeReporter<IRUnitT>::handleIRAfterPass(Any IR, StringRef PassID) {
|
2020-10-01 19:39:02 +02:00
|
|
|
assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
|
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
std::string Name = getIRName(IR);
|
2020-10-01 19:39:02 +02:00
|
|
|
|
2021-01-11 20:11:00 +01:00
|
|
|
if (isIgnored(PassID)) {
|
|
|
|
if (VerboseMode)
|
|
|
|
handleIgnored(PassID, Name);
|
|
|
|
} else if (!isInteresting(IR, PassID)) {
|
|
|
|
if (VerboseMode)
|
|
|
|
handleFiltered(PassID, Name);
|
|
|
|
} else {
|
2020-10-01 19:39:02 +02:00
|
|
|
// Get the before rep from the stack
|
|
|
|
IRUnitT &Before = BeforeStack.back();
|
|
|
|
// Create the after rep
|
|
|
|
IRUnitT After;
|
|
|
|
generateIRRepresentation(IR, PassID, After);
|
|
|
|
|
|
|
|
// Was there a change in IR?
|
2021-01-11 20:11:00 +01:00
|
|
|
if (same(Before, After)) {
|
|
|
|
if (VerboseMode)
|
|
|
|
omitAfter(PassID, Name);
|
|
|
|
} else
|
2020-10-01 19:39:02 +02:00
|
|
|
handleAfter(PassID, Name, Before, After, IR);
|
|
|
|
}
|
|
|
|
BeforeStack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
2020-11-20 15:43:06 +01:00
|
|
|
void ChangeReporter<IRUnitT>::handleInvalidatedPass(StringRef PassID) {
|
2020-10-01 19:39:02 +02:00
|
|
|
assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");
|
|
|
|
|
|
|
|
// Always flag it as invalidated as we cannot determine when
|
|
|
|
// a pass for a filtered function is invalidated since we do not
|
|
|
|
// get the IR in the call. Also, the output is just alternate
|
|
|
|
// forms of the banner anyway.
|
2021-01-11 20:11:00 +01:00
|
|
|
if (VerboseMode)
|
|
|
|
handleInvalidated(PassID);
|
2020-10-01 19:39:02 +02:00
|
|
|
BeforeStack.pop_back();
|
|
|
|
}
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
template <typename IRUnitT>
|
|
|
|
void ChangeReporter<IRUnitT>::registerRequiredCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
2020-11-01 07:08:15 +01:00
|
|
|
PIC.registerBeforeNonSkippedPassCallback(
|
|
|
|
[this](StringRef P, Any IR) { saveIRBeforePass(IR, P); });
|
2020-10-01 19:39:02 +02:00
|
|
|
|
|
|
|
PIC.registerAfterPassCallback(
|
|
|
|
[this](StringRef P, Any IR, const PreservedAnalyses &) {
|
|
|
|
handleIRAfterPass(IR, P);
|
|
|
|
});
|
|
|
|
PIC.registerAfterPassInvalidatedCallback(
|
|
|
|
[this](StringRef P, const PreservedAnalyses &) {
|
|
|
|
handleInvalidatedPass(P);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:09:34 +01:00
|
|
|
ChangedBlockData::ChangedBlockData(const BasicBlock &B)
|
|
|
|
: Label(B.getName().str()) {
|
|
|
|
raw_string_ostream SS(Body);
|
|
|
|
B.print(SS, nullptr, true, true);
|
|
|
|
}
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
template <typename IRUnitT>
|
2021-01-11 20:11:00 +01:00
|
|
|
TextChangeReporter<IRUnitT>::TextChangeReporter(bool Verbose)
|
|
|
|
: ChangeReporter<IRUnitT>(Verbose), Out(dbgs()) {}
|
2020-11-20 15:43:06 +01:00
|
|
|
|
|
|
|
template <typename IRUnitT>
|
|
|
|
void TextChangeReporter<IRUnitT>::handleInitialIR(Any IR) {
|
2020-10-01 19:39:02 +02:00
|
|
|
// Always print the module.
|
|
|
|
// Unwrap and print directly to avoid filtering problems in general routines.
|
2021-04-14 23:52:50 +02:00
|
|
|
auto *M = unwrapModule(IR, /*Force=*/true);
|
|
|
|
assert(M && "Expected module to be unwrapped when forced.");
|
|
|
|
Out << "*** IR Dump At Start ***\n";
|
|
|
|
M->print(Out, nullptr,
|
|
|
|
/*ShouldPreserveUseListOrder=*/true);
|
2020-10-01 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
template <typename IRUnitT>
|
|
|
|
void TextChangeReporter<IRUnitT>::omitAfter(StringRef PassID,
|
|
|
|
std::string &Name) {
|
2021-04-14 23:52:50 +02:00
|
|
|
Out << formatv("*** IR Dump After {0} on {1} omitted because no change ***\n",
|
2020-11-20 15:43:06 +01:00
|
|
|
PassID, Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
|
|
|
void TextChangeReporter<IRUnitT>::handleInvalidated(StringRef PassID) {
|
|
|
|
Out << formatv("*** IR Pass {0} invalidated ***\n", PassID);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
|
|
|
void TextChangeReporter<IRUnitT>::handleFiltered(StringRef PassID,
|
|
|
|
std::string &Name) {
|
|
|
|
SmallString<20> Banner =
|
2021-04-14 23:52:50 +02:00
|
|
|
formatv("*** IR Dump After {0} on {1} filtered out ***\n", PassID, Name);
|
2020-11-20 15:43:06 +01:00
|
|
|
Out << Banner;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IRUnitT>
|
|
|
|
void TextChangeReporter<IRUnitT>::handleIgnored(StringRef PassID,
|
|
|
|
std::string &Name) {
|
2021-04-14 23:52:50 +02:00
|
|
|
Out << formatv("*** IR Pass {0} on {1} ignored ***\n", PassID, Name);
|
2020-11-20 15:43:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IRChangedPrinter::~IRChangedPrinter() {}
|
|
|
|
|
|
|
|
void IRChangedPrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
|
2021-02-08 16:09:34 +01:00
|
|
|
if (PrintChanged == ChangePrinter::PrintChangedVerbose ||
|
|
|
|
PrintChanged == ChangePrinter::PrintChangedQuiet)
|
2020-11-20 15:43:06 +01:00
|
|
|
TextChangeReporter<std::string>::registerRequiredCallbacks(PIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRChangedPrinter::generateIRRepresentation(Any IR, StringRef PassID,
|
|
|
|
std::string &Output) {
|
2020-10-01 19:39:02 +02:00
|
|
|
raw_string_ostream OS(Output);
|
2021-04-14 23:52:50 +02:00
|
|
|
unwrapAndPrint(OS, IR,
|
|
|
|
/*ShouldPreserveUseListOrder=*/true);
|
2020-11-20 15:43:06 +01:00
|
|
|
OS.str();
|
2020-10-01 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
void IRChangedPrinter::handleAfter(StringRef PassID, std::string &Name,
|
|
|
|
const std::string &Before,
|
|
|
|
const std::string &After, Any) {
|
2021-04-14 23:52:50 +02:00
|
|
|
// Report the IR before the changes when requested.
|
|
|
|
if (PrintChangedBefore)
|
|
|
|
Out << "*** IR Dump Before " << PassID << " on " << Name << " ***\n"
|
|
|
|
<< Before;
|
|
|
|
|
2021-04-09 23:13:29 +02:00
|
|
|
// We might not get anything to print if we only want to print a specific
|
|
|
|
// function but it gets deleted.
|
|
|
|
if (After.empty()) {
|
2021-04-14 23:52:50 +02:00
|
|
|
Out << "*** IR Deleted After " << PassID << " on " << Name << " ***\n";
|
2021-04-09 23:13:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
Out << "*** IR Dump After " << PassID << " on " << Name << " ***\n" << After;
|
2020-10-01 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
bool IRChangedPrinter::same(const std::string &S1, const std::string &S2) {
|
|
|
|
return S1 == S2;
|
2020-10-01 19:39:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 16:09:34 +01:00
|
|
|
template <typename IRData>
|
|
|
|
void OrderedChangedData<IRData>::report(
|
|
|
|
const OrderedChangedData &Before, const OrderedChangedData &After,
|
|
|
|
function_ref<void(const IRData *, const IRData *)> HandlePair) {
|
|
|
|
const auto &BFD = Before.getData();
|
|
|
|
const auto &AFD = After.getData();
|
|
|
|
std::vector<std::string>::const_iterator BI = Before.getOrder().begin();
|
|
|
|
std::vector<std::string>::const_iterator BE = Before.getOrder().end();
|
|
|
|
std::vector<std::string>::const_iterator AI = After.getOrder().begin();
|
|
|
|
std::vector<std::string>::const_iterator AE = After.getOrder().end();
|
|
|
|
|
|
|
|
auto handlePotentiallyRemovedIRData = [&](std::string S) {
|
|
|
|
// The order in LLVM may have changed so check if still exists.
|
|
|
|
if (!AFD.count(S)) {
|
|
|
|
// This has been removed.
|
|
|
|
HandlePair(&BFD.find(*BI)->getValue(), nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto handleNewIRData = [&](std::vector<const IRData *> &Q) {
|
|
|
|
// Print out any queued up new sections
|
|
|
|
for (const IRData *NBI : Q)
|
|
|
|
HandlePair(nullptr, NBI);
|
|
|
|
Q.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Print out the IRData in the after order, with before ones interspersed
|
|
|
|
// appropriately (ie, somewhere near where they were in the before list).
|
|
|
|
// Start at the beginning of both lists. Loop through the
|
|
|
|
// after list. If an element is common, then advance in the before list
|
|
|
|
// reporting the removed ones until the common one is reached. Report any
|
|
|
|
// queued up new ones and then report the common one. If an element is not
|
|
|
|
// common, then enqueue it for reporting. When the after list is exhausted,
|
|
|
|
// loop through the before list, reporting any removed ones. Finally,
|
|
|
|
// report the rest of the enqueued new ones.
|
|
|
|
std::vector<const IRData *> NewIRDataQueue;
|
|
|
|
while (AI != AE) {
|
|
|
|
if (!BFD.count(*AI)) {
|
|
|
|
// This section is new so place it in the queue. This will cause it
|
|
|
|
// to be reported after deleted sections.
|
|
|
|
NewIRDataQueue.emplace_back(&AFD.find(*AI)->getValue());
|
|
|
|
++AI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// This section is in both; advance and print out any before-only
|
|
|
|
// until we get to it.
|
|
|
|
while (*BI != *AI) {
|
|
|
|
handlePotentiallyRemovedIRData(*BI);
|
|
|
|
++BI;
|
|
|
|
}
|
|
|
|
// Report any new sections that were queued up and waiting.
|
|
|
|
handleNewIRData(NewIRDataQueue);
|
|
|
|
|
|
|
|
const IRData &AData = AFD.find(*AI)->getValue();
|
|
|
|
const IRData &BData = BFD.find(*AI)->getValue();
|
|
|
|
HandlePair(&BData, &AData);
|
|
|
|
++BI;
|
|
|
|
++AI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check any remaining before sections to see if they have been removed
|
|
|
|
while (BI != BE) {
|
|
|
|
handlePotentiallyRemovedIRData(*BI);
|
|
|
|
++BI;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNewIRData(NewIRDataQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangedIRComparer::compare(Any IR, StringRef Prefix, StringRef PassID,
|
|
|
|
StringRef Name) {
|
|
|
|
if (!getModuleForComparison(IR)) {
|
|
|
|
// Not a module so just handle the single function.
|
|
|
|
assert(Before.getData().size() == 1 && "Expected only one function.");
|
|
|
|
assert(After.getData().size() == 1 && "Expected only one function.");
|
|
|
|
handleFunctionCompare(Name, Prefix, PassID, false,
|
|
|
|
Before.getData().begin()->getValue(),
|
|
|
|
After.getData().begin()->getValue());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChangedIRData::report(
|
|
|
|
Before, After, [&](const ChangedFuncData *B, const ChangedFuncData *A) {
|
|
|
|
ChangedFuncData Missing;
|
|
|
|
if (!B)
|
|
|
|
B = &Missing;
|
|
|
|
else if (!A)
|
|
|
|
A = &Missing;
|
|
|
|
assert(B != &Missing && A != &Missing &&
|
|
|
|
"Both functions cannot be missing.");
|
|
|
|
handleFunctionCompare(Name, Prefix, PassID, true, *B, *A);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangedIRComparer::analyzeIR(Any IR, ChangedIRData &Data) {
|
|
|
|
if (const Module *M = getModuleForComparison(IR)) {
|
|
|
|
// Create data for each existing/interesting function in the module.
|
|
|
|
for (const Function &F : *M)
|
|
|
|
generateFunctionData(Data, F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Function *F = nullptr;
|
|
|
|
if (any_isa<const Function *>(IR))
|
|
|
|
F = any_cast<const Function *>(IR);
|
|
|
|
else {
|
|
|
|
assert(any_isa<const Loop *>(IR) && "Unknown IR unit.");
|
|
|
|
const Loop *L = any_cast<const Loop *>(IR);
|
|
|
|
F = L->getHeader()->getParent();
|
|
|
|
}
|
|
|
|
assert(F && "Unknown IR unit.");
|
|
|
|
generateFunctionData(Data, *F);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Module *ChangedIRComparer::getModuleForComparison(Any IR) {
|
|
|
|
if (any_isa<const Module *>(IR))
|
|
|
|
return any_cast<const Module *>(IR);
|
|
|
|
if (any_isa<const LazyCallGraph::SCC *>(IR))
|
|
|
|
return any_cast<const LazyCallGraph::SCC *>(IR)
|
|
|
|
->begin()
|
|
|
|
->getFunction()
|
|
|
|
.getParent();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChangedIRComparer::generateFunctionData(ChangedIRData &Data,
|
|
|
|
const Function &F) {
|
|
|
|
if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) {
|
|
|
|
ChangedFuncData CFD;
|
|
|
|
for (const auto &B : F) {
|
|
|
|
CFD.getOrder().emplace_back(B.getName());
|
|
|
|
CFD.getData().insert({B.getName(), B});
|
|
|
|
}
|
|
|
|
Data.getOrder().emplace_back(F.getName());
|
|
|
|
Data.getData().insert({F.getName(), CFD});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-04-14 23:52:50 +02:00
|
|
|
const Module *M = unwrapModule(IR);
|
|
|
|
ModuleDescStack.emplace_back(M, getIRName(IR), PassID);
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-12-04 02:38:00 +01:00
|
|
|
if (isIgnored(PassID))
|
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.
|
2021-07-19 19:32:12 +02:00
|
|
|
if (shouldPrintAfterPass(PassID))
|
2018-12-21 12:49:05 +01:00
|
|
|
pushModuleDesc(PassID, IR);
|
|
|
|
|
2020-12-03 19:59:10 +01:00
|
|
|
if (!shouldPrintBeforePass(PassID))
|
2020-07-29 01:31:46 +02:00
|
|
|
return;
|
2018-12-21 12:49:05 +01:00
|
|
|
|
2021-04-14 23:52:50 +02:00
|
|
|
if (!shouldPrintIR(IR))
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbgs() << "*** IR Dump Before " << PassID << " on " << getIRName(IR)
|
|
|
|
<< " ***\n";
|
|
|
|
unwrapAndPrint(dbgs(), IR);
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) {
|
2020-12-04 02:38:00 +01:00
|
|
|
if (isIgnored(PassID))
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
|
2020-12-03 19:59:10 +01:00
|
|
|
if (!shouldPrintAfterPass(PassID))
|
2018-09-24 18:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2021-07-19 19:32:12 +02:00
|
|
|
const Module *M;
|
|
|
|
std::string IRName;
|
|
|
|
StringRef StoredPassID;
|
|
|
|
std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
|
|
|
|
assert(StoredPassID == PassID && "mismatched PassID");
|
2021-04-14 23:52:50 +02:00
|
|
|
|
|
|
|
if (!shouldPrintIR(IR))
|
|
|
|
return;
|
2018-12-21 12:49:05 +01:00
|
|
|
|
2021-07-19 19:32:12 +02:00
|
|
|
dbgs() << "*** IR Dump After " << PassID << " on " << IRName << " ***\n";
|
2021-04-14 23:52:50 +02:00
|
|
|
unwrapAndPrint(dbgs(), IR);
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) {
|
2020-12-03 19:59:10 +01:00
|
|
|
StringRef PassName = PIC->getPassNameForClassName(PassID);
|
2021-07-19 19:32:12 +02:00
|
|
|
if (!shouldPrintAfterPass(PassName))
|
2018-12-21 12:49:05 +01:00
|
|
|
return;
|
|
|
|
|
2020-12-04 02:38:00 +01:00
|
|
|
if (isIgnored(PassID))
|
2018-09-24 18:08:15 +02:00
|
|
|
return;
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
const Module *M;
|
2021-04-14 23:52:50 +02:00
|
|
|
std::string IRName;
|
2018-12-21 12:49:05 +01:00
|
|
|
StringRef StoredPassID;
|
2021-04-14 23:52:50 +02:00
|
|
|
std::tie(M, IRName, StoredPassID) = popModuleDesc(PassID);
|
|
|
|
assert(StoredPassID == PassID && "mismatched PassID");
|
2018-12-21 12:49:05 +01:00
|
|
|
// Additional filtering (e.g. -filter-print-func) can lead to module
|
|
|
|
// printing being skipped.
|
|
|
|
if (!M)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallString<20> Banner =
|
2021-04-14 23:52:50 +02:00
|
|
|
formatv("*** IR Dump After {0} on {1} (invalidated) ***", PassID, IRName);
|
|
|
|
dbgs() << Banner << "\n";
|
|
|
|
printIR(dbgs(), M);
|
2018-12-21 12:49:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:59:10 +01:00
|
|
|
bool PrintIRInstrumentation::shouldPrintBeforePass(StringRef PassID) {
|
|
|
|
if (shouldPrintBeforeAll())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
StringRef PassName = PIC->getPassNameForClassName(PassID);
|
2021-02-14 17:36:20 +01:00
|
|
|
return llvm::is_contained(printBeforePasses(), PassName);
|
2020-12-03 19:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PrintIRInstrumentation::shouldPrintAfterPass(StringRef PassID) {
|
|
|
|
if (shouldPrintAfterAll())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
StringRef PassName = PIC->getPassNameForClassName(PassID);
|
2021-02-14 17:36:20 +01:00
|
|
|
return llvm::is_contained(printAfterPasses(), PassName);
|
2020-12-03 19:59:10 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
void PrintIRInstrumentation::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
2020-12-03 19:59:10 +01:00
|
|
|
this->PIC = &PIC;
|
|
|
|
|
2018-12-21 12:49:05 +01:00
|
|
|
// BeforePass callback is not just for printing, it also saves a Module
|
|
|
|
// for later use in AfterPassInvalidated.
|
2021-07-19 19:32:12 +02:00
|
|
|
if (shouldPrintBeforeSomePass() || shouldPrintAfterSomePass())
|
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
|
|
|
|
2020-12-03 19:59:10 +01:00
|
|
|
if (shouldPrintAfterSomePass()) {
|
2018-12-21 12:49:05 +01:00
|
|
|
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) {
|
2020-11-01 07:08:15 +01:00
|
|
|
PIC.registerShouldRunOptionalPassCallback(
|
|
|
|
[this](StringRef P, Any IR) { return this->shouldRun(P, IR); });
|
2020-07-21 18:48:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-01 07:08:15 +01:00
|
|
|
bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
|
2020-07-21 18:48:22 +02:00
|
|
|
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-11-01 07:08:15 +01:00
|
|
|
bool ShouldRun = !(F && F->hasOptNone());
|
|
|
|
if (!ShouldRun && DebugLogging) {
|
|
|
|
errs() << "Skipping pass " << PassID << " on " << F->getName()
|
|
|
|
<< " due to optnone attribute\n";
|
|
|
|
}
|
|
|
|
return ShouldRun;
|
2020-07-21 18:48:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:36:14 +01:00
|
|
|
void OptBisectInstrumentation::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
2020-12-20 22:47:46 +01:00
|
|
|
if (!OptBisector->isEnabled())
|
2020-11-10 00:36:14 +01:00
|
|
|
return;
|
2020-12-20 22:47:46 +01:00
|
|
|
PIC.registerShouldRunOptionalPassCallback([](StringRef PassID, Any IR) {
|
2021-04-14 23:52:50 +02:00
|
|
|
return isIgnored(PassID) || OptBisector->checkPass(PassID, getIRName(IR));
|
2020-12-04 02:38:00 +01:00
|
|
|
});
|
2020-11-10 00:36:14 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 23:32:20 +02:00
|
|
|
raw_ostream &PrintPassInstrumentation::print() {
|
|
|
|
if (Opts.Indent) {
|
|
|
|
assert(Indent >= 0);
|
|
|
|
dbgs().indent(Indent);
|
|
|
|
}
|
|
|
|
return dbgs();
|
|
|
|
}
|
|
|
|
|
2020-07-29 02:08:24 +02:00
|
|
|
void PrintPassInstrumentation::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
2021-05-07 23:32:20 +02:00
|
|
|
if (!Enabled)
|
2020-07-29 02:08:24 +02:00
|
|
|
return;
|
|
|
|
|
2021-05-04 01:09:56 +02:00
|
|
|
std::vector<StringRef> SpecialPasses;
|
2021-05-07 23:32:20 +02:00
|
|
|
if (!Opts.Verbose) {
|
2021-05-04 01:09:56 +02:00
|
|
|
SpecialPasses.emplace_back("PassManager");
|
2020-07-29 02:08:24 +02:00
|
|
|
SpecialPasses.emplace_back("PassAdaptor");
|
2021-05-04 01:09:56 +02:00
|
|
|
}
|
2020-07-29 02:08:24 +02:00
|
|
|
|
2020-08-07 04:03:09 +02:00
|
|
|
PIC.registerBeforeSkippedPassCallback(
|
2021-05-07 23:32:20 +02:00
|
|
|
[this, SpecialPasses](StringRef PassID, Any IR) {
|
2020-08-07 04:03:09 +02:00
|
|
|
assert(!isSpecialPass(PassID, SpecialPasses) &&
|
|
|
|
"Unexpectedly skipping special pass");
|
|
|
|
|
2021-05-07 23:32:20 +02:00
|
|
|
print() << "Skipping pass: " << PassID << " on " << getIRName(IR)
|
|
|
|
<< "\n";
|
2020-08-07 04:03:09 +02:00
|
|
|
});
|
2021-05-07 23:32:20 +02:00
|
|
|
PIC.registerBeforeNonSkippedPassCallback([this, SpecialPasses](
|
|
|
|
StringRef PassID, Any IR) {
|
|
|
|
if (isSpecialPass(PassID, SpecialPasses))
|
|
|
|
return;
|
2020-08-07 04:03:09 +02:00
|
|
|
|
2021-05-07 23:32:20 +02:00
|
|
|
print() << "Running pass: " << PassID << " on " << getIRName(IR) << "\n";
|
|
|
|
Indent += 2;
|
|
|
|
});
|
|
|
|
PIC.registerAfterPassCallback(
|
|
|
|
[this, SpecialPasses](StringRef PassID, Any IR,
|
|
|
|
const PreservedAnalyses &) {
|
2020-07-29 02:08:24 +02:00
|
|
|
if (isSpecialPass(PassID, SpecialPasses))
|
|
|
|
return;
|
|
|
|
|
2021-05-07 23:32:20 +02:00
|
|
|
Indent -= 2;
|
2021-04-29 09:29:42 +02:00
|
|
|
});
|
|
|
|
PIC.registerAfterPassInvalidatedCallback(
|
2021-05-07 23:32:20 +02:00
|
|
|
[this, SpecialPasses](StringRef PassID, Any IR) {
|
|
|
|
if (isSpecialPass(PassID, SpecialPasses))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Indent -= 2;
|
2021-04-29 09:29:42 +02:00
|
|
|
});
|
|
|
|
|
2021-05-07 23:32:20 +02:00
|
|
|
if (!Opts.SkipAnalyses) {
|
|
|
|
PIC.registerBeforeAnalysisCallback([this](StringRef PassID, Any IR) {
|
|
|
|
print() << "Running analysis: " << PassID << " on " << getIRName(IR)
|
|
|
|
<< "\n";
|
|
|
|
Indent += 2;
|
|
|
|
});
|
|
|
|
PIC.registerAfterAnalysisCallback(
|
|
|
|
[this](StringRef PassID, Any IR) { Indent -= 2; });
|
|
|
|
PIC.registerAnalysisInvalidatedCallback([this](StringRef PassID, Any IR) {
|
|
|
|
print() << "Invalidating analysis: " << PassID << " on " << getIRName(IR)
|
|
|
|
<< "\n";
|
|
|
|
});
|
|
|
|
PIC.registerAnalysesClearedCallback([this](StringRef IRName) {
|
|
|
|
print() << "Clearing all analysis results for: " << IRName << "\n";
|
|
|
|
});
|
|
|
|
}
|
2021-04-29 09:29:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 07:55:24 +02:00
|
|
|
PreservedCFGCheckerInstrumentation::CFG::CFG(const Function *F,
|
|
|
|
bool TrackBBLifetime) {
|
|
|
|
if (TrackBBLifetime)
|
|
|
|
BBGuards = DenseMap<intptr_t, BBGuard>(F->size());
|
|
|
|
for (const auto &BB : *F) {
|
|
|
|
if (BBGuards)
|
|
|
|
BBGuards->try_emplace(intptr_t(&BB), &BB);
|
|
|
|
for (auto *Succ : successors(&BB)) {
|
|
|
|
Graph[&BB][Succ]++;
|
|
|
|
if (BBGuards)
|
|
|
|
BBGuards->try_emplace(intptr_t(Succ), Succ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printBBName(raw_ostream &out, const BasicBlock *BB) {
|
|
|
|
if (BB->hasName()) {
|
|
|
|
out << BB->getName() << "<" << BB << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BB->getParent()) {
|
|
|
|
out << "unnamed_removed<" << BB << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-15 12:38:27 +02:00
|
|
|
if (BB->isEntryBlock()) {
|
2020-09-11 07:55:24 +02:00
|
|
|
out << "entry"
|
|
|
|
<< "<" << BB << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FuncOrderBlockNum = 0;
|
|
|
|
for (auto &FuncBB : *BB->getParent()) {
|
|
|
|
if (&FuncBB == BB)
|
|
|
|
break;
|
|
|
|
FuncOrderBlockNum++;
|
|
|
|
}
|
|
|
|
out << "unnamed_" << FuncOrderBlockNum << "<" << BB << ">";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreservedCFGCheckerInstrumentation::CFG::printDiff(raw_ostream &out,
|
|
|
|
const CFG &Before,
|
|
|
|
const CFG &After) {
|
|
|
|
assert(!After.isPoisoned());
|
|
|
|
if (Before.isPoisoned()) {
|
|
|
|
out << "Some blocks were deleted\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find and print graph differences.
|
|
|
|
if (Before.Graph.size() != After.Graph.size())
|
|
|
|
out << "Different number of non-leaf basic blocks: before="
|
|
|
|
<< Before.Graph.size() << ", after=" << After.Graph.size() << "\n";
|
|
|
|
|
|
|
|
for (auto &BB : Before.Graph) {
|
|
|
|
auto BA = After.Graph.find(BB.first);
|
|
|
|
if (BA == After.Graph.end()) {
|
|
|
|
out << "Non-leaf block ";
|
|
|
|
printBBName(out, BB.first);
|
|
|
|
out << " is removed (" << BB.second.size() << " successors)\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &BA : After.Graph) {
|
|
|
|
auto BB = Before.Graph.find(BA.first);
|
|
|
|
if (BB == Before.Graph.end()) {
|
|
|
|
out << "Non-leaf block ";
|
|
|
|
printBBName(out, BA.first);
|
|
|
|
out << " is added (" << BA.second.size() << " successors)\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BB->second == BA.second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
out << "Different successors of block ";
|
|
|
|
printBBName(out, BA.first);
|
|
|
|
out << " (unordered):\n";
|
|
|
|
out << "- before (" << BB->second.size() << "): ";
|
|
|
|
for (auto &SuccB : BB->second) {
|
|
|
|
printBBName(out, SuccB.first);
|
|
|
|
if (SuccB.second != 1)
|
|
|
|
out << "(" << SuccB.second << "), ";
|
|
|
|
else
|
|
|
|
out << ", ";
|
|
|
|
}
|
|
|
|
out << "\n";
|
|
|
|
out << "- after (" << BA.second.size() << "): ";
|
|
|
|
for (auto &SuccA : BA.second) {
|
|
|
|
printBBName(out, SuccA.first);
|
|
|
|
if (SuccA.second != 1)
|
|
|
|
out << "(" << SuccA.second << "), ";
|
|
|
|
else
|
|
|
|
out << ", ";
|
|
|
|
}
|
|
|
|
out << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:55:18 +02:00
|
|
|
// PreservedCFGCheckerInstrumentation uses PreservedCFGCheckerAnalysis to check
|
|
|
|
// passes, that reported they kept CFG analyses up-to-date, did not actually
|
|
|
|
// change CFG. This check is done as follows. Before every functional pass in
|
|
|
|
// BeforeNonSkippedPassCallback a CFG snapshot (an instance of
|
|
|
|
// PreservedCFGCheckerInstrumentation::CFG) is requested from
|
|
|
|
// FunctionAnalysisManager as a result of PreservedCFGCheckerAnalysis. When the
|
|
|
|
// functional pass finishes and reports that CFGAnalyses or AllAnalyses are
|
|
|
|
// up-to-date then the cached result of PreservedCFGCheckerAnalysis (if
|
|
|
|
// available) is checked to be equal to a freshly created CFG snapshot.
|
|
|
|
struct PreservedCFGCheckerAnalysis
|
|
|
|
: public AnalysisInfoMixin<PreservedCFGCheckerAnalysis> {
|
|
|
|
friend AnalysisInfoMixin<PreservedCFGCheckerAnalysis>;
|
|
|
|
|
|
|
|
static AnalysisKey Key;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Provide the result type for this analysis pass.
|
|
|
|
using Result = PreservedCFGCheckerInstrumentation::CFG;
|
|
|
|
|
|
|
|
/// Run the analysis pass over a function and produce CFG.
|
|
|
|
Result run(Function &F, FunctionAnalysisManager &FAM) {
|
|
|
|
return Result(&F, /* TrackBBLifetime */ true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
AnalysisKey PreservedCFGCheckerAnalysis::Key;
|
|
|
|
|
|
|
|
bool PreservedCFGCheckerInstrumentation::CFG::invalidate(
|
|
|
|
Function &F, const PreservedAnalyses &PA,
|
|
|
|
FunctionAnalysisManager::Invalidator &) {
|
|
|
|
auto PAC = PA.getChecker<PreservedCFGCheckerAnalysis>();
|
|
|
|
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
|
|
|
|
PAC.preservedSet<CFGAnalyses>());
|
|
|
|
}
|
|
|
|
|
2020-09-11 07:55:24 +02:00
|
|
|
void PreservedCFGCheckerInstrumentation::registerCallbacks(
|
2021-04-06 06:55:18 +02:00
|
|
|
PassInstrumentationCallbacks &PIC, FunctionAnalysisManager &FAM) {
|
2020-09-11 07:55:24 +02:00
|
|
|
if (!VerifyPreservedCFG)
|
|
|
|
return;
|
|
|
|
|
2021-04-06 06:55:18 +02:00
|
|
|
FAM.registerPass([&] { return PreservedCFGCheckerAnalysis(); });
|
|
|
|
|
|
|
|
auto checkCFG = [](StringRef Pass, StringRef FuncName, const CFG &GraphBefore,
|
|
|
|
const CFG &GraphAfter) {
|
|
|
|
if (GraphAfter == GraphBefore)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbgs() << "Error: " << Pass
|
|
|
|
<< " does not invalidate CFG analyses but CFG changes detected in "
|
|
|
|
"function @"
|
|
|
|
<< FuncName << ":\n";
|
|
|
|
CFG::printDiff(dbgs(), GraphBefore, GraphAfter);
|
|
|
|
report_fatal_error(Twine("CFG unexpectedly changed by ", Pass));
|
|
|
|
};
|
|
|
|
|
|
|
|
PIC.registerBeforeNonSkippedPassCallback(
|
2021-04-06 08:14:16 +02:00
|
|
|
[this, &FAM](StringRef P, Any IR) {
|
2021-06-14 13:13:18 +02:00
|
|
|
#ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
|
2021-04-06 06:55:18 +02:00
|
|
|
assert(&PassStack.emplace_back(P));
|
2021-06-14 13:13:18 +02:00
|
|
|
#endif
|
2021-04-06 20:30:19 +02:00
|
|
|
(void)this;
|
2021-04-06 06:55:18 +02:00
|
|
|
if (!any_isa<const Function *>(IR))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto *F = any_cast<const Function *>(IR);
|
|
|
|
// Make sure a fresh CFG snapshot is available before the pass.
|
|
|
|
FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(F));
|
|
|
|
});
|
2020-09-11 07:55:24 +02:00
|
|
|
|
|
|
|
PIC.registerAfterPassInvalidatedCallback(
|
|
|
|
[this](StringRef P, const PreservedAnalyses &PassPA) {
|
2021-06-14 13:13:18 +02:00
|
|
|
#ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
|
2021-04-06 06:55:18 +02:00
|
|
|
assert(PassStack.pop_back_val() == P &&
|
2020-09-11 07:55:24 +02:00
|
|
|
"Before and After callbacks must correspond");
|
2021-06-14 13:13:18 +02:00
|
|
|
#endif
|
2021-04-06 20:30:19 +02:00
|
|
|
(void)this;
|
2020-09-11 07:55:24 +02:00
|
|
|
});
|
|
|
|
|
2021-04-06 06:55:18 +02:00
|
|
|
PIC.registerAfterPassCallback([this, &FAM,
|
|
|
|
checkCFG](StringRef P, Any IR,
|
|
|
|
const PreservedAnalyses &PassPA) {
|
2021-06-14 13:13:18 +02:00
|
|
|
#ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
|
2021-04-06 06:55:18 +02:00
|
|
|
assert(PassStack.pop_back_val() == P &&
|
|
|
|
"Before and After callbacks must correspond");
|
2021-06-14 13:13:18 +02:00
|
|
|
#endif
|
2021-04-06 20:30:19 +02:00
|
|
|
(void)this;
|
2020-09-11 07:55:24 +02:00
|
|
|
|
2021-04-06 06:55:18 +02:00
|
|
|
if (!any_isa<const Function *>(IR))
|
2020-09-11 07:55:24 +02:00
|
|
|
return;
|
|
|
|
|
2021-04-06 06:55:18 +02:00
|
|
|
if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() &&
|
|
|
|
!PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>())
|
|
|
|
return;
|
2020-09-11 07:55:24 +02:00
|
|
|
|
2021-04-06 06:55:18 +02:00
|
|
|
const auto *F = any_cast<const Function *>(IR);
|
|
|
|
if (auto *GraphBefore = FAM.getCachedResult<PreservedCFGCheckerAnalysis>(
|
|
|
|
*const_cast<Function *>(F)))
|
|
|
|
checkCFG(P, F->getName(), *GraphBefore,
|
|
|
|
CFG(F, /* TrackBBLifetime */ false));
|
2020-09-11 07:55:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-22 18:34:46 +02:00
|
|
|
void VerifyInstrumentation::registerCallbacks(
|
|
|
|
PassInstrumentationCallbacks &PIC) {
|
|
|
|
PIC.registerAfterPassCallback(
|
|
|
|
[this](StringRef P, Any IR, const PreservedAnalyses &PassPA) {
|
|
|
|
if (isIgnored(P) || P == "VerifierPass")
|
|
|
|
return;
|
|
|
|
if (any_isa<const Function *>(IR) || any_isa<const Loop *>(IR)) {
|
|
|
|
const Function *F;
|
|
|
|
if (any_isa<const Loop *>(IR))
|
|
|
|
F = any_cast<const Loop *>(IR)->getHeader()->getParent();
|
|
|
|
else
|
|
|
|
F = any_cast<const Function *>(IR);
|
|
|
|
if (DebugLogging)
|
|
|
|
dbgs() << "Verifying function " << F->getName() << "\n";
|
|
|
|
|
|
|
|
if (verifyFunction(*F))
|
|
|
|
report_fatal_error("Broken function found, compilation aborted!");
|
|
|
|
} else if (any_isa<const Module *>(IR) ||
|
|
|
|
any_isa<const LazyCallGraph::SCC *>(IR)) {
|
|
|
|
const Module *M;
|
|
|
|
if (any_isa<const LazyCallGraph::SCC *>(IR))
|
|
|
|
M = any_cast<const LazyCallGraph::SCC *>(IR)
|
|
|
|
->begin()
|
|
|
|
->getFunction()
|
|
|
|
.getParent();
|
|
|
|
else
|
|
|
|
M = any_cast<const Module *>(IR);
|
|
|
|
if (DebugLogging)
|
|
|
|
dbgs() << "Verifying module " << M->getName() << "\n";
|
|
|
|
|
|
|
|
if (verifyModule(*M))
|
|
|
|
report_fatal_error("Broken module found, compilation aborted!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:09:34 +01:00
|
|
|
InLineChangePrinter::~InLineChangePrinter() {}
|
|
|
|
|
|
|
|
void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID,
|
|
|
|
ChangedIRData &D) {
|
|
|
|
ChangedIRComparer::analyzeIR(IR, D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name,
|
|
|
|
const ChangedIRData &Before,
|
|
|
|
const ChangedIRData &After, Any IR) {
|
|
|
|
SmallString<20> Banner =
|
2021-04-14 23:52:50 +02:00
|
|
|
formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name);
|
2021-02-08 16:09:34 +01:00
|
|
|
Out << Banner;
|
2021-03-25 15:32:13 +01:00
|
|
|
ChangedIRComparer(Out, Before, After, UseColour)
|
|
|
|
.compare(IR, "", PassID, Name);
|
2021-02-08 16:09:34 +01:00
|
|
|
Out << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InLineChangePrinter::same(const ChangedIRData &D1,
|
|
|
|
const ChangedIRData &D2) {
|
|
|
|
return D1 == D2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChangedIRComparer::handleFunctionCompare(StringRef Name, StringRef Prefix,
|
|
|
|
StringRef PassID, bool InModule,
|
|
|
|
const ChangedFuncData &Before,
|
|
|
|
const ChangedFuncData &After) {
|
|
|
|
// Print a banner when this is being shown in the context of a module
|
|
|
|
if (InModule)
|
|
|
|
Out << "\n*** IR for function " << Name << " ***\n";
|
|
|
|
|
|
|
|
ChangedFuncData::report(
|
|
|
|
Before, After, [&](const ChangedBlockData *B, const ChangedBlockData *A) {
|
|
|
|
StringRef BStr = B ? B->getBody() : "\n";
|
|
|
|
StringRef AStr = A ? A->getBody() : "\n";
|
2021-03-25 15:32:13 +01:00
|
|
|
const std::string Removed =
|
|
|
|
UseColour ? "\033[31m-%l\033[0m\n" : "-%l\n";
|
|
|
|
const std::string Added = UseColour ? "\033[32m+%l\033[0m\n" : "+%l\n";
|
2021-02-08 16:09:34 +01:00
|
|
|
const std::string NoChange = " %l\n";
|
|
|
|
Out << doSystemDiff(BStr, AStr, Removed, Added, NoChange);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InLineChangePrinter::registerCallbacks(PassInstrumentationCallbacks &PIC) {
|
|
|
|
if (PrintChanged == ChangePrinter::PrintChangedDiffVerbose ||
|
2021-03-25 15:32:13 +01:00
|
|
|
PrintChanged == ChangePrinter::PrintChangedDiffQuiet ||
|
|
|
|
PrintChanged == ChangePrinter::PrintChangedColourDiffVerbose ||
|
|
|
|
PrintChanged == ChangePrinter::PrintChangedColourDiffQuiet)
|
2021-02-08 16:09:34 +01:00
|
|
|
TextChangeReporter<ChangedIRData>::registerRequiredCallbacks(PIC);
|
|
|
|
}
|
|
|
|
|
2021-05-07 23:32:20 +02:00
|
|
|
StandardInstrumentations::StandardInstrumentations(
|
|
|
|
bool DebugLogging, bool VerifyEach, PrintPassOptions PrintPassOpts)
|
|
|
|
: PrintPass(DebugLogging, PrintPassOpts), OptNone(DebugLogging),
|
2021-02-08 16:09:34 +01:00
|
|
|
PrintChangedIR(PrintChanged == ChangePrinter::PrintChangedVerbose),
|
2021-03-25 15:32:13 +01:00
|
|
|
PrintChangedDiff(
|
|
|
|
PrintChanged == ChangePrinter::PrintChangedDiffVerbose ||
|
|
|
|
PrintChanged == ChangePrinter::PrintChangedColourDiffVerbose,
|
|
|
|
PrintChanged == ChangePrinter::PrintChangedColourDiffVerbose ||
|
|
|
|
PrintChanged == ChangePrinter::PrintChangedColourDiffQuiet),
|
2021-02-08 16:09:34 +01:00
|
|
|
Verify(DebugLogging), VerifyEach(VerifyEach) {}
|
2021-01-11 20:11:00 +01:00
|
|
|
|
2018-09-24 18:08:15 +02:00
|
|
|
void StandardInstrumentations::registerCallbacks(
|
2021-04-06 06:55:18 +02:00
|
|
|
PassInstrumentationCallbacks &PIC, FunctionAnalysisManager *FAM) {
|
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);
|
2020-11-10 00:36:14 +01:00
|
|
|
OptBisect.registerCallbacks(PIC);
|
2021-04-06 06:55:18 +02:00
|
|
|
if (FAM)
|
|
|
|
PreservedCFGChecker.registerCallbacks(PIC, *FAM);
|
2020-10-01 19:39:02 +02:00
|
|
|
PrintChangedIR.registerCallbacks(PIC);
|
2020-12-11 21:18:31 +01:00
|
|
|
PseudoProbeVerification.registerCallbacks(PIC);
|
2020-09-22 18:34:46 +02:00
|
|
|
if (VerifyEach)
|
|
|
|
Verify.registerCallbacks(PIC);
|
2021-02-08 16:09:34 +01:00
|
|
|
PrintChangedDiff.registerCallbacks(PIC);
|
2018-09-24 18:08:15 +02:00
|
|
|
}
|
2020-11-20 15:43:06 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
template class ChangeReporter<std::string>;
|
|
|
|
template class TextChangeReporter<std::string>;
|
|
|
|
|
2021-02-08 16:09:34 +01:00
|
|
|
template class ChangeReporter<ChangedIRData>;
|
|
|
|
template class TextChangeReporter<ChangedIRData>;
|
|
|
|
|
2020-11-20 15:43:06 +01:00
|
|
|
} // namespace llvm
|