[clang] [CIR] Upstream initial support for union type (PR #137501)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 28 06:56:51 PDT 2025


================
@@ -317,20 +317,25 @@ LValue CIRGenFunction::emitLValueForField(LValue base, const FieldDecl *field) {
   }
 
   unsigned recordCVR = base.getVRQualifiers();
-  if (rec->isUnion()) {
-    cgm.errorNYI(field->getSourceRange(), "emitLValueForField: union");
-    return LValue();
-  }
 
-  assert(!cir::MissingFeatures::preservedAccessIndexRegion());
   llvm::StringRef fieldName = field->getName();
-  const CIRGenRecordLayout &layout =
-      cgm.getTypes().getCIRGenRecordLayout(field->getParent());
-  unsigned fieldIndex = layout.getCIRFieldNo(field);
 
-  assert(!cir::MissingFeatures::lambdaFieldToName());
+  if (rec->isUnion()) {
+    unsigned fieldIndex = field->getFieldIndex();
+    assert(!cir::MissingFeatures::lambdaFieldToName());
----------------
erichkeane wrote:

It seems to me that the only thing that should be in the if/else here is:

unions: the call to `getFieldIndex`/init of `fieldIndex`
Non-unions: assert for `PeservedAccessIndexRegion`/layout init, `fieldIndex` init.

The `addr` creation and the `emitAddrOfFieldStorage` are both identical, so it seems that the we could make sure these stay in sync better by taking them out of the conditional.

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


More information about the cfe-commits mailing list