diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000000..a74fda4b673 --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +AlwaysBreakTemplateDeclarations: Yes diff --git a/.github/actions/setup-build/action.yml b/.github/actions/setup-build/action.yml new file mode 100644 index 00000000000..70dcae5e662 --- /dev/null +++ b/.github/actions/setup-build/action.yml @@ -0,0 +1,45 @@ +# Copyright 2022 The StableHLO Authors. + +# The setup-build action gets everything needed by buildAndTest into the workspace. +# This checks out the proper revision of the LLVM repository, installs ninja, and sets +# up the ccache for faster re-compilation. +name: "Setup build environment (ninja, ccache, llvm, lld)" + +inputs: + llvm-ref: + description: | + LLVM Commit Ref for checkout and build. Used for ccache value and checkout. + required: true + + +runs: + # Based on https://github.com/llvm/torch-mlir/blob/main/.github/actions/setup-build/action.yml + using: "composite" + + steps: + # Checkout llvm at revision specified in input argument. + - uses: actions/checkout@v2 + with: + repository: llvm/llvm-project + ref: ${{ inputs.llvm-ref }} + path: llvm-project + fetch-depth: 1 + + # Get ninja for cmake build. + - name: Install Ninja + uses: llvm/actions/install-ninja@55d844821959226fab4911f96f37071c1d4c3268 + + # Get LLD + - name: Install LLD + if: "runner.os == 'Linux'" + shell: bash + run: | + sudo apt-get install -y lld + + # Setup C++ caching using ccache. + # Cache key is a combination of OS arch and LLVM ref. + - name: Ccache for C++ compilation + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ runner.os }}-stablehlo_build_assets-${{ inputs.llvm-ref }} + max-size: 4G diff --git a/.github/workflows/buildAndTest.yml b/.github/workflows/buildAndTest.yml new file mode 100644 index 00000000000..945db205904 --- /dev/null +++ b/.github/workflows/buildAndTest.yml @@ -0,0 +1,71 @@ +# Copyright 2022 The StableHLO Authors. + +name: Build and Test + +on: + pull_request: + branches: [ main ] + push: + branches: [ main ] + schedule: + # Always regenerate once every day + - cron: '* 12 * * *' + workflow_dispatch: + +# Ensure that only a single job or workflow using the same +# concurrency group will run at a time. This would cancel +# any in-progress jobs in the same github workflow and github +# ref (e.g. refs/heads/main or refs/pull//merge). +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +# Build using CMake and run tests. +# Use Cached LLVM to improve build times. +# Current setup only builds on linux. +jobs: + build-test: + strategy: + fail-fast: false + matrix: + os-arch: [maci-x86_64, ubuntu-x86_64] + include: + - os-arch: ubuntu-x86_64 + os: ubuntu-22.04 + llvm-build-dir: "$GITHUB_WORKSPACE/llvm-build" + stablehlo-build-dir: "$GITHUB_WORKSPACE/build" + - os-arch: maci-x86_64 + os: macos-latest + llvm-build-dir: "$GITHUB_WORKSPACE/llvm-build-maci" + stablehlo-build-dir: "$GITHUB_WORKSPACE/build-maci" + runs-on: ${{ matrix.os }} + + steps: + + # Check out stablehlo repo + - name: Checkout stablehlo + uses: actions/checkout@v2 + + # Read llvm revision from `build_tools/llvm_version.txt` + - name: Get LLVM Revision + id: llvm-revision + shell: bash + run: | + echo "::set-output name=revision::$(cat ${{ github.workspace }}/build_tools/llvm_version.txt)" + + # Check out LLVM, download Ninja, setup ccache + - name: Setup workspace + uses: ./.github/actions/setup-build + with: + llvm-ref: ${{ steps.llvm-revision.outputs.revision }} + + # Configuration for linux + - name: Configure and Build LLVM os-arch='${{ matrix.os-arch }}' + shell: bash + run: | + ./build_tools/github_actions/ci_build_llvm.sh "$GITHUB_WORKSPACE/llvm-project/" ${{ matrix.llvm-build-dir }} + + # Configure for linux + - name: Build and Test StableHLO os-arch='${{ matrix.os-arch }}' + run: | + ./build_tools/github_actions/ci_build_stablehlo.sh ${{ matrix.llvm-build-dir }} ${{ matrix.stablehlo-build-dir }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000000..00c8b87b2ec --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,25 @@ +# Copyright 2022 The StableHLO Authors. + +name: Lint + +on: [pull_request] + +jobs: + # Based on https://github.com/iree-org/iree/blob/main/.github/workflows/lint.yml + clang-format: + if: "github.event_name == 'pull_request'" + runs-on: ubuntu-latest + steps: + - name: Installing dependencies + run: | + wget https://raw.githubusercontent.com/llvm-mirror/clang/master/tools/clang-format/git-clang-format -O /tmp/git-clang-format + chmod +x /tmp/git-clang-format + - name: Checking out repository + uses: actions/checkout@v2 + - name: Fetching Base Branch + # We have to explicitly fetch the base branch as well + run: git fetch --no-tags --prune --depth=1 origin "${GITHUB_BASE_REF?}:${GITHUB_BASE_REF?}" + - name: Running clang-format on changed source files + run: | + /tmp/git-clang-format "${GITHUB_BASE_REF?}" --style=google + git diff --exit-code diff --git a/build_tools/github_actions/ci_build_llvm.sh b/build_tools/github_actions/ci_build_llvm.sh new file mode 100755 index 00000000000..9c769a96398 --- /dev/null +++ b/build_tools/github_actions/ci_build_llvm.sh @@ -0,0 +1,61 @@ +#!/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 +# cacheing in GitHub Actions to improve build speeds. + +if [[ $# -ne 2 ]] ; then + echo "Usage: $0 " + exit 1 +fi + +# LLVM source +LLVM_SRC_DIR="$1" +LLVM_BUILD_DIR="$2" + +# Check for LLD +FLAGS="" +LLD_FLAG="-DLLVM_ENABLE_LLD=ON" +if command -v lld &> /dev/null +then + echo "lld found, appending flag '$LLD_FLAG'" + FLAGS="$LLD_FLAG" +else + echo "lld not found, using default linker" +fi + +# Call cmake +echo "Using additional flags: '$FLAGS'" + +cmake -GNinja \ + "$FLAGS" \ + "-B$LLVM_BUILD_DIR" \ + "-H$LLVM_SRC_DIR/llvm" \ + -DCMAKE_BUILD_TYPE=RelWithDebInfo \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DCMAKE_C_COMPILER=clang \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DLLVM_BUILD_TOOLS=OFF \ + -DLLVM_ENABLE_ASSERTIONS=On \ + -DLLVM_ENABLE_BINDINGS=OFF \ + -DLLVM_ENABLE_PROJECTS=mlir \ + -DLLVM_INCLUDE_TESTS=OFF \ + -DLLVM_INCLUDE_TOOLS=ON \ + -DLLVM_INSTALL_UTILS=ON \ + -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" + +# Build LLVM/MLIR +cmake --build "$LLVM_BUILD_DIR" --target all --target mlir-cpu-runner diff --git a/build_tools/github_actions/ci_build_stablehlo.sh b/build_tools/github_actions/ci_build_stablehlo.sh new file mode 100755 index 00000000000..fe11c1978a0 --- /dev/null +++ b/build_tools/github_actions/ci_build_stablehlo.sh @@ -0,0 +1,55 @@ +#!/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 +# cacheing 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. + +if [[ $# -ne 2 ]] ; then + echo "Usage: $0 " + exit 1 +fi + +LLVM_BUILD_DIR="$1" +STABLEHLO_BUILD_DIR="$2" + +# Check for LLD +FLAGS="" +LLD_FLAG="-DLLVM_ENABLE_LLD=ON" +if command -v lld &> /dev/null +then + echo "lld found, appending flag '$LLD_FLAG'" + FLAGS="$LLD_FLAG" +else + echo "lld not found, using default linker" +fi + +# Build StableHLO +cmake -GNinja \ + "$FLAGS" \ + -B"$STABLEHLO_BUILD_DIR" \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DCMAKE_C_COMPILER=clang \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DCMAKE_BUILD_TYPE=Release \ + -DLLVM_ENABLE_ASSERTIONS=On \ + -DMLIR_DIR="$LLVM_BUILD_DIR/lib/cmake/mlir" + +cd "$STABLEHLO_BUILD_DIR" +ninja check-stablehlo diff --git a/stablehlo/dialect/StablehloOps.cpp b/stablehlo/dialect/StablehloOps.cpp index a24c5c2f85e..7483aa6fd3b 100644 --- a/stablehlo/dialect/StablehloOps.cpp +++ b/stablehlo/dialect/StablehloOps.cpp @@ -1855,8 +1855,7 @@ LogicalResult verifyCollectivePermuteSourceTargetPairs( } LogicalResult CollectivePermuteOp::verify() { - return verifyCollectivePermuteSourceTargetPairs(*this, - source_target_pairs()); + return verifyCollectivePermuteSourceTargetPairs(*this, source_target_pairs()); } //===----------------------------------------------------------------------===//