Repository navigation
[Codegen] Use overflow-checked math in the CPU and GPU alloc size checks - #24745
Conversation
devtbi
left a comment
There was a problem hiding this comment.
right idea, I'd like to see all size calculations unified
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]>
84fc675 to
486baa5
Compare
|
@egebeysel thanks for the context, #24483/#24627 definitely seem relevant. I opened a new issue with an easy reproducer for this fusion issue -- #24752. |
|
@devtbi i think i addressed all your points, can i ask you to re-review? |
There was a problem hiding this comment.
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]>
|
I see the CI error, i'll push a fix shortly. |
Signed-off-by: Zmicier Prybysh <[email protected]>
|
@dimp-pl very nice contribution :) |
checkStackAllocationSizeaccumulates 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
getStaticShapeSizeInBitsto 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-existingcheckedAdd/checkedMul/checkedAlignTowrappers in LLVMCPUAssignWorkgroupLocalMemory.cpp are replaced withllvm::MulOverflow/llvm::AddOverflow.