Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,11 @@ 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]>;
defm init_global_zero : BoolOptionWithoutMarshalling<"f", "init-global-zero",
PosFlag<SetTrue, [], [FlangOption, FC1Option],
"Zero initialize globals without default initialization (default)">,
NegFlag<SetFalse, [], [FlangOption, FC1Option],
"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
6 changes: 4 additions & 2 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ void Flang::addCodegenOptions(const ArgList &Args,
options::OPT_flang_deprecated_no_hlfir,
options::OPT_fno_ppc_native_vec_elem_order,
options::OPT_fppc_native_vec_elem_order,
options::OPT_ftime_report, options::OPT_ftime_report_EQ,
options::OPT_funroll_loops, options::OPT_fno_unroll_loops});
options::OPT_finit_global_zero,
options::OPT_fno_init_global_zero, options::OPT_ftime_report,
options::OPT_ftime_report_EQ, options::OPT_funroll_loops,
options::OPT_fno_unroll_loops});
}

void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
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(InitGlobalZero, unsigned, 1, 1)
#undef LOWERINGOPT
#undef ENUM_LOWERINGOPT
8 changes: 8 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,14 @@ bool CompilerInvocation::createFromArgs(
invoc.loweringOpts.setNoPPCNativeVecElemOrder(true);
}

// -f[no-]init-global-zero
if (args.hasFlag(clang::driver::options::OPT_finit_global_zero,
clang::driver::options::OPT_fno_init_global_zero,
/*default=*/true))
invoc.loweringOpts.setInitGlobalZero(true);
else
invoc.loweringOpts.setInitGlobalZero(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().getInitGlobalZero())
initValue = builder.create<fir::ZeroOp>(loc, symTy);
else
initValue = builder.create<fir::UndefOp>(loc, symTy);
builder.create<fir::HasValueOp>(loc, initValue);
});
}
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Driver/fno-zero-init.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
! Check that the driver passes through -f[no-]init-global-zero:
! RUN: %flang -### -S -finit-global-zero %s -o - 2>&1 | FileCheck --check-prefix=CHECK-POS %s
! RUN: %flang -### -S -fno-init-global-zero %s -o - 2>&1 | FileCheck --check-prefix=CHECK-NEG %s
! Check that the compiler accepts -f[no-]init-global-zero:
! RUN: %flang_fc1 -emit-hlfir -finit-global-zero %s -o -
! RUN: %flang_fc1 -emit-hlfir -fno-init-global-zero %s -o -

! CHECK-POS: "-fc1"{{.*}}"-finit-global-zero"
! CHECK-NEG: "-fc1"{{.*}}"-fno-init-global-zero"
20 changes: 20 additions & 0 deletions flang/test/Lower/zero_init.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
! RUN: %flang_fc1 -finit-global-zero -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
! RUN: %flang_fc1 -fno-init-global-zero -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-NO-ZERO-INIT %s
! RUN: bbc -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
! RUN: bbc -finit-global-zero -emit-hlfir -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
! RUN: bbc -finit-global-zero=false -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: }
22 changes: 22 additions & 0 deletions flang/test/Lower/zero_init_default_init.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s
! RUN: %flang_fc1 -finit-global-zero -emit-hlfir -o - %s | FileCheck %s
! RUN: %flang_fc1 -fno-init-global-zero -emit-hlfir -o - %s | FileCheck %s
! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
! RUN: bbc -finit-global-zero -emit-hlfir -o - %s | FileCheck %s
! RUN: bbc -finit-global-zero=false -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: }
6 changes: 6 additions & 0 deletions flang/tools/bbc/bbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ static llvm::cl::opt<bool> integerWrapAround(
llvm::cl::desc("Treat signed integer overflow as two's complement"),
llvm::cl::init(false));

static llvm::cl::opt<bool> initGlobalZero(
"finit-global-zero",
llvm::cl::desc("Zero initialize globals without default initialization"),
llvm::cl::init(true));

static llvm::cl::opt<bool>
reallocateLHS("frealloc-lhs",
llvm::cl::desc("Follow Fortran 2003 rules for (re)allocating "
Expand Down Expand Up @@ -381,6 +386,7 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
loweringOptions.setNoPPCNativeVecElemOrder(enableNoPPCNativeVecElemOrder);
loweringOptions.setLowerToHighLevelFIR(useHLFIR || emitHLFIR);
loweringOptions.setIntegerWrapAround(integerWrapAround);
loweringOptions.setInitGlobalZero(initGlobalZero);
loweringOptions.setReallocateLHS(reallocateLHS);
std::vector<Fortran::lower::EnvironmentDefault> envDefaults = {};
Fortran::frontend::TargetOptions targetOpts;
Expand Down
Loading