#include "Delta.h"
#include "ReducerWorkItem.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/ToolOutputFile.h"
#include <fstream>
#include <set>
using namespace llvm;
extern cl::OptionCategory LLVMReduceOptions;
static cl::opt<bool> AbortOnInvalidReduction(
"abort-on-invalid-reduction",
cl::desc("Abort if any reduction results in invalid IR"),
cl::cat(LLVMReduceOptions));
static cl::opt<unsigned int> StartingGranularityLevel(
"starting-granularity-level",
cl::desc("Number of times to divide chunks prior to first test"),
cl::cat(LLVMReduceOptions));
static cl::opt<bool> TmpFilesAsBitcode(
"write-tmp-files-as-bitcode",
cl::desc("Write temporary files as bitcode, instead of textual IR"),
cl::init(false), cl::cat(LLVMReduceOptions));
#ifdef LLVM_ENABLE_THREADS
static cl::opt<unsigned> NumJobs(
"j",
cl::desc("Maximum number of threads to use to process chunks. Set to 1 to "
"disables parallelism."),
cl::init(1), cl::cat(LLVMReduceOptions));
#else
unsigned NumJobs = 1;
#endif
void writeOutput(ReducerWorkItem &M, llvm::StringRef Message);
void writeBitcode(ReducerWorkItem &M, raw_ostream &OutStream);
void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx,
const char *ToolName);
bool isReduced(ReducerWorkItem &M, TestRunner &Test,
SmallString<128> &CurrentFilepath) {
int FD;
std::error_code EC = sys::fs::createTemporaryFile(
"llvm-reduce", M.isMIR() ? "mir" : (TmpFilesAsBitcode ? "bc" : "ll"), FD,
CurrentFilepath);
if (EC) {
errs() << "Error making unique filename: " << EC.message() << "!\n";
exit(1);
}
if (TmpFilesAsBitcode) {
llvm::raw_fd_ostream OutStream(FD, true);
writeBitcode(M, OutStream);
OutStream.close();
if (OutStream.has_error()) {
errs() << "Error emitting bitcode to file '" << CurrentFilepath << "'!\n";
sys::fs::remove(CurrentFilepath);
exit(1);
}
bool Res = Test.run(CurrentFilepath);
sys::fs::remove(CurrentFilepath);
return Res;
}
ToolOutputFile Out(CurrentFilepath, FD);
M.print(Out.os(), nullptr);
Out.os().close();
if (Out.os().has_error()) {
errs() << "Error emitting bitcode to file '" << CurrentFilepath << "'!\n";
exit(1);
}
return Test.run(CurrentFilepath);
}
static int getLines(StringRef Filepath) {
int Lines = 0;
std::string CurrLine;
std::ifstream FileStream{std::string(Filepath)};
while (std::getline(FileStream, CurrLine))
++Lines;
return Lines;
}
static bool increaseGranularity(std::vector<Chunk> &Chunks) {
errs() << "Increasing granularity...";
std::vector<Chunk> NewChunks;
bool SplitOne = false;
for (auto &C : Chunks) {
if (C.End - C.Begin == 0)
NewChunks.push_back(C);
else {
int Half = (C.Begin + C.End) / 2;
NewChunks.push_back({C.Begin, Half});
NewChunks.push_back({Half + 1, C.End});
SplitOne = true;
}
}
if (SplitOne) {
Chunks = NewChunks;
errs() << "Success! New Chunks:\n";
for (auto C : Chunks) {
errs() << '\t';
C.print();
errs() << '\n';
}
}
return SplitOne;
}
template <typename FuncType>
static std::unique_ptr<ReducerWorkItem>
CheckChunk(Chunk &ChunkToCheckForUninterestingness,
std::unique_ptr<ReducerWorkItem> Clone, TestRunner &Test,
FuncType ExtractChunksFromModule,
std::set<Chunk> &UninterestingChunks,
std::vector<Chunk> &ChunksStillConsideredInteresting) {
std::vector<Chunk> CurrentChunks;
CurrentChunks.reserve(ChunksStillConsideredInteresting.size() -
UninterestingChunks.size() - 1);
copy_if(ChunksStillConsideredInteresting, std::back_inserter(CurrentChunks),
[&](const Chunk &C) {
return !UninterestingChunks.count(C) &&
C != ChunkToCheckForUninterestingness;
});
Oracle O(CurrentChunks);
ExtractChunksFromModule(O, *Clone);
if (verifyReducerWorkItem(*Clone, &errs())) {
if (AbortOnInvalidReduction) {
errs() << "Invalid reduction\n";
exit(1);
}
errs() << " **** WARNING | reduction resulted in invalid module, "
"skipping\n";
return nullptr;
}
errs() << "Ignoring: ";
ChunkToCheckForUninterestingness.print();
for (const Chunk &C : UninterestingChunks)
C.print();
SmallString<128> CurrentFilepath;
if (!isReduced(*Clone, Test, CurrentFilepath)) {
errs() << "\n";
return nullptr;
}
return Clone;
}
template <typename FuncType>
SmallString<0> ProcessChunkFromSerializedBitcode(
Chunk &ChunkToCheckForUninterestingness, TestRunner &Test,
FuncType ExtractChunksFromModule, std::set<Chunk> &UninterestingChunks,
std::vector<Chunk> &ChunksStillConsideredInteresting,
SmallString<0> &OriginalBC, std::atomic<bool> &AnyReduced) {
LLVMContext Ctx;
auto CloneMMM = std::make_unique<ReducerWorkItem>();
auto Data = MemoryBufferRef(StringRef(OriginalBC.data(), OriginalBC.size()),
"<bc file>");
readBitcode(*CloneMMM, Data, Ctx, Test.getToolName());
SmallString<0> Result;
if (std::unique_ptr<ReducerWorkItem> ChunkResult =
CheckChunk(ChunkToCheckForUninterestingness, std::move(CloneMMM),
Test, ExtractChunksFromModule, UninterestingChunks,
ChunksStillConsideredInteresting)) {
raw_svector_ostream BCOS(Result);
writeBitcode(*ChunkResult, BCOS);
AnyReduced = true;
}
return Result;
}
void llvm::runDeltaPass(TestRunner &Test,
ReductionFunc ExtractChunksFromModule) {
assert(!verifyReducerWorkItem(Test.getProgram(), &errs()) &&
"input module is broken before making changes");
SmallString<128> CurrentFilepath;
if (!isReduced(Test.getProgram(), Test, CurrentFilepath)) {
errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
exit(1);
}
int Targets;
{
std::vector<Chunk> AllChunks = {{0, INT_MAX}};
Oracle Counter(AllChunks);
ExtractChunksFromModule(Counter, Test.getProgram());
Targets = Counter.count();
assert(!verifyReducerWorkItem(Test.getProgram(), &errs()) &&
"input module is broken after counting chunks");
assert(isReduced(Test.getProgram(), Test, CurrentFilepath) &&
"input module no longer interesting after counting chunks");
#ifndef NDEBUG
std::vector<Chunk> NoChunks;
Oracle NoChunksCounter(NoChunks);
std::unique_ptr<ReducerWorkItem> Clone =
cloneReducerWorkItem(Test.getProgram(), Test.getTargetMachine());
ExtractChunksFromModule(NoChunksCounter, *Clone);
assert(Targets == NoChunksCounter.count() &&
"number of chunks changes when reducing");
#endif
}
if (!Targets) {
errs() << "\nNothing to reduce\n";
return;
}
std::vector<Chunk> ChunksStillConsideredInteresting = {{0, Targets - 1}};
std::unique_ptr<ReducerWorkItem> ReducedProgram;
for (unsigned int Level = 0; Level < StartingGranularityLevel; Level++) {
increaseGranularity(ChunksStillConsideredInteresting);
}
std::atomic<bool> AnyReduced;
std::unique_ptr<ThreadPool> ChunkThreadPoolPtr;
if (NumJobs > 1)
ChunkThreadPoolPtr =
std::make_unique<ThreadPool>(hardware_concurrency(NumJobs));
bool FoundAtLeastOneNewUninterestingChunkWithCurrentGranularity;
do {
FoundAtLeastOneNewUninterestingChunkWithCurrentGranularity = false;
std::set<Chunk> UninterestingChunks;
SmallString<0> OriginalBC;
if (NumJobs > 1) {
raw_svector_ostream BCOS(OriginalBC);
writeBitcode(Test.getProgram(), BCOS);
}
std::deque<std::shared_future<SmallString<0>>> TaskQueue;
for (auto I = ChunksStillConsideredInteresting.rbegin(),
E = ChunksStillConsideredInteresting.rend();
I != E; ++I) {
std::unique_ptr<ReducerWorkItem> Result = nullptr;
unsigned WorkLeft = std::distance(I, E);
if (NumJobs > 1 && WorkLeft > 1) {
unsigned NumInitialTasks = std::min(WorkLeft, unsigned(NumJobs));
unsigned NumChunksProcessed = 0;
ThreadPool &ChunkThreadPool = *ChunkThreadPoolPtr;
TaskQueue.clear();
AnyReduced = false;
for (unsigned J = 0; J < NumInitialTasks; ++J) {
TaskQueue.emplace_back(ChunkThreadPool.async(
[J, I, &Test, &ExtractChunksFromModule, &UninterestingChunks,
&ChunksStillConsideredInteresting, &OriginalBC, &AnyReduced]() {
return ProcessChunkFromSerializedBitcode(
*(I + J), Test, ExtractChunksFromModule,
UninterestingChunks, ChunksStillConsideredInteresting,
OriginalBC, AnyReduced);
}));
}
while (!TaskQueue.empty()) {
auto &Future = TaskQueue.front();
Future.wait();
NumChunksProcessed++;
SmallString<0> Res = Future.get();
TaskQueue.pop_front();
if (Res.empty()) {
unsigned NumScheduledTasks = NumChunksProcessed + TaskQueue.size();
if (!AnyReduced && I + NumScheduledTasks != E) {
Chunk &ChunkToCheck = *(I + NumScheduledTasks);
TaskQueue.emplace_back(ChunkThreadPool.async(
[&Test, &ExtractChunksFromModule, &UninterestingChunks,
&ChunksStillConsideredInteresting, &OriginalBC,
&ChunkToCheck, &AnyReduced]() {
return ProcessChunkFromSerializedBitcode(
ChunkToCheck, Test, ExtractChunksFromModule,
UninterestingChunks, ChunksStillConsideredInteresting,
OriginalBC, AnyReduced);
}));
}
continue;
}
Result = std::make_unique<ReducerWorkItem>();
auto Data = MemoryBufferRef(StringRef(Res.data(), Res.size()),
"<bc file>");
readBitcode(*Result, Data, Test.getProgram().M->getContext(),
Test.getToolName());
break;
}
I += NumChunksProcessed - 1;
} else {
Result = CheckChunk(
*I,
cloneReducerWorkItem(Test.getProgram(), Test.getTargetMachine()),
Test, ExtractChunksFromModule, UninterestingChunks,
ChunksStillConsideredInteresting);
}
if (!Result)
continue;
Chunk &ChunkToCheckForUninterestingness = *I;
FoundAtLeastOneNewUninterestingChunkWithCurrentGranularity = true;
UninterestingChunks.insert(ChunkToCheckForUninterestingness);
ReducedProgram = std::move(Result);
errs() << " **** SUCCESS | lines: " << getLines(CurrentFilepath) << "\n";
writeOutput(*ReducedProgram, "Saved new best reduction to ");
}
erase_if(ChunksStillConsideredInteresting,
[&UninterestingChunks](const Chunk &C) {
return UninterestingChunks.count(C);
});
} while (!ChunksStillConsideredInteresting.empty() &&
(FoundAtLeastOneNewUninterestingChunkWithCurrentGranularity ||
increaseGranularity(ChunksStillConsideredInteresting)));
if (ReducedProgram)
Test.setProgram(std::move(ReducedProgram));
errs() << "Couldn't increase anymore.\n";
}