name: MLIR

on:
  push:
    branches:
    - main
  pull_request:
    branches:
    - main
  merge_group:

concurrency:
  # Skip intermediate builds: always.
  # Cancel intermediate builds: only if it is a pull request build.
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}

jobs:
  build-linux:
    name: MLIR ${{ matrix.build }} ${{ matrix.os }}
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        build: ["Release", "Debug"] # "RelWithDebInfo"
        llbuild: ["Release"]
        os: [linux-x86-n2-32]

    timeout-minutes: 500

    container:
      image: ${{ (contains(matrix.os, 'linux') && 'ghcr.io/enzymead/reactant-docker-images@sha256:91e1edb7a7c869d5a70db06e417f22907be0e67ca86641d48adcea221fedc674' ) || '' }}

    steps:
    - name: Install dependencies
      run: |
          apt-get update
          apt-get install -y binutils ninja-build cmake gcc g++ python3 python3-dev

    - uses: actions/checkout@v4
      with:
        path: 'Enzyme'

    - uses: actions/checkout@v4
      with:
        repository: 'llvm/llvm-project'
        ref: '2bc22ea02edda5926f3e53f141def9bf212ac1db'
        path: 'llvm-project'

    - name: Set BASE_DIR
      # We have to use `${GITHUB_WORKSPACE}` instead of `github.workspace` because GitHub
      # is terrible and the two don't match inside containers:
      # https://github.com/actions/runner/issues/2058
      run: |
        BASE_DIR=${GITHUB_WORKSPACE}
        # Make sure this directory exists, for good measure
        ls -lhrt "${BASE_DIR}"/Enzyme
        ls -lhrt "${BASE_DIR}"/llvm-project
        echo "BASE_DIR=${BASE_DIR}" >> ${GITHUB_ENV}

    - name: Get MLIR commit hash
      id: mlir-commit
      working-directory: 'llvm-project'
      run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

    - name: Cache MLIR
      id: cache-mlir
      uses: actions/cache@v4
      with:
        path: ${{ env.BASE_DIR }}/llvm-project/mlir-build
        key: ${{ matrix.llbuild }}-${{ matrix.os }}-mlir-${{ steps.mlir-commit.outputs.sha_short }}

    - name: MLIR build
      if: steps.cache-mlir.outputs.cache-hit != 'true'
      working-directory: ${{ env.BASE_DIR }}/'llvm-project'
      run: |
        mkdir mlir-build && cd mlir-build && cmake ../llvm -GNinja -DLLVM_ENABLE_PROJECTS="llvm;clang;mlir" -DLLVM_ENABLE_RUNTIMES=openmp -DCMAKE_BUILD_TYPE=${{ matrix.llbuild }} \
        -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_TARGETS_TO_BUILD=X86 \
        -DLLVM_USE_LINKER=gold -DLLVM_PARALLEL_LINK_JOBS=2 \
        -DCLANG_ENABLE_STATIC_ANALYZER=OFF -DCLANG_ENABLE_ARCMT=OFF \
        -DLLVM_OPTIMIZED_TABLEGEN=ON && ninja || echo "already built"

    - name: Enzyme build
      working-directory: ${{ env.BASE_DIR }}/'Enzyme'
      run: |
          mkdir enzyme-build && cd enzyme-build
          cmake ../enzyme -GNinja -DCMAKE_BUILD_TYPE=${{ matrix.build }} -DLLVM_DIR=${{ env.BASE_DIR }}/llvm-project/mlir-build -DENZYME_MLIR=ON
          ninja

    - name: Check enzyme-mlir
      working-directory: 'Enzyme/enzyme-build'
      run: ninja check-enzymemlir

