Skip to content

[Bugfix][Relax] Avoid dangling globals in AllocateWorkspace - #20487

Open
Nanmur wants to merge 1 commit into
apache:mainfrom
Nanmur:codex/fix-allocate-workspace-globalvar
Open

Nanmur wants to merge 1 commit into
apache:mainfrom
Nanmur:codex/fix-allocate-workspace-globalvar

Conversation

@Nanmur

@Nanmur Nanmur commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Why

AllocateWorkspace skips rewriting Codegen and Composite function bodies, but it could still rename and remove workspace-bearing global functions referenced by those bodies. This left the skipped callers with dangling GlobalVar references.

What

  • Preserve workspace-bearing globals referenced by skipped Codegen/Composite functions.
  • Keep the existing workspace rewrite for callees whose call sites can be updated.
  • Add a regression test covering main -> Codegen -> global Composite.

Fixes #20479

Testing

  • Native incremental build on Windows with MSVC and LLVM enabled.
  • python -m pytest tests/python/relax/test_transform_allocate_workspace.py -q (2 passed)
  • python tests/lint/check_file_type.py
  • git diff --check

@wwoosshh

wwoosshh commented Oct 7, 2026

Copy link
Copy Markdown

I checked this on top of current main (d55759e). The C++ change merges and builds cleanly, but the new test module uses T.Buffer, which was renamed to T.Tensor in #20519. Because NestedGlobalFunctions is defined at module level, tests/python/relax/test_transform_allocate_workspace.py now fails at collection with AttributeError: No script namespace 'Buffer', so none of the tests in that file run.

With the two T.Buffer in add_one replaced by T.Tensor, both tests in the file pass, and test_nested_global_function_is_well_formed fails without the C++ change (the well-formed check after AllocateWorkspace raises), so it covers the fix.

@Nanmur
Nanmur force-pushed the codex/fix-allocate-workspace-globalvar branch from e602837 to 506d79c Compare October 7, 2026 05:33
@Nanmur

Nanmur commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Updated in 506d79cd26.

The branch is now rebased onto d55759ec1, and the two add_one parameters use T.Tensor instead of the removed T.Buffer spelling. The C++ fix is unchanged.

Local verification completed:

  • the updated test module passes py_compile;
  • all repository pre-commit hooks for both changed files pass.

A fresh upstream CI run is starting for the rebased revision. Your confirmation that both focused tests pass and that the regression test fails without the C++ change verifies that the test still exercises the intended bug.

@wwoosshh wwoosshh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the update. I re-checked 506d79cd2 on top of main (d55759e): both tests in tests/python/relax/test_transform_allocate_workspace.py pass, test_nested_global_function_is_well_formed fails when allocate_workspace.cc is reverted to main, and pre-commit passes on the changed files.

LGTM.

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.

[Bug] AllocateWorkspace returns an ill-formed module with a dangling GlobalVar

2 participants