LLVM dialect: replacing malloc and free with custom functions

I’m developing a project for which custom heap allocation/deallocation instructions have to be used (also for profiling reasons), but I’m clashing against the hardcoded malloc and free.

I really would like to reuse the Memref dialect, but I have been obliged to basically reimplement the AllocOp and its conversion for my own mydialect::AllocOp operation.

Would it be possible to specify the desired name of such functions, maybe by means of some options for pass itself? I know I could just fork the LLVM-monorepo and change them, but it seems an overkill for such a small feature.

You can also provide custom patterns for converting memref.alloc/free and assemble your own version of the memref-to-llvm pass rather easily.

The suggested change sounds reasonable so feel free to submit a patch.

I would also want to move from malloc/free to something like _mlir_alloc and _mlir_free by default, maybe just using those names and providing custom implementations would also work.

You can also provide custom patterns for converting memref.alloc/free and assemble your own version of the memref-to-llvm pass rather easily.

I thought about this but it seemed to me a copy-paste of the existing work, which I already had to do as I could not use the memref dialect.

The suggested change sounds reasonable so feel free to submit a patch

I never contributed to the project but I will try

I would also want to move from malloc/free to something like _mlir_alloc and _mlir_free by default, maybe just using those names and providing custom implementations would also work.

This sounds even better than the “options” way, but I didn’t know it was good enough in the long term perspective.
I will proceed in changing the names to _mlir_alloc and _mlir_free and implementing them in the runtime library as wrappers to malloc and free.

1 Like

I uploaded the patch on Phabricator. First time doing this, so please let me know if there’s something wrong: ⚙ D128791 [MLIR] Generic 'malloc', 'aligned_alloc' and 'free' functions