Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3171,10 +3171,11 @@ def fthin_link_bitcode_EQ : Joined<["-"], "fthin-link-bitcode=">,
MarshallingInfoString<CodeGenOpts<"ThinLinkBitcodeFile">>;
defm fat_lto_objects : BoolFOption<"fat-lto-objects",
CodeGenOpts<"FatLTO">, DefaultFalse,
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Enable">,
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Disable">,
PosFlag<SetTrue, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Enable">,
NegFlag<SetFalse, [], [ClangOption, CC1Option, FlangOption, FC1Option], "Disable">,
BothFlags<[], [ClangOption, CC1Option], " fat LTO object support">>;
def flto_partitions_EQ : Joined<["-"], "flto-partitions=">, Group<f_Group>,
Visibility<[ClangOption, FlangOption]>,
HelpText<"Number of partitions to use for parallel full LTO codegen, ld.lld only.">;
def fmacro_backtrace_limit_EQ : Joined<["-"], "fmacro-backtrace-limit=">,
Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>,
Expand Down
32 changes: 20 additions & 12 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ void Flang::addCodegenOptions(const ArgList &Args,
CmdArgs.push_back("-fcoarray");
}

void Flang::addLTOOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
const ToolChain &TC = getToolChain();
const Driver &D = TC.getDriver();
DiagnosticsEngine &Diags = D.getDiags();
LTOKind LTOMode = D.getLTOMode();
// LTO mode is parsed by the Clang driver library.
assert(LTOMode != LTOK_Unknown && "Unknown LTO mode.");
if (LTOMode == LTOK_Full)
CmdArgs.push_back("-flto=full");
else if (LTOMode == LTOK_Thin) {
Diags.Report(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this isn't part of this PR, but do you know what flto=thin does in flang? Does it do anything at all or do we just accept this option, then ignore it? This warning is not very informative, and I wonder if it is worth providing something different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://reviews.llvm.org/D142420 is the changes I can see. It adds the pipeline buildThinLTOPreLinkDefaultPipeline(level) in FrontendAction.cpp. It does not ignore the option but says further development is required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for bringing this up. There are TODOs mentioned in the above PR:

The TODOs would be:

Add support for -f[no]split-lto-unit
Emit LTO summary
Use ThinLTOBitcodeWriterPass for thin lto

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this. It looks like there were some concerns about that commit message even back then. Did you have plans to work on any of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to work on enabling module summary.

Diags.getCustomDiagID(DiagnosticsEngine::Warning,
"the option '-flto=thin' is a work in progress"));
CmdArgs.push_back("-flto=thin");
}
Args.addAllArgs(CmdArgs, {options::OPT_ffat_lto_objects,
options::OPT_fno_fat_lto_objects});
}

void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
// ParsePICArgs parses -fPIC/-fPIE and their variants and returns a tuple of
// (RelocationModel, PICLevel, IsPIE).
Expand Down Expand Up @@ -821,7 +840,6 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,

const Driver &D = TC.getDriver();
ArgStringList CmdArgs;
DiagnosticsEngine &Diags = D.getDiags();

// Invoke ourselves in -fc1 mode.
CmdArgs.push_back("-fc1");
Expand Down Expand Up @@ -884,17 +902,7 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,

handleColorDiagnosticsArgs(D, Args, CmdArgs);

// LTO mode is parsed by the Clang driver library.
LTOKind LTOMode = D.getLTOMode();
assert(LTOMode != LTOK_Unknown && "Unknown LTO mode.");
if (LTOMode == LTOK_Full)
CmdArgs.push_back("-flto=full");
else if (LTOMode == LTOK_Thin) {
Diags.Report(
Diags.getCustomDiagID(DiagnosticsEngine::Warning,
"the option '-flto=thin' is a work in progress"));
CmdArgs.push_back("-flto=thin");
}
addLTOOptions(Args, CmdArgs);

