Skip to content

Commit 9719d94

Browse files
committed
[mlir][tblgen] Fix region and successor references in custom directives
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.
1 parent f90025e commit 9719d94

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

mlir/test/mlir-tblgen/op-format-spec.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ def DirectiveCustomValidD : TestFormat_Op<[{
4949
def DirectiveCustomValidE : TestFormat_Op<[{
5050
custom<MyDirective>(prop-dict) attr-dict
5151
}]>, Arguments<(ins UnitAttr:$flag)>;
52+
def DirectiveCustomValidF : TestFormat_Op<[{
53+
$operand custom<MyDirective>(ref($operand)) attr-dict
54+
}]>, Arguments<(ins Optional<I64>:$operand)>;
55+
def DirectiveCustomValidG : TestFormat_Op<[{
56+
$body custom<MyDirective>(ref($body)) attr-dict
57+
}]> {
58+
let regions = (region AnyRegion:$body);
59+
}
60+
def DirectiveCustomValidH : TestFormat_Op<[{
61+
$successor custom<MyDirective>(ref($successor)) attr-dict
62+
}]> {
63+
let successors = (successor AnySuccessor:$successor);
64+
}
5265

5366
//===----------------------------------------------------------------------===//
5467
// functional-type

mlir/test/mlir-tblgen/op-format.td

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,24 @@ def OptionalGroupC : TestFormat_Op<[{
109109
def OptionalGroupD : TestFormat_Op<[{
110110
(custom<Custom>($a, $b)^)? attr-dict
111111
}], [AttrSizedOperandSegments]>, Arguments<(ins Optional<I64>:$a, Optional<I64>:$b)>;
112+
113+
114+
// CHECK-LABEL: RegionRef::parse
115+
// CHECK: auto odsResult = parseCustom(parser, *bodyRegion);
116+
// CHECK-LABEL: RegionRef::print
117+
// CHECK: printCustom(_odsPrinter, *this, getBody());
118+
def RegionRef : TestFormat_Op<[{
119+
$body custom<Custom>(ref($body)) attr-dict
120+
}]> {
121+
let regions = (region AnyRegion:$body);
122+
}
123+
124+
// CHECK-LABEL: SuccessorRef::parse
125+
// CHECK: auto odsResult = parseCustom(parser, successorSuccessor);
126+
// CHECK-LABEL: SuccessorRef::print
127+
// CHECK: printCustom(_odsPrinter, *this, getSuccessor());
128+
def SuccessorRef : TestFormat_Op<[{
129+
$successor custom<Custom>(ref($successor)) attr-dict
130+
}]> {
131+
let successors = (successor AnySuccessor:$successor);
132+
}

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,11 +3376,13 @@ OpFormatParser::parseVariableImpl(SMLoc loc, StringRef name, Context ctx) {
33763376
if (ctx == TopLevelContext || ctx == CustomDirectiveContext) {
33773377
if (hasAllRegions || !seenRegions.insert(region).second)
33783378
return emitError(loc, "region '" + name + "' is already bound");
3379-
} else if (ctx == RefDirectiveContext && !seenRegions.count(region)) {
3380-
return emitError(loc, "region '" + name +
3381-
"' must be bound before it is referenced");
3379+
} else if (ctx == RefDirectiveContext) {
3380+
if (!seenRegions.count(region))
3381+
return emitError(loc, "region '" + name +
3382+
"' must be bound before it is referenced");
33823383
} else {
3383-
return emitError(loc, "regions can only be used at the top level");
3384+
return emitError(loc, "regions can only be used at the top level "
3385+
"or in a ref directive");
33843386
}
33853387
return create<RegionVariable>(region);
33863388
}
@@ -3396,11 +3398,13 @@ OpFormatParser::parseVariableImpl(SMLoc loc, StringRef name, Context ctx) {
33963398
if (ctx == TopLevelContext || ctx == CustomDirectiveContext) {
33973399
if (hasAllSuccessors || !seenSuccessors.insert(successor).second)
33983400
return emitError(loc, "successor '" + name + "' is already bound");
3399-
} else if (ctx == RefDirectiveContext && !seenSuccessors.count(successor)) {
3400-
return emitError(loc, "successor '" + name +
3401-
"' must be bound before it is referenced");
3401+
} else if (ctx == RefDirectiveContext) {
3402+
if (!seenSuccessors.count(successor))
3403+
return emitError(loc, "successor '" + name +
3404+
"' must be bound before it is referenced");
34023405
} else {
3403-
return emitError(loc, "successors can only be used at the top level");
3406+
return emitError(loc, "successors can only be used at the top level "
3407+
"or in a ref directive");
34043408
}
34053409

34063410
return create<SuccessorVariable>(successor);

0 commit comments

Comments
 (0)