Skip to content
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3494,6 +3494,9 @@ def fno_struct_path_tbaa : Flag<["-"], "fno-struct-path-tbaa">, Group<f_Group>;
def fno_strict_enums : Flag<["-"], "fno-strict-enums">, Group<f_Group>;
def fno_strict_overflow : Flag<["-"], "fno-strict-overflow">, Group<f_Group>,
Visibility<[ClangOption, FlangOption]>;
def fno_zero_init_global_without_init : Flag<["-"], "fno-zero-init-global-without-init">, Group<f_Group>,
Visibility<[FlangOption, FC1Option]>,
HelpText<"Do not zero initialize globals without default initialization">;
def fno_pointer_tbaa : Flag<["-"], "fno-pointer-tbaa">, Group<f_Group>;
def fno_temp_file : Flag<["-"], "fno-temp-file">, Group<f_Group>,
Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>, HelpText<
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ void Flang::addCodegenOptions(const ArgList &Args,
Args.addAllArgs(CmdArgs, {options::OPT_flang_experimental_hlfir,
options::OPT_flang_deprecated_no_hlfir,
options::OPT_fno_ppc_native_vec_elem_order,
options::OPT_fno_zero_init_global_without_init,
options::OPT_fppc_native_vec_elem_order});
}

Expand Down
3 changes: 3 additions & 0 deletions flang/include/flang/Lower/LoweringOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
/// If false, assume that the shapes/types/allocation-status match.
ENUM_LOWERINGOPT(ReallocateLHS, unsigned, 1, 1)

/// If true, initialize globals without initialization to zero.
/// On by default.
ENUM_LOWERINGOPT(ZeroInitGlobalsWithoutInit, unsigned, 1, 1)
#undef LOWERINGOPT
#undef ENUM_LOWERINGOPT
6 changes: 6 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,12 @@ bool CompilerInvocation::createFromArgs(
invoc.loweringOpts.setNoPPCNativeVecElemOrder(true);
}

// -fno-zero-init-global-without-init
if (args.hasArg(
clang::driver::options::OPT_fno_zero_init_global_without_init)) {
invoc.loweringOpts.setZeroInitGlobalsWithoutInit(false);
}

// Preserve all the remark options requested, i.e. -Rpass, -Rpass-missed or
// -Rpass-analysis. This will be used later when processing and outputting the
// remarks generated by LLVM in ExecuteCompilerInvocation.cpp.
Expand Down
6 changes: 5 additions & 1 deletion flang/lib/Lower/ConvertVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,11 @@ static fir::GlobalOp defineGlobal(Fortran::lower::AbstractConverter &converter,
global.setLinkName(builder.createCommonLinkage());
Fortran::lower::createGlobalInitialization(
builder, global, [&](fir::FirOpBuilder &builder) {
mlir::Value initValue = builder.create<fir::ZeroOp>(loc, symTy);
mlir::Value initValue;
if (converter.getLoweringOptions().getZeroInitGlobalsWithoutInit())
initValue = builder.create<fir::ZeroOp>(loc, symTy);
else
initValue = builder.create<fir::UndefOp>(loc, symTy);
builder.create<fir::HasValueOp>(loc, initValue);
});
}
Expand Down
5 changes: 5 additions & 0 deletions flang/test/Driver/fno-zero-init.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
! Check that the driver passes through -fno-zero-init-global-without-init:
! RUN: %flang -### -S -fno-zero-init-global-without-init %s -o - 2>&1 | FileCheck %s
! Check that the compiler accepts -fno-zero-init-global-without-init:
! RUN: %flang_fc1 -emit-hlfir -fno-zero-init-global-without-init %s -o -
! CHECK: "-fc1"{{.*}}"-fno-zero-init-global-without-init"
16 changes: 16 additions & 0 deletions flang/test/Lower/zero_init.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
! RUN: %flang_fc1 -fno-zero-init-global-without-init -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-NO-ZERO-INIT %s

module m1
real :: x
end module m1

!CHECK-DEFAULT: fir.global @_QMm1Ex : f32 {
!CHECK-DEFAULT: %[[UNDEF:.*]] = fir.zero_bits f32
!CHECK-DEFAULT: fir.has_value %[[UNDEF]] : f32
!CHECK-DEFAULT: }

!CHECK-NO-ZERO-INIT: fir.global @_QMm1Ex : f32 {
!CHECK-NO-ZERO-INIT: %[[UNDEF:.*]] = fir.undefined f32
!CHECK-NO-ZERO-INIT: fir.has_value %[[UNDEF]] : f32
!CHECK-NO-ZERO-INIT: }
18 changes: 18 additions & 0 deletions flang/test/Lower/zero_init_default_init.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s
! RUN: %flang_fc1 -fno-zero-init-global-without-init -emit-hlfir -o - %s | FileCheck %s

! Test that the flag does not affect globals with default init

module m2
type val
integer :: my_val = 1
end type val
type(val) :: v1
end module m2

!CHECK: fir.global @_QMm2Ev1 : !fir.type<_QMm2Tval{my_val:i32}> {
!CHECK: %[[V1:.*]] = fir.undefined !fir.type<_QMm2Tval{my_val:i32}>
!CHECK: %[[ONE:.*]] = arith.constant 1 : i32
!CHECK: %[[V1_INIT:.*]] = fir.insert_value %[[V1]], %[[ONE]], ["my_val", !fir.type<_QMm2Tval{my_val:i32}>] : (!fir.type<_QMm2Tval{my_val:i32}>, i32) -> !fir.type<_QMm2Tval{my_val:i32}>
!CHECK: fir.has_value %[[V1_INIT]] : !fir.type<_QMm2Tval{my_val:i32}>
!CHECK: }
Loading