Skip to content

[Codegen] Use overflow-checked math in the CPU and GPU alloc size checks - #24745

Merged
AGindinson merged 3 commits into
iree-org:mainfrom
dimp-pl:llvmcpu-stack-check-overflow
Sep 4, 2026
Merged

AGindinson merged 3 commits into
iree-org:mainfrom
dimp-pl:llvmcpu-stack-check-overflow

Conversation

@dimp-pl

@dimp-pl dimp-pl commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

checkStackAllocationSize accumulates the allocation size in a plain int64_t. Allocas sized by the upper bound of an unbounded dynamic dimension (the util.assume.int default, 2^53-1) overflow it, the wrapped value passes the limit check and the alloca reaches ConvertToLLVM, where its element count folds to llvm.mlir.poison and the program loads and stores through a garbage pointer at runtime.

The reproduction was reduced from the quantized ResNet downsample construct: an ONNX model with a dynamic batch doing DequantizeLinear -> 1x1 stride-2 Conv compiles without diagnostics and returns completely wrong results.

Move getStaticShapeSizeInBits to Codegen/Utils (taken from #24684) and use it from both LLVMCPUCheckIRBeforeLLVMConversion and GPUCheckResourceUsage. Callers supply the leaf element bit width,
which lets the GPU check keep its index-bitwidth handling and the recursion into shaped element types. On the CPU side the remaining scalar steps are overflow-checked in place with llvm::MulOverflow / llvm::AddOverflow. The pre-existing checkedAdd/checkedMul/checkedAlignTo wrappers in LLVMCPUAssignWorkgroupLocalMemory.cpp are replaced with llvm::MulOverflow / llvm::AddOverflow.

@dimp-pl
dimp-pl requested a review from hanhanW as a code owner July 22, 2026 14:46

@devtbi devtbi 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.

right idea, I'd like to see all size calculations unified

@egebeysel

Copy link
Copy Markdown
Contributor

JFYI at first glance, these seem relevant:

#24483
#24627
#24532

@egebeysel

Copy link
Copy Markdown
Contributor

Allocas sized by the upper bound of an unbounded dynamic dimension (the util.assume.int default, 2^53-1)

This guardrail is good, though I think independent of that, we should not have these allocas. Seems like a tiling/fusion bug. Can you create an issue with a reproducer so that we can also take a look at what's causing those?

`checkStackAllocationSize` accumulates the allocation size in a plain
int64_t. Allocas sized by the upper bound of an unbounded dynamic
dimension (the util.assume.int default, 2^53-1) overflow it, the
wrapped value passes the limit check and the alloca reaches
ConvertToLLVM, where its element count folds to llvm.mlir.poison and
the program loads and stores through a garbage pointer at runtime.

The reproduction was reduced from the quantized ResNet downsample
construct: an ONNX model with a dynamic batch doing DequantizeLinear
-> 1x1 stride-2 Conv compiles without diagnostics and returns
completely wrong results.

Move `getStaticShapeSizeInBits` to Codegen/Utils (taken from iree-org#24684)
and use it from both LLVMCPUCheckIRBeforeLLVMConversion and
GPUCheckResourceUsage. Callers supply the leaf element bit width,
which lets the GPU check keep its index-bitwidth handling and the
recursion into shaped element types. On the CPU side the remaining
scalar steps are overflow-checked in place with `llvm::MulOverflow` /
`llvm::AddOverflow`. The pre-existing `checkedAdd`/`checkedMul`/
`checkedAlignTo` wrappers in LLVMCPUAssignWorkgroupLocalMemory.cpp
are likewise replaced with `llvm::MulOverflow` / `llvm::AddOverflow`.

Signed-off-by: Zmicier Prybysh <[email protected]>
@dimp-pl
dimp-pl force-pushed the llvmcpu-stack-check-overflow branch from 84fc675 to 486baa5 Compare July 23, 2026 16:10
@dimp-pl dimp-pl changed the title [Codegen][LLVMCPU] Use overflow-checked math in the stack allocation size check [Codegen] Use overflow-checked math in the stack allocation size check Jul 23, 2026
@dimp-pl dimp-pl changed the title [Codegen] Use overflow-checked math in the stack allocation size check [Codegen] Use overflow-checked math in the CPU and GPU alloc size checks Jul 23, 2026
@dimp-pl

dimp-pl commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

@egebeysel thanks for the context, #24483/#24627 definitely seem relevant. I opened a new issue with an easy reproducer for this fusion issue -- #24752.

@egebeysel
egebeysel requested a review from devtbi July 24, 2026 13:32
@dimp-pl

dimp-pl commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@devtbi i think i addressed all your points, can i ask you to re-review?

@devtbi devtbi 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.

sorry for taking so long, I missed to publish the review and forgot about it.
Thanks a lot for unifying LLVMCPU and GPU - that will help a lot :)

review in summary: missing a bit of testing coverage + a few small improvements. Let me know if you want/need help.

- GPUCheckResourceUsage: refactor, add overflow check for the alignment
  round-up and the accumulation + add tests for corner cases.
- LLVMCPUCheckIRBeforeLLVMConversion: refactor, remove the "unbouned in
  practice" message, add more tests for corner cases.

Signed-off-by: Zmicier Prybysh <[email protected]>

@devtbi devtbi 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.

thanks a lot :)

@dimp-pl

dimp-pl commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

I see the CI error, i'll push a fix shortly.

@AGindinson
AGindinson enabled auto-merge (squash) September 4, 2026 09:15
@AGindinson
AGindinson merged commit cdf4e1f into iree-org:main Sep 4, 2026
57 checks passed
@devtbi

devtbi commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@dimp-pl very nice contribution :)

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.

4 participants