Skip to content
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

Move optimizeFunction into the base Backend. #2289

Merged
merged 1 commit into from
Jan 23, 2019
Merged
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
3 changes: 3 additions & 0 deletions include/glow/Backends/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Backend {
/// \returns true if the Backend wants the buffer sharing optimization
/// performed.
virtual bool shouldShareBuffers() const { return true; }

/// Optimize the Function \p F given compilation mode \p mode.
void optimizeFunction(CompilationMode mode, Function *F);
};

/// Create a backend of kind \p kind.
Expand Down
3 changes: 0 additions & 3 deletions include/glow/ExecutionEngine/ExecutionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class ExecutionEngine final {
/// A glow function compiled for this ExecutionEngine's backend.
std::unique_ptr<CompiledFunction> function_;

/// Optimize the Function \p F given compilation mode \p mode.
void optimizeFunction(CompilationMode mode, Function *F);

public:
ExecutionEngine(BackendKind backendKind = BackendKind::Interpreter);

Expand Down
49 changes: 49 additions & 0 deletions lib/Backends/Backend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "glow/Backends/Backend.h"
#include "glow/Graph/Graph.h"
#include "glow/Optimizer/Optimizer.h"

using namespace glow;

void Backend::optimizeFunction(CompilationMode mode, Function *F) {
// Verify the function pre-optimization/lowering.
assert(F->verify() && "Function must be valid");

// Optimize the graph.
::glow::optimize(F, mode);

// Allow the backend to transform the graph prior to lowering.
if (transformPreLowering(F, mode)) {
// Optimize the graph again after the backend transformation.
// In particular, DCE is very likely to be useful.
::glow::optimize(F, mode);
}

// Lower the graph into a sequence of low-level linear algebra operations.
::glow::lower(F, *this);

// Optimize the graph again.
::glow::optimize(F, mode);

// Allow the backend to transform the graph after lowering.
if (transformPostLowering(F, mode)) {
// Optimize the graph again after the backend transformation.
// In particular, DCE is very likely to be useful.
::glow::optimize(F, mode);
}
}
4 changes: 3 additions & 1 deletion lib/Backends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
add_library(Backends Backends.cpp)
add_library(Backends
Backend.cpp
Backends.cpp)

add_library(BackendUtils BackendUtils.cpp)

Expand Down
32 changes: 2 additions & 30 deletions lib/ExecutionEngine/ExecutionEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,14 @@ void glow::runBatch(ExecutionEngine &EE, Context &ctx, size_t iterations,
}
}

void ExecutionEngine::optimizeFunction(CompilationMode mode, Function *F) {
// Verify the function pre-optimization/lowering.
assert(F->verify() && "Function must be valid");

// Optimize the graph.
::glow::optimize(F, mode);

// Allow the backend to transform the graph prior to lowering.
if (backend_->transformPreLowering(F, mode)) {
// Optimize the graph again after the backend transformation.
// In particular, DCE is very likely to be useful.
::glow::optimize(F, mode);
}

// Lower the graph into a sequence of low-level linear algebra operations.
::glow::lower(F, *backend_);

// Optimize the graph again.
::glow::optimize(F, mode);

// Allow the backend to transform the graph after lowering.
if (backend_->transformPostLowering(F, mode)) {
// Optimize the graph again after the backend transformation.
// In particular, DCE is very likely to be useful.
::glow::optimize(F, mode);
}
}

void ExecutionEngine::compile(CompilationMode mode, Function *F) {
optimizeFunction(mode, F);
backend_->optimizeFunction(mode, F);
function_ = backend_->compile(F);
}

void ExecutionEngine::save(CompilationMode mode, Function *F,
llvm::StringRef outputDir,
llvm::StringRef networkName) {
optimizeFunction(mode, F);
backend_->optimizeFunction(mode, F);
backend_->save(F, outputDir, networkName);
}
31 changes: 1 addition & 30 deletions tests/unittests/CPUDeviceManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,13 @@ std::unique_ptr<Module> makeBasicModule(std::string functionName = "main") {
return module;
}

// TODO: This really should be a helper function somewhere
void optimizeFunction(Backend *backend, CompilationMode mode, Function *F) {
// Verify the function pre-optimization/lowering.
assert(F->verify() && "Function must be valid");

// Optimize the graph.
::glow::optimize(F, mode);

// Allow the backend to transform the graph prior to lowering.
if (backend->transformPreLowering(F, mode)) {
// Optimize the graph again after the backend transformation.
// In particular, DCE is very likely to be useful.
::glow::optimize(F, mode);
}

// Lower the graph into a sequence of low-level linear algebra operations.
::glow::lower(F, *backend);

// Optimize the graph again.
::glow::optimize(F, mode);

// Allow the backend to transform the graph after lowering.
if (backend->transformPostLowering(F, mode)) {
// Optimize the graph again after the backend transformation.
// In particular, DCE is very likely to be useful.
::glow::optimize(F, mode);
}
}

FunctionMapTy
compileFunctions(Module *module,
std::vector<std::unique_ptr<CompiledFunction>> &backing) {
FunctionMapTy results;
auto *backend = createBackend(BackendKind::CPU);
for (auto *F : module->getFunctions()) {
optimizeFunction(backend, CompilationMode::Infer, F);
backend->optimizeFunction(CompilationMode::Infer, F);
auto f = backend->compile(F);
backing.push_back(std::move(f));
results.emplace(F->getName(), backing.back().get());
Expand Down