gemini-code-assist[bot] commented on code in PR #18874:
URL: https://github.com/apache/tvm/pull/18874#discussion_r2885797582
##########
src/tir/transform/lower_tvm_builtin.cc:
##########
@@ -212,10 +220,26 @@ class BuiltinLower : public StmtExprMutator {
}
}
- Stmt VisitStmt_(const LetStmtNode* op) final {
+ Stmt VisitStmt_(const SeqStmtNode* op) final {
+ Stmt result = StmtExprMutator::VisitStmt_(op);
+ if (!pending_nd_frees_.empty()) {
+ const auto* seq = result.as<SeqStmtNode>();
+ if (seq) {
+ ffi::Array<Stmt> new_seq(seq->seq.begin(), seq->seq.end());
+ for (const auto& free_stmt : pending_nd_frees_) {
+ new_seq.push_back(free_stmt);
+ }
+ pending_nd_frees_.clear();
+ return SeqStmt(new_seq);
+ }
+ }
+ return result;
+ }
Review Comment:


The use of `pending_nd_frees_` to handle `nd_mem_alloc_with_scope`
deallocations is flawed. By pushing `free` statements to a global list and
appending them to the nearest enclosing `SeqStmt`, the pass may incorrectly
hoist a `free` call out of a conditional branch (e.g., an `IfThenElse` block).
If the allocation was conditional but the `free` is now unconditional and
outside that branch, it will lead to a free of an uninitialized or null pointer
if the allocation branch was not taken. This can result in memory corruption or
a crash on the target device. Additionally, if a `Bind` with an allocation is
not directly inside a `SeqStmt`, the `free` call might be lost or appended to
an unrelated `SeqStmt` later in the function.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]