Skip to content

Commit 920faa8

Browse files
authored
[Fix][LLVM] Preserve 64-bit AllocBuffer extents (#20141)
Fixes #20125. ## What changed LLVM CPU code generation now keeps constant `AllocBuffer` extents as signed 64-bit values through stack allocation emission instead of narrowing them to `int32_t`. The temporary-allocation alignment helper accepts the same width and avoids multiplying when a large allocation cannot reduce the requested alignment, preventing overflow in that calculation. This change is intentionally limited to the CPU `CodeGenLLVM` path. The separate NVPTX and AMDGPU overrides described in the issue remain out of scope. ## Why An extent of `2**32 + 1` previously wrapped to one before `CreateAlloca`, producing a one-element stack allocation followed by an `i64 4294967296` element access in unoptimized TIR-X LLVM IR. The generated module could therefore contain an out-of-bounds access even though the original extent was valid as an `int64_t`. The regression test only compiles and inspects LLVM IR; it does not execute or materialize the very large allocation. It covers extents that previously wrapped to one and four elements. ## Validation - New regression on the unmodified base: 2 failures - New regression after the fix: 2 passed - `tests/python/codegen/test_target_codegen_llvm.py`: 380 passed - Rebuilt all 65 affected C++ units, including the NVPTX and AMDGPU callers of the alignment helper - Changed-file pre-commit hooks and `git diff --check`: passed - Compile-only boundary probes through `INT64_MAX - 3`: exact `i64` allocation counts, with no allocation executed ## AI assistance This PR was prepared with OpenAI Codex. The patch was derived from the repository and the issue's documented failure mode; no external third-party code was copied into the change. Generated-by: OpenAI Codex Co-authored-by: Igor Stadnyk <[email protected]>
1 parent d8fcef2 commit 920faa8

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎src/target/llvm/codegen_llvm.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ void CodeGenLLVM::VisitStmt_(const AllocBufferNode* op) {
21912191

21922192
const IntImmNode* dim_imm = op->buffer->shape[0].as<IntImmNode>();
21932193
TVM_FFI_ICHECK(dim_imm) << "Can only handle constant size stack allocation";
2194-
int32_t constant_size = static_cast<int32_t>(dim_imm->value);
2194+
int64_t constant_size = dim_imm->value;
21952195
TVM_FFI_ICHECK_GT(constant_size, 0) << "Can only handle constant size stack allocation";
21962196

21972197
StorageInfo& info = alloc_storage_info_[op->buffer.get()];
@@ -2206,7 +2206,7 @@ void CodeGenLLVM::VisitStmt_(const AllocBufferNode* op) {
22062206
info.alignment = 16;
22072207
}
22082208
llvm::AllocaInst* alloca = WithFunctionEntry([&]() {
2209-
return builder_->CreateAlloca(DTypeToLLVMType(op->buffer->dtype), ConstInt32(constant_size));
2209+
return builder_->CreateAlloca(DTypeToLLVMType(op->buffer->dtype), ConstInt64(constant_size));
22102210
});
22112211
auto alignment = static_cast<unsigned>(alloca->getAlign().value());
22122212
if (alignment < static_cast<unsigned>(info.alignment)) {

‎src/tirx/transform/ir_utils.h‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,17 @@ inline PrimType APIType(const PrimType& t) {
181181
* \param const_size The constant size of the array.
182182
* \return the alignment
183183
*/
184-
inline int GetTempAllocaAlignment(const PrimType& type, int32_t const_size) {
184+
inline int GetTempAllocaAlignment(const PrimType& type, int64_t const_size) {
185185
int align = runtime::kTempAllocaAlignment;
186186
if (const_size > 0) {
187-
int64_t const_s = static_cast<int64_t>(const_size) * type.StorageBytes();
188-
while (align > const_s) {
189-
align = align / 2;
187+
int64_t element_bytes = type.StorageBytes();
188+
// Only compute the total size when it can reduce the alignment. This also avoids
189+
// overflowing for very large allocations.
190+
if (element_bytes > 0 && const_size <= (align - 1) / element_bytes) {
191+
int64_t const_s = const_size * element_bytes;
192+
while (align > const_s) {
193+
align = align / 2;
194+
}
190195
}
191196
}
192197
return align;

‎tests/python/codegen/test_target_codegen_llvm.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,29 @@ def Kirby(v: T.float32) -> T.float32:
957957
assert matches == sorted(matches)
958958

959959

960+
@pytest.mark.skipif(not env.has_llvm(), reason="need llvm")
961+
@pytest.mark.parametrize("extent", [2**32 + 1, 2**32 + 4])
962+
def test_llvm_large_stack_allocation_uses_64bit_extent(extent):
963+
@T.prim_func(s_tir=True)
964+
def main(A: T.Buffer((1,), "float32")):
965+
B = T.alloc_buffer(
966+
(extent,),
967+
"float32",
968+
scope="global",
969+
annotations={"disable_lower_builtin": True},
970+
)
971+
A[0] = B[extent - 1]
972+
973+
module = tvm.tirx.build(
974+
main,
975+
target={"kind": "llvm", "opt-level": 0},
976+
pipeline="tirx",
977+
)
978+
llvm_ir = module.inspect_source("ll")
979+
980+
assert re.search(rf"alloca float, i64 {extent}(?:,|$)", llvm_ir)
981+
982+
960983
@pytest.mark.skipif(not env.has_llvm(), reason="need llvm")
961984
@tvm.testing.skip_if_32bit
962985
def test_llvm_import():

0 commit comments

Comments
 (0)