diff --git a/mlir/examples/CMakeLists.txt b/mlir/examples/CMakeLists.txt --- a/mlir/examples/CMakeLists.txt +++ b/mlir/examples/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(toy) add_subdirectory(transform) +add_subdirectory(minimal-opt) diff --git a/mlir/examples/minimal-opt/CMakeLists.txt b/mlir/examples/minimal-opt/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/mlir/examples/minimal-opt/CMakeLists.txt @@ -0,0 +1,54 @@ +set(LLVM_LINK_COMPONENTS + Support + ) + + +set(LIBS + MLIRParser + MLIRSupport + MLIRIR +) + +add_mlir_tool(mlir-cat + mlir-cat.cpp + PARTIAL_SOURCES_INTENDED + + DEPENDS + ${LIBS} + ) +target_link_libraries(mlir-cat PRIVATE ${LIBS}) +llvm_update_compile_flags(mlir-cat) +mlir_check_all_link_libraries(mlir-cat) + +list(APPEND LIBS + MLIROptLib + MLIRPass +) +add_mlir_tool(mlir-minimal-opt + mlir-minimal-opt.cpp + PARTIAL_SOURCES_INTENDED + + DEPENDS + ${LIBS} + ) +target_link_libraries(mlir-minimal-opt PRIVATE ${LIBS}) +llvm_update_compile_flags(mlir-minimal-opt) +mlir_check_all_link_libraries(mlir-minimal-opt) + + +list(APPEND LIBS + MLIROptLib + MLIRPass + MLIRTransforms +) +add_mlir_tool(mlir-minimal-opt-canonicalize + mlir-minimal-opt-canonicalize.cpp + PARTIAL_SOURCES_INTENDED + + DEPENDS + ${LIBS} + ) +target_link_libraries(mlir-minimal-opt-canonicalize PRIVATE ${LIBS}) +llvm_update_compile_flags(mlir-minimal-opt-canonicalize) +mlir_check_all_link_libraries(mlir-minimal-opt-canonicalize) + diff --git a/mlir/examples/minimal-opt/README.md b/mlir/examples/minimal-opt/README.md new file mode 100644 --- /dev/null +++ b/mlir/examples/minimal-opt/README.md @@ -0,0 +1,14 @@ +# Minmal MLIR binaries + +This folder contains example of minimal MLIR setup that can showcase the +intended binary footprint of the framework. + +- 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. \ No newline at end of file diff --git a/mlir/examples/minimal-opt/mlir-cat.cpp b/mlir/examples/minimal-opt/mlir-cat.cpp new file mode 100644 --- /dev/null +++ b/mlir/examples/minimal-opt/mlir-cat.cpp @@ -0,0 +1,57 @@ +//===- standalone-opt.cpp ---------------------------------------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "mlir/IR/MLIRContext.h" +#include "mlir/Parser/Parser.h" +#include "mlir/Support/FileUtilities.h" +#include "mlir/Support/LLVM.h" +#include "mlir/Support/LogicalResult.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/raw_ostream.h" + +using namespace mlir; + +/// This example parse its input, either from a file or its standard input (in +/// bytecode or textual assembly) and print it back. + +int main(int argc, char **argv) { + // Set up the input file. + StringRef inputFile; + if (argc <= 1) { + llvm::errs() << "Reading from stdin...\n"; + inputFile = "-"; + } else { + inputFile = argv[1]; + } + std::string errorMessage; + auto file = openInputFile(inputFile, &errorMessage); + if (!file) { + llvm::errs() << errorMessage << "\n"; + exit(1); + } + llvm::SourceMgr sourceMgr; + sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc()); + + auto output = openOutputFile("-", &errorMessage); + if (!output) { + llvm::errs() << errorMessage << "\n"; + exit(1); + } + + DialectRegistry registry; + MLIRContext context(registry, MLIRContext::Threading::DISABLED); + context.allowUnregisteredDialects(true); + OwningOpRef op = parseSourceFile(sourceMgr, &context); + if (!op) { + llvm::errs() << "Failed to parse input file"; + exit(1); + } + output->os() << *(op.get()) << "\n"; +} diff --git a/mlir/examples/minimal-opt/mlir-minimal-opt-canonicalize.cpp b/mlir/examples/minimal-opt/mlir-minimal-opt-canonicalize.cpp new file mode 100644 --- /dev/null +++ b/mlir/examples/minimal-opt/mlir-minimal-opt-canonicalize.cpp @@ -0,0 +1,21 @@ +//===- standalone-opt.cpp ---------------------------------------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "mlir/Tools/mlir-opt/MlirOptMain.h" +#include "mlir/Transforms/Passes.h" + +int main(int argc, char **argv) { + // Register only the canonicalize pass + // This pulls in the pattern rewrite engine as well as the whole PDL + // compiler/intepreter. + mlir::registerCanonicalizerPass(); + + mlir::DialectRegistry registry; + return mlir::asMainReturnCode(mlir::MlirOptMain( + argc, argv, "Minimal Standalone optimizer driver\n", registry)); +} diff --git a/mlir/examples/minimal-opt/mlir-minimal-opt.cpp b/mlir/examples/minimal-opt/mlir-minimal-opt.cpp new file mode 100644 --- /dev/null +++ b/mlir/examples/minimal-opt/mlir-minimal-opt.cpp @@ -0,0 +1,18 @@ +//===- standalone-opt.cpp ---------------------------------------*- C++ -*-===// +// +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "mlir/Tools/mlir-opt/MlirOptMain.h" + +/// This test includes the minimal amount of components for mlir-opt, that is +/// the CoreIR, the printer/parser, the bytecode reader/writer, the +/// passmanagement infrastructure and all the instrumentation. +int main(int argc, char **argv) { + mlir::DialectRegistry registry; + return mlir::asMainReturnCode(mlir::MlirOptMain( + argc, argv, "Minimal Standalone optimizer driver\n", registry)); +} diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -143,6 +143,7 @@ toyc-ch5 transform-opt-ch2 transform-opt-ch3 + mlir-minimal-opt ) if(MLIR_ENABLE_EXECUTION_ENGINE) list(APPEND MLIR_TEST_DEPENDS