[clang] [flang] [llvm] [flang] Add -finit-local= to initialize automatic variables (PR #216164)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 23:04:06 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:

I confirmed that compilation at `-O0` and `-O2` now preserves pointer association and the user-written store. At neither level does the compiler emit a null-source descriptor copy or `unreachable`.

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


More information about the llvm-commits mailing list