Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ option(SD_MUSA "sd: musa backend" OFF)
option(SD_BUILD_SHARED_LIBS "sd: build shared libs" OFF)
option(SD_BUILD_SHARED_GGML_LIB "sd: build ggml as a separate shared lib" OFF)
option(SD_USE_SYSTEM_GGML "sd: use system-installed GGML library" OFF)
option(SD_LTO "sd: enable link time optimization" OFF)
#option(SD_BUILD_SERVER "sd: build server example" ON)

set(CMAKE_C_STANDARD 11)
Expand Down Expand Up @@ -361,6 +362,26 @@ endif()

add_subdirectory(thirdparty)

if(SD_LTO)
include(CheckIPOSupported)
check_ipo_supported(RESULT SD_IPO_SUPPORTED OUTPUT SD_IPO_ERROR LANGUAGES C CXX)
if(NOT SD_IPO_SUPPORTED)
message(FATAL_ERROR "SD_LTO requires IPO support: ${SD_IPO_ERROR}")
endif()
endif()

foreach(SD_TARGET IN ITEMS ${SD_LIB} zip)
if(MSVC)
# Static-library consumers can discard unused code and embedded data.
target_compile_options(${SD_TARGET} PRIVATE
"$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>,$<CONFIG:MinSizeRel>>:/Gy>"
"$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>,$<CONFIG:MinSizeRel>>:/Gw>")
endif()
if(SD_LTO)
set_property(TARGET ${SD_TARGET} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
endforeach()

target_sources(${SD_LIB} PRIVATE $<TARGET_OBJECTS:zip>)
target_link_libraries(${SD_LIB} PUBLIC ggml)
target_include_directories(${SD_LIB} PUBLIC . src include)
Expand Down
16 changes: 16 additions & 0 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ vendored builds the in-tree `add_definitions(-DGGML_MAX_NAME=128)` applies.
The GPU backend flags below (`-DSD_METAL=ON`, `-DSD_CUDA=ON`, ...) apply to both
paths.

## Link-time optimization

Pass `-DSD_LTO=ON` to enable CMake's interprocedural optimization for the engine
and its embedded zip object library. With clang-cl this uses ThinLTO; the final
consumer must use a compatible LLVM toolchain (`llvm-lib` and `lld-link`).
Configuration fails if the compiler/linker combination cannot support IPO.

Enable `-DGGML_LTO=ON` when building ggml too. For `SD_USE_SYSTEM_GGML=ON`, that
option belongs in the separate ggml build. Both options default to `OFF`.

Windows release builds package functions and data in individual sections
(`/Gy`, `/Gw`). A consuming DLL or executable can discard unused sections and
fold identical data with `/OPT:REF` and `/OPT:ICF`. In particular, these settings
allow unused and duplicate embedded Vulkan shaders to be removed at the final
link. The library does not change the consumer's exports or optimization level.

## WebP and WebM Support in Examples

The example applications (`examples/cli` and `examples/server`) use `libwebp` to support WebP image I/O, and `examples/cli` can also use `libwebm` for `.webm` video output. Both are enabled by default. WebM output currently reuses `libwebp` to encode each frame as VP8 before muxing with `libwebm`.
Expand Down
Loading