[Mlir-commits] [mlir] e7f8b45 - Add an MLIR example of some mimimal example of MLIR setup

Mehdi Amini llvmlistbot at llvm.org
Tue Jul 25 11:38:27 PDT 2023


Author: Mehdi Amini
Date: 2023-07-25T11:38:19-07:00
New Revision: e7f8b459532de54a8606c7d387ded7ccf5108cb5

URL: https://github.com/llvm/llvm-project/commit/e7f8b459532de54a8606c7d387ded7ccf5108cb5
DIFF: https://github.com/llvm/llvm-project/commit/e7f8b459532de54a8606c7d387ded7ccf5108cb5.diff

LOG: 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

Added: 
    mlir/examples/minimal-opt/CMakeLists.txt
    mlir/examples/minimal-opt/README.md
    mlir/examples/minimal-opt/mlir-cat.cpp
    mlir/examples/minimal-opt/mlir-minimal-opt-canonicalize.cpp
    mlir/examples/minimal-opt/mlir-minimal-opt.cpp

Modified: 
    mlir/examples/CMakeLists.txt
    mlir/test/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/mlir/examples/CMakeLists.txt b/mlir/examples/CMakeLists.txt
index bdbba3ead9abf5..d256bf1a5cbb13 100644
--- 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
index 00000000000000..dea9bd494c8a5b
--- /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
index 00000000000000..d3f2fa151791f5
--- /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 builtin dialect, the textual parser/printer,
+  the support for bytecode serialization.
+- 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
index 00000000000000..518654c0ab1a05
--- /dev/null
+++ b/mlir/examples/minimal-opt/mlir-cat.cpp
@@ -0,0 +1,57 @@
+//===- mlir-cat.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<Operation *> 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
index 00000000000000..9aead76dea7920
--- /dev/null
+++ b/mlir/examples/minimal-opt/mlir-minimal-opt-canonicalize.cpp
@@ -0,0 +1,21 @@
+//===- mlir-minimal-opt-canonicalize.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
index 00000000000000..8da2dd3c58b023
--- /dev/null
+++ b/mlir/examples/minimal-opt/mlir-minimal-opt.cpp
@@ -0,0 +1,18 @@
+//===- mlir-minimal-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
index c21984beb05ede..0a954c702bf5ed 100644
--- a/mlir/test/CMakeLists.txt
+++ b/mlir/test/CMakeLists.txt
@@ -143,6 +143,7 @@ if(LLVM_BUILD_EXAMPLES)
     toyc-ch5
     transform-opt-ch2
     transform-opt-ch3
+    mlir-minimal-opt
     )
   if(MLIR_ENABLE_EXECUTION_ENGINE)
     list(APPEND MLIR_TEST_DEPENDS


        


More information about the Mlir-commits mailing list