[llvm] Add flags check to createVariantMemberType (PR #139261)

Tom Tromey via llvm-commits llvm-commits at lists.llvm.org
Fri May 9 06:05:11 PDT 2025


https://github.com/tromey created https://github.com/llvm/llvm-project/pull/139261

I noticed that DIDerivedType overloads the "ExtraData" member depending on the precise type being implemented.  A variant part uses this to store the discriminant (a reference to another member), but a bit field uses it to store the storage offset.

This patch changes createVariantMemberType to ensure that the FlagBitField is not used when creating a variant part.  If this flag is used, the ExtraData field would be erroneously used in two different ways.

The patch also updates a comment in DIDerivedType to list a couple more cases.

>From 57c02a46e7b907e092e2c1d63433b745b9fa6245 Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey at adacore.com>
Date: Fri, 9 May 2025 06:52:46 -0600
Subject: [PATCH] Add flags check to createVariantMemberType

I noticed that DIDerivedType overloads the "ExtraData" member
depending on the precise type being implemented.  A variant part uses
this to store the discriminant (a reference to another member), but a
bit field uses it to store the storage offset.

This patch changes createVariantMemberType to ensure that the
FlagBitField is not used when creating a variant part.  If this flag
is used, the ExtraData field would be erroneously used in two
different ways.

The patch also updates a comment in DIDerivedType to list a couple
more cases.
---
 llvm/include/llvm/IR/DebugInfoMetadata.h | 3 ++-
 llvm/lib/IR/DIBuilder.cpp                | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index d82c69aebb026..8531424cc136f 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -1253,7 +1253,8 @@ class DIDerivedType : public DIType {
   ///
   /// Class type for pointer-to-members, objective-c property node for ivars,
   /// global constant wrapper for static members, virtual base pointer offset
-  /// for inheritance, or a tuple of template parameters for template aliases.
+  /// for inheritance, a tuple of template parameters for template aliases,
+  /// discriminant for a variant, or storage offset for a bit field.
   ///
   /// TODO: Separate out types that need this extra operand: pointer-to-member
   /// types and member fields (static members and ivars).
diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp
index d9cc49fdad89c..bf34dd5014323 100644
--- a/llvm/lib/IR/DIBuilder.cpp
+++ b/llvm/lib/IR/DIBuilder.cpp
@@ -438,6 +438,9 @@ DIDerivedType *DIBuilder::createVariantMemberType(
     DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNumber,
     uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
     Constant *Discriminant, DINode::DIFlags Flags, DIType *Ty) {
+  // "ExtraData" is overloaded for bit fields and for variants, so
+  // make sure to disallow this.
+  assert((Flags & DINode::FlagBitField) == 0);
   return DIDerivedType::get(
       VMContext, dwarf::DW_TAG_member, Name, File, LineNumber,
       getNonCompileUnitScope(Scope), Ty, SizeInBits, AlignInBits, OffsetInBits,



More information about the llvm-commits mailing list