Skip to content

Commit f69a9f3

Browse files
committed
[mlir][ODS] Add support for passing properties to ref in custom
This is essentially a follow up to https://reviews.llvm.org/D155072 This adds support for also passing properties as `ref` parameter to `custom`. This requires the property to have been bound previously and will error otherwise. This makes it possible for an implementation of `custom` to take previously parsed data into account, creating nice context-dependent grammars :-) Differential Revision: https://reviews.llvm.org/D155297
1 parent feaf70b commit f69a9f3

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

mlir/test/IR/properties.mlir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ test.with_wrapped_properties <{prop = "content for properties"}>
2424
// GENERIC: "test.using_property_in_custom"()
2525
// GENERIC-SAME: prop = array<i64: 1, 4, 20>
2626
test.using_property_in_custom [1, 4, 20]
27+
28+
// CHECK: test.using_property_ref_in_custom
29+
// CHECK-SAME: 1 + 4 = 5
30+
// GENERIC: "test.using_property_ref_in_custom"()
31+
// GENERIC-SAME: <{
32+
// GENERIC-SAME: first = 1
33+
// GENERIC-SAME: second = 4
34+
// GENERIC-SAME: }>
35+
test.using_property_ref_in_custom 1 + 4 = 5

mlir/test/lib/Dialect/Test/TestDialect.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,34 @@ static void printUsingPropertyInCustom(OpAsmPrinter &printer, Operation *op,
19451945
printer << '[' << value << ']';
19461946
}
19471947

1948+
static bool parseIntProperty(OpAsmParser &parser, int64_t &value) {
1949+
return failed(parser.parseInteger(value));
1950+
}
1951+
1952+
static void printIntProperty(OpAsmPrinter &printer, Operation *op,
1953+
int64_t value) {
1954+
printer << value;
1955+
}
1956+
1957+
static bool parseSumProperty(OpAsmParser &parser, int64_t &second,
1958+
int64_t first) {
1959+
int64_t sum;
1960+
auto loc = parser.getCurrentLocation();
1961+
if (parser.parseInteger(second) || parser.parseEqual() ||
1962+
parser.parseInteger(sum))
1963+
return true;
1964+
if (sum != second + first) {
1965+
parser.emitError(loc, "Expected sum to equal first + second");
1966+
return true;
1967+
}
1968+
return false;
1969+
}
1970+
1971+
static void printSumProperty(OpAsmPrinter &printer, Operation *op,
1972+
int64_t second, int64_t first) {
1973+
printer << second << " = " << (second + first);
1974+
}
1975+
19481976
#include "TestOpEnums.cpp.inc"
19491977
#include "TestOpInterfaces.cpp.inc"
19501978
#include "TestTypeInterfaces.cpp.inc"

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,6 +3349,11 @@ def TestOpUsingPropertyInCustom : TEST_Op<"using_property_in_custom"> {
33493349
let arguments = (ins ArrayProperty<"int64_t", 3>:$prop);
33503350
}
33513351

3352+
def TestOpUsingPropertyRefInCustom : TEST_Op<"using_property_ref_in_custom"> {
3353+
let assemblyFormat = "custom<IntProperty>($first) `+` custom<SumProperty>($second, ref($first)) attr-dict";
3354+
let arguments = (ins IntProperty<"int64_t">:$first, IntProperty<"int64_t">:$second);
3355+
}
3356+
33523357
// Op with a properties struct defined out-of-line. The struct has custom
33533358
// printer/parser.
33543359

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,11 @@ def VariableInvalidO : TestFormat_Op<[{
483483
custom<Test>($prop, $prop) attr-dict
484484
}]>, Arguments<(ins IntProperty<"int64_t">:$prop)>;
485485

486+
// CHECK: error: property 'prop' must be bound before it is referenced
487+
def VariableInvalidP : TestFormat_Op<[{
488+
custom<Test>(ref($prop)) attr-dict
489+
}]>, Arguments<(ins IntProperty<"int64_t">:$prop)>;
490+
486491
//===----------------------------------------------------------------------===//
487492
// Coverage Checks
488493
//===----------------------------------------------------------------------===//

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,12 +2957,18 @@ OpFormatParser::parseVariableImpl(SMLoc loc, StringRef name, Context ctx) {
29572957
}
29582958

29592959
if (const NamedProperty *property = findArg(op.getProperties(), name)) {
2960-
if (ctx != CustomDirectiveContext)
2960+
if (ctx != CustomDirectiveContext && ctx != RefDirectiveContext)
29612961
return emitError(
29622962
loc, "properties currently only supported in `custom` directive");
29632963

2964-
if (!seenProperties.insert(property).second)
2965-
return emitError(loc, "property '" + name + "' is already bound");
2964+
if (ctx == RefDirectiveContext) {
2965+
if (!seenProperties.count(property))
2966+
return emitError(loc, "property '" + name +
2967+
"' must be bound before it is referenced");
2968+
} else {
2969+
if (!seenProperties.insert(property).second)
2970+
return emitError(loc, "property '" + name + "' is already bound");
2971+
}
29662972

29672973
return create<PropertyVariable>(property);
29682974
}

0 commit comments

Comments
 (0)