[libcxx-commits] [flang] [libc] [compiler-rt] [libcxx] [clang-tools-extra] [clang] [llvm] [Clang] Generate the GEP instead of adding AST nodes (PR #73730)

John McCall via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 18 13:57:14 PST 2023


================
@@ -3946,6 +4080,32 @@ static Address emitArraySubscriptGEP(CodeGenFunction &CGF, Address addr,
   return Address(eltPtr, CGF.ConvertTypeForMem(eltType), eltAlign);
 }
 
+static bool GetFieldOffsetInBits(CodeGenFunction &CGF, const RecordDecl *RD,
+                                 const FieldDecl *FD, uint64_t &Offset) {
+  ASTContext &Ctx = CGF.getContext();
+  const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
+  unsigned FieldNo = 0;
+
+  for (const Decl *D : RD->decls()) {
+    if (const auto *Record = dyn_cast<RecordDecl>(D))
+      if (GetFieldOffsetInBits(CGF, Record, FD, Offset)) {
+        Offset += Layout.getFieldOffset(FieldNo);
+        return true;
+      }
+
+    if (const auto *Field = dyn_cast<FieldDecl>(D))
+      if (FD == Field) {
+        Offset += Layout.getFieldOffset(FieldNo);
+        return true;
+      }
+
+    if (isa<FieldDecl>(D))
+      ++FieldNo;
+  }
----------------
rjmccall wrote:

I am deeply confused by the code in this function.  Why are we visiting every member of the record?

It looks like the caller has the exact field path you need, but it's throwing that away and doing a brute-force recursive search.  Just take a `NamedDecl` here and require it to be either a `FieldDecl` or an `IndirectFieldDecl`.  Or better yet, add that as a method to `ASTRecordLayout`.

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


More information about the libcxx-commits mailing list