How to register a dialect and op?

Viewed 210

1.在查看#issue 80 的时候,现在会报错误:1682215707058.png
错误是在python中调用 self.module = mlir.ir.Module.parse(context, self.ctx)报出的,尝试查找错误来源,可能是在下面的地方报错:
image.png

想请教的2个问题:
1.dialect和operation除了在ODS中定义,是否需要做register,如果需要应该如何做?
2.mlir 文件中的多数算子是带有引号,也有不带引号的算子,例如:tpu.Yield算子,请问有引和没有引号的算子有什么区别,定义算子时,哪些地方会导致生成的mlir文件中算子有无引号差别?
1682216179012.png

1 Answers

MLIR提供标准方式打印和自定义打印,标准方式的打印会使用generic格式来描述算子,

string-literal `(` value-use-list? `)`  successor-list?
                         region-list? dictionary-attribute? `:` function-type

用户自定义打印则会使用自定义的形式:
bare-id custom-operation-format
详见LangRef/#operations

该问题是由于TPU—MLIR的python parser使用了仅包含build in dialect和少部分MLIR工程dialect(quant,function,scf,affine,tensor等)并不包含top和tpu dialect。如果输入的文件使用generic format来打印,那么是可以被MLIR的通用parser理解,但是如果有自定义格式(在TD中声明了 assemblyFormat)那么就无法被标准parser理解,需要导入cpp中定义的parser功能。

解决方法有两个:

  1. 使用generic格式输出mlir文件,如:Debugging中描述的方式
-mlir-print-op-generic prints ops in their generic form, which is isomorphic with the underlying C++ data structures. This is often useful, since invalid ops frequently will crash while being printed in their “pretty form”. Also, even if the “pretty form” can be printed, it can still be misleading for an invalid op, such as by failing to print an unexpected extra operand that got added by a buggy pass.

    This option also influences the behavior of op.dump().
  1. TPU-MLIR中的python parser注册top/tpu dialect(该功能在添加中,暂时还未完成)。

第一种方式容易实现,可以绕开当前的限制。第二种方式需要等一段时间。