[llvm-commits] [PATCH] CMake option for enabling optimization for utilities in debug build

Óscar Fuentes ofv at wanadoo.es
Sun Mar 13 15:23:38 PDT 2011


Erik Olofsson <Erik.Olofsson at hansoft.se>
writes:

> This patch enables optimization for the utilities such as tblgen in
> the utils dir in Debug builds. This decreases the compile time of the
> debug build considerably on Visual Studio, and somewhat on OSX.

There is one way of using an optimized tlbgen executable: configure
setting LLVM_TABLEGEN. You can point it to a tblgen.exe that still does
not exist. Supposing that you are using VS and your build directory is
c:\build :

cmake -DLLVM_TABLEGEN=c:/build/bin/Release/tblgen.exe path/to/llvm/sources

Now we build the tblgen target in Release mode. You can do that from the
IDE but with the command line is:

cmake --build . --target tblgen --config Release

Now you can build in Debug mode. The tblgen.exe generated above will be
used:

cmake --build . --config Debug

[snip]

> Index: cmake/modules/HandleLLVMOptions.cmake
> ===================================================================
> --- cmake/modules/HandleLLVMOptions.cmake	(revision 127564)
> +++ cmake/modules/HandleLLVMOptions.cmake	(working copy)

HandleLLVMOptions.cmake is intended to share code that must be run by
external projects which depend on the build machinery of LLVM
(i.e. Clang). This is not the case, so the right place is the toplevel
CMakeLists.txt.

> @@ -102,6 +102,29 @@
>    endif( LLVM_BUILD_32_BITS )
>  endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
>  
> +option(LLVM_ALWAYS_OPTIMIZE_UTILS 
> +  "Always uses optimized utilities even in debug builds. This decreases build time but decreases debugability for the utilities. Default is 0."
> +  OFF)

"... Default is OFF."

> +if( LLVM_ALWAYS_OPTIMIZE_UTILS )
> +  set(LLVM_UTILS_SUPPORT_LIB_NAME LLVMSupportRelease)
> +  message(STATUS "Utilities always compiled with optimizations enabled")
> +  macro(fix_utils_compile_options)
> +    if( MSVC )
> +      # Enable debug info for MSVC
> +      set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
> +      set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_RELEASE} /Zi")

Zi ? Why? Is it because the linker complains? Then, you should set the
flags for linking too.

