name: HIP quality check

on:
  workflow_dispatch: # allows manual triggering
  push:
    branches:
      - master
    paths: [
      '.github/workflows/hip-quality-check.yml',
      '**/*.cu',
      '**/*.cuh',
      'scripts/hip/gcn-cdna-vgpr-check.py'
    ]

  pull_request:
    types: [opened, synchronize, reopened]
    paths: [
      '.github/workflows/hip-quality-check.yml',
      '**/*.cu',
      '**/*.cuh',
      'scripts/hip/gcn-cdna-vgpr-check.py'
    ]

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}
  cancel-in-progress: true

env:
  GGML_NLOOP: 3
  GGML_N_THREADS: 1
  LLAMA_ARG_LOG_COLORS: 1
  LLAMA_ARG_LOG_PREFIX: 1
  LLAMA_ARG_LOG_TIMESTAMPS: 1

jobs:
  ubuntu-22-hip-quality-check:
    runs-on: ubuntu-22.04
    container: rocm/dev-ubuntu-22.04:7.2.1
    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@v6

      - name: Dependencies
        id: depends
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential git cmake rocblas-dev hipblas-dev libssl-dev python3

      - name: ccache
        uses: ggml-org/ccache-action@v1.2.21
        with:
          key: hip-quality-check-ubuntu-22.04
          evict-old-files: 1d
          save: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}

      - name: Build with Werror
        id: cmake_build
        run: |
          cmake -B build -S . \
            -DCMAKE_HIP_COMPILER="$(hipconfig -l)/clang" \
            -DGPU_TARGETS=gfx942 \
            -DGGML_HIP=ON \
            -DGGML_HIP_EXPORT_METRICS=Off \
            -DCMAKE_HIP_FLAGS="-Werror -Wno-tautological-compare" \
            -DCMAKE_BUILD_TYPE=Release
          cd build
          make -j $(nproc)

      - name: Check for major VGPR spills
        id: vgpr_check
        run: |
          cmake -B build -S . \
            -DCMAKE_HIP_COMPILER="$(hipconfig -l)/clang" \
            -DGPU_TARGETS=gfx908 \
            -DGGML_HIP=ON \
            -DGGML_HIP_EXPORT_METRICS=On \
            -DCMAKE_HIP_FLAGS="" \
            -DCMAKE_BUILD_TYPE=Release
          cd build
          make -j $(nproc) 2>&1 | tee metrics.log | grep -v 'Rpass-analysis=kernel-resource-usage\|remark:\|^$'
          python3 ../scripts/hip/gcn-cdna-vgpr-check.py metrics.log
