Translation.h is currently awkwardly shoved into the top-level mlir, even though it is
specific to the mlir-translate tool. This commit moves it to a new Tools/mlir-translate
directory, which is intended for libraries used to implement tools. It also splits the
translate registry from the main entry point, to more closely mirror what mlir-opt
does.
Differential Revision: https://reviews.llvm.org/D121026
MLIRParser
MLIRPass
MLIRSPIRV
- MLIRTranslation
+ MLIRTranslateLib
MLIRSupport
)
#include "mlir/InitAllTranslations.h"
#include "mlir/Support/LogicalResult.h"
-#include "mlir/Translation.h"
+#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
#include "Standalone/StandaloneDialect.h"
--- /dev/null
+//===- MlirTranslateMain.h - MLIR Translation Driver main -------*- C++ -*-===//
+//
+// Part of the LLVM Project, 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Main entry function for mlir-translate for when built as standalone binary.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TOOLS_MLIRTRANSLATE_MLIRTRANSLATEMAIN_H
+#define MLIR_TOOLS_MLIRTRANSLATE_MLIRTRANSLATEMAIN_H
+
+#include "mlir/Support/LogicalResult.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace mlir {
+/// Translate to/from an MLIR module from/to an external representation (e.g.
+/// LLVM IR, SPIRV binary, ...). This is the entry point for the implementation
+/// of tools like `mlir-translate`. The translation to perform is parsed from
+/// the command line. The `toolName` argument is used for the header displayed
+/// by `--help`.
+LogicalResult mlirTranslateMain(int argc, char **argv, StringRef toolName);
+} // namespace mlir
+
+#endif // MLIR_TOOLS_MLIRTRANSLATE_MLIRTRANSLATEMAIN_H
// Registry for user-provided translations.
//
//===----------------------------------------------------------------------===//
-#ifndef MLIR_TRANSLATION_H
-#define MLIR_TRANSLATION_H
+
+#ifndef MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
+#define MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
#include "llvm/Support/CommandLine.h"
size_t globalWidth) const override;
};
-/// Translate to/from an MLIR module from/to an external representation (e.g.
-/// LLVM IR, SPIRV binary, ...). This is the entry point for the implementation
-/// of tools like `mlir-translate`. The translation to perform is parsed from
-/// the command line. The `toolName` argument is used for the header displayed
-/// by `--help`.
-LogicalResult mlirTranslateMain(int argc, char **argv,
- llvm::StringRef toolName);
-
} // namespace mlir
-#endif // MLIR_TRANSLATION_H
+#endif // MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
add_subdirectory(Target)
add_subdirectory(Tools)
add_subdirectory(Transforms)
-add_subdirectory(Translation)
# Only enable the ExecutionEngine if the native target is configured in.
if(TARGET ${LLVM_NATIVE_ARCH})
MLIRSPIRVSerialization
MLIRSupport
MLIRTransforms
- MLIRTranslation
+ MLIRTranslateLib
)
MLIRMath
MLIRSCF
MLIRSupport
- MLIRTranslation
+ MLIRTranslateLib
)
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dialect.h"
#include "mlir/Target/Cpp/CppEmitter.h"
-#include "mlir/Translation.h"
+#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/Support/CommandLine.h"
using namespace mlir;
MLIRDLTI
MLIRLLVMIR
MLIRLLVMIRTransforms
- MLIRTranslation
+ MLIRTranslateLib
)
add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
LINK_LIBS PUBLIC
MLIRDLTI
MLIRLLVMIR
- MLIRTranslation
+ MLIRTranslateLib
)
#include "mlir/IR/MLIRContext.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Target/LLVMIR/TypeFromLLVM.h"
-#include "mlir/Translation.h"
+#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/TypeSwitch.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Target/LLVMIR/Dialect/All.h"
#include "mlir/Target/LLVMIR/Export.h"
-#include "mlir/Translation.h"
+#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
MLIRSPIRVSerialization
MLIRSPIRVDeserialization
MLIRSupport
- MLIRTranslation
+ MLIRTranslateLib
)
MLIRSPIRV
MLIRSPIRVBinaryUtils
MLIRSupport
- MLIRTranslation
+ MLIRTranslateLib
)
MLIRSPIRV
MLIRSPIRVBinaryUtils
MLIRSupport
- MLIRTranslation
+ MLIRTranslateLib
)
#include "mlir/Support/FileUtilities.h"
#include "mlir/Target/SPIRV/Deserialization.h"
#include "mlir/Target/SPIRV/Serialization.h"
-#include "mlir/Translation.h"
+#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SMLoc.h"
add_subdirectory(mlir-lsp-server)
add_subdirectory(mlir-opt)
add_subdirectory(mlir-reduce)
+add_subdirectory(mlir-translate)
add_subdirectory(PDLL)
--- /dev/null
+add_mlir_library(MLIRTranslateLib
+ MlirTranslateMain.cpp
+ Translation.cpp
+
+ ADDITIONAL_HEADER_DIRS
+ ${MLIR_MAIN_INCLUDE_DIR}/mlir/Tools/mlir-translate
+
+ LINK_LIBS PUBLIC
+ MLIRIR
+ MLIRParser
+ )
--- /dev/null
+//===- MlirTranslateMain.cpp - MLIR Translation entry point ---------------===//
+//
+// Part of the LLVM Project, 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-translate/MlirTranslateMain.h"
+#include "mlir/IR/AsmState.h"
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/Dialect.h"
+#include "mlir/IR/Verifier.h"
+#include "mlir/Parser/Parser.h"
+#include "mlir/Support/FileUtilities.h"
+#include "mlir/Support/ToolUtilities.h"
+#include "mlir/Tools/mlir-translate/Translation.h"
+#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/ToolOutputFile.h"
+
+using namespace mlir;
+
+//===----------------------------------------------------------------------===//
+// Translation Parser
+//===----------------------------------------------------------------------===//
+
+LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
+ llvm::StringRef toolName) {
+
+ 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> allowUnregisteredDialects(
+ "allow-unregistered-dialect",
+ llvm::cl::desc("Allow operation with no registered dialects"),
+ llvm::cl::init(false));
+
+ 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));
+
+ llvm::InitLLVM y(argc, argv);
+
+ // Add flags for all the registered translations.
+ llvm::cl::opt<const TranslateFunction *, false, TranslationParser>
+ translationRequested("", llvm::cl::desc("Translation to perform"),
+ llvm::cl::Required);
+ registerAsmPrinterCLOptions();
+ registerMLIRContextCLOptions();
+ llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
+
+ std::string errorMessage;
+ auto input = openInputFile(inputFilename, &errorMessage);
+ if (!input) {
+ llvm::errs() << errorMessage << "\n";
+ return failure();
+ }
+
+ auto output = openOutputFile(outputFilename, &errorMessage);
+ if (!output) {
+ llvm::errs() << errorMessage << "\n";
+ return failure();
+ }
+
+ // Processes the memory buffer with a new MLIRContext.
+ auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
+ raw_ostream &os) {
+ MLIRContext context;
+ context.allowUnregisteredDialects(allowUnregisteredDialects);
+ context.printOpOnDiagnostic(!verifyDiagnostics);
+ llvm::SourceMgr sourceMgr;
+ sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
+
+ if (!verifyDiagnostics) {
+ 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.
+ SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr, &context);
+ (void)(*translationRequested)(sourceMgr, os, &context);
+ return sourceMgrHandler.verify();
+ };
+
+ if (splitInputFile) {
+ if (failed(splitAndProcessBuffer(std::move(input), processBuffer,
+ output->os())))
+ return failure();
+ } else if (failed(processBuffer(std::move(input), output->os()))) {
+ return failure();
+ }
+
+ output->keep();
+ return success();
+}
//
//===----------------------------------------------------------------------===//
-#include "mlir/Translation.h"
+#include "mlir/Tools/mlir-translate/Translation.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Verifier.h"
#include "mlir/Parser/Parser.h"
-#include "mlir/Support/FileUtilities.h"
-#include "mlir/Support/ToolUtilities.h"
-#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/SourceMgr.h"
-#include "llvm/Support/ToolOutputFile.h"
using namespace mlir;
});
llvm::cl::parser<const TranslateFunction *>::printOptionInfo(o, globalWidth);
}
-
-LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
- llvm::StringRef toolName) {
-
- 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> allowUnregisteredDialects(
- "allow-unregistered-dialect",
- llvm::cl::desc("Allow operation with no registered dialects"),
- llvm::cl::init(false));
-
- 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));
-
- llvm::InitLLVM y(argc, argv);
-
- // Add flags for all the registered translations.
- llvm::cl::opt<const TranslateFunction *, false, TranslationParser>
- translationRequested("", llvm::cl::desc("Translation to perform"),
- llvm::cl::Required);
- registerAsmPrinterCLOptions();
- registerMLIRContextCLOptions();
- llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
-
- std::string errorMessage;
- auto input = openInputFile(inputFilename, &errorMessage);
- if (!input) {
- llvm::errs() << errorMessage << "\n";
- return failure();
- }
-
- auto output = openOutputFile(outputFilename, &errorMessage);
- if (!output) {
- llvm::errs() << errorMessage << "\n";
- return failure();
- }
-
- // Processes the memory buffer with a new MLIRContext.
- auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
- raw_ostream &os) {
- MLIRContext context;
- context.allowUnregisteredDialects(allowUnregisteredDialects);
- context.printOpOnDiagnostic(!verifyDiagnostics);
- llvm::SourceMgr sourceMgr;
- sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
-
- if (!verifyDiagnostics) {
- 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.
- SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr, &context);
- (void)(*translationRequested)(sourceMgr, os, &context);
- return sourceMgrHandler.verify();
- };
-
- if (splitInputFile) {
- if (failed(splitAndProcessBuffer(std::move(input), processBuffer,
- output->os())))
- return failure();
- } else if (failed(processBuffer(std::move(input), output->os()))) {
- return failure();
- }
-
- output->keep();
- return success();
-}
+++ /dev/null
-add_mlir_library(MLIRTranslation
- Translation.cpp
-
- ADDITIONAL_HEADER_DIRS
- ${MLIR_MAIN_INCLUDE_DIR}/mlir/Translation
-
- LINK_LIBS PUBLIC
- MLIRIR
- MLIRParser
- )
MLIRSPIRV
MLIRTargetLLVMIRExport
MLIRTransforms
- MLIRTranslation
+ MLIRTranslateLib
MLIRSupport
)
endif()
MLIRParser
MLIRPass
MLIRSPIRV
- MLIRTranslation
+ MLIRTranslateLib
MLIRSupport
)
#include "mlir/InitAllTranslations.h"
#include "mlir/Support/LogicalResult.h"
-#include "mlir/Translation.h"
+#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
using namespace mlir;
MLIRSupport
MLIRTargetLLVMIRExport
MLIRTransforms
- MLIRTranslation
+ MLIRTranslateLib
${Vulkan_LIBRARY}
)