<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/88506>88506</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] inferReturnTypes doesn't work with properties in ODS-generated parser
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
JoelWee
</td>
</tr>
</table>
<pre>
When we have ODS-generated assembly format and want to infer the return type, it doesn't work.
E.g. I extended TestOpWithProperties to have a return type, and to infer that return type
```
def TestOpWithProperties : TEST_Op<"with_properties", [DeclareOpInterfaceMethods<InferTypeOpInterface>]> {
let assemblyFormat = "prop-dict attr-dict";
let arguments = (ins
IntProperty<"int64_t">:$a,
StrAttr:$b, // Attributes can directly be used here.
ArrayProperty<"int64_t", 4>:$array // example of an array
);
let results = (outs
Variadic<AnyType>:$results
);
}
...
LogicalResult TestOpWithProperties::inferReturnTypes(
MLIRContext *, std::optional<Location> location, ValueRange operands,
DictionaryAttr attributes, OpaqueProperties properties, RegionRange regions,
SmallVectorImpl<Type> &inferredReturnTypes) {
TestOpWithProperties::Adaptor adaptor(operands, attributes, properties,
regions);
// Fails when trying to access the "b" operand.
if (!adaptor.getB().empty()) return failure();
return success();
}
```
Code is here as well: https://github.com/llvm/llvm-project/compare/main...JoelWee:llvm-project:piper_export_cl_624139506
When running the mlir-opt test `/llvm-project/mlir/test/IR/properties.mlir`,
we get a crash with
```
Stack dump:
0. Program arguments: mlir-opt /build/work/c7ba005c238e0434776d2e70e8f76290545d/google3/runfiles/google3/third_party/llvm/llvm-project/mlir/test/IR/properties.mlir -split-input-file
1. MLIR Parser: custom op parser 'test.with_properties'
```
===
Doing some digging, I think the following is happening:
1. The assembly format doesn't have return type, so we infer it during parsing
2. The `parseProperties` function parses the properties into operationState.propertiesAttr, but this only gets converted to a proper Properties class after parsing.
3. During parsing, we apply inferReturnTypes to get the return type, but the info is neither in properties nor attributes, it's in propertiesAttr which isn't passed in at all.
4. So parsing fails because we try to access something in properties which isn't there (yet)
The auto generated parser looks something like
```
::mlir::ParseResult TestOpWithProperties::parse(::mlir::OpAsmParser &parser, ::mlir::OperationState &result) {
if (parseProperties(parser, result))
return ::mlir::failure();
{
auto loc = parser.getCurrentLocation();(void)loc;
if (parser.parseOptionalAttrDict(result.attributes))
return ::mlir::failure();
if (failed(verifyInherentAttrs(result.name, result.attributes, [&]() {
return parser.emitError(loc) << "'" << result.name.getStringRef() << "' op ";
})))
return ::mlir::failure();
}
::llvm::SmallVector<::mlir::Type> inferredReturnTypes;
if (::mlir::failed(TestOpWithProperties::inferReturnTypes(parser.getContext(),
result.location, result.operands,
result.attributes.getDictionary(parser.getContext()),
result.getRawProperties(),
result.regions, inferredReturnTypes)))
return ::mlir::failure();
result.addTypes(inferredReturnTypes);
return ::mlir::success();
}
```
(Note for myself: to test this, I copied over the autogenerated parsers and printers and added CHECKs here and there)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykWN9v4jAS_mvMy6gROIQfDzywUHS9272u2mr3sTLJkPjq2DnbKct_fxongQTaXa2uqiA49nwzn8czXyKck7lGXLHkC0u2I1H7wtjVPw2qn4ijvclOq58FajgiFOId4XH7fJejRis8ZiCcw3KvTnAwthQehM7gKLQHb0DqA1rwBYJFX1sN_lQh4xuQHjKDTjM-93A09i1i4y0br5vP-yiP4AHwl0edYQYv6Pxj9VP64rs1FVov0ZH54I24tk0O9LCFH0zowbDZuP0PPzM8fIzE4jW83D-_vD5WLN4wzo_SF6_VeQLjnHBZ8mWLqRIWH6sH7dEeRIrf0BcmcyzePJA_L6eqf5fF9yzZsvge2PxL4wWAQn8mdddwyuItMM4J8i6TqQfhvQ1XhB0Pl9q8LlF7165aSO3a-w_at2GdmkCk9rPpa2PknsVrxqeC8U1nDuDZ27X3trm1D1HyHeM7oFG5rz06SIWGTFpMvTrBHqF2mEGBFqOLnbW14vQpNt_A9OIATe1g8J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