Link Struct to Metadata


Given one structure type, is there any method to get the metadata that describes the structure information?

For example:

%2 = getelementptr inbounds %“class.std::thread”, %“class.std::thread”* %this, i64 0, i32 0, i32 0, !dbg !2748

then I know the pointer operand type is class.std::thread. I want to find the associated metadata of this type:

!217 = !DICompositeType(tag: DW_TAG_class_type, name: “thread”, scope: !34, file: !218, line: 60, size: 64, align: 64, elements: !219, identifier: “_ZTSSt6thread”)

The challenge is that the structure name is different in the IR instruction, from the one in the metadata. One is class.std::thread, while another is _ZTSSt6thread.

What you are describing is not really possible.

  • %“class.std::thread” is an LLVM type in the LLVM type system.
  • !217 is an LLVM IR metadata node that describes a C++ type.

There exists no direct mapping between these two. The only link between the two is when there is a source variable of that type described by a llvm.dbg.value or llvm.dbg.declare intrinsic, such as a

  • call @llvm.dbg.value(metadata %s, i64 0, metadata !217, metadata !DIExpression())

Can you explain your use-case some more? Perhaps there exists a better solution to what you are trying to do.

– adrian

Thanks, Adrian.

Currently I’m writing a LLVM pass to do some statistics of C++ class member usage.The analysis requires C++ class information, like members, functions, etc. Since there is no such information retained in the LLVM IR, I’m utilizing the metadata (like DICompositeType, and so on) to retrieve the class information in LLVM IR. Therefore I need a mapping from the StructType to the MDString.

I understand that there is no direct mapping now, and want to know the best way to implement such a map. Now I’m looking into the clang front end, to understand how the StructType and the MDString are created, and hope there are some connections between these two there.

I’m still quite new to LLVM, and will be very happy if you can give me some suggestions.