1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-22 12:33:33 +02:00
llvm-mirror/unittests/IR/PassManagerTest.cpp
Chandler Carruth 84780666b4 [PM] Extend the explicit 'invalidate' method API on analysis results to
accept an Invalidator that allows them to invalidate themselves if their
dependencies are in turn invalidated.

Rather than recording the dependency graph ahead of time when analysis
get results from other analyses, this simply lets each result trigger
the immediate invalidation of any analyses they actually depend on. They
do this in a way that has three nice properties:

1) They don't have to handle transitive dependencies because the
   infrastructure will recurse for them.
2) The invalidate methods are still called only once. We just
   dynamically discover the necessary topological ordering, everything
   is memoized nicely.
3) The infrastructure still provides a default implementation and can
   access it so that only analyses which have dependencies need to do
   anything custom.

To make this work at all, the invalidation logic also has to defer the
deletion of the result objects themselves so that they can remain alive
until we have collected the complete set of results to invalidate.

A unittest is added here that has exactly the dependency pattern we are
concerned with. It hit the use-after-free described by Sean in much
detail in the long thread about analysis invalidation before this
change, and even in an intermediate form of this change where we failed
to defer the deletion of the result objects.

There is an important problem with doing dependency invalidation that
*isn't* solved here: we don't *enforce* that results correctly
invalidate all the analyses whose results they depend on.

I actually looked at what it would take to do that, and it isn't as hard
as I had thought but the complexity it introduces seems very likely to
outweigh the benefit. The technique would be to provide a base class for
an analysis result that would be populated with other results, and
automatically provide the invalidate method which immediately does the
correct thing. This approach has some nice pros IMO:
- Handles the case we care about and nothing else: only *results*
  that depend on other analyses trigger extra invalidation.
- Localized to the result rather than centralized in the analysis
  manager.
- Ties the storage of the reference to another result to the triggering
  of the invalidation of that analysis.
- Still supports extending invalidation in customized ways.

But the down sides here are:
- Very heavy-weight meta-programming is needed to provide this base
  class.
- Requires a pretty awful API for accessing the dependencies.

Ultimately, I fear it will not pull its weight. But we can re-evaluate
this at any point if we start discovering consistent problems where the
invalidation and dependencies get out of sync. It will fit as a clean
layer on top of the facilities in this patch that we can add if and when
we need it.

Note that I'm not really thrilled with the names for these APIs... The
name "Invalidator" seems ok but not great. The method name "invalidate"
also. In review some improvements were suggested, but they really need
*other* uses of these terms to be updated as well so I'm going to do
that in a follow-up commit.

I'm working on the actual fixes to various analyses that need to use
these, but I want to try to get tests for each of them so we don't
regress. And those changes are seperable and obvious so once this goes
in I should be able to roll them out throughout LLVM.

Many thanks to Sean, Justin, and others for help reviewing here.

Differential Revision: https://reviews.llvm.org/D23738

llvm-svn: 288077
2016-11-28 22:04:31 +00:00

495 lines
16 KiB
C++

