[clang] [clang] Add value_type attr, use it to add noalias when pass-by-value. (PR #95004)

Florian Hahn via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 17 07:02:06 PDT 2025


================
@@ -9242,3 +9242,15 @@ Declares that a function potentially allocates heap memory, and prevents any pot
 of ``nonallocating`` by the compiler.
   }];
 }
+
+def ValueTypeDocs : Documentation {
+  let Category = DocCatDecl;
+  let Content = [{
+The ``value_type`` attribute can be used to mark user-defined types as 'value
+types'. When objects of value types are passed value to functions, the objects
+are always considered to be formally copied into a new object. This means the
+argument itself must be the only value referencing the passed object. This
----------------
fhahn wrote:

@jyknight thanks for sharing the example! I made a few changes to make it runable: https://clang.godbolt.org/z/WrE8Ga6d9

What's interesting is that we get different results with Clang with and without optimizations (without optimizations, there's a copy due to `byval`, while with optimizations to function is inlined and the byval is gone). GCC seems to create copies both with and without optimizations and gives consistent results.

On AArch64, the argument isn't marked as `byval`, so we get the same results with and w/o optimizations. But it behaves differently to X86 w/o optimizations.

So overall it seems like there already are a number of inconsistencies when trying to capture the address of a pass-by-value argument across multiple dimensions; IIUC the program should return the same results for both GCC, Clang, with opts, w/o opts and different architectures.

The attribute itself is intended to forbid such uses of the type, but I agree it is not clear if it would be possible to use other for types in libc++.

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


More information about the cfe-commits mailing list