[clang] [flang] [Flang][Driver] Add support for option -f[no-]split-machine-functions in flang (PR #216637)

Kaviya Rajendiran via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 18 23:51:52 PDT 2026


https://github.com/kaviya2510 updated https://github.com/llvm/llvm-project/pull/216637

>From 5441101cef918cdf08cfe2bd5638f564e1934967 Mon Sep 17 00:00:00 2001
From: Kaviya Rajendiran <kaviyara2000 at gmail.com>
Date: Mon, 17 Aug 2026 10:25:49 +0530
Subject: [PATCH 1/3] [Flang][Driver] Add support for option
 -f[no-]split-machine-functions in flang

---
 clang/include/clang/Driver/CommonArgs.h       |  5 +++++
 clang/include/clang/Options/Options.td        |  4 ++--
 clang/lib/Driver/ToolChains/Clang.cpp         | 12 +----------
 clang/lib/Driver/ToolChains/CommonArgs.cpp    | 17 ++++++++++++++++
 clang/lib/Driver/ToolChains/Flang.cpp         |  2 ++
 flang/include/flang/Frontend/TargetOptions.h  |  3 +++
 flang/lib/Frontend/CompilerInstance.cpp       |  1 +
 flang/lib/Frontend/CompilerInvocation.cpp     |  3 +++
 .../test/Driver/fsplit-machine-functions.f90  | 16 +++++++++++++++
 .../Driver/split-machine-function-pass.f90    | 20 +++++++++++++++++++
 10 files changed, 70 insertions(+), 13 deletions(-)
 create mode 100644 flang/test/Driver/fsplit-machine-functions.f90
 create mode 100644 flang/test/Driver/split-machine-function-pass.f90

diff --git a/clang/include/clang/Driver/CommonArgs.h b/clang/include/clang/Driver/CommonArgs.h
index 3b53df94fc79f..be15d15a1661e 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -257,6 +257,11 @@ void addMachineOutlinerArgs(const Driver &D, const llvm::opt::ArgList &Args,
                             const llvm::Triple &Triple, bool IsLTO,
                             const StringRef PluginOptPrefix = "");
 
+void addSplitMachineFunctionsArgs(const Driver &D,
+                                  const llvm::opt::ArgList &Args,
+                                  llvm::opt::ArgStringList &CmdArgs,
+                                  const llvm::Triple &Triple);
+
 void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs,
                         llvm::opt::ArgStringList &CC1Args,
                         StringRef BitcodeSuffix, const llvm::Triple &Triple,
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index adc4224dd561c..7eacbc7a44827 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -5068,8 +5068,8 @@ defm separate_named_sections : BoolFOption<"separate-named-sections",
 
 defm split_machine_functions: BoolFOption<"split-machine-functions",
   CodeGenOpts<"SplitMachineFunctions">, DefaultFalse,
-  PosFlag<SetTrue, [], [ClangOption, CC1Option], "Enable">,
-  NegFlag<SetFalse, [], [ClangOption], "Disable">,
+  PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Enable">,
+  NegFlag<SetFalse, [], [ClangOption, FlangOption], "Disable">,
   BothFlags<[], [ClangOption], " late function splitting using profile information (x86 and aarch64 ELF)">>;
 
 defm partition_static_data_sections: BoolFOption<"partition-static-data-sections",
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 54583fe3abbd8..ccc226eff0782 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6618,17 +6618,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   Args.addOptInFlag(CmdArgs, options::OPT_funique_basic_block_section_names,
                     options::OPT_fno_unique_basic_block_section_names);
 
-  if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions,
-                               options::OPT_fno_split_machine_functions)) {
-    if (!A->getOption().matches(options::OPT_fno_split_machine_functions)) {
-      // This codegen pass is only available on x86 and AArch64 ELF targets.
-      if ((Triple.isX86() || Triple.isAArch64()) && Triple.isOSBinFormatELF())
-        A->render(Args, CmdArgs);
-      else
-        D.Diag(diag::err_drv_unsupported_opt_for_target)
-            << A->getAsString(Args) << TripleStr;
-    }
-  }
+  addSplitMachineFunctionsArgs(D, Args, CmdArgs, Triple);
 
   if (Arg *A =
           Args.getLastArg(options::OPT_fpartition_static_data_sections,
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index e1c07fb94ea76..7db070699b23f 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -3086,6 +3086,23 @@ void tools::addMachineOutlinerArgs(const Driver &D,
     addArg(Twine("-codegen-data-use-path=") + CodeGenDataUseArg->getValue());
 }
 
+void tools::addSplitMachineFunctionsArgs(const Driver &D,
+                                         const llvm::opt::ArgList &Args,
+                                         llvm::opt::ArgStringList &CmdArgs,
+                                         const llvm::Triple &Triple) {
+  if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions,
+                               options::OPT_fno_split_machine_functions)) {
+    if (!A->getOption().matches(options::OPT_fno_split_machine_functions)) {
+      // This codegen pass is only available on x86 and AArch64 ELF targets.
+      if ((Triple.isX86() || Triple.isAArch64()) && Triple.isOSBinFormatELF())
+        A->render(Args, CmdArgs);
+      else
+        D.Diag(diag::err_drv_unsupported_opt_for_target)
+            << A->getAsString(Args) << Triple.getTriple();
+    }
+  }
+}
+
 void tools::addOpenMPDeviceRTL(const Driver &D,
                                const llvm::opt::ArgList &DriverArgs,
                                llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index a48e41159f367..8eac9992f9f10 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -1233,6 +1233,8 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, const JobAction &JA,
   if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling,
                    options::OPT_fno_pseudo_probe_for_profiling, false))
     CmdArgs.push_back("-fpseudo-probe-for-profiling");
