aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-reduce/llvm-reduce.cpp
diff options
context:
space:
mode:
authorMatthew Voss <matthew.voss@sony.com>2022-06-30 08:53:00 -0700
committerMatthew Voss <matthew.voss@sony.com>2022-06-30 08:58:24 -0700
commit6b3956e123db1a53e38f5506aa164be4d644a477 (patch)
tree23da0a5b72b4545ad54d20d5a725d5a44df92375 /llvm/tools/llvm-reduce/llvm-reduce.cpp
parent13f9089ac92b1f800b6752d031c287781f215575 (diff)
downloadllvm-6b3956e123db1a53e38f5506aa164be4d644a477.zip
llvm-6b3956e123db1a53e38f5506aa164be4d644a477.tar.gz
llvm-6b3956e123db1a53e38f5506aa164be4d644a477.tar.bz2
[llvm-reduce] Add support for LTO bitcode files
Adds support for reading and writing LTO bitcode files. - Emit a summary if the original bitcode file had a summary - Use split LTO units if the original bitcode file used them. Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D127168
Diffstat (limited to 'llvm/tools/llvm-reduce/llvm-reduce.cpp')
-rw-r--r--llvm/tools/llvm-reduce/llvm-reduce.cpp51
1 files changed, 40 insertions, 11 deletions
diff --git a/llvm/tools/llvm-reduce/llvm-reduce.cpp b/llvm/tools/llvm-reduce/llvm-reduce.cpp
index 3cf2bbc..4d338f4 100644
--- a/llvm/tools/llvm-reduce/llvm-reduce.cpp
+++ b/llvm/tools/llvm-reduce/llvm-reduce.cpp
@@ -17,10 +17,15 @@
#include "DeltaManager.h"
#include "ReducerWorkItem.h"
#include "TestRunner.h"
+#include "llvm/Analysis/ProfileSummaryInfo.h"
+#include "llvm/Analysis/ModuleSummaryAnalysis.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/Bitcode/BitcodeReader.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
@@ -32,6 +37,7 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Transforms/IPO.h"
#include <system_error>
#include <vector>
@@ -94,13 +100,6 @@ static cl::opt<int>
static codegen::RegisterCodeGenFlags CGF;
-static void initializeTargetInfo() {
- InitializeAllTargets();
- InitializeAllTargetMCs();
- InitializeAllAsmPrinters();
- InitializeAllAsmParsers();
-}
-
void writeOutput(ReducerWorkItem &M, StringRef Message) {
if (ReplaceInput) // In-place
OutputFilename = InputFilename.c_str();
@@ -116,6 +115,39 @@ void writeOutput(ReducerWorkItem &M, StringRef Message) {
errs() << Message << OutputFilename << "\n";
}
+void writeBitcode(ReducerWorkItem &M, llvm::raw_ostream &OutStream) {
+ if (M.LTOInfo && M.LTOInfo->IsThinLTO && M.LTOInfo->EnableSplitLTOUnit) {
+ legacy::PassManager PM;
+ PM.add(llvm::createWriteThinLTOBitcodePass(OutStream));
+ PM.run(*(M.M));
+ } else {
+ std::unique_ptr<ModuleSummaryIndex> Index;
+ if (M.LTOInfo && M.LTOInfo->HasSummary) {
+ ProfileSummaryInfo PSI(M);
+ Index = std::make_unique<ModuleSummaryIndex>(
+ buildModuleSummaryIndex(M, nullptr, &PSI));
+ }
+ WriteBitcodeToFile(M, OutStream, Index.get());
+ }
+}
+
+void readBitcode(ReducerWorkItem &M, MemoryBufferRef Data, LLVMContext &Ctx, const char *ToolName) {
+ Expected<BitcodeFileContents> IF = llvm::getBitcodeFileContents(Data);
+ if (!IF) {
+ WithColor::error(errs(), ToolName) << IF.takeError();
+ exit(1);
+ }
+ BitcodeModule BM = IF->Mods[0];
+ Expected<BitcodeLTOInfo> LI = BM.getLTOInfo();
+ Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(Ctx);
+ if (!LI || !MOrErr) {
+ WithColor::error(errs(), ToolName) << IF.takeError();
+ exit(1);
+ }
+ M.LTOInfo = std::make_unique<BitcodeLTOInfo>(*LI);
+ M.M = std::move(MOrErr.get());
+}
+
int main(int Argc, char **Argv) {
InitLLVM X(Argc, Argv);
@@ -135,9 +167,6 @@ int main(int Argc, char **Argv) {
return 0;
}
- if (ReduceModeMIR)
- initializeTargetInfo();
-
LLVMContext Context;
std::unique_ptr<TargetMachine> TM;
@@ -149,7 +178,7 @@ int main(int Argc, char **Argv) {
// Initialize test environment
TestRunner Tester(TestFilename, TestArguments, std::move(OriginalProgram),
- std::move(TM));
+ std::move(TM), Argv[0]);
// Try to reduce code
runDeltaPasses(Tester, MaxPassIterations);