[LTO][Legacy] Decouple option parsing from LTOCodeGenerator · llvm/llvm-project@563cdea (original) (raw)
`@@ -63,8 +63,12 @@ static std::string sLastErrorString;
`
63
63
`// *** Not thread safe ***
`
64
64
`static bool initialized = false;
`
65
65
``
66
``
`-
// Holds the command-line option parsing state of the LTO module.
`
67
``
`-
static bool parsedOptions = false;
`
``
66
`+
// Represent the state of parsing command line debug options.
`
``
67
`+
static enum class OptParsingState {
`
``
68
`+
NotParsed, // Initial state.
`
``
69
`+
Early, // After lto_set_debug_options is called.
`
``
70
`+
Done // After maybeParseOptions is called.
`
``
71
`+
} optionParsingState = OptParsingState::NotParsed;
`
68
72
``
69
73
`static LLVMContext *LTOContext = nullptr;
`
70
74
``
`@@ -418,10 +422,11 @@ void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
`
418
422
`}
`
419
423
``
420
424
`static void maybeParseOptions(lto_code_gen_t cg) {
`
421
``
`-
if (!parsedOptions) {
`
``
425
`+
if (optionParsingState != OptParsingState::Done) {
`
``
426
`+
// Parse options if any were set by the lto_codegen_debug_options* function.
`
422
427
`unwrap(cg)->parseCodeGenDebugOptions();
`
423
428
`lto_add_attrs(cg);
`
424
``
`-
parsedOptions = true;
`
``
429
`+
optionParsingState = OptParsingState::Done;
`
425
430
` }
`
426
431
`}
`
427
432
``
`@@ -460,7 +465,22 @@ bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
`
460
465
`return !unwrap(cg)->compile_to_file(name);
`
461
466
`}
`
462
467
``
``
468
`+
void lto_set_debug_options(const char *const *options, int number) {
`
``
469
`+
assert(optionParsingState == OptParsingState::NotParsed &&
`
``
470
`+
"option processing already happened");
`
``
471
`+
// Need to put each suboption in a null-terminated string before passing to
`
``
472
`+
// parseCommandLineOptions().
`
``
473
`+
std::vectorstd::string Options;
`
``
474
`+
for (int i = 0; i < number; ++i)
`
``
475
`+
Options.push_back(options[i]);
`
``
476
+
``
477
`+
llvm::parseCommandLineOptions(Options);
`
``
478
`+
optionParsingState = OptParsingState::Early;
`
``
479
`+
}
`
``
480
+
463
481
`void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
`
``
482
`+
assert(optionParsingState != OptParsingState::Early &&
`
``
483
`+
"early option processing already happened");
`
464
484
` SmallVector<StringRef, 4> Options;
`
465
485
`for (std::pair<StringRef, StringRef> o = getToken(opt); !o.first.empty();
`
466
486
` o = getToken(o.second))
`
`@@ -471,6 +491,8 @@ void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
`
471
491
``
472
492
`void lto_codegen_debug_options_array(lto_code_gen_t cg,
`
473
493
`const char *const *options, int number) {
`
``
494
`+
assert(optionParsingState != OptParsingState::Early &&
`
``
495
`+
"early option processing already happened");
`
474
496
` SmallVector<StringRef, 4> Options;
`
475
497
`for (int i = 0; i < number; ++i)
`
476
498
` Options.push_back(options[i]);
`