MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
main.cxx
Go to the documentation of this file.
1/*
2 * Copyright 2025 Yeong-won Seo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pch.h"
21
22namespace {
23 auto LoadModule(mlir::MLIRContext &context, const std::string &filename) -> std::unique_ptr<mlir::ModuleOp> {
24
25 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr = llvm::MemoryBuffer::getFile(filename);
26 if (const std::error_code ec = fileOrErr.getError()) {
27 llvm::errs() << "error: couldn't open input file: " << ec.message() << "\n";
28 return nullptr;
29 }
30
31 llvm::SourceMgr sourceManager;
32 sourceManager.AddNewSourceBuffer(std::move(*fileOrErr), llvm::SMLoc());
33 mlir::OwningOpRef<mlir::ModuleOp> owningModule = mlir::parseSourceFile<mlir::ModuleOp>(sourceManager, &context);
34 if (!owningModule) {
35 llvm::errs() << "error: couldn't load file " << filename << "\n";
36 return nullptr;
37 }
38
39 return std::make_unique<mlir::ModuleOp>(owningModule.release());
40 }
41} // namespace
42
43auto main(const int argc, char **argv) -> int {
44 mlir::MLIRContext context;
45
46 mlir::DialectRegistry registry;
47 registry.insert<
49 >();
50 context.appendDialectRegistry(registry);
51
52 if (argc <= 2) {
53 llvm::errs() << "usage: mllif-mlir <output> <file>...\n";
54 return 1;
55 }
56 const std::string output = argv[1];
57
58 mllif::mlir::Tree symbols;
59
60 for (auto i = 2; i < argc; ++i) {
61 const std::shared_ptr module = LoadModule(context, std::string(argv[i]));
62
63 module->walk([&module, &symbols](mlir::Operation *op, const mlir::WalkStage &stage) {
64 if (!stage.isAfterAllRegions()) {
65 return;
66 }
67
68 for (const auto &adapter : mllif::mlir::Adapters) {
69 adapter->handle(symbols, module, op);
70 }
71 });
72 }
73
74 std::error_code error;
75 llvm::raw_fd_ostream os(output, error);
76 if (error.value()) {
77 llvm::errs() << "error: couldn't open file '" << output << "': " << error.message() << "\n";
78 return 1;
79 }
80
81 symbols.root().print(os);
82
83 return 0;
84}
auto main() -> int
Definition main.cxx:33
A tree struct for symbol tree. It's just a simple wrapper for root node.
Definition Tree.h:147
std::vector< std::shared_ptr< Adapter > > Adapters
Definition Adapter.cxx:21