Fix rewrite rules to handle attribute references (RefAttr) properly #2381
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.
This PR fixes an issue where rewrite rules would fail when encountering attribute references (
RefAttr
) inside functions, addressing cases where attribute values are not available as concrete values but reference function parameters.Problem
Rewrite rules previously failed in two scenarios involving
RefAttr
:Function extraction with
as_function=True
: When a rewrite rule tried to extract a matched subgraph into a function and encountered nodes withRefAttr
, it would raiseNotImplementedError: RefAttr not supported
in the_copy_for_function
utility.Pattern matching with attribute access: Replacement patterns that tried to access
.value
on attributes would fail when the attribute was aRefAttr
instead of a concrete attribute.Solution
1. Fixed RefAttr handling in function extraction
Changed
copy_attr_value
function in_rewrite_rule.py
to properly handleRefAttr
:Instead of raising
NotImplementedError
,RefAttr
objects are now returned as-is since they are immutable and can be safely shared in the new function context.2. Fixed RefAttr handling in pattern matching
Updated
generic_pattern_test.py
to check forRefAttr
before accessing.value
:This allows replacement patterns to gracefully handle cases where optimization cannot proceed due to attribute references.
Testing
Added comprehensive test
test_rewrite_rule_with_ref_attr
that verifies:as_function=True
work correctly when encounteringRefAttr
RefAttr
is properly preserved in extracted functionsExample
Before this fix:
After this fix:
Fixes #1442.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.