Skip to content

Start sketching out the optimizer #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ExternalProject_Add(llvm
GIT_SHALLOW
1
GIT_TAG
b1e6cc762cd729de01f1b4f3c0f6d9a493635ae0
release_50
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=RelWithDebInfo
-DBUILD_SHARED_LIBS=NO
Expand Down
12 changes: 12 additions & 0 deletions include/glow/Optimizer/Optimizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef GLOW_OPTIMIZER_OPTIMIZER_H
#define GLOW_OPTIMIZER_OPTIMIZER_H

namespace glow {

class Module;

void optimize(Module &M);

} // namespace glow

#endif // GLOW_OPTIMIZER_OPTIMIZER_H
3 changes: 2 additions & 1 deletion src/glow/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

add_subdirectory(IR)
add_subdirectory(Importer)
add_subdirectory(Interpreter)
add_subdirectory(IR)
add_subdirectory(Models)
add_subdirectory(Network)
add_subdirectory(Optimizer)
add_subdirectory(Support)

8 changes: 8 additions & 0 deletions src/glow/Optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

add_library(Optimizer
Optimizer.cpp)

target_link_libraries(Optimizer
PRIVATE
IR)

54 changes: 54 additions & 0 deletions src/glow/Optimizer/Optimizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "glow/Optimizer/Optimizer.h"
#include "glow/IR/IR.h"
#include "glow/IR/Instrs.h"
#include "glow/Support/Casting.h"

#include <unordered_map>

using namespace glow;

/// Hoists Dealloc instructions right after their last use.
static void hoistDealloc(Module &M) {
using iterator = Module::InstListTy::iterator;
// Maps activation instructions to their last non-dealloc user.
std::unordered_map<AllocActivationInst *, iterator> lastUser;
auto &instrs = M.getInstrs();

// Record the last use of each dealloc.
for (auto it = instrs.begin(), e = instrs.end(); it != e; ++it) {
if (isa<DeallocActivationInst>(*it))
continue;

for (int i = 0, e = (*it)->getNumOperands(); i < e; i++) {
auto op = (*it)->getOperand(i).first;
if (auto alloc = dyn_cast<AllocActivationInst>(op)) {
lastUser[alloc] = it;
}
}
}

// Now that we've found the last user we can hoist the instruction.
for (auto it = instrs.begin(), e = instrs.end(); it != e; /* increment below */) {
iterator curr = it;
auto *da = dyn_cast<DeallocActivationInst>(*curr);
if (!da) {
++it;
continue;
}

auto *alloc = cast<AllocActivationInst>(da->getOperand(0).first);

it = instrs.erase(curr);
auto &where = lastUser[alloc];
where++;
instrs.insert(where, da);
}
}

void glow::optimize(Module &M) {
M.verify();

hoistDealloc(M);

M.verify();
}