Add an alloca op to the std dialect

This is a proposal for adding an std.alloca op that allocates memory on the stack - it is in other ways (syntax / operands wise esp.) same as alloc except that the memory is automatically released (when the surrounding func like op returns). It readily lowers to LLVM’s alloca with the same semantics. In addition to the benefits of stack allocation, support for alignment comes out of the box on the LLVM path (even by default, an alloca is aligned to something that works well with the type) unlike for alloc’s that are currently lowered via heap allocations.

Here’s the patch that introduces this:

With this, we could also drop the -use-alloca cl option hack that uses stack allocation for all alloc’s or none when lowering to the LLVM dialect. Instead, the alloca op could be used where needed and one could also have a pass/rewriter to convert alloc’s to alloca’s.

There has been discussion (links below) on whether to make the alloc op configurable (with attributes) to model stack allocations or use a separate alloca op since the latter doesn’t require a dealloc and is thus ‘very different’ from heap allocations. Since we have Op Interfaces, being able to see both as one where needed and reuse methods across both isn’t an issue any more. Having two separate ops thus is a lot simpler for a lot of passes / canonicalizations that have to look for / deal with matching dealloc’s (for eg. dead alloc/dealloc removal, etc.).

  1. Redirecting to Google Groups

  2. Configurable call-back method name for AllocOp and DeallocOp by nmostafa · Pull Request #55 · tensorflow/mlir · GitHub