Commit eda52385 authored by Diego Caballero's avatar Diego Caballero Committed by nmostafa

[MLIR] Fix redundant MLIR initialization.

MLIR is now initialize once once in CPU backend.
parent 86bc31cc
......@@ -383,6 +383,7 @@ if (NGRAPH_CPU_ENABLE)
endif()
if (NGRAPH_MLIR_ENABLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_MLIR_ENABLE")
set(NGRAPH_MLIR_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src/contrib/mlir)
endif()
......
......@@ -32,6 +32,7 @@
#include <mlir/Target/LLVMIR.h>
#include <mlir/Transforms/DialectConversion.h>
#include <mlir/Transforms/Passes.h>
#include <mutex>
#include "dialect/dialect.hpp"
#include "dialect/type.hpp"
#include "lowerer.hpp"
......@@ -63,9 +64,19 @@ MLIRCompiler::MLIRCompiler(const ngraph::op::CompiledKernel* compiled_kernel,
void MLIRCompiler::init_mlir()
{
mlir::registerDialect<mlir::NGDialect>();
// Register any LLVM command line options
llvm::cl::ParseEnvironmentOptions("ngraph", "MLIR_LLVM_OPTIONS", "");
// Mutex to safely initialize MLIR.
static std::mutex mlir_init_mutex;
static bool initialized = false;
std::unique_lock<std::mutex> lock(mlir_init_mutex);
if (!initialized)
{
mlir::registerDialect<mlir::NGDialect>();
// Register any LLVM command line options
llvm::cl::ParseEnvironmentOptions("ngraph", "MLIR_LLVM_OPTIONS", "");
initialized = true;
}
}
void MLIRCompiler::compile_and_run()
......
......@@ -25,6 +25,11 @@
#include "ngraph/runtime/cpu/cpu_tensor_view.hpp"
#include "ngraph/util.hpp"
#ifdef NGRAPH_MLIR_ENABLE
#include "contrib/mlir/compiler.hpp"
#endif
using namespace ngraph;
using namespace std;
......@@ -90,6 +95,14 @@ shared_ptr<runtime::Executable>
ngraph::pass::PassConfig& pass_config,
bool performance_counters_enabled)
{
#ifdef NGRAPH_MLIR_ENABLE
if (std::getenv("NGRAPH_MLIR") != nullptr)
{
// Initialize MLIR compiler
ngmlir::MLIRCompiler::init_mlir();
}
#endif
shared_ptr<runtime::Executable> rc;
auto it = m_exec_map.find(func);
if (it != m_exec_map.end())
......
......@@ -193,9 +193,6 @@
#include "ngraph/runtime/cpu/pass/cpu_workspace_insertion.hpp"
#include "ngraph/runtime/cpu/pass/halide_subgraph_extraction.hpp"
#ifdef NGRAPH_MLIR_ENABLE
#include "contrib/mlir/compiler.hpp"
#endif
using namespace std;
using namespace ngraph;
......@@ -1400,14 +1397,6 @@ void runtime::cpu::CPU_ExternalFunction::build(ngraph::pass::PassConfig& pass_co
// After processing inputs, outputs, constants, and intermediates, set the buffer size.
m_buffer_size = buffer_index;
#ifdef NGRAPH_MLIR_ENABLE
if (std::getenv("NGRAPH_MLIR") != nullptr)
{
// Initialize MLIR compiler
ngmlir::MLIRCompiler::init_mlir();
}
#endif
for (shared_ptr<Node> node : m_function->get_ordered_ops())
{
if (node->is_parameter() || node->is_constant())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment