Skip to content

[flang] Support -f[no-]realloc-lhs. #120165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3463,6 +3463,9 @@ defm diagnostics_show_line_numbers : BoolFOption<"diagnostics-show-line-numbers"
NegFlag<SetFalse, [], [ClangOption, CC1Option],
"Show line numbers in diagnostic code snippets">,
PosFlag<SetTrue>>;
def fno_realloc_lhs : Flag<["-"], "fno-realloc-lhs">, Group<f_Group>,
HelpText<"An allocatable left-hand side of an intrinsic assignment is assumed to be allocated and match the shape/type of the right-hand side">,
Visibility<[FlangOption, FC1Option]>;
def fno_stack_protector : Flag<["-"], "fno-stack-protector">, Group<f_Group>,
HelpText<"Disable the use of stack protectors">;
def fno_strict_aliasing : Flag<["-"], "fno-strict-aliasing">, Group<f_Group>,
Expand Down Expand Up @@ -4296,6 +4299,9 @@ defm stack_size_section : BoolFOption<"stack-size-section",
PosFlag<SetTrue, [], [ClangOption, CC1Option],
"Emit section containing metadata on function stack sizes">,
NegFlag<SetFalse>>;
def frealloc_lhs : Flag<["-"], "frealloc-lhs">, Group<f_Group>,
Visibility<[FlangOption, FC1Option]>,
HelpText<"If an allocatable left-hand side of an intrinsic assignment is unallocated or its shape/type does not match the right-hand side, then it is automatically (re)allocated">;
def fstack_usage : Flag<["-"], "fstack-usage">, Group<f_Group>,
HelpText<"Emit .su file containing information on function stack sizes">;
def stack_usage_file : Separate<["-"], "stack-usage-file">,
Expand Down Expand Up @@ -6775,7 +6781,6 @@ defm real_4_real_8 : BooleanFFlag<"real-4-real-8">, Group<gfortran_Group>;
defm real_8_real_10 : BooleanFFlag<"real-8-real-10">, Group<gfortran_Group>;
defm real_8_real_16 : BooleanFFlag<"real-8-real-16">, Group<gfortran_Group>;
defm real_8_real_4 : BooleanFFlag<"real-8-real-4">, Group<gfortran_Group>;
defm realloc_lhs : BooleanFFlag<"realloc-lhs">, Group<gfortran_Group>;
defm recursive : BooleanFFlag<"recursive">, Group<gfortran_Group>;
defm repack_arrays : BooleanFFlag<"repack-arrays">, Group<gfortran_Group>;
defm second_underscore : BooleanFFlag<"second-underscore">, Group<gfortran_Group>;
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
options::OPT_fdefault_double_8,
options::OPT_flarge_sizes,
options::OPT_fno_automatic,
options::OPT_fhermetic_module_files});
options::OPT_fhermetic_module_files,
options::OPT_frealloc_lhs,
options::OPT_fno_realloc_lhs});
}

