I have a small qs relating to how ArrayRef is used in storing information w.r.t mlir::TypeStorage.
I am trying to follow the idiom for type creation as given in llvm-project/Dialect.cpp at main · llvm/llvm-project · GitHub
On that line we are using ‘llvm::ArrayRef’ used to store type information for the created type.
But as ArrayRef is just a pointer to some memory, where is this buffer allocated ? Does it count on calling code to keep the array alive ?
If you look a few lines above, the ArrayRef is copied into the storage allocator that was provided when constructing the storage instance. This keeps the data live for the duration of the storage instance.
You only really need to copy into the allocator if the parameter “references” something with a lifetime that isn’t guaranteed to be at least as long as the new type storage instance. For ArrayRef, you need to allocate because it generally contains a pointer to some temporary data (often stack allocated). Type, on the other hand, points to data that is guaranteed to outlive your type storage instance, so you don’t need to copy anything for it.