[llvm] [BPF] Support for `DW_TAG_variant_part` in BTF generation (PR #155783)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 30 10:45:17 PDT 2025


================
@@ -301,21 +303,62 @@ void BTFTypeStruct::completeType(BTFDebug &BDebug) {
 
   BTFType.NameOff = BDebug.addString(STy->getName());
 
+  uint64_t InitialOffset = 0;
+  if (STy->getTag() == dwarf::DW_TAG_variant_part) {
+    // Variant parts have a discriminator, which has its own memory location at
+    // the beginning, and variants, which share the memory location afterwards.
+    // LLVM DI doesn't consider discriminator as an element and instead keeps
+    // it as a separate reference.
+    // To keep BTF simple, let's represent the structure as an union with
+    // discriminator as the first element and apply offsets to the other
+    // elements.
+    struct BTF::BTFMember Discriminator;
+    const auto *DDTy = STy->getDiscriminator();
+
+    InitialOffset += DDTy->getOffsetInBits() + DDTy->getSizeInBits();
+
+    Discriminator.NameOff = BDebug.addString(DDTy->getName());
+    Discriminator.Offset = DDTy->getOffsetInBits();
+    const auto *BaseTy = tryRemoveAtomicType(DDTy->getBaseType());
----------------
yonghong-song wrote:

I do not think we need tryRemoveAtomicType(). Did you find a use case that tryRemoveAtomicType() could be necessary?

https://github.com/llvm/llvm-project/pull/155783


More information about the llvm-commits mailing list