void Flang::addPreprocessingOptions(const ArgList &Args,
Expand Down
6 changes: 6 additions & 0 deletions flang/include/flang/Lower/LoweringOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ ENUM_LOWERINGOPT(Underscoring, unsigned, 1, 1)
/// (i.e. wraps around as two's complement). Off by default.
ENUM_LOWERINGOPT(IntegerWrapAround, unsigned, 1, 0)

/// If true (default), follow Fortran 2003 rules for (re)allocating
/// the allocatable on the left side of the intrinsic assignment,
/// if LHS and RHS have mismatching shapes/types.
/// If false, assume that the shapes/types/allocation-status match.
ENUM_LOWERINGOPT(ReallocateLHS, unsigned, 1, 1)

#undef LOWERINGOPT
#undef ENUM_LOWERINGOPT
5 changes: 5 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,11 @@ bool CompilerInvocation::createFromArgs(
invoc.getDiagnosticOpts().Remarks.push_back(a->getValue());
}

// -frealloc-lhs is the default.
if (!args.hasFlag(clang::driver::options::OPT_frealloc_lhs,
clang::driver::options::OPT_fno_realloc_lhs, true))
invoc.loweringOpts.setReallocateLHS(false);

success &= parseFrontendArgs(invoc.getFrontendOpts(), args, diags);
parseTargetArgs(invoc.getTargetOpts(), args);
parsePreprocessorArgs(invoc.getPreprocessorOpts(), args);
Expand Down
3 changes: 2 additions & 1 deletion flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4461,7 +4461,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
// lowered.
const bool isWholeAllocatableAssignment =
!userDefinedAssignment && !isInsideHlfirWhere() &&
Fortran::lower::isWholeAllocatable(assign.lhs);
Fortran::lower::isWholeAllocatable(assign.lhs) &&
bridge.getLoweringOptions().getReallocateLHS();
const bool isUserDefAssignToPointerOrAllocatable =
userDefinedAssignment &&
firstDummyIsPointerOrAllocatable(*userDefinedAssignment);
Expand Down
11 changes: 11 additions & 0 deletions flang/test/Driver/frealloc-lhs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
! Check that the driver passes through -f[no-]realloc-lhs:
! RUN: %flang -### -S -frealloc-lhs %s -o - 2>&1 | FileCheck %s --check-prefix=ON
! RUN: %flang -### -S -fno-realloc-lhs %s -o - 2>&1 | FileCheck %s --check-prefix=OFF

! Check that the compiler accepts -f[no-]realloc-lhs:
! RUN: %flang_fc1 -emit-hlfir -frealloc-lhs %s -o -
! RUN: %flang_fc1 -emit-hlfir -fno-realloc-lhs %s -o -

! ON: "-fc1"{{.*}}"-frealloc-lhs"

! OFF: "-fc1"{{.*}}"-fno-realloc-lhs"
32 changes: 32 additions & 0 deletions flang/test/Lower/reallocate-lhs.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
! RUN: bbc %s -o - -emit-hlfir | FileCheck %s --check-prefixes=ALL,REALLOCLHS
! RUN: bbc %s -o - -emit-hlfir -frealloc-lhs | FileCheck %s --check-prefixes=ALL,REALLOCLHS
! RUN: bbc %s -o - -emit-hlfir -frealloc-lhs=false | FileCheck %s --check-prefixes=ALL,NOREALLOCLHS
! RUN: %flang_fc1 %s -o - -emit-hlfir | FileCheck %s --check-prefixes=ALL,REALLOCLHS
! RUN: %flang_fc1 %s -o - -emit-hlfir -frealloc-lhs | FileCheck %s --check-prefixes=ALL,REALLOCLHS
! RUN: %flang_fc1 %s -o - -emit-hlfir -fno-realloc-lhs | FileCheck %s --check-prefixes=ALL,NOREALLOCLHS

subroutine test1(a, b)
integer, allocatable :: a(:), b(:)
a = b + 1
end

! ALL-LABEL: func.func @_QPtest1(
! ALL: %[[VAL_3:.*]]:2 = hlfir.declare{{.*}}{fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFtest1Ea"}
! REALLOCLHS: hlfir.assign %{{.*}} to %[[VAL_3]]#0 realloc : !hlfir.expr<?xi32>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>

! NOREALLOCLHS: %[[VAL_20:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
! NOREALLOCLHS: hlfir.assign %{{.*}} to %[[VAL_20]] : !hlfir.expr<?xi32>, !fir.box<!fir.heap<!fir.array<?xi32>>>

subroutine test2(a, b)
character(len=*), allocatable :: a(:)
character(len=*) :: b(:)
a = b
end subroutine test2

! ALL-LABEL: func.func @_QPtest2(
! ALL: %[[VAL_3:.*]]:2 = hlfir.declare{{.*}}{fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFtest2Ea"}
! REALLOCLHS: hlfir.assign %{{.*}} to %[[VAL_3]]#0 realloc keep_lhs_len : !fir.box<!fir.array<?x!fir.char<1,?>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>>

! NOREALLOCLHS: %[[VAL_7:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>>
! NOREALLOCLHS: hlfir.assign %{{.*}} to %[[VAL_7]] : !fir.box<!fir.array<?x!fir.char<1,?>>>, !fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>

7 changes: 7 additions & 0 deletions flang/tools/bbc/bbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ 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>
reallocateLHS("frealloc-lhs",
llvm::cl::desc("Follow Fortran 2003 rules for (re)allocating "
"the LHS of the intrinsic assignment"),
llvm::cl::init(true));

#define FLANG_EXCLUDE_CODEGEN
#include "flang/Optimizer/Passes/CommandLineOpts.h"
#include "flang/Optimizer/Passes/Pipelines.h"
Expand Down Expand Up @@ -375,6 +381,7 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
loweringOptions.setNoPPCNativeVecElemOrder(enableNoPPCNativeVecElemOrder);
loweringOptions.setLowerToHighLevelFIR(useHLFIR || emitHLFIR);
loweringOptions.setIntegerWrapAround(integerWrapAround);
loweringOptions.setReallocateLHS(reallocateLHS);
std::vector<Fortran::lower::EnvironmentDefault> envDefaults = {};
Fortran::frontend::TargetOptions targetOpts;
Fortran::frontend::CodeGenOptions cgOpts;
Expand Down
Loading