[PATCH] D78158: [ELF] Refactor the way we handle -plugin-opt= (GCC collect2 or clang LTO related options)

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 15:12:40 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, ruiu, tejohnson.
Herald added subscribers: llvm-commits, dexonsmith, steven_wu, hiraditya, arichardson, inglorion, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.
MaskRay edited the summary of this revision.

GCC collect2 passes several options to the linker even if LTO is not used
(note, lld does not support GCC LTO). The lto-wrapper may be a relative
path (especially during development, when gcc is in a build directory), e.g.

  -plugin-opt=relative/path/to/lto-wrapper

We need to ignore such options, which are currently interpreted by
cl::ParseCommandLineOptions() and will fail with `error: --plugin-opt: ld.lld: Unknown command line argument 'relative/path/to/lto-wrapper'`
because the path is apparently not an option registered by an `llvm::cl::opt`.

See lto-plugin-ignore.s for how we interpret various -plugin-opt= options now.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78158

Files:
  lld/ELF/Driver.cpp
  lld/ELF/Options.td
  lld/test/ELF/lto-plugin-ignore.s


Index: lld/test/ELF/lto-plugin-ignore.s
===================================================================
--- lld/test/ELF/lto-plugin-ignore.s
+++ lld/test/ELF/lto-plugin-ignore.s
@@ -1,10 +1,25 @@
 # REQUIRES: x86
+## Test we ignore some LTO related options from clang/GCC collect2.
 
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -plugin-opt=/foo/bar -plugin-opt=-fresolution=zed \
-# RUN:   -plugin-opt=-pass-through=-lgcc -plugin-opt=-function-sections \
-# RUN:   -plugin-opt=-data-sections -plugin-opt=thinlto -o /dev/null
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
 
-# RUN: not ld.lld %t -plugin-opt=-abc -plugin-opt=-xyz 2>&1 | FileCheck %s
-# CHECK: ld.lld: error: --plugin-opt: ld.lld{{.*}}: Unknown command line argument '-abc'
-# CHECK: ld.lld: error: --plugin-opt: ld.lld{{.*}}: Unknown command line argument '-xyz'
+## GCC collect2 passes several LTO related options to the linker even if -flto is not used.
+## We need to ignore them. Note that a path can be relative.
+# RUN: ld.lld %t.o -o /dev/null \
+# RUN:   -plugin-opt=/abs/path/to/liblto_plugin.so \
+# RUN:   -plugin-opt=relative/path/to/lto-wrapper \
+# RUN:   -plugin-opt=-fresolution=zed \
+# RUN:   -plugin-opt=-pass-through=-lgcc
+
+## Clang LTO passes several options to the linker, which are intended to be consumed by
+## LLVMgold.so. We need to ignore them.
+# RUN: ld.lld %t.o -o /dev/null -plugin /path/to/LLVMgold.so -plugin-opt=thinlto
+
+## Other -plugin-opt=- prefixed options are passed through to cl::ParseCommandLineOptions.
+# RUN: not ld.lld %t.o -o /dev/null -plugin-opt=-abc -plugin-opt=-xyz 2>&1 | FileCheck %s
+# CHECK: ld.lld: error: -plugin-opt=-: ld.lld{{.*}}: Unknown command line argument '-abc'
+# CHECK: ld.lld: error: -plugin-opt=-: ld.lld{{.*}}: Unknown command line argument '-xyz'
+
+## Some LLVMgold.so options start with -plugin-opt= but not with -plugin-opt=-.
+## They are silently ignored.
+# RUN: ld.lld %t.o -o /dev/null -plugin-opt=LLVMgold-feature
Index: lld/ELF/Options.td
===================================================================
--- lld/ELF/Options.td
+++ lld/ELF/Options.td
@@ -504,7 +504,6 @@
   HelpText<"Include hotness information in the optimization remarks file">;
 def opt_remarks_format: Separate<["--"], "opt-remarks-format">,
   HelpText<"The format used for serializing remarks (default: YAML)">;
-defm plugin_opt: Eq<"plugin-opt", "specifies LTO options for compatibility with GNU linkers">;
 def save_temps: F<"save-temps">;
 def lto_basicblock_sections: J<"lto-basicblock-sections=">,
   HelpText<"Enable basic block sections for LTO">;
@@ -524,6 +523,8 @@
 def thinlto_object_suffix_replace_eq: J<"thinlto-object-suffix-replace=">;
 def thinlto_prefix_replace_eq: J<"thinlto-prefix-replace=">;
 
+def plugin_opt_EQ_MINUS: J<"plugin-opt=-">,
+  HelpText<"Specify an LLVM option for compatibility with LLVMgold.so">;
 def: J<"plugin-opt=O">, Alias<lto_O>, HelpText<"Alias for --lto-O">;
 def: F<"plugin-opt=debug-pass-manager">,
   Alias<lto_debug_pass_manager>, HelpText<"Alias for --lto-debug-pass-manager">;
@@ -571,10 +572,9 @@
 // --version output.
 defm plugin: Eq<"plugin", "Ignored for compatibility with GNU linkers">;
 
+def plugin_opt_EQ: J<"plugin-opt=">;
 def plugin_opt_fresolution_eq: J<"plugin-opt=-fresolution=">;
 def plugin_opt_pass_through_eq: J<"plugin-opt=-pass-through=">;
-def plugin_opt_thinlto: J<"plugin-opt=thinlto">;
-def plugin_opt_slash: J<"plugin-opt=/">;
 
 // Options listed below are silently ignored for now for compatibility.
 def: F<"detect-odr-violations">;
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -1035,8 +1035,8 @@
     parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
                      arg->getSpelling());
 
-  for (auto *arg : args.filtered(OPT_plugin_opt))
-    parseClangOption(arg->getValue(), arg->getSpelling());
+  for (opt::Arg *arg : args.filtered(OPT_plugin_opt_EQ_MINUS))
+    parseClangOption(std::string("-") + arg->getValue(), arg->getSpelling());
 
   // Parse -mllvm options.
   for (auto *arg : args.filtered(OPT_mllvm))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78158.257510.patch
Type: text/x-patch
Size: 4208 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200414/4734f3f5/attachment.bin>


More information about the llvm-commits mailing list