[Relax] Fix FoldConstant crash on data-dependent reshape with unknown shape - #20488
Open
cchung100m wants to merge 3 commits into
Open
cchung100m wants to merge 3 commits into
cchung100m wants to merge 3 commits into
Conversation
cchung100m
force-pushed
the
issue-20260
branch
6 times, most recently
from
September 29, 2026 12:38
2953448 to
56ad79a
Compare
cchung100m
force-pushed
the
issue-20260
branch
from
September 29, 2026 12:55
56ad79a to
5553544
Compare
…hape_with_unknown_shape
…se_ops_is_skipped
cchung100m
force-pushed
the
issue-20260
branch
from
September 30, 2026 01:49
2a7512f to
afa1085
Compare
cchung100m
marked this pull request as ready for review
September 30, 2026 02:53
Contributor
Author
This branch has not been deployed
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.
Hi Committers,
This PR fixes issues #20260. Any suggestions would be appreciated if you are available.
Root Cause
FoldConstanthas a speculative step that calls an op's registeredFLegalizefunction to check if it would fold into an all-constantcall_tir. Unlike the realLegalizeOpspass, this speculative probe never checks whether the call's argument/return shapes are actually known before invokingFLegalize.When
reshape's target shape comes from an unfoldedtensor_to_shapecall (i.e., a genuinely non-constant tensor, so the shape values are unknown), this lets the unresolved shape reachreshape's legalizer (reshape_call_te), which asserts - with no message - that the shape has already resolved to aShapeExpr. That's what actually crashes.Note: the original issue attributes the crash to
fold_constant.cc'sshape_to_tensorspecial case, but that code path is dead for this repro (shape_to_tensorhas had a realFLegalizesince #19957) - it was already stale when the issue was filed. This PR fixes the crash reachable today (thetensor_to_shape->reshapecase). The issue's other example (shape_to_tensorfed directly intoreshape) is unrelated to this crash path. It is now rejected earlier, at graph-construction time, by unrelated, more recent type-inference changes - not by this PR.Solutions
LegalizeOps) into a sharedCanLegalizedCallhelper insrc/relax/transform/utils.h, reused by bothLegalizeOpsandFoldConstant.FoldConstantnow checksCanLegalizeCallbefore its speculative legalize-and-fold probe; when shapes aren't statically known, it skips folding and restores the call's original type (previously reset toType::Missing(), which could let a later stage infer an ill-formed type referencing a block-scopedDataflowVar).reshape/broadcast_to/collapse_sum_*'s sharedFLegalize(reshape_call_te) to raise a descriptiveValueErrorinstead of a bareassert, as defense in depth.