+
+  addSplitMachineFunctionsArgs(TC.getDriver(), Args, CmdArgs, TC.getTriple());
 }
 
 void Flang::ConstructJob(Compilation &C, const JobAction &JA,
diff --git a/flang/include/flang/Frontend/TargetOptions.h b/flang/include/flang/Frontend/TargetOptions.h
index f6e5634d5a995..fc7a2a3089f15 100644
--- a/flang/include/flang/Frontend/TargetOptions.h
+++ b/flang/include/flang/Frontend/TargetOptions.h
@@ -54,6 +54,9 @@ class TargetOptions {
   /// Print verbose assembly
   bool asmVerbose = false;
 
+  /// Enable splitting of machine functions using profile information.
+  bool SplitMachineFunctions = false;
+
   /// Atomic control options
   bool atomicIgnoreDenormalMode = false;
   bool atomicRemoteMemory = false;
diff --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp
index 02f39376f1d78..2416d3b593481 100644
--- a/flang/lib/Frontend/CompilerInstance.cpp
+++ b/flang/lib/Frontend/CompilerInstance.cpp
@@ -381,6 +381,7 @@ bool CompilerInstance::setUpTargetMachine() {
 
   llvm::TargetOptions tOpts = llvm::TargetOptions();
   tOpts.EnableAIXExtendedAltivecABI = targetOpts.EnableAIXExtendedAltivecABI;
+  tOpts.EnableMachineFunctionSplitter = targetOpts.SplitMachineFunctions;
   tOpts.VecLib = convertDriverVectorLibraryToVectorLibrary(CGOpts.getVecLib());
   tOpts.DisableIntegratedAS = CGOpts.DisableIntegratedAS;
   tOpts.FunctionSections = CGOpts.FunctionSections;
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index b57bc4583be38..fef48a279ad72 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -610,6 +610,9 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
     }
   }
 
+  opts.SplitMachineFunctions =
+      args.hasArg(clang::options::OPT_fsplit_machine_functions);
+
   opts.asmVerbose = args.hasFlag(clang::options::OPT_fverbose_asm,
                                  clang::options::OPT_fno_verbose_asm, false);
 }
