[flang-commits] [clang] [flang] [flang] Add -finit-local= to initialize automatic variables (PR #216164)
Daniel Chen via flang-commits
flang-commits at lists.llvm.org
Sun Aug 16 19:42:20 PDT 2026
================
@@ -1250,6 +1254,236 @@ 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, and vars with explicit or default initialization.
+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))
+ return false;
+ if (Fortran::lower::hasDefaultInitialization(sym))
+ return false;
+ if (const auto *obj =
+ sym.detailsIf<Fortran::semantics::ObjectEntityDetails>())
+ if (obj->init())
+ return false;
+ if (Fortran::semantics::FindEquivalenceSet(sym))
+ return false;
+ return true;
----------------
DanielCChen wrote:
> Managed host storage now initializes correctly.
>
> In `attributes(global) subroutine test_global_local`, the user-declared `n` still receives `data_attr=device` and remains uninitialized. The only store of zero targets the compiler-created `__builtin_warpsize`.
>
> Deleting the declaration of `n` and the assignment `n = 42` from the new test leaves all three new CHECK lines passing.
>
> Could you account for device execution when deciding whether to initialize a local? Could you also make the test verify that the store targets the user-declared `n`?
Here it is as plain text you can copy:
Fixed. `SetImplicitCUDADevice` assigns `device` to every local in a device subprogram, so the old check was too broad. `shouldInitLocal` now uses `IsCUDADeviceContext(&sym.owner())` to distinguish implicit per-thread stack locals (initialize) from explicit global device memory (skip). The test is tightened to anchor the `fir.store` to `_QFtest_global_localEn` via `hlfir.declare`.
https://github.com/llvm/llvm-project/pull/216164
More information about the flang-commits
mailing list