diff --git a/CMakeLists.txt b/CMakeLists.txt index d36b82edf04..2af226099f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,8 @@ option(STABLEHLO_BUILD_EMBEDDED "Build StableHLO as part of another project" OFF option(STABLEHLO_ENABLE_BINDINGS_PYTHON "Enables StableHLO Python bindings" OFF) option(STABLEHLO_ENABLE_STRICT_BUILD "Build StableHLO with strict warnings and warnings as errors" OFF) option(STABLEHLO_ENABLE_SANITIZER "Enable a sanitizer [OFF, address]" OFF) +option(STABLEHLO_ENABLE_SPLIT_DWARF "Enable split DWARF if the platform supports it" OFF) +option(STABLEHLO_ENABLE_LLD "Use LLD as the linker if available" OFF) #------------------------------------------------------------------------------- # Project setup and globals @@ -128,6 +130,22 @@ if(LLVM_ENABLE_ZLIB) find_package(ZLIB) endif() +#------------------------------------------------------------------------------- +# Performance configuration +#------------------------------------------------------------------------------- + +include(CheckCXXCompilerFlag) +include(CheckLinkerFlag) +add_link_options("$<$:-fuse-ld=lld>") +if(STABLEHLO_ENABLE_SPLIT_DWARF) + check_cxx_compiler_flag(-gsplit-dwarf STABLEHLO_SUPPORTS_SPLIT_DWARF) + add_compile_options("$<$:-gsplit-dwarf;-ggnu-pubnames>") + check_linker_flag(CXX "-Wl,--gdb-index" STABLEHLO_SUPPORTS_GDB_INDEX) + # If we set LLD it doesn't seem to affect the check_linker_flag above. + # Account for it with the generator expression OR + add_link_options("$<$,$>:-Wl,--gdb-index>") +endif() + include(TableGen) include(AddLLVM) include(AddMLIR) diff --git a/README.md b/README.md index 83fe996bae2..d630e776b57 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,30 @@ Here's how to build the StableHLO repo on Linux or macOS: -DMLIR_DIR=${PWD}/../llvm-build/lib/cmake/mlir ``` + If you are actively developing StableHLO, you may want the following additional + CMake settings: + + ```shell + cmake .. -GNinja \ + -DSTABLEHLO_ENABLE_LLD=ON \ + -DCMAKE_BUILD_TYPE=RelWithDebInfo \ + -DLLVM_ENABLE_ASSERTIONS=ON \ + -DSTABLEHLO_ENABLE_BINDINGS_PYTHON=OFF \ + -DSTABLEHLO_ENABLE_SPLIT_DWARF=ON \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DSTABLEHLO_ENABLE_SANITIZER=address \ + -DMLIR_DIR=${PWD}/../llvm-build/lib/cmake/mlir + ``` + + This will enable debug symbols and ccache, which can speed up incremental + builds. It also creates a GDB index file in the binary to speed up + debugging. + + If you build MLIR using the script above it should also set by default + `LLVM_USE_SPLIT_DWARF` which does the majority of the size saving for + the binary and should also be set. + 7. Now you can make sure it works by running some tests: ```sh diff --git a/build_tools/build_mlir.sh b/build_tools/build_mlir.sh index e56183e65ac..75f6ae51590 100755 --- a/build_tools/build_mlir.sh +++ b/build_tools/build_mlir.sh @@ -56,6 +56,7 @@ cmake -GNinja \ -DLLVM_BUILD_TOOLS=OFF \ -DLLVM_INCLUDE_TESTS=OFF \ -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \ + -DLLVM_USE_SPLIT_DWARF=ON \ -DLLVM_ENABLE_ASSERTIONS=ON cmake --build "$build_dir" --target all diff --git a/cmake/SetupSanitizers.cmake b/cmake/SetupSanitizers.cmake index df0f6cce4f2..5c1debb76c1 100644 --- a/cmake/SetupSanitizers.cmake +++ b/cmake/SetupSanitizers.cmake @@ -47,7 +47,7 @@ function(setup_sanitizers) if (STABLEHLO_ENABLE_SANITIZER_LOWERCASE STREQUAL "address") add_compile_options(-fsanitize=address -fsanitize=undefined -fsanitize=leak -fno-omit-frame-pointer) - link_libraries(-fsanitize=address -fsanitize=undefined -fsanitize=leak) + add_link_options(-fsanitize=address -fsanitize=undefined -fsanitize=leak) else () message(FATAL_ERROR "Unknown sanitizer type: ${STABLEHLO_ENABLE_SANITIZER}") endif ()