Skip to content

Commit c593e3d

Browse files
[Flang][Driver] Add a flag to control zero initialization of global v… (#122144)
…ariables Patch adds a flag to control zero initialization of global variables without default initialization. The default is to zero initialize.
1 parent e9504c5 commit c593e3d

File tree

8 files changed

+69
-2
lines changed

8 files changed

+69
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,11 @@ def fno_struct_path_tbaa : Flag<["-"], "fno-struct-path-tbaa">, Group<f_Group>;
35053505
def fno_strict_enums : Flag<["-"], "fno-strict-enums">, Group<f_Group>;
35063506
def fno_strict_overflow : Flag<["-"], "fno-strict-overflow">, Group<f_Group>,
35073507
Visibility<[ClangOption, FlangOption]>;
3508+
defm init_global_zero : BoolOptionWithoutMarshalling<"f", "init-global-zero",
3509+
PosFlag<SetTrue, [], [FlangOption, FC1Option],
3510+
"Zero initialize globals without default initialization (default)">,
3511+
NegFlag<SetFalse, [], [FlangOption, FC1Option],
3512+
"Do not zero initialize globals without default initialization">>;
35083513
def fno_pointer_tbaa : Flag<["-"], "fno-pointer-tbaa">, Group<f_Group>;
35093514
def fno_temp_file : Flag<["-"], "fno-temp-file">, Group<f_Group>,
35103515
Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>, HelpText<

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ void Flang::addCodegenOptions(const ArgList &Args,
155155
options::OPT_flang_deprecated_no_hlfir,
156156
options::OPT_fno_ppc_native_vec_elem_order,
157157
options::OPT_fppc_native_vec_elem_order,
158-
options::OPT_ftime_report, options::OPT_ftime_report_EQ});
158+
options::OPT_finit_global_zero,
159+
options::OPT_fno_init_global_zero, options::OPT_ftime_report,
160+
options::OPT_ftime_report_EQ});
159161
}
160162

161163
void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {

flang/include/flang/Lower/LoweringOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,8 @@ ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)
4444
/// If false, assume that the shapes/types/allocation-status match.
4545
ENUM_LOWERINGOPT(ReallocateLHS, unsigned, 1, 1)
4646

47+
/// If true, initialize globals without initialization to zero.
48+
/// On by default.
49+
ENUM_LOWERINGOPT(InitGlobalZero, unsigned, 1, 1)
4750
#undef LOWERINGOPT
4851
#undef ENUM_LOWERINGOPT

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,14 @@ bool CompilerInvocation::createFromArgs(
13731373
invoc.loweringOpts.setNoPPCNativeVecElemOrder(true);
13741374
}
13751375

1376+
// -f[no-]init-global-zero
1377+
if (args.hasFlag(clang::driver::options::OPT_finit_global_zero,
1378+
clang::driver::options::OPT_fno_init_global_zero,
1379+
/*default=*/true))
1380+
invoc.loweringOpts.setInitGlobalZero(true);
1381+
else
1382+
invoc.loweringOpts.setInitGlobalZero(false);
1383+
13761384
// Preserve all the remark options requested, i.e. -Rpass, -Rpass-missed or
13771385
// -Rpass-analysis. This will be used later when processing and outputting the
13781386
// remarks generated by LLVM in ExecuteCompilerInvocation.cpp.

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,11 @@ static fir::GlobalOp defineGlobal(Fortran::lower::AbstractConverter &converter,
635635
global.setLinkName(builder.createCommonLinkage());
636636
Fortran::lower::createGlobalInitialization(
637637
builder, global, [&](fir::FirOpBuilder &builder) {
638-
mlir::Value initValue = builder.create<fir::ZeroOp>(loc, symTy);
638+
mlir::Value initValue;
639+
if (converter.getLoweringOptions().getInitGlobalZero())
640+
initValue = builder.create<fir::ZeroOp>(loc, symTy);
641+
else
642+
initValue = builder.create<fir::UndefOp>(loc, symTy);
639643
builder.create<fir::HasValueOp>(loc, initValue);
640644
});
641645
}

flang/test/Driver/fno-zero-init.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! Check that the driver passes through -f[no-]init-global-zero:
2+
! RUN: %flang -### -S -finit-global-zero %s -o - 2>&1 | FileCheck --check-prefix=CHECK-POS %s
3+
! RUN: %flang -### -S -fno-init-global-zero %s -o - 2>&1 | FileCheck --check-prefix=CHECK-NEG %s
4+
! Check that the compiler accepts -f[no-]init-global-zero:
5+
! RUN: %flang_fc1 -emit-hlfir -finit-global-zero %s -o -
6+
! RUN: %flang_fc1 -emit-hlfir -fno-init-global-zero %s -o -
7+
8+
! CHECK-POS: "-fc1"{{.*}}"-finit-global-zero"
9+
! CHECK-NEG: "-fc1"{{.*}}"-fno-init-global-zero"

flang/test/Lower/zero_init.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
2+
! RUN: %flang_fc1 -finit-global-zero -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
3+
! RUN: %flang_fc1 -fno-init-global-zero -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-NO-ZERO-INIT %s
4+
5+
module m1
6+
real :: x
7+
end module m1
8+
9+
!CHECK-DEFAULT: fir.global @_QMm1Ex : f32 {
10+
!CHECK-DEFAULT: %[[UNDEF:.*]] = fir.zero_bits f32
11+
!CHECK-DEFAULT: fir.has_value %[[UNDEF]] : f32
12+
!CHECK-DEFAULT: }
13+
14+
!CHECK-NO-ZERO-INIT: fir.global @_QMm1Ex : f32 {
15+
!CHECK-NO-ZERO-INIT: %[[UNDEF:.*]] = fir.undefined f32
16+
!CHECK-NO-ZERO-INIT: fir.has_value %[[UNDEF]] : f32
17+
!CHECK-NO-ZERO-INIT: }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s
2+
! RUN: %flang_fc1 -finit-global-zero -emit-hlfir -o - %s | FileCheck %s
3+
! RUN: %flang_fc1 -fno-init-global-zero -emit-hlfir -o - %s | FileCheck %s
4+
5+
! Test that the flag does not affect globals with default init
6+
7+
module m2
8+
type val
9+
integer :: my_val = 1
10+
end type val
11+
type(val) :: v1
12+
end module m2
13+
14+
!CHECK: fir.global @_QMm2Ev1 : !fir.type<_QMm2Tval{my_val:i32}> {
15+
!CHECK: %[[V1:.*]] = fir.undefined !fir.type<_QMm2Tval{my_val:i32}>
16+
!CHECK: %[[ONE:.*]] = arith.constant 1 : i32
17+
!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}>
18+
!CHECK: fir.has_value %[[V1_INIT]] : !fir.type<_QMm2Tval{my_val:i32}>
19+
!CHECK: }

0 commit comments

Comments
 (0)