Skip to content

Commit 63d19cf

Browse files
committed
Revert "[Kaleidoscope] Switch to the new PassManager. (#69032)"
This reverts commit 7b94744. 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.
1 parent 2db9b6a commit 63d19cf

File tree

5 files changed

+95
-286
lines changed

5 files changed

+95
-286
lines changed

llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.rst

Lines changed: 26 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ use, in the form of "passes".
9494
LLVM Optimization Passes
9595
========================
9696

97+
.. warning::
98+
99+
Due to the transition to the new PassManager infrastructure this tutorial
100+
is based on ``llvm::legacy::FunctionPassManager`` which can be found in
101+
`LegacyPassManager.h <https://llvm.org/doxygen/classllvm_1_1legacy_1_1FunctionPassManager.html>`_.
102+
For the purpose of the this tutorial the above should be used until
103+
the pass manager transition is complete.
104+
97105
LLVM provides many optimization passes, which do many different sorts of
98106
things and have different tradeoffs. Unlike other systems, LLVM doesn't
99107
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
119127
exactly the code we have now, except that we would defer running the
120128
optimizer until the entire file has been parsed.
121129

122-
In addition to the distinction between function and module passes, passes can be
123-
divided into transform and analysis passes. Transform passes mutate the IR, and
124-
analysis passes compute information that other passes can use. In order to add
125-
a transform pass, all analysis passes it depends upon must be registered in
126-
advance.
127-
128130
In order to get per-function optimizations going, we need to set up a
129131
`FunctionPassManager <../../WritingAnLLVMPass.html#what-passmanager-doesr>`_ to hold
130132
and organize the LLVM optimizations that we want to run. Once we have
131133
that, we can add a set of optimizations to run. We'll need a new
132134
FunctionPassManager for each module that we want to optimize, so we'll
133-
add to a function created in the previous chapter (``InitializeModule()``):
135+
write a function to create and initialize both the module and pass manager
136+
for us:
134137

135138
.. code-block:: c++
136139

137-
void InitializeModuleAndManagers(void) {
140+
void InitializeModuleAndPassManager(void) {
138141
// Open a new context and module.
139-
TheContext = std::make_unique<LLVMContext>();
140-
TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
141-
TheModule->setDataLayout(TheJIT->getDataLayout());
142-
143-
// Create a new builder for the module.
144-
Builder = std::make_unique<IRBuilder<>>(*TheContext);
145-
146-
// Create new pass and analysis managers.
147-
TheFPM = std::make_unique<FunctionPassManager>();
148-
TheFAM = std::make_unique<FunctionAnalysisManager>();
149-
TheMAM = std::make_unique<ModuleAnalysisManager>();
150-
ThePIC = std::make_unique<PassInstrumentationCallbacks>();
151-
TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
152-
/*DebugLogging*/ true);
153-
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
154-
...
155-
156-
After initializing the global module ``TheModule`` and the FunctionPassManager,
157-
we need to initialize other parts of the framework. The FunctionAnalysisManager
158-
and ModuleAnalysisManager allow us to add analysis passes that run across the
159-
function and the whole module, respectively. PassInstrumentationCallbacks
160-
and StandardInstrumentations are required for the pass instrumentation
161-
framework, which allows developers to customize what
162-
happens between passes.
142+
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
163143
164-
Once these managers are set up, we use a series of "addPass" calls to add a
165-
bunch of LLVM transform passes:
166-
167-
.. code-block:: c++
144+
// Create a new pass manager attached to it.
145+
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
168146

169-
// Add transform passes.
170147
// Do simple "peephole" optimizations and bit-twiddling optzns.
171-
TheFPM->addPass(InstCombinePass());
148+
TheFPM->add(createInstructionCombiningPass());
172149
// Reassociate expressions.
173-
TheFPM->addPass(ReassociatePass());
150+
TheFPM->add(createReassociatePass());
174151
// Eliminate Common SubExpressions.
175-
TheFPM->addPass(GVNPass());
152+
TheFPM->add(createGVNPass());
176153
// Simplify the control flow graph (deleting unreachable blocks, etc).
177-
TheFPM->addPass(SimplifyCFGPass());
154+
TheFPM->add(createCFGSimplificationPass());
155+
156+
TheFPM->doInitialization();
157+
}
158+
159+
This code initializes the global module ``TheModule``, and the function pass
160+
manager ``TheFPM``, which is attached to ``TheModule``. Once the pass manager is
161+
set up, we use a series of "add" calls to add a bunch of LLVM passes.
178162

