[flang-commits] [flang] [flang][lowering] Implement component-wise initialization for derived types (PR #187465)
via flang-commits
flang-commits at lists.llvm.org
Mon Mar 23 04:16:01 PDT 2026
https://github.com/jeanPerier commented:
Thanks for working on this!
Have you tried to see what it would take to have this as a FIR to FIR pass?
That is, to try to optimize the copy of constant values instead?
My question is motivated by the fact that this is an optimization, and it while you did not introduce any kind of heuristic (like how many components have an init vs how many do not to justify that it is better to do a piece by piece copy rather than a whole copy even if some bytes are zeroed).
The second point is that you should restrict this change to some niche cases because this is breaking a common existing behavior of Fortran compiler that we support to avoid breaking existing code which is that as soon as one component has an initial value, the rest of the data is zeroed (see [this commit](https://github.com/llvm/llvm-project/commit/87e25210226086a54f44de4f1d4575fee9073721)).
Your patch is motivated by the fact that flang create extra initialization for ALLOCATABLE/POINTER components, and I think it may be worth limiting it to that exact case (that is, there is no initialization required from a language point of view, just the flang specific need to initialized the descriptors of POINTER/ALLOCATABLE components to proper state).
I think that at least by default, we should limit it because a some code out there is relying on their compiler having "pad" the user initialization with zero initialization, and LLVM is particularly aggressive at optimizing out code using undef values which makes this kinds of bugs time consuming to investigate. Some users with fully valid code may want to optimize all initialization and opt out from this extension.
So my thinking is that you should likely keep the lowering version of this change but limit it to only deals with the cases where there are POINTER/ALLOCATABLE components without init value (that flang needs to set up) and all the other components have no initial values. This should make the lowering change a lot more trivial and be portable with existing Fotran compilers.
The version that decide to do a piece by piece init version for derived type with user provided initialization should likely be a FIR to FIR pass that try optimize the copy from the constant and should be opt in for portability purposes with existing compilers.
https://github.com/llvm/llvm-project/pull/187465
More information about the flang-commits
mailing list