Skip to content

Commit aa3effc

Browse files
authored
Move optimizeFunction from ExecutionEngine into the base Backend so it can be used outside of the EE. (#2289)
1 parent da381d5 commit aa3effc

File tree

6 files changed

+58
-64
lines changed

6 files changed

+58
-64
lines changed

include/glow/Backends/Backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class Backend {
8282
/// \returns true if the Backend wants the buffer sharing optimization
8383
/// performed.
8484
virtual bool shouldShareBuffers() const { return true; }
85+
86+
/// Optimize the Function \p F given compilation mode \p mode.
87+
void optimizeFunction(CompilationMode mode, Function *F);
8588
};
8689

8790
/// Create a backend of kind \p kind.

include/glow/ExecutionEngine/ExecutionEngine.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ class ExecutionEngine final {
4343
/// A glow function compiled for this ExecutionEngine's backend.
4444
std::unique_ptr<CompiledFunction> function_;
4545

46-
/// Optimize the Function \p F given compilation mode \p mode.
47-
void optimizeFunction(CompilationMode mode, Function *F);
48-
4946
public:
5047
ExecutionEngine(BackendKind backendKind = BackendKind::Interpreter);
5148

lib/Backends/Backend.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "glow/Backends/Backend.h"
18+
#include "glow/Graph/Graph.h"
19+
#include "glow/Optimizer/Optimizer.h"
20+
21+
using namespace glow;
22+
23+
void Backend::optimizeFunction(CompilationMode mode, Function *F) {
24+
// Verify the function pre-optimization/lowering.
25+
assert(F->verify() && "Function must be valid");
26+
27+
// Optimize the graph.
28+
::glow::optimize(F, mode);
29+
30+
// Allow the backend to transform the graph prior to lowering.
31+
if (transformPreLowering(F, mode)) {
32+
// Optimize the graph again after the backend transformation.
33+
// In particular, DCE is very likely to be useful.
34+
::glow::optimize(F, mode);
35+
}
36+
37+
// Lower the graph into a sequence of low-level linear algebra operations.
38+
::glow::lower(F, *this);
39+
40+
// Optimize the graph again.
41+
::glow::optimize(F, mode);
42+
43+
// Allow the backend to transform the graph after lowering.
44+
if (transformPostLowering(F, mode)) {
45+
// Optimize the graph again after the backend transformation.
46+
// In particular, DCE is very likely to be useful.
47+
::glow::optimize(F, mode);
48+
}
49+
}

lib/Backends/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
add_library(Backends Backends.cpp)
1+
add_library(Backends
2+
Backend.cpp
3+
Backends.cpp)
24

35
add_library(BackendUtils BackendUtils.cpp)
46

lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,42 +121,14 @@ void glow::runBatch(ExecutionEngine &EE, Context &ctx, size_t iterations,
121121
}
122122
}
123123

124-
void ExecutionEngine::optimizeFunction(CompilationMode mode, Function *F) {
125-
// Verify the function pre-optimization/lowering.
126-
assert(F->verify() && "Function must be valid");
127-
128-
// Optimize the graph.
129-
::glow::optimize(F, mode);
130-
131-
// Allow the backend to transform the graph prior to lowering.
132-
if (backend_->transformPreLowering(F, mode)) {
133-
// Optimize the graph again after the backend transformation.
134-
// In particular, DCE is very likely to be useful.
135-
::glow::optimize(F, mode);
136-
}
137-
138-
// Lower the graph into a sequence of low-level linear algebra operations.
139-
::glow::lower(F, *backend_);
140-
141-
// Optimize the graph again.
142-
::glow::optimize(F, mode);
143-
144-
// Allow the backend to transform the graph after lowering.
145-
if (backend_->transformPostLowering(F, mode)) {
146-
// Optimize the graph again after the backend transformation.
147-
// In particular, DCE is very likely to be useful.
148-
::glow::optimize(F, mode);
149-
}
150-
}
151-
152124
void ExecutionEngine::compile(CompilationMode mode, Function *F) {
153-
optimizeFunction(mode, F);
125+
backend_->optimizeFunction(mode, F);
154126
function_ = backend_->compile(F);
155127
}
156128

157129
void ExecutionEngine::save(CompilationMode mode, Function *F,
158130
llvm::StringRef outputDir,
159131
llvm::StringRef networkName) {
160-
optimizeFunction(mode, F);
132+
backend_->optimizeFunction(mode, F);
161133
backend_->save(F, outputDir, networkName);
162134
}

tests/unittests/CPUDeviceManagerTest.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,13 @@ std::unique_ptr<Module> makeBasicModule(std::string functionName = "main") {
4141
return module;
4242
}
4343

44-
// TODO: This really should be a helper function somewhere
45-
void optimizeFunction(Backend *backend, CompilationMode mode, Function *F) {
46-
// Verify the function pre-optimization/lowering.
47-
assert(F->verify() && "Function must be valid");
48-
49-
// Optimize the graph.
50-
::glow::optimize(F, mode);
51-
52-
// Allow the backend to transform the graph prior to lowering.
53-
if (backend->transformPreLowering(F, mode)) {
54-
// Optimize the graph again after the backend transformation.
55-
// In particular, DCE is very likely to be useful.
56-
::glow::optimize(F, mode);
57-
}
58-
59-
// Lower the graph into a sequence of low-level linear algebra operations.
60-
::glow::lower(F, *backend);
61-
62-
// Optimize the graph again.
63-
::glow::optimize(F, mode);
64-
65-
// Allow the backend to transform the graph after lowering.
66-
if (backend->transformPostLowering(F, mode)) {
67-
// Optimize the graph again after the backend transformation.
68-
// In particular, DCE is very likely to be useful.
69-
::glow::optimize(F, mode);
70-
}
71-
}
72-
7344
FunctionMapTy
7445
compileFunctions(Module *module,
7546
std::vector<std::unique_ptr<CompiledFunction>> &backing) {
7647
FunctionMapTy results;
7748
auto *backend = createBackend(BackendKind::CPU);
7849
for (auto *F : module->getFunctions()) {
79-
optimizeFunction(backend, CompilationMode::Infer, F);
50+
backend->optimizeFunction(CompilationMode::Infer, F);
8051
auto f = backend->compile(F);
8152
backing.push_back(std::move(f));
8253
results.emplace(F->getName(), backing.back().get());

0 commit comments

Comments
 (0)