From e61ea78b2d555ace786a9af286f1f3268519a89a Mon Sep 17 00:00:00 2001 From: Vladimir Milosevic <157983820+vmilosevic@users.noreply.github.com> Date: Thu, 8 Aug 2024 12:59:28 +0200 Subject: [PATCH] Building inside docker container (#293) Building inside a docker container Adding support for running CI pipelines inside a docker container Added - Dockerfile based on Ubuntu 22.04 - Docker installs dependencies and prebuilds toolchain - Workflow for generating docker image - Workflow that runs build and tests using the docker container --- .github/Dockerfile | 90 +++++++++++++++++++ .github/workflows/build-image.yml | 37 ++++++++ .github/workflows/docker-build.yml | 90 +++++++++++++++++++ .../workflows/{build.yml => macos-build.yml} | 3 +- .../{on-pr-and-push-to-main.yml => on-pr.yml} | 11 +-- .github/workflows/on-push.yml | 20 +++++ docs/src/internal-build.md | 63 ++++++++++++- 7 files changed, 304 insertions(+), 10 deletions(-) create mode 100644 .github/Dockerfile create mode 100644 .github/workflows/build-image.yml create mode 100644 .github/workflows/docker-build.yml rename .github/workflows/{build.yml => macos-build.yml} (95%) rename .github/workflows/{on-pr-and-push-to-main.yml => on-pr.yml} (60%) create mode 100644 .github/workflows/on-push.yml diff --git a/.github/Dockerfile b/.github/Dockerfile new file mode 100644 index 000000000..13d3b66d4 --- /dev/null +++ b/.github/Dockerfile @@ -0,0 +1,90 @@ +FROM ubuntu:22.04 +SHELL ["/bin/bash", "-c"] + +# Set environment variables +ENV DEBIAN_FRONTEND=noninteractive +ENV TTMLIR_TOOLCHAIN_DIR=/opt/ttmlir-toolchain + +# Install dependencies +RUN apt-get update && apt-get install -y \ + software-properties-common \ + build-essential \ + python3-dev \ + python3-venv \ + python3-pip \ + git \ + git-lfs \ + libhwloc-dev \ + pandoc \ + libtbb-dev \ + libcapstone-dev \ + pkg-config \ + linux-tools-generic \ + ninja-build \ + wget \ + libgtest-dev \ + cmake \ + ccache \ + doxygen \ + graphviz \ + patchelf \ + libyaml-cpp-dev \ + libboost-all-dev + +# Install clang 17 +RUN wget https://apt.llvm.org/llvm.sh && \ + chmod u+x llvm.sh && \ + ./llvm.sh 17 && \ + apt install -y libc++-17-dev libc++abi-17-dev && \ + ln -s /usr/bin/clang-17 /usr/bin/clang && \ + ln -s /usr/bin/clang++-17 /usr/bin/clang++ + +# Install python packages +RUN pip install cmake + +# Install Googletest +RUN git clone https://github.com/google/googletest.git -b release-1.12.1 && \ + cd googletest && \ + mkdir build && \ + cd build && \ + cmake .. -DBUILD_GMOCK=OFF && \ + make && \ + make install && \ + cd ../.. && \ + rm -rf googletest + +# Create a directory for the build and toolchain +ARG BUILD_DIR=/home/build +RUN mkdir -p $BUILD_DIR && \ + mkdir -p $TTMLIR_TOOLCHAIN_DIR + +# Clone the project and update submodules +RUN git clone https://github.com/tenstorrent/tt-mlir.git $BUILD_DIR/tt-mlir && \ + cd $BUILD_DIR/tt-mlir && \ + git submodule update --init --recursive -f + +# Build the toolchain +WORKDIR $BUILD_DIR/tt-mlir +RUN cmake -B env/build env && \ + cmake --build env/build + +# Build project to test the container +RUN source env/activate && \ + cmake -G Ninja \ + -B build \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_C_COMPILER=clang \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DTTMLIR_ENABLE_RUNTIME=ON \ + -DTTMLIR_ENABLE_RUNTIME_TESTS=ON && \ + cmake --build build --config Release + +# Run clang-tidy +RUN cmake --build build -- clang-tidy || true + +# Run the tests +RUN cmake --build build -- check-ttmlir || true + +# Clean up the build directory +RUN rm -rf $BUILD_DIR/tt-mlir diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml new file mode 100644 index 000000000..66e3990f4 --- /dev/null +++ b/.github/workflows/build-image.yml @@ -0,0 +1,37 @@ +name: Build and Publish Docker Image + +on: + workflow_dispatch: + workflow_call: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + sparse-checkout: | + .github/Dockerfile + sparse-checkout-cone-mode: false + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: .github + file: .github/Dockerfile + push: true + tags: | + ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:${{ github.sha }} + ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:latest diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 000000000..fb1724c97 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,90 @@ +name: Build in Docker + +on: + workflow_dispatch: + workflow_call: + +jobs: + + build-and-test: + + strategy: + fail-fast: false + matrix: + image: ["ubuntu-22-04"] + build: [ + {runs-on: ubuntu-latest, build_type: Release, enable_runtime: OFF}, + {runs-on: self-hosted, build_type: Release, enable_runtime: ON}, + ] + + runs-on: ${{ matrix.build.runs-on }} + + container: + image: ghcr.io/${{ github.repository }}/tt-mlir-${{ matrix.image }}:latest + options: --user root + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch all history and tags + + - name: Set reusable strings + id: strings + shell: bash + run: | + echo "work-dir=$(pwd)" >> "$GITHUB_OUTPUT" + echo "build-output-dir=$(pwd)/build" >> "$GITHUB_OUTPUT" + + - name: Git safe dir + run: git config --global --add safe.directory ${{ steps.strings.outputs.work-dir }} + + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + create-symlink: true + key: ${{ matrix.build.runs-on }}-runtime-${{ matrix.build.enable_runtime }}-${{ env.SDK_VERSION }} + + - name: Configure CMake + shell: bash + run: | + source env/activate + cmake -G Ninja \ + -B ${{ steps.strings.outputs.build-output-dir }} \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_C_COMPILER=clang \ + -DCMAKE_BUILD_TYPE=${{ matrix.build.build_type }} \ + -DTTMLIR_ENABLE_RUNTIME=${{ matrix.build.enable_runtime }} \ + -DTTMLIR_ENABLE_RUNTIME_TESTS=${{ matrix.build.enable_runtime }} \ + -S ${{ steps.strings.outputs.work-dir }} + + - name: Build + shell: bash + run: | + source env/activate + cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }} + + - name: Lint + shell: bash + run: | + source env/activate + cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }} -- clang-tidy + continue-on-error: true + + - name: Run Test + shell: bash + run: | + source env/activate + cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }} -- check-ttmlir + + - name: Upload Test Report + uses: actions/upload-artifact@v4 + with: + name: test-reports-${{ matrix.build.runs-on }} + path: build/test/report.xml + + - name: Show Test Report + uses: mikepenz/action-junit-report@v4 + if: success() || failure() + with: + report_paths: build/test/report.xml + check_name: MLIR Tests diff --git a/.github/workflows/build.yml b/.github/workflows/macos-build.yml similarity index 95% rename from .github/workflows/build.yml rename to .github/workflows/macos-build.yml index fc6fb1c3f..82c464244 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/macos-build.yml @@ -1,4 +1,4 @@ -name: Build +name: Build on macos-latest on: workflow_dispatch: @@ -13,7 +13,6 @@ jobs: fail-fast: false matrix: build: [ - {runs-on: ubuntu-20.04, c_compiler: clang, cpp_compiler: clang++, build_type: Release, enable_runtime: ON}, {runs-on: macos-latest, c_compiler: clang, cpp_compiler: clang++, build_type: Release, enable_runtime: OFF} ] runs-on: ${{ matrix.build.runs-on }} diff --git a/.github/workflows/on-pr-and-push-to-main.yml b/.github/workflows/on-pr.yml similarity index 60% rename from .github/workflows/on-pr-and-push-to-main.yml rename to .github/workflows/on-pr.yml index a4786bd91..05caa505c 100644 --- a/.github/workflows/on-pr-and-push-to-main.yml +++ b/.github/workflows/on-pr.yml @@ -1,9 +1,7 @@ -name: On PR and push to main +name: On PR on: workflow_dispatch: - push: - branches: [ "main" ] pull_request: branches: [ "main" ] @@ -14,6 +12,9 @@ jobs: spdx: uses: ./.github/workflows/spdx.yml secrets: inherit - build: - uses: ./.github/workflows/build.yml + macos-build: + uses: ./.github/workflows/macos-build.yml + secrets: inherit + docker-build: + uses: ./.github/workflows/docker-build.yml secrets: inherit diff --git a/.github/workflows/on-push.yml b/.github/workflows/on-push.yml new file mode 100644 index 000000000..a6a0e24b4 --- /dev/null +++ b/.github/workflows/on-push.yml @@ -0,0 +1,20 @@ +name: On push + +on: + workflow_dispatch: + push: + branches: [ "main" ] + +jobs: + pre-commit: + uses: ./.github/workflows/pre-commit.yml + secrets: inherit + spdx: + uses: ./.github/workflows/spdx.yml + secrets: inherit + macos-build: + uses: ./.github/workflows/macos-build.yml + secrets: inherit + docker-build: + uses: ./.github/workflows/docker-build.yml + secrets: inherit diff --git a/docs/src/internal-build.md b/docs/src/internal-build.md index f0be46023..f6a68011b 100644 --- a/docs/src/internal-build.md +++ b/docs/src/internal-build.md @@ -1,9 +1,66 @@ # Internal Build Notes / IRD -- When building the runtime we must use Ubuntu 20.04 docker image +- When building the runtime we must use Ubuntu 22.04 docker image - When making an IRD reservation use `--docker-image - yyz-gitlab.local.tenstorrent.com:5005/tenstorrent/infra/ird-ubuntu-20-04-amd64:latest` -- You'll have to manaully install a newer version of cmake, at least 3.20, the easiest way to do this is to `pip install cmake` and make sure this one is in your path + yyz-gitlab.local.tenstorrent.com:5005/tenstorrent/infra/ird-ubuntu-22-04-amd64:latest` +- You'll have to manaully install a newer version of cmake, at least 3.22, the easiest way to do this is to `pip install cmake` and make sure this one is in your path - You'll want LLVM installation to persist IRD reservations, you can achieve this by: - mkdir /localdev/$USER/ttmlir-toolchain - When requesting an IRD use `--volumes /localdev/$USER/ttmlir-toolchain:/opt/ttmlir-toolchain` + +## Working with Docker Images + +Components: + - Dockerfile + - Workflow for building Docker image + - Project build using Docker image + +Overview: + +The [Dockerfile](.github/Dockerfile) describes how to create an image for building the tt-mlir project file. It starts with a supported base image (Ubuntu 22.04) and installs the necessary packages. The purpose of the Docker build is to: + + - Set up build dependencies + - Prepare the tt-mlir toolchain + +During the Docker build, the project is built and tests are run to ensure that everything is set up correctly. If any dependencies are missing, the Docker build will fail. + +This process also prepopulates caches for Python packages and the ccache cache in the image, which should make subsequent builds faster. + +### Building the Docker Image using GitHub Actions + +The GitHub Actions workflow [Build and Publish Docker Image](.github/workflows/build-image.yml) builds the Docker image and uploads it to GitHub Packages at https://github.com/orgs/tenstorrent/packages?repo_name=tt-mlir. The image name is tt-mlir-ubuntu-22-04, and we use the git SHA we build from as the tag. + +### Building the Docker Image Locally + +To test the changes and build the image locally, use the following command: +```bash +docker build -f .github/Dockerfile -t tt-mlir-ubuntu-22-04:latest . +``` + +### Pushing the Docker Image to GitHub + +Images built locally can be pushed to GitHub. First, we need to generate a PAT token with the "write:packages" access enabled. Go to GitHub -> Settings -> Developer settings -> Personal access tokens -> Generate new token. + +Authenticate with GitHub Container Registry: +```bash +echo "" | docker login ghcr.io -u --password-stdin +``` + +Add a tag to the built image: +```bash +docker tag tt-mlir-ubuntu-22-04:latest ghcr.io/tenstorrent/tt-mlir/tt-mlir-ubuntu-22-04:latest +``` + +Push the image: +```bash +docker push ghcr.io/tenstorrent/tt-mlir/tt-mlir-ubuntu-22-04:latest +``` + +### Using the Image in GitHub Actions Jobs + +The GitHub Actions workflow [Build in Docker](.github/workflows/docker-build.yml) uses a Docker container for building: +```yaml + container: + image: ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:latest + options: --user root +```