Skip to content

[flang] Support -ffunction-sections and -fdata-sections.#199731

Open
abidh wants to merge 1 commit into
llvm:mainfrom
abidh:163550
Open

[flang] Support -ffunction-sections and -fdata-sections.#199731
abidh wants to merge 1 commit into
llvm:mainfrom
abidh:163550

Conversation

@abidh
Copy link
Copy Markdown
Contributor

@abidh abidh commented May 26, 2026

Wire the flags through the driver, frontend, and TargetMachine, and add driver and codegen lit tests.

Fixes #163550

Wire the flags through the driver, frontend, and TargetMachine, and add driver and codegen lit tests.
@abidh abidh requested review from banach-space and tarunprabhu May 26, 2026 17:00
@llvmorg-github-actions llvmorg-github-actions Bot added clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels May 26, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

llvmorg-github-actions Bot commented May 26, 2026

@llvm/pr-subscribers-flang-driver

@llvm/pr-subscribers-clang-driver

Author: Abid Qadeer (abidh)

Changes

Wire the flags through the driver, frontend, and TargetMachine, and add driver and codegen lit tests.

Fixes #163550


Full diff: https://github.com/llvm/llvm-project/pull/199731.diff

7 Files Affected:

  • (modified) clang/include/clang/Options/Options.td (+4-4)
  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+12)
  • (modified) flang/include/flang/Frontend/CodeGenOptions.def (+3)
  • (modified) flang/lib/Frontend/CompilerInstance.cpp (+2)
  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+8)
  • (added) flang/test/Driver/code-gen-function-sections.f90 (+32)
  • (added) flang/test/Driver/function-sections.f90 (+39)
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 753e3ac1b74a5..358ece91e0e9c 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -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.">,
@@ -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,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 4c722a2e021eb..4c2282347c36b 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -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");
 }
diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def
index 17b8e61649da9..d8bbb94bd8cde 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -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.
diff --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp
index 3e820d386f8a9..7f562b1c39027 100644
--- a/flang/lib/Frontend/CompilerInstance.cpp
+++ b/flang/lib/Frontend/CompilerInstance.cpp
@@ -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,
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 9853fc600ff6a..cb6a7a85ad55f 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -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();
diff --git a/flang/test/Driver/code-gen-function-sections.f90 b/flang/test/Driver/code-gen-function-sections.f90
new file mode 100644
index 0000000000000..722d2537ee8d9
--- /dev/null
+++ b/flang/test/Driver/code-gen-function-sections.f90
@@ -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.
diff --git a/flang/test/Driver/function-sections.f90 b/flang/test/Driver/function-sections.f90
new file mode 100644
index 0000000000000..ff6a5fff1d13e
--- /dev/null
+++ b/flang/test/Driver/function-sections.f90
@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flang does not support -fdata-sections and -ffunction-sections flags

1 participant