SumTypes Implementation with MLIR

I’m trying to implement Sum Types in my MLIR dialect. Typically, it would look something like this: !SumType = !adt.type<Small+Big> . In this case, Small and Big are the constructors for my new SumType. However, I’m unsure about the type representation for these constructors. I’ve looked through the Builtin dialect and other available types, but haven’t found a suitable option. Should I create a new type specifically for these constructors, or is there an existing type in MLIR that I can utilize?

The toy tutorial has an aggregate:

An enum is probably a value and some representation, which value is active.

I have already seen the example of the type Toy. I don’t think you understood my question. For example, the constructor Small, how am I going to store it? I will store Small as a string type. The problem is about the constructor and not about the sum types.

You probably still have to define an Enum type with at least a value.
Then you have constructor ops for Small and Big.

You have an idea, of what I could do in this constructor ops ?

Access and Constructor ops for the aggregate in the toy dialect:

Here for example, the variable in a struct implementation could have two types either a TensorType or a StructType. But In my example, the Small and Big in what mlir type should be represented with ? I cannot create a Small and Big type, because I should have an impelementation that deals with all SumTypes with a different number of constructor (in the example that I gave it was 2, but it could be 4 or 5), and also with different names (I could have something like <Red+Black+Green>). I don’t know if you understand more my question now.

The constant op has an attribute attached to it. You could attach StringAttr to the constant op and have Red, Black, and Green as strings.

Just a question about the StringAttribute. I got the following error, I tried to include the BuitltinAtttributes.td and BuiltinAttributeInterfaces.td. Can please someone tell me what’s the right include ?

let arguments = (ins Builtin_StringAttr:$value);

That works for C-ish enums. Enums with payload are a different story:

    enum Enum2 {
        Unit,
        Tuple(u16),
        Struct,
    }

In fact, I declared a constant Operation for my sum types. And I added the “AdtOps.cpp.inc” with function add_operation in MLIR. But I got this weird error, telling that there’s a problem with the fact that the operation that I declared isn’t a member of mlir::adt. Should I declare my operation in a specific way ?

Header:
#define GET_OP_CLASSES
#include “Hir/HirOps.h.inc”
Cpp File:
#define GET_OP_CLASSES
#include “Hir/HirOps.cpp.inc”

and

void HirDialect::initialize() {
  addOperations<
#define GET_OP_LIST
#include "Hir/HirOps.cpp.inc"
      >();
}
1 Like

After trying coding an operation, I’m little bit confused, because I tried to wrote a file in mlir (as you can see in the screenshot), but I’m not sure if it’s the correct format.

Are you trying enum with payload?
Did you print the attributes?

It’s my first operation, so I don’t know how to manipulate it, I tried to do something close to what I have seen in some examples. But, I don’t know where for example my string attribute are stored, and how I can get access to them.
Screenshot from 2023-06-20 16-27-44