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

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Thu Jul 9 06:12: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
----------------
eugeneepshteyn wrote:

To clarify, this code
```
subroutine modit(x)
  integer, intent(in), target :: x
  integer, pointer :: p
  p => x
  p = 42
end subroutine
```
... would produce this LLVM IR?
```
define void @modit_(ptr readonly captures(none) %0) ... {
  store i32 42, ptr %0, align 4     ; writes THROUGH the "readonly" arg
  ret void
}
```
Wouldn't this result in bad code after optimizations? Maybe should exclude TARGET dummy args from readonly.

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


More information about the flang-commits mailing list