Skip to content

Commit

Permalink
[MLIR][mlir-link] Generate composite module
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Jan 22, 2025
1 parent bcc180d commit f21b247
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions mlir/lib/Tools/mlir-link/MlirLinkMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@
//===----------------------------------------------------------------------===//

#include "mlir/Tools/mlir-link/MlirLinkMain.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/OwningOpRef.h"
#include "mlir/Support/FileUtilities.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/WithColor.h"

using namespace mlir;
using namespace llvm;

OwningOpRef<ModuleOp> makeCompositeModule(MLIRContext *context) {
OpBuilder builder(context);
ModuleOp op =
builder.create<ModuleOp>(FileLineColLoc::get(context, "mlir-link", 0, 0));
OwningOpRef<ModuleOp> composite(op);
return composite;
}

LogicalResult mlir::MlirLinkMain(int argc, char **argv,
DialectRegistry &registry) {
static cl::OptionCategory linkCategory("Link options");
Expand All @@ -28,13 +42,33 @@ LogicalResult mlir::MlirLinkMain(int argc, char **argv,
"o", cl::desc("Override output filename"), cl::init("-"),
cl::value_desc("filename"), cl::cat(linkCategory));

static ExitOnError ExitOnErr;
static cl::opt<bool> verbose(
"v", cl::desc("Print information about actions taken"),
cl::cat(linkCategory));

static ExitOnError exitOnErr;

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

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

MLIRContext context(registry);
auto composite = makeCompositeModule(&context);

std::string errorMessage;

auto output = openOutputFile(outputFilename, &errorMessage);
if (!output) {
errs() << errorMessage;
return failure();
}

if (verbose)
errs() << "Writing linked module to '" << outputFilename << "'\n";

composite.get()->print(output->os());
output->keep();
return success();
}

0 comments on commit f21b247

Please sign in to comment.