https://github.com/shivaramaarao updated https://github.com/llvm/llvm-project/pull/202858
>From b3cb46767631c13a87c393a000627bd4b0e75017 Mon Sep 17 00:00:00 2001 From: Rahul Kumar <[email protected]> Date: Wed, 10 Jun 2026 11:09:03 +0530 Subject: [PATCH] [Flang][Driver] Adding -fsplit-lto-unit option to enable combined LTO build When mixing Fortran objects from Flang with C/C++ objects compiled by Clang during a combined LTO build, it is necessary to ensure that all files use the same setting for split-lto-unit. This requires the support for -fsplit-lto-unit option in the flang driver. This support is added as part of this commit --- clang/include/clang/Options/Options.td | 4 ++-- clang/lib/Driver/ToolChains/Flang.cpp | 8 ++++++++ flang/include/flang/Frontend/CodeGenOptions.def | 1 + flang/lib/Frontend/CompilerInvocation.cpp | 4 ++++ flang/lib/Frontend/FrontendActions.cpp | 6 ++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 8f5ed945a40fe..ed0354786c43e 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -4804,9 +4804,9 @@ defm whole_program_vtables : BoolFOption<"whole-program-vtables", NegFlag<SetFalse>, BothFlags<[], [ClangOption, CLOption]>>; defm split_lto_unit : BoolFOption<"split-lto-unit", CodeGenOpts<"EnableSplitLTOUnit">, DefaultFalse, - PosFlag<SetTrue, [], [ClangOption, CC1Option], + PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Enables splitting of the LTO unit">, - NegFlag<SetFalse>, BothFlags<[], [ClangOption, CLOption]>>; + NegFlag<SetFalse>, BothFlags<[], [ClangOption, CLOption, FlangOption, FC1Option]>>; defm force_emit_vtables : BoolFOption<"force-emit-vtables", CodeGenOpts<"ForceEmitVTables">, DefaultFalse, PosFlag<SetTrue, [], [ClangOption, CC1Option], diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index 224ece3239efd..d98ab694f401a 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -282,6 +282,14 @@ void Flang::addLTOOptions(const ArgList &Args, ArgStringList &CmdArgs) const { CmdArgs.push_back("-flto=full"); else if (LTOMode == LTOK_Thin) CmdArgs.push_back("-flto=thin"); + + if (Arg *splitLTOArg = Args.getLastArg(options::OPT_fsplit_lto_unit, + options::OPT_fno_split_lto_unit)) { + if (splitLTOArg->getOption().matches(options::OPT_fsplit_lto_unit)) { + CmdArgs.push_back("-fsplit-lto-unit"); + } + } + Args.addAllArgs(CmdArgs, {options::OPT_ffat_lto_objects, options::OPT_fno_fat_lto_objects}); } diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def index d8bbb94bd8cde..37931c0ffecc1 100644 --- a/flang/include/flang/Frontend/CodeGenOptions.def +++ b/flang/include/flang/Frontend/CodeGenOptions.def @@ -45,6 +45,7 @@ CODEGENOPT(PrepareForFullLTO , 1, 0) ///< Set when -flto is enabled on the ///< compile step. CODEGENOPT(PrepareForThinLTO , 1, 0) ///< Set when -flto=thin is enabled on the ///< compile step. +CODEGENOPT(EnableSplitLTOUnit, 1, 0) ///< Set when -fsplit-lto-unit is enabled. CODEGENOPT(ProtectParens, 1, 1) ///< -fprotect-parens (enable parenthesis protection) CODEGENOPT(StackArrays, 1, 0) ///< -fstack-arrays (enable the stack-arrays pass) CODEGENOPT(EnableSafeTrampoline, 1, 0) ///< -fsafe-trampoline (W^X compliant trampoline pool) diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index a3335fc9a250f..bf10ef346b1fc 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -1551,6 +1551,10 @@ static bool parseLinkerOptionsArgs(CompilerInvocation &invoc, opts.PrepareForThinLTO = true; } + // -fsplit-lto-unit option + if (args.hasArg(clang::options::OPT_fsplit_lto_unit)) + opts.EnableSplitLTOUnit = true; + // -ffat-lto-objects if (const llvm::opt::Arg *arg = args.getLastArg(clang::options::OPT_ffat_lto_objects, diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index 66602ed52f6cd..e484e857dbe43 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -1065,6 +1065,12 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) { llvmModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0)); } + if (opts.EnableSplitLTOUnit && emitSummary && + !llvmModule->getModuleFlag("EnableSplitLTOUnit")) { + llvmModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit", + uint32_t(1)); + } + if (action == BackendActionTy::Backend_EmitBC) { if (opts.PrepareForThinLTO) { mpm.addPass(llvm::ThinLTOBitcodeWriterPass(os, nullptr)); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
