diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt index 9e786154a2b40..8a9d4a98f20da 100644 --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -63,6 +63,7 @@ list(INSERT CMAKE_MODULE_PATH 0 ) include(AddMLIR) +include(IRDLToCpp) # -BSymbolic is incompatible with TypeID if("${CMAKE_SHARED_LINKER_FLAGS}" MATCHES "-Bsymbolic[^-]") diff --git a/mlir/cmake/modules/CMakeLists.txt b/mlir/cmake/modules/CMakeLists.txt index 3ac1c79b090ed..054379706c869 100644 --- a/mlir/cmake/modules/CMakeLists.txt +++ b/mlir/cmake/modules/CMakeLists.txt @@ -80,6 +80,8 @@ set(MLIR_CONFIG_TABLEGEN_EXE mlir-tblgen) set(MLIR_CONFIG_PDLL_TABLEGEN_EXE mlir-pdll) set(MLIR_CONFIG_SRC_SHARDER_TABLEGEN_EXE mlir-src-sharder) +set(MLIR_CONFIG_IRDL_TO_CPP_EXE mlir-irdl-to-cpp) + configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/MLIRConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfig.cmake @@ -103,6 +105,7 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/MLIRConfigVersion.cmake ${CMAKE_CURRENT_SOURCE_DIR}/AddMLIR.cmake ${CMAKE_CURRENT_SOURCE_DIR}/AddMLIRPython.cmake + ${CMAKE_CURRENT_SOURCE_DIR}/IRDLToCpp.cmake ${CMAKE_CURRENT_SOURCE_DIR}/MLIRDetectPythonEnv.cmake DESTINATION ${MLIR_INSTALL_PACKAGE_DIR} COMPONENT mlir-cmake-exports) diff --git a/mlir/cmake/modules/IRDLToCpp.cmake b/mlir/cmake/modules/IRDLToCpp.cmake new file mode 100644 index 0000000000000..651e115e86357 --- /dev/null +++ b/mlir/cmake/modules/IRDLToCpp.cmake @@ -0,0 +1,9 @@ +function(add_irdl_to_cpp_target target irdl_file) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${irdl_file}.cpp.inc + COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/${irdl_file} -o ${CMAKE_CURRENT_BINARY_DIR}/${irdl_file}.cpp.inc + DEPENDS mlir-irdl-to-cpp ${irdl_file} + COMMENT "Building ${irdl_file}..." + ) + add_custom_target(${target} ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${irdl_file}.cpp.inc) +endfunction() diff --git a/mlir/cmake/modules/MLIRConfig.cmake.in b/mlir/cmake/modules/MLIRConfig.cmake.in index 7076d94a32f2b..114cb61ed6f55 100644 --- a/mlir/cmake/modules/MLIRConfig.cmake.in +++ b/mlir/cmake/modules/MLIRConfig.cmake.in @@ -12,6 +12,7 @@ set(MLIR_INCLUDE_DIRS "@MLIR_CONFIG_INCLUDE_DIRS@") set(MLIR_TABLEGEN_EXE "@MLIR_CONFIG_TABLEGEN_EXE@") set(MLIR_PDLL_TABLEGEN_EXE "@MLIR_CONFIG_PDLL_TABLEGEN_EXE@") set(MLIR_SRC_SHARDER_TABLEGEN_EXE "@MLIR_CONFIG_SRC_SHARDER_TABLEGEN_EXE@") +set(MLIR_IRDL_TO_CPP_EXE "@MLIR_CONFIG_IRDL_TO_CPP_EXE@") set(MLIR_INSTALL_AGGREGATE_OBJECTS "@MLIR_INSTALL_AGGREGATE_OBJECTS@") set(MLIR_ENABLE_BINDINGS_PYTHON "@MLIR_ENABLE_BINDINGS_PYTHON@") set(MLIR_ENABLE_EXECUTION_ENGINE "@MLIR_ENABLE_EXECUTION_ENGINE@") diff --git a/mlir/test/lib/Dialect/CMakeLists.txt b/mlir/test/lib/Dialect/CMakeLists.txt index 29fb4441a24fd..b14448099e0c5 100644 --- a/mlir/test/lib/Dialect/CMakeLists.txt +++ b/mlir/test/lib/Dialect/CMakeLists.txt @@ -19,6 +19,7 @@ add_subdirectory(SPIRV) add_subdirectory(Tensor) add_subdirectory(Test) add_subdirectory(TestDyn) +add_subdirectory(TestIRDLToCpp) add_subdirectory(Tosa) add_subdirectory(Transform) add_subdirectory(Vector) diff --git a/mlir/test/lib/Dialect/TestIRDLToCpp/CMakeLists.txt b/mlir/test/lib/Dialect/TestIRDLToCpp/CMakeLists.txt new file mode 100644 index 0000000000000..c3ee76f4b9e71 --- /dev/null +++ b/mlir/test/lib/Dialect/TestIRDLToCpp/CMakeLists.txt @@ -0,0 +1,13 @@ +add_irdl_to_cpp_target(TestIRDLToCppGen test_irdl_to_cpp.irdl.mlir) + +add_mlir_library(MLIRTestIRDLToCppDialect + TestIRDLToCppDialect.cpp + + EXCLUDE_FROM_LIBMLIR + + DEPENDS + TestIRDLToCppGen + + LINK_LIBS PUBLIC + MLIRIR +) diff --git a/mlir/test/lib/Dialect/TestIRDLToCpp/TestIRDLToCppDialect.cpp b/mlir/test/lib/Dialect/TestIRDLToCpp/TestIRDLToCppDialect.cpp new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/mlir/test/lib/Dialect/TestIRDLToCpp/test_irdl_to_cpp.irdl.mlir b/mlir/test/lib/Dialect/TestIRDLToCpp/test_irdl_to_cpp.irdl.mlir new file mode 100644 index 0000000000000..48caa8c89ce6f --- /dev/null +++ b/mlir/test/lib/Dialect/TestIRDLToCpp/test_irdl_to_cpp.irdl.mlir @@ -0,0 +1 @@ +irdl.dialect @test_irdl_to_cpp {} diff --git a/mlir/tools/CMakeLists.txt b/mlir/tools/CMakeLists.txt index 072e83c5d45ea..93e8ebb117581 100644 --- a/mlir/tools/CMakeLists.txt +++ b/mlir/tools/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(mlir-irdl-to-cpp) add_subdirectory(mlir-lsp-server) add_subdirectory(mlir-opt) add_subdirectory(mlir-parser-fuzzer) diff --git a/mlir/tools/mlir-irdl-to-cpp/CMakeLists.txt b/mlir/tools/mlir-irdl-to-cpp/CMakeLists.txt new file mode 100644 index 0000000000000..36bdfc58af015 --- /dev/null +++ b/mlir/tools/mlir-irdl-to-cpp/CMakeLists.txt @@ -0,0 +1,7 @@ +add_mlir_tool(mlir-irdl-to-cpp + mlir-irdl-to-cpp.cpp + ) +mlir_target_link_libraries(mlir-irdl-to-cpp + PRIVATE + MLIRTargetIRDLToCpp + ) diff --git a/mlir/tools/mlir-irdl-to-cpp/mlir-irdl-to-cpp.cpp b/mlir/tools/mlir-irdl-to-cpp/mlir-irdl-to-cpp.cpp new file mode 100644 index 0000000000000..5e0ee6c161a41 --- /dev/null +++ b/mlir/tools/mlir-irdl-to-cpp/mlir-irdl-to-cpp.cpp @@ -0,0 +1,94 @@ +//===- mlir-irdl-to-cpp.cpp - IRDL to C++ conversion tool -----------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This is a command line utility that translates an IRDL dialect definition +// into a C++ implementation to be included in MLIR. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Dialect/IRDL/IR/IRDL.h" +#include "mlir/IR/AsmState.h" +#include "mlir/IR/DialectRegistry.h" +#include "mlir/IR/MLIRContext.h" +#include "mlir/Support/FileUtilities.h" +#include "mlir/Support/ToolUtilities.h" +#include "mlir/Target/IRDLToCpp/IRDLToCpp.h" +#include "mlir/Tools/ParseUtilities.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" + +using namespace mlir; + +static LogicalResult translateIRDLToCpp(int argc, char **argv) { + static llvm::cl::opt inputFilename( + llvm::cl::Positional, llvm::cl::desc(""), + llvm::cl::init("-")); + + static llvm::cl::opt outputFilename( + "o", llvm::cl::desc("Output filename"), llvm::cl::value_desc("filename"), + llvm::cl::init("-")); + + llvm::InitLLVM y(argc, argv); + + llvm::cl::ParseCommandLineOptions(argc, argv, "mlir-irdl-to-cpp"); + + std::string errorMessage; + std::unique_ptr input = + openInputFile(inputFilename, &errorMessage); + if (!input) { + llvm::errs() << errorMessage << "\n"; + return failure(); + } + + std::unique_ptr output = + openOutputFile(outputFilename, &errorMessage); + if (!output) { + llvm::errs() << errorMessage << "\n"; + return failure(); + } + + auto sourceMgr = std::make_shared(); + sourceMgr->AddNewSourceBuffer(std::move(input), SMLoc()); + + DialectRegistry registry; + registry.insert(); + MLIRContext ctx(registry); + ctx.printOpOnDiagnostic(true); + + ParserConfig parseConfig(&ctx); + OwningOpRef op = + parseSourceFileForTool(sourceMgr, parseConfig, true); + if (!op) + return failure(); + + auto moduleOp = llvm::cast(*op); + + SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &ctx); + + for (Operation &op : moduleOp.getOps()) { + auto dialectOp = llvm::dyn_cast(op); + if (!dialectOp) + continue; + + // TODO: accept multiple operations in translation to not generate headers + // multiple times. + if (failed(irdl::translateIRDLDialectToCpp(dialectOp, output->os()))) + return failure(); + } + + output->keep(); + return success(); +} + +int main(int argc, char **argv) { + return failed(translateIRDLToCpp(argc, argv)); +}