[PATCH] D76047: [MLIR] Add support for out of tree external projects using MLIR

Stephen Neuendorffer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 11 23:18:07 PDT 2020


stephenneuendorffer created this revision.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, aartbik, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini, mgorny.
Herald added a project: LLVM.
stephenneuendorffer added reviewers: mehdi_amini, vchuravy.

LLVM has a documented mechanism for passing configuration information
to an out of tree project using cmake.  See
https://llvm.org/docs/CMake.html#embedding-llvm-in-your-project.  This
patch adds similar support for MLIR.

Using this requires something like:

cmake_minimum_required(VERSION 3.4.3)
project(SimpleProject)

find_package(MLIR REQUIRED CONFIG)

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
link_directories(${LLVM_BUILD_LIBRARY_DIR})
add_definitions(${LLVM_DEFINITIONS})

set(CMAKE_MODULE_PATH ${LLVM_BINARY_DIR}/lib/cmake/llvm)
include(AddLLVM)

add_executable(test-opt test-opt.cpp)
llvm_update_compile_flags(test-opt)

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
message(dialects=${dialect_libs})
set(LIBS

  ${dialect_libs}
  ${conversion_libs}
  MLIRLoopOpsTransforms
  MLIRLoopAnalysis
  MLIRAnalysis
  MLIRDialect
  MLIREDSC
  MLIROptLib
  MLIRParser
  MLIRPass
  MLIRQuantizerFxpMathConfig
  MLIRQuantizerSupport
  MLIRQuantizerTransforms
  MLIRSPIRV
  MLIRSPIRVTestPasses
  MLIRSPIRVTransforms
  MLIRTransforms
  MLIRTransformUtils
  MLIRTestDialect
  MLIRTestIR
  MLIRTestPass
  MLIRTestTransforms
  MLIRSupport
  MLIRIR
  MLIROptLib
  LLVMSupport
  LLVMCore
  LLVMAsmParser
  )

target_link_libraries(test-opt ${LIBS})


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76047

Files:
  mlir/cmake/modules/CMakeLists.txt
  mlir/cmake/modules/MLIRConfig.cmake.in


Index: mlir/cmake/modules/MLIRConfig.cmake.in
===================================================================
--- /dev/null
+++ mlir/cmake/modules/MLIRConfig.cmake.in
@@ -0,0 +1,24 @@
+# This file allows users to call find_package(MLIR) and pick up our targets.
+
+ at MLIR_CONFIG_CODE@
+
+find_package(LLVM REQUIRED CONFIG
+             HINTS "@MLIR_CONFIG_LLVM_CMAKE_DIR@")
+
+set(MLIR_EXPORTED_TARGETS "@MLIR_EXPORTS@")
+set(MLIR_CMAKE_DIR "@MLIR_CONFIG_CMAKE_DIR@")
+set(MLIR_INCLUDE_DIRS "@MLIR_CONFIG_INCLUDE_DIRS@")
+
+set_property(GLOBAL PROPERTY MLIR_ALL_LIBS "@MLIR_ALL_LIBS@")
+set_property(GLOBAL PROPERTY MLIR_DIALECT_LIBS "@MLIR_DIALECT_LIBS@")
+set_property(GLOBAL PROPERTY MLIR_CONVERSION_LIBS "@MLIR_CONVERSION_LIBS@")
+
+# Provide all our library targets to users.
+include("@MLIR_CONFIG_EXPORTS_FILE@")
+
+# By creating mlir-tablegen-targets here, subprojects that depend on MLIR's
+# tablegen-generated headers can always depend on this target whether building
+# in-tree with MLIR or not.
+if(NOT TARGET mlir-tablegen-targets)
+  add_custom_target(mlir-tablegen-targets)
+endif()
Index: mlir/cmake/modules/CMakeLists.txt
===================================================================
--- mlir/cmake/modules/CMakeLists.txt
+++ mlir/cmake/modules/CMakeLists.txt
@@ -1,7 +1,7 @@
 # Generate a list of CMake library targets so that other CMake projects can
 # link against them. LLVM calls its version of this file LLVMExports.cmake, but
 # the usual CMake convention seems to be ${Project}Targets.cmake.
