[clang] [flang] [flang] Add -finit-local= to initialize automatic variables (PR #216164)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 16 00:07:07 PDT 2026
================
@@ -1250,6 +1254,267 @@ getSafeRepackAttrs(Fortran::lower::AbstractConverter &converter) {
return attrs.empty() ? mlir::ArrayAttr{} : builder.getArrayAttr(attrs);
}
+//===----------------------------------------------------------------------===//
+// -finit-local= helpers
+//===----------------------------------------------------------------------===//
+
+/// Returns true when \p var is an automatic local variable eligible for
+/// -finit-local= initialization. Excluded: variables without a symbol,
+/// globals, dummy arguments, SAVE'd vars, ALLOCATABLE/POINTER, vars in
+/// an EQUIVALENCE set, vars with explicit or default initialization, and
+/// CUDA variables whose storage is not host-accessible (device, constant,
+/// shared, usedevice).
+static bool shouldInitLocal(const Fortran::lower::pft::Variable &var) {
+ if (!var.hasSymbol() || var.isGlobal())
+ return false;
+ const Fortran::semantics::Symbol &sym = var.getSymbol();
+ if (Fortran::semantics::IsDummy(sym))
+ return false;
+ if (Fortran::semantics::IsSaved(sym))
+ return false;
+ if (Fortran::semantics::IsAllocatableOrPointer(sym))
----------------
MattPD wrote:
Cray pointees pass the eligibility checks in `shouldInitLocal` even though their FIR base is a pointer-box descriptor and they own no storage. You can reproduce this by saving the following as `repro.f90`:
```fortran
integer :: res(10), x(10)
integer(8) :: p
pointer (p, x)
p = loc(res)
x(3) = 7
```
`flang -fc1 -emit-llvm -O0 -finit-local=zero repro.f90 -o -` emits a 48-byte `memcpy` from null into the pointee descriptor. With `-O2`, the function becomes `unreachable`. Compiling without `-finit-local`, or with an unpatched compiler, keeps the store to `x(3)`.
Could you exclude Cray pointees in `shouldInitLocal`? Could you reject FIR box types in the scalar fallback? Could you also add `-O0` and `-O2` LLVM regression tests?
https://github.com/llvm/llvm-project/pull/216164
More information about the cfe-commits
mailing list