https://github.com/shivaramaarao updated https://github.com/llvm/llvm-project/pull/202858
>From 9b60bae4efbb1637f91b5b673e90180d002e24d1 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 | 5 ++++ .../include/flang/Frontend/CodeGenOptions.def | 1 + flang/lib/Frontend/CompilerInvocation.cpp | 4 +++ flang/lib/Frontend/FrontendActions.cpp | 15 ++++++++--- flang/test/Driver/split-lto-unit.f90 | 18 +++++++++++++ flang/test/Integration/split-lto-unit-2.f90 | 25 +++++++++++++++++++ 7 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 flang/test/Driver/split-lto-unit.f90 create mode 100644 flang/test/Integration/split-lto-unit-2.f90 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..d213137e348d0 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -282,6 +282,11 @@ 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 (Args.hasFlag(options::OPT_fsplit_lto_unit, + options::OPT_fno_split_lto_unit, /*Default=*/false)) + 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..426dea4869001 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -1059,10 +1059,17 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) { if (action == BackendActionTy::Backend_EmitBC || action == BackendActionTy::Backend_EmitLL || opts.PrepareForFatLTO) { - // If it is not ThinLTO, emits the module flag and sets it to be off. - if (!opts.PrepareForThinLTO && emitSummary && - !llvmModule->getModuleFlag("ThinLTO")) { - llvmModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0)); + if (opts.PrepareForThinLTO) { + if (!llvmModule->getModuleFlag("EnableSplitLTOUnit")) + llvmModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit", + opts.EnableSplitLTOUnit); + } else if (emitSummary && opts.PrepareForFullLTO) { + // If it is not ThinLTO, emits the module flag and sets it to be off. + if (!llvmModule->getModuleFlag("ThinLTO")) + llvmModule->addModuleFlag(llvm::Module::Error, "ThinLTO", uint32_t(0)); + if (!llvmModule->getModuleFlag("EnableSplitLTOUnit")) + llvmModule->addModuleFlag(llvm::Module::Error, "EnableSplitLTOUnit", + uint32_t(1)); } if (action == BackendActionTy::Backend_EmitBC) { diff --git a/flang/test/Driver/split-lto-unit.f90 b/flang/test/Driver/split-lto-unit.f90 new file mode 100644 index 0000000000000..60cc39d844b06 --- /dev/null +++ b/flang/test/Driver/split-lto-unit.f90 @@ -0,0 +1,18 @@ +! Check that -fsplit-lto-unit is passed to fc1 by the driver +! RUN: %flang -### -fsplit-lto-unit %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FC1,CHECK-OPTION + +! Check that -fsplit-lto-unit is passed to fc1 by the driver +! RUN: %flang -### -fno-split-lto-unit -fsplit-lto-unit %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FC1,CHECK-OPTION + +! Check that -fsplit-lto-unit is not passed to fc1 by the driver +! RUN: %flang -### -fno-split-lto-unit %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FC1,CHECK-NO-OPTION + +! Check that -fsplit-lto-unit is not passed to fc1 by the driver +! RUN: %flang -### -fsplit-lto-unit -fno-split-lto-unit %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FC1,CHECK-NO-OPTION + +! CHECK-FC1: "-fc1" +! CHECK-OPTION: "-fsplit-lto-unit" +! CHECK-NO-OPTION-NOT: "-fsplit-lto-unit" + +program main +end program main diff --git a/flang/test/Integration/split-lto-unit-2.f90 b/flang/test/Integration/split-lto-unit-2.f90 new file mode 100644 index 0000000000000..7096d7624725c --- /dev/null +++ b/flang/test/Integration/split-lto-unit-2.f90 @@ -0,0 +1,25 @@ +! Check that -flto=thin without -fsplit-lto-unit has EnableSplitLTOUnit = 0 +! RUN: %flang -flto=thin -S -emit-llvm -o - %s | FileCheck %s +! RUN: %flang -flto=thin -S -emit-llvm -o - %s | FileCheck %s +! RUN: %flang -flto=thin --target=x86_64-linux-gnu -emit-llvm -S -o - %s | FileCheck %s +! CHECK: !{i32 1, !"EnableSplitLTOUnit", i32 0} +! +! Check that -flto=thin with -fsplit-lto-unit has EnableSplitLTOUnit = 1 +! RUN: %flang -flto=thin -fsplit-lto-unit -emit-llvm -S -o - %s | FileCheck %s --check-prefix=SPLIT +! RUN: %flang -flto=thin --target=x86_64-linux-gnu -fsplit-lto-unit -emit-llvm -S -o - %s | FileCheck %s --check-prefix=SPLIT +! SPLIT: !{i32 1, !"EnableSplitLTOUnit", i32 1} +! +! Check that regular LTO has EnableSplitLTOUnit = 1 +! RUN: %flang -flto -emit-llvm -S -o - %s | FileCheck %s --implicit-check-not="EnableSplitLTOUnit" --check-prefix=SPLIT +! RUN: %flang -flto --target=x86_64-linux-gnu -emit-llvm -S -o - %s | FileCheck %s --implicit-check-not="EnableSplitLTOUnit" --check-prefix=SPLIT + +! Check that regular LTO has no EnableSplitLTOUnit = 1 for apple targets +! RUN: %flang -flto --target=x86_64-apple-macosx -emit-llvm -S -o - %s | not FileCheck %s --implicit-check-not="EnableSplitLTOUnit" --check-prefix=SPLIT + +! Check that -fsplit-lto-unit is passed to fc1 by the driver +! RUN: %flang -### -v -fsplit-lto-unit` %s | FileCheck %s +! CHECK: "-fc1" +! CHECK-SAME: "-fsplit-lto-unit" + +program main +end program main _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
