Index: lld/trunk/ELF/Config.h =================================================================== --- lld/trunk/ELF/Config.h +++ lld/trunk/ELF/Config.h @@ -90,6 +90,7 @@ ELFKind EKind = ELFNoneKind; uint16_t EMachine = llvm::ELF::EM_NONE; uint64_t EntryAddr = -1; + unsigned LtoO = 2; unsigned Optimize = 0; }; Index: lld/trunk/ELF/Driver.cpp =================================================================== --- lld/trunk/ELF/Driver.cpp +++ lld/trunk/ELF/Driver.cpp @@ -282,6 +282,12 @@ error("invalid optimization level"); } + if (auto *Arg = Args.getLastArg(OPT_lto_O)) { + StringRef Val = Arg->getValue(); + if (Val.getAsInteger(10, Config->LtoO)) + error("invalid optimization level"); + } + if (auto *Arg = Args.getLastArg(OPT_hash_style)) { StringRef S = Arg->getValue(); if (S == "gnu") { Index: lld/trunk/ELF/LTO.cpp =================================================================== --- lld/trunk/ELF/LTO.cpp +++ lld/trunk/ELF/LTO.cpp @@ -62,7 +62,7 @@ PMB.VerifyOutput = true; PMB.LoopVectorize = true; PMB.SLPVectorize = true; - PMB.OptLevel = 2; // FIXME: This should be an option. + PMB.OptLevel = Config->LtoO; PMB.populateLTOPassManager(LtoPasses); LtoPasses.run(M); Index: lld/trunk/ELF/Options.td =================================================================== --- lld/trunk/ELF/Options.td +++ lld/trunk/ELF/Options.td @@ -75,6 +75,9 @@ def l : JoinedOrSeparate<["-"], "l">, MetaVarName<"">, HelpText<"Root name of library to use">; +def lto_O : Joined<["--"], "lto-O">, MetaVarName<"">, + HelpText<"Optimization level for LTO">; + def m : JoinedOrSeparate<["-"], "m">, HelpText<"Set target emulation">; Index: lld/trunk/test/ELF/lto/opt-level.ll =================================================================== --- lld/trunk/test/ELF/lto/opt-level.ll +++ lld/trunk/test/ELF/lto/opt-level.ll @@ -0,0 +1,21 @@ +; RUN: llvm-as -o %t.o %s +; RUN: ld.lld -o %t0 -m elf_x86_64 -e main --lto-O0 %t.o +; RUN: llvm-nm %t0 | FileCheck --check-prefix=CHECK-O0 %s +; RUN: ld.lld -o %t2 -m elf_x86_64 -e main --lto-O2 %t.o +; RUN: llvm-nm %t2 | FileCheck --check-prefix=CHECK-O2 %s +; RUN: ld.lld -o %t2a -m elf_x86_64 -e main %t.o +; RUN: llvm-nm %t2a | FileCheck --check-prefix=CHECK-O2 %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK-O0: foo +; CHECK-O2-NOT: foo +define internal void @foo() { + ret void +} + +define void @main() { + call void @foo() + ret void +}