Skip to content

[Relax] Fix FoldConstant crash on data-dependent reshape with unknown shape - #20488

Open
cchung100m wants to merge 12 commits into
apache:mainfrom
cchung100m:issue-20260
Open

cchung100m wants to merge 12 commits into
apache:mainfrom
cchung100m:issue-20260

Conversation

@cchung100m

@cchung100m cchung100m commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Hi Committers,

This PR fixes issues #20260. Any suggestions would be appreciated if you are available.

Root Cause

FoldConstant has a speculative step that calls an op's registered FLegalize function to check if it would fold into an all-constant call_tir. Unlike the real LegalizeOps pass, this speculative probe never checks whether the call's argument/return shapes are actually known before invoking FLegalize.

When reshape's target shape comes from an unfolded tensor_to_shape call (i.e., a genuinely non-constant tensor, so the shape values are unknown), this lets the unresolved shape reach reshape's legalizer (reshape_call_te), which asserts - with no message - that the shape has already resolved to a ShapeExpr. That's what actually crashes.

Note: the original issue attributes the crash to fold_constant.cc's shape_to_tensor special case, but that code path is dead for this repro (shape_to_tensor has had a real FLegalize since #19957) - it was already stale when the issue was filed. This PR fixes the crash reachable today (the tensor_to_shape -> reshape case). The issue's other example (shape_to_tensor fed directly into reshape) 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

  • Extract the "can this op be legalized right now" check (previously a private lambda inside LegalizeOps) into a shared CanLegalizedCall helper in src/relax/transform/utils.h, reused by both LegalizeOps and FoldConstant.
  • FoldConstant now checks CanLegalizeCall before its speculative legalize-and-fold probe; when shapes aren't statically known, it skips folding and restores the call's original type (previously reset to Type::Missing(), which could let a later stage infer an ill-formed type referencing a block-scoped DataflowVar).
  • Hardened reshape/broadcast_to/collapse_sum_*'s shared FLegalize (reshape_call_te) to raise a descriptive ValueError instead of a bare assert, as defense in depth.

@cchung100m
cchung100m force-pushed the issue-20260 branch 6 times, most recently from 2953448 to 56ad79a Compare September 29, 2026 12:38
@cchung100m
cchung100m marked this pull request as ready for review September 30, 2026 02:53
@cchung100m

Copy link
Copy Markdown
Contributor Author

cc @tlopex @mshr-h

@mshr-h

mshr-h commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

please rebase from main and fix ci errors. @cchung100m

@cchung100m

Copy link
Copy Markdown
Contributor Author

Hi @mshr-h

Thanks for reminder, I rebased on main and fixed the CI errors.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

FoldConstant can still emit ill-formed IR when reshape data folds to a constant with a dynamic target, and its type fallback discards safe refinements that prevent incorrect empty-shape decomposition.

2 open issues
What changed in this PR

This PR targets #20260 by aligning Relax’s speculative constant-folding legalization with the shape checks used by LegalizeOps, avoiding legalization attempts on unresolved runtime shapes.

Changes:

  • Extracts shared shape-availability checks for both passes.
  • Restores call types when folding is skipped and improves shape-binding diagnostics.
  • Adds regression tests for data-dependent reshapes with unknown target shapes.
File Description
tests/​python/​relax/​test_transform_legalize_ops_manipulate.py Tests that legalization skips unresolved data-dependent reshapes.
tests/​python/​relax/​test_transform_fold_constant.py Tests constant-folding behavior with unknown reshape targets.
src/​relax/​transform/​utils.h Adds shared shape-availability and legalization checks.
src/​relax/​transform/​legalize_ops.cc Replaces the local shape gate with the shared helper.
src/​relax/​transform/​fold_constant.cc Gates speculative legalization and restores skipped-call types.
python/​tvm/​relax/​transform/​legalize_ops/​manipulate.py Replaces a bare assertion with a descriptive error.

Review effort: Max

}
new_args.push_back(arg);
}
Type original_ty = post_call->ty;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The failure already occurs after BindParams, before FoldConstant, because InferTypeReshape puts the DataflowVar into the type. Restoring types here wouldn't fix it, so we might track it in a separate issue. Does that work, @mshr-h ?

Comment on lines +376 to +379
} else {
// Restore the original (well-formed) type instead of the `Missing` type set above.
return Call(original_ty, post_call->op, post_call->args, post_call->attrs,
post_call->ty_args, post_call->loc);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I couldn't reproduce this: for that example the type is already refined to shape[2] at parse time, so restoring it keeps the refinement.

This branch has not been deployed

No deployments
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.

3 participants