Skip to content

Commit f21b247

Browse files
committed
[MLIR][mlir-link] Generate composite module
1 parent bcc180d commit f21b247

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

mlir/lib/Tools/mlir-link/MlirLinkMain.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "mlir/Tools/mlir-link/MlirLinkMain.h"
10+
#include "mlir/IR/Builders.h"
11+
#include "mlir/IR/BuiltinOps.h"
1012
#include "mlir/IR/Dialect.h"
13+
#include "mlir/IR/Location.h"
14+
#include "mlir/IR/OwningOpRef.h"
15+
#include "mlir/Support/FileUtilities.h"
1116
#include "llvm/Support/CommandLine.h"
1217
#include "llvm/Support/Error.h"
1318
#include "llvm/Support/InitLLVM.h"
19+
#include "llvm/Support/ToolOutputFile.h"
1420
#include "llvm/Support/WithColor.h"
1521

1622
using namespace mlir;
1723
using namespace llvm;
1824

25+
OwningOpRef<ModuleOp> makeCompositeModule(MLIRContext *context) {
26+
OpBuilder builder(context);
27+
ModuleOp op =
28+
builder.create<ModuleOp>(FileLineColLoc::get(context, "mlir-link", 0, 0));
29+
OwningOpRef<ModuleOp> composite(op);
30+
return composite;
31+
}
32+
1933
LogicalResult mlir::MlirLinkMain(int argc, char **argv,
2034
DialectRegistry &registry) {
2135
static cl::OptionCategory linkCategory("Link options");
@@ -28,13 +42,33 @@ LogicalResult mlir::MlirLinkMain(int argc, char **argv,
2842
"o", cl::desc("Override output filename"), cl::init("-"),
2943
cl::value_desc("filename"), cl::cat(linkCategory));
3044

31-
static ExitOnError ExitOnErr;
45+
static cl::opt<bool> verbose(
46+
"v", cl::desc("Print information about actions taken"),
47+
cl::cat(linkCategory));
48+
49+
static ExitOnError exitOnErr;
3250

3351
InitLLVM y(argc, argv);
34-
ExitOnErr.setBanner(std::string(argv[0]) + ": ");
52+
exitOnErr.setBanner(std::string(argv[0]) + ": ");
3553

3654
cl::HideUnrelatedOptions({&linkCategory, &getColorCategory()});
3755
cl::ParseCommandLineOptions(argc, argv, "mlir linker\n");
3856

57+
MLIRContext context(registry);
58+
auto composite = makeCompositeModule(&context);
59+
60+
std::string errorMessage;
61+
62+
auto output = openOutputFile(outputFilename, &errorMessage);
63+
if (!output) {
64+
errs() << errorMessage;
65+
return failure();
66+
}
67+
68+
if (verbose)
69+
errs() << "Writing linked module to '" << outputFilename << "'\n";
70+
71+
composite.get()->print(output->os());
72+
output->keep();
3973
return success();
4074
}

0 commit comments

Comments
 (0)