[ELF] - Use InitTargetOptionsFromCodeGenFlags/ParseCommandLineOptions for parsing LTO options.

gold plugin uses InitTargetOptionsFromCodeGenFlags + 
ParseCommandLineOptions for parsing LTO options.
Patch do the same change for LLD.

Such change helps to avoid parsing/whitelisting LTO
plugin options again on linker side, what can help LLD
to automatically support new -plugin-opt=xxx options
passed.

Differential revision: https://reviews.llvm.org/D42733

llvm-svn: 324322
This commit is contained in:
George Rimar 2018-02-06 09:05:56 +00:00
parent 89ea2648bb
commit 86372289a8
2 changed files with 14 additions and 17 deletions

View File

@ -42,6 +42,7 @@
#include "lld/Common/Driver.h" #include "lld/Common/Driver.h"
#include "lld/Common/ErrorHandler.h" #include "lld/Common/ErrorHandler.h"
#include "lld/Common/Memory.h" #include "lld/Common/Memory.h"
#include "lld/Common/TargetOptionsCommandFlags.h"
#include "lld/Common/Threads.h" #include "lld/Common/Threads.h"
#include "lld/Common/Version.h" #include "lld/Common/Version.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
@ -252,18 +253,11 @@ void LinkerDriver::addLibrary(StringRef Name) {
// LTO calls LLVM functions to compile bitcode files to native code. // LTO calls LLVM functions to compile bitcode files to native code.
// Technically this can be delayed until we read bitcode files, but // Technically this can be delayed until we read bitcode files, but
// we don't bother to do lazily because the initialization is fast. // we don't bother to do lazily because the initialization is fast.
static void initLLVM(opt::InputArgList &Args) { static void initLLVM() {
InitializeAllTargets(); InitializeAllTargets();
InitializeAllTargetMCs(); InitializeAllTargetMCs();
InitializeAllAsmPrinters(); InitializeAllAsmPrinters();
InitializeAllAsmParsers(); InitializeAllAsmParsers();
// Parse and evaluate -mllvm options.
std::vector<const char *> V;
V.push_back("lld (LLVM option parsing)");
for (auto *Arg : Args.filtered(OPT_mllvm))
V.push_back(Arg->getValue());
cl::ParseCommandLineOptions(V.size(), V.data());
} }
// Some command line options or some combinations of them are not allowed. // Some command line options or some combinations of them are not allowed.
@ -372,7 +366,7 @@ void LinkerDriver::main(ArrayRef<const char *> ArgsArr, bool CanExitEarly) {
} }
readConfigs(Args); readConfigs(Args);
initLLVM(Args); initLLVM();
createFiles(Args); createFiles(Args);
inferMachineType(); inferMachineType();
setConfigs(Args); setConfigs(Args);
@ -696,6 +690,7 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args) {
Config->ZWxneeded = hasZOption(Args, "wxneeded"); Config->ZWxneeded = hasZOption(Args, "wxneeded");
// Parse LTO plugin-related options for compatibility with gold. // Parse LTO plugin-related options for compatibility with gold.
std::vector<const char *> LTOOptions({Config->Argv[0].data()});
for (auto *Arg : Args.filtered(OPT_plugin_opt)) { for (auto *Arg : Args.filtered(OPT_plugin_opt)) {
StringRef S = Arg->getValue(); StringRef S = Arg->getValue();
if (S == "disable-verify") if (S == "disable-verify")
@ -709,11 +704,13 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args) {
else if (S.startswith("jobs=")) else if (S.startswith("jobs="))
Config->ThinLTOJobs = parseInt(S.substr(5), Arg); Config->ThinLTOJobs = parseInt(S.substr(5), Arg);
else if (!S.startswith("/") && !S.startswith("-fresolution=") && else if (!S.startswith("/") && !S.startswith("-fresolution=") &&
!S.startswith("-pass-through=") && !S.startswith("mcpu=") && !S.startswith("-pass-through="))
!S.startswith("thinlto") && S != "-function-sections" && LTOOptions.push_back(S.data());
S != "-data-sections")
error(Arg->getSpelling() + ": unknown option: " + S);
} }
// Parse and evaluate -mllvm options.
for (auto *Arg : Args.filtered(OPT_mllvm))
LTOOptions.push_back(Arg->getValue());
cl::ParseCommandLineOptions(LTOOptions.size(), LTOOptions.data());
if (Config->LTOO > 3) if (Config->LTOO > 3)
error("invalid optimization level for LTO: " + Twine(Config->LTOO)); error("invalid optimization level for LTO: " + Twine(Config->LTOO));

View File

@ -5,7 +5,7 @@
# RUN: -plugin-opt=-pass-through=-lgcc -plugin-opt=-function-sections \ # RUN: -plugin-opt=-pass-through=-lgcc -plugin-opt=-function-sections \
# RUN: -plugin-opt=-data-sections -o /dev/null # RUN: -plugin-opt=-data-sections -o /dev/null
# RUN: not ld.lld %t -plugin-opt=-data-sectionssss \ # RUN: not ld.lld %t -plugin-opt=-data-sectionxxx \
# RUN: -plugin-opt=-function-sectionsss 2>&1 | FileCheck %s # RUN: -plugin-opt=-function-sectionxxx 2>&1 | FileCheck %s
# CHECK: unknown option: -data-sectionsss # CHECK: Unknown command line argument '-data-sectionxxx'
# CHECK: unknown option: -function-sectionsss # CHECK: Unknown command line argument '-function-sectionxxx'