parameters:
- name: componentName
  type: string
  default: MIOpen
- name: checkoutRepo
  type: string
  default: 'self'
- name: checkoutRef
  type: string
  default: ''
# monorepo related parameters
- name: sparseCheckoutDir
  type: string
  default: ''
- name: triggerDownstreamJobs
  type: boolean
  default: false
- name: downstreamAggregateNames
  type: string
  default: ''
- name: buildDependsOn
  type: object
  default: null
- name: unifiedBuild
  type: boolean
  default: false
# set to true if doing full build of ROCm stack
# and dependencies are pulled from same pipeline
- name: aggregatePipeline
  type: boolean
  default: false
- name: aptPackages
  type: object
  default:
    - cmake
    - jq
    - libdrm-dev
    - libmsgpack-dev
    - libsqlite3-dev
    - libstdc++-12-dev
    - ninja-build
    - python3-pip
    - python3-venv
    - software-properties-common
    - zip
- name: pipModules
  type: object
  default:
    - cget
- name: rocmDependencies
  type: object
  default:
    - half
    - rocRAND
    - rocBLAS
    - hipBLAS
    - hipBLASLt
    - hipBLAS-common
    - rocm-cmake
    - llvm-project
    - ROCR-Runtime
    - rocprofiler-register
    - clr
    - rocminfo
    - roctracer
- name: rocmTestDependencies
  type: object
  default:
    - clr
    - half
    - hipBLAS
    - hipBLAS-common
    - hipBLASLt
    - llvm-project
    - rocBLAS
    - rocm-cmake
    - rocminfo
    - ROCR-Runtime
    - rocprofiler-register
    - rocRAND
    - roctracer

- name: jobMatrix
  type: object
  default:
    buildJobs:
      - gfx942:
        target: gfx942
      - gfx90a:
        target: gfx90a
    testJobs:
      - gfx942:
        target: gfx942
      - gfx90a:
        target: gfx90a
- name: downstreamComponentMatrix
  type: object
  default:
    - MIVisionX:
      name: MIVisionX
      checkoutRepo: mivisionx_repo
      sparseCheckoutDir: ''
      skipUnifiedBuild: 'false'
      buildDependsOn:
        - MIOpen_build
    - AMDMIGraphX:
      name: AMDMIGraphX
      checkoutRepo: amdmigraphx_repo
      sparseCheckoutDir: ''
      skipUnifiedBuild: 'false'
      buildDependsOn:
        - MIOpen_build