> +    else()
> +      set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE}")
> +      set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_RELEASE}")
> +    endif()
> +  endmacro(fix_utils_compile_options)   
> +else( LLVM_ALWAYS_OPTIMIZE_UTILS )
> +  set(LLVM_UTILS_SUPPORT_LIB_NAME LLVMSupport)
> +  macro(fix_utils_compile_options)
> +  endmacro(fix_utils_compile_options)   
> +endif( LLVM_ALWAYS_OPTIMIZE_UTILS )
> +  
>  if( MSVC_IDE AND ( MSVC90 OR MSVC10 ) )
>    # Only Visual Studio 2008 and 2010 officially supports /MP.
>    # Visual Studio 2005 do support it but it's experimental there.
> Index: docs/CMake.html
> ===================================================================
> --- docs/CMake.html	(revision 127564)
> +++ docs/CMake.html	(working copy)
> @@ -350,6 +350,11 @@
>      Function Interface library. If the library or its headers are
>      installed on a custom location, you can set the variables
>      FFI_INCLUDE_DIR and FFI_LIBRARY_DIR. Defaults to OFF.</dd>
> +  
> +  <dt><b>LLVM_ALWAYS_OPTIMIZE_UTILS</b>:BOOL</dt>
> +  <dd>Always uses optimized utilities even in debug builds. This 
> +    decreases build time but also decreases debugability for the 
> +    utilities. Defaults to OFF.</dd>
>  </dl>
>  
>  </div>
> Index: lib/Support/CMakeLists.txt
> ===================================================================
> --- lib/Support/CMakeLists.txt	(revision 127564)
> +++ lib/Support/CMakeLists.txt	(working copy)
> @@ -4,100 +4,110 @@
>    set(LLVM_REQUIRES_EH 1)
>  endif()
>  
> -add_llvm_library(LLVMSupport
> -  APFloat.cpp
> -  APInt.cpp
> -  APSInt.cpp
> -  Allocator.cpp
> -  circular_raw_ostream.cpp
> -  CommandLine.cpp
> -  ConstantRange.cpp
> -  CrashRecoveryContext.cpp
> -  Debug.cpp
> -  DeltaAlgorithm.cpp
> -  DAGDeltaAlgorithm.cpp
> -  Dwarf.cpp
> -  ErrorHandling.cpp
> -  FileUtilities.cpp
> -  FoldingSet.cpp
> -  FormattedStream.cpp
> -  GraphWriter.cpp
> -  IntEqClasses.cpp
> -  IntervalMap.cpp
> -  IsInf.cpp
> -  IsNAN.cpp
> -  ManagedStatic.cpp
> -  MemoryBuffer.cpp
> -  MemoryObject.cpp
> -  PluginLoader.cpp
> -  PrettyStackTrace.cpp
> -  Regex.cpp
> -  SmallPtrSet.cpp
> -  SmallVector.cpp
> -  SourceMgr.cpp
> -  Statistic.cpp
> -  StringExtras.cpp
> -  StringMap.cpp
> -  StringPool.cpp
> -  StringRef.cpp
> -  SystemUtils.cpp
> -  TargetRegistry.cpp
> -  Timer.cpp
> -  ToolOutputFile.cpp
> -  Triple.cpp
> -  Twine.cpp
> -  raw_os_ostream.cpp
> -  raw_ostream.cpp
> -  regcomp.c
> -  regerror.c
> -  regexec.c
> -  regfree.c
> -  regstrlcpy.c
> +#This macro is used both from this file and SupportRelease\CMakeLists.txt with the intention that both targets should have the same files but different compile options
> +macro(add_llvm_support_library prefix name)
> +  add_llvm_library(${name}
> +    ${prefix}APFloat.cpp
> +    ${prefix}APInt.cpp
> +    ${prefix}APSInt.cpp
> +    ${prefix}Allocator.cpp
> +    ${prefix}circular_raw_ostream.cpp
> +    ${prefix}CommandLine.cpp
> +    ${prefix}ConstantRange.cpp
> +    ${prefix}CrashRecoveryContext.cpp
> +    ${prefix}Debug.cpp
> +    ${prefix}DeltaAlgorithm.cpp
> +    ${prefix}DAGDeltaAlgorithm.cpp
> +    ${prefix}Dwarf.cpp
> +    ${prefix}ErrorHandling.cpp
> +    ${prefix}FileUtilities.cpp
> +    ${prefix}FoldingSet.cpp
> +    ${prefix}FormattedStream.cpp
> +    ${prefix}GraphWriter.cpp
> +    ${prefix}IntEqClasses.cpp
> +    ${prefix}IntervalMap.cpp
> +    ${prefix}IsInf.cpp
> +    ${prefix}IsNAN.cpp
> +    ${prefix}ManagedStatic.cpp
> +    ${prefix}MemoryBuffer.cpp
> +    ${prefix}MemoryObject.cpp
> +    ${prefix}PluginLoader.cpp
> +    ${prefix}PrettyStackTrace.cpp
> +    ${prefix}Regex.cpp
> +    ${prefix}SmallPtrSet.cpp
> +    ${prefix}SmallVector.cpp
> +    ${prefix}SourceMgr.cpp
> +    ${prefix}Statistic.cpp
> +    ${prefix}StringExtras.cpp
> +    ${prefix}StringMap.cpp
> +    ${prefix}StringPool.cpp
> +    ${prefix}StringRef.cpp
> +    ${prefix}SystemUtils.cpp
> +    ${prefix}TargetRegistry.cpp
> +    ${prefix}Timer.cpp
> +    ${prefix}ToolOutputFile.cpp
> +    ${prefix}Triple.cpp
> +    ${prefix}Twine.cpp
> +    ${prefix}raw_os_ostream.cpp
> +    ${prefix}raw_ostream.cpp
> +    ${prefix}regcomp.c
> +    ${prefix}regerror.c
> +    ${prefix}regexec.c
> +    ${prefix}regfree.c
> +    ${prefix}regstrlcpy.c
>  
> -# System
> -  Atomic.cpp
> -  Disassembler.cpp
> -  DynamicLibrary.cpp
> -  Errno.cpp
> -  Host.cpp
> -  IncludeFile.cpp
> -  Memory.cpp
> -  Mutex.cpp
> -  Path.cpp
> -  PathV2.cpp
> -  Process.cpp
> -  Program.cpp
> -  RWMutex.cpp
> -  SearchForAddressOfSpecialSymbol.cpp
> -  Signals.cpp
> -  system_error.cpp
> -  ThreadLocal.cpp
> -  Threading.cpp
> -  TimeValue.cpp
> -  Valgrind.cpp
> -  Unix/Host.inc
> -  Unix/Memory.inc
> -  Unix/Mutex.inc
> -  Unix/Path.inc
> -  Unix/PathV2.inc
> -  Unix/Process.inc
> -  Unix/Program.inc
> -  Unix/RWMutex.inc
> -  Unix/Signals.inc
> -  Unix/system_error.inc
> -  Unix/ThreadLocal.inc
> -  Unix/TimeValue.inc
> -  Windows/DynamicLibrary.inc
> -  Windows/Host.inc
> -  Windows/Memory.inc
> -  Windows/Mutex.inc
> -  Windows/Path.inc
> -  Windows/PathV2.inc
> -  Windows/Process.inc
> -  Windows/Program.inc
> -  Windows/RWMutex.inc
> -  Windows/Signals.inc
> -  Windows/system_error.inc
> -  Windows/ThreadLocal.inc
> -  Windows/TimeValue.inc
> -  )
> +  # System
> +    ${prefix}Atomic.cpp
> +    ${prefix}Disassembler.cpp
> +    ${prefix}DynamicLibrary.cpp
> +    ${prefix}Errno.cpp
> +    ${prefix}Host.cpp
> +    ${prefix}IncludeFile.cpp
> +    ${prefix}Memory.cpp
> +    ${prefix}Mutex.cpp
> +    ${prefix}Path.cpp
> +    ${prefix}PathV2.cpp
> +    ${prefix}Process.cpp
> +    ${prefix}Program.cpp
> +    ${prefix}RWMutex.cpp
> +    ${prefix}SearchForAddressOfSpecialSymbol.cpp
> +    ${prefix}Signals.cpp
> +    ${prefix}system_error.cpp
> +    ${prefix}ThreadLocal.cpp
> +    ${prefix}Threading.cpp
> +    ${prefix}TimeValue.cpp
> +    ${prefix}Valgrind.cpp
> +    ${prefix}Unix/Host.inc
> +    ${prefix}Unix/Memory.inc
> +    ${prefix}Unix/Mutex.inc
> +    ${prefix}Unix/Path.inc
> +    ${prefix}Unix/PathV2.inc
> +    ${prefix}Unix/Process.inc
> +    ${prefix}Unix/Program.inc
> +    ${prefix}Unix/RWMutex.inc
> +    ${prefix}Unix/Signals.inc
> +    ${prefix}Unix/system_error.inc
> +    ${prefix}Unix/ThreadLocal.inc
> +    ${prefix}Unix/TimeValue.inc
> +    ${prefix}Windows/DynamicLibrary.inc
> +    ${prefix}Windows/Host.inc
> +    ${prefix}Windows/Memory.inc
> +    ${prefix}Windows/Mutex.inc
> +    ${prefix}Windows/Path.inc
> +    ${prefix}Windows/PathV2.inc
> +    ${prefix}Windows/Process.inc
> +    ${prefix}Windows/Program.inc
> +    ${prefix}Windows/RWMutex.inc
> +    ${prefix}Windows/Signals.inc
> +    ${prefix}Windows/system_error.inc
> +    ${prefix}Windows/ThreadLocal.inc
> +    ${prefix}Windows/TimeValue.inc
> +    )
> +endmacro(add_llvm_support_library name)

