################################################################################
# Autogenerated by build_tools/bazel_to_cmake/bazel_to_cmake.py from           #
# compiler/plugins/target/ROCM/BUILD.bazel                                     #
#                                                                              #
# Use iree_cmake_extra_content from iree/build_defs.oss.bzl to add arbitrary   #
# CMake-only content.                                                          #
#                                                                              #
# To disable autogeneration for this file entirely, delete this header.        #
################################################################################

iree_add_all_subdirs()

iree_compiler_register_plugin(
  PLUGIN_ID
    hal_target_rocm
  TARGET
    ::ROCM
)

iree_cc_library(
  NAME
    ROCM
  HDRS
    "ROCMTargetUtils.h"
  SRCS
    "ROCMTarget.cpp"
    "ROCMTargetUtils.cpp"
  DEPS
    LLVMAMDGPUCodeGen
    LLVMAnalysis
    LLVMBitWriter
    LLVMCore
    LLVMIRReader
    LLVMLinker
    LLVMMC
    LLVMPasses
    LLVMSupport
    LLVMTarget
    LLVMTransformUtils
    LLVMipo
    MLIRAMDGPUDialect
    MLIRAMDGPUToROCDL
    MLIRAMDGPUTransforms
    MLIRAMDGPUUtils
    MLIRBuiltinToLLVMIRTranslation
    MLIRIR
    MLIRLLVMDialect
    MLIRLLVMToLLVMIRTranslation
    MLIRPass
    MLIRROCDLDialect
    MLIRROCDLTarget
    MLIRROCDLToLLVMIRTranslation
    MLIRSupport
    MLIRTargetLLVMIRExport
    iree::compiler::Codegen::Common
    iree::compiler::Codegen::Dialect::Codegen::IR::IREECodegenDialect
    iree::compiler::Codegen::Dialect::GPU::IR::IREEGPUDialect
    iree::compiler::Codegen::Dialect::GPU::TargetUtils::KnownTargets
    iree::compiler::Codegen::Dialect::VectorExt::IR::IREEVectorExtDialect
    iree::compiler::Codegen::LLVMGPU
    iree::compiler::Codegen::Utils
    iree::compiler::Dialect::HAL::IR
    iree::compiler::Dialect::HAL::Target
    iree::compiler::Dialect::HAL::Utils::ExecutableDebugInfoUtils
    iree::compiler::Dialect::HAL::Utils::LLVMLinkerUtils
    iree::compiler::PluginAPI
    iree::compiler::Utils
    iree::compiler::plugins::target::ROCM::builtins::tuning::iree_default_tuning_specs_amdgpu
    iree::compiler::plugins::target::ROCM::builtins::ukernel::iree_uk_amdgpu_bitcode
    iree::schemas::amdgpu_executable_def_c_fbs
    iree::schemas::executable_debug_info_c_fbs
    iree::schemas::hip_executable_def_c_fbs
  PUBLIC
)

### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###

# We either use device bitcode files from a configurable path
# or we download them from a fixed location and copy them from
# there. We default to downloading as it is a better use experience
# by default.
# See: https://github.com/shark-infra/amdgpu-device-libs
set(_amd_required_libs "ocml.bc" "ockl.bc")
set(_amd_device_bc_url
    "https://github.com/shark-infra/amdgpu-device-libs/releases/download/v20231101/amdgpu-device-libs-llvm-6086c272a3a59eb0b6b79dcbe00486bf4461856a.tgz")
set(_amd_device_bc_sha256 "336362416c68fdd8bb80328f65ca7ebaa0c119ea19c95df6df30c832a4df39b9")
set(_amd_device_bc_stamp "${_amd_device_bc_url} : ${_amd_device_bc_sha256}")
set(IREE_TARGET_BACKEND_ROCM_DEVICE_BC_PATH "" CACHE PATH
    "Optional path to load device bitcode from (default to dynamic fetch)")
set(_platform_lib_srcdir)

if(IREE_TARGET_BACKEND_ROCM_DEVICE_BC_PATH)
  # Don't fetch: Get from local directory.
  set(_platform_lib_srcdir "${IREE_TARGET_BACKEND_ROCM_DEVICE_BC_PATH}")
