diff --git a/docs/Backends.md b/docs/Backends.md index c57a401224..8d76c2dd25 100644 --- a/docs/Backends.md +++ b/docs/Backends.md @@ -22,16 +22,18 @@ are two pure virtual functions all backends must implement: - `virtual std::unique_ptr 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 compileWithoutConstants(Function *F) const;` +- `virtual std::unique_ptr 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> compileFunctions(llvm::ArrayRef 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 diff --git a/include/glow/Backends/Backend.h b/include/glow/Backends/Backend.h index bbc8f4de49..737763fb3f 100644 --- a/include/glow/Backends/Backend.h +++ b/include/glow/Backends/Backend.h @@ -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> + compileFunctions(llvm::ArrayRef functions, + CompilationOptions &opts) const { + std::vector> compiledFunctions; + for (auto &function : functions) { + compiledFunctions.push_back(compile(function, opts)); + } + return compiledFunctions; + } + virtual std::unique_ptr compile(Function *F) const { CompilationOptions opts; return compile(F, opts); diff --git a/tests/unittests/BackendTest.cpp b/tests/unittests/BackendTest.cpp index f4e344405e..388a7b944b 100644 --- a/tests/unittests/BackendTest.cpp +++ b/tests/unittests/BackendTest.cpp @@ -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 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(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.