You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "akaashrp (via GitHub)" <gi...@apache.org> on 2026/08/30 05:13:42 UTC

[PR] [Fix][Metal] Bound symbolic stack allocations [tvm]

akaashrp opened a new pull request, #20236:
URL: https://github.com/apache/tvm/pull/20236

   Use analyzer-proven finite upper bounds when emitting fixed-size Metal arrays. Reject unbounded, nonpositive, and overflowing extents while allowing shapes such as `min(dynamic_extent, constant_limit)`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Metal] Bound symbolic stack allocations [tvm]

Posted by "akaashrp (via GitHub)" <gi...@apache.org>.
akaashrp commented on code in PR #20236:
URL: https://github.com/apache/tvm/pull/20236#discussion_r3909455950


##########
src/backend/metal/codegen/codegen_metal.cc:
##########
@@ -357,14 +359,25 @@ void CodeGenMetal::VisitStmt_(const AllocBufferNode* op) {
   std::string vid = AllocVarID(op->buffer.get());
 
   this->PrintIndent();
-  // Compute constant_size from buffer shape
+  // Compute a compile-time upper bound on the number of buffer elements.
   size_t constant_size = 1;
+  arith::Analyzer analyzer;
   for (const auto& dim : op->buffer->shape) {
-    const IntImmNode* dim_imm = dim.as<IntImmNode>();
-    TVM_FFI_ICHECK(dim_imm) << "Can only handle constant size stack allocation for now";
-    constant_size *= dim_imm->value;
+    const auto* dim_imm = dim.as<IntImmNode>();
+    int64_t dim_size = dim_imm ? dim_imm->value : analyzer->const_int_bound(dim)->max_value;
+    if (dim_imm == nullptr) {
+      const auto* dtype_max = max_value(dim.ty()).as<IntImmNode>();
+      // An integer dtype's intrinsic maximum is not a program-derived allocation bound.
+      TVM_FFI_ICHECK(dtype_max && dim_size < dtype_max->value)
+          << "Metal allocation extent requires a finite compile-time upper bound, but got " << dim;

Review Comment:
   Thanks for pointing this out. Updated in c13fab94fe: `kPosInf` is rejected independently, and the intrinsic dtype maximum is compared only when it is representable as an `IntImm`. Bounded and unbounded uint64 regressions cover both paths.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Metal] Bound symbolic stack allocations [tvm]

Posted by "akaashrp (via GitHub)" <gi...@apache.org>.
akaashrp commented on code in PR #20236:
URL: https://github.com/apache/tvm/pull/20236#discussion_r3909455950


##########
src/backend/metal/codegen/codegen_metal.cc:
##########
@@ -357,14 +359,25 @@ void CodeGenMetal::VisitStmt_(const AllocBufferNode* op) {
   std::string vid = AllocVarID(op->buffer.get());
 
   this->PrintIndent();
-  // Compute constant_size from buffer shape
+  // Compute a compile-time upper bound on the number of buffer elements.
   size_t constant_size = 1;
+  arith::Analyzer analyzer;
   for (const auto& dim : op->buffer->shape) {
-    const IntImmNode* dim_imm = dim.as<IntImmNode>();
-    TVM_FFI_ICHECK(dim_imm) << "Can only handle constant size stack allocation for now";
-    constant_size *= dim_imm->value;
+    const auto* dim_imm = dim.as<IntImmNode>();
+    int64_t dim_size = dim_imm ? dim_imm->value : analyzer->const_int_bound(dim)->max_value;
+    if (dim_imm == nullptr) {
+      const auto* dtype_max = max_value(dim.ty()).as<IntImmNode>();
+      // An integer dtype's intrinsic maximum is not a program-derived allocation bound.
+      TVM_FFI_ICHECK(dtype_max && dim_size < dtype_max->value)
+          << "Metal allocation extent requires a finite compile-time upper bound, but got " << dim;

Review Comment:
   Thanks—this is correct. Updated in c13fab94fe: `kPosInf` is rejected independently, and the intrinsic dtype maximum is compared only when it is representable as an `IntImm`. Bounded and unbounded uint64 regressions cover both paths. After rebasing onto current main, all 19 applicable Metal codegen tests and pre-commit checks pass.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Metal] Bound symbolic stack allocations [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #20236:
URL: https://github.com/apache/tvm/pull/20236


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Metal] Bound symbolic stack allocations [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex commented on code in PR #20236:
URL: https://github.com/apache/tvm/pull/20236#discussion_r3901394480


##########
src/backend/metal/codegen/codegen_metal.cc:
##########
@@ -357,14 +359,25 @@ void CodeGenMetal::VisitStmt_(const AllocBufferNode* op) {
   std::string vid = AllocVarID(op->buffer.get());
 
   this->PrintIndent();
-  // Compute constant_size from buffer shape
+  // Compute a compile-time upper bound on the number of buffer elements.
   size_t constant_size = 1;
+  arith::Analyzer analyzer;
   for (const auto& dim : op->buffer->shape) {
-    const IntImmNode* dim_imm = dim.as<IntImmNode>();
-    TVM_FFI_ICHECK(dim_imm) << "Can only handle constant size stack allocation for now";
-    constant_size *= dim_imm->value;
+    const auto* dim_imm = dim.as<IntImmNode>();
+    int64_t dim_size = dim_imm ? dim_imm->value : analyzer->const_int_bound(dim)->max_value;
+    if (dim_imm == nullptr) {
+      const auto* dtype_max = max_value(dim.ty()).as<IntImmNode>();
+      // An integer dtype's intrinsic maximum is not a program-derived allocation bound.
+      TVM_FFI_ICHECK(dtype_max && dim_size < dtype_max->value)
+          << "Metal allocation extent requires a finite compile-time upper bound, but got " << dim;

Review Comment:
   This incorrectly rejects bounded symbolic extents with dtype `uint64`.
   
   `max_value(PrimType::UInt(64))` is represented by `LargeUIntImm`, because `UINT64_MAX` cannot fit in `IntImmNode::value`. Therefore, `as<IntImmNode>()` returns null and this check fails even when the analyzer has proven a small finite upper bound.
   
   For example:
   
       def main(n: T.uint64):
           scratch = T.alloc_buffer(
               (T.min(n, T.uint64(64)),),
               "float32",
               scope="local",
           )
   
   `const_int_bound` reports `[0, 64]`, but Metal codegen rejects it with the "requires a finite compile-time upper bound" error. The equivalent `int32`, `int64`, and `uint32` cases all emit `thread float scratch[64]`.
   
   Could we check `dim_size != arith::ConstIntBound::kPosInf` independently, and only compare against the dtype maximum when it is representable as an `IntImmNode`? That would continue rejecting an unbounded `uint64` extent while accepting genuinely bounded expressions. Please also add a `uint64` regression test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org