Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Add option to skip writing an output file
Browse files Browse the repository at this point in the history
Summary:
The user may wish to run BOLT for printing statistics only
(i.e. to check that the profile is valid). Add an option to run BOLT
without writing any output file, similar to a dry run. This option
is triggered by supplying -o with "/dev/null".

Reviewed By: maksfb

Differential Revision: D26347237

fbshipit-source-id: 14214eab075
  • Loading branch information
rafaelauler authored and facebook-github-bot committed Feb 10, 2021
1 parent 125a226 commit de8a890
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<ToolOutputFile> TempOut =
llvm::make_unique<ToolOutputFile>(opts::OutputFilename + ".bolt.o",
EC, sys::fs::F_None);
llvm::make_unique<ToolOutputFile>(OutObjectPath, EC, sys::fs::F_None);
check_error(EC, "cannot create output object file");

std::unique_ptr<buffer_ostream> BOS =
Expand Down Expand Up @@ -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() {
Expand Down
29 changes: 29 additions & 0 deletions test/X86/no-output.test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit de8a890

Please sign in to comment.