Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mlir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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[^-]")
Expand Down
3 changes: 3 additions & 0 deletions mlir/cmake/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions mlir/cmake/modules/IRDLToCpp.cmake
Original file line number Diff line number Diff line change
@@ -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 $<TARGET_FILE:mlir-irdl-to-cpp> ${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()
1 change: 1 addition & 0 deletions mlir/cmake/modules/MLIRConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@")
Expand Down
1 change: 1 addition & 0 deletions mlir/test/lib/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 13 additions & 0 deletions mlir/test/lib/Dialect/TestIRDLToCpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
irdl.dialect @test_irdl_to_cpp {}
1 change: 1 addition & 0 deletions mlir/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(mlir-irdl-to-cpp)
add_subdirectory(mlir-lsp-server)
add_subdirectory(mlir-opt)
add_subdirectory(mlir-parser-fuzzer)
Expand Down
7 changes: 7 additions & 0 deletions mlir/tools/mlir-irdl-to-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
94 changes: 94 additions & 0 deletions mlir/tools/mlir-irdl-to-cpp/mlir-irdl-to-cpp.cpp
Original file line number Diff line number Diff line change
@@ -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<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("-"));

llvm::InitLLVM y(argc, argv);

llvm::cl::ParseCommandLineOptions(argc, argv, "mlir-irdl-to-cpp");

std::string errorMessage;
std::unique_ptr<llvm::MemoryBuffer> input =
openInputFile(inputFilename, &errorMessage);
if (!input) {
llvm::errs() << errorMessage << "\n";
return failure();
}

std::unique_ptr<llvm::ToolOutputFile> output =
openOutputFile(outputFilename, &errorMessage);
if (!output) {
llvm::errs() << errorMessage << "\n";
return failure();
}

auto sourceMgr = std::make_shared<llvm::SourceMgr>();
sourceMgr->AddNewSourceBuffer(std::move(input), SMLoc());

DialectRegistry registry;
registry.insert<irdl::IRDLDialect>();
MLIRContext ctx(registry);
ctx.printOpOnDiagnostic(true);

ParserConfig parseConfig(&ctx);
OwningOpRef<Operation *> op =
parseSourceFileForTool(sourceMgr, parseConfig, true);
if (!op)
return failure();

auto moduleOp = llvm::cast<ModuleOp>(*op);

SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &ctx);

for (Operation &op : moduleOp.getOps()) {
auto dialectOp = llvm::dyn_cast<irdl::DialectOp>(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));
}