[flang-commits] [flang] [flang] Rely on global initialization for simpler derived types (PR #114002)
via flang-commits
flang-commits at lists.llvm.org
Tue Oct 29 03:09:53 PDT 2024
================
@@ -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);
----------------
jeanPerier wrote:
Using defineGlobal may have ununited effects like setting variable attributes that are irrelevant to the global holding the initial value. It may also trigger the generation of debug info for it as if it were a user variable, which is weird.
Instead, you should create the global yourself here with the full control of its attributes using `builder.createGlobal(` and then use something like:
```
Fortran::lower::createGlobalInitialization(
builder, global, [&](fir::FirOpBuilder &builder) {
Fortran::lower::StatementContext stmtCtx(
/*cleanupProhibited=*/true);
mlir::Value initVal =
genDefaultInitializerValue(converter, loc, sym, symTy, stmtCtx);
mlir::Value castTo = builder.createConvert(loc, symTy, initVal);
builder.create<fir::HasValueOp>(loc, castTo);
});
```
https://github.com/llvm/llvm-project/pull/114002
More information about the flang-commits
mailing list