179163
In this case, we choose to add four optimization passes.
180164
The passes we choose here are a pretty standard set
181165
of "cleanup" optimizations that are useful for a wide variety of code. I won't
182166
delve into what they do but, believe me, they are a good starting place :).
183167

184-
Next, we register the analysis passes used by the transform passes. This is
185-
generally done using ``PassBuilder::register...Analyses()``, but we'll do it
186-
manually to make clearer what's under the hood.
187-
188-
.. code-block:: c++
189-
190-
// Register analysis passes used in these transform passes.
191-
TheFAM->registerPass([&] { return AAManager(); });
192-
TheFAM->registerPass([&] { return AssumptionAnalysis(); });
193-
TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
194-
TheFAM->registerPass([&] { return LoopAnalysis(); });
195-
TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
196-
TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
197-
TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
198-
TheFAM->registerPass([&] {
199-
return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
200-
});
201-
TheFAM->registerPass(
202-
[&] { return PassInstrumentationAnalysis(ThePIC.get()); });
203-
TheFAM->registerPass([&] { return TargetIRAnalysis(); });
204-
TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
205-
206-
TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
207-
}
208-
209168
Once the PassManager is set up, we need to make use of it. We do this by
210169
running it after our newly created function is constructed (in
211170
``FunctionAST::codegen()``), but before it is returned to the client:
@@ -220,7 +179,7 @@ running it after our newly created function is constructed (in
220179
verifyFunction(*TheFunction);
221180
222181
// Optimize the function.
223-
TheFPM->run(*TheFunction, *TheFAM);
182+
TheFPM->run(*TheFunction);
224183
225184
return TheFunction;
226185
}

llvm/examples/Kaleidoscope/Chapter4/toy.cpp

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
#include "../include/KaleidoscopeJIT.h"
22
#include "llvm/ADT/APFloat.h"
33
#include "llvm/ADT/STLExtras.h"
4-
#include "llvm/Analysis/AssumptionCache.h"
5-
#include "llvm/Analysis/BasicAliasAnalysis.h"
6-
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
7-
#include "llvm/Analysis/MemorySSA.h"
8-
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
9-
#include "llvm/Analysis/ProfileSummaryInfo.h"
10-
#include "llvm/Analysis/TargetTransformInfo.h"
114
#include "llvm/IR/BasicBlock.h"
125
#include "llvm/IR/Constants.h"
136
#include "llvm/IR/DerivedTypes.h"
147
#include "llvm/IR/Function.h"
158
#include "llvm/IR/IRBuilder.h"
169
#include "llvm/IR/LLVMContext.h"
10+
#include "llvm/IR/LegacyPassManager.h"
1711
#include "llvm/IR/Module.h"
18-
#include "llvm/IR/PassManager.h"
1912
#include "llvm/IR/Type.h"
2013
#include "llvm/IR/Verifier.h"
21-
#include "llvm/Passes/PassBuilder.h"
22-
#include "llvm/Passes/StandardInstrumentations.h"
2314
#include "llvm/Support/TargetSelect.h"
2415
#include "llvm/Target/TargetMachine.h"
2516
#include "llvm/Transforms/InstCombine/InstCombine.h"
2617
#include "llvm/Transforms/Scalar.h"
2718
#include "llvm/Transforms/Scalar/GVN.h"
28-
#include "llvm/Transforms/Scalar/Reassociate.h"
29-
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
3019
#include <algorithm>
3120
#include <cassert>
3221
#include <cctype>
@@ -424,12 +413,8 @@ static std::unique_ptr<LLVMContext> TheContext;
424413
static std::unique_ptr<Module> TheModule;
425414
static std::unique_ptr<IRBuilder<>> Builder;
426415
static std::map<std::string, Value *> NamedValues;
416+
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
427417
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
428-
static std::unique_ptr<FunctionPassManager> TheFPM;
429-
static std::unique_ptr<FunctionAnalysisManager> TheFAM;
430-
static std::unique_ptr<ModuleAnalysisManager> TheMAM;
431-
static std::unique_ptr<PassInstrumentationCallbacks> ThePIC;
432-
static std::unique_ptr<StandardInstrumentations> TheSI;
433418
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
434419
static ExitOnError ExitOnErr;
435420

