Why do we need MDString for metadata in LLVM

Hi, I am new to llvm and have some question about metadata:
Why do we use the MDString class to store string? Why do not just use the string to store?

The short answer as to why MDString is necessary is because everything that can be stuffed into metadata needs to have a common base class (in this case, Metadata). So strings and integers end up needing to be packed into ConstantAsMetadata and MDString, respectively.

Thanks for your reply. You mean the operand of a MDNode is the subclass of metadata.

And I have another question:
What is the MDTuple used for? Is there an example to show? I just can not imagine where should we use the MDTuple.

I think most of the non-debug metadata uses tuples in some way, it’s just a list of other nodes. For example compiling an empty file:

!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7}
!llvm.ident = !{!8}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 1, !"branch-target-enforcement", i32 0}
!2 = !{i32 1, !"sign-return-address", i32 0}
!3 = !{i32 1, !"sign-return-address-all", i32 0}
!4 = !{i32 1, !"sign-return-address-with-bkey", i32 0}
!5 = !{i32 7, !"PIC Level", i32 2}
!6 = !{i32 7, !"uwtable", i32 2}
!7 = !{i32 7, !"frame-pointer", i32 1}
!8 = !{!"clang version 15.0.0 (ssh://git@repo hash)"}

All of those !{ ... } are MDTuples I believe.

1 Like

Thanks a lot! I traced with GDB and confirm it. You are right.