-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][tblgen] Fix region and successor references in custom directives #146242
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
base: main
Are you sure you want to change the base?
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core Author: Henrich Lauko (xlauko) ChangesPreviously, references to regions and successors were incorrectly disallowed outside the top-level assembly form. This change enables the use of bound regions and successors as variables in custom directives. Full diff: https://github.com/llvm/llvm-project/pull/146242.diff 3 Files Affected:
diff --git a/mlir/test/mlir-tblgen/op-format-spec.td b/mlir/test/mlir-tblgen/op-format-spec.td
index 02bf65609b21a..03b63f42c7767 100644
--- a/mlir/test/mlir-tblgen/op-format-spec.td
+++ b/mlir/test/mlir-tblgen/op-format-spec.td
@@ -49,6 +49,19 @@ def DirectiveCustomValidD : TestFormat_Op<[{
def DirectiveCustomValidE : TestFormat_Op<[{
custom<MyDirective>(prop-dict) attr-dict
}]>, Arguments<(ins UnitAttr:$flag)>;
+def DirectiveCustomValidF : TestFormat_Op<[{
+ $operand custom<MyDirective>(ref($operand)) attr-dict
+}]>, Arguments<(ins Optional<I64>:$operand)>;
+def DirectiveCustomValidG : TestFormat_Op<[{
+ $body custom<MyDirective>(ref($body)) attr-dict
+}]> {
+ let regions = (region AnyRegion:$body);
+}
+def DirectiveCustomValidH : TestFormat_Op<[{
+ $successor custom<MyDirective>(ref($successor)) attr-dict
+}]> {
+ let successors = (successor AnySuccessor:$successor);
+}
//===----------------------------------------------------------------------===//
// functional-type
diff --git a/mlir/test/mlir-tblgen/op-format.td b/mlir/test/mlir-tblgen/op-format.td
index 09e068b91a40b..93d1fc9ba1f8d 100644
--- a/mlir/test/mlir-tblgen/op-format.td
+++ b/mlir/test/mlir-tblgen/op-format.td
@@ -109,3 +109,24 @@ def OptionalGroupC : TestFormat_Op<[{
def OptionalGroupD : TestFormat_Op<[{
(custom<Custom>($a, $b)^)? attr-dict
}], [AttrSizedOperandSegments]>, Arguments<(ins Optional<I64>:$a, Optional<I64>:$b)>;
+
+
+// CHECK-LABEL: RegionRef::parse
+// CHECK: auto odsResult = parseCustom(parser, *bodyRegion);
+// CHECK-LABEL: RegionRef::print
+// CHECK: printCustom(_odsPrinter, *this, getBody());
+def RegionRef : TestFormat_Op<[{
+ $body custom<Custom>(ref($body)) attr-dict
+}]> {
+ let regions = (region AnyRegion:$body);
+}
+
+// CHECK-LABEL: SuccessorRef::parse
+// CHECK: auto odsResult = parseCustom(parser, successorSuccessor);
+// CHECK-LABEL: SuccessorRef::print
+// CHECK: printCustom(_odsPrinter, *this, getSuccessor());
+def SuccessorRef : TestFormat_Op<[{
+ $successor custom<Custom>(ref($successor)) attr-dict
+}]> {
+ let successors = (successor AnySuccessor:$successor);
+}
diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
index 11edf2523f1aa..8044188c05fac 100644
--- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpFormatGen.cpp
@@ -3376,11 +3376,13 @@ OpFormatParser::parseVariableImpl(SMLoc loc, StringRef name, Context ctx) {
if (ctx == TopLevelContext || ctx == CustomDirectiveContext) {
if (hasAllRegions || !seenRegions.insert(region).second)
return emitError(loc, "region '" + name + "' is already bound");
- } else if (ctx == RefDirectiveContext && !seenRegions.count(region)) {
- return emitError(loc, "region '" + name +
- "' must be bound before it is referenced");
+ } else if (ctx == RefDirectiveContext) {
+ if (!seenRegions.count(region))
+ return emitError(loc, "region '" + name +
+ "' must be bound before it is referenced");
} else {
- return emitError(loc, "regions can only be used at the top level");
+ return emitError(loc, "regions can only be used at the top level "
+ "or in a ref directive");
}
return create<RegionVariable>(region);
}
@@ -3396,11 +3398,13 @@ OpFormatParser::parseVariableImpl(SMLoc loc, StringRef name, Context ctx) {
if (ctx == TopLevelContext || ctx == CustomDirectiveContext) {
if (hasAllSuccessors || !seenSuccessors.insert(successor).second)
return emitError(loc, "successor '" + name + "' is already bound");
- } else if (ctx == RefDirectiveContext && !seenSuccessors.count(successor)) {
- return emitError(loc, "successor '" + name +
- "' must be bound before it is referenced");
+ } else if (ctx == RefDirectiveContext) {
+ if (!seenSuccessors.count(successor))
+ return emitError(loc, "successor '" + name +
+ "' must be bound before it is referenced");
} else {
- return emitError(loc, "successors can only be used at the top level");
+ return emitError(loc, "successors can only be used at the top level "
+ "or in a ref directive");
}
return create<SuccessorVariable>(successor);
|
Previously, references to regions and successors were incorrectly disallowed outside the top-level assembly form. This change enables the use of bound regions and successors as variables in custom directives.
9719d94
to
43e2282
Compare
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.
Makes sense to me!
Could you add an op that exercises this in the test dialect as well?
The tests currently only verify that the generated code by ODS looks as expected, but doesn't compile it using the C++ compiler
Previously, references to regions and successors were incorrectly disallowed outside the top-level assembly form. This change enables the use of bound regions and successors as variables in custom directives.