[clang] [Clang][CodeGen] Emit load of GEP after EmitMemberExpr (PR #110487)

Jan Hendrik Farr via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 1 10:31:59 PDT 2024


================
@@ -1160,23 +1162,10 @@ llvm::Value *CodeGenFunction::EmitLoadOfCountedByField(
   if (!StructBase || StructBase->HasSideEffects(getContext()))
     return nullptr;
 
-  llvm::Value *Res = nullptr;
-  if (const auto *DRE = dyn_cast<DeclRefExpr>(StructBase)) {
-    Res = EmitDeclRefLValue(DRE).getPointer(*this);
-    Res = Builder.CreateAlignedLoad(ConvertType(DRE->getType()), Res,
-                                    getPointerAlign(), "dre.load");
-  } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(StructBase)) {
-    LValue LV = EmitMemberExpr(ME);
-    Address Addr = LV.getAddress();
-    Res = Addr.emitRawPointer(*this);
-  } else if (StructBase->getType()->isPointerType()) {
-    LValueBaseInfo BaseInfo;
-    TBAAAccessInfo TBAAInfo;
-    Address Addr = EmitPointerWithAlignment(StructBase, &BaseInfo, &TBAAInfo);
-    Res = Addr.emitRawPointer(*this);
-  } else {
-    return nullptr;
-  }
+  LValueBaseInfo BaseInfo;
+  TBAAAccessInfo TBAAInfo;
+  Address Addr = EmitPointerWithAlignment(StructBase, &BaseInfo, &TBAAInfo);
----------------
Cydox wrote:

In the example C file that causes the compiler to segfault, `StructBase` is not a pointer here, so this fails.

Here's a dump of StructBase:
```
MemberExpr 0x1240b890 'struct variable' lvalue ->growable 0x1240ac20
`-ImplicitCastExpr 0x1240b878 'struct bucket *' <LValueToRValue>
  `-DeclRefExpr 0x1240b858 'struct bucket *' lvalue Var 0x1240afe8 'p' 'struct bucket *'
```

So you have to distinguish between the MemberExpr and the pointer case right here, at which point you are basically back to my PR (although I guess the case for `DeclRefExpr` can be removed additionally; that does break tests, so I'll check carefully it the behavior is still correct with that case removed)


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


More information about the cfe-commits mailing list