Skip to content

[Codegen] Fix GPU allocation size calculation overflows - #24684

Closed
devtbi wants to merge 1 commit into
iree-org:mainfrom
devtbi:devtbi/fix-spirv-alloc-overflow
Closed

devtbi wants to merge 1 commit into
iree-org:mainfrom
devtbi:devtbi/fix-spirv-alloc-overflow

Conversation

@devtbi

@devtbi devtbi commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

allocation size calculation should not be done in int
unify between GPU and CPU

Co-authored: Claude (which apprently loves lambdas)
needs a bit of cleanup

allocation should not be done in int
unify between GPU and CPU

Signed-off-by: Tobias Fuchs <[email protected]>
dimp-pl added a commit to dimp-pl/iree that referenced this pull request Jul 23, 2026
`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]>
AGindinson pushed a commit that referenced this pull request Sep 4, 2026
…cks (#24745)

`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`.

---------

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

devtbi commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

fixed in a more general PR collab

@devtbi devtbi closed this Sep 16, 2026
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.

1 participant