[flang-commits] [flang] [flang] Propagate INTENT(IN) dummy arguments as readonly (PR #207732)

Sergey Shcherbinin via flang-commits flang-commits at lists.llvm.org
Sat Jul 11 10:23:35 PDT 2026


================
@@ -187,6 +187,30 @@ asImplicitArg(Fortran::evaluate::characteristics::DummyDataObject &&dummy) {
                                                        std::move(shape)));
 }
 
+/// An INTENT(IN) data object passed by reference is not modified by the callee,
+/// so the LLVM `readonly` argument attribute can be set. Notes on the
+/// exclusions:
+///   - VALUE, POINTER, and ALLOCATABLE dummies require ABI- or
+///     descriptor-specific handling before `readonly` can be applied and are
+///     left out of scope;
+///   - ASYNCHRONOUS memory may change underneath the callee;
+///   - VOLATILE+INTENT(IN) is prohibited by the standard (C870); kept here
+///     defensively.
+/// TARGET and derived types are intentionally allowed: any write that
----------------
SergeyShch01 wrote:

Thanks for raising this. I checked the exact case against F2023. The pointer association itself is valid in a non-PURE procedure:
p => x
However, the subsequent assignment is nonconforming because it defines the target x:
p = 42
F2023 8.5.10p2 states that a nonpointer INTENT(IN) dummy argument shall neither be defined nor become undefined during procedure execution. This is not caught by C846 because p, rather than the name x, appears in the variable-definition context, so a compile-time diagnostic is not required.
Malcolm Cohen, editor of the Fortran standard, states the same in [J3/25-179r1](https://j3-fortran.org/doc/year/25/25-179r1.txt) with this exact example:
REAL, INTENT(IN), TARGET :: x
REAL, POINTER :: p
p => x ! Valid.
and explicitly:
	`The standard does forbid subsequent modification of an INTENT(IN) or PROTECTED target via the pointer.`
Flang’s Aliasing.md treats this as a conservative analysis hole because such a write is difficult to diagnose statically, but that does not make the write conforming under 8.5.10p2.
Therefore, LLVM readonly remains valid for INTENT(IN), TARGET dummy arguments in conforming programs. TARGET must still prevent noalias and nocapture, which the patch already handles independently.

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


More information about the flang-commits mailing list