//===- llvm/unittest/IR/PassManager.cpp - PassManager tests ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
class TestFunctionAnalysis : public AnalysisInfoMixin<TestFunctionAnalysis> {
public:
struct Result {
Result(int Count) : InstructionCount(Count) {}
int InstructionCount;
};
TestFunctionAnalysis(int &Runs) : Runs(Runs) {}
/// \brief Run the analysis pass over the function and return a result.
Result run(Function &F, FunctionAnalysisManager &AM) {
++Runs;
int Count = 0;
for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
++II)
++Count;
return Result(Count);
}
private:
friend AnalysisInfoMixin<TestFunctionAnalysis>;
static AnalysisKey Key;
int &Runs;
};
AnalysisKey TestFunctionAnalysis::Key;
class TestModuleAnalysis : public AnalysisInfoMixin<TestModuleAnalysis> {
public:
struct Result {
Result(int Count) : FunctionCount(Count) {}
int FunctionCount;
};
TestModuleAnalysis(int &Runs) : Runs(Runs) {}
Result run(Module &M, ModuleAnalysisManager &AM) {
++Runs;
int Count = 0;
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
++Count;
return Result(Count);
}
private:
friend AnalysisInfoMixin<TestModuleAnalysis>;
static AnalysisKey Key;
int &Runs;
};
AnalysisKey TestModuleAnalysis::Key;
struct TestModulePass : PassInfoMixin<TestModulePass> {
TestModulePass(int &RunCount) : RunCount(RunCount) {}
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
++RunCount;
return PreservedAnalyses::none();
}
int &RunCount;
};
struct TestPreservingModulePass : PassInfoMixin<TestPreservingModulePass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
return PreservedAnalyses::all();
}
};
struct TestMinPreservingModulePass
: PassInfoMixin<TestMinPreservingModulePass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
PreservedAnalyses PA;
// Force running an analysis.
(void)AM.getResult<TestModuleAnalysis>(M);
PA.preserve<FunctionAnalysisManagerModuleProxy>();
return PA;
}
};
struct TestFunctionPass : PassInfoMixin<TestFunctionPass> {
TestFunctionPass(int &RunCount, int &AnalyzedInstrCount,
int &AnalyzedFunctionCount,
bool OnlyUseCachedResults = false)
: RunCount(RunCount), AnalyzedInstrCount(AnalyzedInstrCount),
AnalyzedFunctionCount(AnalyzedFunctionCount),
OnlyUseCachedResults(OnlyUseCachedResults) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
++RunCount;
const ModuleAnalysisManager &MAM =
AM.getResult<ModuleAnalysisManagerFunctionProxy>(F).getManager();
if (TestModuleAnalysis::Result *TMA =
MAM.getCachedResult<TestModuleAnalysis>(*F.getParent()))
AnalyzedFunctionCount += TMA->FunctionCount;
if (OnlyUseCachedResults) {
// Hack to force the use of the cached interface.
if (TestFunctionAnalysis::Result *AR =
AM.getCachedResult<TestFunctionAnalysis>(F))
AnalyzedInstrCount += AR->InstructionCount;
} else {
// Typical path just runs the analysis as needed.
TestFunctionAnalysis::Result &AR = AM.getResult<TestFunctionAnalysis>(F);
AnalyzedInstrCount += AR.InstructionCount;
}
return PreservedAnalyses::all();
}
int &RunCount;
int &AnalyzedInstrCount;
int &AnalyzedFunctionCount;
bool OnlyUseCachedResults;
};
// A test function pass that invalidates all function analyses for a function
// with a specific name.
struct TestInvalidationFunctionPass
: PassInfoMixin<TestInvalidationFunctionPass> {
TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
return F.getName() == Name ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}
StringRef Name;
};
std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) {
SMDiagnostic Err;
return parseAssemblyString(IR, Err, Context);
}
class PassManagerTest : public ::testing::Test {
protected:
LLVMContext Context;
std::unique_ptr<Module> M;
public:
PassManagerTest()
: M(parseIR(Context, "define void @f() {\n"
"entry:\n"
" call void @g()\n"
" call void @h()\n"
" ret void\n"
"}\n"
"define void @g() {\n"
" ret void\n"
"}\n"
"define void @h() {\n"
" ret void\n"
"}\n")) {}
};
TEST_F(PassManagerTest, BasicPreservedAnalyses) {
PreservedAnalyses PA1 = PreservedAnalyses();
EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>());
EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>());
PreservedAnalyses PA2 = PreservedAnalyses::none();
EXPECT_FALSE(PA2.preserved<TestFunctionAnalysis>());
EXPECT_FALSE(PA2.preserved<TestModuleAnalysis>());
PreservedAnalyses PA3 = PreservedAnalyses::all();
EXPECT_TRUE(PA3.preserved<TestFunctionAnalysis>());
EXPECT_TRUE(PA3.preserved<TestModuleAnalysis>());
PreservedAnalyses PA4 = PA1;
EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>());
EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>());
PA4 = PA3;
EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>());
EXPECT_TRUE(PA4.preserved<TestModuleAnalysis>());
PA4 = std::move(PA2);
EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>());
EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>());
PA4.preserve<TestFunctionAnalysis>();
EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>());
EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>());
PA1.preserve<TestModuleAnalysis>();
EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>());
EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>());
PA1.preserve<TestFunctionAnalysis>();
EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>());
EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>());
PA1.intersect(PA4);
EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>());
EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>());
}
TEST_F(PassManagerTest, Basic) {
FunctionAnalysisManager FAM;
int FunctionAnalysisRuns = 0;
FAM.registerPass([&] { return TestFunctionAnalysis(FunctionAnalysisRuns); });
ModuleAnalysisManager MAM;
int ModuleAnalysisRuns = 0;
MAM.registerPass([&] { return TestModuleAnalysis(ModuleAnalysisRuns); });
MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
ModulePassManager MPM;
// Count the runs over a Function.
int FunctionPassRunCount1 = 0;
int AnalyzedInstrCount1 = 0;
int AnalyzedFunctionCount1 = 0;
{
// Pointless scoped copy to test move assignment.
ModulePassManager NestedMPM;
FunctionPassManager FPM;
{
// Pointless scope to test move assignment.
FunctionPassManager NestedFPM;
NestedFPM.addPass(TestFunctionPass(
FunctionPassRunCount1, AnalyzedInstrCount1, AnalyzedFunctionCount1));
FPM = std::move(NestedFPM);
}
NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
MPM = std::move(NestedMPM);
}
// Count the runs over a module.
int ModulePassRunCount = 0;
MPM.addPass(TestModulePass(ModulePassRunCount));
// Count the runs over a Function in a separate manager.
int FunctionPassRunCount2 = 0;
int AnalyzedInstrCount2 = 0;
int AnalyzedFunctionCount2 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestFunctionPass(FunctionPassRunCount2, AnalyzedInstrCount2,
AnalyzedFunctionCount2));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
// A third function pass manager but with only preserving intervening passes
// and with a function pass that invalidates exactly one analysis.
MPM.addPass(TestPreservingModulePass());
int FunctionPassRunCount3 = 0;
int AnalyzedInstrCount3 = 0;
int AnalyzedFunctionCount3 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestFunctionPass(FunctionPassRunCount3, AnalyzedInstrCount3,
AnalyzedFunctionCount3));
FPM.addPass(TestInvalidationFunctionPass("f"));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
// A fourth function pass manager but with a minimal intervening passes.
MPM.addPass(TestMinPreservingModulePass());
int FunctionPassRunCount4 = 0;
int AnalyzedInstrCount4 = 0;
int AnalyzedFunctionCount4 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestFunctionPass(FunctionPassRunCount4, AnalyzedInstrCount4,
AnalyzedFunctionCount4));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
// A fifth function pass manager but which uses only cached results.
int FunctionPassRunCount5 = 0;
int AnalyzedInstrCount5 = 0;
int AnalyzedFunctionCount5 = 0;
{
FunctionPassManager FPM;
FPM.addPass(TestInvalidationFunctionPass("f"));
FPM.addPass(TestFunctionPass(FunctionPassRunCount5, AnalyzedInstrCount5,
AnalyzedFunctionCount5,
/*OnlyUseCachedResults=*/true));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
}
MPM.run(*M, MAM);
// Validate module pass counters.
EXPECT_EQ(1, ModulePassRunCount);
// Validate all function pass counter sets are the same.
EXPECT_EQ(3, FunctionPassRunCount1);
EXPECT_EQ(5, AnalyzedInstrCount1);
EXPECT_EQ(0, AnalyzedFunctionCount1);
EXPECT_EQ(3, FunctionPassRunCount2);
EXPECT_EQ(5, AnalyzedInstrCount2);
EXPECT_EQ(0, AnalyzedFunctionCount2);
EXPECT_EQ(3, FunctionPassRunCount3);
EXPECT_EQ(5, AnalyzedInstrCount3);
EXPECT_EQ(0, AnalyzedFunctionCount3);
EXPECT_EQ(3, FunctionPassRunCount4);
EXPECT_EQ(5, AnalyzedInstrCount4);
EXPECT_EQ(0, AnalyzedFunctionCount4);
EXPECT_EQ(3, FunctionPassRunCount5);
EXPECT_EQ(2, AnalyzedInstrCount5); // Only 'g' and 'h' were cached.
EXPECT_EQ(0, AnalyzedFunctionCount5);
// Validate the analysis counters:
// first run over 3 functions, then module pass invalidates
// second run over 3 functions, nothing invalidates
// third run over 0 functions, but 1 function invalidated
// fourth run over 1 function
EXPECT_EQ(7, FunctionAnalysisRuns);
EXPECT_EQ(1, ModuleAnalysisRuns);
}
// A customized pass manager that passes extra arguments through the
// infrastructure.
typedef AnalysisManager<Function, int> CustomizedAnalysisManager;
typedef PassManager<Function, CustomizedAnalysisManager, int, int &>
CustomizedPassManager;
class CustomizedAnalysis : public AnalysisInfoMixin<CustomizedAnalysis> {
public:
struct Result {
Result(int I) : I(I) {}
int I;
};
Result run(Function &F, CustomizedAnalysisManager &AM, int I) {
return Result(I);
}
private:
friend AnalysisInfoMixin<CustomizedAnalysis>;
static AnalysisKey Key;
};
AnalysisKey CustomizedAnalysis::Key;
struct CustomizedPass : PassInfoMixin<CustomizedPass> {
std::function<void(CustomizedAnalysis::Result &, int &)> Callback;
template <typename CallbackT>
CustomizedPass(CallbackT Callback) : Callback(Callback) {}
PreservedAnalyses run(Function &F, CustomizedAnalysisManager &AM, int I,
int &O) {
Callback(AM.getResult<CustomizedAnalysis>(F, I), O);
return PreservedAnalyses::none();
}
};
TEST_F(PassManagerTest, CustomizedPassManagerArgs) {
CustomizedAnalysisManager AM;
AM.registerPass([&] { return CustomizedAnalysis(); });
CustomizedPassManager PM;
// Add an instance of the customized pass that just accumulates the input
// after it is round-tripped through the analysis.
int Result = 0;
PM.addPass(
CustomizedPass([](CustomizedAnalysis::Result &R, int &O) { O += R.I; }));
// Run this over every function with the input of 42.
for (Function &F : *M)
PM.run(F, AM, 42, Result);
// And ensure that we accumulated the correct result.
EXPECT_EQ(42 * (int)M->size(), Result);
}
/// A test analysis pass which caches in its result another analysis pass and
/// uses it to serve queries. This requires the result to invalidate itself
/// when its dependency is invalidated.
struct TestIndirectFunctionAnalysis
: public AnalysisInfoMixin<TestIndirectFunctionAnalysis> {
struct Result {
Result(TestFunctionAnalysis::Result &Dep) : Dep(Dep) {}
TestFunctionAnalysis::Result &Dep;
bool invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &Inv) {
return !PA.preserved<TestIndirectFunctionAnalysis>() ||
Inv.invalidate<TestFunctionAnalysis>(F, PA);
}
};
TestIndirectFunctionAnalysis(int &Runs) : Runs(Runs) {}
/// Run the analysis pass over the function and return a result.
Result run(Function &F, FunctionAnalysisManager &AM) {
++Runs;
return Result(AM.getResult<TestFunctionAnalysis>(F));
}
private:
friend AnalysisInfoMixin<TestIndirectFunctionAnalysis>;
static AnalysisKey Key;
int &Runs;
};
AnalysisKey TestIndirectFunctionAnalysis::Key;
struct LambdaPass : public PassInfoMixin<LambdaPass> {
using FuncT = std::function<PreservedAnalyses(Function &, FunctionAnalysisManager &)>;
LambdaPass(FuncT Func) : Func(std::move(Func)) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) {
return Func(F, AM);
}
FuncT Func;
};
TEST_F(PassManagerTest, IndirectAnalysisInvalidation) {
FunctionAnalysisManager FAM(/*DebugLogging*/ true);
int AnalysisRuns = 0, IndirectAnalysisRuns = 0;
FAM.registerPass([&] { return TestFunctionAnalysis(AnalysisRuns); });
FAM.registerPass(
[&] { return TestIndirectFunctionAnalysis(IndirectAnalysisRuns); });
ModuleAnalysisManager MAM(/*DebugLogging*/ true);
MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
int InstrCount = 0;
ModulePassManager MPM(/*DebugLogging*/ true);
FunctionPassManager FPM(/*DebugLogging*/ true);
// First just use the analysis to get the instruction count, and preserve
// everything.
FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
InstrCount +=
AM.getResult<TestIndirectFunctionAnalysis>(F).Dep.InstructionCount;
return PreservedAnalyses::all();
}));
// Next, invalidate
// - both analyses for "f",
// - just the underlying (indirect) analysis for "g", and
// - just the direct analysis for "h".
FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
InstrCount +=
AM.getResult<TestIndirectFunctionAnalysis>(F).Dep.InstructionCount;
auto PA = PreservedAnalyses::none();
if (F.getName() == "g")
PA.preserve<TestFunctionAnalysis>();
else if (F.getName() == "h")
PA.preserve<TestIndirectFunctionAnalysis>();
return PA;
}));
// Finally, use the analysis again on each function, forcing re-computation
// for all of them.
FPM.addPass(LambdaPass([&](Function &F, FunctionAnalysisManager &AM) {
InstrCount +=
AM.getResult<TestIndirectFunctionAnalysis>(F).Dep.InstructionCount;
return PreservedAnalyses::all();
}));
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
MPM.run(*M, MAM);
// There are generally two possible runs for each of the three functions. But
// for one function, we only invalidate the indirect analysis so the base one
// only gets run five times.
EXPECT_EQ(5, AnalysisRuns);
// The indirect analysis is invalidated for each function (either directly or
// indirectly) and run twice for each.
EXPECT_EQ(6, IndirectAnalysisRuns);
// There are five instructions in the module and we add the count three
// times.
EXPECT_EQ(5 * 3, InstrCount);
}
}