Skip to content

Commit 78068dc

Browse files
committed
[flang][Driver] Enables lto-partitions and fat-lto-object.
lto-partition helps in performing parallel lto and fat-lto-objects allows bit code to be embedded in object files generated.
1 parent 9b2c605 commit 78068dc

File tree

8 files changed

+82
-15
lines changed

8 files changed

+82
-15
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,10 +3171,11 @@ def fthin_link_bitcode_EQ : Joined<["-"], "fthin-link-bitcode=">,
31713171
MarshallingInfoString<CodeGenOpts<"ThinLinkBitcodeFile">>;
31723172
defm fat_lto_objects : BoolFOption<"fat-lto-objects",
31733173
CodeGenOpts<"FatLTO">, DefaultFalse,
3174-
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Enable">,
3175-
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Disable">,
3174+
PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Enable">,
3175+
NegFlag<SetFalse, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Disable">,
31763176
BothFlags<[], [ClangOption, CC1Option], " fat LTO object support">>;
31773177
def flto_partitions_EQ : Joined<["-"], "flto-partitions=">, Group<f_Group>,
3178+
Visibility<[ClangOption, FlangOption]>,
31783179
HelpText<"Number of partitions to use for parallel full LTO codegen, ld.lld only.">;
31793180
def fmacro_backtrace_limit_EQ : Joined<["-"], "fmacro-backtrace-limit=">,
31803181
Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ void Flang::addCodegenOptions(const ArgList &Args,
182182
CmdArgs.push_back("-fcoarray");
183183
}
184184

185+
void Flang::addLTOOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
186+
const auto &TC = getToolChain();
187+
const Driver &D = TC.getDriver();
188+
DiagnosticsEngine &Diags = D.getDiags();
189+
LTOKind LTOMode = D.getLTOMode();
190+
// LTO mode is parsed by the Clang driver library.
191+
assert(LTOMode != LTOK_Unknown && "Unknown LTO mode.");
192+
if (LTOMode == LTOK_Full)
193+
CmdArgs.push_back("-flto=full");
194+
else if (LTOMode == LTOK_Thin) {
195+
Diags.Report(
196+
Diags.getCustomDiagID(DiagnosticsEngine::Warning,
197+
"the option '-flto=thin' is a work in progress"));
198+
CmdArgs.push_back("-flto=thin");
199+
}
200+
if (Args.hasArg(options::OPT_flto_partitions_EQ)) {
201+
StringRef A = Args.getLastArgValue(options::OPT_flto_partitions_EQ, "8");
202+
CmdArgs.push_back(Args.MakeArgString("-flto-partitions=" + A));
203+
}
204+
Args.addAllArgs(CmdArgs, {options::OPT_ffat_lto_objects,
205+
options::OPT_fno_fat_lto_objects});
206+
}
207+
185208
void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
186209
// ParsePICArgs parses -fPIC/-fPIE and their variants and returns a tuple of
187210
// (RelocationModel, PICLevel, IsPIE).
@@ -821,7 +844,6 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
821844

822845
const Driver &D = TC.getDriver();
823846
ArgStringList CmdArgs;
824-
DiagnosticsEngine &Diags = D.getDiags();
825847

826848
// Invoke ourselves in -fc1 mode.
827849
CmdArgs.push_back("-fc1");
@@ -884,17 +906,7 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
884906

885907
handleColorDiagnosticsArgs(D, Args, CmdArgs);
886908

887-
// LTO mode is parsed by the Clang driver library.
888-
LTOKind LTOMode = D.getLTOMode();
889-
assert(LTOMode != LTOK_Unknown && "Unknown LTO mode.");
890-
if (LTOMode == LTOK_Full)
891-
CmdArgs.push_back("-flto=full");
892-
else if (LTOMode == LTOK_Thin) {
893-
Diags.Report(
894-
Diags.getCustomDiagID(DiagnosticsEngine::Warning,
895-
"the option '-flto=thin' is a work in progress"));
896-
CmdArgs.push_back("-flto=thin");
897-
}
909+
addLTOOptions(Args, CmdArgs);
898910

899911
// -fPIC and related options.
900912
addPicOptions(Args, CmdArgs);

