Skip to content

Commit bcc180d

Browse files
committed
[MLIR][mlir-link] Add mlir-link tool main entry-point
This commit introduces mlir-link tool suited for mlir module linking. Followup commits will add its logic and more options. The structure of this tool is similar to mlir-opt and mlir-translate, i.e. the implementation is structured as a library that users can call into to implement entry points that contain the dialects/passes that they are interested in. Note: This tool is inspired by llvm-link, copying most of its logic to create a generalized solution using MLIR interfaces. This approach was chosen as an effective initial path, with the option to unify it in the future if minimal divergence from llvm-link occurs.
1 parent c695e79 commit bcc180d

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- MlirLinkMain.h - MLIR Link main --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Main entry function for mlir-link for when built as standalone binary.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TOOLS_MLIR_LINK_MLIRLINKMAIN_H
14+
#define MLIR_TOOLS_MLIR_LINK_MLIRLINKMAIN_H
15+
16+
namespace llvm {
17+
struct LogicalResult;
18+
} // namespace llvm
19+
20+
namespace mlir {
21+
class DialectRegistry;
22+
23+
/// Implementation for tools like `mlir-link`.
24+
/// - registry should contain all the dialects that can be parsed in source IR
25+
/// passed to the linker.
26+
llvm::LogicalResult MlirLinkMain(int argc, char **argv,
27+
DialectRegistry &registry);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TOOLS_MLIR_LINK_MLIRLINKMAIN_H

mlir/lib/Tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_subdirectory(lsp-server-support)
2+
add_subdirectory(mlir-link)
23
add_subdirectory(mlir-lsp-server)
34
add_subdirectory(mlir-opt)
45
add_subdirectory(mlir-pdll-lsp-server)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_mlir_library(MLIRLinkLib
2+
MlirLinkMain.cpp
3+
4+
ADDITIONAL_HEADER_DIRS
5+
${MLIR_MAIN_INCLUDE_DIR}/mlir/Tools/mlir-link
6+
7+
LINK_LIBS PUBLIC
8+
MLIRFunctionInterfaces
9+
MLIRIR
10+
MLIRParser
11+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- MlirLinkMain.cpp - MLIR Link main ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir/Tools/mlir-link/MlirLinkMain.h"
10+
#include "mlir/IR/Dialect.h"
11+
#include "llvm/Support/CommandLine.h"
12+
#include "llvm/Support/Error.h"
13+
#include "llvm/Support/InitLLVM.h"
14+
#include "llvm/Support/WithColor.h"
15+
16+
using namespace mlir;
17+
using namespace llvm;
18+
19+
LogicalResult mlir::MlirLinkMain(int argc, char **argv,
20+
DialectRegistry &registry) {
21+
static cl::OptionCategory linkCategory("Link options");
22+
23+
static cl::list<std::string> inputFilenames(cl::Positional, cl::OneOrMore,
24+
cl::desc("<input mlir files>"),
25+
cl::cat(linkCategory));
26+
27+
static cl::opt<std::string> outputFilename(
28+
"o", cl::desc("Override output filename"), cl::init("-"),
29+
cl::value_desc("filename"), cl::cat(linkCategory));
30+
31+
static ExitOnError ExitOnErr;
32+
33+
InitLLVM y(argc, argv);
34+
ExitOnErr.setBanner(std::string(argv[0]) + ": ");
35+
36+
cl::HideUnrelatedOptions({&linkCategory, &getColorCategory()});
37+
cl::ParseCommandLineOptions(argc, argv, "mlir linker\n");
38+
39+
return success();
40+
}

mlir/tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(mlir-link)
12
add_subdirectory(mlir-lsp-server)
23
add_subdirectory(mlir-opt)
34
add_subdirectory(mlir-parser-fuzzer)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
3+
set(LLVM_LINK_COMPONENTS
4+
Core
5+
Support
6+
AsmParser
7+
)
8+
9+
if(MLIR_INCLUDE_TESTS)
10+
set(test_libs
11+
)
12+
endif()
13+
14+
set(LIBS
15+
${conversion_libs}
16+
${dialect_libs}
17+
${test_libs}
18+
MLIRDialect
19+
MLIRFuncAllExtensions
20+
MLIRIR
21+
MLIRLinkLib
22+
MLIRSupport
23+
MLIRTensorAllExtensions
24+
)
25+
26+
add_mlir_tool(mlir-link
27+
mlir-link.cpp
28+
29+
DEPENDS
30+
${LIBS}
31+
)
32+
33+
target_link_libraries(mlir-link PRIVATE ${LIBS})
34+
llvm_update_compile_flags(mlir-link)
35+
36+
mlir_check_all_link_libraries(mlir-link)
37+
export_executable_symbols_for_plugins(mlir-link)

mlir/tools/mlir-link/mlir-link.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===- mlir-link.cpp - MLIR linker ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This utility may be invoked in the following manner:
10+
// mlir-link a.mlir b.mlir c.mlir -o x.mlir
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "mlir/IR/Dialect.h"
15+
#include "mlir/IR/MLIRContext.h"
16+
#include "mlir/InitAllDialects.h"
17+
#include "mlir/InitAllExtensions.h"
18+
#include "mlir/Tools/mlir-link/MlirLinkMain.h"
19+
20+
using namespace mlir;
21+
22+
int main(int argc, char **argv) {
23+
DialectRegistry registry;
24+
registerAllDialects(registry);
25+
registerAllExtensions(registry);
26+
27+
return failed(MlirLinkMain(argc, argv, registry));
28+
}

0 commit comments

Comments
 (0)