Skip to content

Commit c4ecdf6

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 c4ecdf6

File tree

7 files changed

+50
-2
lines changed

7 files changed

+50
-2
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static llvm::cl::opt<bool>
5050
allowAssumedRank("allow-assumed-rank",
5151
llvm::cl::desc("Enable assumed rank lowering"),
5252
llvm::cl::init(true));
53-
5453
#define DEBUG_TYPE "flang-lower-variable"
5554

5655
/// Helper to lower a scalar expression using a specific symbol mapping.
@@ -635,7 +634,11 @@ static fir::GlobalOp defineGlobal(Fortran::lower::AbstractConverter &converter,
635634
global.setLinkName(builder.createCommonLinkage());
636635
Fortran::lower::createGlobalInitialization(
637636
builder, global, [&](fir::FirOpBuilder &builder) {
638-
mlir::Value initValue = builder.create<fir::ZeroOp>(loc, symTy);
637+
mlir::Value initValue;
638+
if (converter.getLoweringOptions().getZeroInitGlobalsWithoutInit())
639+
initValue = builder.create<fir::ZeroOp>(loc, symTy);
640+
else
641+
initValue = builder.create<fir::UndefOp>(loc, symTy);
639642
builder.create<fir::HasValueOp>(loc, initValue);
640643
});
641644
}

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: 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 %s
2+
! RUN: %flang_fc1 -fno-zero-init-global-without-init -emit-hlfir -o - %s | FileCheck %s
3+
4+
module m2
5+
type val
6+
integer :: my_val = 1
7+
end type val
8+
type(val) :: v1
9+
end module m2
10+
11+
!CHECK: fir.global @_QMm2Ev1 : !fir.type<_QMm2Tval{my_val:i32}> {
12+
!CHECK: %[[V1:.*]] = fir.undefined !fir.type<_QMm2Tval{my_val:i32}>
13+
!CHECK: %[[ONE:.*]] = arith.constant 1 : i32
14+
!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}>
15+
!CHECK: fir.has_value %[[V1_INIT]] : !fir.type<_QMm2Tval{my_val:i32}>
16+
!CHECK: }

0 commit comments

Comments
 (0)