44#include " llvm/IRReader/IRReader.h"
55#include " llvm/ModuleSplitter/ModuleSplitter.h"
66#include " llvm/Support/CommandLine.h"
7+ #include " llvm/Support/Error.h"
8+ #include " llvm/Support/FileSystem.h"
79#include " llvm/Support/SourceMgr.h"
810#include " llvm/Support/ToolOutputFile.h"
911#include " llvm/Support/raw_ostream.h"
1214
1315using namespace llvm ;
1416
17+ std::string InputFilename{" -" };
18+ std::string OutputPrefix{" -" };
19+ bool PerFunctionSplit = false ;
20+
21+ llvm::cl::OptionCategory Cat{" Common command line options" };
22+
23+ cl::opt<std::string, true > InputFilenameOpt{
24+ llvm::cl::Positional, llvm::cl::desc (" <input file>" ),
25+ llvm::cl::location (InputFilename), llvm::cl::cat (Cat)};
26+
27+ cl::opt<std::string, true > OutputPrefixOpt{
28+ " output-prefix" , llvm::cl::desc (" output prefix" ),
29+ llvm::cl::value_desc (" output prefix" ), llvm::cl::location (OutputPrefix),
30+ llvm::cl::cat (Cat)};
31+
32+ cl::opt<bool , true > PerFunctionSplitOpt{
33+ " per-func" , llvm::cl::desc (" split each function into separate modules" ),
34+ llvm::cl::value_desc (" split each function into separate modules" ),
35+ llvm::cl::location (PerFunctionSplit), llvm::cl::cat (Cat)};
36+
1537// ===----------------------------------------------------------------------===//
1638// Module Splitter
1739// ===----------------------------------------------------------------------===//
@@ -32,70 +54,79 @@ int main(int argc, char **argv) {
3254 // Enable command line options for various MLIR internals.
3355 llvm::cl::ParseCommandLineOptions (argc, argv);
3456
35- LLVMModuleAndContext Module;
57+ LLVMModuleAndContext M;
58+ return 0 ;
59+ Expected<bool > Err =
60+ M.create ([&](LLVMContext &Ctx) -> Expected<std::unique_ptr<Module>> {
61+ if (std::unique_ptr<Module> m = readModule (Ctx, InputFilename))
62+ return m;
63+ return make_error<StringError>(" could not load LLVM file" ,
64+ inconvertibleErrorCode ());
65+ });
66+
67+ if (Err) {
68+ llvm::errs () << toString (Err.takeError ()) << " \n " ;
69+ return -1 ;
70+ }
71+
72+ std::unique_ptr<llvm::ToolOutputFile> Output = nullptr ;
73+ if (OutputPrefix == " -" ) {
74+ std::error_code Error;
75+ Output = std::make_unique<llvm::ToolOutputFile>(OutputPrefix, Error,
76+ llvm::sys::fs::OF_None);
77+ if (Error) {
78+ llvm::errs () << " Cannot open output file: '" + OutputPrefix +
79+ " ':" + Error.message ()
80+ << " \n " ;
81+ return -1 ;
82+ }
83+ }
84+
85+ auto OutputLambda =
86+ [&](llvm::unique_function<LLVMModuleAndContext ()> ProduceModule,
87+ std::optional<int64_t > Idx, unsigned NumFunctionsBase) mutable {
88+ LLVMModuleAndContext SubModule = ProduceModule ();
89+ if (OutputPrefix == " -" ) {
90+ Output->os () << " ##############################################\n " ;
91+ if (Idx)
92+ Output->os () << " # [LLVM Module Split: submodule " << *Idx << " ]\n " ;
93+ else
94+ Output->os () << " # [LLVM Module Split: main module]\n " ;
95+ Output->os () << " ##############################################\n " ;
96+ Output->os () << *SubModule;
97+ Output->os () << " \n " ;
98+ } else {
99+ std::string OutPath;
100+ if (!Idx) {
101+ OutPath = OutputPrefix + " .ll" ;
102+ } else {
103+ OutPath = (OutputPrefix + " ." + Twine (*Idx) + " .ll" ).str ();
104+ }
105+
106+ std::error_code EC;
107+ raw_fd_ostream OutFile (OutPath.c_str (), EC, llvm::sys::fs::OF_None);
108+
109+ if (OutFile.error ()) {
110+ llvm::errs () << " Cannot open output file: '" + OutPath + " ."
111+ << " \n " ;
112+ exit (-1 );
113+ }
114+
115+ OutFile << *SubModule;
116+ OutFile.close ();
117+ llvm::outs () << " Write llvm module to " << OutPath << " \n " ;
118+ }
119+ };
120+
121+ llvm::StringMap<llvm::GlobalValue::LinkageTypes> SymbolLinkageTypes;
122+ if (PerFunctionSplit)
123+ splitPerFunction (std::move (M), OutputLambda);
124+ else {
125+ SmallVector<llvm::Function> Anchors;
126+ splitPerAnchored (std::move (M), OutputLambda, Anchors);
127+ }
128+
129+ if (Output)
130+ Output->keep ();
36131 return 0 ;
37- // ErrorOrSuccess err = module.create(
38- // [&](LLVMContext &ctx) -> M::ErrorOr<std::unique_ptr<Module>> {
39- // if (std::unique_ptr<Module> module =
40- // readModule(ctx, clOptions.inputFilename))
41- // return module;
42- // return M::Error("could not load LLVM file");
43- // });
44- // if (err) {
45- // llvm::errs() << err.getError() << "\n";
46- // return -1;
47- // }
48-
49- // std::unique_ptr<llvm::ToolOutputFile> output = nullptr;
50- // if (clOptions.outputPrefix == "-") {
51- // std::error_code error;
52- // output = std::make_unique<llvm::ToolOutputFile>(
53- // clOptions.outputPrefix, error, llvm::sys::fs::OF_None);
54- // if (error)
55- // exit(clOptions.options.reportError("Cannot open output file: '" +
56- // clOptions.outputPrefix +
57- // "':" + error.message()));
58- // }
59-
60- // auto outputLambda =
61- // [&](llvm::unique_function<LLVMModuleAndContext()> produceModule,
62- // std::optional<int64_t> idx, unsigned numFunctionsBase) mutable {
63- // LLVMModuleAndContext subModule = produceModule();
64- // if (clOptions.outputPrefix == "-") {
65- // output->os() << "##############################################\n";
66- // if (idx)
67- // output->os() << "# [LLVM Module Split: submodule " << *idx << "]\n";
68- // else
69- // output->os() << "# [LLVM Module Split: main module]\n";
70- // output->os() << "##############################################\n";
71- // output->os() << *subModule;
72- // output->os() << "\n";
73- // } else {
74- // std::string outPath;
75- // if (!idx) {
76- // outPath = clOptions.outputPrefix + ".ll";
77- // } else {
78- // outPath =
79- // (clOptions.outputPrefix + "." + Twine(*idx) + ".ll").str();
80- // }
81- // auto outFile = mlir::openOutputFile(outPath);
82- // if (!outFile) {
83- // exit(clOptions.options.reportError("Cannot open output file: '" +
84- // outPath + "."));
85- // }
86- // outFile->os() << *subModule;
87- // outFile->keep();
88- // llvm::outs() << "Write llvm module to " << outPath << "\n";
89- // }
90- // };
91-
92- // llvm::StringMap<llvm::GlobalValue::LinkageTypes> symbolLinkageTypes;
93- // if (clOptions.perFunctionSplit)
94- // splitPerFunction(std::move(module), outputLambda, symbolLinkageTypes);
95- // else
96- // splitPerExported(std::move(module), outputLambda);
97-
98- // if (output)
99- // output->keep();
100- // return 0;
101132}
0 commit comments