Skip to content

Commit a58bb99

Browse files
Revert "Revert "[Flang][Driver] Add a flag to control zero initialization of global v…" (llvm#123067)"
This reverts commit 44ba43a. Adds the flag to bbc as well.
1 parent ff1b01b commit a58bb99

File tree

9 files changed

+82
-3
lines changed

9 files changed

+82
-3
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,10 @@ 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,
159-
options::OPT_funroll_loops, options::OPT_fno_unroll_loops});
158+
options::OPT_finit_global_zero,
159+
options::OPT_fno_init_global_zero, options::OPT_ftime_report,
160+
options::OPT_ftime_report_EQ, options::OPT_funroll_loops,
161+
options::OPT_fno_unroll_loops});
160162
}
161163

162164
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
@@ -1377,6 +1377,14 @@ bool CompilerInvocation::createFromArgs(
13771377
invoc.loweringOpts.setNoPPCNativeVecElemOrder(true);
13781378
}
13791379

1380+
// -f[no-]init-global-zero
1381+
if (args.hasFlag(clang::driver::options::OPT_finit_global_zero,
1382+
clang::driver::options::OPT_fno_init_global_zero,
1383+
/*default=*/true))
1384+
invoc.loweringOpts.setInitGlobalZero(true);
1385+
else
1386+
invoc.loweringOpts.setInitGlobalZero(false);
1387+
13801388
// Preserve all the remark options requested, i.e. -Rpass, -Rpass-missed or
13811389
// -Rpass-analysis. This will be used later when processing and outputting the
13821390
// 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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
! RUN: bbc -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
5+
! RUN: bbc -finit-global-zero -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
6+
! RUN: bbc -finit-global-zero=false -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-NO-ZERO-INIT %s
7+
8+
module m1
9+
real :: x
10+
end module m1
11+
12+
!CHECK-DEFAULT: fir.global @_QMm1Ex : f32 {
13+
!CHECK-DEFAULT: %[[UNDEF:.*]] = fir.zero_bits f32
14+
!CHECK-DEFAULT: fir.has_value %[[UNDEF]] : f32
15+
!CHECK-DEFAULT: }
16+
17+
!CHECK-NO-ZERO-INIT: fir.global @_QMm1Ex : f32 {
18+
!CHECK-NO-ZERO-INIT: %[[UNDEF:.*]] = fir.undefined f32
19+
!CHECK-NO-ZERO-INIT: fir.has_value %[[UNDEF]] : f32
20+
!CHECK-NO-ZERO-INIT: }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
5+
! RUN: bbc -finit-global-zero -emit-hlfir -o - %s | FileCheck %s
6+
! RUN: bbc -finit-global-zero=false -emit-hlfir -o - %s | FileCheck %s
7+
8+
! Test that the flag does not affect globals with default init
9+
10+
module m2
11+
type val
12+
integer :: my_val = 1
13+
end type val
14+
type(val) :: v1
15+
end module m2
16+
17+
!CHECK: fir.global @_QMm2Ev1 : !fir.type<_QMm2Tval{my_val:i32}> {
18+
!CHECK: %[[V1:.*]] = fir.undefined !fir.type<_QMm2Tval{my_val:i32}>
19+
!CHECK: %[[ONE:.*]] = arith.constant 1 : i32
20+
!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}>
21+
!CHECK: fir.has_value %[[V1_INIT]] : !fir.type<_QMm2Tval{my_val:i32}>
22+
!CHECK: }

flang/tools/bbc/bbc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ static llvm::cl::opt<bool> integerWrapAround(
234234
llvm::cl::desc("Treat signed integer overflow as two's complement"),
235235
llvm::cl::init(false));
236236

237+
static llvm::cl::opt<bool> initGlobalZero(
238+
"finit-global-zero",
239+
llvm::cl::desc("Zero initialize globals without default initialization"),
240+
llvm::cl::init(true));
241+
237242
static llvm::cl::opt<bool>
238243
reallocateLHS("frealloc-lhs",
239244
llvm::cl::desc("Follow Fortran 2003 rules for (re)allocating "
@@ -381,6 +386,7 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
381386
loweringOptions.setNoPPCNativeVecElemOrder(enableNoPPCNativeVecElemOrder);
382387
loweringOptions.setLowerToHighLevelFIR(useHLFIR || emitHLFIR);
383388
loweringOptions.setIntegerWrapAround(integerWrapAround);
389+
loweringOptions.setInitGlobalZero(initGlobalZero);
384390
loweringOptions.setReallocateLHS(reallocateLHS);
385391
std::vector<Fortran::lower::EnvironmentDefault> envDefaults = {};
386392
Fortran::frontend::TargetOptions targetOpts;

0 commit comments

Comments
 (0)