[llvm-dev] Creating a global variable for a struct array
Tim Northover via llvm-dev
llvm-dev at lists.llvm.org
Wed Sep 23 01:04:31 PDT 2020
On Tue, 22 Sep 2020 at 23:52, Niddodi, Chaitra via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> dhash* h1 = new dhash[10];
Note that this "new" expression needs a runtime call to actually
allocate the memory on the heap. In LLVM terms this is A
GlobalVariable with type PointerType::get(StructTy, ...), initialized
to null, together with a global init function (runs when the program
starts) that makes the call to new and stores it to that global. My
guess is that's not what you intended
> I also need to allocate space for:
> 1) the field llist in struct dhash which is a pointer to another struct dlist and
> 2) the field dptr in struct dlist
>
> Is there an example that I can refer to for doing this ?
I think you should start by writing exactly what you want in C++, and
then (when you're happy with the assembly output) using Clang's "-S
-emit-llvm" options to see what LLVM IR that produces.
That will clear up the question of how things should be allocated,
which isn't quite obvious from the questions you're asking, and make
translating the IR into LLVM API calls more straightforward.
> I tried to create a GlobalVariable using ConstantStruct::get(StructType*, ArrayRef<Constant *>). I'm not sure how to get the second argument of type ArrayRef<Constant *> from the above variable h1.
Hopefully it'll be clearer when you have the real IR you want (in
textual form) in front of you.
Cheers.
Tim.
More information about the llvm-dev
mailing list