Question from a beginner: Is it possible to traverse a dialect to print out the operations without matching to the AST?

I want to print out the ops in a dialect. I saw that there is a “getRegisteredOperations()” function in MLIRContext, but from my understanding, it returns an array of the operation name instead of the operations. Is it possible to traverse the dialect to get the operations? Otherwise, is it possible to set up an iterator to iterate every operation in the dialect and use dump or other print function to print it out?

For example, in the toy tutorial, instead of dumping the mlir, is it possible to traverse the ToyDialect to print the ops, such as ConstantOps, TransposeOp, etc.?

This is correct, note that the RegisteredOperationName (as well as the base class OperationName) is providing a bit more information than the “name” alone (like if an op has a traits or not for example).

This does not really make sense: an “operation” exists only as an instance in the IR: that is you needs operands, results, types, attributes which are each specific to each instance of an operation.
A good analogy would be to say that the registration is like writing a C++ class (it is a “template” to create the object), while each operation will be a specific instance of it (which are not known by the dialect at all).

Hey,

Welcome! Perhaps it could help if you expanded the example more. Im unsure if you are asking to have all operations in dialect printed, all operations in the input of a given dialect, or something else.

Thanks

I see. Thanks! Then, is it possible to use the builder to create the operations one by one without matching them to the ast in MLIRGen?

You can create operation as you want, but you need to know the contraints of an operation (how many operands, restrictions on types, necessary attributes, etc.) to be able to build one.

It is pretty rare that we need to do this, mostly because you get the initial IR from either the AST or from a .mlir file, and then passes are creating ops when transforming this IR.

Here is a small C++ unit-test creating IR “out-of-thin-air” as an example:

I see. Then I am thinking about saving only one operation in the AST so that the eventual IR will have only one operation. I noticed that the parser parses functions first and looks into the expressions in the functions. Is it possible to jump the function and only save one of the operations? Take the toy tutorial as an example. Can I save only the transpose operation in the multiply-transpose function so that the final IR will only have “toy.transpose” (%arg0) : (tensor<*xf64>) → tensor<*xf64>"?

Where would %arg0 come from?