Skip to content

Added new compileFunctions method #2531

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

Merged
merged 1 commit into from
Mar 13, 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
25 changes: 17 additions & 8 deletions docs/Backends.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ are two pure virtual functions all backends must implement:

- `virtual std::unique_ptr<CompiledFunction> compile(Function *F) const;`

- This function takes a `Function *F` to compile. It should return a unique pointer to the
[`CompiledFunction`](#compiledfunction-abstract-class) of `F`. It also stores the constants used by the function in the `CompiledFunction`'s [RuntimeBundle](#runtimebundle-helper-class). If the backend uses Glow low-level IR, it can call `generateAndOptimizeIR()` to generate an optimized `IRFunction`.
- This function takes a `Function *F` to compile with default
[`CompilationOptions`](#compilationoptions-abstract-class). It should return a unique pointer to the
[`CompiledFunction`](#compiledfunction-abstract-class) of `F`. If the backend uses Glow low-level IR, it can call `generateAndOptimizeIR()` to generate an optimized `IRFunction`.

- `virtual std::unique_ptr<CompiledFunction> compileWithoutConstants(Function *F) const;`
- `virtual std::unique_ptr<CompiledFunction> compile(Function *F, CompilationOptions &opts) const;`

- This function takes a `Function *F` to compile. This function does not store
constants in the `RuntimeBundle` this allows for that action to be handled later. It should return
a unique pointer to the [`CompiledFunction`](#compiledfunction-abstract-class) of `F`.
If the backend uses Glow low-level IR, it can call `generateAndOptimizeIR()` to generate
an optimized `IRFunction`.
- This function takes a `Function *F` and the provided
[`CompilationOptions`](#compilationoptions-abstract-class). It should return a unique pointer to the
[`CompiledFunction`](#compiledfunction-abstract-class) of `F`. If the backend uses Glow low-level IR, it can call `generateAndOptimizeIR()` to generate an optimized `IRFunction`.

- `virtual std::vector<std::unique_ptr<CompiledFunction>> compileFunctions(llvm::ArrayRef<Function *> functions, CompilationOptions &opts) const;`
- This function takes an `ArrayRef` of `Function *`s and compiles them using the same `CompilationOptions` object for all functions. This allows the compiler to reason over things like shared constants between functions.

- `virtual bool isOpSupported(const NodeInfo &NI) const;`

Expand Down Expand Up @@ -117,6 +119,13 @@ to a context, and allows the `Function` to be freed after compilation. The symbo
is stored in a table where the key is the symbol name and the payload contains symbol information
including, size, offset, type, and whether it is an input or output of the function. `RuntimeBundle` also contains a pointer that may point to a block of memory that contains the constants for the `CompiledFunction` if that `Backend` uses it.

### `CompilationOptions` Helper Class

`CompilationOptions` is a helper class that contains the options for compilation. The options include:
- `CompilationMode mode` - This can be Infer or Train. Default: Infer
- `bool collectConstants` - Whether constants should be collected and stored in the `CompiledFunction`'s [RuntimeBundle](#runtimebundle-helper-class). Default: True
- `bool autoInstrument` - Whether `TraceEvents` should be inserted for profiling. Default: False

## Backend-Specific Nodes and Instructions Transformations

Different backends may prefer to transform or optimize the graph differently for
Expand Down
13 changes: 13 additions & 0 deletions include/glow/Backends/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ class Backend {
/// \returns the kind of Backend this is.
virtual BackendKind getBackendKind() const = 0;

/// Generate code for a vector of functions, \p functions. All compilations
/// use the same settings provided by \p opts. This allows the compiler to
/// support shared constants between functions.
virtual std::vector<std::unique_ptr<CompiledFunction>>
compileFunctions(llvm::ArrayRef<Function *> functions,
CompilationOptions &opts) const {
std::vector<std::unique_ptr<CompiledFunction>> compiledFunctions;
for (auto &function : functions) {
compiledFunctions.push_back(compile(function, opts));
}
return compiledFunctions;
}

virtual std::unique_ptr<CompiledFunction> compile(Function *F) const {
CompilationOptions opts;
return compile(F, opts);
Expand Down
17 changes: 17 additions & 0 deletions tests/unittests/BackendTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,23 @@ TEST_P(BackendTest, CompileWithoutConstants) {
auto function = backend->compile(F, opts);
}

/// Test compiling a vector of functions completes without error.
TEST_P(BackendTest, compileVectorOfFunctions) {
Module mod;
std::vector<Function *> functions;
for (unsigned int i = 0; i < 3; i++) {
Function *F = mod.createFunction("function" + std::to_string(i));
auto *X = mod.createPlaceholder(ElemKind::FloatTy, {3},
"X" + std::to_string(i), false);
auto *pow = F->createPow("Pow" + std::to_string(i), X, 2.0);
F->createSave("save" + std::to_string(i), pow);
functions.push_back(F);
}
std::unique_ptr<Backend> backend(createBackend(GetParam()));
CompilationOptions opts;
auto function = backend->compileFunctions(functions, opts);
}

/// This test checks that we can compile a function without depending on the
/// graph representation. We compile some function and then delete the function.
/// Later we execute the code and check that things work.
Expand Down