diff --git a/src/RewriteInstance.cpp b/src/RewriteInstance.cpp index 58037d99df88..85f1555d9589 100644 --- a/src/RewriteInstance.cpp +++ b/src/RewriteInstance.cpp @@ -58,6 +58,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/DataExtractor.h" #include "llvm/Support/Errc.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" @@ -836,6 +837,9 @@ void RewriteInstance::run() { if (opts::LinuxKernelMode) { errs() << "BOLT-WARNING: not writing the output file for Linux Kernel\n"; return; + } else if (opts::OutputFilename == "/dev/null") { + outs() << "BOLT-INFO: skipping writing final binary to disk\n"; + return; } // Rewrite allocatable contents and copy non-allocatable parts with mods. @@ -2939,9 +2943,10 @@ void RewriteInstance::emitAndLink() { // This is an object file, which we keep for debugging purposes. // Once we decide it's useless, we should create it in memory. + SmallString<128> OutObjectPath; + sys::fs::getPotentiallyUniqueTempFileName("output", "o", OutObjectPath); std::unique_ptr TempOut = - llvm::make_unique(opts::OutputFilename + ".bolt.o", - EC, sys::fs::F_None); + llvm::make_unique(OutObjectPath, EC, sys::fs::F_None); check_error(EC, "cannot create output object file"); std::unique_ptr BOS = @@ -3095,8 +3100,12 @@ void RewriteInstance::emitAndLink() { CacheMetrics::printAll(BC->getSortedFunctions()); } - if (opts::KeepTmp) + if (opts::KeepTmp) { TempOut->keep(); + outs() << "BOLT-INFO: intermediary output object file saved for debugging " + "purposes: " + << OutObjectPath << "\n"; + } } void RewriteInstance::updateMetadata() { diff --git a/test/X86/no-output.test b/test/X86/no-output.test new file mode 100644 index 000000000000..b8b25248bf13 --- /dev/null +++ b/test/X86/no-output.test @@ -0,0 +1,29 @@ +# This script checks that BOLT is able to work in dry run mode (no output) + +# REQUIRES: system-linux + +# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown \ +# RUN: %s -o %t.o +# RUN: link_fdata %s %t.o %t.fdata +# RUN: strip --strip-unneeded %t.o +# RUN: %host_cc %t.o -o %t.exe -Wl,-q +# RUN: llvm-bolt %t.exe -relocs=1 -print-profile-stats -o /dev/null \ +# RUN: -data %t.fdata | FileCheck %s + + .text + .globl main + .type main, %function + .p2align 4 +main: +# FDATA: 0 [unknown] 0 1 main 0 0 510 + pushq %rbp + movq %rsp, %rbp + subq $0x18, %rsp + addq $0x18, %rsp + xorq %rax, %rax + leaveq + retq +.size main, .-main + + +# CHECK: Skipping writing final binary