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

# Nightly Linux run.

name: Nightly Linux tests / LLVM trunk

permissions: read-all

# Run daily - test sse2-avx512 targets @ -O0/-O1/-O2
on:
  schedule:
    - cron:  '0 7 * * *'
  push:
    branches:
      - '**test_nightly**'
  workflow_dispatch:
    inputs:
      llvm_sha:
        description: 'Specific LLVM commit SHA to build (leave empty for trunk HEAD)'
        required: false
        type: string

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

jobs:
  define-targets:
    # Only run in the main ispc/ispc repo to avoid unnecessary scheduled runs on forks.
    if: github.repository == 'ispc/ispc'
    runs-on: ubuntu-latest
    outputs:
      # TODO: restore avx2vnni targets when SDE problem is fixed
      targets: >-
        ["generic-i1x4", "generic-i1x8", "generic-i1x16", "generic-i1x32",
         "generic-i8x16", "generic-i8x32",
         "generic-i16x8", "generic-i16x16",
         "generic-i32x4", "generic-i32x8", "generic-i32x16",
         "generic-i64x4",
         "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",
         "avx512icl-x4", "avx512icl-x8", "avx512icl-x16", "avx512icl-x64", "avx512icl-x32",
         "avx512spr-x4", "avx512spr-x8", "avx512spr-x16", "avx512spr-x64", "avx512spr-x32",
         "avx512gnr-x4", "avx512gnr-x8", "avx512gnr-x16", "avx512gnr-x64", "avx512gnr-x32",
         "avx10.2dmr-x4", "avx10.2dmr-x8", "avx10.2dmr-x16", "avx10.2dmr-x64", "avx10.2dmr-x32"]
      llvm_sha: ${{ steps.get-llvm-sha.outputs.llvm_sha }}
    steps:
      - name: Get LLVM SHA
        id: get-llvm-sha
        env:
          INPUT_LLVM_SHA: ${{ inputs.llvm_sha }}
        run: |
          if [ -n "$INPUT_LLVM_SHA" ]; then
            LLVM_SHA="$INPUT_LLVM_SHA"
            echo "Using provided LLVM SHA: $LLVM_SHA"
          else
            LLVM_SHA=$(git ls-remote https://github.com/llvm/llvm-project.git refs/heads/main | cut -f1)
            echo "Fetched LLVM main HEAD: $LLVM_SHA"
          fi
          echo "llvm_sha=$LLVM_SHA" >> "$GITHUB_OUTPUT"
          echo "$LLVM_SHA" > llvm-commit-sha.txt

      - name: Upload LLVM SHA artifact
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: llvm_commit_sha
          path: llvm-commit-sha.txt

  linux-build-llvm-trunk:
    needs: [define-targets]
    runs-on: ubuntu-22.04

    steps:
    - name: Free disk space
      uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
      with:
        tool-cache: false
        android: true
        dotnet: true
        haskell: true
        large-packages: true
        docker-images: true
        swap-storage: true

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

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

    - name: Build LLVM
      run: |
        cd docker/ubuntu/22.04/cpu_ispc_build
        ls -al
        docker buildx create --use
        docker buildx build --tag ispc/ubuntu22.04 --target=llvm_build --build-arg REPO="${GITHUB_REPOSITORY}" --build-arg SHA="${GITHUB_SHA}" --build-arg LLVM_VERSION=trunk --build-arg LLVM_SHA="${{ needs.define-targets.outputs.llvm_sha }}" --output=type=tar,dest=result.tar .

    - name: Pack LLVM
      run: |
        cd docker/ubuntu/22.04/cpu_ispc_build
        tar xvf result.tar usr/local/src/llvm
        mv usr/local/src/llvm/bin-trunk .
        # Note using gzip here, instead of xz - trading of space for speed, as it's just for passing to another stage.
        tar czvf llvm-trunk-ubuntu22.04-Release+Asserts-x86.arm.wasm.tar.gz bin-trunk

    - name: Upload package
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
      with:
        name: llvm_trunk_linux
        path: docker/ubuntu/22.04/cpu_ispc_build/llvm-trunk-ubuntu22.04-Release+Asserts-x86.arm.wasm.tar.gz

  linux-build-ispc-llvm-trunk:
    needs: [linux-build-llvm-trunk]
    runs-on: ubuntu-22.04

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

    - name: Download package
      uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
      with:
        name: llvm_trunk_linux

    - name: Install dependencies
      env:
        LLVM_TAR: llvm-trunk-ubuntu22.04-Release+Asserts-x86.arm.wasm.tar.gz
        LLVM_VERSION: trunk
      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_trunk_linux
        path: build/ispc-trunk-linux.tar.gz

  linux-test-llvm-trunk:
    needs: [define-targets, linux-build-ispc-llvm-trunk]
    strategy:
      fail-fast: false
      matrix:
        arch: [x86, x86-64]
    uses: ./.github/workflows/reusable.ispc.test.yml
    with:
      platform: linux
      architecture: ${{ matrix.arch }}
      artifact_name: ispc_llvm_trunk_linux
      targets: ${{ needs.define-targets.outputs.targets }}
      optsets: -O0 -O1 -O2