Ouch!

> +add_llvm_support_library("" LLVMSupport)
> +
> +if( LLVM_ALWAYS_OPTIMIZE_UTILS )
> +  add_subdirectory(SupportRelease)
> +endif()
> + 
> \ No newline at end of file
> Index: lib/Support/SupportRelease/CMakeLists.txt
> ===================================================================
> --- lib/Support/SupportRelease/CMakeLists.txt	(revision 0)
> +++ lib/Support/SupportRelease/CMakeLists.txt	(revision 0)
> @@ -0,0 +1,9 @@
> +## FIXME: This only requires RTTI because tblgen uses it.  Fix that.
> +set(LLVM_REQUIRES_RTTI 1)
> +if( MINGW )
> +  set(LLVM_REQUIRES_EH 1)
> +endif()
> +
> +fix_utils_compile_options()
> +
> +add_llvm_support_library("../" LLVMSupportRelease)
> Index: utils/FileCheck/CMakeLists.txt

IIRC you can build two libraries with the same source files from the
same CMakeLists.txt:

set(SOURCES
  APFloat.cpp
  <rest of source files>
  )

add_llvm_library(LLVMSupport ${SOURCES})

if( LLVM_ALWAYS_OPTIMIZE_UTILS )
  add_llvm_library(OptimizedLLVMSupport ${SOURCES})
  <set compiler options for target OptimizedLLVMSupport>
endif()

[snip]

> Index: utils/TableGen/CMakeLists.txt
> ===================================================================
> --- utils/TableGen/CMakeLists.txt	(revision 127564)
> +++ utils/TableGen/CMakeLists.txt	(working copy)
> @@ -3,6 +3,8 @@
>  
>  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
>  
> +fix_utils_compile_options()
> +
>  add_llvm_utility(tblgen
>    ARMDecoderEmitter.cpp
>    AsmMatcherEmitter.cpp
> @@ -45,7 +47,7 @@
>    X86RecognizableInstr.cpp
>    )
>  
> -target_link_libraries(tblgen LLVMSupport)
> +target_link_libraries(tblgen ${LLVM_UTILS_SUPPORT_LIB_NAME})
>  if( MINGW )
>    target_link_libraries(tblgen imagehlp psapi)
>  endif( MINGW )

I think that all those changes to the utils/*/CMakeLists.txt files will
be unnecessary if you take advantage of the existence of
add_llvm_utility (defined in
$LLVM_SOURCE_ROOT/cmake/modules/AddLLVM.cmake). It would make
fix_utils_compile_options unnecessary too.




More information about the llvm-commits mailing list