Skip to content

Commit 6f8a363

Browse files
authored
[Kaleidoscope] Add mem2reg pass to function pass manager (#119707)
Kaleidoscope has switched to new pass manager before (#72324), but both code and tutorial document have some missing parts. This pull request fixes the following problems: 1. Adds `PromotePass` to the function pass manager. This pass was removed during the switch from legacy pass manager to the new pass manager. 2. Syncs the tutorial with the code.
1 parent bc28be0 commit 6f8a363

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl07.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ the function:
336336
/// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
337337
/// the function. This is used for mutable variables etc.
338338
static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
339-
const std::string &VarName) {
339+
StringRef VarName) {
340340
IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
341341
TheFunction->getEntryBlock().begin());
342342
return TmpB.CreateAlloca(Type::getDoubleTy(*TheContext), nullptr,
@@ -440,11 +440,11 @@ get good codegen once again:
440440
.. code-block:: c++
441441

442442
// Promote allocas to registers.
443-
TheFPM->add(createPromoteMemoryToRegisterPass());
443+
TheFPM->addPass(PromotePass());
444444
// Do simple "peephole" optimizations and bit-twiddling optzns.
445-
TheFPM->add(createInstructionCombiningPass());
445+
TheFPM->addPass(InstCombinePass());
446446
// Reassociate expressions.
447-
TheFPM->add(createReassociatePass());
447+
TheFPM->addPass(ReassociatePass());
448448
...
449449

450450
It is interesting to see what the code looks like before and after the

llvm/examples/Kaleidoscope/Chapter7/toy.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "llvm/Transforms/Scalar/GVN.h"
2222
#include "llvm/Transforms/Scalar/Reassociate.h"
2323
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
24-
#include "llvm/Transforms/Utils.h"
24+
#include "llvm/Transforms/Utils/Mem2Reg.h"
2525
#include <algorithm>
2626
#include <cassert>
2727
#include <cctype>
@@ -1142,6 +1142,8 @@ static void InitializeModuleAndManagers() {
11421142
TheSI->registerCallbacks(*ThePIC, TheMAM.get());
11431143

11441144
// Add transform passes.
1145+
// Promote allocas to registers.
1146+
TheFPM->addPass(PromotePass());
11451147
// Do simple "peephole" optimizations and bit-twiddling optzns.
11461148
TheFPM->addPass(InstCombinePass());
11471149
// Reassociate expressions.

0 commit comments

Comments
 (0)