clang/lib/Driver/ToolChains/Flang.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class LLVM_LIBRARY_VISIBILITY Flang : public Tool {
4040
void addPreprocessingOptions(const llvm::opt::ArgList &Args,
4141
llvm::opt::ArgStringList &CmdArgs) const;
4242

43+
/// Extract LTO options from the driver arguments and add them to
44+
/// the command arguments.
45+
///
46+
/// \param [in] Args The list of input driver arguments
47+
/// \param [out] CmdArgs The list of output command arguments
48+
void addLTOOptions(const llvm::opt::ArgList &Args,
49+
llvm::opt::ArgStringList &CmdArgs) const;
50+
4351
/// Extract PIC options from the driver arguments and add them to
4452
/// the command arguments.
4553
///

flang/include/flang/Frontend/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CODEGENOPT(InstrumentFunctions, 1, 0) ///< Set when -finstrument_functions is
3535

3636
CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level.
3737
CODEGENOPT(PICLevel, 2, 0) ///< PIC level of the LLVM module.
38+
CODEGENOPT(PrepareForFatLTO , 1, 0) ///< Set when -ffat-lto-objects is enabled.
3839
CODEGENOPT(PrepareForFullLTO , 1, 0) ///< Set when -flto is enabled on the
3940
///< compile step.
4041
CODEGENOPT(PrepareForThinLTO , 1, 0) ///< Set when -flto=thin is enabled on the

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
325325
if (args.hasArg(clang::driver::options::OPT_finstrument_functions))
326326
opts.InstrumentFunctions = 1;
327327

328+
opts.PrepareForFatLTO =
329+
args.hasFlag(clang::driver::options::OPT_ffat_lto_objects,
330+
clang::driver::options::OPT_fno_fat_lto_objects, false);
331+
328332
// -flto=full/thin option.
329333
if (const llvm::opt::Arg *a =
330334
args.getLastArg(clang::driver::options::OPT_flto_EQ)) {

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,9 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
995995

996996
// Create the pass manager.
997997
llvm::ModulePassManager mpm;
998-
if (opts.PrepareForFullLTO)
998+
if (opts.PrepareForFatLTO)
999+
mpm = pb.buildFatLTODefaultPipeline(level, opts.PrepareForThinLTO, true);
1000+
else if (opts.PrepareForFullLTO)
9991001
mpm = pb.buildLTOPreLinkDefaultPipeline(level);
10001002
else if (opts.PrepareForThinLTO)
10011003
mpm = pb.buildThinLTOPreLinkDefaultPipeline(level);

flang/test/Driver/lto-fatlto.f90

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! REQUIRES: x86-registered-target
2+
! checks fatlto objects: that valid bitcode is included in the object file generated.
3+
4+
! RUN: %flang -fc1 -triple x86_64-unknown-linux-gnu -flto -ffat-lto-objects -emit-obj %s -o %t.o
5+
! RUN: llvm-readelf -S %t.o | FileCheck %s --check-prefixes=ELF
6+
! RUN: llvm-objcopy --dump-section=.llvm.lto=%t.bc %t.o
7+
! RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefixes=DIS
8+
9+
! ELF: .llvm.lto
10+
! DIS: define void @_QQmain()
11+
! DIS-NEXT: ret void
12+
! DIS-NEXT: }
13+
14+
! RUN: %flang -fc1 -triple x86_64-unknown-linux-gnu -flto -ffat-lto-objects -S %s -o - | FileCheck %s --check-prefixes=ASM
15+
16+
! ASM: .section .llvm.lto,"e",@llvm_lto
17+
! ASM-NEXT: .Lllvm.embedded.object:
18+
! ASM-NEXT: .asciz "BC
19+
! ASM-NEXT: .size .Lllvm.embedded.object
20+
end program
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! UNSUPPORTED: system-windows
2+
! check flto-partitions is passed to lld, fc1
3+
! RUN: %flang -### -fuse-ld=lld -flto=full -flto-partitions=16 %s 2>&1 | FileCheck %s --check-prefixes=LLD-PART,FC1-PART
4+
5+
! FC1-PART: "-fc1"
6+
! FC1-PART-SAME: "-flto=full"
7+
! FC1-PART-SAME: "-flto-partitions=16"
8+
! LLD-PART: ld.lld
9+
! LLD-PART-SAME: "--lto-partitions=16"
10+
11+
! check fat-lto-objects is passed to lld, fc1
12+
! RUN: %flang -### -fuse-ld=lld -flto -ffat-lto-objects %s 2>&1 | FileCheck %s --check-prefixes=LLD-FAT,FC1-FAT
13+
14+
! FC1-FAT: "-fc1"
15+
! FC1-FAT-SAME: "-flto=full"
16+
! FC1-FAT-SAME: "-ffat-lto-objects"
17+
! LLD-FAT: ld.lld
18+
! LLD-FAT-SAME: "--fat-lto-objects"
19+
end program

0 commit comments

Comments
 (0)