Search code examples
llvmclangllvm-clang

How to preserve struct member identifiers when compiling to LLVM IR with Clang?


Say I have the following C structure definition:

struct stringStructure
{
    char *stringVariable;
};

For the above, Clang produces the following LLVM IR:

%struct.stringStructure = type { i8* }

...which includes everything in my definition except the variable identifier stringVariable.

I'd like to find some way to export the identifier into the generated LLVM IR, so that I can refer to it by name from my application (which uses the LLVM C++ API).

I've tried adding the annotate attribute, as follows:

    char *stringVariable __attribute__((annotate("stringVariable")));

...but the annotation doesn't seem to make it through (the structure is still just defined as type { i8* }).

Any ideas?


Solution

  • LLVM IR does not preserve many features of the source language. You can check this via the code like

    struct Foo {
       char *a;
    };
    
    struct Bar {
       char *a;
    };
    

    Only one type will be emitted at all since types in LLVM are structurally equivalent. If you want to preserve arbitrary information about the constructs of your source code you have to emit/use debug information.