else()
  # Fetch from remote archive.
  set(_platform_lib_archive "${CMAKE_CURRENT_BINARY_DIR}/_fetch_device_libs.tgz")
  set(_platform_lib_srcdir "${CMAKE_CURRENT_BINARY_DIR}/_fetch_device_libs")
  set(_fetch_stamp_file "${CMAKE_CURRENT_BINARY_DIR}/_fetch_device_libs.stamp")
  set(_needs_fetch ON)
  if(EXISTS "${_fetch_stamp_file}" AND IS_DIRECTORY "${_platform_lib_srcdir}")
    file(READ "${_fetch_stamp_file}" _stamp_contents)
    if ("${_stamp_contents}" STREQUAL "${_amd_device_bc_stamp}")
      set(_needs_fetch OFF)
    endif()
  endif()

  # Download, extract, stamp.
  if(_needs_fetch)
    message(STATUS "Downloading AMD Device bitcode from ${_amd_device_bc_url} to ${_platform_lib_srcdir}")
    file(DOWNLOAD "${_amd_device_bc_url}" "${_platform_lib_archive}"
      EXPECTED_HASH SHA256=${_amd_device_bc_sha256})
    file(MAKE_DIRECTORY "${_platform_lib_srcdir}")
    execute_process(
      COMMAND
        ${CMAKE_COMMAND} -E tar xzf ${_platform_lib_archive}
      WORKING_DIRECTORY "${_platform_lib_srcdir}"
      COMMAND_ERROR_IS_FATAL ANY
    )
    file(WRITE "${_fetch_stamp_file}" "${_amd_device_bc_stamp}")
  else()
    message(STATUS "Already fetched AMD Device bitcode into ${_platform_lib_srcdir}")
  endif()
endif()

# Set up copy rules.
set(_platform_lib_reldir "iree_platform_libs/rocm")
set(_platform_lib_absdir "${IREE_COMPILER_DYLIB_DIR}/${_platform_lib_reldir}")
file(MAKE_DIRECTORY "${_platform_lib_absdir}")
set(_all_device_bc_copy_commands)
set(_all_device_bc_files)
foreach(_amd_lib_name ${_amd_required_libs})
  # Copy to lib/ tree.
  set(_device_bc_srcpath "${_platform_lib_srcdir}/${_amd_lib_name}")
  set(_device_bc_relpath "${_platform_lib_reldir}/${_amd_lib_name}")
  list(APPEND _all_device_bc_files "${IREE_COMPILER_DYLIB_DIR}/${_device_bc_relpath}")
  list(APPEND _all_device_bc_deps "${_device_bc_path}")
  list(APPEND _all_device_bc_copy_commands
    COMMAND ${CMAKE_COMMAND} -E copy
      "${_device_bc_srcpath}"
      "${IREE_COMPILER_DYLIB_DIR}/${_device_bc_relpath}"
  )

  # Note this bc file as being part of the bundle that must be included with
  # the compiler dylib.
  set_property(GLOBAL APPEND PROPERTY IREE_COMPILER_DYLIB_RELPATHS "${_device_bc_relpath}")
endforeach()

# Generate a custom target with all file level dependencies and commands to
# copy to our build tree locations.
# Our GenDeviceLibs target depends on all of the defined device lib targets.
add_custom_command(
  OUTPUT ${_all_device_bc_files}
  DEPENDS ${_all_device_bc_deps}
  POST_BUILD
    ${_all_device_bc_copy_commands}
)
add_custom_target(iree_compiler_plugins_target_ROCM_GenDeviceLibs
  DEPENDS
    ${_all_device_bc_files}
)

# Ensure that the device libs are built when the compiler dylib is built.
set_property(GLOBAL APPEND PROPERTY IREE_COMPILER_DEPENDS
  iree_compiler_plugins_target_ROCM_GenDeviceLibs)

# Install.
install(FILES ${_all_device_bc_files}
  DESTINATION "${IREE_COMPILER_DYLIB_INSTALL_PREFIX}/${_platform_lib_reldir}")