@@ -550,7 +535,7 @@ Function *FunctionAST::codegen() {
550535
verifyFunction(*TheFunction);
551536

552537
// Run the optimizer on the function.
553-
TheFPM->run(*TheFunction, *TheFAM);
538+
TheFPM->run(*TheFunction);
554539

555540
return TheFunction;
556541
}
@@ -564,51 +549,28 @@ Function *FunctionAST::codegen() {
564549
// Top-Level parsing and JIT Driver
565550
//===----------------------------------------------------------------------===//
566551

567-
static void InitializeModuleAndManagers() {
552+
static void InitializeModuleAndPassManager() {
568553
// Open a new context and module.
569554
TheContext = std::make_unique<LLVMContext>();
570-
TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
555+
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
571556
TheModule->setDataLayout(TheJIT->getDataLayout());
572557

573558
// Create a new builder for the module.
574559
Builder = std::make_unique<IRBuilder<>>(*TheContext);
575560

576-
// Create new pass and analysis managers.
577-
TheFPM = std::make_unique<FunctionPassManager>();
578-
TheFAM = std::make_unique<FunctionAnalysisManager>();
579-
TheMAM = std::make_unique<ModuleAnalysisManager>();
580-
ThePIC = std::make_unique<PassInstrumentationCallbacks>();
581-
TheSI = std::make_unique<StandardInstrumentations>(*TheContext,
582-
/*DebugLogging*/ true);
583-
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
561+
// Create a new pass manager attached to it.
562+
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
584563

585-
// Add transform passes.
586564
// Do simple "peephole" optimizations and bit-twiddling optzns.
587-
TheFPM->addPass(InstCombinePass());
565+
TheFPM->add(createInstructionCombiningPass());
588566
// Reassociate expressions.
589-
TheFPM->addPass(ReassociatePass());
567+
TheFPM->add(createReassociatePass());
590568
// Eliminate Common SubExpressions.
591-
TheFPM->addPass(GVNPass());
569+
TheFPM->add(createGVNPass());
592570
// Simplify the control flow graph (deleting unreachable blocks, etc).
593-
TheFPM->addPass(SimplifyCFGPass());
594-
595-
// Register analysis passes used in these transform passes.
596-
TheFAM->registerPass([&] { return AAManager(); });
597-
TheFAM->registerPass([&] { return AssumptionAnalysis(); });
598-
TheFAM->registerPass([&] { return DominatorTreeAnalysis(); });
599-
TheFAM->registerPass([&] { return LoopAnalysis(); });
600-
TheFAM->registerPass([&] { return MemoryDependenceAnalysis(); });
601-
TheFAM->registerPass([&] { return MemorySSAAnalysis(); });
602-
TheFAM->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
603-
TheFAM->registerPass([&] {
604-
return OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>(*TheMAM);
605-
});
606-
TheFAM->registerPass(
607-
[&] { return PassInstrumentationAnalysis(ThePIC.get()); });
608-
TheFAM->registerPass([&] { return TargetIRAnalysis(); });
609-
TheFAM->registerPass([&] { return TargetLibraryAnalysis(); });
610-
611-
TheMAM->registerPass([&] { return ProfileSummaryAnalysis(); });
571+
TheFPM->add(createCFGSimplificationPass());
572+
573+
TheFPM->doInitialization();
612574
}
613575

614576
static void HandleDefinition() {
@@ -619,7 +581,7 @@ static void HandleDefinition() {
619581
fprintf(stderr, "\n");
620582
ExitOnErr(TheJIT->addModule(
621583
ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
622-
InitializeModuleAndManagers();
584+
InitializeModuleAndPassManager();
623585
}
624586
} else {
625587
// Skip token for error recovery.
@@ -651,7 +613,7 @@ static void HandleTopLevelExpression() {
651613

652614
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
653615
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
654-
InitializeModuleAndManagers();
616+
InitializeModuleAndPassManager();
655617

656618
// Search the JIT for the __anon_expr symbol.
657619
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
@@ -737,7 +699,7 @@ int main() {
737699

738700
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
739701

740-
InitializeModuleAndManagers();
702+
InitializeModuleAndPassManager();
741703

742704
// Run the main "interpreter loop" now.
743705
MainLoop();

0 commit comments

Comments
 (0)