[REFACTOR][IR] Phase out src/ir/structural_{hash,equal}.cc to tvm-ffi#19613
Merged
Conversation
…e homes The serialization TypeAttrDef registrations for ffi::ModuleObj and ffi::TensorObj were sitting in src/ir/structural_hash.cc as a historical accident; their natural home is alongside the rest of the type's machinery. This commit relocates them to src/ir/module.cc and src/runtime/tensor.cc respectively, both of which already host a TVM_FFI_STATIC_INIT_BLOCK for the same type.
…ckends Python wrappers in tvm.ir.base were forwarding to TVM-side adapters (node.StructuralEqual / .StructuralHash / .GetFirstStructuralMismatch) which only existed to bridge into tvm-ffi. Cut out the middle layer: call the tvm-ffi APIs directly. assert_structural_equal rebuilds the diagnostic message in Python using the existing TVMScript printer with path_to_underline, byte-identical to the C++ adapter's output.
With Module/Tensor serialization attrs relocated to their type homes and Python callers migrated to tvm-ffi directly, these two TU's hold only the now-unused node.Structural* FFI globals. Delete the files.
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors structural equality and hashing mechanisms by migrating FFI registrations. It deletes src/ir/structural_equal.cc and src/ir/structural_hash.cc, moving JSON serialization/deserialization logic for ModuleObj and TensorObj directly to src/ir/module.cc and src/runtime/tensor.cc respectively. In Python, assert_structural_equal is updated to provide detailed mismatch paths using GetFirstStructuralMismatch and the script printer. A review comment suggests using a direct StructuralEqual API instead of GetFirstStructuralMismatch in structural_equal to avoid path-tracking overhead and improve performance.
2 tasks
…undant convert Switches structural_equal / get_first_structural_mismatch / assert_structural_equal / structural_hash from _ffi_api.X to the higher-level tvm_ffi.X wrappers (smaller FFI api surface), and drops the redundant tvm.runtime.convert() calls since tvm-ffi auto-converts.
tlopex
approved these changes
May 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The tvm-ffi layer now provides fully featured structural-hash and structural-equal APIs (including
GetFirstStructuralMismatchwithAccessPathpair output). The two TUssrc/ir/structural_hash.ccandsrc/ir/structural_equal.cchad become thin adapters with no logic of their own — they forwarded to tvm-ffi and registered the results asnode.Structural*globals for Python to call. This PR removes the indirection.[REFACTOR][IR]): relocates theffi::ModuleObjandffi::TensorObj__data_to_json__/__data_from_json__TypeAttrDefregistrations fromstructural_hash.ccintosrc/ir/module.ccandsrc/runtime/tensor.ccrespectively, both of which already have aTVM_FFI_STATIC_INIT_BLOCKfor those types.[REFACTOR][PYTHON]): rewrites the four Python wrappers intvm.ir.base(structural_equal,get_first_structural_mismatch,assert_structural_equal,structural_hash) to calltvm_ffi._ffi_apidirectly, bypassing the now-redundantnode.Structural*globals.assert_structural_equalreconstructs the same diagnostic message in Python usingTVMScriptPrinterScriptwithpath_to_underline.[REFACTOR][IR]): deletessrc/ir/structural_hash.ccandsrc/ir/structural_equal.ccwhose remaining content (thenode.Structural*FFI global registrations) is now unused.