[BugFix][Relax] Add structural_equal verification to subroutine cache lookup#18930
[BugFix][Relax] Add structural_equal verification to subroutine cache lookup#189303em0 wants to merge 2 commits intoapache:mainfrom
Conversation
… lookup SubroutineMixin._get_subroutine() used structural_hash as the sole cache key without structural_equal verification. If two different arg_sinfo values produced the same 64-bit hash, the cache would return a previously compiled function with mismatched parameter shapes, leading to silently incorrect compiled output. The fix changes the cache to store a list of (arg_sinfo, result) pairs per hash bucket and verifies each candidate with structural_equal before returning. This follows the same pattern used in block_builder.cc (StructuralHash + StructuralEqual). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical correctness defect in the Relax frontend's subroutine caching mechanism. Previously, the cache relied solely on Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses a security advisory concerning a subroutine cache hash collision in SubroutineMixin._get_subroutine(). The fix modifies the caching mechanism to use structural_equal verification after an initial hash lookup, preventing incorrect compiled output when different input shapes produce the same hash. A new test case has been added to validate this behavior. The review comments suggest minor readability improvements, such as using a temporary variable for a tuple and refactoring a loop into a list comprehension.
- Extract result tuple into variable and break down setdefault/append - Use list comprehension for subroutine collection in test Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
SubroutineMixin._get_subroutine()usedstructural_hashas the sole cache key withoutstructural_equalverification. If two differentarg_sinfovalues produced the same 64-bit hash (collision), the cache would return a previously compiled function with mismatched parameter shapes, leading to silently incorrect compiled output.(arg_sinfo, result)pairs per hash bucket and verify withstructural_equalon lookup, consistent with the pattern inblock_builder.cc.Root Cause
The subroutine cache (
cls._gvar) was keyed by(structural_hash(arg_sinfo), is_dataflow). A hash match was treated as proof of structural equality, skipping the necessarystructural_equalcheck. This is a hash-only lookup anti-pattern — hash determines the bucket, but equality must confirm the match.For comparison,
block_builder.cccorrectly usesStructuralHash+StructuralEqualtogether as the hash and equality functions forstd::unordered_map.Test plan
test_linearpasses (no regression)test_different_shapes_produce_distinct_subroutinespasses — verifies that the same Module class with different input shapes generates distinct subroutines🤖 Generated with Claude Code