Skip to content

[MLIR][IRDL] Added IRDL to C++ Translation #133982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 96 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
74f7aa6
set up mlir-translate for IRDL translation
Moxinilian Jan 13, 2025
9f37bb6
simplify translation registration
Moxinilian Jan 13, 2025
c1ca4bd
generate dialect declaration
Moxinilian Jan 13, 2025
483d5a2
add constraints on dialect names
Moxinilian Jan 13, 2025
71cc646
add dialect def
Moxinilian Jan 13, 2025
4b6bd5d
first draft of op template
Moxinilian Jan 14, 2025
39dda7d
added base type class
hhkit Jan 15, 2025
cb7337d
generate singleton types
hhkit Jan 16, 2025
451c439
basic operation work + escaped braces in operationdecl
hhkit Jan 17, 2025
f25ce38
mlir explicit type id
hhkit Jan 17, 2025
228729d
used namespace path for type reg
hhkit Jan 17, 2025
bf226ad
Merge branch 'main' of https://github.com/opencompl/llvm-project into…
Jan 20, 2025
8d4bad5
merged from upstream, added name to mvp
Jan 20, 2025
704f111
return operand names
hhkit Jan 20, 2025
d30c976
return operand names
hhkit Jan 20, 2025
aa82910
Merge branch 'hhkit/irdl-to-cpp' of https://github.com/opencompl/llvm…
hhkit Jan 20, 2025
3411c33
revert bad edit
hhkit Jan 20, 2025
731fd7c
typedef list + print/parse defs
hhkit Jan 20, 2025
79d5acb
compile decls and defs for build
hhkit Jan 21, 2025
02d396e
refactored name processing logic
hhkit Jan 21, 2025
3ddd47b
define typeIDs
hhkit Jan 22, 2025
f6920fe
irdl-to-cpp toolchain (#16)
Moxinilian Jan 24, 2025
69ee9b0
add cross-compile support (#17)
Moxinilian Jan 26, 2025
0da3cb6
build driver
hhkit Jan 29, 2025
dfdb06a
first step towards IRDL to cpp test (#18)
Moxinilian Jan 31, 2025
19ae518
header builds
hhkit Feb 3, 2025
9160c95
fixed compile errors in define
hhkit Feb 3, 2025
2c496ec
successful registration in mlir-opt
hhkit Feb 3, 2025
b3801fe
abort fix
hhkit Feb 4, 2025
3b9a32d
removed custom print and parse
hhkit Feb 6, 2025
35d9dba
simplified test case
hhkit Feb 6, 2025
1cbb39d
[IRDL2CPP] Attempt at Templating (#19)
hhkit Feb 10, 2025
aac4b98
some changes
Moxinilian Feb 28, 2025
5f77c3a
fixed errors
hhkit Mar 1, 2025
f00bf0d
getters for operands
hhkit Mar 3, 2025
739e8b1
clang-format
hhkit Mar 3, 2025
9736b13
removed unneeded functions
hhkit Mar 3, 2025
7727f15
handle errors
hhkit Mar 3, 2025
bc41321
support negative
hhkit Mar 3, 2025
24b9d78
check results type
hhkit Mar 3, 2025
cb754ee
mlir-opt verify test
hhkit Mar 7, 2025
c8e98d9
fixed the easy changes
hhkit Mar 10, 2025
73e8ff9
PR changes pt ii
hhkit Mar 10, 2025
98efc62
case conversion
hhkit Mar 10, 2025
77aaaf2
removed oplist and typelist defs
hhkit Mar 10, 2025
bda0295
newline fix
hhkit Mar 10, 2025
1995868
added test for conversion
hhkit Mar 10, 2025
14b3fde
Merge branch 'main' of https://github.com/opencompl/llvm-project into…
hhkit Mar 11, 2025
4a9735e
new lines, macro guard documentation
hhkit Mar 11, 2025
447e474
Update mlir/lib/Target/IRDLToCpp/IRDLToCpp.cpp
hhkit Mar 18, 2025
c1feed3
Merge branch 'hhkit/irdl-to-cpp' of https://github.com/opencompl/llvm…
hhkit Mar 18, 2025
edc9fa2
simple PR changes
hhkit Mar 18, 2025
21eada6
set up for multiple dialect conversions
hhkit Mar 18, 2025
d2ca62c
Merge branch 'main' of https://github.com/opencompl/llvm-project into…
hhkit Mar 18, 2025
1c5c3c1
conversion test works again
hhkit Mar 18, 2025
26f580e
getODSResults
hhkit Mar 18, 2025
732e606
fixed header naming
hhkit Mar 24, 2025
44976b6
symbol verification
hhkit Mar 24, 2025
318f759
invalid names
hhkit Mar 24, 2025
6d1beac
check every irdl op
hhkit Mar 24, 2025
16b0414
unsupported irdl ops
hhkit Mar 24, 2025
05fa3a8
base types
hhkit Mar 24, 2025
ef0d97e
simplified test
hhkit Mar 24, 2025
91d6f80
split file works omg cleanup needed
hhkit Mar 24, 2025
8f7e939
silence most failing tests
hhkit Mar 25, 2025
764d638
allow types and attribs to be prefixed with ! and #
hhkit Mar 25, 2025
dfbb919
clean up mlir-irdl-to-cpp
hhkit Mar 26, 2025
3638c9b
fixed PR changes
hhkit Mar 31, 2025
a1f308e
renamed name checker
hhkit Mar 31, 2025
db38f4e
invert verifysupport check
hhkit Mar 31, 2025
1295496
attempt to clean up main.cpp
hhkit Mar 31, 2025
b35f796
removed name with underscore
hhkit Mar 31, 2025
7c52b16
simple cleanup
hhkit Apr 1, 2025
81a1530
remaining fixes
hhkit Apr 1, 2025
cf508a9
fix templates
Moxinilian Apr 1, 2025
da16951
add comments and fix bug
Moxinilian Apr 1, 2025
784b04d
extracted getters and builders
hhkit Apr 1, 2025
d99bc6a
Merge branch 'main' into hhkit/irdl-to-cpp
hhkit Apr 1, 2025
e980d88
fixed comments
hhkit Apr 2, 2025
c26e36d
fixed multidialect bug
hhkit Apr 4, 2025
b9fb698
Merge branch 'main' into hhkit/irdl-to-cpp
hhkit Apr 4, 2025
bc37a24
Update mlir/lib/Target/IRDLToCpp/IRDLToCpp.cpp
hhkit Apr 4, 2025
e51ae35
Update mlir/lib/Target/IRDLToCpp/IRDLToCpp.cpp
hhkit Apr 4, 2025
ef16ac5
Update mlir/lib/Target/IRDLToCpp/IRDLToCpp.cpp
hhkit Apr 4, 2025
e21a21f
Update mlir/lib/Target/IRDLToCpp/IRDLToCpp.cpp
hhkit Apr 4, 2025
c79ddcd
PR suggestions
hhkit Apr 4, 2025
5ef348a
more PR fixes
hhkit Apr 4, 2025
a30bb8d
moved invalid name test
hhkit Apr 4, 2025
b1ccf66
Update mlir/lib/Target/IRDLToCpp/Templates/PerOperationDecl.txt
hhkit Apr 4, 2025
aae9e19
added basic filecheck for irdl translation
hhkit Apr 4, 2025
29581b7
comments, as requested by PR
hhkit Apr 8, 2025
180f3a5
hopefully fix CMake problems
Moxinilian Apr 25, 2025
c6b1fcd
hopefully make CI happy about templates
Moxinilian Apr 26, 2025
fafc0ee
simplify processBuffer code to please MSVC
Moxinilian Apr 26, 2025
2abd9dc
wrap at 80 cols
hhkit Apr 28, 2025
f04509d
Merge branch 'main' into hhkit/irdl-to-cpp
hhkit Apr 28, 2025
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
2 changes: 2 additions & 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 Expand Up @@ -206,6 +207,7 @@ include_directories(BEFORE
# Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
# MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
# from another directory like tools
add_subdirectory(tools/mlir-irdl-to-cpp)
add_subdirectory(tools/mlir-linalg-ods-gen)
add_subdirectory(tools/mlir-pdll)
add_subdirectory(tools/mlir-tblgen)
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 @@ -112,6 +112,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 @@ -135,6 +137,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
12 changes: 12 additions & 0 deletions mlir/cmake/modules/IRDLToCpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function(add_irdl_to_cpp_target target irdl_file)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${irdl_file}.cpp.inc
COMMAND ${MLIR_IRDL_TO_CPP_EXE} ${CMAKE_CURRENT_SOURCE_DIR}/${irdl_file} -o ${CMAKE_CURRENT_BINARY_DIR}/${irdl_file}.cpp.inc

# The command output depends on the executable to ensure IRDL sources are properly rebuilt
# if the tool changes.
DEPENDS ${MLIR_IRDL_TO_CPP_EXE} ${CMAKE_CURRENT_SOURCE_DIR}/${irdl_file}
COMMENT "Building ${irdl_file}..."
)
add_custom_target(${target} 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
4 changes: 4 additions & 0 deletions mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def IRDL_TypeOp : IRDL_Op<"type",
let regions = (region SizedRegion<1>:$body);
let assemblyFormat =
"$sym_name attr-dict-with-keyword custom<SingleBlockRegion>($body)";
let hasVerifier = 1;
}

def IRDL_AttributeOp : IRDL_Op<"attribute",
Expand Down Expand Up @@ -126,6 +127,8 @@ def IRDL_AttributeOp : IRDL_Op<"attribute",
let regions = (region SizedRegion<1>:$body);
let assemblyFormat =
"$sym_name attr-dict-with-keyword custom<SingleBlockRegion>($body)";

let hasVerifier = 1;
}

def IRDL_ParametersOp : IRDL_Op<"parameters",
Expand Down Expand Up @@ -202,6 +205,7 @@ def IRDL_OperationOp : IRDL_Op<"operation",
let assemblyFormat =
"$sym_name attr-dict-with-keyword custom<SingleBlockRegion>($body)";
let hasRegionVerifier = true;
let hasVerifier = 1;
}

def IRDL_OperandsOp : IRDL_Op<"operands", [HasParent<"OperationOp">]> {
Expand Down
3 changes: 3 additions & 0 deletions mlir/include/mlir/InitAllTranslations.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef MLIR_INITALLTRANSLATIONS_H
#define MLIR_INITALLTRANSLATIONS_H

#include "mlir/Target/IRDLToCpp/TranslationRegistration.h"

namespace mlir {

void registerFromLLVMIRTranslation();
Expand All @@ -33,6 +35,7 @@ inline void registerAllTranslations() {
static bool initOnce = []() {
registerFromLLVMIRTranslation();
registerFromSPIRVTranslation();
registerIRDLToCppTranslation();
registerToCppTranslation();
registerToLLVMIRTranslation();
registerToSPIRVTranslation();
Expand Down
38 changes: 38 additions & 0 deletions mlir/include/mlir/Target/IRDLToCpp/IRDLToCpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===- IRDLToCpp.h - Register translation -----------------------*- 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the registration function for the IRDL to C++ translation.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TARGET_IRDLTOCPP_IRDLTOCPP_H
#define MLIR_TARGET_IRDLTOCPP_IRDLTOCPP_H

#include "mlir/Dialect/IRDL/IR/IRDL.h"

namespace mlir {
namespace irdl {

/// Translates an IRDL dialect definition to a C++ definition that can be used
/// with MLIR.
///
/// The following preprocessor macros will generate the following code:
///
/// // This define generates code for the dialect's class declarations
/// #define GEN_DIALECT_DECL_HEADER
///
/// // This define generates code for the dialect's class definitions
/// #define GEN_DIALECT_DEF
LogicalResult
translateIRDLDialectToCpp(llvm::ArrayRef<irdl::DialectOp> dialects,
raw_ostream &output);

} // namespace irdl
} // namespace mlir

#endif // MLIR_TARGET_IRDLTOCPP_IRDLTOCPP_H
22 changes: 22 additions & 0 deletions mlir/include/mlir/Target/IRDLToCpp/TranslationRegistration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===- TranslationRegistration.h - Register translation ---------*- 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the registration function for the IRDL to C++ translation.
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_TARGET_IRDLTOCPP_TRANSLATIONREGISTRATION_H
#define MLIR_TARGET_IRDLTOCPP_TRANSLATIONREGISTRATION_H

namespace mlir {

void registerIRDLToCppTranslation();

} // namespace mlir

#endif // MLIR_TARGET_IRDLTOCPP_TRANSLATIONREGISTRATION_H
65 changes: 53 additions & 12 deletions mlir/lib/Dialect/IRDL/IR/IRDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,62 @@ static void printSingleBlockRegion(OpAsmPrinter &p, Operation *op,
if (!region.getBlocks().front().empty())
p.printRegion(region);
}
static llvm::LogicalResult isValidName(llvm::StringRef in, mlir::Operation *loc,
const Twine &label) {
if (in.empty())
return loc->emitError("name of ") << label << " is empty";

bool allowUnderscore = false;
for (auto &elem : in) {
if (elem == '_') {
if (!allowUnderscore)
return loc->emitError("name of ")
<< label << " should not contain leading or double underscores";
} else {
if (!isalnum(elem))
return loc->emitError("name of ")
<< label
<< " must contain only lowercase letters, digits and "
"underscores";

if (llvm::isUpper(elem))
return loc->emitError("name of ")
<< label << " should not contain uppercase letters";
}

allowUnderscore = elem != '_';
}

return success();
}
Comment on lines +77 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this change (and the other changes related to the names) in a separate PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@Moxinilian Moxinilian Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll clean that up in a subsequent PR if you don't mind. This is a little bit of a workaround for the symbol limitations. I'd like to remove symbols entirely from IRDL once this is merged. If you prefer it to be done the other way around it's also possible, but it would add some delay on this PR so it's your call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem for me, that's okay


LogicalResult DialectOp::verify() {
if (!Dialect::isValidNamespace(getName()))
return emitOpError("invalid dialect name");
if (failed(isValidName(getSymName(), getOperation(), "dialect")))
return failure();

return success();
}

LogicalResult OperationOp::verify() {
return isValidName(getSymName(), getOperation(), "operation");
}

LogicalResult TypeOp::verify() {
auto symName = getSymName();
if (symName.front() == '!')
symName = symName.substr(1);
return isValidName(symName, getOperation(), "type");
}

LogicalResult AttributeOp::verify() {
auto symName = getSymName();
if (symName.front() == '#')
symName = symName.substr(1);
return isValidName(symName, getOperation(), "attribute");
}

LogicalResult OperationOp::verifyRegions() {
// Stores pairs of value kinds and the list of names of values of this kind in
// the operation.
Expand Down Expand Up @@ -133,18 +182,10 @@ static LogicalResult verifyNames(Operation *op, StringRef kindName,
DenseMap<StringRef, size_t> nameMap;
for (auto [i, name] : llvm::enumerate(names)) {
StringRef nameRef = llvm::cast<StringAttr>(name).getValue();
if (nameRef.empty())
return op->emitOpError()
<< "name of " << kindName << " #" << i << " is empty";
if (!llvm::isAlpha(nameRef[0]) && nameRef[0] != '_')
return op->emitOpError()
<< "name of " << kindName << " #" << i
<< " must start with either a letter or an underscore";
if (llvm::any_of(nameRef,
[](char c) { return !llvm::isAlnum(c) && c != '_'; }))
return op->emitOpError()
<< "name of " << kindName << " #" << i
<< " must contain only letters, digits and underscores";

if (failed(isValidName(nameRef, op, Twine(kindName) + " #" + Twine(i))))
return failure();

if (nameMap.contains(nameRef))
return op->emitOpError() << "name of " << kindName << " #" << i
<< " is a duplicate of the name of " << kindName
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(Cpp)
add_subdirectory(IRDLToCpp)
add_subdirectory(SPIRV)
add_subdirectory(LLVMIR)
add_subdirectory(LLVM)
Expand Down
18 changes: 18 additions & 0 deletions mlir/lib/Target/IRDLToCpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_mlir_translation_library(MLIRTargetIRDLToCpp
TranslationRegistration.cpp
IRDLToCpp.cpp
Templates/DialectDecl.txt
Templates/DialectDef.txt
Templates/Header.txt
Templates/PerOperationDecl.txt
Templates/PerOperationDef.txt
Templates/TypeDecl.txt
Templates/TypeDef.txt
Templates/TypeHeaderDecl.txt
Templates/TypeHeaderDef.txt

LINK_LIBS PUBLIC
MLIRIR
MLIRIRDL
MLIRTranslateLib
)
Loading