Skip to content

[Codegen] Don't hoist statically-bound allocations with bogus huge bounds - #24627

Open
Alex-Wengg wants to merge 1 commit into
iree-org:mainfrom
Alex-Wengg:fix-24483-hoist-umax-alloca
Open

Alex-Wengg wants to merge 1 commit into
iree-org:mainfrom
Alex-Wengg:fix-24483-hoist-umax-alloca

Conversation

@Alex-Wengg

@Alex-Wengg Alex-Wengg commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

hoistOneStaticallyBoundAllocation uses the result of computeConstantBound
as the static shape of the hoisted alloca. For a dim derived from an
unconstrained index value that bound is the type-level 2^53 - 1, so the
#24483 reproducer hoists to memref.alloca() : memref<4503599627370496xi32>
(~16 PiB) with no diagnostic.

Since #24745 the LLVMCPU stack check is overflow-safe, so there this now fails
to compile instead of miscompiling. VMVX bufferizes through the same
cpuAllocationFn but has no such check, so it still gets the bogus static
alloca silently.

Fix

Don't hoist when the product of the constant bounds exceeds 2^32 elements; the
alloca stays dynamic, which is always valid. Scalable bounds are skipped in the
product (vscale >= 1, so it is still a lower bound).

The cap is a fixed, backend-agnostic sanity limit rather than the target's
max_stack_allocation_size (as in #24527): with
--iree-llvmcpu-fail-on-out-of-bounds-stack-allocation=false the stack check
is off, and a budget-based cap would stop hoisting legitimately large tiles.
Real tiles are KB to MB, far below 2^32 elements.

Testing

Fixes #24483.

Assisted-by: Claude Code

@Ha-Eyz

Ha-Eyz commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Hey,

I also ran into the same problem as #24483 for the VMVX backend and the changes in this PR would fix this for me, so I wanted to ask if you still intend to merge these changes or not?

…unds

hoistOneStaticallyBoundAllocation used the result of computeConstantBound
verbatim as the static dimension of a hoisted alloca. An unconstrained
`index` operand carries the trivial type-level upper bound (2^53 - 1), so a
single `affine.apply` (e.g. `ceildiv 2`) turns it into a ~2^52-element static
`memref.alloca` (~16 PiB), emitted silently. Downstream stride/size math then
overflows i64 and the dispatch reads/writes undefined memory.

Refuse to hoist when the product of the constant bounds exceeds a generous
element-count cap (2^32), leaving the allocation dynamic, which is always
valid. Scalable bounds are skipped in the product (vscale >= 1, so it is still
a lower bound). The cap is intentionally backend-agnostic rather than the
target's max_stack_allocation_size: the stack check still enforces the real
limit downstream on LLVMCPU, and a budget-based cap would stop hoisting
legitimately large tiles when that check is disabled.

Fixes iree-org#24483.

Signed-off-by: Alex-Wengg <[email protected]>
@Alex-Wengg
Alex-Wengg force-pushed the fix-24483-hoist-umax-alloca branch from 950920c to bb85bb4 Compare November 16, 2026 16:09
@Alex-Wengg
Alex-Wengg marked this pull request as ready for review November 16, 2026 16:35

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.

hoistOneStaticallyBoundAllocation produces bogus 2^52-element static allocas when dynamic dim bounds default to the index umax

2 participants