name: Validate Operations

env:
  LLVM_COMMIT: 0c2701fe7fa002e1befc5f86c268a7964f96d286
  PROTOBUF_REF: v34.0
  CMAKE_VERSION: 4.3.0
  MOLD_LINKER_FLAGS: -fuse-ld=mold

on:
  push:
  pull_request:

jobs:
  validate-operations:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        run: |
          git clone --depth 1 --recurse-submodules --branch ${GITHUB_REF_NAME} \
            https://chef.heaplab.deib.polimi.it/git/${GITHUB_REPOSITORY}.git \
            ${GITHUB_WORKSPACE}

      - name: Install system dependencies
        run: |
          sudo apt update
          sudo apt install -y cmake ninja-build build-essential mold curl ca-certificates

      - name: Install CMake
        run: |
          ARCH="$(uname -m)"
          case "$ARCH" in
            x86_64) CMAKE_ARCH="linux-x86_64" ;;
            aarch64|arm64) CMAKE_ARCH="linux-aarch64" ;;
            *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
          esac

          curl -fsSL -o /tmp/cmake.sh \
            "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-${CMAKE_ARCH}.sh"
          sudo sh /tmp/cmake.sh --skip-license --prefix=/usr/local
          rm -f /tmp/cmake.sh

          cmake --version
          which cmake

      - name: Prepare MLIR Cache
        uses: ./.github/actions/prepare-mlir-cache
        with:
          llvm-commit: ${{ env.LLVM_COMMIT }}
          mold-linker-flags: ${{ env.MOLD_LINKER_FLAGS }}

      - name: Prepare Protobuf Cache
        uses: ./.github/actions/prepare-protobuf-cache
        with:
          protobuf-ref: ${{ env.PROTOBUF_REF }}
          mold-linker-flags: ${{ env.MOLD_LINKER_FLAGS }}

      - name: Register installed libraries
        run: sudo ldconfig

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install Python dependencies
        run: pip install numpy onnx colorama

      - name: Restore Raptor Build Cache
        id: restore-raptor-build-cache
        uses: ./.github/actions/restore-raptor-build-cache
        with:
          key: raptor-build-${{ runner.os }}-${{ github.ref_name }}-${{ env.LLVM_COMMIT }}-${{ env.PROTOBUF_REF }}-${{ env.CMAKE_VERSION }}-${{ github.sha }}
          restore-keys: |
            raptor-build-${{ runner.os }}-${{ github.ref_name }}-${{ env.LLVM_COMMIT }}-${{ env.PROTOBUF_REF }}-${{ env.CMAKE_VERSION }}-
            raptor-build-${{ runner.os }}-${{ env.LLVM_COMMIT }}-${{ env.PROTOBUF_REF }}-${{ env.CMAKE_VERSION }}-

      - name: Build Raptor
        id: build-raptor
        run: |
          MLIR_DIR=$(pwd)/onnx-mlir/llvm-project/build/lib/cmake/mlir
          cmake -S . -B build -G Ninja \
            -DCMAKE_BUILD_TYPE=Debug \
            -DONNX_MLIR_ACCELERATORS=PIM \
            -DLLVM_ENABLE_ASSERTIONS=ON \
            -DMLIR_DIR=${MLIR_DIR} \
            -DCMAKE_EXE_LINKER_FLAGS="${MOLD_LINKER_FLAGS}" \
            -DCMAKE_SHARED_LINKER_FLAGS="${MOLD_LINKER_FLAGS}" \
            -DCMAKE_MODULE_LINKER_FLAGS="${MOLD_LINKER_FLAGS}"
          cmake --build build

      - name: Save Raptor Build Cache
        if: steps.build-raptor.outcome == 'success' && steps.restore-raptor-build-cache.outputs.cache-hit != 'true'
        uses: ./.github/actions/save-raptor-build-cache
        with:
          key: raptor-build-${{ runner.os }}-${{ github.ref_name }}-${{ env.LLVM_COMMIT }}-${{ env.PROTOBUF_REF }}-${{ env.CMAKE_VERSION }}-${{ github.sha }}

      - name: Run validation
        run: |
          python validation/validate.py \
            --raptor-path build/Debug/bin/onnx-mlir \
            --onnx-include-dir onnx-mlir/include
