Skip to content

Conversation

@Hzfengsy
Copy link
Member

This commit introduces functionality to merge block annotations in TVM script. The implementation includes:

  • MergeAnnotations function that recursively merges annotation dictionaries
  • Support for nested dictionary merging with new values overriding old ones
  • Error handling for conflicting annotation values
  • BlockAttrs function that uses the merging logic to combine multiple T.block_attr() calls within the same block

The feature allows users to specify block attributes incrementally using multiple T.block_attr() calls, which will be automatically merged together.

@Hzfengsy Hzfengsy force-pushed the script_merge_attr branch from acbcaf7 to 947e5b8 Compare June 19, 2025 14:17
}
// Case 2.2: the values are not both dicts, check if the keys are the same
if (!ffi::AnyEqual()(old_value.value(), value)) {
LOG(FATAL) << "ValueError: Try to merge two annotations with different values for key `"
Copy link
Member

Choose a reason for hiding this comment

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

might be useful to cross check error reporting here, to make sure when error happens, we can locate the span and have right ^^^^^ to point at the code location. @Hzfengsy can you confirm?

Copy link
Member Author

@Hzfengsy Hzfengsy Jun 20, 2025

Choose a reason for hiding this comment

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

Confirmed it works well. Try with testcase:

from tvm.script import tir as T

@T.prim_func
def func3():
    with T.block():
        T.block_attr({"key1": "block1"})
        T.block_attr({"key1": "block2"})
        T.evaluate(0)

get output:

error: Try to merge two annotations with different values for key `key1`, previous one is "block1", new one is "block2"
 --> /path/to/tvm/t.py:7:9
   |  
 7 |          T.block_attr({"key1": "block2"})
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: run with `TVM_BACKTRACE=1` environment variable to display a backtrace.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also I added a test case to test the error in tests/python/tvmscript/test_tvmscript_error_report.py

This commit introduces functionality to merge block annotations in TVM script.
The implementation includes:

- MergeAnnotations function that recursively merges annotation dictionaries
- Support for nested dictionary merging with new values overriding old ones
- Error handling for conflicting annotation values
- BlockAttrs function that uses the merging logic to combine multiple
  T.block_attr() calls within the same block

The feature allows users to specify block attributes incrementally using
multiple T.block_attr() calls, which will be automatically merged together.
@Hzfengsy Hzfengsy force-pushed the script_merge_attr branch from 947e5b8 to cd54fcc Compare June 20, 2025 03:29
@tqchen tqchen merged commit c3281c0 into apache:main Jun 20, 2025
15 checks passed
ShiboXing pushed a commit to ShiboXing/tvm that referenced this pull request Aug 10, 2025
This commit introduces functionality to merge block annotations in TVM script.
The implementation includes:

- MergeAnnotations function that recursively merges annotation dictionaries
- Support for nested dictionary merging with new values overriding old ones
- Error handling for conflicting annotation values
- BlockAttrs function that uses the merging logic to combine multiple
  T.block_attr() calls within the same block

The feature allows users to specify block attributes incrementally using
multiple T.block_attr() calls, which will be automatically merged together.
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.

2 participants