From f6425cbdddec6e3abbce7a4e3548420e772353e8 Mon Sep 17 00:00:00 2001
From: Diego Caballero <diego.caballero@intel.com>
Date: Tue, 3 Sep 2019 17:43:39 -0700
Subject: [PATCH] [MLIR] Clean up MLIR related CMake files (#3541)

* [MLIR] Move MLIR compiler files to contrib/mlir/compiler

We'll add contrib/mlir/tools directory in subsequent PRs for ngrpah-opt
and other tools.

* [MLIR] Clean up MLIR related CMake files

This PR prepares the introduction of src/contrib/mlir/tools and LIT testing by
refactoring and cleaning up the common part with src/contrib/mlir/compiler:

- Common code for src/contrib/mlir/tools and src/contrib/mlir/compiler is
moved to src/contrib/mlir/CMakeList.txt.
- More generic code, common for src/contrib/mlir/* and LIT configuration
files, is moved to cmake/external_mlir.cmake with pre-existing similar code.
- Redundant environment var LLVM_DIR is removed in favor of LLVM_CMAKE_PATH.
- Redundant cmake messages are removed.

* Replacing LLVM_CMAKE_PATH with LLVM_DIR
---
 cmake/external_mlir.cmake                | 19 ++++++++++++++++---
 src/contrib/mlir/CMakeLists.txt          |  8 ++++++++
 src/contrib/mlir/compiler/CMakeLists.txt | 18 ------------------
 src/ngraph/runtime/cpu/CMakeLists.txt    |  4 +---
 4 files changed, 25 insertions(+), 24 deletions(-)

diff --git a/cmake/external_mlir.cmake b/cmake/external_mlir.cmake
index d6b1549c1..829d5b691 100644
--- a/cmake/external_mlir.cmake
+++ b/cmake/external_mlir.cmake
@@ -47,10 +47,23 @@ execute_process(COMMAND "${CMAKE_COMMAND}" --build . --target ext_mlir_llvm
 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)
+# Enable modules for LLVM.
+set(LLVM_DIR "${MLIR_BUILD_DIR}/lib/cmake/llvm"
+    CACHE PATH "Path to LLVM cmake modules")
+list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
+include(AddLLVM)
+
+# Enable LLVM package, definitions and env vars.
+find_package(LLVM REQUIRED CONFIG)
+add_definitions(${LLVM_DEFINITIONS})
+message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
+message(STATUS "Using modules in: ${LLVM_DIR}")
+message(STATUS "LLVM RTTI is ${LLVM_ENABLE_RTTI}")
 
 set(MLIR_SRC_INCLUDE_PATH ${MLIR_SOURCE_DIR}/include)
 set(MLIR_BIN_INCLUDE_PATH ${MLIR_BUILD_DIR}/projects/mlir/include)
-set(MLIR_INCLUDE_PATHS  ${MLIR_SRC_INCLUDE_PATH};${MLIR_BIN_INCLUDE_PATH})
+set(MLIR_INCLUDE_PATHS ${MLIR_SRC_INCLUDE_PATH};${MLIR_BIN_INCLUDE_PATH})
+set(MLIR_LLVM_INCLUDE_PATH ${LLVM_INCLUDE_DIRS})
 
+message(STATUS "MLIR headers at: ${MLIR_INCLUDE_PATHS}")
+message(STATUS "LLVM headers at: ${MLIR_LLVM_INCLUDE_PATH}")
diff --git a/src/contrib/mlir/CMakeLists.txt b/src/contrib/mlir/CMakeLists.txt
index 7622d3520..3c0fa0ba3 100644
--- a/src/contrib/mlir/CMakeLists.txt
+++ b/src/contrib/mlir/CMakeLists.txt
@@ -14,4 +14,12 @@
 # limitations under the License.
 # ******************************************************************************
 
+# Common MLIR/LLVM setup for the sub-directories below
+
+include_directories(
+    ${NGRAPH_INCLUDE_PATH}
+    ${MLIR_LLVM_INCLUDE_PATH}
+    ${MLIR_INCLUDE_PATHS}
+)
+
 add_subdirectory(compiler)
diff --git a/src/contrib/mlir/compiler/CMakeLists.txt b/src/contrib/mlir/compiler/CMakeLists.txt
index ed92cb0f3..8a79d3dcf 100644
--- a/src/contrib/mlir/compiler/CMakeLists.txt
+++ b/src/contrib/mlir/compiler/CMakeLists.txt
@@ -27,24 +27,6 @@ set(SRC
 
 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_PATHS}")
-message(STATUS "LLVM Headers at : ${MLIR_LLVM_INCLUDEPATH}")
-
-target_include_directories(mlir_backend PRIVATE ${MLIR_INCLUDE_PATHS}) 
-
 llvm_map_components_to_libnames(llvm_libs support core irreader)
 
 # Link MLIR libs
diff --git a/src/ngraph/runtime/cpu/CMakeLists.txt b/src/ngraph/runtime/cpu/CMakeLists.txt
index 5f8680515..a8f013084 100644
--- a/src/ngraph/runtime/cpu/CMakeLists.txt
+++ b/src/ngraph/runtime/cpu/CMakeLists.txt
@@ -233,10 +233,8 @@ if (NGRAPH_CPU_ENABLE)
                                DEFINITION MLIR_LLVM_INCLUDEPATH)
         
         message(STATUS "Building CPU backend with MLIR")
-        message(STATUS "MLIR INCLUDE DIRS: ${MLIR_INCLUDE_PATHS}")
-        message(STATUS "LLVM INCLUDE DIRS: ${MLIR_LLVM_INCLUDEPATH}")
         add_dependencies(cpu_backend mlir_backend)
-        target_include_directories(cpu_backend PUBLIC ${MLIR_INCLUDE_PATHS} ${MLIR_LLVM_INCLUDEPATH})
+        target_include_directories(cpu_backend PUBLIC ${MLIR_INCLUDE_PATHS} ${MLIR_LLVM_INCLUDE_PATH})
         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.
-- 
2.18.0