[ODS] Question about `TypesMatchWith`

Hi all,

I have an ‘rtl.read_inout’ operation that takes an operand of “!rtl.inout” type and returns a value of type T, but rejects some T’s.

In this patch, I switched to using TypesMatchWith to allow ODS to know that the types are bound and how to relate them, this allows this syntax:

  %x = rtl.read_inout %y : i32

to know that the %y operand is an !rtl.inout<i32> and the result is i32. This is pretty nice, and very declarative which I enjoy.

However, !rtl.inout type doesn’t allow all subelement types, notable it doesn’t allow functions or other inout types (these are rejected in InOutType::verifyConstructionInvariants), btw InOutType is generated by ODS.

The problem here is that TypesMatchWith doesn’t allow the predicate to fail. I hacked this a bit by having the transform function return a null type, which causes a static error, but this isn’t really right. Is there a better way to get a nice error message out? You can see a testcase at the bottom of the patch.

One other random question for @jdd: the generated InOutType::get() creation method is synthesized with both a context argument as well as an element type. Is it possible to teach tblgen to know that the context can always be obtained from the element type with element.getContext(), eliminating the need for the context argument?

Thanks!

-Chris

In the general case, no. Non-parameterized types and types with only primitive parameters don’t get passed anything which has a .getContext() method. It would be possible to detect the conditions under which the context can be omitted, though the ODS code currently doesn’t look at the type string and adding that functionality would add significant complexity. I also appreciate the consistency.

I’d appreciate handling the case when there are subtypes that get passed in, since that is something that is already known by the pattern and is extremely common. While consistency can sometimes be helpful, in this case it is just boilerplate which requires me to find a way to specify a custom builder/get.

If I need to specify a custom get method, how do I do that?

-Chris

There’s an extraClassDeclaration field you can use.

The way to do this is to ensure that the argument/result types are properly constrained in ODS. The LHS side of TypesMatchWith is verified before it is used in the autogenerated parser, so if you’ve properly constrained the LHS it shouldn’t be possible for it to fail. It seems that in your patch, you still have the input/result type marked as AnyType. If you move isRTLValueType to a TypeConstraint and attach it to result, you should get proper errors/no crashes.

I agree here that this is a somewhat common use case. I plan to try moving over the builtin types to ODS soonish, which means that these types of things will need to be addressed as those types get moved over.

– River

That worked great, thank you!

That worked! It would be really nice for ODS to generate these overloads, since it is so common for types to take other types, but this totally works.

Thanks both!

-Chris