[clang] [Clang][CodeGen] Add metadata for load from reference (PR #98746)

Yingwei Zheng via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 22 06:09:49 PDT 2024


================
@@ -2799,9 +2799,37 @@ CodeGenFunction::EmitLoadOfReference(LValue RefLVal,
   llvm::LoadInst *Load =
       Builder.CreateLoad(RefLVal.getAddress(), RefLVal.isVolatile());
   CGM.DecorateInstructionWithTBAA(Load, RefLVal.getTBAAInfo());
-  return makeNaturalAddressForPointer(Load, RefLVal.getType()->getPointeeType(),
-                                      CharUnits(), /*ForPointeeType=*/true,
-                                      PointeeBaseInfo, PointeeTBAAInfo);
+  QualType PTy = RefLVal.getType()->getPointeeType();
+  if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
+    llvm::LLVMContext &Ctx = getLLVMContext();
+    llvm::MDBuilder MDB(Ctx);
+    // Emit !dereferenceable metadata
+    Load->setMetadata(
+        llvm::LLVMContext::MD_dereferenceable,
+        llvm::MDNode::get(Ctx,
+                          MDB.createConstant(llvm::ConstantInt::get(
+                              Builder.getInt64Ty(),
----------------
dtcxzyw wrote:

See https://eel.is/c++draft/dcl.ref#4
> 9.3.4.3 References
> ...
> It is unspecified whether or not a reference requires storage

Does this statement mean a reference may not be dereferenceable?

> This problem also exists for function parameters, but I think it's much more likely to cause issues in this context.

I copied this logic from function parameters.


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


More information about the cfe-commits mailing list