[Mlir-commits] [mlir] 3bff62d - [mlir] Extend standalone example by standalone-translate

Stephen Neuendorffer llvmlistbot at llvm.org
Thu May 28 14:08:18 PDT 2020


Author: Marius Brehler
Date: 2020-05-28T14:07:55-07:00
New Revision: 3bff62d45f83ab2c480d82809cd91a32b4a6553d

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

LOG: [mlir] Extend standalone example by standalone-translate

Extend the standalone by standalone-translate, based on mlir-translate.

Differential Revision: https://reviews.llvm.org/D80737

Added: 
    mlir/examples/standalone/standalone-translate/CMakeLists.txt
    mlir/examples/standalone/standalone-translate/standalone-translate.cpp
    mlir/examples/standalone/test/Standalone/standalone-translate.mlir

Modified: 
    mlir/examples/standalone/CMakeLists.txt
    mlir/examples/standalone/README.md
    mlir/examples/standalone/test/CMakeLists.txt
    mlir/examples/standalone/test/lit.cfg.py
    mlir/test/Examples/standalone/test.toy

Removed: 
    


################################################################################
diff  --git a/mlir/examples/standalone/CMakeLists.txt b/mlir/examples/standalone/CMakeLists.txt
index fe309b501963..9f30f70f949f 100644
--- a/mlir/examples/standalone/CMakeLists.txt
+++ b/mlir/examples/standalone/CMakeLists.txt
@@ -44,3 +44,4 @@ add_subdirectory(include)
 add_subdirectory(lib)
 add_subdirectory(test)
 add_subdirectory(standalone-opt)
+add_subdirectory(standalone-translate)

