[flang-commits] [flang] [flang] Propagate INTENT(IN) dummy arguments as readonly (PR #207732)
Sergey Shcherbinin via flang-commits
flang-commits at lists.llvm.org
Tue Jul 7 06:31:55 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,
----------------
SergeyShch01 wrote:
Thanks for the comment. I considered deriving readonly from [hl]fir.declare, but chose explicit propagation from CallInterface, where both the dummy’s semantic characteristics and FIR argument position are known. This follows an existing pattern: CallInterface attaches fir.target and fir.asynchronous to function arguments for later use by FunctionAttr. Some duplication already exists - FIR AliasAnalysis explicitly notes that TARGET is represented both on the declaration and the entry-block argument.
At the current FunctionAttr position, PreCGRewrite has already replaced fir.declare with its memref or converted it to fircg.ext_declare, which does not preserve fortran_attrs. Using that information would require an earlier stage, moving or splitting FunctionAttr, or changing PreCGRewrite to preserve it.
Declaration-to-argument mapping is implementable - CUF already has a specialized implementation using dummy_arg_no - but a generic solution needs additional care. After inlining, callee declarations have separate dummy_scopes and may refer to caller arguments even though the callee’s INTENT(IN) does not constrain the whole caller. A getMemref() fallback may also need to traverse packing, views, and copy-in chains, while excluding locals and temporaries. Bodyless declarations have no fir.declare. This is error-prone in both directions: a false negative loses the optimization, while a false positive creates an invalid readonly contract and may miscompile. Explicit propagation avoids this reconstruction: the marker is attached to the known FIR argument and later consumed by exact index. This is why I preferred the small duplication.
https://github.com/llvm/llvm-project/pull/207732
More information about the flang-commits
mailing list