[flang-commits] [flang] [flang] Rely on global initialization for simpler derived types (PR #114002)

via flang-commits flang-commits at lists.llvm.org
Mon Jan 27 06:22:01 PST 2025


================
@@ -793,11 +794,35 @@ void Fortran::lower::defaultInitializeAtRuntime(
         })
         .end();
   } else {
-    mlir::Value box = builder.createBox(loc, exv);
-    fir::runtime::genDerivedTypeInitialize(builder, loc, box);
+    /// For "simpler" types, relying on "_FortranAInitialize"
+    /// leads to poor runtime performance. Hence optimize
+    /// the same.
+    const Fortran::semantics::DeclTypeSpec *declTy = sym.GetType();
+    mlir::Type symTy = converter.genType(var);
+    if (!var.isAlias() && !hasAllocatableDirectComponent(sym) &&
+        declTy->category() ==
+            Fortran::semantics::DeclTypeSpec::Category::TypeDerived &&
+        !mlir::isa<fir::SequenceType>(symTy) &&
+        !sym.test(Fortran::semantics::Symbol::Flag::OmpPrivate) &&
+        !sym.test(Fortran::semantics::Symbol::Flag::OmpFirstPrivate)) {
----------------
jeanPerier wrote:

No, use `Fortran::semantics::IsPolymorphic(symbol)`.
`hasDefaultInitialization` is false in my example only because the static type does not have default initialization. But if you extend the example to the following, it will fall through I think:

```
module m
 type t
  integer :: i = 1
 end type
contains
subroutine foo(x)
  class(t), intent(out) :: x
end subroutine
end module
```

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


More information about the flang-commits mailing list