[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)) {
+      std::string globalName = converter.mangleName(sym) + "_globalinit";
+      mlir::Location loc = genLocation(converter, sym);
+      mlir::StringAttr linkage = getLinkageAttribute(builder, var);
+      cuf::DataAttributeAttr dataAttr =
+          Fortran::lower::translateSymbolCUFDataAttribute(builder.getContext(),
+                                                          sym);
+      fir::GlobalOp global =
+          defineGlobal(converter, var, globalName, linkage, dataAttr);
+      auto addrOf = builder.create<fir::AddrOfOp>(loc, global.resultType(),
+                                                  global.getSymbol());
+      fir::LoadOp load = builder.create<fir::LoadOp>(loc, addrOf.getResult());
+      builder.create<fir::StoreOp>(loc, load, fir::getBase(exv));
----------------
jeanPerier wrote:

No, see comment above, but with the polymorphic exclusion, you should be fine until support for array is added.

Also, derived types may be huge (when containing an array component), so it is better to stay away from load/store pattern from them (see https://github.com/flang-compiler/f18-llvm-project/pull/1293).

I think we will want to call memcpy in the end. Please add a FIXME, I will introduce a FIR operation for that. 

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


More information about the flang-commits mailing list