-set(MLIR_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm)
+set(MLIR_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/mlir)
 set(mlir_cmake_builddir "${CMAKE_BINARY_DIR}/${MLIR_INSTALL_PACKAGE_DIR}")
 
 # Keep this in sync with llvm/cmake/CMakeLists.txt!
@@ -9,16 +9,24 @@
 set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}")
 
 get_property(MLIR_EXPORTS GLOBAL PROPERTY MLIR_EXPORTS)
-export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MlirTargets.cmake)
+export(TARGETS ${MLIR_EXPORTS} FILE ${mlir_cmake_builddir}/MLIRTargets.cmake)
+
+get_property(MLIR_ALL_LIBS GLOBAL PROPERTY MLIR_ALL_LIBS)
+get_property(MLIR_DIALECT_LIBS GLOBAL PROPERTY MLIR_DIALECT_LIBS)
+get_property(MLIR_CONVERSION_LIBS GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
 
 # Generate MlirConfig.cmake for the build tree.
 set(MLIR_CONFIG_CMAKE_DIR "${mlir_cmake_builddir}")
 set(MLIR_CONFIG_LLVM_CMAKE_DIR "${llvm_cmake_builddir}")
-set(MLIR_CONFIG_EXPORTS_FILE "${mlir_cmake_builddir}/MlirTargets.cmake")
+set(MLIR_CONFIG_EXPORTS_FILE "${mlir_cmake_builddir}/MLIRTargets.cmake")
 set(MLIR_CONFIG_INCLUDE_DIRS
   "${MLIR_SOURCE_DIR}/include"
   "${MLIR_BINARY_DIR}/include"
   )
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfig.cmake.in
+  ${mlir_cmake_builddir}/MLIRConfig.cmake
+  @ONLY)
 set(MLIR_CONFIG_CMAKE_DIR)
 set(MLIR_CONFIG_LLVM_CMAKE_DIR)
 set(MLIR_CONFIG_EXPORTS_FILE)
@@ -36,10 +44,14 @@
 endforeach(p)
 set(MLIR_CONFIG_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}/${MLIR_INSTALL_PACKAGE_DIR}")
 set(MLIR_CONFIG_LLVM_CMAKE_DIR "\${MLIR_INSTALL_PREFIX}/${LLVM_INSTALL_PACKAGE_DIR}")
-set(MLIR_CONFIG_EXPORTS_FILE "\${MLIR_CMAKE_DIR}/MlirTargets.cmake")
+set(MLIR_CONFIG_EXPORTS_FILE "\${MLIR_CMAKE_DIR}/MLIRTargets.cmake")
 set(MLIR_CONFIG_INCLUDE_DIRS
   "\${MLIR_INSTALL_PREFIX}/include"
   )
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfig.cmake.in
+  ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfig.cmake
+  @ONLY)
 set(MLIR_CONFIG_CODE)
 set(MLIR_CONFIG_CMAKE_DIR)
 set(MLIR_CONFIG_EXPORTS_FILE)
@@ -49,11 +61,12 @@
   # Include the cmake files so other tools can use mlir-tblgen, etc.
   get_property(mlir_has_exports GLOBAL PROPERTY MLIR_HAS_EXPORTS)
   if(mlir_has_exports)
-    install(EXPORT MlirTargets DESTINATION ${MLIR_INSTALL_PACKAGE_DIR}
+    install(EXPORT MLIRTargets DESTINATION ${MLIR_INSTALL_PACKAGE_DIR}
             COMPONENT mlir-cmake-exports)
   endif()
 
-  install(FILES #${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MlirConfig.cmake
+  install(FILES
+    ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfig.cmake
     ${CMAKE_CURRENT_SOURCE_DIR}/AddMLIR.cmake
     DESTINATION ${MLIR_INSTALL_PACKAGE_DIR}
     COMPONENT mlir-cmake-exports)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76047.249841.patch
Type: text/x-patch
Size: 4221 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200312/016b92a7/attachment.bin>


More information about the llvm-commits mailing list