# Copyright 2024 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(CMakeDependentOption)

iree_cc_library(
  NAME
    backends
  SRCS
    "backends.cc"
  DEPS
    iree-amd-aie::driver::xrt::registration
    iree::hal
    iree::hal::cts::util::registry
  TESTONLY
  ALWAYSLINK
  PUBLIC
)

iree_hal_cts_test_suite(
  BACKENDS_LIB
    ::backends
  LABELS
    "driver=xrt"
    "requires-npu"
)

set(PEANO_INSTALL_DIR "" CACHE PATH "")
set(VITIS_DIR "" CACHE PATH "")
if((NOT PEANO_INSTALL_DIR) AND (NOT VITIS_DIR))
  message(FATAL_ERROR "either PEANO_INSTALL_DIR or VITIS_DIR must be set")
endif()
cmake_dependent_option(USE_CHESS "" "1" "VITIS_DIR" "0")
set(TARGET_DEVICE "npu1_4col" CACHE STRING "")

iree_bytecode_module(
  NAME
    xrt_executable_cache_test_module
  MODULE_FILE_NAME
    xrt_executable_cache_test.bin
  SRC
    "${CMAKE_CURRENT_LIST_DIR}/executable_cache_test.mlir"
  FLAGS
    --compile-mode=hal-executable
    --iree-hal-dump-executable-files-to=${CMAKE_CURRENT_BINARY_DIR}
    # on windows iree-aie-xclbinutil for some reason isn't found by iree's findTool
    # so set this instead to the bin dir
    --iree-amd-aie-install-dir=${CMAKE_BINARY_DIR}
    --iree-hal-target-backends=amd-aie
    --iree-amdaie-lower-to-aie-pipeline=air
    --iree-amdaie-target-device=${TARGET_DEVICE}
    --iree-amd-aie-peano-install-dir=${PEANO_INSTALL_DIR}
    --iree-amd-aie-vitis-install-dir=${VITIS_DIR}
    --iree-amd-aie-enable-chess=$<BOOL:${USE_CHESS}>
    --iree-amd-aie-enable-chess-for-ukernel=1
    --iree-amd-aie-show-invoked-commands
    --iree-hal-memoization=false
    --iree-hal-indirect-command-buffers=false
  DEPS
    iree-aie-xclbinutil
  PUBLIC
  TESTONLY
)

iree_c_embed_data(
  NAME
    xrt_executables_c
  SRCS
    xrt_executable_cache_test.bin
  C_FILE_OUTPUT
    xrt_executables_c.c
  H_FILE_OUTPUT
    xrt_executables_c.h
  IDENTIFIER
    iree_cts_testdata_executables_aie_xrt
  STRIP_PREFIX
    xrt_
  DEPENDS
    ::xrt_executable_cache_test_module
  FLATTEN
  PUBLIC
  TESTONLY
)

iree_cc_test(
  NAME
    xrt_executable_cache_test
  SRCS
    executable_cache_test.cc
  DEPS
    ::xrt_executables_c
    iree-amd-aie::driver::xrt::registration
    iree::base
    iree::hal
    iree::hal::cts::util::test_base
    iree::testing::gtest_main
)

iree_cc_test(
  NAME
    xrt_dispatch_test
  SRCS
    matmul_dispatch_test.cc
  DEPS
    ::xrt_executables_c
    iree-amd-aie::driver::xrt::registration
    iree::base
    iree::hal
    iree::hal::cts::util::test_base
    iree::testing::gtest_main
    iree::tools::testing::e2e::e2e_test_util
)

target_include_directories(iree-amd-aie_driver_xrt_cts_xrt_executable_cache_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_include_directories(iree-amd-aie_driver_xrt_cts_xrt_dispatch_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
