Revert "[Kaleidoscope] Switch to the new PassManager. (#69032)"

This reverts commit 7b94744e77aaf752abc2c6ab38ee3fdfafbeff9d.

This breaks the expensive checks bot: https://green.lab.llvm.org/green/job/clang-stage1-cmake-RA-expensive/26026/

We didn't notice because it was broken for other reasons I think.
This commit is contained in:
Amara Emerson 2023-11-13 14:09:15 -08:00
parent 2db9b6a93f
commit 63d19cfd85
5 changed files with 91 additions and 282 deletions

View File

@ -94,6 +94,14 @@ use, in the form of "passes".
LLVM Optimization Passes
========================
.. warning::
Due to the transition to the new PassManager infrastructure this tutorial
is based on ``llvm::legacy::FunctionPassManager`` which can be found in
`LegacyPassManager.h <https://llvm.org/doxygen/classllvm_1_1legacy_1_1FunctionPassManager.html>`_.
For the purpose of the this tutorial the above should be used until
the pass manager transition is complete.
LLVM provides many optimization passes, which do many different sorts of
things and have different tradeoffs. Unlike other systems, LLVM doesn't
hold to the mistaken notion that one set of optimizations is right for
@ -119,93 +127,44 @@ in. If we wanted to make a "static Kaleidoscope compiler", we would use
exactly the code we have now, except that we would defer running the
optimizer until the entire file has been parsed.
In addition to the distinction between function and module passes, passes can be
divided into transform and analysis passes. Transform passes mutate the IR, and
analysis passes compute information that other passes can use. In order to add
a transform pass, all analysis passes it depends upon must be registered in
advance.
In order to get per-function optimizations going, we need to set up a
`FunctionPassManager <../../WritingAnLLVMPass.html#what-passmanager-doesr>`_ to hold
and organize the LLVM optimizations that we want to run. Once we have
that, we can add a set of optimizations to run. We'll need a new
FunctionPassManager for each module that we want to optimize, so we'll
add to a function created in the previous chapter (``InitializeModule()``):
write a function to create and initialize both the module and pass manager
for us:
.. code-block:: c++
void InitializeModuleAndManagers(void) {
void InitializeModuleAndPassManager(void) {
// Open a new context and module.
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
// Create a new builder for the module.
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Create a new pass manager attached to it.
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Create new pass and analysis managers.
TheFPM = std::make_unique<FunctionPassManager>();
TheFAM = std::make_unique<FunctionAnalysisManager>();
TheMAM = std::make_unique<ModuleAnalysisManager>();
ThePIC = std::make_unique<PassInstrumentationCallbacks>();
TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
/*DebugLogging*/ true);
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
...
After initializing the global module ``TheModule`` and the FunctionPassManager,
we need to initialize other parts of the framework. The FunctionAnalysisManager
and ModuleAnalysisManager allow us to add analysis passes that run across the
function and the whole module, respectively. PassInstrumentationCallbacks
and StandardInstrumentations are required for the pass instrumentation
framework, which allows developers to customize what
happens between passes.
Once these managers are set up, we use a series of "addPass" calls to add a
bunch of LLVM transform passes:
.. code-block:: c++
// Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
TheFPM->addPass(InstCombinePass());
TheFPM->add(createInstructionCombiningPass());
// Reassociate expressions.
TheFPM->addPass(ReassociatePass());
TheFPM->add(createReassociatePass());
// Eliminate Common SubExpressions.
TheFPM->addPass(GVNPass());
TheFPM->add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
TheFPM->addPass(SimplifyCFGPass());
TheFPM->add(createCFGSimplificationPass());
TheFPM->doInitialization();
}
This code initializes the global module ``TheModule``, and the function pass
manager ``TheFPM``, which is attached to ``TheModule``. Once the pass manager is
set up, we use a series of "add" calls to add a bunch of LLVM passes.
In this case, we choose to add four optimization passes.
The passes we choose here are a pretty standard set
of "cleanup" optimizations that are useful for a wide variety of code. I won't
delve into what they do but, believe me, they are a good starting place :).
Next, we register the analysis passes used by the transform passes. This is
generally done using ``PassBuilder::register...Analyses()``, but we'll do it
manually to make clearer what's under the hood.
.. code-block:: c++
// Register analysis passes used in these transform passes.
TheFAM->registerPass([&] { return AAManager(); });
TheFAM->registerPass([&] { return AssumptionAnalysis(); });
TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
TheFAM->registerPass([&] { return LoopAnalysis(); });
TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
TheFAM->registerPass([&] {
return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
});
TheFAM->registerPass(
[&] { return PassInstrumentationAnalysis(ThePIC.get()); });
TheFAM->registerPass([&] { return TargetIRAnalysis(); });
TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
}
Once the PassManager is set up, we need to make use of it. We do this by
running it after our newly created function is constructed (in
``FunctionAST::codegen()``), but before it is returned to the client:
@ -220,7 +179,7 @@ running it after our newly created function is constructed (in
verifyFunction(*TheFunction);
// Optimize the function.
TheFPM->run(*TheFunction, *TheFAM);
TheFPM->run(*TheFunction);
return TheFunction;
}

