[pkg-d-commits] [ldc] 80/149: [CMake] Add option to build LDC using LTO: `-DLDC_BUILD_WITH_LTO={OFF, ON, FULL, THIN}`.

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:37:00 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.2.0
in repository ldc.

commit 305fcddd57baa86f3fbd3379045703f90d6ac746
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Wed Jan 25 22:44:02 2017 +0100

    [CMake] Add option to build LDC using LTO: `-DLDC_BUILD_WITH_LTO={OFF, ON, FULL, THIN}`.
    
    When LDC_BUILD_WITH_LTO is set, the script adds -flto switch for the C++ compiler and D compiler, but only if they can create an executable with it.
    
    Tested on OSX with ThinLTO (both with and without ThinLTO LLVM build). Some extra work is probably needed on Linux. On Windows, it won't do anything as the cmdline flag checks will fail.
    On Linux, we should also enable the thinlto cache if possible.
    Quick measurements indicate ~3% percent increased performance for -O0 and -O3 compilation, and a ~20% ldc2 file size reduction. Future step is adding PGO ;-)
---
 CMakeLists.txt                               |  6 ++++
 cmake/Modules/HandleLTOPGOBuildOptions.cmake | 46 ++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1452330..08a7cad 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,6 +11,7 @@ include(FindDCompiler)
 include(CheckIncludeFile)
 include(CheckLibraryExists)
 include(CheckCXXCompilerFlag)
+include(CheckDSourceCompiles)
 
 # The script currently only supports the DMD-style commandline interface
 if (NOT D_COMPILER_DMD_COMPAT)
@@ -398,6 +399,11 @@ source_group("Generated Files" REGULAR_EXPRESSION "(id\\.[cdh]|impcnvtab\\.c)$")
 
 
 #
+# Configure the build system to use LTO and/or PGO while building LDC
+#
+include(HandleLTOPGOBuildOptions)
+
+#
 # Enable PGO if supported for this platform and LLVM version.
 # LLVM >= 3.7 is required for PGO.
 #
diff --git a/cmake/Modules/HandleLTOPGOBuildOptions.cmake b/cmake/Modules/HandleLTOPGOBuildOptions.cmake
new file mode 100644
index 0000000..fc48b78
--- /dev/null
+++ b/cmake/Modules/HandleLTOPGOBuildOptions.cmake
@@ -0,0 +1,46 @@
+# Handles the LDC_BUILD_WITH_LTO build option.
+# For example `cmake -DLDC_BUILD_WITH_LTO=thin`.
+#
+# LTO is enabled for the C++ and D compilers, provided that they accept the `-flto` flag.
+
+# TODO: implement a LDC_BUILD_WITH_PGO build option (or something similar) to generate/use an LDC PGO profile.
+
+set(__LTO_FLAG)
+set(LDC_BUILD_WITH_LTO OFF CACHE STRING "Build LDC with LTO. May be specified as Thin or Full to use a particular kind of LTO")
+string(TOUPPER "${LDC_BUILD_WITH_LTO}" uppercase_LDC_BUILD_WITH_LTO)
+if(uppercase_LDC_BUILD_WITH_LTO STREQUAL "THIN")
+    set(__LTO_FLAG "-flto=thin")
+elseif(uppercase_LDC_BUILD_WITH_LTO STREQUAL "FULL")
+    set(__LTO_FLAG "-flto=full")
+elseif(LDC_BUILD_WITH_LTO)
+    set(__LTO_FLAG "-flto")
+endif()
+if(__LTO_FLAG)
+    message(STATUS "Building LDC using LTO: ${__LTO_FLAG}")
+    check_cxx_compiler_flag(${__LTO_FLAG} CXX_COMPILER_ACCEPTS_FLTO_${uppercase_LDC_BUILD_WITH_LTO})
+    if (CXX_COMPILER_ACCEPTS_FLTO_${uppercase_LDC_BUILD_WITH_LTO})
+        append(${__LTO_FLAG} CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
+    endif()
+
+    check_d_source_compiles("void main(){}" D_COMPILER_ACCEPTS_FLTO_${uppercase_LDC_BUILD_WITH_LTO} FLAGS ${__LTO_FLAG})
+    if(D_COMPILER_ACCEPTS_FLTO_${uppercase_LDC_BUILD_WITH_LTO})
+        append(${__LTO_FLAG} DDMD_DFLAGS)
+    endif()
+
+    if(uppercase_LDC_BUILD_WITH_LTO STREQUAL "THIN")
+        # On darwin, enable the lto cache.
+        if(APPLE)
+            append("-Wl,-cache_path_lto,${PROJECT_BINARY_DIR}/lto.cache" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
+        endif()
+    endif()
+    if(uppercase_LDC_BUILD_WITH_LTO MATCHES "^(THIN|FULL)$")
+        if (APPLE)
+            # Explicitly use the LTO library that shipped with the host LDC, assuming it is newer than the system-installed lib.
+            get_filename_component(HOST_LDC_BINDIR ${D_COMPILER} DIRECTORY)
+            if(EXISTS ${HOST_LDC_BINDIR}/../lib/libLTO-ldc.dylib)
+                message(STATUS "Using ${HOST_LDC_BINDIR}/../lib/libLTO-ldc.dylib for LTO")
+                append("-Wl,-lto_library,${HOST_LDC_BINDIR}/../lib/libLTO-ldc.dylib" CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
+            endif()
+        endif()
+    endif()
+endif()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list