jobs:
- ${{ each job in parameters.jobMatrix.buildJobs }}:
  - job: ${{ parameters.componentName }}_build_ubuntu2204_${{ job.target }}
    ${{ if parameters.buildDependsOn }}:
      dependsOn:
        - ${{ each build in parameters.buildDependsOn }}:
          - ${{ build }}_ubuntu2204_${{ job.target }}
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: ROCM_PATH
      value: $(Agent.BuildDirectory)/rocm
    pool: ${{ variables.MEDIUM_BUILD_POOL }}
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-cmake-custom.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
        sparseCheckoutDir: ${{ parameters.sparseCheckoutDir }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/miopen-get-ck-build.yml
      parameters:
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        gpuTarget: ${{ job.target }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
        ${{ if parameters.triggerDownstreamJobs }}:
          downstreamAggregateNames: ${{ parameters.downstreamAggregateNames }}
    - task: Bash@3
      displayName: Build and install other dependencies
      retryCountOnTaskFailure: 3
      inputs:
        targetType: inline
        workingDirectory: $(Agent.BuildDirectory)/s
        script: |
          sed -i '/composable_kernel/d' requirements.txt
          mkdir -p $(Agent.BuildDirectory)/miopen-deps
          export CXX=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang++
          export CC=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang
          cmake -P install_deps.cmake --prefix $(Agent.BuildDirectory)/miopen-deps --generator Ninja
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        extraBuildFlags: >-
          -DMIOPEN_BACKEND=HIP
          -DCMAKE_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang++
          -DCMAKE_PREFIX_PATH=$(Agent.BuildDirectory)/rocm;$(Agent.BuildDirectory)/miopen-deps
          -DGPU_TARGETS=${{ job.target }}
          -DMIOPEN_ENABLE_AI_KERNEL_TUNING=OFF
          -DMIOPEN_ENABLE_AI_IMMED_MODE_FALLBACK=OFF
          -DCMAKE_BUILD_TYPE=Release
          -DBUILD_TESTING=ON
          -GNinja
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
      parameters:
        gpuTarget: ${{ job.target }}
        sparseCheckoutDir: ${{ parameters.sparseCheckoutDir }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
      parameters:
        componentName: ${{ parameters.componentName }}
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-links.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
        gpuTarget: ${{ job.target }}
        extraCopyDirectories:
          - miopen-deps

- ${{ each job in parameters.jobMatrix.testJobs }}:
  - job: ${{ parameters.componentName }}_test_ubuntu2204_${{ job.target }}
    timeoutInMinutes: 180
    dependsOn: ${{ parameters.componentName }}_build_ubuntu2204_${{ job.target }}
    condition:
      and(succeeded(),
        eq(variables['ENABLE_${{ upper(job.target) }}_TESTS'], 'true'),
        not(containsValue(split(variables['DISABLED_${{ upper(job.target) }}_TESTS'], ','), variables['Build.DefinitionName'])),
        eq(${{ parameters.aggregatePipeline }}, False)
      )
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: ROCM_PATH
      value: $(Agent.BuildDirectory)/rocm
    pool: ${{ job.target }}_test_pool
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-cmake-custom.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
        sparseCheckoutDir: ${{ parameters.sparseCheckoutDir }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-aqlprofile.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/miopen-get-ck-build.yml
      parameters:
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmTestDependencies }}
        gpuTarget: ${{ job.target }}
        ${{ if parameters.triggerDownstreamJobs }}:
          downstreamAggregateNames: ${{ parameters.downstreamAggregateNames }}
    - task: Bash@3
      displayName: Build and install other dependencies
      retryCountOnTaskFailure: 3
      inputs:
        targetType: inline
        workingDirectory: $(Agent.BuildDirectory)/s
        script: |
          sed -i '/composable_kernel/d' requirements.txt
          mkdir -p $(Agent.BuildDirectory)/miopen-deps
          export CXX=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang++
          export CC=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang
          cmake -P install_deps.cmake --prefix $(Agent.BuildDirectory)/miopen-deps
    - task: CMake@1
      displayName: 'MIOpen Test CMake Flags'
      inputs:
        cmakeArgs: >-
          -DCMAKE_PREFIX_PATH=$(Agent.BuildDirectory)/rocm;$(Agent.BuildDirectory)/s/bin;$(Agent.BuildDirectory)/miopen-deps
          -DCMAKE_INSTALL_PREFIX=$(Agent.BuildDirectory)/rocm
          -DCMAKE_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang++
          -DCMAKE_C_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang
          -DMIOPEN_BACKEND=HIP
          -DMIOPEN_TEST_FLAGS=" --disable-verification-cache"
          -DCMAKE_BUILD_TYPE=release
          -DBUILD_DEV=OFF
          -DMIOPEN_USE_MLIR=ON
          -DMIOPEN_GPU_SYNC=OFF
          $(Agent.BuildDirectory)/s
    - task: Bash@3
      displayName: 'MIOpen Test Build'
      inputs:
        targetType: inline
        workingDirectory: build
        script: |
          cmake --build . --target tests -- -j$(nproc)
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/gpu-diagnostics.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: ${{ parameters.componentName }}
        testParameters: '--output-on-failure --force-new-ctest-process --output-junit test_output.xml --exclude-regex "test_rnn_seq_api|GPU_Conv2dTuningAsm_FP32|GPU_Conv2dTuningAsmBwdWrw_FP32"'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
        environment: test
        gpuTarget: ${{ job.target }}
        extraCopyDirectories:
          - miopen-deps

# - ${{ if parameters.triggerDownstreamJobs }}:
#   - ${{ each component in parameters.downstreamComponentMatrix }}:
#     - ${{ if not(and(parameters.unifiedBuild, eq(component.skipUnifiedBuild, 'true'))) }}:
#       - template: /.azuredevops/components/${{ component.name }}.yml@pipelines_repo
#         parameters:
#           checkoutRepo: ${{ component.checkoutRepo }}
#           # sparseCheckoutDir: ${{ component.sparseCheckoutDir }}
#           buildDependsOn: ${{ component.buildDependsOn }}
#           downstreamAggregateNames: ${{ parameters.downstreamAggregateNames }}+${{ parameters.componentName }}
#           triggerDownstreamJobs: true
#           unifiedBuild: ${{ parameters.unifiedBuild }}
