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

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 21 03:25:01 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(),
----------------
nikic wrote:

I don't think this is safe. It's not spelled out in LangRef, but effectively `!dereferenceable` metadata means "dereferenceable while this SSA value is in scope". References in C++ are allowed to become dangling, as long as they are not used (in the sense of lvalue to rvalue conversion).

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

Note that rustc also does not emit dereferenceable on function returns and loads, only on parameters, for this reason.

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


More information about the cfe-commits mailing list