Skip to content

Commit bd6e324

Browse files
authored
[clang][driver] Enable '-flto' on bare-metal (#94738)
Pass the linker LTO options enabled by the clang '-flto' command line options when targeting bare-metal. --------- Co-authored-by: Keith Walker <[email protected]>
1 parent c3a5087 commit bd6e324

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
429429
ArgStringList CmdArgs;
430430

431431
auto &TC = static_cast<const toolchains::BareMetal &>(getToolChain());
432+
const Driver &D = getToolChain().getDriver();
432433
const llvm::Triple::ArchType Arch = TC.getArch();
433434
const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
434435

@@ -466,6 +467,19 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
466467
TC.AddLinkRuntimeLib(Args, CmdArgs);
467468
}
468469

470+
if (D.isUsingLTO()) {
471+
assert(!Inputs.empty() && "Must have at least one input.");
472+
// Find the first filename InputInfo object.
473+
auto Input = llvm::find_if(
474+
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
475+
if (Input == Inputs.end())
476+
// For a very rare case, all of the inputs to the linker are
477+
// InputArg. If that happens, just use the first InputInfo.
478+
Input = Inputs.begin();
479+
480+
addLTOOptions(TC, Args, CmdArgs, Output, *Input,
481+
D.getLTOMode() == LTOK_Thin);
482+
}
469483
if (TC.getTriple().isRISCV())
470484
CmdArgs.push_back("-X");
471485

clang/test/Driver/baremetal-ld.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang -### --target=armv7-unknown-none-eabi -mcpu=cortex-m4 --sysroot= -fuse-ld=ld %s 2>&1 | FileCheck --check-prefix=NOLTO %s
2+
// NOLTO: {{".*ld.*"}} {{.*}}
3+
// NOLTO-NOT: "-plugin-opt=mcpu"
4+
5+
// RUN: %clang -### --target=armv7-unknown-none-eabi -mcpu=cortex-m4 --sysroot= -fuse-ld=ld -flto -O3 %s 2>&1 | FileCheck --check-prefix=LTO %s
6+
// LTO: {{".*ld.*"}} {{.*}} "-plugin-opt=mcpu=cortex-m4" "-plugin-opt=O3"

0 commit comments

Comments
 (0)