diff  --git a/mlir/examples/standalone/README.md b/mlir/examples/standalone/README.md
index dd2fad7a5d81..b1ca6275e598 100644
--- a/mlir/examples/standalone/README.md
+++ b/mlir/examples/standalone/README.md
@@ -8,7 +8,7 @@ This setup assumes that you have built LLVM and MLIR in `$BUILD_DIR` and install
 ```sh
 mkdir build && cd build
 cmake -G Ninja .. -DMLIR_DIR=$PREFIX/lib/cmake/mlir -DLLVM_EXTERNAL_LIT=$BUILD_DIR/bin/llvm-lit
-cmake --build . --target check-standalone-opt
+cmake --build . --target check-standalone
 ```
 To build the documentation from the TableGen description of the dialect operations, run
 ```sh

diff  --git a/mlir/examples/standalone/standalone-translate/CMakeLists.txt b/mlir/examples/standalone/standalone-translate/CMakeLists.txt
new file mode 100644
index 000000000000..137f7947cfac
--- /dev/null
+++ b/mlir/examples/standalone/standalone-translate/CMakeLists.txt
@@ -0,0 +1,24 @@
+set(LLVM_LINK_COMPONENTS
+  Support
+  )
+
+get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
+get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
+
+add_llvm_executable(standalone-translate
+  standalone-translate.cpp
+  )
+llvm_update_compile_flags(standalone-translate)
+target_link_libraries(standalone-translate
+  PRIVATE
+  ${dialect_libs}
+  ${translation_libs}
+  MLIRIR
+  MLIRParser
+  MLIRPass
+  MLIRSPIRV
+  MLIRTranslation
+  MLIRSupport
+  )
+
+mlir_check_link_libraries(standalone-translate)

diff  --git a/mlir/examples/standalone/standalone-translate/standalone-translate.cpp b/mlir/examples/standalone/standalone-translate/standalone-translate.cpp
new file mode 100644
index 000000000000..c81e8dc14bc2
--- /dev/null
+++ b/mlir/examples/standalone/standalone-translate/standalone-translate.cpp
@@ -0,0 +1,114 @@
+//===- standalone-translate.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
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a command line utility that translates a file from/to MLIR using one
+// of the registered translations.
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/IR/AsmState.h"
+#include "mlir/IR/Diagnostics.h"
+#include "mlir/IR/MLIRContext.h"
+#include "mlir/InitAllDialects.h"
+#include "mlir/InitAllTranslations.h"
+#include "mlir/Support/FileUtilities.h"
+#include "mlir/Support/LogicalResult.h"
+#include "mlir/Support/ToolUtilities.h"
+#include "mlir/Translation.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/ToolOutputFile.h"
+
+#include "Standalone/StandaloneDialect.h"
+
+static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
+                                                llvm::cl::desc("<input file>"),
+                                                llvm::cl::init("-"));
+
+static llvm::cl::opt<std::string>
+    outputFilename("o", llvm::cl::desc("Output filename"),
+                   llvm::cl::value_desc("filename"), llvm::cl::init("-"));
+
+static llvm::cl::opt<bool>
+    splitInputFile("split-input-file",
+                   llvm::cl::desc("Split the input file into pieces and "
+                                  "process each chunk independently"),
+                   llvm::cl::init(false));
+
+static llvm::cl::opt<bool> verifyDiagnostics(
+    "verify-diagnostics",
+    llvm::cl::desc("Check that emitted diagnostics match "
+                   "expected-* lines on the corresponding line"),
+    llvm::cl::init(false));
+
+int main(int argc, char **argv) {
+  mlir::registerAllDialects();
+  mlir::registerAllTranslations();
+
+  mlir::registerDialect<mlir::standalone::StandaloneDialect>();
+  // TODO: Register standalone translations here.
+
+  llvm::InitLLVM y(argc, argv);
+
+  // Add flags for all the registered translations.
+  llvm::cl::opt<const mlir::TranslateFunction *, false, mlir::TranslationParser>
+      translationRequested("", llvm::cl::desc("Translation to perform"),
+                           llvm::cl::Required);
+  mlir::registerAsmPrinterCLOptions();
+  mlir::registerMLIRContextCLOptions();
+  llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR translation driver\n");
+
+  std::string errorMessage;
+  auto input = mlir::openInputFile(inputFilename, &errorMessage);
+  if (!input) {
+    llvm::errs() << errorMessage << "\n";
+    return 1;
+  }
+
+  auto output = mlir::openOutputFile(outputFilename, &errorMessage);
+  if (!output) {
+    llvm::errs() << errorMessage << "\n";
+    return 1;
+  }
+
+  // Processes the memory buffer with a new MLIRContext.
+  auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
+                           llvm::raw_ostream &os) {
+    mlir::MLIRContext context;
+    context.allowUnregisteredDialects();
+    context.printOpOnDiagnostic(!verifyDiagnostics);
+    llvm::SourceMgr sourceMgr;
+    sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), llvm::SMLoc());
+
+    if (!verifyDiagnostics) {
+      mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
+      return (*translationRequested)(sourceMgr, os, &context);
+    }
+
+    // In the diagnostic verification flow, we ignore whether the translation
+    // failed (in most cases, it is expected to fail). Instead, we check if the
+    // diagnostics were produced as expected.
+    mlir::SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr,
+                                                              &context);
+    (*translationRequested)(sourceMgr, os, &context);
+    return sourceMgrHandler.verify();
+  };
+
+  if (splitInputFile) {
+    if (failed(mlir::splitAndProcessBuffer(std::move(input), processBuffer,
+                                           output->os())))
+      return 1;
+  } else {
+    if (failed(processBuffer(std::move(input), output->os())))
+      return 1;
+  }
+
+  output->keep();
+  return 0;
+}

diff  --git a/mlir/examples/standalone/test/CMakeLists.txt b/mlir/examples/standalone/test/CMakeLists.txt
index 8f31f9f1dfaa..29da2b87ba4f 100644
--- a/mlir/examples/standalone/test/CMakeLists.txt
+++ b/mlir/examples/standalone/test/CMakeLists.txt
@@ -5,15 +5,16 @@ configure_lit_site_cfg(
         ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
 )
 
-set(STANDALONE_OPT_TEST_DEPENDS
+set(STANDALONE_TEST_DEPENDS
         FileCheck count not
         standalone-opt
+        standalone-translate
         )
 
-add_lit_testsuite(check-standalone-opt "Running the standalone-opt regression tests"
+add_lit_testsuite(check-standalone "Running the standalone regression tests"
         ${CMAKE_CURRENT_BINARY_DIR}
-        DEPENDS ${STANDALONE_OPT_TEST_DEPENDS}
+        DEPENDS ${STANDALONE_TEST_DEPENDS}
         )
-set_target_properties(check-standalone-opt PROPERTIES FOLDER "Tests")
+set_target_properties(check-standalone PROPERTIES FOLDER "Tests")
 
-add_lit_testsuites(STANDALONE_OPT ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${STANDALONE_OPT_TEST_DEPENDS})
+add_lit_testsuites(STANDALONE ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${STANDALONE_TEST_DEPENDS})

diff  --git a/mlir/examples/standalone/test/Standalone/standalone-translate.mlir b/mlir/examples/standalone/test/Standalone/standalone-translate.mlir
new file mode 100644
index 000000000000..2a096c38e128
--- /dev/null
+++ b/mlir/examples/standalone/test/Standalone/standalone-translate.mlir
@@ -0,0 +1,8 @@
+// RUN: standalone-translate --help | FileCheck %s
+// CHECK: --avx512-mlir-to-llvmir
+// CHECK: --deserialize-spirv
+// CHECK: --import-llvm
+// CHECK: --mlir-to-llvmir
+// CHECK: --mlir-to-nvvmir
+// CHECK: --mlir-to-rocdlir
+// CHECK: --serialize-spirv

diff  --git a/mlir/examples/standalone/test/lit.cfg.py b/mlir/examples/standalone/test/lit.cfg.py
index 049fbc73cd81..9fb5b543f006 100644
--- a/mlir/examples/standalone/test/lit.cfg.py
+++ b/mlir/examples/standalone/test/lit.cfg.py
@@ -16,7 +16,7 @@
 # Configuration file for the 'lit' test runner.
 
 # name: The name of this test suite.
-config.name = 'STANDALONE_OPT'
+config.name = 'STANDALONE'
 
 config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
 
@@ -54,7 +54,8 @@
 
 tool_dirs = [config.standalone_tools_dir, config.llvm_tools_dir]
 tools = [
-    'standalone-opt'
+    'standalone-opt',
+    'standalone-translate'
 ]
 
 llvm_config.add_tool_substitutions(tools, tool_dirs)

diff  --git a/mlir/test/Examples/standalone/test.toy b/mlir/test/Examples/standalone/test.toy
index 4f9ba5cc78e1..034fd9385d46 100644
--- a/mlir/test/Examples/standalone/test.toy
+++ b/mlir/test/Examples/standalone/test.toy
@@ -1,4 +1,4 @@
-# RUN: %cmake %mlir_src_root/examples/standalone -DCMAKE_CXX_COMPILER=%host_cxx -DCMAKE_C_COMPILER=%host_cc -DMLIR_DIR=%llvm_lib_dir/cmake/mlir ; %cmake --build . --target check-standalone-opt | tee %t | FileCheck %s
+# RUN: %cmake %mlir_src_root/examples/standalone -DCMAKE_CXX_COMPILER=%host_cxx -DCMAKE_C_COMPILER=%host_cc -DMLIR_DIR=%llvm_lib_dir/cmake/mlir ; %cmake --build . --target check-standalone | tee %t | FileCheck %s
 
-# CHECK: Expected Passes: 1
+# CHECK: Expected Passes: 2
 # UNSUPPORTED: windows, android


        


More information about the Mlir-commits mailing list