Skip to content
Permalink
Browse files

Added new compileFunctions method which compiles multiple functions a…

…t once

Updated backends.md
  • Loading branch information...
gcatron committed Mar 13, 2019
1 parent 9d9d04f commit 685551aec412ae8bd4adc0b276d28d380f5d3f8f
Showing with 47 additions and 8 deletions.
  1. +17 −8 docs/Backends.md
  2. +13 −0 include/glow/Backends/Backend.h
  3. +17 −0 tests/unittests/BackendTest.cpp
@@ -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;`

@@ -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
@@ -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);
@@ -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.

0 comments on commit 685551a

Please sign in to comment.
You can’t perform that action at this time.