Skip to content

Commit e7f8b45

Browse files
committed
Add an MLIR example of some mimimal example of MLIR setup
These may serve as example and showcase of the MLIR binary footprint. Right now a release build of these tools on a linux machine shows: - mlir-cat: 2MB This includes the Core IR, the textual parser/printer, the support for bytecode. - mlir-minimal-opt: 3MB This adds all the tooling for an mlir-opt tool: the pass infrastructure and all the instrumentation associated with it. - mlir-miminal-opt-canonicalize: 4.8MB This add the canonicalizer pass, which pulls in all the pattern/rewrite machinery, including the PDL compiler and intepreter. Differential Revision: https://reviews.llvm.org/D156218
1 parent b530eee commit e7f8b45

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed

mlir/examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(toy)
22
add_subdirectory(transform)
3+
add_subdirectory(minimal-opt)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
set(LLVM_LINK_COMPONENTS
2+
Support
3+
)
4+
5+
6+
set(LIBS
7+
MLIRParser
8+
MLIRSupport
9+
MLIRIR
10+
)
11+
12+
add_mlir_tool(mlir-cat
13+
mlir-cat.cpp
14+
PARTIAL_SOURCES_INTENDED
15+
16+
DEPENDS
17+
${LIBS}
18+
)
19+
target_link_libraries(mlir-cat PRIVATE ${LIBS})
20+
llvm_update_compile_flags(mlir-cat)
21+
mlir_check_all_link_libraries(mlir-cat)
22+
23+
list(APPEND LIBS
24+
MLIROptLib
25+
MLIRPass
26+
)
27+
add_mlir_tool(mlir-minimal-opt
28+
mlir-minimal-opt.cpp
29+
PARTIAL_SOURCES_INTENDED
30+
31+
DEPENDS
32+
${LIBS}
33+
)
34+
target_link_libraries(mlir-minimal-opt PRIVATE ${LIBS})
35+
llvm_update_compile_flags(mlir-minimal-opt)
36+
mlir_check_all_link_libraries(mlir-minimal-opt)
37+
38+
39+
list(APPEND LIBS
40+
MLIROptLib
41+
MLIRPass
42+
MLIRTransforms
43+
)
44+
add_mlir_tool(mlir-minimal-opt-canonicalize
45+
mlir-minimal-opt-canonicalize.cpp
46+
PARTIAL_SOURCES_INTENDED
47+
48+
DEPENDS
49+
${LIBS}
50+
)
51+
target_link_libraries(mlir-minimal-opt-canonicalize PRIVATE ${LIBS})
52+
llvm_update_compile_flags(mlir-minimal-opt-canonicalize)
53+
mlir_check_all_link_libraries(mlir-minimal-opt-canonicalize)
54+

mlir/examples/minimal-opt/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Minmal MLIR binaries
2+
3+
This folder contains example of minimal MLIR setup that can showcase the
4+
intended binary footprint of the framework.
5+
6+
- mlir-cat: ~2MB
7+
This includes the Core IR, the builtin dialect, the textual parser/printer,
8+
the support for bytecode serialization.
9+
- mlir-minimal-opt: ~3MB
10+
This adds all the tooling for an mlir-opt tool: the pass infrastructure
11+
and all the instrumentation associated with it.
12+
- mlir-miminal-opt-canonicalize: ~4.8MB
13+
This add the canonicalizer pass, which pulls in all the pattern/rewrite
14+
machinery, including the PDL compiler and intepreter.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===- mlir-cat.cpp ---------------------------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed 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/IR/MLIRContext.h"
10+
#include "mlir/Parser/Parser.h"
11+
#include "mlir/Support/FileUtilities.h"
12+
#include "mlir/Support/LLVM.h"
13+
#include "mlir/Support/LogicalResult.h"
14+
#include "llvm/Support/MemoryBuffer.h"
15+
#include "llvm/Support/SourceMgr.h"
16+
#include "llvm/Support/ToolOutputFile.h"
17+
#include "llvm/Support/raw_ostream.h"
18+
19+
using namespace mlir;
20+
21+
/// This example parse its input, either from a file or its standard input (in
22+
/// bytecode or textual assembly) and print it back.
23+
24+
int main(int argc, char **argv) {
25+
// Set up the input file.
26+
StringRef inputFile;
27+
if (argc <= 1) {
28+
llvm::errs() << "Reading from stdin...\n";
29+
inputFile = "-";
30+
} else {
31+
inputFile = argv[1];
32+
}
33+
std::string errorMessage;
34+
auto file = openInputFile(inputFile, &errorMessage);
35+
if (!file) {
36+
llvm::errs() << errorMessage << "\n";
37+
exit(1);
38+
}
39+
llvm::SourceMgr sourceMgr;
40+
sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc());
41+
42+
auto output = openOutputFile("-", &errorMessage);
43+
if (!output) {
44+
llvm::errs() << errorMessage << "\n";
45+
exit(1);
46+
}
47+
48+
DialectRegistry registry;
49+
MLIRContext context(registry, MLIRContext::Threading::DISABLED);
50+
context.allowUnregisteredDialects(true);
51+
OwningOpRef<Operation *> op = parseSourceFile(sourceMgr, &context);
52+
if (!op) {
53+
llvm::errs() << "Failed to parse input file";
54+
exit(1);
55+
}
56+
output->os() << *(op.get()) << "\n";
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===- mlir-minimal-opt-canonicalize.cpp ------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed 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-opt/MlirOptMain.h"
10+
#include "mlir/Transforms/Passes.h"
11+
12+
int main(int argc, char **argv) {
13+
// Register only the canonicalize pass
14+
// This pulls in the pattern rewrite engine as well as the whole PDL
15+
// compiler/intepreter.
16+
mlir::registerCanonicalizerPass();
17+
18+
mlir::DialectRegistry registry;
19+
return mlir::asMainReturnCode(mlir::MlirOptMain(
20+
argc, argv, "Minimal Standalone optimizer driver\n", registry));
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===- mlir-minimal-opt.cpp -------------------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed 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-opt/MlirOptMain.h"
10+
11+
/// This test includes the minimal amount of components for mlir-opt, that is
12+
/// the CoreIR, the printer/parser, the bytecode reader/writer, the
13+
/// passmanagement infrastructure and all the instrumentation.
14+
int main(int argc, char **argv) {
15+
mlir::DialectRegistry registry;
16+
return mlir::asMainReturnCode(mlir::MlirOptMain(
17+
argc, argv, "Minimal Standalone optimizer driver\n", registry));
18+
}

mlir/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ if(LLVM_BUILD_EXAMPLES)
143143
toyc-ch5
144144
transform-opt-ch2
145145
transform-opt-ch3
146+
mlir-minimal-opt
146147
)
147148
if(MLIR_ENABLE_EXECUTION_ENGINE)
148149
list(APPEND MLIR_TEST_DEPENDS

0 commit comments

Comments
 (0)