diff --git a/flang/test/Driver/fsplit-machine-functions.f90 b/flang/test/Driver/fsplit-machine-functions.f90
new file mode 100644
index 0000000000000..5b5b267e8e540
--- /dev/null
+++ b/flang/test/Driver/fsplit-machine-functions.f90
@@ -0,0 +1,16 @@
+! Test handling of -fsplit-machine-functions and -fno-split-machine-functions.
+
+! RUN: %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=NEG_FLAG
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG
+! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG
+! RUN: not %flang -### --target=arm-unknown-linux-gnueabi -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=CHECK_ERROR
+
+! POS_FLAG: "-fsplit-machine-functions"
+! NEG_FLAG-NOT: "-fsplit-machine-functions"
+! CHECK_ERROR: error: unsupported option '-fsplit-machine-functions' for target
+
+subroutine test(x)
+    integer, intent(in) :: x
+end subroutine test
diff --git a/flang/test/Driver/split-machine-function-pass.f90 b/flang/test/Driver/split-machine-function-pass.f90
new file mode 100644
index 0000000000000..1f62288a65fbd
--- /dev/null
+++ b/flang/test/Driver/split-machine-function-pass.f90
@@ -0,0 +1,20 @@
+! Verify that the MachineFunctionSplitter pass is enabled while passing -fsplit-machine-functions.
+
+! REQUIRES: x86-registered-target
+
+! RUN: %flang_fc1 -S -fsplit-machine-functions %s \
+! RUN:   -triple x86_64-unknown-linux-gnu \
+! RUN:   -mllvm -debug-pass=Structure -o %t 2>&1 \
+! RUN:   | FileCheck %s --check-prefix=ENABLED
+
+! RUN: %flang_fc1 -S %s \
+! RUN:   -triple x86_64-unknown-linux-gnu \
+! RUN:   -mllvm -debug-pass=Structure -o %t 2>&1 \
+! RUN:   | FileCheck %s --check-prefix=DISABLED
+
+! ENABLED: Machine Function Splitter Transformation
+! DISABLED-NOT: Machine Function Splitter Transformation
+
+subroutine test(x)
+    integer, intent(in) :: x
+  end subroutine test

>From b435cf4a1c60857e62bde20b52895303d971c73a Mon Sep 17 00:00:00 2001
From: Kaviya Rajendiran <kaviyara2000 at gmail.com>
Date: Tue, 18 Aug 2026 12:38:36 +0530
Subject: [PATCH 2/3] [Flang][Driver] Guard the testcase with
 'x86-registered-target' and 'arm-registered-target'

---
 flang/test/Driver/fsplit-machine-functions.f90 | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/flang/test/Driver/fsplit-machine-functions.f90 b/flang/test/Driver/fsplit-machine-functions.f90
index 5b5b267e8e540..f12c73a23c2a7 100644
--- a/flang/test/Driver/fsplit-machine-functions.f90
+++ b/flang/test/Driver/fsplit-machine-functions.f90
@@ -1,11 +1,11 @@
 ! Test handling of -fsplit-machine-functions and -fno-split-machine-functions.
 
