add automatic patches to onnx-mlir with CMakeLists.txt

This commit is contained in:
NiccoloN
2026-02-25 13:00:55 +01:00
parent 77f815a7a2
commit 5ca8916f4f
10 changed files with 169 additions and 63 deletions

View File

@@ -30,4 +30,59 @@ raptor_ensure_symlink(
"${CMAKE_CURRENT_SOURCE_DIR}/test/PIM"
)
# 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")
# 1) Disable the absl dependency
raptor_apply_patch(
"${ONNX_MLIR_DIR}/CMakeLists.txt"
"find_package(absl REQUIRED)"
"#find_package(absl REQUIRED)"
"Disable find_package(absl)"
)
# 2) 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"
)
# 3) 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)