View File

@ -1,32 +1,21 @@
#include "../include/KaleidoscopeJIT.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/Reassociate.h"
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
#include <algorithm>
#include <cassert>
#include <cctype>
@ -424,12 +413,8 @@ static std::unique_ptr<LLVMContext> TheContext;
static std::unique_ptr<Module> TheModule;
static std::unique_ptr<IRBuilder<>> Builder;
static std::map<std::string, Value *> NamedValues;
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
static std::unique_ptr<FunctionPassManager> TheFPM;
static std::unique_ptr<FunctionAnalysisManager> TheFAM;
static std::unique_ptr<ModuleAnalysisManager> TheMAM;
static std::unique_ptr<PassInstrumentationCallbacks> ThePIC;
static std::unique_ptr<StandardInstrumentations> TheSI;
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
static ExitOnError ExitOnErr;
@ -550,7 +535,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
TheFPM->run(*TheFunction, *TheFAM);
TheFPM->run(*TheFunction);
return TheFunction;
}
@ -564,51 +549,28 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
static void InitializeModuleAndManagers() {
static void InitializeModuleAndPassManager() {
// Open a new context and module.
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// Create a new builder for the module.
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Create new pass and analysis managers.
TheFPM = std::make_unique<FunctionPassManager>();
TheFAM = std::make_unique<FunctionAnalysisManager>();
TheMAM = std::make_unique<ModuleAnalysisManager>();
ThePIC = std::make_unique<PassInstrumentationCallbacks>();
TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
/*DebugLogging*/ true);
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
// Create a new pass manager attached to it.
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
TheFPM->addPass(InstCombinePass());
TheFPM->add(createInstructionCombiningPass());
// Reassociate expressions.
TheFPM->addPass(ReassociatePass());
TheFPM->add(createReassociatePass());
// Eliminate Common SubExpressions.
TheFPM->addPass(GVNPass());
TheFPM->add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
TheFPM->addPass(SimplifyCFGPass());
TheFPM->add(createCFGSimplificationPass());
// Register analysis passes used in these transform passes.
TheFAM->registerPass([&] { return AAManager(); });
TheFAM->registerPass([&] { return AssumptionAnalysis(); });
TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
TheFAM->registerPass([&] { return LoopAnalysis(); });
TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
TheFAM->registerPass([&] {
return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
});
TheFAM->registerPass(
[&] { return PassInstrumentationAnalysis(ThePIC.get()); });
TheFAM->registerPass([&] { return TargetIRAnalysis(); });
TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
TheFPM->doInitialization();
}
static void HandleDefinition() {
@ -619,7 +581,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
}
} else {
// Skip token for error recovery.
@ -651,7 +613,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@ -737,7 +699,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Run the main "interpreter loop" now.
MainLoop();

View File

@ -1,13 +1,6 @@
#include "../include/KaleidoscopeJIT.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
@ -15,19 +8,15 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/Reassociate.h"
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
#include <algorithm>
#include <cassert>
#include <cctype>
@ -551,12 +540,8 @@ static std::unique_ptr<LLVMContext> TheContext;
static std::unique_ptr<Module> TheModule;
static std::unique_ptr<IRBuilder<>> Builder;
static std::map<std::string, Value *> NamedValues;
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
static std::unique_ptr<FunctionPassManager> TheFPM;
static std::unique_ptr<FunctionAnalysisManager> TheFAM;
static std::unique_ptr<ModuleAnalysisManager> TheMAM;
static std::unique_ptr<PassInstrumentationCallbacks> ThePIC;
static std::unique_ptr<StandardInstrumentations> TheSI;
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
static ExitOnError ExitOnErr;
@ -824,7 +809,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
TheFPM->run(*TheFunction, *TheFAM);
TheFPM->run(*TheFunction);
return TheFunction;
}
@ -838,51 +823,28 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
static void InitializeModuleAndManagers() {
// Open a new context and module.
static void InitializeModuleAndPassManager() {
// Open a new module.
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// Create a new builder for the module.
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Create new pass and analysis managers.
TheFPM = std::make_unique<FunctionPassManager>();
TheFAM = std::make_unique<FunctionAnalysisManager>();
TheMAM = std::make_unique<ModuleAnalysisManager>();
ThePIC = std::make_unique<PassInstrumentationCallbacks>();
TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
/*DebugLogging*/ true);
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
// Create a new pass manager attached to it.
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
TheFPM->addPass(InstCombinePass());
TheFPM->add(createInstructionCombiningPass());
// Reassociate expressions.
TheFPM->addPass(ReassociatePass());
TheFPM->add(createReassociatePass());
// Eliminate Common SubExpressions.
TheFPM->addPass(GVNPass());
TheFPM->add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
TheFPM->addPass(SimplifyCFGPass());
TheFPM->add(createCFGSimplificationPass());
// Register analysis passes used in these transform passes.
TheFAM->registerPass([&] { return AAManager(); });
TheFAM->registerPass([&] { return AssumptionAnalysis(); });
TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
TheFAM->registerPass([&] { return LoopAnalysis(); });
TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
TheFAM->registerPass([&] {
return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
});
TheFAM->registerPass(
[&] { return PassInstrumentationAnalysis(ThePIC.get()); });
TheFAM->registerPass([&] { return TargetIRAnalysis(); });
TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
TheFPM->doInitialization();
}
static void HandleDefinition() {
@ -893,7 +855,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
}
} else {
// Skip token for error recovery.
@ -925,7 +887,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@ -1011,7 +973,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Run the main "interpreter loop" now.
MainLoop();

View File

@ -1,13 +1,6 @@
#include "../include/KaleidoscopeJIT.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
@ -15,19 +8,15 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/Reassociate.h"
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
#include <algorithm>
#include <cassert>
#include <cctype>
@ -643,12 +632,8 @@ static std::unique_ptr<LLVMContext> TheContext;
static std::unique_ptr<Module> TheModule;
static std::unique_ptr<IRBuilder<>> Builder;
static std::map<std::string, Value *> NamedValues;
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
static std::unique_ptr<FunctionPassManager> TheFPM;
static std::unique_ptr<FunctionAnalysisManager> TheFAM;
static std::unique_ptr<ModuleAnalysisManager> TheMAM;
static std::unique_ptr<PassInstrumentationCallbacks> ThePIC;
static std::unique_ptr<StandardInstrumentations> TheSI;
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
static ExitOnError ExitOnErr;
@ -940,7 +925,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
TheFPM->run(*TheFunction, *TheFAM);
TheFPM->run(*TheFunction);
return TheFunction;
}
@ -957,51 +942,28 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
static void InitializeModuleAndManagers() {
// Open a new context and module.
static void InitializeModuleAndPassManager() {
// Open a new module.
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// Create a new builder for the module.
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Create new pass and analysis managers.
TheFPM = std::make_unique<FunctionPassManager>();
TheFAM = std::make_unique<FunctionAnalysisManager>();
TheMAM = std::make_unique<ModuleAnalysisManager>();
ThePIC = std::make_unique<PassInstrumentationCallbacks>();
TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
/*DebugLogging*/ true);
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
// Create a new pass manager attached to it.
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
TheFPM->addPass(InstCombinePass());
TheFPM->add(createInstructionCombiningPass());
// Reassociate expressions.
TheFPM->addPass(ReassociatePass());
TheFPM->add(createReassociatePass());
// Eliminate Common SubExpressions.
TheFPM->addPass(GVNPass());
TheFPM->add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
TheFPM->addPass(SimplifyCFGPass());
TheFPM->add(createCFGSimplificationPass());
// Register analysis passes used in these transform passes.
TheFAM->registerPass([&] { return AAManager(); });
TheFAM->registerPass([&] { return AssumptionAnalysis(); });
TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
TheFAM->registerPass([&] { return LoopAnalysis(); });
TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
TheFAM->registerPass([&] {
return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
});
TheFAM->registerPass(
[&] { return PassInstrumentationAnalysis(ThePIC.get()); });
TheFAM->registerPass([&] { return TargetIRAnalysis(); });
TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
TheFPM->doInitialization();
}
static void HandleDefinition() {
@ -1012,7 +974,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
}
} else {
// Skip token for error recovery.
@ -1044,7 +1006,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@ -1130,7 +1092,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Run the main "interpreter loop" now.
MainLoop();

View File

@ -1,13 +1,6 @@
#include "../include/KaleidoscopeJIT.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
@ -15,19 +8,15 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/Reassociate.h"
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
#include "llvm/Transforms/Utils.h"
#include <algorithm>
#include <cassert>
@ -716,12 +705,8 @@ static std::unique_ptr<LLVMContext> TheContext;
static std::unique_ptr<Module> TheModule;
static std::unique_ptr<IRBuilder<>> Builder;
static std::map<std::string, AllocaInst *> NamedValues;
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
static std::unique_ptr<FunctionPassManager> TheFPM;
static std::unique_ptr<FunctionAnalysisManager> TheFAM;
static std::unique_ptr<ModuleAnalysisManager> TheMAM;
static std::unique_ptr<PassInstrumentationCallbacks> ThePIC;
static std::unique_ptr<StandardInstrumentations> TheSI;
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
static ExitOnError ExitOnErr;
@ -1109,7 +1094,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
TheFPM->run(*TheFunction, *TheFAM);
TheFPM->run(*TheFunction);
return TheFunction;
}
@ -1126,51 +1111,30 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
static void InitializeModuleAndManagers() {
// Open a new context and module.
static void InitializeModuleAndPassManager() {
// Open a new module.
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// Create a new builder for the module.
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Create new pass and analysis managers.
TheFPM = std::make_unique<FunctionPassManager>();
TheFAM = std::make_unique<FunctionAnalysisManager>();
TheMAM = std::make_unique<ModuleAnalysisManager>();
ThePIC = std::make_unique<PassInstrumentationCallbacks>();
TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
/*DebugLogging*/ true);
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
// Create a new pass manager attached to it.
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Add transform passes.
// Promote allocas to registers.
TheFPM->add(createPromoteMemoryToRegisterPass());
// Do simple "peephole" optimizations and bit-twiddling optzns.
TheFPM->addPass(InstCombinePass());
TheFPM->add(createInstructionCombiningPass());
// Reassociate expressions.
TheFPM->addPass(ReassociatePass());
TheFPM->add(createReassociatePass());
// Eliminate Common SubExpressions.
TheFPM->addPass(GVNPass());
TheFPM->add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
TheFPM->addPass(SimplifyCFGPass());
TheFPM->add(createCFGSimplificationPass());
// Register analysis passes used in these transform passes.
TheFAM->registerPass([&] { return AAManager(); });
TheFAM->registerPass([&] { return AssumptionAnalysis(); });
TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
TheFAM->registerPass([&] { return LoopAnalysis(); });
TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
TheFAM->registerPass([&] {
return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
});
TheFAM->registerPass(
[&] { return PassInstrumentationAnalysis(ThePIC.get()); });
TheFAM->registerPass([&] { return TargetIRAnalysis(); });
TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
TheFPM->doInitialization();
}
static void HandleDefinition() {
@ -1181,7 +1145,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
}
} else {
// Skip token for error recovery.
@ -1213,7 +1177,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@ -1300,7 +1264,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
InitializeModuleAndManagers();
InitializeModuleAndPassManager();
// Run the main "interpreter loop" now.
MainLoop();