Commit 36ae2556 authored by Peter Kokai's avatar Peter Kokai Committed by Fangrui Song
Browse files

[opt] Fix run-twice crash and detection problem

1. Execute `opt -run-twice a.ll` with in a terminal will crash.
   https://bugs.llvm.org/show_bug.cgi?id=44382
2. `-run-twice` saves output into two buffers and compares them.
   When outputing the result is disabled, that produces two empty string thus
   they are going to be equal all the time resulting false-positive results.

The proposed solution is to generate the results even if the output will not be
emitted, as that is required for the comparision.

Differential Revision: https://reviews.llvm.org/D71967
parent b47b35ff
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -901,8 +901,10 @@ int main(int argc, char **argv) {
  std::unique_ptr<raw_svector_ostream> BOS;
  raw_ostream *OS = nullptr;

  const bool ShouldEmitOutput = !NoOutput && !AnalyzeOnly;

  // Write bitcode or assembly to the output as the last step...
  if (!NoOutput && !AnalyzeOnly) {
  if (ShouldEmitOutput || RunTwice) {
    assert(Out);
    OS = &Out->os();
    if (RunTwice) {
@@ -950,12 +952,15 @@ int main(int argc, char **argv) {
             "Writing the result of the second run to the specified output.\n"
             "To generate the one-run comparison binary, just run without\n"
             "the compile-twice option\n";
      if (ShouldEmitOutput) {
        Out->os() << BOS->str();
        Out->keep();
      }
      if (RemarksFile)
        RemarksFile->keep();
      return 1;
    }
    if (ShouldEmitOutput)
      Out->os() << BOS->str();
  }