[LTO][Legacy] Add API for passing LLVM options separately
authorFrancis Visoiu Mistrih <francisvm@yahoo.com>
Tue, 19 Nov 2019 22:04:59 +0000 (14:04 -0800)
committerFrancis Visoiu Mistrih <francisvm@yahoo.com>
Wed, 20 Nov 2019 00:30:37 +0000 (16:30 -0800)
In order to correctly pass options to LLVM, including options containing
spaces which are used as delimiters for multiple options in
lto_codegen_debug_options, add a new API:
lto_codegen_debug_options_array.

Unfortunately, tools/lto has no testing infrastructure yet, so there are
no tests associated with this patch.

Differential Revision: https://reviews.llvm.org/D70463

llvm/include/llvm-c/lto.h
llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h
llvm/lib/LTO/LTOCodeGenerator.cpp
llvm/tools/lto/lto.cpp
llvm/tools/lto/lto.exports

index 0c6ce673162a2316327a041b409e76d3b98d1d2b..97a8f4823320216fdeaea77d997d52b79ff7ddc8 100644 (file)
@@ -46,7 +46,7 @@ typedef bool lto_bool_t;
  * @{
  */
 
-#define LTO_API_VERSION 25
+#define LTO_API_VERSION 26
 
 /**
  * \since prior to LTO_API_VERSION=3
@@ -514,11 +514,24 @@ lto_api_version(void);
 /**
  * Sets options to help debug codegen bugs.
  *
+ * This function takes one or more options separated by spaces.
+ * Warning: passing file paths through this function may confuse the argument
+ * parser if the paths contain spaces.
+ *
  * \since prior to LTO_API_VERSION=3
  */
 extern void
 lto_codegen_debug_options(lto_code_gen_t cg, const char *);
 
+/**
+ * Same as the previous function, but takes every option separately through an
+ * array.
+ *
+ * \since prior to LTO_API_VERSION=26
+ */
+extern void lto_codegen_debug_options_array(lto_code_gen_t cg,
+                                            const char *const *, int number);
+
 /**
  * Initializes LLVM disassemblers.
  * FIXME: This doesn't really belong here.
index acb0b1fec93800ac5e4a8ed3ce63d9554d63897d..114ba85947a506408f92ff93af9791fabfa43db5 100644 (file)
@@ -35,6 +35,7 @@
 #define LLVM_LTO_LTOCODEGENERATOR_H
 
 #include "llvm-c/lto.h"
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
@@ -122,7 +123,7 @@ struct LTOCodeGenerator {
   /// name is misleading).  This function should be called before
   /// LTOCodeGenerator::compilexxx(), and
   /// LTOCodeGenerator::writeMergedModules().
-  void setCodeGenDebugOptions(StringRef Opts);
+  void setCodeGenDebugOptions(ArrayRef<const char *> Opts);
 
   /// Parse the options set in setCodeGenDebugOptions.
   ///
index f87f232c63ebcda999f8fd094c8b98e742b6e114..5fef14230a9bb24e1f47e4d436725b595e7dfb02 100644 (file)
@@ -622,12 +622,9 @@ bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
   return true;
 }
 
-/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
-/// LTO problems.
-void LTOCodeGenerator::setCodeGenDebugOptions(StringRef Options) {
-  for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
-       o = getToken(o.second))
-    CodegenOptions.push_back(o.first);
+void LTOCodeGenerator::setCodeGenDebugOptions(ArrayRef<const char *> Options) {
+  for (StringRef Option : Options)
+    CodegenOptions.push_back(Option);
 }
 
 void LTOCodeGenerator::parseCodeGenDebugOptions() {
index 9ff16e85db4c763ff06aaec2a4e273ac69e31869..9933af94de1e5243fc02e00da1c2ba5be66ddc96 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "llvm-c/lto.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/IR/DiagnosticInfo.h"
@@ -453,7 +454,17 @@ bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
 }
 
 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
-  unwrap(cg)->setCodeGenDebugOptions(opt);
+  std::vector<const char *> Options;
+  for (std::pair<StringRef, StringRef> o = getToken(opt); !o.first.empty();
+       o = getToken(o.second))
+    Options.push_back(o.first.data());
+
+  unwrap(cg)->setCodeGenDebugOptions(Options);
+}
+
+void lto_codegen_debug_options_array(lto_code_gen_t cg,
+                                     const char *const *options, int number) {
+  unwrap(cg)->setCodeGenDebugOptions(makeArrayRef(options, number));
 }
 
 unsigned int lto_api_version() { return LTO_API_VERSION; }
index d24809fe762f51a6b57ed25311f69ed7c5ca3061..fd2212cb5f33839408d386a9d8c0fe0ed5c83607 100644 (file)
@@ -33,6 +33,7 @@ lto_codegen_set_debug_model
 lto_codegen_set_pic_model
 lto_codegen_write_merged_modules
 lto_codegen_debug_options
+lto_codegen_debug_options_array
 lto_codegen_set_assembler_args
 lto_codegen_set_assembler_path
 lto_codegen_set_cpu