Skip to content

Commit

Permalink
Chapter 4: IR optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
srp-mx committed Dec 21, 2022
1 parent 09ace40 commit 99fdc00
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/ast/ast_codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ FunctionAST::codegen()
// Validate the generated code, checking for consistency
llvm::verifyFunction(*TheFunction);

// Optimize the function
TheFPM->run(*TheFunction);

return TheFunction;
}

Expand Down
26 changes: 26 additions & 0 deletions src/kaleidoscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ InitializeModule()
Builder = std::make_unique<llvm::IRBuilder<>>(*TheContext);
}

internal void
InitializePassManager()
{
// Create a new pass manager attached to it.
TheFPM = std::make_unique<llvml::FunctionPassManager>(TheModule.get());

// Do simple "peephole" and bit-twiddling optimizations.
TheFPM->add(llvm::createInstructionCombiningPass());
// Reassociate expressions
TheFPM->add(llvm::createReassociatePass());
// Eliminate Common SubExpressions
TheFPM->add(llvm::createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc.)
TheFPM->add(llvm::createCFGSimplificationPass());

TheFPM->doInitialization();
// TODO(srp): See https://llvm.org/docs/Passes.html
}

internal void
InitializeLLVM()
{
InitializeModule();
InitializePassManager();
}

internal void
HandleDefinition()
{
Expand Down
2 changes: 1 addition & 1 deletion src/linux_kaleidoscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main()
getNextToken();

// Make the module, which holds all the code.
InitializeModule();
InitializeLLVM();

// Run the main "interpreter loop" now
MainLoop();
Expand Down
7 changes: 7 additions & 0 deletions src/platform/llvm/linux_llvm_include.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"

#include "llvm/Pass.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"

3 changes: 3 additions & 0 deletions src/platform/llvm/llvm_include.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
#include <map>
#include <string>

namespace llvml = llvm::legacy;

global_variable std::unique_ptr<llvm::LLVMContext> TheContext;
global_variable std::unique_ptr<llvm::Module> TheModule;
global_variable std::unique_ptr<llvm::IRBuilder<>> Builder;
global_variable std::map<std::string, llvm::Value*> NamedValues;
global_variable std::unique_ptr<llvml::FunctionPassManager> TheFPM;

0 comments on commit 99fdc00

Please sign in to comment.