In C language, a basic block will dealloc(maybe, i’m not sure) inner variable. but the mlir block seem has not this ability.
In fact, what i want to know is how to dealloc some temporary variable that only is passed to a function as arguments.
In C language, a basic block will dealloc(maybe, i’m not sure) inner variable. but the mlir block seem has not this ability.
In fact, what i want to know is how to dealloc some temporary variable that only is passed to a function as arguments.
C lexical scopes are different from basic blocks, let’s not confound the two. There is also no real notion of “variable” in MLIR, values are immutable by construction, however the value may be a memory address where the value in the memory can be stored into (note that it doesn’t make the memory address value mutable).
Now, with clear terminology, function arguments are values. They are immutable and can be thought of as being located in hardware registers (even if is not necessarily the case in practice, they behave as if they are, lower level of the compilation stack will take care of spilling them onto stack and cleaning it up). There is never a need to “deallocate” a value.
However, if the value is a memory address, that memory must have been allocated somehow. If the allocation requested automatic deallocation, e.g,. memref.alloca
or llvm.alloca
, the deallocation will happen automatically when the control flow exits the closest ancestor operation with AutomaticAllocationScope
trait, such as a func.func
, llvm.func
or memref.alloca_scope
. If the allocation did not, e.g., memref.alloc
or llvm.call @malloc
, the memory must be deallocated by the corresponding operation, e.g., memref.free
or llvm.call @free
. Note that the mere fact of passing a value of pointer-like type into a function does not mean it has to be deallocated in the function.
i’m sorry for my fault express costing your time.
my real intention is dealloca a stack memory from alloca operation. i know that it will auto recycle when return statement executed. but if there is a function, it receive a “object” as argument and i alloca a space for preparing this object and pass the address of the object to this function.
In fact, this object only used in this function. but it have not be dealloced when return from this function.
now, let us loop calling this function, you will find that the stack is leaked.
so, i want to know how dealloc a stack space except for return statement.
my real intention is dealloca a stack memory from alloca operation.
You cannot do that. The whole point of alloc_a is automatic deallocation.
and i alloca a space for preparing this object
It will be deallocated whenever the control flow exits an operation with the AutomaticAllocationScope
trait surrounding that allocation. It does not have to be the surrounding function. If you want this to happen earlier, you can wrap the code with memref.alloca_scope
, so the deallocation happens when the control flow exits this operation. If you are lowering to LLVM, you should see the calls to stacksave
and stackrestrore
intrinsics.
thank you very much.