Beginner LLVM Questions

I’ve recently started using LLVM IR generation APIs for my project. The documentation and LLC tool are pretty helpful, but I haven’t been able to find answer to the following -

All code that creates LLVM IR instructions using their APIs seems to “new” instructions instead of creating them as stack variables.

auto x = new Alloca(...) vs. Alloca(...) x;

I am wondering what the ownership semantics are for the pointers that are created? Do I have to call delete on these instruction objects. All code that I’ve seen calls “delete engine;”.

I am guessing that all this memory is owned by this Module object and when the module is destroyed, all the memory occupied by these instructions are also destroyed. By looking at some of the code, it seems that all these instruction objects are created by using “placement new” … Is this understanding correct?

It would be great if someone can explain the memory allocation schema used by these APIs.

Thanks!

I've recently started using LLVM IR generation APIs for my project. The
documentation and LLC tool are pretty helpful, but I haven't been able to
find answer to the following -

All code that creates LLVM IR instructions using their APIs seems to "new"
instructions instead of creating them as stack variables.

auto x = new Alloca(...) vs. Alloca(...) x;

I am wondering what the ownership semantics are for the pointers that are
created? Do I have to call delete on these instruction objects. All code
that I've seen calls "delete engine;".

I am guessing that all this memory is owned by this Module object and when
the module is destroyed, all the memory occupied by these instructions are
also destroyed.

That's correct. There are various operations on Instructions/Basic
Blocks/etc that allow you to remove an Instruction/BB/etc from a
BB/Function/Module, etc and take ownership so you can insert it back in
somewhere else if you want to.

By looking at some of the code, it seems that all these instruction
objects are created by using "placement new" ... Is this understanding
correct?

Overloaded operator new, to be more precise - this is just to play some
memory tricks so we can allocate objects more efficiently (take a look at
User::operator new, for a/the canonical example) - performing a single
allocation for an object and its dynamic but constant-at-constructor-time
sequence of operands (or whatever) after it.

- David