Add mcTVM MACA FFI patch guards#37
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds existence checks (if(EXISTS)) before reading and modifying several third-party files in cmake/modules/MACA.cmake. The reviewer feedback highlights that if any of the string(REPLACE) operations fail to find their target strings, CMake will still write the unmodified contents back to the files on every configure run. This updates their modification times and triggers unnecessary rebuilds. The reviewer suggests adding a check to ensure NEW_CONTENTS is different from FILE_CONTENTS before performing the write operation.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if(EXISTS "${dlpack_header}") | ||
| file(READ "${dlpack_header}" FILE_CONTENTS) | ||
| if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*") | ||
| string(REPLACE "} DLDeviceType;" " kDLMACA = 19,\n kDLMACAHost = 20,\n} DLDeviceType;" NEW_CONTENTS "${FILE_CONTENTS}") | ||
| file(WRITE "${dlpack_header}" "${NEW_CONTENTS}") | ||
| endif() | ||
| endif() |
There was a problem hiding this comment.
If the string(REPLACE) operation fails to find the target string (e.g., due to upstream changes in the file structure), NEW_CONTENTS will be identical to FILE_CONTENTS. Since NOT FILE_CONTENTS MATCHES ".*kDLMACA.*" remains true, CMake will write to the file on every single configure run. This updates the file's modification time and triggers unnecessary rebuilds of dependent targets.
To prevent this, only write the file if NEW_CONTENTS is actually different from FILE_CONTENTS.
if(EXISTS "${dlpack_header}")
file(READ "${dlpack_header}" FILE_CONTENTS)
if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*")
string(REPLACE "} DLDeviceType;" " kDLMACA = 19,\n kDLMACAHost = 20,\n} DLDeviceType;" NEW_CONTENTS "${FILE_CONTENTS}")
if(NOT "${NEW_CONTENTS}" STREQUAL "${FILE_CONTENTS}")
file(WRITE "${dlpack_header}" "${NEW_CONTENTS}")
endif()
endif()
endif()
| if(EXISTS "${ffi_core_pyi}") | ||
| file(READ "${ffi_core_pyi}" FILE_CONTENTS) | ||
| if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*") | ||
| string(REPLACE "kDLTrn = 17" "kDLTrn = 17\n kDLMACA = 19\n kDLMACAHost = 20" NEW_CONTENTS "${FILE_CONTENTS}") | ||
| file(WRITE "${ffi_core_pyi}" "${NEW_CONTENTS}") | ||
| endif() | ||
| endif() |
There was a problem hiding this comment.
If the string(REPLACE) operation fails to find the target string, NEW_CONTENTS will be identical to FILE_CONTENTS. Since NOT FILE_CONTENTS MATCHES ".*kDLMACA.*" remains true, CMake will write to the file on every single configure run. This updates the file's modification time and triggers unnecessary rebuilds of dependent targets.
To prevent this, only write the file if NEW_CONTENTS is actually different from FILE_CONTENTS.
if(EXISTS "${ffi_core_pyi}")
file(READ "${ffi_core_pyi}" FILE_CONTENTS)
if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*")
string(REPLACE "kDLTrn = 17" "kDLTrn = 17\n kDLMACA = 19\n kDLMACAHost = 20" NEW_CONTENTS "${FILE_CONTENTS}")
if(NOT "${NEW_CONTENTS}" STREQUAL "${FILE_CONTENTS}")
file(WRITE "${ffi_core_pyi}" "${NEW_CONTENTS}")
endif()
endif()
endif()
| if(EXISTS "${ffi_container_tensor}") | ||
| file(READ "${ffi_container_tensor}" FILE_CONTENTS) | ||
| if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*") | ||
| string(REPLACE "device.device_type == kDLROCMHost;" "device.device_type == kDLROCMHost ||\n device.device_type == kDLMACA || device.device_type == kDLMACAHost;" NEW_CONTENTS "${FILE_CONTENTS}") | ||
| file(WRITE "${ffi_container_tensor}" "${NEW_CONTENTS}") | ||
| endif() | ||
| endif() |
There was a problem hiding this comment.
If the string(REPLACE) operation fails to find the target string, NEW_CONTENTS will be identical to FILE_CONTENTS. Since NOT FILE_CONTENTS MATCHES ".*kDLMACA.*" remains true, CMake will write to the file on every single configure run. This updates the file's modification time and triggers unnecessary rebuilds of dependent targets.
To prevent this, only write the file if NEW_CONTENTS is actually different from FILE_CONTENTS.
if(EXISTS "${ffi_container_tensor}")
file(READ "${ffi_container_tensor}" FILE_CONTENTS)
if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*")
string(REPLACE "device.device_type == kDLROCMHost;" "device.device_type == kDLROCMHost ||\n device.device_type == kDLMACA || device.device_type == kDLMACAHost;" NEW_CONTENTS "${FILE_CONTENTS}")
if(NOT "${NEW_CONTENTS}" STREQUAL "${FILE_CONTENTS}")
file(WRITE "${ffi_container_tensor}" "${NEW_CONTENTS}")
endif()
endif()
endif()
| if(EXISTS "${ffi_cython_base_pxi}") | ||
| file(READ "${ffi_cython_base_pxi}" FILE_CONTENTS) | ||
| if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*") | ||
| string(REPLACE "kDLTrn = 18" "kDLTrn = 18\n kDLMACA = 19\n kDLMACAHost = 20" NEW_CONTENTS "${FILE_CONTENTS}") | ||
| file(WRITE "${ffi_cython_base_pxi}" "${NEW_CONTENTS}") | ||
| endif() | ||
| endif() |
There was a problem hiding this comment.
If the string(REPLACE) operation fails to find the target string, NEW_CONTENTS will be identical to FILE_CONTENTS. Since NOT FILE_CONTENTS MATCHES ".*kDLMACA.*" remains true, CMake will write to the file on every single configure run. This updates the file's modification time and triggers unnecessary rebuilds of dependent targets.
To prevent this, only write the file if NEW_CONTENTS is actually different from FILE_CONTENTS.
if(EXISTS "${ffi_cython_base_pxi}")
file(READ "${ffi_cython_base_pxi}" FILE_CONTENTS)
if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*")
string(REPLACE "kDLTrn = 18" "kDLTrn = 18\n kDLMACA = 19\n kDLMACAHost = 20" NEW_CONTENTS "${FILE_CONTENTS}")
if(NOT "${NEW_CONTENTS}" STREQUAL "${FILE_CONTENTS}")
file(WRITE "${ffi_cython_base_pxi}" "${NEW_CONTENTS}")
endif()
endif()
endif()
| if(EXISTS "${ffi_cython_device_pxi}") | ||
| file(READ "${ffi_cython_device_pxi}" FILE_CONTENTS) | ||
| if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*") | ||
| string(REPLACE "kDLTrn = 17" "kDLTrn = 17\n kDLMACA = 19\n kDLMACAHost = 20" NEW_CONTENTS "${FILE_CONTENTS}") | ||
| string(REPLACE "DLDeviceType.kDLTrn: \"trn\"," "DLDeviceType.kDLTrn: \"trn\",\n DLDeviceType.kDLMACA: \"maca\",\n DLDeviceType.kDLMACAHost: \"maca_host\"," NEW_CONTENTS "${NEW_CONTENTS}") | ||
| string(REPLACE "\"trn\": DLDeviceType.kDLTrn," "\"trn\": DLDeviceType.kDLTrn,\n \"maca\": DLDeviceType.kDLMACA," NEW_CONTENTS "${NEW_CONTENTS}") | ||
| file(WRITE "${ffi_cython_device_pxi}" "${NEW_CONTENTS}") | ||
| endif() | ||
| endif() |
There was a problem hiding this comment.
If the string(REPLACE) operation fails to find the target string, NEW_CONTENTS will be identical to FILE_CONTENTS. Since NOT FILE_CONTENTS MATCHES ".*kDLMACA.*" remains true, CMake will write to the file on every single configure run. This updates the file's modification time and triggers unnecessary rebuilds of dependent targets.
To prevent this, only write the file if NEW_CONTENTS is actually different from FILE_CONTENTS.
if(EXISTS "${ffi_cython_device_pxi}")
file(READ "${ffi_cython_device_pxi}" FILE_CONTENTS)
if(NOT FILE_CONTENTS MATCHES ".*kDLMACA.*")
string(REPLACE "kDLTrn = 17" "kDLTrn = 17\n kDLMACA = 19\n kDLMACAHost = 20" NEW_CONTENTS "${FILE_CONTENTS}")
string(REPLACE "DLDeviceType.kDLTrn: \"trn\"," "DLDeviceType.kDLTrn: \"trn\",\n DLDeviceType.kDLMACA: \"maca\",\n DLDeviceType.kDLMACAHost: \"maca_host\"," NEW_CONTENTS "${NEW_CONTENTS}")
string(REPLACE "\"trn\": DLDeviceType.kDLTrn," "\"trn\": DLDeviceType.kDLTrn,\n \"maca\": DLDeviceType.kDLMACA," NEW_CONTENTS "${NEW_CONTENTS}")
if(NOT "${NEW_CONTENTS}" STREQUAL "${FILE_CONTENTS}")
file(WRITE "${ffi_cython_device_pxi}" "${NEW_CONTENTS}")
endif()
endif()
endif()
Summary
Validation
Review notes