aboutsummaryrefslogtreecommitdiff
path: root/llvm/examples
diff options
context:
space:
mode:
authorLogikable <seanluchen@google.com>2023-10-18 09:25:50 -0700
committerGitHub <noreply@github.com>2023-10-18 09:25:50 -0700
commit7b94744e77aaf752abc2c6ab38ee3fdfafbeff9d (patch)
tree1485e426892258fd8b0293462d6cf9ab2c5499f6 /llvm/examples
parent47ed9219856791f4ad26b62d0b12601a1778392d (diff)
downloadllvm-7b94744e77aaf752abc2c6ab38ee3fdfafbeff9d.zip
llvm-7b94744e77aaf752abc2c6ab38ee3fdfafbeff9d.tar.gz
llvm-7b94744e77aaf752abc2c6ab38ee3fdfafbeff9d.tar.bz2
[Kaleidoscope] Switch to the new PassManager. (#69032)
Using the new pass manager is more verbose; let me know if the tutorial doesn't flow well with all the additions.
Diffstat (limited to 'llvm/examples')
-rw-r--r--llvm/examples/Kaleidoscope/Chapter4/toy.cpp70
-rw-r--r--llvm/examples/Kaleidoscope/Chapter5/toy.cpp72
-rw-r--r--llvm/examples/Kaleidoscope/Chapter6/toy.cpp72
-rw-r--r--llvm/examples/Kaleidoscope/Chapter7/toy.cpp74
4 files changed, 219 insertions, 69 deletions
diff --git a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
index fb443c7..19ec70e 100644
--- a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
@@ -1,21 +1,32 @@
#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>
@@ -413,8 +424,12 @@ 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;
@@ -535,7 +550,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
- TheFPM->run(*TheFunction);
+ TheFPM->run(*TheFunction, *TheFAM);
return TheFunction;
}
@@ -549,28 +564,51 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
-static void InitializeModuleAndPassManager() {
+static void InitializeModuleAndManagers() {
// Open a new context and module.
TheContext = std::make_unique<LLVMContext>();
- TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+ TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// 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());
+ // Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
- TheFPM->add(createInstructionCombiningPass());
+ TheFPM->addPass(InstCombinePass());
// Reassociate expressions.
- TheFPM->add(createReassociatePass());
+ TheFPM->addPass(ReassociatePass());
// Eliminate Common SubExpressions.
- TheFPM->add(createGVNPass());
+ TheFPM->addPass(GVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
- TheFPM->add(createCFGSimplificationPass());
-
- TheFPM->doInitialization();
+ TheFPM->addPass(SimplifyCFGPass());
+
+ // 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(); });
}
static void HandleDefinition() {
@@ -581,7 +619,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
}
} else {
// Skip token for error recovery.
@@ -613,7 +651,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@@ -699,7 +737,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Run the main "interpreter loop" now.
MainLoop();
diff --git a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
index dc7174a..f41f08de 100644
--- a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
@@ -1,6 +1,13 @@
#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"
@@ -8,15 +15,19 @@
#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>
@@ -540,8 +551,12 @@ 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;
@@ -809,7 +824,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
- TheFPM->run(*TheFunction);
+ TheFPM->run(*TheFunction, *TheFAM);
return TheFunction;
}
@@ -823,28 +838,51 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
-static void InitializeModuleAndPassManager() {
- // Open a new module.
+static void InitializeModuleAndManagers() {
+ // Open a new context and module.
TheContext = std::make_unique<LLVMContext>();
- TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+ TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// 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());
+ // Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
- TheFPM->add(createInstructionCombiningPass());
+ TheFPM->addPass(InstCombinePass());
// Reassociate expressions.
- TheFPM->add(createReassociatePass());
+ TheFPM->addPass(ReassociatePass());
// Eliminate Common SubExpressions.
- TheFPM->add(createGVNPass());
+ TheFPM->addPass(GVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
- TheFPM->add(createCFGSimplificationPass());
-
- TheFPM->doInitialization();
+ TheFPM->addPass(SimplifyCFGPass());
+
+ // 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(); });
}
static void HandleDefinition() {
@@ -855,7 +893,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
}
} else {
// Skip token for error recovery.
@@ -887,7 +925,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@@ -973,7 +1011,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Run the main "interpreter loop" now.
MainLoop();
diff --git a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
index f40eea3..ad275ed 100644
--- a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
@@ -1,6 +1,13 @@
#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"
@@ -8,15 +15,19 @@
#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>
@@ -632,8 +643,12 @@ 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;
@@ -925,7 +940,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
- TheFPM->run(*TheFunction);
+ TheFPM->run(*TheFunction, *TheFAM);
return TheFunction;
}
@@ -942,28 +957,51 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
-static void InitializeModuleAndPassManager() {
- // Open a new module.
+static void InitializeModuleAndManagers() {
+ // Open a new context and module.
TheContext = std::make_unique<LLVMContext>();
- TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+ TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// 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());
+ // Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
- TheFPM->add(createInstructionCombiningPass());
+ TheFPM->addPass(InstCombinePass());
// Reassociate expressions.
- TheFPM->add(createReassociatePass());
+ TheFPM->addPass(ReassociatePass());
// Eliminate Common SubExpressions.
- TheFPM->add(createGVNPass());
+ TheFPM->addPass(GVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
- TheFPM->add(createCFGSimplificationPass());
-
- TheFPM->doInitialization();
+ TheFPM->addPass(SimplifyCFGPass());
+
+ // 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(); });
}
static void HandleDefinition() {
@@ -974,7 +1012,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
}
} else {
// Skip token for error recovery.
@@ -1006,7 +1044,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@@ -1092,7 +1130,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Run the main "interpreter loop" now.
MainLoop();
diff --git a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
index 5bbab8d..f2954a4 100644
--- a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
@@ -1,6 +1,13 @@
#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"
@@ -8,15 +15,19 @@
#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>
@@ -705,8 +716,12 @@ 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;
@@ -1094,7 +1109,7 @@ Function *FunctionAST::codegen() {
verifyFunction(*TheFunction);
// Run the optimizer on the function.
- TheFPM->run(*TheFunction);
+ TheFPM->run(*TheFunction, *TheFAM);
return TheFunction;
}
@@ -1111,30 +1126,51 @@ Function *FunctionAST::codegen() {
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
-static void InitializeModuleAndPassManager() {
- // Open a new module.
+static void InitializeModuleAndManagers() {
+ // Open a new context and module.
TheContext = std::make_unique<LLVMContext>();
- TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+ TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
// 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());
- // Promote allocas to registers.
- TheFPM->add(createPromoteMemoryToRegisterPass());
+ // Add transform passes.
// Do simple "peephole" optimizations and bit-twiddling optzns.
- TheFPM->add(createInstructionCombiningPass());
+ TheFPM->addPass(InstCombinePass());
// Reassociate expressions.
- TheFPM->add(createReassociatePass());
+ TheFPM->addPass(ReassociatePass());
// Eliminate Common SubExpressions.
- TheFPM->add(createGVNPass());
+ TheFPM->addPass(GVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
- TheFPM->add(createCFGSimplificationPass());
-
- TheFPM->doInitialization();
+ TheFPM->addPass(SimplifyCFGPass());
+
+ // 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(); });
}
static void HandleDefinition() {
@@ -1145,7 +1181,7 @@ static void HandleDefinition() {
fprintf(stderr, "\n");
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
}
} else {
// Skip token for error recovery.
@@ -1177,7 +1213,7 @@ static void HandleTopLevelExpression() {
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@@ -1264,7 +1300,7 @@ int main() {
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
- InitializeModuleAndPassManager();
+ InitializeModuleAndManagers();
// Run the main "interpreter loop" now.
MainLoop();