[polly] r301558 - [CMake] Use object library to build the two flavours of Polly.

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 27 09:13:04 PDT 2017


Author: meinersbur
Date: Thu Apr 27 11:13:03 2017
New Revision: 301558

URL: http://llvm.org/viewvc/llvm-project?rev=301558&view=rev
Log:
[CMake] Use object library to build the two flavours of Polly.

Polly comes in two library flavors: One loadable module to use the
LLVM framework -load mechanism, and another one that host applications
can link to. These have very different requirements for Polly's
own dependencies.

The loadable module assumes that all its LLVM dependencies are already
available in the address space of the host application, and is not allowed
to bring in its own copy of any LLVM library (including the NVPTX
backend in case of Polly-ACC).

The non-module library is intended to be linked to using
target_link_libraries. CMake would then resolve all of its dependencies,
including NVPTX and ensure that only a single instance of each library
will be used.

Differential Revision: https://reviews.llvm.org/D32442

Modified:
    polly/trunk/CMakeLists.txt
    polly/trunk/lib/CMakeLists.txt
    polly/trunk/unittests/CMakeLists.txt

Modified: polly/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/CMakeLists.txt?rev=301558&r1=301557&r2=301558&view=diff
==============================================================================
--- polly/trunk/CMakeLists.txt (original)
+++ polly/trunk/CMakeLists.txt Thu Apr 27 11:13:03 2017
@@ -104,7 +104,7 @@ if (NOT DEFINED LLVM_MAIN_SRC_DIR)
       "${UNITTEST_DIR}/googletest"
       "${UNITTEST_DIR}/googlemock"
     )
-    target_link_libraries(gtest ${LLVM_SYSTEM_LIBS})
+    target_link_libraries(gtest -lpthread)
 
     add_library(gtest_main ${UNITTEST_DIR}/UnitTestMain/TestMain.cpp)
     target_link_libraries(gtest_main gtest)

Modified: polly/trunk/lib/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CMakeLists.txt?rev=301558&r1=301557&r2=301558&view=diff
==============================================================================
--- polly/trunk/lib/CMakeLists.txt (original)
+++ polly/trunk/lib/CMakeLists.txt Thu Apr 27 11:13:03 2017
@@ -26,7 +26,9 @@ if (MSVC_IDE OR XCODE)
   file(GLOB_RECURSE POLLY_HEADER_FILES "${POLLY_SOURCE_DIR}/include/polly/*.h")
 endif ()
 
