Commit d9dd03ce authored by Nagy Mostafa's avatar Nagy Mostafa Committed by nmostafa

[MLIR] Move mlir code under runtime/mlir

* Create MLIR as cmake external project. Clone and build via ngraph cmake

* Moved code and enabled compilation. Need to clone and build MLIR/LLVM during cmake step, so find_package can work

* clone and build llvm/mlir during configuration. Compiles now. Needs more testing

* Force DEX only if MLIR is ON

* Remove extra cmake file. Style-apply

* Remove redundant files in cpu/mlir

* Update CODEOWNERS. Check for ninja and fail if not found

* Fixing post merge issues
parent e941412e
......@@ -162,6 +162,7 @@ ngraph_var(NGRAPH_USE_PREBUILT_LLVM DEFAULT "FALSE")
option(NGRAPH_UNIT_TEST_ENABLE "Control the building of unit tests" TRUE)
option(NGRAPH_TOOLS_ENABLE "Control the building of tool" TRUE)
option(NGRAPH_CPU_ENABLE "Control the building of the CPU backend" TRUE)
option(NGRAPH_MLIR_ENABLE "Control the building of MLIR backend" FALSE)
option(NGRAPH_INTELGPU_ENABLE "Control the building of the Intel GPU backend with clDNN" FALSE)
option(NGRAPH_GPU_ENABLE "Control the building of the GPU backend" FALSE)
option(NGRAPH_INTERPRETER_ENABLE "Control the building of the INTERPRETER backend" TRUE)
......@@ -189,6 +190,12 @@ if (NGRAPH_CPU_ENABLE
set(NGRAPH_INTEL_CPU_ONLY_ENABLE ON)
endif()
if (NGRAPH_MLIR_ENABLE)
#disable code-gen due to sym collision with LLVM
message(STATUS "Forcing NGRAPH_DEX_ONLY to ON")
set(NGRAPH_DEX_ONLY ON)
endif()
if (NGRAPH_DISTRIBUTED_ENABLE)
if ("${NGRAPH_DISTRIBUTED_ENABLE}" STREQUAL "MLSL")
if (NGRAPH_INTEL_CPU_ONLY_ENABLE)
......@@ -231,6 +238,7 @@ endmacro()
NORMALIZE_BOOL(NGRAPH_UNIT_TEST_ENABLE)
NORMALIZE_BOOL(NGRAPH_TOOLS_ENABLE)
NORMALIZE_BOOL(NGRAPH_CPU_ENABLE)
NORMALIZE_BOOL(NGRAPH_MLIR_ENABLE)
NORMALIZE_BOOL(NGRAPH_INTELGPU_ENABLE)
NORMALIZE_BOOL(NGRAPH_GPU_ENABLE)
NORMALIZE_BOOL(NGRAPH_INTERPRETER_ENABLE)
......@@ -252,6 +260,7 @@ NORMALIZE_BOOL(NGRAPH_JSON_ENABLE)
message(STATUS "NGRAPH_UNIT_TEST_ENABLE: ${NGRAPH_UNIT_TEST_ENABLE}")
message(STATUS "NGRAPH_TOOLS_ENABLE: ${NGRAPH_TOOLS_ENABLE}")
message(STATUS "NGRAPH_CPU_ENABLE: ${NGRAPH_CPU_ENABLE}")
message(STATUS "NGRAPH_MLIR_ENABLE: ${NGRAPH_MLIR_ENABLE}")
message(STATUS "NGRAPH_INTELGPU_ENABLE: ${NGRAPH_INTELGPU_ENABLE}")
message(STATUS "NGRAPH_GPU_ENABLE: ${NGRAPH_GPU_ENABLE}")
message(STATUS "NGRAPH_INTERPRETER_ENABLE: ${NGRAPH_INTERPRETER_ENABLE}")
......@@ -373,6 +382,10 @@ if (NGRAPH_CPU_ENABLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_CPU_ENABLE")
endif()
if (NGRAPH_MLIR_ENABLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_MLIR_ENABLE")
endif()
if (NGRAPH_PLAIDML_ENABLE)
find_package(PlaidML CONFIG)
if (NOT PLAIDML_FOUND)
......@@ -495,6 +508,9 @@ endif()
if(NGRAPH_CPU_ENABLE)
include(cmake/external_mkldnn.cmake)
endif()
if (NGRAPH_MLIR_ENABLE)
include(cmake/external_mlir.cmake)
endif()
if(NGRAPH_INTELGPU_ENABLE)
include(cmake/external_cldnn.cmake)
endif()
......
......@@ -42,6 +42,7 @@
/src/ngraph/pattern/ @jbobba @aprocter
/src/ngraph/runtime/ @rkimballn1 @jbobba
/src/ngraph/runtime/cpu/ @jbobba
/src/ngraph/runtime/mlir/ @nmostafa @dcaballe @jbobba
/src/ngraph/runtime/cpu/builder/allreduce.*pp @wenzhe-nrv @jbobba @avijit-nervana
/src/ngraph/runtime/dynamic/ @aprocter
/src/ngraph/runtime/gpu/ @rkimballn1
......
# ******************************************************************************
# Copyright 2017-2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ******************************************************************************
include(ExternalProject)
set(MLIR_LLVM_REPO_URL https://github.com/llvm/llvm-project.git)
set(MLIR_REPO_URL https://github.com/tensorflow/mlir.git)
# Change these commit IDs to move to latest stable versions
set(MLIR_LLVM_COMMIT_ID 43d110b)
set(MLIR_COMMIT_ID 1fbf407)
set(MLIR_PROJECT_ROOT ${CMAKE_CURRENT_BINARY_DIR}/mlir_project)
set(MLIR_LLVM_ROOT ${MLIR_PROJECT_ROOT}/llvm-projects)
set(MLIR_SOURCE_DIR ${MLIR_LLVM_ROOT}/llvm/projects/mlir)
set(MLIR_BUILD_DIR ${MLIR_LLVM_ROOT}/build)
# MLIR has to be pre-built before ngraph build starts
# this will clone and build MLIR during cmake config instead
find_program(NINJA ninja)
if (NINJA)
configure_file(${CMAKE_SOURCE_DIR}/cmake/mlir_fetch.cmake.in ${MLIR_PROJECT_ROOT}/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${MLIR_PROJECT_ROOT}")
# clone and build llvm
execute_process(COMMAND "${CMAKE_COMMAND}" --build . --target ext_mlir_llvm
WORKING_DIRECTORY "${MLIR_PROJECT_ROOT}")
# clone and build mlir
execute_process(COMMAND "${CMAKE_COMMAND}" --build . --target ext_mlir
WORKING_DIRECTORY "${MLIR_PROJECT_ROOT}")
# point find_package to the pre-built libs
set(LLVM_DIR ${MLIR_LLVM_ROOT}/build/lib/cmake/llvm)
set(MLIR_INCLUDE_PATH ${MLIR_SOURCE_DIR}/include)
set(MLIR_INCLUDE_PATH ${MLIR_INCLUDE_PATH};${MLIR_BUILD_DIR}/projects/mlir/include)
else()
message(FATAL_ERROR "Cannot find ninja. Cannot build with NGRAPH_MLIR_ENABLE=ON")
endif()
# ******************************************************************************
# Copyright 2017-2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ******************************************************************************
cmake_minimum_required(VERSION 3.1)
include(ExternalProject)
project(mlir-fetch NONE)
ExternalProject_Add(
ext_mlir_llvm
PREFIX mlir_llvm
GIT_REPOSITORY ${MLIR_LLVM_REPO_URL}
GIT_TAG ${MLIR_LLVM_COMMIT_ID}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
UPDATE_COMMAND ""
SOURCE_DIR ${MLIR_LLVM_ROOT}
DOWNLOAD_NO_PROGRESS TRUE
EXCLUDE_FROM_ALL TRUE
)
ExternalProject_Add(
ext_mlir
PREFIX mlir
DEPENDS ${MLIR_DEPENDS}
GIT_REPOSITORY ${MLIR_REPO_URL}
GIT_TAG ${MLIR_COMMIT_ID}
CONFIGURE_COMMAND ""
CMAKE_GENERATOR ${CMAKE_GENERATOR}
CMAKE_GENERATOR_PLATFORM ${CMAKE_GENERATOR_PLATFORM}
CMAKE_GENERATOR_TOOLSET ${CMAKE_GENERATOR_TOOLSET}
BUILD_COMMAND ${CMAKE_COMMAND} -G Ninja ../llvm -DLLVM_BUILD_EXAMPLES=OFF -DLLVM_ENABLE_CXX1Y=Y -DLLVM_TARGETS_TO_BUILD=host -DLLVM_ENABLE_RTTI=ON -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
COMMAND ${CMAKE_COMMAND} --build . --target check-mlir
INSTALL_COMMAND ""
UPDATE_COMMAND ""
SOURCE_DIR ${MLIR_SOURCE_DIR}
BINARY_DIR ${MLIR_BUILD_DIR}
STAMP_DIR "${MLIR_PROJECT_ROOT}/mlir/stamp"
DOWNLOAD_NO_PROGRESS TRUE
EXCLUDE_FROM_ALL TRUE
)
......@@ -16,6 +16,11 @@
add_subdirectory(interpreter)
# This must be added before any backend that uses MLIR
if (NGRAPH_MLIR_ENABLE)
add_subdirectory(mlir)
endif()
# With CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS, when creating cpu_backend.dll, link reports error: library limit of 65535 objects exceeded
if (NGRAPH_CPU_ENABLE)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS FALSE)
......
......@@ -16,14 +16,6 @@
include(FindOpenMP)
set(MLIR_SRC
mlir/dialect/dialect.cpp
mlir/dialect/type.cpp
mlir/dialect/ops.cpp
mlir/compiler.cpp
mlir/lowerer.cpp
mlir/memory_manager.cpp
)
set(SRC
cpu_backend.cpp
cpu_builder.cpp
......@@ -131,7 +123,6 @@ set(SRC
pass/cpu_rnn_fusion.cpp
pass/cpu_workspace_insertion.cpp
ngraph_version.cpp
${MLIR_SRC}
)
if (NOT NGRAPH_DEX_ONLY)
......@@ -215,65 +206,23 @@ if (NGRAPH_CPU_ENABLE)
set_property(TARGET cpu_backend APPEND PROPERTY LINK_FLAGS "-Wl,-Bsymbolic-functions -Wl,--exclude-libs=ALL")
endif()
# Link LLVM and MLIR
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "LLVM RTTI is ${LLVM_ENABLE_RTTI}")
add_definitions(${LLVM_DEFINITIONS})
target_include_directories(cpu_backend PRIVATE ${LLVM_INCLUDE_DIRS})
if(DEFINED ENV{MLIR_INCLUDE_PATH})
string(REPLACE ":" ";" MLIR_INCLUDE_PATH $ENV{MLIR_INCLUDE_PATH})
message(STATUS "MLIR Headers at : ${MLIR_INCLUDE_PATH}")
target_include_directories(cpu_backend PRIVATE ${MLIR_INCLUDE_PATH})
if (NGRAPH_MLIR_ENABLE)
# TODO: can we get away without LLVM/MLIR include path.
# Currently mlir backend compiler.hpp include LLVM/MLIR files
get_directory_property(MLIR_LLVM_INCLUDEPATH
DIRECTORY ${CMAKE_SOURCE_DIR}/src/ngraph/runtime/mlir
DEFINITION MLIR_LLVM_INCLUDEPATH)
message(STATUS "Building CPU backend with MLIR")
message(STATUS "MLIR INCLUDE DIRS: ${MLIR_INCLUDE_PATH}")
message(STATUS "LLVM INCLUDE DIRS: ${MLIR_LLVM_INCLUDEPATH}")
add_dependencies(cpu_backend mlir_backend)
target_include_directories(cpu_backend PUBLIC ${MLIR_INCLUDE_PATH} ${MLIR_LLVM_INCLUDEPATH})
target_link_libraries(cpu_backend PUBLIC mlir_backend)
# TODO: Get rid of the compile time def, and move all MLIR code to separate src files
# and add them to cpu_backend here instead.
target_compile_definitions(cpu_backend PRIVATE "NGRAPH_MLIR_ENABLE")
endif()
llvm_map_components_to_libnames(llvm_libs support core irreader)
# Link MLIR libs
target_link_libraries(
cpu_backend PUBLIC
MLIRAnalysis
MLIREDSC
MLIRExecutionEngine
MLIRIR
MLIRLLVMIR
MLIRParser
MLIRPass
MLIRTargetLLVMIR
MLIRTransforms
MLIRSupport
)
# some libs need whole archive linkage because of Globals static initialization
# TODO: move this helper somewhere else.
function(whole_archive_link target)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(link_flags "-Llib -Wl,-all_load ")
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "${LIB}")
ENDFOREACH(LIB)
else()
set(link_flags "-Llib -Wl,--whole-archive,")
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "${LIB},")
ENDFOREACH(LIB)
string(CONCAT link_flags ${link_flags} "--no-whole-archive")
endif()
message(STATUS "MLIR Ops link flag: ${link_flags}" )
set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_flags})
endfunction(whole_archive_link)
whole_archive_link(cpu_backend
${LLVM_BUILD_LIBRARY_DIR}/libMLIRAffineOps.a
${LLVM_BUILD_LIBRARY_DIR}/libMLIRStandardOps.a
)
# Link LLVM libs
target_link_libraries(
cpu_backend PRIVATE
${llvm_libs}
)
install(TARGETS cpu_backend DESTINATION ${NGRAPH_INSTALL_LIB})
endif()
......@@ -21,7 +21,6 @@
#include "ngraph/runtime/cpu/kernel/add.hpp"
#include "ngraph/runtime/cpu/mkldnn_invoke.hpp"
#include "ngraph/runtime/cpu/mkldnn_utils.hpp"
#include "ngraph/runtime/cpu/mlir/compiler.hpp"
using namespace std;
using namespace ngraph;
......
......@@ -109,6 +109,10 @@
#include "ngraph/type/element_type.hpp"
#include "ngraph/util.hpp"
#ifdef NGRAPH_MLIR_ENABLE
#include "ngraph/runtime/mlir/compiler.hpp"
#endif
using namespace std;
using namespace ngraph;
......@@ -504,6 +508,10 @@ namespace ngraph
}
}
// TODO:
// Get rid of the #ifdefs by moving MLIR hooks to separate files in cpu backend
// we can then instead compile them conditionally based on NGRAPH_MLIR_ENABLE cmake flag
#ifdef NGRAPH_MLIR_ENABLE
using namespace ngraph::runtime::cpu;
CPUKernelFunctor Builder::build_mlir_single_output_binary_op(const ngraph::Node* node,
......@@ -528,3 +536,4 @@ CPUKernelFunctor Builder::build_mlir_single_output_binary_op(const ngraph::Node*
return functor;
}
#endif
\ No newline at end of file
......@@ -191,7 +191,10 @@
#include "ngraph/runtime/cpu/pass/cpu_rnn_fusion.hpp"
#include "ngraph/runtime/cpu/pass/cpu_workspace_insertion.hpp"
#include "ngraph/runtime/cpu/pass/halide_subgraph_extraction.hpp"
#include "ngraph/runtime/cpu/mlir/compiler.hpp"
#ifdef NGRAPH_MLIR_ENABLE
#include "ngraph/runtime/mlir/compiler.hpp"
#endif
using namespace std;
using namespace ngraph;
......@@ -1393,11 +1396,13 @@ 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
MLIRCompiler::init_mlir();
}
#endif
for (shared_ptr<Node> node : m_function->get_ordered_ops())
{
......
# ******************************************************************************
# Copyright 2017-2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ******************************************************************************
set(SRC
dialect/dialect.cpp
dialect/type.cpp
dialect/ops.cpp
compiler.cpp
lowerer.cpp
memory_manager.cpp
)
if (NGRAPH_MLIR_ENABLE)
add_library(mlir_backend SHARED ${SRC})
message(STATUS "LLVM Directory: ${LLVM_DIR}")
# Link LLVM and MLIR
find_package(LLVM REQUIRED CONFIG)
set(MLIR_LLVM_INCLUDEPATH ${LLVM_INCLUDE_DIRS})
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "LLVM RTTI is ${LLVM_ENABLE_RTTI}")
add_definitions(${LLVM_DEFINITIONS})
target_include_directories(mlir_backend PRIVATE ${LLVM_INCLUDE_DIRS})
message(STATUS "MLIR Headers at : ${MLIR_INCLUDE_PATH}")
message(STATUS "LLVM Headers at : ${MLIR_LLVM_INCLUDEPATH}")
target_include_directories(mlir_backend PRIVATE ${MLIR_INCLUDE_PATH})
llvm_map_components_to_libnames(llvm_libs support core irreader)
# Link MLIR libs
target_link_libraries(
mlir_backend PRIVATE
MLIRAnalysis
MLIREDSC
MLIRExecutionEngine
MLIRIR
MLIRLLVMIR
MLIRParser
MLIRPass
MLIRTargetLLVMIR
MLIRTransforms
MLIRSupport
)
# some libs need whole archive linkage because of Globals static initialization
function(whole_archive_link target)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(link_flags "-Llib -Wl,-all_load ")
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "${LIB}")
ENDFOREACH(LIB)
else()
set(link_flags "-Llib -Wl,--whole-archive,")
FOREACH(LIB ${ARGN})
string(CONCAT link_flags ${link_flags} "${LIB},")
ENDFOREACH(LIB)
string(CONCAT link_flags ${link_flags} "--no-whole-archive")
endif()
message(STATUS "MLIR Ops link flag: ${link_flags}" )
set_target_properties(${target} PROPERTIES LINK_FLAGS ${link_flags})
endfunction(whole_archive_link)
whole_archive_link(mlir_backend
${LLVM_BUILD_LIBRARY_DIR}/libMLIRAffineOps.a
${LLVM_BUILD_LIBRARY_DIR}/libMLIRStandardOps.a
)
# Link LLVM libs
target_link_libraries(
mlir_backend PRIVATE
${llvm_libs}
)
# Link ngraph
target_link_libraries(mlir_backend PUBLIC ngraph)
endif()
......@@ -19,11 +19,11 @@
#include "ngraph/graph_util.hpp"
#include "ngraph/node_vector.hpp"
#include "ngraph/op/add.hpp"
#include "ngraph/runtime/cpu/mlir/dialect/dialect.hpp"
#include "ngraph/runtime/cpu/mlir/dialect/ops.hpp"
#include "ngraph/runtime/cpu/mlir/dialect/type.hpp"
#include "ngraph/runtime/cpu/mlir/lowerer.hpp"
#include "ngraph/runtime/cpu/op/matmul_bias.hpp"
#include "ngraph/runtime/mlir/dialect/dialect.hpp"
#include "ngraph/runtime/mlir/dialect/ops.hpp"
#include "ngraph/runtime/mlir/dialect/type.hpp"
#include "ngraph/runtime/mlir/lowerer.hpp"
#include "ngraph/type/element_type.hpp"
#include <llvm/ADT/STLExtras.h>
......
......@@ -25,8 +25,8 @@
#include "mlir/IR/StandardTypes.h"
#include "mlir/Transforms/DialectConversion.h"
#include "ngraph/assertion.hpp"
#include "ngraph/runtime/cpu/mlir/dialect/ops.hpp"
#include "ngraph/runtime/cpu/mlir/dialect/type.hpp"
#include "ngraph/runtime/mlir/dialect/ops.hpp"
#include "ngraph/runtime/mlir/dialect/type.hpp"
using namespace ngraph::runtime::cpu;
// anonymous namespace
......
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