Skip to content

Commit 4835015

Browse files
[Flang][Driver] Add a flag to control zero initialization of global variables
Patch adds a flag to control zero initialization of global variables without default initialization. The default is to zero initialize.
1 parent 29ed600 commit 4835015

File tree

8 files changed

+57
-1
lines changed

8 files changed

+57
-1
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,6 +3494,9 @@ def fno_struct_path_tbaa : Flag<["-"], "fno-struct-path-tbaa">, Group<f_Group>;
34943494
def fno_strict_enums : Flag<["-"], "fno-strict-enums">, Group<f_Group>;
34953495
def fno_strict_overflow : Flag<["-"], "fno-strict-overflow">, Group<f_Group>,
34963496
Visibility<[ClangOption, FlangOption]>;
3497+
def fno_zero_init_global_without_init : Flag<["-"], "fno-zero-init-global-without-init">, Group<f_Group>,
3498+
Visibility<[FlangOption, FC1Option]>,
3499+
HelpText<"Do not zero initialize globals without default initialization">;
34973500
def fno_pointer_tbaa : Flag<["-"], "fno-pointer-tbaa">, Group<f_Group>;
34983501
def fno_temp_file : Flag<["-"], "fno-temp-file">, Group<f_Group>,
34993502
Visibility<[ClangOption, CC1Option, CLOption, DXCOption]>, HelpText<

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ void Flang::addCodegenOptions(const ArgList &Args,
153153
Args.addAllArgs(CmdArgs, {options::OPT_flang_experimental_hlfir,
154154
options::OPT_flang_deprecated_no_hlfir,
155155
options::OPT_fno_ppc_native_vec_elem_order,
156+
options::OPT_fno_zero_init_global_without_init,
156157
options::OPT_fppc_native_vec_elem_order});
157158
}
158159

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(ZeroInitGlobalsWithoutInit, unsigned, 1, 1)
4750
#undef LOWERINGOPT
4851
#undef ENUM_LOWERINGOPT

flang/lib/Frontend/CompilerInvocation.cpp

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

1376+
// -fno-zero-init-global-without-init
1377+
if (args.hasArg(
1378+
clang::driver::options::OPT_fno_zero_init_global_without_init)) {
1379+
invoc.loweringOpts.setZeroInitGlobalsWithoutInit(false);
1380+
}
1381+
13761382
// Preserve all the remark options requested, i.e. -Rpass, -Rpass-missed or
13771383
// -Rpass-analysis. This will be used later when processing and outputting the
13781384
// 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().getZeroInitGlobalsWithoutInit())
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
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
! Check that the driver passes through -fno-zero-init-global-without-init:
2+
! RUN: %flang -### -S -fno-zero-init-global-without-init %s -o - 2>&1 | FileCheck %s
3+
! Check that the compiler accepts -fno-zero-init-global-without-init:
4+
! RUN: %flang_fc1 -emit-hlfir -fno-zero-init-global-without-init %s -o -
5+
! CHECK: "-fc1"{{.*}}"-fno-zero-init-global-without-init"

flang/test/Lower/zero_init.f90

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

0 commit comments

Comments
 (0)