# Copyright 2021-2026, Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

name: SVML

permissions: read-all

on:
  workflow_dispatch:
    inputs:
      flow_type:
        description: 'Workflow flow type (full or smoke)'
        required: true
        default: 'full'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  TARGETS_SMOKE: '["avx2-i32x8"]'
  OPTSETS_SMOKE: "-O2"
  TARGETS_FULL:  '["sse2-i32x4", "sse2-i32x8",
                   "sse4-i8x16", "sse4-i16x8", "sse4-i32x4", "sse4-i32x8",
                   "avx1-i32x4", "avx1-i32x8", "avx1-i32x16", "avx1-i64x4",
                   "avx2-i8x32", "avx2-i16x16", "avx2-i32x4", "avx2-i32x8", "avx2-i32x16", "avx2-i64x4",
                   "avx512skx-x4", "avx512skx-x8", "avx512skx-x16", "avx512skx-x64", "avx512skx-x32"]'
  OPTSETS_FULL: "-O0 -O1 -O2"

jobs:
  define-flow:
    runs-on: ubuntu-latest
    outputs:
      tests_matrix_targets: ${{ steps.set-flow.outputs.matrix }}
      tests_optsets: ${{ steps.set-flow.outputs.optsets }}
      flow_type: ${{ steps.set-flow.outputs.type }}
    env:
      # for debug purposes
      REF_NAME: ${{ github.ref }}
      EVENT_NAME: ${{ github.event_name }}

      # define rule when to run full flow
      RUN_FULL: ${{ (github.ref == 'refs/heads/main' && github.event_name == 'schedule') ||
                    (github.event_name == 'workflow_dispatch' && github.event.inputs.flow_type == 'full') }}

      # define rule when to run smoke flow
      RUN_SMOKE: ${{ github.event_name == 'pull_request' ||
                    (github.event_name == 'workflow_dispatch' && github.event.inputs.flow_type == 'smoke') }}
    steps:
    - name: Set workflow jobs flow
      id: set-flow
      run: |
        # one and only one var should be set
        if [[ "$RUN_SMOKE" == false && "$RUN_FULL" == true ]]; then
          echo "type=full" >> "$GITHUB_OUTPUT"
        elif [[ "$RUN_SMOKE" == true && "$RUN_FULL" == false ]]; then
          echo "type=smoke" >> "$GITHUB_OUTPUT"
        else
          echo "One and only one env var must be set: RUN_SMOKE or RUN_FULL"
          exit 1
        fi
        # set tests matrix depends on flow
        if [[ "$RUN_SMOKE" == true ]]; then
          echo "matrix=${TARGETS_SMOKE}" >> "$GITHUB_OUTPUT"
        fi
        if [[ "$RUN_FULL" == true ]]; then
          echo "matrix=${TARGETS_FULL}" >> "$GITHUB_OUTPUT"
        fi
        # set tests optsets
        if [[ "$RUN_SMOKE" == true ]]; then
          echo "optsets=${OPTSETS_SMOKE}" >> "$GITHUB_OUTPUT"
        fi
        if [[ "$RUN_FULL" == true ]]; then
          echo "optsets=${OPTSETS_FULL}" >> "$GITHUB_OUTPUT"
        fi

  linux-build-ispc-llvm:
    needs: [define-flow]
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
      with:
        submodules: true

    - name: Install dependencies
      run: |
        .github/workflows/scripts/install-build-deps.sh

    - name: Check environment
      run: |
        which -a clang
        cat /proc/cpuinfo

    - name: Build package
      run: |
        .github/workflows/scripts/build-ispc.sh

    - name: Sanity testing (make check-all, make test)
      run: |
        .github/workflows/scripts/check-ispc.sh

    - name: Upload package
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      with:
        name: ispc_llvm_linux
        path: build/ispc-*-linux.tar.gz


  linux-test-llvm:
    needs: [define-flow, linux-build-ispc-llvm]
    runs-on: ubuntu-latest
    continue-on-error: false
    strategy:
      fail-fast: false
      matrix:
        target: ${{fromJson(needs.define-flow.outputs.tests_matrix_targets)}}
    steps:
    - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
    - name: Download package
      uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
      with:
        name: ispc_llvm_linux

    - name: Install dependencies and unpack artifacts
      run: |
        .github/workflows/scripts/install-svml-test-deps.sh

    - name: Check environment
      run: |
        cat /proc/cpuinfo

    - name: Running tests
      run: |
        echo "PATH=$PATH"
        . /opt/intel/oneapi/setvars.sh
        ./scripts/alloy.py -r --only="stability current ${{ needs.define-flow.outputs.tests_optsets }}" --only-targets="${MATRIX_TARGET}" --time --update-errors=FP --ispc-flags="--math-lib=svml" --compiler=icx
      env:
        MATRIX_TARGET: ${{ matrix.target }}

    - name: Check
      run: |
        # Print fails to the log.
        git diff --exit-code

    - name: Upload fail_db.txt
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      if: failure()
      with:
        name: fail_db.llvm21.${{matrix.target}}.txt
        path: fail_db.txt

