2015-08-21 10:48:20 +08:00
|
|
|
//===-- llvm-split: command line tool for testing module splitter ---------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-21 10:48:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This program can be used to test the llvm::SplitModule function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2015-08-21 10:48:20 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-03-29 05:37:02 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2015-08-21 10:48:20 +08:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Transforms/Utils/SplitModule.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
|
|
|
|
cl::init("-"), cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2),
|
|
|
|
cl::desc("Number of output files"));
|
|
|
|
|
2016-01-19 05:07:13 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
PreserveLocals("preserve-locals", cl::Prefix, cl::init(false),
|
|
|
|
cl::desc("Split without externalizing locals"));
|
|
|
|
|
2015-08-21 10:48:20 +08:00
|
|
|
int main(int argc, char **argv) {
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2015-08-21 10:48:20 +08:00
|
|
|
SMDiagnostic Err;
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n");
|
|
|
|
|
|
|
|
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
|
|
|
|
|
|
|
|
if (!M) {
|
|
|
|
Err.print(argv[0], errs());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned I = 0;
|
|
|
|
SplitModule(std::move(M), NumOutputs, [&](std::unique_ptr<Module> MPart) {
|
|
|
|
std::error_code EC;
|
2017-09-23 09:03:17 +08:00
|
|
|
std::unique_ptr<ToolOutputFile> Out(
|
2019-08-05 13:43:48 +08:00
|
|
|
new ToolOutputFile(OutputFilename + utostr(I++), EC, sys::fs::OF_None));
|
2015-08-21 10:48:20 +08:00
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-05-19 03:41:36 +08:00
|
|
|
if (verifyModule(*MPart, &errs())) {
|
|
|
|
errs() << "Broken module!\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*MPart, Out->os());
|
2015-08-21 10:48:20 +08:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
2016-01-19 05:07:13 +08:00
|
|
|
}, PreserveLocals);
|
2015-08-21 10:48:20 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|