// -fPIC and related options.
addPicOptions(Args, CmdArgs);
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Driver/ToolChains/Flang.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class LLVM_LIBRARY_VISIBILITY Flang : public Tool {
void addPreprocessingOptions(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const;

/// Extract LTO options from the driver arguments and add them to
/// the command arguments.
///
/// \param [in] Args The list of input driver arguments
/// \param [out] CmdArgs The list of output command arguments
void addLTOOptions(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const;

/// Extract PIC options from the driver arguments and add them to
/// the command arguments.
///
Expand Down
1 change: 1 addition & 0 deletions flang/include/flang/Frontend/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ CODEGENOPT(InstrumentFunctions, 1, 0) ///< Set when -finstrument_functions is

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.
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
Expand Down
41 changes: 29 additions & 12 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,6 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
if (args.hasArg(clang::driver::options::OPT_finstrument_functions))
opts.InstrumentFunctions = 1;

// -flto=full/thin option.
if (const llvm::opt::Arg *a =
args.getLastArg(clang::driver::options::OPT_flto_EQ)) {
llvm::StringRef s = a->getValue();
assert((s == "full" || s == "thin") && "Unknown LTO mode.");
if (s == "full")
opts.PrepareForFullLTO = true;
else
opts.PrepareForThinLTO = true;
}

if (const llvm::opt::Arg *a = args.getLastArg(
clang::driver::options::OPT_mcode_object_version_EQ)) {
llvm::StringRef s = a->getValue();
Expand Down Expand Up @@ -1482,6 +1471,7 @@ static bool parseLinkerOptionsArgs(CompilerInvocation &invoc,
llvm::opt::ArgList &args,
clang::DiagnosticsEngine &diags) {
llvm::Triple triple = llvm::Triple(invoc.getTargetOpts().triple);
CodeGenOptions &opts = invoc.getCodeGenOpts();

// TODO: support --dependent-lib on other platforms when MLIR supports
// !llvm.dependent.lib
Expand All @@ -1494,8 +1484,35 @@ static bool parseLinkerOptionsArgs(CompilerInvocation &invoc,
return false;
}

invoc.getCodeGenOpts().DependentLibs =
opts.DependentLibs =
args.getAllArgValues(clang::driver::options::OPT_dependent_lib);

// -flto=full/thin option.
if (const llvm::opt::Arg *a =
args.getLastArg(clang::driver::options::OPT_flto_EQ)) {
llvm::StringRef s = a->getValue();
assert((s == "full" || s == "thin") && "Unknown LTO mode.");
if (s == "full")
opts.PrepareForFullLTO = true;
else
opts.PrepareForThinLTO = true;
}

// -ffat-lto-objects
if (const llvm::opt::Arg *arg =
args.getLastArg(clang::driver::options::OPT_ffat_lto_objects,
clang::driver::options::OPT_fno_fat_lto_objects)) {
opts.PrepareForFatLTO =
arg->getOption().matches(clang::driver::options::OPT_ffat_lto_objects);
if (opts.PrepareForFatLTO) {
assert((opts.PrepareForFullLTO || opts.PrepareForThinLTO) &&
"Unknown LTO mode");

if (!triple.isOSBinFormatELF())
diags.Report(clang::diag::err_drv_unsupported_opt_for_target)
<< arg->getAsString(args) << triple.getTriple();
}
}
return true;
}

Expand Down
9 changes: 8 additions & 1 deletion flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,14 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {

// Create the pass manager.
llvm::ModulePassManager mpm;
if (opts.PrepareForFullLTO)
if (opts.PrepareForFatLTO) {
// The module summary should be emitted by default for regular LTO
// except for ld64 targets.
bool emitSummary = opts.PrepareForThinLTO || opts.PrepareForFullLTO ||
triple.getVendor() != llvm::Triple::Apple;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be add a comment why this llvm::Triple::Apple check is required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Updated.

mpm = pb.buildFatLTODefaultPipeline(level, opts.PrepareForThinLTO,
emitSummary);
} else if (opts.PrepareForFullLTO)
mpm = pb.buildLTOPreLinkDefaultPipeline(level);
else if (opts.PrepareForThinLTO)
mpm = pb.buildThinLTOPreLinkDefaultPipeline(level);
Expand Down
6 changes: 6 additions & 0 deletions flang/test/Driver/fatlto-err.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
! RUN: not %flang_fc1 -triple x86_64-apple-macos10.13 -flto -ffat-lto-objects -emit-llvm-bc %s 2>&1 | FileCheck %s --check-prefix=ERROR
! ERROR: error: unsupported option '-ffat-lto-objects' for target 'x86_64-apple-macos10.13'

parameter(i=1)
integer :: j
end program
21 changes: 21 additions & 0 deletions flang/test/Driver/lto-fatlto.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
! REQUIRES: x86-registered-target
! checks fatlto objects: that valid bitcode is included in the object file generated.

! RUN: %flang -fc1 -triple x86_64-unknown-linux-gnu -flto -ffat-lto-objects -emit-obj %s -o %t.o
! RUN: llvm-readelf -S %t.o | FileCheck %s --check-prefixes=ELF
! RUN: llvm-objcopy --dump-section=.llvm.lto=%t.bc %t.o
! RUN: llvm-dis %t.bc -o - | FileCheck %s --check-prefixes=DIS

! ELF: .llvm.lto
! DIS: define void @_QQmain()
! DIS-NEXT: ret void
! DIS-NEXT: }

! RUN: %flang -fc1 -triple x86_64-unknown-linux-gnu -flto -ffat-lto-objects -S %s -o - | FileCheck %s --check-prefixes=ASM

! ASM: .section .llvm.lto,"e",@llvm_lto
! ASM-NEXT: .Lllvm.embedded.object:
! ASM-NEXT: .asciz "BC
! ASM-NEXT: .size .Lllvm.embedded.object
program test
end program
20 changes: 20 additions & 0 deletions flang/test/Driver/lto-lld-flags.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
! UNSUPPORTED: system-windows
! check flto-partitions is passed to lld, and not to fc1
! RUN: %flang -### -fuse-ld=lld -flto=full -flto-partitions=16 %s 2>&1 | FileCheck %s --check-prefixes=LLD-PART,FC1-PART

! FC1-PART: "-fc1"
! FC1-PART-SAME: "-flto=full"
! NOT-FC1-PART-SAME: "-flto-partitions=16"
! LLD-PART: ld.lld
! LLD-PART-SAME: "--lto-partitions=16"

! check fat-lto-objects is passed to lld, fc1
! RUN: %flang -### -fuse-ld=lld -flto -ffat-lto-objects %s 2>&1 | FileCheck %s --check-prefixes=LLD-FAT,FC1-FAT

! FC1-FAT: "-fc1"
! FC1-FAT-SAME: "-flto=full"
! FC1-FAT-SAME: "-ffat-lto-objects"
! LLD-FAT: ld.lld
! LLD-FAT-SAME: "--fat-lto-objects"
program test
end program