From 7bce3771135ec738cd9db28beaa060c62bb53f6d Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Thu, 16 May 2019 14:06:38 -0700 Subject: [PATCH] Fix LTO build failure on ubuntu (#1048) * Fix LTO build failure on ubuntu --- cmake/CMakeLists.txt | 23 +++++++++++------------ cmake/onnxruntime.cmake | 5 ++++- cmake/onnxruntime_python.cmake | 5 ++++- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 2e6337efc6..85af7df79a 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -68,7 +68,8 @@ option(onnxruntime_ENABLE_MICROSOFT_INTERNAL "Use this option to enable/disable option(onnxruntime_USE_NUPHAR "Build with Nupha" OFF) option(onnxruntime_USE_BRAINSLICE "Build with BrainSlice" OFF) option(onnxruntime_USE_TENSORRT "Build with TensorRT support" OFF) -option(onnxruntime_ENABLE_LTO "Enable link time optimization, which is not stable on older GCCs" OFF) +option(onnxruntime_ENABLE_LTO "Enable link time optimization" ON) + option(onnxruntime_CROSS_COMPILING "Cross compiling onnx runtime" OFF) option(onnxruntime_BUILD_SERVER "Build ONNX Runtime Server" OFF) option(onnxruntime_USE_FULL_PROTOBUF "Use full protobuf" OFF) @@ -82,18 +83,16 @@ set(NSYNC_ENABLE_TESTS OFF CACHE BOOL "Build protobuf tests" FORCE) set(ONNX_ML 1) if(onnxruntime_ENABLE_LTO) - #LTO(or LTCG) is great, in our case it can reduce binary size by 1/3. - #cmake can only help us check if the compiler support LTO or not, it can't tell us if the feature works well - #Don't enable this option in Ubuntu 16.04, protoc will crash - include(CheckIPOSupported) - check_ipo_supported(RESULT ipo_enabled OUTPUT ipo_output) #TODO: figure out why nsync doesn't work - if(NOT onnxruntime_USE_NSYNC) - if(ipo_enabled) - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON) - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON) - else() - message(WARNING "IPO is not supported: ${ipo_output}") + if(onnxruntime_USE_NSYNC) + message(WARNING "IPO is not supported when nsync is in use") + set(onnxruntime_ENABLE_LTO OFF) + else() + include(CheckIPOSupported) + check_ipo_supported(RESULT ipo_enabled OUTPUT ipo_output) + if(NOT ipo_enabled) + message(WARNING "IPO is not supported by this compiler") + set(onnxruntime_ENABLE_LTO OFF) endif() endif() endif() diff --git a/cmake/onnxruntime.cmake b/cmake/onnxruntime.cmake index 952732b99d..ecf833a546 100644 --- a/cmake/onnxruntime.cmake +++ b/cmake/onnxruntime.cmake @@ -74,7 +74,10 @@ target_link_libraries(onnxruntime PRIVATE set_property(TARGET onnxruntime APPEND_STRING PROPERTY LINK_FLAGS ${ONNXRUNTIME_SO_LINK_FLAG}) set_target_properties(onnxruntime PROPERTIES LINK_DEPENDS ${SYMBOL_FILE}) - +if(onnxruntime_ENABLE_LTO) + set_target_properties(onnxruntime PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) + set_target_properties(onnxruntime PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE) +endif() install(TARGETS onnxruntime ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/cmake/onnxruntime_python.cmake b/cmake/onnxruntime_python.cmake index 6f2d325090..24a2e973bc 100644 --- a/cmake/onnxruntime_python.cmake +++ b/cmake/onnxruntime_python.cmake @@ -109,7 +109,10 @@ endif() set_target_properties(onnxruntime_pybind11_state PROPERTIES PREFIX "") set_target_properties(onnxruntime_pybind11_state PROPERTIES FOLDER "ONNXRuntime") - +if(onnxruntime_ENABLE_LTO) + set_target_properties(onnxruntime_pybind11_state PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) + set_target_properties(onnxruntime_pybind11_state PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE) +endif() if (MSVC) set_target_properties(onnxruntime_pybind11_state PROPERTIES SUFFIX ".pyd") else()