# Match the minimum required version of onnx-mlir cmake_minimum_required(VERSION 3.20.0) project(raptor) # Materialize a CMake shim directory function(raptor_write_external_cmake_shim shim_dir external_source_dir description) get_filename_component(real_external_source_dir "${external_source_dir}" REALPATH) file(RELATIVE_PATH relative_external_source_dir "${shim_dir}" "${real_external_source_dir}") if (NOT EXISTS "${real_external_source_dir}/CMakeLists.txt") message(FATAL_ERROR "External CMake source directory not found or missing CMakeLists.txt:\n" " ${real_external_source_dir}" ) endif () if (IS_SYMLINK "${shim_dir}") message(STATUS "Removing old full-directory symlink: ${shim_dir}") file(REMOVE "${shim_dir}") endif () if (EXISTS "${shim_dir}" AND NOT IS_DIRECTORY "${shim_dir}") message(FATAL_ERROR "Expected directory or absent path, got file: ${shim_dir}") endif () file(MAKE_DIRECTORY "${shim_dir}") set(shim_file "${shim_dir}/CMakeLists.txt") set(shim_contents "get_filename_component(raptor_external_source_dir \"\${CMAKE_CURRENT_LIST_DIR}/${relative_external_source_dir}\" REALPATH ) add_subdirectory( \"\${raptor_external_source_dir}\" \"\${CMAKE_CURRENT_BINARY_DIR}/raptor-external\" ) if (DEFINED PIM_ENABLED) set(PIM_ENABLED \"\${PIM_ENABLED}\" PARENT_SCOPE) endif () " ) if (EXISTS "${shim_file}") file(READ "${shim_file}" old_contents) else () set(old_contents "") endif () if (NOT old_contents STREQUAL shim_contents) file(WRITE "${shim_file}" "${shim_contents}") message(STATUS "Wrote CMake shim for ${description}: ${shim_file}") else () message(STATUS "CMake shim already up to date for ${description}") endif () # Mirror the external tree's first-level entries into the shim directory # so legacy includes like src/Accelerators/PIM/Compiler/... keep working. file(GLOB children RELATIVE "${real_external_source_dir}" "${real_external_source_dir}/*") foreach (child IN LISTS children) if (child STREQUAL "CMakeLists.txt") continue() endif () set(real_child "${real_external_source_dir}/${child}") set(shim_child "${shim_dir}/${child}") if (IS_SYMLINK "${shim_child}") file(READ_SYMLINK "${shim_child}" existing_link_target) if (existing_link_target STREQUAL real_child) continue() endif () file(REMOVE_RECURSE "${shim_child}") elseif (EXISTS "${shim_child}") # Do not delete real files/directories. This protects the generated shim. continue() endif () file(CREATE_LINK "${real_child}" "${shim_child}" SYMBOLIC ) endforeach () endfunction() raptor_write_external_cmake_shim( "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir/src/Accelerators/PIM" "${CMAKE_CURRENT_SOURCE_DIR}/src/PIM" "PIM accelerator" ) raptor_write_external_cmake_shim( "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir/test/accelerators/PIM" "${CMAKE_CURRENT_SOURCE_DIR}/test/PIM" "PIM accelerator tests" ) # Patch onnx-mlir sources for PIM accelerator support. # Each patch searches for a context-aware anchor string rather than relying on # line numbers, so that moderate upstream changes are tolerated. function(raptor_apply_patch file_path anchor replacement description) file(READ "${file_path}" contents) # Already applied – replacement text is present string(FIND "${contents}" "${replacement}" already_applied_pos) if (NOT already_applied_pos EQUAL -1) message(STATUS "Patch already applied: ${description}") return() endif () # Anchor must exist for the patch to be applicable string(FIND "${contents}" "${anchor}" anchor_pos) if (anchor_pos EQUAL -1) message(FATAL_ERROR "Patch anchor not found – onnx-mlir may have changed.\n" " Patch : ${description}\n" " File : ${file_path}\n" " Anchor: ${anchor}" ) endif () string(REPLACE "${anchor}" "${replacement}" patched "${contents}") file(WRITE "${file_path}" "${patched}") message(STATUS "Patch applied: ${description}") endfunction() set(ONNX_MLIR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/onnx-mlir") # Register PIM compiler options alongside NNPA raptor_apply_patch( "${ONNX_MLIR_DIR}/src/Accelerators/Accelerator.hpp" "#include \"src/Accelerators/NNPA/Compiler/NNPACompilerOptions.hpp\"" "#include \"src/Accelerators/NNPA/Compiler/NNPACompilerOptions.hpp\"\n#include \"src/Accelerators/PIM/Compiler/PimCompilerOptions.hpp\"" "Add PIM compiler options include" ) # Short-circuit output emission for the PIM accelerator raptor_apply_patch( "${ONNX_MLIR_DIR}/src/Compiler/CompilerUtils.cpp" "switch (emissionTarget) {\n case EmitObj: {" "if (llvm::is_contained(maccel, accel::Accelerator::Kind::PIM))\n return CompilerSuccess;\n switch (emissionTarget) {\n case EmitObj: {" "Skip output emission for PIM accelerator" ) add_subdirectory(onnx-mlir)