diff --git a/.github/workflows/buildAndTestCMake.yml b/.github/workflows/buildAndTestCMake.yml index 2cdb0725c40..953126f1cd2 100644 --- a/.github/workflows/buildAndTestCMake.yml +++ b/.github/workflows/buildAndTestCMake.yml @@ -37,11 +37,6 @@ jobs: # Only run scheduled CI on main repo if: (github.repository == 'openxla/stablehlo' || github.event_name != 'schedule') name: "cmake-build ${{ github.event_name == 'schedule' && '(llvm-project@HEAD)' || ''}}" - env: - LLVM_PROJECT_DIR: "llvm-project" - LLVM_BUILD_DIR: "llvm-build" - STABLEHLO_BUILD_DIR: "stablehlo-build" - STABLEHLO_PYTHON_BUILD_DIR: "stablehlo-python-build" strategy: fail-fast: false runs-on: ${{ github.repository == 'openxla/stablehlo' && 'ubuntu-22.04-64core' || 'ubuntu-22.04' }} @@ -66,14 +61,6 @@ jobs: with: llvm-version: ${{ steps.llvm-version.outputs.version }} - - name: Configure and Build LLVM - shell: bash - run: | - ./build_tools/github_actions/ci_build_cmake_llvm.sh "$LLVM_PROJECT_DIR" "$LLVM_BUILD_DIR" - env: - CMAKE_BUILD_TYPE: Release - MLIR_ENABLE_BINDINGS_PYTHON: ON - - name: Fix kernel mmap rnd bits # Asan in llvm 14 provided in ubuntu 22.04 is incompatible with # high-entropy ASLR in much newer kernels that GitHub runners are @@ -84,16 +71,11 @@ jobs: - name: Build and Test StableHLO (with AddressSanitizer) shell: bash run: | - ./build_tools/github_actions/ci_build_cmake.sh "$LLVM_BUILD_DIR" "$STABLEHLO_BUILD_DIR" - env: - CMAKE_BUILD_TYPE: Release - STABLEHLO_ENABLE_BINDINGS_PYTHON: OFF - STABLEHLO_ENABLE_SANITIZER: address + cmake --preset debug + cmake --build ./build --target check-stablehlo-ci - name: Build and Test StableHLO (with Python bindings) shell: bash run: | - ./build_tools/github_actions/ci_build_cmake.sh "$LLVM_BUILD_DIR" "$STABLEHLO_BUILD_DIR" - env: - CMAKE_BUILD_TYPE: Release - STABLEHLO_ENABLE_BINDINGS_PYTHON: ON + cmake --preset debug-python + cmake --build ./build --target check-stablehlo-ci diff --git a/CMakeLists.txt b/CMakeLists.txt index c466b474e17..6fa0c198b77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,13 +89,69 @@ if(STABLEHLO_EXTERNAL_PROJECT_BUILD) list(APPEND CMAKE_MODULE_PATH "${MLIR_MAIN_SRC_DIR}/cmake/modules") elseif(NOT STABLEHLO_BUILD_EMBEDDED) message(STATUS "Building StableHLO with an installed MLIR") - find_package(MLIR REQUIRED CONFIG) + + # These defaults are moderately important to us, but the user *can* + # override them (enabling some of these brings in deps that will conflict, + # so ymmv). + # https://github.com/openxla/iree/blob/f3b6bcd79b24ef4a9b355eb3f3496ffafcbd0881/build_tools/cmake/iree_llvm.cmake#L127 + set(LLVM_INCLUDE_EXAMPLES OFF CACHE BOOL "") + set(LLVM_INCLUDE_TESTS OFF CACHE BOOL "") + set(LLVM_INCLUDE_BENCHMARKS OFF CACHE BOOL "") + set(LLVM_APPEND_VC_REV OFF CACHE BOOL "") + set(LLVM_ENABLE_IDE ON CACHE BOOL "") + set(LLVM_ENABLE_BINDINGS OFF CACHE BOOL "") + # LLVM defaults to building all targets. We always enable targets that we need + # as we need them, so default to none. The user can override this as needed, + # which is fine. + set(LLVM_TARGETS_TO_BUILD "" CACHE STRING "") + + # We enable LLVM projects as needed. The user can override this. + set(LLVM_ENABLE_PROJECTS "" CACHE STRING "") + set(LLVM_EXTERNAL_PROJECTS "" CACHE STRING "") + + # Unconditionally enable mlir. + list(APPEND LLVM_ENABLE_PROJECTS mlir) + + # Setup LLVM lib and bin directories. + set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/llvm-project/bin) + set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/llvm-project/lib) + set(LLVM_TOOLS_BINARY_DIR ${CMAKE_BINARY_DIR}/llvm-project/bin) + + list(APPEND CMAKE_MESSAGE_INDENT " ") + set(_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR "llvm-project/llvm") + add_subdirectory("${_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR}" "llvm-project" EXCLUDE_FROM_ALL) + get_directory_property(LLVM_VERSION_MAJOR DIRECTORY "${_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR}" LLVM_VERSION_MAJOR) + if (NOT LLVM_VERSION_MAJOR) + message(SEND_ERROR "Failed to read LLVM_VERSION_MAJOR property on LLVM directory. Should have been set since https://github.com/llvm/llvm-project/pull/83346.") + endif() + list(POP_BACK CMAKE_MESSAGE_INDENT) + + # Set some CMake variables that mirror things exported in the find_package + # world. Source of truth for these is in an installed LLVMConfig.cmake, + # MLIRConfig.cmake, LLDConfig.cmake (etc) and in the various standalone + # build segments of each project's top-level CMakeLists. + set(LLVM_CMAKE_DIR "${CMAKE_BINARY_DIR}/llvm-project/lib/cmake/llvm") + list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") + # TODO: Fix MLIR upstream so it doesn't spew into the containing project + # binary dir. See mlir/cmake/modules/CMakeLists.txt + # (and other LLVM sub-projects). + set(MLIR_CMAKE_DIR "${CMAKE_BINARY_DIR}/lib/cmake/mlir") + if(NOT EXISTS "${MLIR_CMAKE_DIR}/AddMLIR.cmake") + message(SEND_ERROR "Could not find AddMLIR.cmake in ${MLIR_CMAKE_DIR}: LLVM sub-projects may have changed their layout. See the mlir_cmake_builddir variable in mlir/cmake/modules/CMakeLists.txt") + endif() + list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") + message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") - set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin) - set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib) - list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") - list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") + + set(LLVM_INCLUDE_DIRS + ${CMAKE_SOURCE_DIR}/${_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR}/include + ${CMAKE_BINARY_DIR}/llvm-project/include + ) + set(MLIR_INCLUDE_DIRS + ${CMAKE_SOURCE_DIR}/llvm-project/mlir/include + ${CMAKE_BINARY_DIR}/llvm-project/tools/mlir/include + ) else() message(STATUS "Building StableHLO embedded in another project") endif() diff --git a/CMakePresets.json b/CMakePresets.json index fbdfb40ad69..746b57f0206 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -11,13 +11,16 @@ "LLVM_ENABLE_ASSERTIONS": "ON", "LLVM_ENABLE_LLD": "ON", "STABLEHLO_ENABLE_BINDINGS_PYTHON" : "OFF", - "STABLEHLO_ENABLE_SPLIT_DWARF": "ON", "CMAKE_CXX_COMPILER_LAUNCHER": "ccache", "CMAKE_CXX_COMPILER": "clang++", "CMAKE_C_COMPILER_LAUNCHER": "ccache", "CMAKE_C_COMPILER": "clang", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", - "MLIR_DIR": "${sourceDir}/llvm-build/lib/cmake/mlir" + "CMAKE_PLATFORM_NO_VERSIONED_SONAME": "ON", + "LLVM_VERSION_SUFFIX": "", + "LLVM_USE_SPLIT_DWARF": "ON", + "STABLEHLO_ENABLE_SPLIT_DWARF": "ON", + "STABLEHLO_ENABLE_SANITIZER": "address" } }, { @@ -25,21 +28,10 @@ "displayName": "Debug w/ python bindings", "inherits": "debug", "cacheVariables": { + "MLIR_ENABLE_BINDINGS_PYTHON": "ON", "STABLEHLO_ENABLE_BINDINGS_PYTHON" : "ON", "STABLEHLO_ENABLE_SANITIZER": "OFF" } } - ], - "buildPresets": [ - { - "name": "debug", - "displayName": "Build Debug", - "configurePreset": "debug" - }, - { - "name": "debug-python", - "displayName": "Build Debug w/ python bindings", - "configurePreset": "debug-python" - } - ] + ] } diff --git a/README.md b/README.md index 5295b8d7663..dbb1f485f98 100644 --- a/README.md +++ b/README.md @@ -32,20 +32,13 @@ Here's how to build the StableHLO repo on Linux or macOS: ```sh # On Linux - sudo apt install cmake ninja-build lld + sudo apt install cmake ninja-build lld ccache # On macOS - brew install cmake ninja + brew install cmake ninja ccache ``` -2. Set the `LLVM_ENABLE_LLD` shell variable depending on your preferences. We - recommend setting it to `ON` on Linux and to `OFF` on macOS. - - ```sh - [[ "$(uname)" != "Darwin" ]] && LLVM_ENABLE_LLD="ON" || LLVM_ENABLE_LLD="OFF" - ``` - -3. Clone the StableHLO repo and the LLVM repository: +2. Clone the StableHLO repo and the LLVM repository: ```sh git clone https://github.com/openxla/stablehlo @@ -57,7 +50,7 @@ Here's how to build the StableHLO repo on Linux or macOS: Cloning the LLVM repository may take a few minutes. -4. Make sure you check out the correct commit in the LLVM repository: +3. Make sure you check out the correct commit in the LLVM repository: ```sh (cd llvm-project && git fetch && git checkout $(cat ../build_tools/llvm_version.txt)) @@ -65,73 +58,26 @@ Here's how to build the StableHLO repo on Linux or macOS: You need to do this every time `llvm_version.txt` changes. -5. Configure and build MLIR: - - ```sh - MLIR_ENABLE_BINDINGS_PYTHON=OFF build_tools/build_mlir.sh ${PWD}/llvm-project/ ${PWD}/llvm-build - ``` - - This will take a considerable amount of time. For example, on a MacBook Pro - with an M1 Pro chip, building MLIR took around 10 minutes at the moment - of writing. - - Again, you need to do this every time `llvm_version.txt` changes. - -6. Build StableHLO as a standalone library: +4. Build StableHLO as a standalone library and run all the tests: ```sh - mkdir -p build && cd build - - cmake .. -GNinja \ - -DLLVM_ENABLE_LLD="$LLVM_ENABLE_LLD" \ - -DCMAKE_BUILD_TYPE=Release \ - -DLLVM_ENABLE_ASSERTIONS=ON \ - -DSTABLEHLO_ENABLE_BINDINGS_PYTHON=OFF \ - -DMLIR_DIR=${PWD}/../llvm-build/lib/cmake/mlir - - cmake --build . - ``` - - If you are actively developing StableHLO, you may want the following additional - CMake settings: - - ```sh - 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 - - cmake --build . - ``` - - 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 - ninja check-stablehlo-tests + # first configure the build system + cmake --preset debug + # then build the project + cmake --build ./build --target check-stablehlo-ci ``` You should see results like this: ```txt - Testing Time: 5.99s - Passed: 47 + Testing Time: 4.13s + + Total Discovered Tests: 137 + Passed: 137 (100.00%) ``` - This runs all the tests in `stablehlo/tests/`. + This runs all the tests in `stablehlo/tests/`. You can change the target + to build or test specific parts of the project. ## Python diff --git a/build_tools/build_mlir.sh b/build_tools/build_mlir.sh deleted file mode 100755 index c0311c33534..00000000000 --- a/build_tools/build_mlir.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/bash -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# Copyright 2022 The StableHLO Authors. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e - -if [[ $# -ne 2 ]] ; then - echo "Usage: $0 " - exit 1 -fi - -# LLVM source -LLVM_SRC_DIR="$1" -build_dir="$2" -CMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE:-RelWithDebInfo}" -# Turn on building Python bindings -MLIR_ENABLE_BINDINGS_PYTHON="${MLIR_ENABLE_BINDINGS_PYTHON:-OFF}" - -# Check if ccache is available and set the compiler launcher -if command -v ccache &>/dev/null; then - echo "Enabling ccache for the build." - export CMAKE_CXX_COMPILER_LAUNCHER=ccache - export CMAKE_C_COMPILER_LAUNCHER=ccache -fi - -if ! [ -f "$LLVM_SRC_DIR/llvm/CMakeLists.txt" ]; then - echo "Expected the path to LLVM to be set correctly (got '$LLVM_SRC_DIR'): can't find CMakeLists.txt" - exit 1 -fi -echo "Using LLVM source dir: $LLVM_SRC_DIR" - -# Setup directories. -echo "Building MLIR in $build_dir" -mkdir -p "$build_dir" - -echo "Beginning build (commands will echo)" -set -x - -[[ "$(uname)" != "Darwin" ]] && LLVM_ENABLE_LLD="ON" || LLVM_ENABLE_LLD="OFF" -cmake -GNinja \ - "-H$LLVM_SRC_DIR/llvm" \ - "-B$build_dir" \ - -DLLVM_INSTALL_UTILS=ON \ - -DLLVM_ENABLE_LLD="$LLVM_ENABLE_LLD" \ - -DLLVM_ENABLE_PROJECTS=mlir \ - -DLLVM_TARGETS_TO_BUILD=host \ - -DLLVM_INCLUDE_TOOLS=ON \ - -DMLIR_ENABLE_BINDINGS_PYTHON="${MLIR_ENABLE_BINDINGS_PYTHON}" \ - -DLLVM_ENABLE_BINDINGS=OFF \ - -DLLVM_VERSION_SUFFIX="" \ - -DCMAKE_PLATFORM_NO_VERSIONED_SONAME:BOOL=ON \ - -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/build_tools/github_actions/ci_build_cmake.sh b/build_tools/github_actions/ci_build_cmake.sh deleted file mode 100755 index 25a6536fa1f..00000000000 --- a/build_tools/github_actions/ci_build_cmake.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# Copyright 2022 The StableHLO Authors. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This file is similar to build_mlir.sh, but passes different flags for -# caching in GitHub Actions. - -# This file gets called on build directory where resources are placed -# during `ci_configure`, and builds stablehlo in the directory specified -# by the second argument. - -set -o errexit -set -o nounset -set -o pipefail - -if [[ $# -ne 2 ]] ; then - echo "Usage: $0 " - exit 1 -fi - -LLVM_BUILD_DIR="$1" -STABLEHLO_BUILD_DIR="$2" -CMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE:-RelWithDebInfo}" - -# Turn on building Python bindings -STABLEHLO_ENABLE_BINDINGS_PYTHON="${STABLEHLO_ENABLE_BINDINGS_PYTHON:-OFF}" -# Turn on building Sanitizers -# Note: This is not congruent with building python bindings -STABLEHLO_ENABLE_SANITIZER="${STABLEHLO_ENABLE_SANITIZER:-OFF}" - -# Configure StableHLO -# CMAKE_PLATFORM_NO_VERSIONED_SONAME Disables generation of "version soname" -# (i.e. libFoo.so.), which causes pure -# duplication of various shlibs for Python wheels. -cmake -GNinja \ - -B"$STABLEHLO_BUILD_DIR" \ - -DLLVM_ENABLE_LLD=ON \ - -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \ - -DLLVM_ENABLE_ASSERTIONS=ON \ - -DMLIR_DIR="$LLVM_BUILD_DIR/lib/cmake/mlir" \ - -DCMAKE_CXX_COMPILER=clang++ \ - -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ - -DCMAKE_C_COMPILER=clang \ - -DCMAKE_C_COMPILER_LAUNCHER=ccache \ - -DSTABLEHLO_ENABLE_STRICT_BUILD=ON \ - -DCMAKE_PLATFORM_NO_VERSIONED_SONAME:BOOL=ON \ - -DSTABLEHLO_ENABLE_SANITIZER="$STABLEHLO_ENABLE_SANITIZER" \ - -DSTABLEHLO_ENABLE_BINDINGS_PYTHON="$STABLEHLO_ENABLE_BINDINGS_PYTHON" - -# Build and Test StableHLO -cd "$STABLEHLO_BUILD_DIR" || exit -ninja check-stablehlo-ci diff --git a/build_tools/github_actions/ci_build_cmake_llvm.sh b/build_tools/github_actions/ci_build_cmake_llvm.sh deleted file mode 100755 index 5bfe5c33928..00000000000 --- a/build_tools/github_actions/ci_build_cmake_llvm.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# Copyright 2022 The StableHLO Authors. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This file is similar to build_mlir.sh, but passes different flags for -# caching in GitHub Actions to improve build speeds. - -set -o errexit -set -o nounset -set -o pipefail - -if [[ $# -ne 2 ]] ; then - echo "Usage: $0 " - exit 1 -fi - -# LLVM source -LLVM_SRC_DIR="$1" -LLVM_BUILD_DIR="$2" - -CMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE:-RelWithDebInfo}" -# Turn on building Python bindings -MLIR_ENABLE_BINDINGS_PYTHON="${MLIR_ENABLE_BINDINGS_PYTHON:-OFF}" - -# Configure LLVM -# LLVM_VERSION_SUFFIX to get rid of that annoying af git on the end of .17git -# CMAKE_PLATFORM_NO_VERSIONED_SONAME Disables generation of "version soname" -# (i.e. libFoo.so.), which causes pure -# duplication of various shlibs for Python wheels. -cmake -GNinja \ - "-H$LLVM_SRC_DIR/llvm" \ - "-B$LLVM_BUILD_DIR" \ - -DLLVM_INSTALL_UTILS=ON \ - -DLLVM_ENABLE_LLD=ON \ - -DLLVM_ENABLE_PROJECTS=mlir \ - -DLLVM_TARGETS_TO_BUILD=host \ - -DLLVM_INCLUDE_TOOLS=ON \ - -DMLIR_ENABLE_BINDINGS_PYTHON="${MLIR_ENABLE_BINDINGS_PYTHON}" \ - -DLLVM_ENABLE_BINDINGS=OFF \ - -DLLVM_BUILD_TOOLS=OFF \ - -DLLVM_INCLUDE_TESTS=OFF \ - -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \ - -DLLVM_ENABLE_ASSERTIONS=On \ - -DLLVM_VERSION_SUFFIX="" \ - -DCMAKE_PLATFORM_NO_VERSIONED_SONAME:BOOL=ON \ - -DCMAKE_CXX_COMPILER=clang++ \ - -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ - -DCMAKE_C_COMPILER=clang \ - -DCMAKE_C_COMPILER_LAUNCHER=ccache - -# Build LLVM/MLIR -cmake --build "$LLVM_BUILD_DIR" --target all