-add_polly_library(Polly
+# Use an object-library to add the same files to multiple libs without requiring
+# the sources them to be recompiled for each of them.
+add_library(PollyCore OBJECT
   Analysis/DependenceInfo.cpp
   Analysis/PolyhedralInfo.cpp
   Analysis/ScopDetection.cpp
@@ -66,44 +68,76 @@ add_polly_library(Polly
   ${POLLY_HEADER_FILES}
   )
 
+# Create the library that can be linked into LLVM's tools and Polly's unittests.
+# It depends on all library it needs, such that with
+# LLVM_POLLY_LINK_INTO_TOOLS=ON, its dependencies like PollyISL are linked as
+# well.
+add_polly_library(Polly $<TARGET_OBJECTS:PollyCore>)
+target_link_libraries(Polly
+  ${ISL_TARGET}
+)
+
+# Additional dependencies for Polly-ACC.
 if (GPU_CODEGEN)
   target_link_libraries(Polly PollyPPCG)
-endif (GPU_CODEGEN)
-
-target_link_libraries(Polly ${ISL_TARGET})
+endif ()
 
-if (BUILD_SHARED_LIBS)
-  target_link_libraries(Polly
-    LLVMSupport
-    LLVMCore
-    LLVMScalarOpts
-    LLVMInstCombine
-    LLVMTransformUtils
-    LLVMAnalysis
-    LLVMipo
-    LLVMMC
+# Add Polly's LLVM dependencies.
+# When built inside the LLVM source tree, these are CMake targets that will
+# depend on their dependencies and CMake ensures they are added in the right
+# order.
+# If Polly is built independently, just add all LLVM libraries. LLVM_ROOT_DIR
+# might have been configured to compile to individual libraries or a single
+# libLLVM.so (called dylib), reported by llvm-config, so there is no fixed list
+# of required libraries.
+if (DEFINED LLVM_MAIN_SRC_DIR)
+
+  # Polly-ACC requires the NVPTX backend to work. Ask LLVM about its libraries.
+  set(nvptx_libs)
+  if (GPU_CODEGEN)
+    # This call emits an error if they NVPTX backend is not enable.
+    llvm_map_components_to_libnames(nvptx_libs NVPTX)
+  endif ()
+
+  if (LLVM_LINK_LLVM_DYLIB)
+    # The shlib/dylib contains all the LLVM components
+    # (including NVPTX is enabled) already. Adding them to target_link_libraries
+    # would cause them being twice in the address space
+    # (their LLVM*.a/so and their copies in libLLVM.so)
+    # which results in errors when the two instances try to register the same
+    # command-line switches.
+    target_link_libraries(Polly LLVM)
+  else ()
+    target_link_libraries(Polly
+      LLVMSupport
+      LLVMCore
+      LLVMScalarOpts
+      LLVMInstCombine
+      LLVMTransformUtils
+      LLVMAnalysis
+      LLVMipo
+      LLVMMC
+      ${nvptx_libs}
 # The libraries below are required for darwin: http://PR26392
-    LLVMBitReader
-    LLVMMCParser
-    LLVMObject
-    LLVMProfileData
-    LLVMTarget
-    LLVMVectorize
-  )
-  link_directories(
-    ${LLVM_LIBRARY_DIR}
-  )
-elseif (LLVM_LINK_LLVM_DYLIB)
+      LLVMBitReader
+      LLVMMCParser
+      LLVMObject
+      LLVMProfileData
+      LLVMTarget
+      LLVMVectorize
+    )
+  endif ()
+else ()
+  # When Polly-ACC is enabled, we assume that the host LLVM was also built with
+  # the NVPTX target enabled.
   target_link_libraries(Polly
-    LLVM
+    ${LLVM_LIBS}
+    ${LLVM_SYSTEM_LIBS}
   )
-  link_directories(
-    ${LLVM_LIBRARY_DIR}
-  )
-endif()
+endif ()
 
-# Build a monolithic Polly.a and a thin module LLVMPolly.moduleext that links to
-# that static library.
+# Create a loadable module Polly.so that can be loaded using
+# LLVM's/clang's "-load" option.
 if (MSVC)
   # Add dummy target, because loadable modules are not supported on Windows
   add_custom_target(LLVMPolly)
@@ -111,9 +145,21 @@ if (MSVC)
 else ()
   add_polly_loadable_module(LLVMPolly
     Polly.cpp
+    $<TARGET_OBJECTS:PollyCore>
   )
 
-  target_link_libraries(LLVMPolly Polly)
+  # Only add the dependencies that are not part of LLVM. The latter are assumed
+  # to be already available in the address space the module is loaded into.
+  # Adding them once more would have the effect that both copies try to register
+  # the same command line options, to which LLVM reacts with an error.
+  # If Polly-ACC is enabled, the NVPTX target is also expected to reside in the
+  # hosts. This is not the case for bugpoint. Use LLVM_POLLY_LINK_INTO_TOOLS=ON
+  # instead which will automatically resolve the additional dependencies by
+  # Polly.
+  target_link_libraries(LLVMPolly ${ISL_TARGET})
+  if (GPU_CODEGEN)
+    target_link_libraries(LLVMPolly PollyPPCG)
+  endif ()
 
   set_target_properties(LLVMPolly
     PROPERTIES

Modified: polly/trunk/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/unittests/CMakeLists.txt?rev=301558&r1=301557&r2=301558&view=diff
==============================================================================
--- polly/trunk/unittests/CMakeLists.txt (original)
+++ polly/trunk/unittests/CMakeLists.txt Thu Apr 27 11:13:03 2017
@@ -7,27 +7,16 @@ set_target_properties(PollyUnitTests PRO
 function(add_polly_unittest test_name)
   if(COMMAND add_unittest)
     add_unittest(PollyUnitTests ${test_name} ${ARGN})
-    target_link_libraries(${test_name} Polly)
-
-    # The Polly target does not depend on its required libraries, except:
-    # - BUILD_SHARED_LIBS=ON
-    #     in which case calling target_link_libraries again is redundant.
-    # - LLVM_LINK_LLVM_DYLIB=ON
-    #     in which case it depends on libLLVM.so, so no additional libs needed.
-    #     We are also not allowed to link against the plain LLVM* libraries,
-    #     which would result in multiple instances of these to be loaded.
-    if (NOT LLVM_LINK_LLVM_DYLIB)
-      target_link_libraries(${test_name} LLVMCore LLVMSupport LLVMDemangle LLVMipo)
-    endif ()
   else()
     add_executable(${test_name} EXCLUDE_FROM_ALL ${ARGN})
     set_target_properties(${test_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
 
-    target_link_libraries(${test_name} gtest_main gtest Polly ${LLVM_LIBS})
+    target_link_libraries(${test_name} gtest_main gtest)
     add_dependencies(PollyUnitTests ${test_name})
 
     set_property(TARGET ${test_name} PROPERTY FOLDER "Polly")
   endif()
+  target_link_libraries(${test_name} Polly)
 endfunction()
 
 add_subdirectory(Isl)




More information about the llvm-commits mailing list