Deallocation of type storage

I am having a problem in which I get asan errors in the cases where my type storage class allocates memory (e.g. via llvm::SmallString/SmallVector, std::string, etc).

It seems obvious to me that the destructors of the members in the type storage class are never called in my case. I.e. short string works since they don’t allocate on the heap, and we can play around with the SmallString/Vector length to avoid heap allocation and eliminate the asan errors, but that does not make for a very robust implementation.

Is this a known problem or something that has to be solved on our types with some pattern?

Type storage instances live in a dedicated arena allocator inside the context. When the context dies, the entire arena is discarded. If you need heap-like allocation, you should use the the mlir::TypeStorageAllocator that is passed as first argument into the <Type>::construct method and only keep pointers (ArrayRef, StringRef) to the allocated data in the TypeStorage subclass. You should not attempt any other allocation in the storage class. An example is available in the tutorial, note how copyInto is used to store an array.

This mechanism is necessary for type uniquing and immortality-in-context: types are pointer-comparable and remain live within the context even if nothing uses them at the moment.

1 Like