[clang] [Clang][CodeGen] Emit dereferenceable and nofree for indirect arguments (PR #213347)

Sayan Sivakumaran via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 2 18:33:05 PDT 2026


================
@@ -3238,6 +3235,23 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
       assert(!Align.isZero());
       Attrs.addAlignmentAttr(Align.getQuantity());
 
+      // According to [basic.stc.auto], parameters have automatic storage
+      // duration. Therefore, the underlying object of this pointer will not be
+      // freed during the function's execution. If the parameter is realigned,
+      // this may not be true, but realignment does not currently occur for
+      // non-byval. Hmm....
+      //
+      // We can already infer noalias and nofree like optimization behavior if
+      // the byval attribute is present.
+      if (!AI.getIndirectByVal()) {
+        assert(!AI.getIndirectRealign() &&
+               "Pointer copied from realign legal to be freed?");
+        Attrs.addAttribute(llvm::Attribute::NoFree);
+        if (!ParamType->isIncompleteType() && ParamType->isConstantSizeType())
----------------
sivakusayan wrote:

Okay, fixed. 

If I understand correctly, these checks were needed for references because you can have C++ references to opaque types and C-style arrays which we don't know the size of at compile time. However, opaque types can't be passed directly, and C-style arrays will decay to pointers, so this is non-applicable.

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


More information about the cfe-commits mailing list