[Mlir-commits] [mlir] 8e628f7 - [mlir] Avoid building some shared libraries when PIC is off

Han Zhu llvmlistbot at llvm.org
Mon Feb 13 10:13:57 PST 2023


Author: Han Zhu
Date: 2023-02-13T10:12:57-08:00
New Revision: 8e628f72b00cd6d8224b1d2aee3266ee49f50973

URL: https://github.com/llvm/llvm-project/commit/8e628f72b00cd6d8224b1d2aee3266ee49f50973
DIFF: https://github.com/llvm/llvm-project/commit/8e628f72b00cd6d8224b1d2aee3266ee49f50973.diff

LOG: [mlir] Avoid building some shared libraries when PIC is off

When `LLVM_ENABLE_PIC = OFF`, shared libraries cannot be built against code
that's compiled without -fPIC. Example error message:
``
ld.lld: error: relocation R_X86_64_32 cannot be used against local symbol;
recompile with -fPIC
>>> defined in lib/libLLVMSupport.a(StringMap.cpp.o)
>>> referenced by StringMap.cpp
>>>               StringMap.cpp.o:(llvm::StringMapImpl::StringMapImpl(unsigned
>>>               int, unsigned int)) in archive lib/libLLVMSupport.a
``
Similar to [how libclang handles
this](https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-shlib/CMakeLists.txt#L2-L4),
skip building these shared libraries when `LLVM_ENABLE_PIC = OFF`.

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

Added: 
    

Modified: 
    mlir/lib/ExecutionEngine/CMakeLists.txt
    mlir/test/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/mlir/lib/ExecutionEngine/CMakeLists.txt b/mlir/lib/ExecutionEngine/CMakeLists.txt
index bedc86fe185b..a212e1ac2048 100644
--- a/mlir/lib/ExecutionEngine/CMakeLists.txt
+++ b/mlir/lib/ExecutionEngine/CMakeLists.txt
@@ -119,162 +119,166 @@ add_mlir_library(MLIRJitRunner
   MLIRSupport
 )
 
-add_mlir_library(mlir_float16_utils
-  SHARED
-  Float16bits.cpp
-
-  EXCLUDE_FROM_LIBMLIR
-  )
-set_property(TARGET mlir_float16_utils PROPERTY CXX_STANDARD 17)
-target_compile_definitions(mlir_float16_utils PRIVATE mlir_float16_utils_EXPORTS)
-
-add_subdirectory(SparseTensor)
-
-add_mlir_library(mlir_c_runner_utils
-  SHARED
-  CRunnerUtils.cpp
-  SparseTensorRuntime.cpp
-
-  EXCLUDE_FROM_LIBMLIR
+# When -fPIC is not provided, shared libraries cannot be built if it links against
+# non-PIC code.
+if(LLVM_ENABLE_PIC)
+  add_mlir_library(mlir_float16_utils
+    SHARED
+    Float16bits.cpp
 
-  LINK_LIBS PUBLIC
-  mlir_float16_utils
-  MLIRSparseTensorEnums
-  MLIRSparseTensorRuntime
-  )
-set_property(TARGET mlir_c_runner_utils PROPERTY CXX_STANDARD 17)
-target_compile_definitions(mlir_c_runner_utils PRIVATE mlir_c_runner_utils_EXPORTS)
+    EXCLUDE_FROM_LIBMLIR
+    )
+  set_property(TARGET mlir_float16_utils PROPERTY CXX_STANDARD 17)
+  target_compile_definitions(mlir_float16_utils PRIVATE mlir_float16_utils_EXPORTS)
 
-add_mlir_library(mlir_runner_utils
-  SHARED
-  RunnerUtils.cpp
+  add_subdirectory(SparseTensor)
 
-  EXCLUDE_FROM_LIBMLIR
-)
-target_compile_definitions(mlir_runner_utils PRIVATE mlir_runner_utils_EXPORTS)
+  add_mlir_library(mlir_c_runner_utils
+    SHARED
+    CRunnerUtils.cpp
+    SparseTensorRuntime.cpp
 
-add_mlir_library(mlir_async_runtime
-  SHARED
-  AsyncRuntime.cpp
+    EXCLUDE_FROM_LIBMLIR
 
-  EXCLUDE_FROM_LIBMLIR
+    LINK_LIBS PUBLIC
+    mlir_float16_utils
+    MLIRSparseTensorEnums
+    MLIRSparseTensorRuntime
+    )
+  set_property(TARGET mlir_c_runner_utils PROPERTY CXX_STANDARD 17)
+  target_compile_definitions(mlir_c_runner_utils PRIVATE mlir_c_runner_utils_EXPORTS)
 
-  LINK_LIBS PUBLIC
-  ${LLVM_PTHREAD_LIB}
-)
-set_property(TARGET mlir_async_runtime PROPERTY CXX_VISIBILITY_PRESET hidden)
-target_compile_definitions(mlir_async_runtime PRIVATE mlir_async_runtime_EXPORTS)
-
-if(MLIR_ENABLE_CUDA_RUNNER)
-  # Configure CUDA support. Using check_language first allows us to give a
-  # custom error message.
-  include(CheckLanguage)
-  check_language(CUDA)
-  if (CMAKE_CUDA_COMPILER)
-    enable_language(CUDA)
-  else()
-    message(SEND_ERROR
-      "Building the mlir cuda runner requires a working CUDA install")
-  endif()
+  add_mlir_library(mlir_runner_utils
+    SHARED
+    RunnerUtils.cpp
 
-  # We need the libcuda.so library.
-  find_library(CUDA_RUNTIME_LIBRARY cuda)
+    EXCLUDE_FROM_LIBMLIR
+  )
+  target_compile_definitions(mlir_runner_utils PRIVATE mlir_runner_utils_EXPORTS)
 
-  add_mlir_library(mlir_cuda_runtime
+  add_mlir_library(mlir_async_runtime
     SHARED
-    CudaRuntimeWrappers.cpp
+    AsyncRuntime.cpp
 
     EXCLUDE_FROM_LIBMLIR
-  )
-  set_property(TARGET mlir_cuda_runtime PROPERTY CXX_STANDARD 14)
-  target_include_directories(mlir_cuda_runtime
-    PRIVATE
-    ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
-  )
-  target_link_libraries(mlir_cuda_runtime
-    PRIVATE
-    ${CUDA_RUNTIME_LIBRARY}
-  )
-endif()
 
-if(MLIR_ENABLE_ROCM_RUNNER)
-  # Configure ROCm support.
-  if (NOT DEFINED ROCM_PATH)
-    if (NOT DEFINED ENV{ROCM_PATH})
-      set(ROCM_PATH "/opt/rocm" CACHE PATH "Path to which ROCm has been installed")
+    LINK_LIBS PUBLIC
+    ${LLVM_PTHREAD_LIB}
+  )
+  set_property(TARGET mlir_async_runtime PROPERTY CXX_VISIBILITY_PRESET hidden)
+  target_compile_definitions(mlir_async_runtime PRIVATE mlir_async_runtime_EXPORTS)
+
+  if(MLIR_ENABLE_CUDA_RUNNER)
+    # Configure CUDA support. Using check_language first allows us to give a
+    # custom error message.
+    include(CheckLanguage)
+    check_language(CUDA)
+    if (CMAKE_CUDA_COMPILER)
+      enable_language(CUDA)
     else()
-      set(ROCM_PATH $ENV{ROCM_PATH} CACHE PATH "Path to which ROCm has been installed")
+      message(SEND_ERROR
+        "Building the mlir cuda runner requires a working CUDA install")
     endif()
+
+    # We need the libcuda.so library.
+    find_library(CUDA_RUNTIME_LIBRARY cuda)
+
+    add_mlir_library(mlir_cuda_runtime
+      SHARED
+      CudaRuntimeWrappers.cpp
+
+      EXCLUDE_FROM_LIBMLIR
+    )
+    set_property(TARGET mlir_cuda_runtime PROPERTY CXX_STANDARD 14)
+    target_include_directories(mlir_cuda_runtime
+      PRIVATE
+      ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
+    )
+    target_link_libraries(mlir_cuda_runtime
+      PRIVATE
+      ${CUDA_RUNTIME_LIBRARY}
+    )
   endif()
-  # A lot of the ROCm CMake files expect to find their own dependencies in
-  # CMAKE_PREFIX_PATH and don't respect PATHS or HINTS :( .
-  # Therefore, temporarily add the ROCm path to CMAKE_PREFIX_PATH so we can
-  # load HIP, then remove it
-  set(REAL_CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}")
-  list(APPEND CMAKE_PREFIX_PATH ${ROCM_PATH} "${ROCM_PATH}/hip")
-  find_package(hip REQUIRED)
-  set(CMAKE_PREFIX_PATH "${REAL_CMAKE_PREFIX_PATH}")
-
-  if (NOT DEFINED ROCM_TEST_CHIPSET)
-    execute_process(COMMAND "${ROCM_PATH}/bin/rocm_agent_enumerator"
-    OUTPUT_VARIABLE AGENTS_STRING
-    ERROR_VARIABLE AGENTS_STRING
-    RESULT_VARIABLE AGENT_ENUMERATOR_RESULT)
-
-    if (NOT AGENT_ENUMERATOR_RESULT EQUAL 0)
-      message(SEND_ERROR "Could not run rocm_agent_enumerator and ROCM_TEST_CHIPSET is not defined")
-      set(AGENTS_STRING "")
+
+  if(MLIR_ENABLE_ROCM_RUNNER)
+    # Configure ROCm support.
+    if (NOT DEFINED ROCM_PATH)
+      if (NOT DEFINED ENV{ROCM_PATH})
+        set(ROCM_PATH "/opt/rocm" CACHE PATH "Path to which ROCm has been installed")
+      else()
+        set(ROCM_PATH $ENV{ROCM_PATH} CACHE PATH "Path to which ROCm has been installed")
+      endif()
     endif()
-    string(STRIP AGENTS_STRING ${AGENTS_STRING})
-    string(REPLACE "\n" ";" AGENTS_LIST ${AGENTS_STRING})
-    list(FILTER AGENTS_LIST EXCLUDE REGEX "gfx000")
-    if (AGENTS_LIST STREQUAL "")
-      message(SEND_ERROR "No non-CPU ROCm agents found on the system, and ROCM_TEST_CHIPSET is not defined")
-    else()
-      list(GET AGENTS_LIST 0 FIRST_AGENT)
-      set(ROCM_TEST_CHIPSET ${FIRST_AGENT} CACHE STRING "Chipset for which to compile ROCm integration tests")
-      message(STATUS "Compiling integration tests for ${ROCM_TEST_CHIPSET}")
+    # A lot of the ROCm CMake files expect to find their own dependencies in
+    # CMAKE_PREFIX_PATH and don't respect PATHS or HINTS :( .
+    # Therefore, temporarily add the ROCm path to CMAKE_PREFIX_PATH so we can
+    # load HIP, then remove it
+    set(REAL_CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}")
+    list(APPEND CMAKE_PREFIX_PATH ${ROCM_PATH} "${ROCM_PATH}/hip")
+    find_package(hip REQUIRED)
+    set(CMAKE_PREFIX_PATH "${REAL_CMAKE_PREFIX_PATH}")
+
+    if (NOT DEFINED ROCM_TEST_CHIPSET)
+      execute_process(COMMAND "${ROCM_PATH}/bin/rocm_agent_enumerator"
+      OUTPUT_VARIABLE AGENTS_STRING
+      ERROR_VARIABLE AGENTS_STRING
+      RESULT_VARIABLE AGENT_ENUMERATOR_RESULT)
+
+      if (NOT AGENT_ENUMERATOR_RESULT EQUAL 0)
+        message(SEND_ERROR "Could not run rocm_agent_enumerator and ROCM_TEST_CHIPSET is not defined")
+        set(AGENTS_STRING "")
+      endif()
+      string(STRIP AGENTS_STRING ${AGENTS_STRING})
+      string(REPLACE "\n" ";" AGENTS_LIST ${AGENTS_STRING})
+      list(FILTER AGENTS_LIST EXCLUDE REGEX "gfx000")
+      if (AGENTS_LIST STREQUAL "")
+        message(SEND_ERROR "No non-CPU ROCm agents found on the system, and ROCM_TEST_CHIPSET is not defined")
+      else()
+        list(GET AGENTS_LIST 0 FIRST_AGENT)
+        set(ROCM_TEST_CHIPSET ${FIRST_AGENT} CACHE STRING "Chipset for which to compile ROCm integration tests")
+        message(STATUS "Compiling integration tests for ${ROCM_TEST_CHIPSET}")
+      endif()
     endif()
-  endif()
 
-  add_mlir_library(mlir_rocm_runtime
-    SHARED
-    RocmRuntimeWrappers.cpp
+    add_mlir_library(mlir_rocm_runtime
+      SHARED
+      RocmRuntimeWrappers.cpp
 
-    EXCLUDE_FROM_LIBMLIR
-  )
+      EXCLUDE_FROM_LIBMLIR
+    )
 
-  # Supress compiler warnings from HIP headers
-  check_cxx_compiler_flag(-Wno-c++98-compat-extra-semi
-    CXX_SUPPORTS_NO_CXX98_COMPAT_EXTRA_SEMI_FLAG)
-  if (CXX_SUPPORTS_CXX98_COMPAT_EXTRA_SEMI_FLAG)
-    target_compile_options(mlir_rocm_runtime PRIVATE
-      "-Wno-c++98-compat-extra-semi")
-  endif()
-  check_cxx_compiler_flag(-Wno-return-type-c-linkage
-      CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
-  if (CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
-    target_compile_options(mlir_rocm_runtime PRIVATE
-      "-Wno-return-type-c-linkage")
-  endif()
-  check_cxx_compiler_flag(-Wno-nested-anon-types
-    CXX_SUPPORTS_WNO_NESTED_ANON_TYPES_FLAG)
-  if (CXX_SUPPORTS_WNO_NESTED_ANON_TYPES_FLAG)
-    target_compile_options(mlir_rocm_runtime PRIVATE
-      "-Wno-nested-anon-types")
-  endif()
-  check_cxx_compiler_flag(-Wno-gnu-anonymous-struct
-    CXX_SUPPORTS_WNO_GNU_ANONYMOUS_STRUCT_FLAG)
-  if (CXX_SUPPORTS_WNO_GNU_ANONYMOUS_STRUCT_FLAG)
-    target_compile_options(mlir_rocm_runtime PRIVATE
-     "-Wno-gnu-anonymous-struct")
-  endif()
+    # Supress compiler warnings from HIP headers
+    check_cxx_compiler_flag(-Wno-c++98-compat-extra-semi
+      CXX_SUPPORTS_NO_CXX98_COMPAT_EXTRA_SEMI_FLAG)
+    if (CXX_SUPPORTS_CXX98_COMPAT_EXTRA_SEMI_FLAG)
+      target_compile_options(mlir_rocm_runtime PRIVATE
+        "-Wno-c++98-compat-extra-semi")
+    endif()
+    check_cxx_compiler_flag(-Wno-return-type-c-linkage
+        CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
+    if (CXX_SUPPORTS_WNO_RETURN_TYPE_C_LINKAGE_FLAG)
+      target_compile_options(mlir_rocm_runtime PRIVATE
+        "-Wno-return-type-c-linkage")
+    endif()
+    check_cxx_compiler_flag(-Wno-nested-anon-types
+      CXX_SUPPORTS_WNO_NESTED_ANON_TYPES_FLAG)
+    if (CXX_SUPPORTS_WNO_NESTED_ANON_TYPES_FLAG)
+      target_compile_options(mlir_rocm_runtime PRIVATE
+        "-Wno-nested-anon-types")
+    endif()
+    check_cxx_compiler_flag(-Wno-gnu-anonymous-struct
+      CXX_SUPPORTS_WNO_GNU_ANONYMOUS_STRUCT_FLAG)
+    if (CXX_SUPPORTS_WNO_GNU_ANONYMOUS_STRUCT_FLAG)
+      target_compile_options(mlir_rocm_runtime PRIVATE
+      "-Wno-gnu-anonymous-struct")
+    endif()
 
-  set_property(TARGET mlir_rocm_runtime
-    PROPERTY INSTALL_RPATH_USE_LINK_PATH ON)
+    set_property(TARGET mlir_rocm_runtime
+      PROPERTY INSTALL_RPATH_USE_LINK_PATH ON)
 
-  target_link_libraries(mlir_rocm_runtime
-    PUBLIC
-    hip::host hip::amdhip64
-  )
+    target_link_libraries(mlir_rocm_runtime
+      PUBLIC
+      hip::host hip::amdhip64
+    )
+  endif()
 endif()

diff  --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt
index 8e17a24efc54..e60fbac1e5c1 100644
--- a/mlir/test/CMakeLists.txt
+++ b/mlir/test/CMakeLists.txt
@@ -89,7 +89,7 @@ set(MLIR_TEST_DEPENDS
 # The native target may not be enabled, in this case we won't
 # run tests that involves executing on the host: do not build
 # useless binaries.
-if(TARGET ${LLVM_NATIVE_ARCH})
+if(LLVM_ENABLE_PIC AND TARGET ${LLVM_NATIVE_ARCH})
   list(APPEND MLIR_TEST_DEPENDS
     mlir-cpu-runner
     llc


        


More information about the Mlir-commits mailing list