Skip to content

Add mcTVM MACA FFI patch guards#37

Open
ghangz wants to merge 2 commits into
MetaX-MACA:devfrom
ghangz:mengz/mctvm-maca-guard-ffi-patches
Open

Add mcTVM MACA FFI patch guards#37
ghangz wants to merge 2 commits into
MetaX-MACA:devfrom
ghangz:mengz/mctvm-maca-guard-ffi-patches

Conversation

@ghangz

@ghangz ghangz commented Jul 1, 2026

Copy link
Copy Markdown

Summary

  • This change adds guard logic around MACA FFI patch handling so repeated configure or partial dependency states fail earlier with clearer behavior.
  • The change is limited to MACA build discovery or guarded setup logic and keeps existing default behavior compatible.
  • Main files: cmake/modules/MACA.cmake

Validation

  • Verified on Gitee.AI MetaX GPU resources in the mcTVM TileLang/MACA TVM build batch, 6/6 PASS.
  • Branch validation command: CMake configure and mxcc compile smoke validation in the MACA TVM build image.
  • Pull request text is ASCII-only to avoid encoding issues on web forms and API clients.

Review notes

  • Source branch: ghangz:mengz/mctvm-maca-guard-ffi-patches
  • Target branch: MetaX-MACA/mcTVM:dev
  • Maintainers can modify this branch if follow-up adjustments are needed.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmake/modules/MACA.cmake
Comment on lines +20 to 26
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()

Comment thread cmake/modules/MACA.cmake
Comment on lines +28 to 34
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()

Comment thread cmake/modules/MACA.cmake
Comment on lines +36 to 42
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()

Comment thread cmake/modules/MACA.cmake
Comment on lines +44 to 50
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()

Comment thread cmake/modules/MACA.cmake
Comment on lines +52 to 60
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()

@ghangz ghangz changed the title Add mcTVM mctvm MACA guard FFI patches Add mcTVM MACA FFI patch guards Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant