How to set metadata tbaa in llvm

In C, I can store a pointer to a function in a data structure in the following ways.

void func_()
{ // … }
void* addr = (void*) func_;

When converted to IR statement, it is as follows:

store i8* bitcast (i32 ()* @main to i8*), i8** %3, align 8, !dbg !64, !tbaa !65

Now, I have created the corresponding structure in the IR phase, I have implemented element access, and I have stored the required Pointers into the specified structure. But the IR statement I get is:

store i8* bitcast (i32 ()* @main to i8*), i8** %3, align 8, !dbg !50

When I check, I find that the stored pointer is null, and its output is (nil). I think the problem should be the TBAA property, but I searched around and could not find the setting method. How should I solve this problem?

The problem is very unlikely to be TBAA. TBAA stands for “type based aliasing analysis”, which models the fact that in a language like C or C++, you’re not allowed to dereference (say) a variable thart’s really an int as a float (also known as “type punning”).

So in LLVM terms, adding !tbaa nodes just gives LLVM permission to do more aggressive optimizations than it would otherwise by describing the language’s type system, leaving them out is always more conservative.

Do you have a complete example of the kind of module you’re generating, and how you’re executing it to produce the unexpected null?

1 Like

I’m sorry. I made a stupid mistake. I rechecked my content after seeing your reply that it had nothing to do with TBAA. I did not modify the content of the output statement used for confirmation to the content of the data structure created. It now works correctly. Thank you very much!