-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[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
Changes from 79 commits
74f7aa6
9f37bb6
c1ca4bd
483d5a2
71cc646
4b6bd5d
39dda7d
cb7337d
451c439
f25ce38
228729d
bf226ad
8d4bad5
704f111
d30c976
aa82910
3411c33
731fd7c
79d5acb
02d396e
3ddd47b
f6920fe
69ee9b0
0da3cb6
dfdb06a
19ae518
9160c95
2c496ec
b3801fe
3b9a32d
35d9dba
1cbb39d
aac4b98
5f77c3a
f00bf0d
739e8b1
9736b13
7727f15
bc41321
24b9d78
cb754ee
c8e98d9
73e8ff9
98efc62
77aaaf2
bda0295
1995868
14b3fde
4a9735e
447e474
c1feed3
edc9fa2
21eada6
d2ca62c
1c5c3c1
26f580e
732e606
44976b6
318f759
6d1beac
16b0414
05fa3a8
ef0d97e
91d6f80
8f7e939
764d638
dfbb919
3638c9b
a1f308e
db38f4e
1295496
b35f796
7c52b16
81a1530
cf508a9
da16951
784b04d
d99bc6a
e980d88
c26e36d
b9fb698
bc37a24
e51ae35
ef16ac5
e21a21f
c79ddcd
5ef348a
a30bb8d
b1ccf66
aae9e19
29581b7
180f3a5
c6b1fcd
fafc0ee
2abd9dc
f04509d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 ${MLIR_IRDL_TO_CPP_EXE} ${CMAKE_CURRENT_SOURCE_DIR}/${irdl_file} -o ${CMAKE_CURRENT_BINARY_DIR}/${irdl_file}.cpp.inc | ||||||
| DEPENDS ${MLIR_IRDL_TO_CPP_TARGET} ${irdl_file} | ||||||
| COMMENT "Building ${irdl_file}..." | ||||||
| ) | ||||||
| add_custom_target(${target} ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${irdl_file}.cpp.inc) | ||||||
Moxinilian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES | |
| GENERATED 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The separate custom command is useful because target does not track file output: if there is no separate command, the output files are rebuilt every time (due to a dependency CMake infers I presume?).
Adding GENERATED seems unnecessary, as CMake adds it to the output of commands anyway? But I found that depending on the executable directly instead of the target fixes the problem, albeit in a slightly sledgehammer way. If that sounds good to you, we can move on with merging.
| 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 |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
@@ -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 | ||
|
|
||
| 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) |
| 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 | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.