Skip to content
Open
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
8 changes: 4 additions & 4 deletions clang/include/clang/Options/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -4825,9 +4825,9 @@ defm zero_initialized_in_bss : BoolFOption<"zero-initialized-in-bss",
PosFlag<SetFalse>>;
defm function_sections : BoolFOption<"function-sections",
CodeGenOpts<"FunctionSections">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption, CC1Option],
PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option],
"Place each function in its own section">,
NegFlag<SetFalse>>;
NegFlag<SetFalse, [], [FlangOption, FC1Option]>>;
defm basic_block_address_map : BoolFOption<"basic-block-address-map",
CodeGenOpts<"BBAddrMap">, DefaultFalse,
PosFlag<SetTrue, [], [CC1Option], "Emit the basic block address map section.">,
Expand All @@ -4840,9 +4840,9 @@ def fbasic_block_sections_EQ : Joined<["-"], "fbasic-block-sections=">, Group<f_
MarshallingInfoString<CodeGenOpts<"BBSections">, [{"none"}]>;
defm data_sections : BoolFOption<"data-sections",
CodeGenOpts<"DataSections">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption, CC1Option],
PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option],
"Place each data in its own section">,
NegFlag<SetFalse>>;
NegFlag<SetFalse, [], [FlangOption, FC1Option]>>;
defm experimental_call_graph_section
: BoolFOption<"experimental-call-graph-section",
CodeGenOpts<"CallGraphSection">, DefaultFalse,
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ void Flang::addCodegenOptions(const ArgList &Args,
options::OPT_fstack_repack_arrays, options::OPT_fno_stack_repack_arrays,
options::OPT_ftime_report, options::OPT_ftime_report_EQ,
options::OPT_funroll_loops, options::OPT_fno_unroll_loops});

const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
bool UseSeparateSections = isUseSeparateSections(Triple);
if (Args.hasFlag(options::OPT_ffunction_sections,
options::OPT_fno_function_sections, UseSeparateSections))
CmdArgs.push_back("-ffunction-sections");

bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF();
if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
UseSeparateSections || HasDefaultDataSections))
CmdArgs.push_back("-fdata-sections");

if (Args.hasArg(options::OPT_fcoarray))
CmdArgs.push_back("-fcoarray");
}
Expand Down
3 changes: 3 additions & 0 deletions flang/include/flang/Frontend/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ CODEGENOPT(InstrumentFunctions, 1, 0) ///< Set when -finstrument_functions is

CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as

CODEGENOPT(FunctionSections, 1, 0) ///< -ffunction-sections
CODEGENOPT(DataSections, 1, 0) ///< -fdata-sections

CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level.
CODEGENOPT(PICLevel, 2, 0) ///< PIC level of the LLVM module.
CODEGENOPT(PrepareForFatLTO , 1, 0) ///< Set when -ffat-lto-objects is enabled.
Expand Down
2 changes: 2 additions & 0 deletions flang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ bool CompilerInstance::setUpTargetMachine() {
tOpts.EnableAIXExtendedAltivecABI = targetOpts.EnableAIXExtendedAltivecABI;
tOpts.VecLib = convertDriverVectorLibraryToVectorLibrary(CGOpts.getVecLib());
tOpts.DisableIntegratedAS = CGOpts.DisableIntegratedAS;
tOpts.FunctionSections = CGOpts.FunctionSections;
tOpts.DataSections = CGOpts.DataSections;

targetMachine.reset(theTarget->createTargetMachine(
triple, /*CPU=*/targetOpts.cpu,
Expand Down
8 changes: 8 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
clang::options::OPT_fno_integrated_as, true))
opts.DisableIntegratedAS = 1;

opts.FunctionSections =
args.hasFlag(clang::options::OPT_ffunction_sections,
clang::options::OPT_fno_function_sections,
/*Default=*/false);
opts.DataSections = args.hasFlag(clang::options::OPT_fdata_sections,
clang::options::OPT_fno_data_sections,
/*Default=*/false);

if (const llvm::opt::Arg *a =
args.getLastArg(clang::options::OPT_mcode_object_version_EQ)) {
llvm::StringRef s = a->getValue();
Expand Down
32 changes: 32 additions & 0 deletions flang/test/Driver/code-gen-function-sections.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
! Test -ffunction-sections and -fdata-sections codegen (X86).

! REQUIRES: x86-registered-target

! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -S -ffunction-sections \
! RUN: -o - %s | FileCheck %s --check-prefix=FUNC-SECT
! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -S -o - %s \
! RUN: | FileCheck %s --check-prefix=FUNC-PLAIN

! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -S -fdata-sections \
! RUN: -o - %s | FileCheck %s --check-prefix=DATA-SECT
! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -S -o - %s \
! RUN: | FileCheck %s --check-prefix=DATA-PLAIN

module data_sect_mod
integer, save :: g = 1
end module

subroutine foo
end subroutine

program test
use data_sect_mod
call foo
end program

! FUNC-SECT: .section{{.*}}.text.
! FUNC-PLAIN-NOT: .section{{.*}}.text.

! DATA-SECT: .section{{.*}}.data.
! DATA-PLAIN: .data
! DATA-PLAIN-NOT: .section{{.*}}.data.
39 changes: 39 additions & 0 deletions flang/test/Driver/function-sections.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
! Test handling of -f(no-)function-sections and -f(no-)data-sections (driver).
!
! CHECK-FS: "-ffunction-sections"
! CHECK-NOFS-NOT: "-ffunction-sections"
! CHECK-DS: "-fdata-sections"
! CHECK-NODS-NOT: "-fdata-sections"

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s 2>&1 \
! RUN: | FileCheck %s --check-prefix=CHECK-NOFS --check-prefix=CHECK-NODS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -ffunction-sections 2>&1 | FileCheck %s --check-prefix=CHECK-FS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -fno-function-sections 2>&1 | FileCheck %s --check-prefix=CHECK-NOFS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -ffunction-sections -fno-function-sections 2>&1 \
! RUN: | FileCheck %s --check-prefix=CHECK-NOFS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -fno-function-sections -ffunction-sections 2>&1 \
! RUN: | FileCheck %s --check-prefix=CHECK-FS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -fdata-sections 2>&1 | FileCheck %s --check-prefix=CHECK-DS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -fno-data-sections 2>&1 | FileCheck %s --check-prefix=CHECK-NODS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -fdata-sections -fno-data-sections 2>&1 \
! RUN: | FileCheck %s --check-prefix=CHECK-NODS

! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu %s \
! RUN: -fno-data-sections -fdata-sections 2>&1 \
! RUN: | FileCheck %s --check-prefix=CHECK-DS

end program