diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md index fbeada77d6443..cf6b65c0a6f5e 100644 --- a/flang/docs/Extensions.md +++ b/flang/docs/Extensions.md @@ -99,6 +99,7 @@ end * `<>` as synonym for `.NE.` and `/=` * `$` and `@` as legal characters in names * Initialization in type declaration statements using `/values/` +* Saved integer, logical and real scalars are zero initialized. * Kind specification with `*`, e.g. `REAL*4` * `DOUBLE COMPLEX` as a synonym for `COMPLEX(KIND(0.D0))` -- but not when spelled `TYPE(DOUBLECOMPLEX)`. diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp index 65ae694d23f66..c0343356a84af 100644 --- a/flang/lib/Lower/ConvertVariable.cpp +++ b/flang/lib/Lower/ConvertVariable.cpp @@ -495,7 +495,9 @@ static fir::GlobalOp defineGlobal(Fortran::lower::AbstractConverter &converter, } else { TODO(loc, "global"); // Procedure pointer or something else } - // Creates undefined initializer for globals without initializers + // Creates zero or undefined initializer for globals without initializers + // Zero initializer is used for "simple types" (integer, real and logical), + // undefined is used for types aside from those types. if (!globalIsInitialized(global)) { // TODO: Is it really required to add the undef init if the Public // visibility is set ? We need to make sure the global is not optimized out @@ -507,8 +509,12 @@ static fir::GlobalOp defineGlobal(Fortran::lower::AbstractConverter &converter, TODO(loc, "BIND(C) module variable linkage"); Fortran::lower::createGlobalInitialization( builder, global, [&](fir::FirOpBuilder &builder) { - builder.create( - loc, builder.create(loc, symTy)); + mlir::Value initValue; + if (symTy.isa()) + initValue = builder.create(loc, symTy); + else + initValue = builder.create(loc, symTy); + builder.create(loc, initValue); }); } // Set public visibility to prevent global definition to be optimized out diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp index 72f02cb425299..c96ce573e9c4d 100644 --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -3283,7 +3283,7 @@ struct ZeroOpConversion : public FIROpConversion { rewriter.replaceOpWithNewOp(zero, ty); } else if (ty.isa()) { rewriter.replaceOpWithNewOp( - zero, ty, mlir::IntegerAttr::get(zero.getType(), 0)); + zero, ty, mlir::IntegerAttr::get(ty, 0)); } else if (mlir::LLVM::isCompatibleFloatingPointType(ty)) { rewriter.replaceOpWithNewOp( zero, ty, mlir::FloatAttr::get(zero.getType(), 0.0)); diff --git a/flang/test/Lower/target_definition.f90 b/flang/test/Lower/target_definition.f90 index 2bde8b31c4a1d..88b31e4598d43 100644 --- a/flang/test/Lower/target_definition.f90 +++ b/flang/test/Lower/target_definition.f90 @@ -2,7 +2,7 @@ ! Test TARGET attributes on a definition of a global symbol. ! CHECK: fir.global @_QMtarget_modEx target : f32 { -! CHECK: %[[init:.*]] = fir.undefined f32 +! CHECK: %[[init:.*]] = fir.zero_bits f32 ! CHECK: fir.has_value %[[init]] : f32 ! CHECK: }