Skip to content

Commit a8d9173

Browse files
committed
compiles.
1 parent fb12509 commit a8d9173

File tree

4 files changed

+105
-75
lines changed

4 files changed

+105
-75
lines changed

llvm/include/llvm/ModuleSplitter/ModuleSplitter.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ forwardModule(LLVMModuleAndContext &&Module) {
6464
/// Support for splitting an LLVM module into multiple parts using anchored
6565
/// functions (e.g. exported functions), and pull in all dependency on the
6666
// call stack into one module.
67-
void splitPerAnchored(LLVMModuleAndContext Module,
68-
LLVMSplitProcessFn ProcessFn,
69-
llvm::SmallVectorImpl<llvm::Function>& Anchors);
67+
void splitPerAnchored(LLVMModuleAndContext Module, LLVMSplitProcessFn ProcessFn,
68+
llvm::SmallVectorImpl<llvm::Function> &Anchors);
7069

7170
/// Support for splitting an LLVM module into multiple parts with each part
7271
/// contains only one function.
73-
void splitPerFunction(
74-
LLVMModuleAndContext Module, LLVMSplitProcessFn ProcessFn);
72+
void splitPerFunction(LLVMModuleAndContext Module,
73+
LLVMSplitProcessFn ProcessFn);
7574

7675
} // namespace llvm
7776

llvm/lib/ModuleSplitter/ModuleSplitter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ static LLVMModuleAndContext readAndMaterializeDependencies(
228228
SmallVector<unsigned> SortIndices =
229229
llvm::to_vector(llvm::make_second_range(Set));
230230
llvm::sort(SortIndices, std::less<unsigned>());
231-
auto* IdxIt = SortIndices.begin();
232-
auto* IdxEnd = SortIndices.end();
231+
auto *IdxIt = SortIndices.begin();
232+
auto *IdxEnd = SortIndices.end();
233233

234234
// The global value indices go from globals, functions, then aliases. This
235235
// mirrors the order in which global values are deleted by LLVM's GlobalDCE.
@@ -354,8 +354,7 @@ void LLVMModuleSplitterImpl::split(
354354
// If this value depends on another value that is going to be split, we
355355
// don't want to duplicate the symbol. Keep all the users together.
356356
if (It != Value) {
357-
if (auto* DepIt = TransDeps.find(It);
358-
DepIt != TransDeps.end()) {
357+
if (auto *DepIt = TransDeps.find(It); DepIt != TransDeps.end()) {
359358
auto &Users = SplitAnchorUsers[It];
360359
Users.insert(&Deps);
361360
// Make sure to include the other value in its own user list.
@@ -493,7 +492,7 @@ namespace {
493492
class LLVMModulePerFunctionSplitterImpl {
494493
public:
495494
LLVMModulePerFunctionSplitterImpl(LLVMModuleAndContext Module)
496-
: mainModule(std::move(Module)) {}
495+
: MainModule(std::move(Module)) {}
497496

498497
/// Split the LLVM module into multiple modules using the provided process
499498
/// function.

llvm/tools/llvm-module-splitter/llvm-module-splitter.cpp

Lines changed: 96 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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"
@@ -12,6 +14,26 @@
1214

1315
using 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
}

utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,7 @@ cc_library(
21002100
":Core",
21012101
":IRReader",
21022102
":Support",
2103+
":TransformUtils",
21032104
],
21042105
)
21052106

0 commit comments

Comments
 (0)