[clang] [CIR] Introduce data_member_offset attribute (PR #208777)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 15 06:51:42 PDT 2026


================
@@ -1415,11 +1415,17 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &value,
     const auto *fieldDecl = cast<FieldDecl>(memberDecl);
     const auto *mpt = destType->castAs<MemberPointerType>();
     const auto *destClass = mpt->getMostRecentCXXRecordDecl();
-    if (fieldDecl->hasAttr<NoUniqueAddressAttr>()) {
-      assert(!cir::MissingFeatures::noUniqueAddressLayout());
-      cgm.errorNYI("ConstExprEmitter::tryEmitPrivate: no_unique_address field");
-      return {};
+
+    // Empty [[no_unique_address]] fields have no CIR field index; represent the
+    // pointer-to-data-member by its concrete byte offset.
+    if (cgm.isEmptyFieldForMemberPointer(fieldDecl)) {
+      const ASTContext &astContext = cgm.getASTContext();
+      CharUnits offset =
+          astContext.getMemberPointerPathAdjustment(value) +
----------------
erichkeane wrote:

THIS case:
```
struct FirstField { int a; };
struct HoldsNUA { int b; [[no_unique_address]] EmptyBase e; };
struct DerivesNUA : FirstField, HoldsNUA {};
EmptyBase DerivesNUA::* nua_in_base = &DerivesNUA::e;
// CIR-BEFORE-DAG: cir.global external @nua_in_base = #cir.data_member_offset<4> : !cir.data_member<!rec_EmptyBase in !rec_DerivesNUA>
// CIR-AFTER-DAG: cir.global external @nua_in_base = #cir.int<4> : !s64i
// LLVM-DAG: @nua_in_base = global i64 4
```

Does.  the '4' comes from the base offset.  That said, the field offset is `0` thanks to NUA, so I'll add a 2nd one to that so that it ends up having a '5' as well. 

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


More information about the cfe-commits mailing list