-! RUN: %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=NEG_FLAG
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG
-! RUN: %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG
-! RUN: not %flang -### --target=arm-unknown-linux-gnueabi -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=CHECK_ERROR
+! RUN: %if x86-registered-target %{ %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=NEG_FLAG %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG %}
+! RUN: %if arm-registered-target %{ not %flang -### --target=arm-unknown-linux-gnueabi -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=CHECK_ERROR %}
 
 ! POS_FLAG: "-fsplit-machine-functions"
 ! NEG_FLAG-NOT: "-fsplit-machine-functions"

>From 0f42445f901d3f99abbf0174a19a8899704f43cb Mon Sep 17 00:00:00 2001
From: Kaviya Rajendiran <kaviyara2000 at gmail.com>
Date: Wed, 19 Aug 2026 12:20:14 +0530
Subject: [PATCH 3/3] [Flang][Driver] Addressed NIT comments in testcase

---
 .../test/Driver/fsplit-machine-functions.f90  | 22 +++++++++----------
 .../Driver/split-machine-function-pass.f90    | 20 +++++------------
 2 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/flang/test/Driver/fsplit-machine-functions.f90 b/flang/test/Driver/fsplit-machine-functions.f90
index f12c73a23c2a7..2a0ffe61811fc 100644
--- a/flang/test/Driver/fsplit-machine-functions.f90
+++ b/flang/test/Driver/fsplit-machine-functions.f90
@@ -1,16 +1,14 @@
 ! Test handling of -fsplit-machine-functions and -fno-split-machine-functions.
 
-! RUN: %if x86-registered-target %{ %flang_fc1 -emit-llvm -triple x86_64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=NEG_FLAG %}
-! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG %}
-! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG %}
-! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NEG_FLAG %}
-! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=POS_FLAG %}
-! RUN: %if arm-registered-target %{ not %flang -### --target=arm-unknown-linux-gnueabi -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=CHECK_ERROR %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu %s 2>&1 | FileCheck %s --check-prefix=NO-SPLIT-MACHINE-FUNCTIONS  %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=SPLIT-MACHINE-FUNCTIONS  %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NO-SPLIT-MACHINE-FUNCTIONS  %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fsplit-machine-functions -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NO-SPLIT-MACHINE-FUNCTIONS  %}
+! RUN: %if x86-registered-target %{ %flang -### --target=x86_64-unknown-linux-gnu -fno-split-machine-functions -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=SPLIT-MACHINE-FUNCTIONS  %}
+! RUN: %if arm-registered-target %{ not %flang -### --target=arm-unknown-linux -fsplit-machine-functions %s 2>&1 | FileCheck %s --check-prefix=UNSUPPORTED-OPT %}
+! RUN: %if arm-registered-target %{ %flang -### --target=arm-unknown-linux -fno-split-machine-functions %s 2>&1 | FileCheck %s --check-prefix=NO-SPLIT-MACHINE-FUNCTIONS %}
 
-! POS_FLAG: "-fsplit-machine-functions"
-! NEG_FLAG-NOT: "-fsplit-machine-functions"
-! CHECK_ERROR: error: unsupported option '-fsplit-machine-functions' for target
+! SPLIT-MACHINE-FUNCTIONS: "-fsplit-machine-functions"
+! NO-SPLIT-MACHINE-FUNCTIONS-NOT: "-fsplit-machine-functions"
+! UNSUPPORTED-OPT: error: unsupported option '-fsplit-machine-functions' for target
 
-subroutine test(x)
-    integer, intent(in) :: x
-end subroutine test
diff --git a/flang/test/Driver/split-machine-function-pass.f90 b/flang/test/Driver/split-machine-function-pass.f90
index 1f62288a65fbd..cf8bd0eafb624 100644
--- a/flang/test/Driver/split-machine-function-pass.f90
+++ b/flang/test/Driver/split-machine-function-pass.f90
@@ -2,19 +2,11 @@
 
 ! REQUIRES: x86-registered-target
 
-! RUN: %flang_fc1 -S -fsplit-machine-functions %s \
-! RUN:   -triple x86_64-unknown-linux-gnu \
-! RUN:   -mllvm -debug-pass=Structure -o %t 2>&1 \
-! RUN:   | FileCheck %s --check-prefix=ENABLED
+! RUN: %flang_fc1 -S -fsplit-machine-functions %s -triple x86_64-unknown-linux-gnu -mllvm -debug-pass=Structure -o /dev/null 2>&1 | FileCheck %s --check-prefix=SPLIT
+! RUN: %flang_fc1 -S %s -triple x86_64-unknown-linux-gnu -mllvm -debug-pass=Structure -o /dev/null 2>&1 | FileCheck %s --check-prefix=NO-SPLIT
 
-! RUN: %flang_fc1 -S %s \
-! RUN:   -triple x86_64-unknown-linux-gnu \
-! RUN:   -mllvm -debug-pass=Structure -o %t 2>&1 \
-! RUN:   | FileCheck %s --check-prefix=DISABLED
+! SPLIT: Machine Function Splitter Transformation
+! NO-SPLIT-NOT: Machine Function Splitter Transformation
 
-! ENABLED: Machine Function Splitter Transformation
-! DISABLED-NOT: Machine Function Splitter Transformation
-
-subroutine test(x)
-    integer, intent(in) :: x
-  end subroutine test
+subroutine test
+end subroutine test



More information about the cfe-commits mailing list